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