1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has
27 (defvar mail-use-rfc822 nil
"\
28 *If non-nil, use a full, hairy RFC822 parser on mail addresses.
29 Otherwise, (the default) use a smaller, somewhat faster, and
30 often correct parser.")
32 (defun mail-string-delete (string start end
)
33 "Returns a string containing all of STRING except the part
34 from START (inclusive) to END (exclusive)."
35 (if (null end
) (substring string
0 start
)
36 (concat (substring string
0 start
)
37 (substring string end nil
))))
39 (defun mail-strip-quoted-names (address)
40 "Delete comments and quoted strings in an address list ADDRESS.
41 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
42 Return a modified address list."
46 (progn (require 'rfc822
)
47 (mapconcat 'identity
(rfc822-addresses address
) ", "))
49 (string-match "\\`[ \t\n]*" address
)
50 ;; strip surrounding whitespace
51 (setq address
(substring address
53 (string-match "[ \t\n]*\\'" address
56 ;; Detect nested comments.
57 (if (string-match "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*(" address
)
58 ;; Strip nested comments.
60 (set-buffer (get-buffer-create " *temp*"))
63 (set-syntax-table lisp-mode-syntax-table
)
65 (while (search-forward "(" nil t
)
67 (skip-chars-backward " \t")
68 (delete-region (point)
69 (save-excursion (forward-sexp 1) (point))))
70 (setq address
(buffer-string))
72 ;; Strip non-nested comments an easier way.
73 (while (setq pos
(string-match
74 ;; This doesn't hack rfc822 nested comments
75 ;; `(xyzzy (foo) whinge)' properly. Big deal.
76 "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*)"
79 (mail-string-delete address
82 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
84 (while (setq pos
(string-match
85 "[ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*"
87 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
88 (if (and (> (length address
) (match-end 0))
89 (= (aref address
(match-end 0)) ?
@))
90 (setq pos
(match-end 0))
92 (mail-string-delete address
94 ;; Retain only part of address in <> delims, if there is such a thing.
95 (while (setq pos
(string-match "\\(,\\|\\`\\)[^,]*<\\([^>,]*>\\)"
97 (let ((junk-beg (match-end 1))
98 (junk-end (match-beginning 2))
99 (close (match-end 0)))
100 (setq address
(mail-string-delete address
(1- close
) close
))
101 (setq address
(mail-string-delete address junk-beg junk-end
))))
104 (or (and (boundp 'rmail-default-dont-reply-to-names
)
105 (not (null rmail-default-dont-reply-to-names
)))
106 (setq rmail-default-dont-reply-to-names
"info-"))
108 ; rmail-dont-reply-to-names is defined in loaddefs
109 (defun rmail-dont-reply-to (userids)
110 "Returns string of mail addresses USERIDS sans any recipients
111 that start with matches for `rmail-dont-reply-to-names'.
112 Usenet paths ending in an element that matches are removed also."
113 (if (null rmail-dont-reply-to-names
)
114 (setq rmail-dont-reply-to-names
115 (concat (if rmail-default-dont-reply-to-names
116 (concat rmail-default-dont-reply-to-names
"\\|")
118 (concat (regexp-quote (user-original-login-name))
120 (let ((match (concat "\\(^\\|,\\)[ \t\n]*\\([^,\n]*!\\|\\)\\("
121 rmail-dont-reply-to-names
125 (while (setq pos
(string-match match userids
))
126 (if (> pos
0) (setq pos
(1+ pos
)))
128 (if (string-match "[ \t\n,]+" userids
(match-end 0))
134 ;; get rid of any trailing commas
135 (if (setq pos
(string-match "[ ,\t\n]*\\'" userids
))
136 (setq userids
(substring userids
0 pos
)))
137 ;; remove leading spaces. they bother me.
138 (if (string-match "\\s *" userids
)
139 (substring userids
(match-end 0))
142 (defun mail-fetch-field (field-name &optional last all
)
143 "Return the value of the header field FIELD-NAME.
144 The buffer is expected to be narrowed to just the headers of the message.
145 If second arg LAST is non-nil, use the last such field if there are several.
146 If third arg ALL is non-nil, concatenate all such fields with commas between."
148 (goto-char (point-min))
149 (let ((case-fold-search t
)
150 (name (concat "^" (regexp-quote field-name
) "[ \t]*:[ \t]*")))
151 (goto-char (point-min))
154 (while (re-search-forward name nil t
)
155 (let ((opoint (point)))
156 (while (progn (forward-line 1)
157 (looking-at "[ \t]")))
158 (setq value
(concat value
159 (if (string= value
"") "" ", ")
160 (buffer-substring opoint
(1- (point)))))))
161 (and (not (string= value
"")) value
))
162 (if (re-search-forward name nil t
)
164 (if last
(while (re-search-forward name nil t
)))
165 (let ((opoint (point)))
166 (while (progn (forward-line 1)
167 (looking-at "[ \t]")))
168 (buffer-substring opoint
(1- (point))))))))))
170 ;; Parse a list of tokens separated by commas.
171 ;; It runs from point to the end of the visible part of the buffer.
172 ;; Whitespace before or after tokens is ignored,
173 ;; but whitespace within tokens is kept.
174 (defun mail-parse-comma-list ()
177 (skip-chars-forward " ")
180 (skip-chars-forward "^,")
181 (skip-chars-backward " ")
183 (cons (buffer-substring beg
(point))
185 (skip-chars-forward "^,")
186 (skip-chars-forward ", "))
189 (defun mail-comma-list-regexp (labels)
191 (setq pos
(or (string-match "[^ \t]" labels
) 0))
192 ;; Remove leading and trailing whitespace.
193 (setq labels
(substring labels pos
(string-match "[ \t]*$" labels pos
)))
194 ;; Change each comma to \|, and flush surrounding whitespace.
195 (while (setq pos
(string-match "[ \t]*,[ \t]*" labels
))
197 (concat (substring labels
0 pos
)
199 (substring labels
(match-end 0))))))
202 (provide 'mail-utils
)
204 ;;; mail-utils.el ends here