1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
3 ;; Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc.
6 ;; Keywords: mail, news
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Utility functions for mail and netnews handling. These handle fine
26 ;; points of header parsing.
31 (defcustom mail-use-rfc822 nil
32 "If non-nil, use a full, hairy RFC822 parser on mail addresses.
33 Otherwise, (the default) use a smaller, somewhat faster, and
34 often correct parser."
38 ;; Returns t if file FILE is an Rmail file.
40 (defun mail-file-babyl-p (file)
41 "Return non-nil if FILE is a Babyl file."
43 (insert-file-contents file nil
0 100)
44 (looking-at "BABYL OPTIONS:")))
46 (defun mail-string-delete (string start end
)
47 "Returns a string containing all of STRING except the part
48 from START (inclusive) to END (exclusive)."
49 (if (null end
) (substring string
0 start
)
50 (concat (substring string
0 start
)
51 (substring string end nil
))))
54 (defun mail-quote-printable (string &optional wrapper
)
55 "Convert a string to the \"quoted printable\" Q encoding.
56 If the optional argument WRAPPER is non-nil,
57 we add the wrapper characters =?ISO-8859-1?Q?....?=."
58 (let ((i 0) (result ""))
60 (while (string-match "[?=\"\200-\377]" string i
)
62 (concat result
(substring string i
(match-beginning 0))
63 (upcase (format "=%02x"
64 (aref string
(match-beginning 0))))))
65 (setq i
(match-end 0)))
67 (concat "=?ISO-8859-1?Q?"
68 result
(substring string i
)
70 (concat result
(substring string i
))))))
73 (defun mail-quote-printable-region (beg end
&optional wrapper
)
74 "Convert the region to the \"quoted printable\" Q encoding.
75 If the optional argument WRAPPER is non-nil,
76 we add the wrapper characters =?ISO-8859-1?Q?....?=."
82 (narrow-to-region beg end
)
83 (while (re-search-forward "[?=\"\200-\377]" nil t
)
84 (replace-match (upcase (format "=%02x" (preceding-char)))
88 (insert "=?ISO-8859-1?Q?")
92 (defun mail-unquote-printable-hexdigit (char)
93 (setq char
(upcase char
))
99 (defun mail-unquote-printable (string &optional wrapper
)
100 "Undo the \"quoted printable\" encoding.
101 If the optional argument WRAPPER is non-nil,
102 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=."
105 (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string
)
106 (setq string
(match-string 1 string
)))
108 (while (string-match "=\\(..\\|\n\\)" string i
)
109 (setq strings
(cons (substring string i
(match-beginning 0)) strings
))
110 (unless (= (aref string
(match-beginning 1)) ?
\n)
113 (+ (* 16 (mail-unquote-printable-hexdigit
114 (aref string
(match-beginning 1))))
115 (mail-unquote-printable-hexdigit
116 (aref string
(1+ (match-beginning 1))))))
118 (setq i
(match-end 0)))
119 (apply 'concat
(nreverse (cons (substring string i
) strings
))))))
121 ;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el.
123 (defun mail-unquote-printable-region (beg end
&optional wrapper noerror
125 "Undo the \"quoted printable\" encoding in buffer from BEG to END.
126 If the optional argument WRAPPER is non-nil,
127 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=.
128 On encountering malformed quoted-printable text, exits with an error,
129 unless NOERROR is non-nil, in which case it continues, and returns nil
130 when finished. Returns non-nil on successful completion.
131 If UNIBYTE is non-nil, insert converted characters as unibyte.
132 That is useful if you are going to character code decoding afterward,
134 ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1
135 ;; specific handling should be removed (or moved elsewhere and generalized).
141 (narrow-to-region beg end
)
142 (goto-char (point-min))
144 (looking-at "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?"))
145 (delete-region (match-end 1) end
)
146 (delete-region (point) (match-beginning 1)))
147 (while (re-search-forward "=\\(\\([0-9A-F][0-9A-F]\\)\\|[=\n]\\|..\\)" nil t
)
148 (goto-char (match-end 0))
149 (cond ((= (char-after (match-beginning 1)) ?
\n)
151 ((= (char-after (match-beginning 1)) ?
=)
154 (let ((char (+ (* 16 (mail-unquote-printable-hexdigit
155 (char-after (match-beginning 2))))
156 (mail-unquote-printable-hexdigit
157 (char-after (1+ (match-beginning 2)))))))
161 ;; insert-byte will insert this as a
162 ;; corresponding eight-bit character.
163 (insert-byte char
1))
164 (replace-match (make-string 1 char
) t t
))))
168 (error "Malformed MIME quoted-printable message"))))
171 (eval-when-compile (require 'rfc822
))
173 (defun mail-strip-quoted-names (address)
174 "Delete comments and quoted strings in an address list ADDRESS.
175 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
176 Return a modified address list."
180 (progn (require 'rfc822
)
181 (mapconcat 'identity
(rfc822-addresses address
) ", "))
185 (while (setq pos
(string-match
186 "[ \t]*(\\([^()\\]\\|\\\\.\\|\\\\\n\\)*)"
188 (setq address
(replace-match "" nil nil address
0)))
190 ;; strip surrounding whitespace
191 (string-match "\\`[ \t\n]*" address
)
192 (setq address
(substring address
194 (string-match "[ \t\n]*\\'" address
197 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
199 (while (setq pos
(string-match
200 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
202 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
203 (if (and (> (length address
) (match-end 0))
204 (= (aref address
(match-end 0)) ?
@))
205 (setq pos
(match-end 0))
206 ;; Otherwise discard the "..." part.
207 (setq address
(replace-match "" nil nil address
2))))
208 ;; If this address contains <...>, replace it with just
209 ;; the part between the <...>.
210 (while (setq pos
(string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
212 (setq address
(replace-match (match-string 3 address
)
213 nil
'literal address
2)))
216 ;; The following piece of ugliness is legacy code. The name was an
217 ;; unfortunate choice --- a flagrant violation of the Emacs Lisp
218 ;; coding conventions. `mail-dont-reply-to' would have been
219 ;; infinitely better. Also, `rmail-dont-reply-to-names' might have
220 ;; been better named `mail-dont-reply-to-names' and sourced from this
221 ;; file instead of in rmail.el. Yuck. -pmr
222 (defun rmail-dont-reply-to (destinations)
223 "Prune addresses from DESTINATIONS, a list of recipient addresses.
224 All addresses matching `rmail-dont-reply-to-names' are removed from
225 the comma-separated list. The pruned list is returned."
226 ;; FIXME this (setting a user option the first time a command is used)
227 ;; is somewhat strange. Normally one would never set the option,
228 ;; but instead fall back to the default so long as it was nil.
229 ;; Or just set the default directly in the defcustom.
230 (if (null rmail-dont-reply-to-names
)
231 (setq rmail-dont-reply-to-names
232 (concat (if rmail-default-dont-reply-to-names
233 (concat rmail-default-dont-reply-to-names
"\\|")
235 (if (and user-mail-address
236 (not (equal user-mail-address user-login-name
)))
237 ;; Anchor the login name and email address so
238 ;; that we don't match substrings: if the
239 ;; login name is "foo", we shouldn't match
242 (regexp-quote user-mail-address
)
245 (concat "\\`" (regexp-quote user-login-name
) "@"))))
246 ;; Split up DESTINATIONS and match each element separately.
247 (let ((start-pos 0) (cur-pos 0)
248 (case-fold-search t
))
250 (setq cur-pos
(string-match "[,\"]" destinations cur-pos
))
251 (if (and cur-pos
(equal (match-string 0 destinations
) "\""))
252 ;; Search for matching quote.
253 (let ((next-pos (string-match "\"" destinations
(1+ cur-pos
))))
255 (setq cur-pos
(1+ next-pos
))
256 ;; If the open-quote has no close-quote,
257 ;; delete the open-quote to get something well-defined.
258 ;; This case is not valid, but it can happen if things
259 ;; are weird elsewhere.
260 (setq destinations
(concat (substring destinations
0 cur-pos
)
261 (substring destinations
(1+ cur-pos
))))
262 (setq cur-pos start-pos
)))
263 (let* ((address (substring destinations start-pos cur-pos
))
264 (naked-address (mail-strip-quoted-names address
)))
265 (if (string-match rmail-dont-reply-to-names naked-address
)
266 (setq destinations
(concat (substring destinations
0 start-pos
)
267 (and cur-pos
(substring destinations
270 (setq cur-pos
(and cur-pos
(1+ cur-pos
))
271 start-pos cur-pos
))))))
272 ;; get rid of any trailing commas
273 (let ((pos (string-match "[ ,\t\n]*\\'" destinations
)))
275 (setq destinations
(substring destinations
0 pos
))))
276 ;; remove leading spaces. they bother me.
277 (if (string-match "\\(\\s \\|,\\)*" destinations
)
278 (substring destinations
(match-end 0))
283 (defun mail-fetch-field (field-name &optional last all list
)
284 "Return the value of the header field whose type is FIELD-NAME.
285 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
286 If third arg ALL is non-nil, concatenate all such fields with commas between.
287 If 4th arg LIST is non-nil, return a list of all such fields.
288 The buffer should be narrowed to just the header, else false
289 matches may be returned from the message body."
291 (goto-char (point-min))
292 (let ((case-fold-search t
)
293 (name (concat "^" (regexp-quote field-name
) "[ \t]*:[ \t]*")))
295 (let ((value (if all
"")))
296 (while (re-search-forward name nil t
)
297 (let ((opoint (point)))
298 (while (progn (forward-line 1)
299 (looking-at "[ \t]")))
300 ;; Back up over newline, then trailing spaces or tabs
302 (skip-chars-backward " \t" opoint
)
304 (setq value
(cons (buffer-substring-no-properties
307 (setq value
(concat value
308 (if (string= value
"") "" ", ")
309 (buffer-substring-no-properties
313 (and (not (string= value
"")) value
)))
314 (if (re-search-forward name nil t
)
316 (if last
(while (re-search-forward name nil t
)))
317 (let ((opoint (point)))
318 (while (progn (forward-line 1)
319 (looking-at "[ \t]")))
320 ;; Back up over newline, then trailing spaces or tabs
322 (skip-chars-backward " \t" opoint
)
323 (buffer-substring-no-properties opoint
(point)))))))))
325 ;; Parse a list of tokens separated by commas.
326 ;; It runs from point to the end of the visible part of the buffer.
327 ;; Whitespace before or after tokens is ignored,
328 ;; but whitespace within tokens is kept.
329 (defun mail-parse-comma-list ()
332 (skip-chars-forward " \t\n")
335 (skip-chars-forward "^,")
336 (skip-chars-backward " \t\n")
338 (cons (buffer-substring-no-properties beg
(point))
340 (skip-chars-forward "^,")
341 (skip-chars-forward ", \t\n"))
344 (defun mail-comma-list-regexp (labels)
346 (setq pos
(or (string-match "[^ \t]" labels
) 0))
347 ;; Remove leading and trailing whitespace.
348 (setq labels
(substring labels pos
(string-match "[ \t]*$" labels pos
)))
349 ;; Change each comma to \|, and flush surrounding whitespace.
350 (while (setq pos
(string-match "[ \t]*,[ \t]*" labels
))
352 (concat (substring labels
0 pos
)
354 (substring labels
(match-end 0))))))
357 (defun mail-rfc822-time-zone (time)
358 (let* ((sec (or (car (current-time-zone time
)) 0))
359 (absmin (/ (abs sec
) 60)))
360 (format "%c%02d%02d" (if (< sec
0) ?- ?
+) (/ absmin
60) (% absmin
60))))
362 (defun mail-rfc822-date ()
363 (let* ((time (current-time))
364 (s (current-time-string time
)))
365 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s
)
366 (concat (substring s
(match-beginning 2) (match-end 2)) " "
367 (substring s
(match-beginning 1) (match-end 1)) " "
368 (substring s
(match-beginning 4) (match-end 4)) " "
369 (substring s
(match-beginning 3) (match-end 3)) " "
370 (mail-rfc822-time-zone time
))))
372 (defun mail-mbox-from ()
373 "Return an mbox \"From \" line for the current message.
374 The buffer should be narrowed to just the header."
375 (let* ((from (mail-strip-quoted-names (or (mail-fetch-field "from")
376 (mail-fetch-field "really-from")
377 (mail-fetch-field "sender")
378 (mail-fetch-field "return-path")
380 (date (mail-fetch-field "date"))
381 ;; A From: header can contain multiple addresses, a "From "
382 ;; line must contain only one. (Bug#7760)
383 ;; See eg RFC 5322, 3.6.2. Originator Fields.
384 (end (string-match "[ \t]*[,\n]" from
)))
385 (format "From %s %s\n" (if end
386 (substring from
0 end
)
390 (current-time-string (date-to-time date
))))
391 (current-time-string)))))
393 (provide 'mail-utils
)
395 ;;; mail-utils.el ends here