Use correct match group (bug#8438).
[emacs.git] / lisp / mail / mail-utils.el
blobbbc94256fb784bcce124f5dac210a20e68b3401d
1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
3 ;; Copyright (C) 1985, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail, news
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Utility functions for mail and netnews handling. These handle fine
27 ;; points of header parsing.
29 ;;; Code:
31 ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has
32 ;;; been initialized.
33 (require 'lisp-mode)
35 ;;;###autoload
36 (defcustom mail-use-rfc822 nil
37 "If non-nil, use a full, hairy RFC822 parser on mail addresses.
38 Otherwise, (the default) use a smaller, somewhat faster, and
39 often correct parser."
40 :type 'boolean
41 :group 'mail)
43 ;; Returns t if file FILE is an Rmail file.
44 ;;;###autoload
45 (defun mail-file-babyl-p (file)
46 "Return non-nil if FILE is a Babyl file."
47 (with-temp-buffer
48 (insert-file-contents file nil 0 100)
49 (looking-at "BABYL OPTIONS:")))
51 (defun mail-string-delete (string start end)
52 "Returns a string containing all of STRING except the part
53 from START (inclusive) to END (exclusive)."
54 (if (null end) (substring string 0 start)
55 (concat (substring string 0 start)
56 (substring string end nil))))
58 ;;;###autoload
59 (defun mail-quote-printable (string &optional wrapper)
60 "Convert a string to the \"quoted printable\" Q encoding.
61 If the optional argument WRAPPER is non-nil,
62 we add the wrapper characters =?ISO-8859-1?Q?....?=."
63 (let ((i 0) (result ""))
64 (save-match-data
65 (while (string-match "[?=\"\200-\377]" string i)
66 (setq result
67 (concat result (substring string i (match-beginning 0))
68 (upcase (format "=%02x"
69 (aref string (match-beginning 0))))))
70 (setq i (match-end 0)))
71 (if wrapper
72 (concat "=?ISO-8859-1?Q?"
73 result (substring string i)
74 "?=")
75 (concat result (substring string i))))))
77 ;;;###autoload
78 (defun mail-quote-printable-region (beg end &optional wrapper)
79 "Convert the region to the \"quoted printable\" Q encoding.
80 If the optional argument WRAPPER is non-nil,
81 we add the wrapper characters =?ISO-8859-1?Q?....?=."
82 (interactive "r\nP")
83 (save-match-data
84 (save-excursion
85 (goto-char beg)
86 (save-restriction
87 (narrow-to-region beg end)
88 (while (re-search-forward "[?=\"\200-\377]" nil t)
89 (replace-match (upcase (format "=%02x" (preceding-char)))
90 t t))
91 (when wrapper
92 (goto-char beg)
93 (insert "=?ISO-8859-1?Q?")
94 (goto-char end)
95 (insert "?="))))))
97 (defun mail-unquote-printable-hexdigit (char)
98 (setq char (upcase char))
99 (if (>= char ?A)
100 (+ (- char ?A) 10)
101 (- char ?0)))
103 ;;;###autoload
104 (defun mail-unquote-printable (string &optional wrapper)
105 "Undo the \"quoted printable\" encoding.
106 If the optional argument WRAPPER is non-nil,
107 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=."
108 (save-match-data
109 (and wrapper
110 (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string)
111 (setq string (match-string 1 string)))
112 (let ((i 0) strings)
113 (while (string-match "=\\(..\\|\n\\)" string i)
114 (setq strings (cons (substring string i (match-beginning 0)) strings))
115 (unless (= (aref string (match-beginning 1)) ?\n)
116 (setq strings
117 (cons (make-string 1
118 (+ (* 16 (mail-unquote-printable-hexdigit
119 (aref string (match-beginning 1))))
120 (mail-unquote-printable-hexdigit
121 (aref string (1+ (match-beginning 1))))))
122 strings)))
123 (setq i (match-end 0)))
124 (apply 'concat (nreverse (cons (substring string i) strings))))))
126 ;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el.
127 ;;;###autoload
128 (defun mail-unquote-printable-region (beg end &optional wrapper noerror
129 unibyte)
130 "Undo the \"quoted printable\" encoding in buffer from BEG to END.
131 If the optional argument WRAPPER is non-nil,
132 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=.
133 On encountering malformed quoted-printable text, exits with an error,
134 unless NOERROR is non-nil, in which case it continues, and returns nil
135 when finished. Returns non-nil on successful completion.
136 If UNIBYTE is non-nil, insert converted characters as unibyte.
137 That is useful if you are going to character code decoding afterward,
138 as Rmail does."
139 ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1
140 ;; specific handling should be removed (or moved elsewhere and generalized).
141 (interactive "r\nP")
142 (let (failed)
143 (save-match-data
144 (save-excursion
145 (save-restriction
146 (narrow-to-region beg end)
147 (goto-char (point-min))
148 (when (and wrapper
149 (looking-at "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?"))
150 (delete-region (match-end 1) end)
151 (delete-region (point) (match-beginning 1)))
152 (while (re-search-forward "=\\(\\([0-9A-F][0-9A-F]\\)\\|[=\n]\\|..\\)" nil t)
153 (goto-char (match-end 0))
154 (cond ((= (char-after (match-beginning 1)) ?\n)
155 (replace-match ""))
156 ((= (char-after (match-beginning 1)) ?=)
157 (replace-match "="))
158 ((match-beginning 2)
159 (let ((char (+ (* 16 (mail-unquote-printable-hexdigit
160 (char-after (match-beginning 2))))
161 (mail-unquote-printable-hexdigit
162 (char-after (1+ (match-beginning 2)))))))
163 (if unibyte
164 (progn
165 (replace-match "")
166 ;; insert-byte will insert this as a
167 ;; corresponding eight-bit character.
168 (insert-byte char 1))
169 (replace-match (make-string 1 char) t t))))
170 (noerror
171 (setq failed t))
173 (error "Malformed MIME quoted-printable message"))))
174 (not failed))))))
176 (eval-when-compile (require 'rfc822))
178 (defun mail-strip-quoted-names (address)
179 "Delete comments and quoted strings in an address list ADDRESS.
180 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
181 Return a modified address list."
182 (if (null address)
184 (if mail-use-rfc822
185 (progn (require 'rfc822)
186 (mapconcat 'identity (rfc822-addresses address) ", "))
187 (let (pos)
189 ;; Detect nested comments.
190 (if (string-match "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*(" address)
191 ;; Strip nested comments.
192 (with-temp-buffer
193 (insert address)
194 (set-syntax-table lisp-mode-syntax-table)
195 (goto-char 1)
196 (while (search-forward "(" nil t)
197 (forward-char -1)
198 (skip-chars-backward " \t")
199 (delete-region (point)
200 (save-excursion
201 (condition-case ()
202 (forward-sexp 1)
203 (error (goto-char (point-max))))
204 (point))))
205 (setq address (buffer-string)))
206 ;; Strip non-nested comments an easier way.
207 (while (setq pos (string-match
208 ;; This doesn't hack rfc822 nested comments
209 ;; `(xyzzy (foo) whinge)' properly. Big deal.
210 "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*)"
211 address))
212 (setq address (replace-match "" nil nil address 0))))
214 ;; strip surrounding whitespace
215 (string-match "\\`[ \t\n]*" address)
216 (setq address (substring address
217 (match-end 0)
218 (string-match "[ \t\n]*\\'" address
219 (match-end 0))))
221 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
222 (setq pos 0)
223 (while (setq pos (string-match
224 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
225 address pos))
226 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
227 (if (and (> (length address) (match-end 0))
228 (= (aref address (match-end 0)) ?@))
229 (setq pos (match-end 0))
230 ;; Otherwise discard the "..." part.
231 (setq address (replace-match "" nil nil address 2))))
232 ;; If this address contains <...>, replace it with just
233 ;; the part between the <...>.
234 (while (setq pos (string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
235 address))
236 (setq address (replace-match (match-string 3 address)
237 nil 'literal address 2)))
238 address))))
240 ;; The following piece of ugliness is legacy code. The name was an
241 ;; unfortunate choice --- a flagrant violation of the Emacs Lisp
242 ;; coding conventions. `mail-dont-reply-to' would have been
243 ;; infinitely better. Also, `rmail-dont-reply-to-names' might have
244 ;; been better named `mail-dont-reply-to-names' and sourced from this
245 ;; file instead of in rmail.el. Yuck. -pmr
246 (defun rmail-dont-reply-to (destinations)
247 "Prune addresses from DESTINATIONS, a list of recipient addresses.
248 All addresses matching `rmail-dont-reply-to-names' are removed from
249 the comma-separated list. The pruned list is returned."
250 ;; FIXME this (setting a user option the first time a command is used)
251 ;; is somewhat strange. Normally one would never set the option,
252 ;; but instead fall back to the default so long as it was nil.
253 ;; Or just set the default directly in the defcustom.
254 (if (null rmail-dont-reply-to-names)
255 (setq rmail-dont-reply-to-names
256 (concat (if rmail-default-dont-reply-to-names
257 (concat rmail-default-dont-reply-to-names "\\|")
259 (if (and user-mail-address
260 (not (equal user-mail-address user-login-name)))
261 ;; Anchor the login name and email address so
262 ;; that we don't match substrings: if the
263 ;; login name is "foo", we shouldn't match
264 ;; "barfoo@baz.com".
265 (concat "\\`"
266 (regexp-quote user-mail-address)
267 "\\'\\|")
269 (concat "\\`" (regexp-quote user-login-name) "@"))))
270 ;; Split up DESTINATIONS and match each element separately.
271 (let ((start-pos 0) (cur-pos 0)
272 (case-fold-search t))
273 (while start-pos
274 (setq cur-pos (string-match "[,\"]" destinations cur-pos))
275 (if (and cur-pos (equal (match-string 0 destinations) "\""))
276 ;; Search for matching quote.
277 (let ((next-pos (string-match "\"" destinations (1+ cur-pos))))
278 (if next-pos
279 (setq cur-pos (1+ next-pos))
280 ;; If the open-quote has no close-quote,
281 ;; delete the open-quote to get something well-defined.
282 ;; This case is not valid, but it can happen if things
283 ;; are weird elsewhere.
284 (setq destinations (concat (substring destinations 0 cur-pos)
285 (substring destinations (1+ cur-pos))))
286 (setq cur-pos start-pos)))
287 (let* ((address (substring destinations start-pos cur-pos))
288 (naked-address (mail-strip-quoted-names address)))
289 (if (string-match rmail-dont-reply-to-names naked-address)
290 (setq destinations (concat (substring destinations 0 start-pos)
291 (and cur-pos (substring destinations
292 (1+ cur-pos))))
293 cur-pos start-pos)
294 (setq cur-pos (and cur-pos (1+ cur-pos))
295 start-pos cur-pos))))))
296 ;; get rid of any trailing commas
297 (let ((pos (string-match "[ ,\t\n]*\\'" destinations)))
298 (if pos
299 (setq destinations (substring destinations 0 pos))))
300 ;; remove leading spaces. they bother me.
301 (if (string-match "\\(\\s \\|,\\)*" destinations)
302 (substring destinations (match-end 0))
303 destinations))
306 ;;;###autoload
307 (defun mail-fetch-field (field-name &optional last all list)
308 "Return the value of the header field whose type is FIELD-NAME.
309 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
310 If third arg ALL is non-nil, concatenate all such fields with commas between.
311 If 4th arg LIST is non-nil, return a list of all such fields.
312 The buffer should be narrowed to just the header, else false
313 matches may be returned from the message body."
314 (save-excursion
315 (goto-char (point-min))
316 (let ((case-fold-search t)
317 (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
318 (if (or all list)
319 (let ((value (if all "")))
320 (while (re-search-forward name nil t)
321 (let ((opoint (point)))
322 (while (progn (forward-line 1)
323 (looking-at "[ \t]")))
324 ;; Back up over newline, then trailing spaces or tabs
325 (forward-char -1)
326 (skip-chars-backward " \t" opoint)
327 (if list
328 (setq value (cons (buffer-substring-no-properties
329 opoint (point))
330 value))
331 (setq value (concat value
332 (if (string= value "") "" ", ")
333 (buffer-substring-no-properties
334 opoint (point)))))))
335 (if list
336 value
337 (and (not (string= value "")) value)))
338 (if (re-search-forward name nil t)
339 (progn
340 (if last (while (re-search-forward name nil t)))
341 (let ((opoint (point)))
342 (while (progn (forward-line 1)
343 (looking-at "[ \t]")))
344 ;; Back up over newline, then trailing spaces or tabs
345 (forward-char -1)
346 (skip-chars-backward " \t" opoint)
347 (buffer-substring-no-properties opoint (point)))))))))
349 ;; Parse a list of tokens separated by commas.
350 ;; It runs from point to the end of the visible part of the buffer.
351 ;; Whitespace before or after tokens is ignored,
352 ;; but whitespace within tokens is kept.
353 (defun mail-parse-comma-list ()
354 (let (accumulated
355 beg)
356 (skip-chars-forward " \t\n")
357 (while (not (eobp))
358 (setq beg (point))
359 (skip-chars-forward "^,")
360 (skip-chars-backward " \t\n")
361 (setq accumulated
362 (cons (buffer-substring-no-properties beg (point))
363 accumulated))
364 (skip-chars-forward "^,")
365 (skip-chars-forward ", \t\n"))
366 accumulated))
368 (defun mail-comma-list-regexp (labels)
369 (let (pos)
370 (setq pos (or (string-match "[^ \t]" labels) 0))
371 ;; Remove leading and trailing whitespace.
372 (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
373 ;; Change each comma to \|, and flush surrounding whitespace.
374 (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
375 (setq labels
376 (concat (substring labels 0 pos)
377 "\\|"
378 (substring labels (match-end 0))))))
379 labels)
381 (defun mail-rfc822-time-zone (time)
382 (let* ((sec (or (car (current-time-zone time)) 0))
383 (absmin (/ (abs sec) 60)))
384 (format "%c%02d%02d" (if (< sec 0) ?- ?+) (/ absmin 60) (% absmin 60))))
386 (defun mail-rfc822-date ()
387 (let* ((time (current-time))
388 (s (current-time-string time)))
389 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s)
390 (concat (substring s (match-beginning 2) (match-end 2)) " "
391 (substring s (match-beginning 1) (match-end 1)) " "
392 (substring s (match-beginning 4) (match-end 4)) " "
393 (substring s (match-beginning 3) (match-end 3)) " "
394 (mail-rfc822-time-zone time))))
396 (defun mail-mbox-from ()
397 "Return an mbox \"From \" line for the current message.
398 The buffer should be narrowed to just the header."
399 (let* ((from (mail-strip-quoted-names (or (mail-fetch-field "from")
400 (mail-fetch-field "really-from")
401 (mail-fetch-field "sender")
402 (mail-fetch-field "return-path")
403 "unknown")))
404 (date (mail-fetch-field "date"))
405 ;; A From: header can contain multiple addresses, a "From "
406 ;; line must contain only one. (Bug#7760)
407 ;; See eg RFC 5322, 3.6.2. Originator Fields.
408 (end (string-match "[ \t]*[,\n]" from)))
409 (format "From %s %s\n" (if end
410 (substring from 0 end)
411 from)
412 (or (and date
413 (ignore-errors
414 (current-time-string (date-to-time date))))
415 (current-time-string)))))
417 (provide 'mail-utils)
419 ;;; mail-utils.el ends here