Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / mail / mail-utils.el
1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
2
3 ;; Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail, news
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; Utility functions for mail and netnews handling. These handle fine
26 ;; points of header parsing.
27
28 ;;; Code:
29
30 ;;;###autoload
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."
35 :type 'boolean
36 :group 'mail)
37
38 ;; Returns t if file FILE is an Rmail file.
39 ;;;###autoload
40 (defun mail-file-babyl-p (file)
41 "Return non-nil if FILE is a Babyl file."
42 (with-temp-buffer
43 (insert-file-contents file nil 0 100)
44 (looking-at "BABYL OPTIONS:")))
45
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))))
52
53 ;;;###autoload
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 ""))
59 (save-match-data
60 (while (string-match "[?=\"\200-\377]" string i)
61 (setq result
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)))
66 (if wrapper
67 (concat "=?ISO-8859-1?Q?"
68 result (substring string i)
69 "?=")
70 (concat result (substring string i))))))
71
72 ;;;###autoload
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?....?=."
77 (interactive "r\nP")
78 (save-match-data
79 (save-excursion
80 (goto-char beg)
81 (save-restriction
82 (narrow-to-region beg end)
83 (while (re-search-forward "[?=\"\200-\377]" nil t)
84 (replace-match (upcase (format "=%02x" (preceding-char)))
85 t t))
86 (when wrapper
87 (goto-char beg)
88 (insert "=?ISO-8859-1?Q?")
89 (goto-char end)
90 (insert "?="))))))
91
92 (defun mail-unquote-printable-hexdigit (char)
93 (setq char (upcase char))
94 (if (>= char ?A)
95 (+ (- char ?A) 10)
96 (- char ?0)))
97
98 ;;;###autoload
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?....?=."
103 (save-match-data
104 (and wrapper
105 (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string)
106 (setq string (match-string 1 string)))
107 (let ((i 0) strings)
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)
111 (setq strings
112 (cons (make-string 1
113 (+ (* 16 (mail-unquote-printable-hexdigit
114 (aref string (match-beginning 1))))
115 (mail-unquote-printable-hexdigit
116 (aref string (1+ (match-beginning 1))))))
117 strings)))
118 (setq i (match-end 0)))
119 (apply 'concat (nreverse (cons (substring string i) strings))))))
120
121 ;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el.
122 ;;;###autoload
123 (defun mail-unquote-printable-region (beg end &optional wrapper noerror
124 unibyte)
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,
133 as Rmail does."
134 ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1
135 ;; specific handling should be removed (or moved elsewhere and generalized).
136 (interactive "r\nP")
137 (let (failed)
138 (save-match-data
139 (save-excursion
140 (save-restriction
141 (narrow-to-region beg end)
142 (goto-char (point-min))
143 (when (and wrapper
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)
150 (replace-match ""))
151 ((= (char-after (match-beginning 1)) ?=)
152 (replace-match "="))
153 ((match-beginning 2)
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)))))))
158 (if unibyte
159 (progn
160 (replace-match "")
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))))
165 (noerror
166 (setq failed t))
167 (t
168 (error "Malformed MIME quoted-printable message"))))
169 (not failed))))))
170
171 (eval-when-compile (require 'rfc822))
172
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."
177 (if (null address)
178 nil
179 (if mail-use-rfc822
180 (progn (require 'rfc822)
181 (mapconcat 'identity (rfc822-addresses address) ", "))
182 (let (pos)
183
184 ;; Strip comments.
185 (while (setq pos (string-match
186 "[ \t]*(\\([^()\\]\\|\\\\.\\|\\\\\n\\)*)"
187 address))
188 (setq address (replace-match "" nil nil address 0)))
189
190 ;; strip surrounding whitespace
191 (string-match "\\`[ \t\n]*" address)
192 (setq address (substring address
193 (match-end 0)
194 (string-match "[ \t\n]*\\'" address
195 (match-end 0))))
196
197 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
198 (setq pos 0)
199 (while (setq pos (string-match
200 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
201 address pos))
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-*,\\|\\'\\)"
211 address))
212 (setq address (replace-match (match-string 3 address)
213 nil 'literal address 2)))
214 address))))
215
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 "\\|")
234 "")
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
240 ;; "barfoo@baz.com".
241 (concat "\\`"
242 (regexp-quote user-mail-address)
243 "\\'\\|")
244 "")
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))
249 (while start-pos
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))))
254 (if next-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
268 (1+ cur-pos))))
269 cur-pos start-pos)
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)))
274 (if pos
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))
279 destinations))
280
281 \f
282 ;;;###autoload
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."
290 (save-excursion
291 (goto-char (point-min))
292 (let ((case-fold-search t)
293 (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
294 (if (or all list)
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
301 (forward-char -1)
302 (skip-chars-backward " \t" opoint)
303 (if list
304 (setq value (cons (buffer-substring-no-properties
305 opoint (point))
306 value))
307 (setq value (concat value
308 (if (string= value "") "" ", ")
309 (buffer-substring-no-properties
310 opoint (point)))))))
311 (if list
312 value
313 (and (not (string= value "")) value)))
314 (if (re-search-forward name nil t)
315 (progn
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
321 (forward-char -1)
322 (skip-chars-backward " \t" opoint)
323 (buffer-substring-no-properties opoint (point)))))))))
324 \f
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 ()
330 (let (accumulated
331 beg)
332 (skip-chars-forward " \t\n")
333 (while (not (eobp))
334 (setq beg (point))
335 (skip-chars-forward "^,")
336 (skip-chars-backward " \t\n")
337 (setq accumulated
338 (cons (buffer-substring-no-properties beg (point))
339 accumulated))
340 (skip-chars-forward "^,")
341 (skip-chars-forward ", \t\n"))
342 accumulated))
343
344 (defun mail-comma-list-regexp (labels)
345 (let (pos)
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))
351 (setq labels
352 (concat (substring labels 0 pos)
353 "\\|"
354 (substring labels (match-end 0))))))
355 labels)
356 \f
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))))
361
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))))
371
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")
379 "unknown")))
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)
387 from)
388 (or (and date
389 (ignore-errors
390 (current-time-string (date-to-time date))))
391 (current-time-string)))))
392
393 (provide 'mail-utils)
394
395 ;;; mail-utils.el ends here