* lisp/subr.el (read-quoted-char): Fix up last change.
[emacs.git] / lisp / mail / mail-utils.el
blob1c527f22113afbb683ed75bbee66877a1ad28198
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 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-current-buffer (get-buffer-create " *temp*")
193 (erase-buffer)
194 (insert address)
195 (set-syntax-table lisp-mode-syntax-table)
196 (goto-char 1)
197 (while (search-forward "(" nil t)
198 (forward-char -1)
199 (skip-chars-backward " \t")
200 (delete-region (point)
201 (save-excursion
202 (condition-case ()
203 (forward-sexp 1)
204 (error (goto-char (point-max))))
205 (point))))
206 (setq address (buffer-string))
207 (erase-buffer))
208 ;; Strip non-nested comments an easier way.
209 (while (setq pos (string-match
210 ;; This doesn't hack rfc822 nested comments
211 ;; `(xyzzy (foo) whinge)' properly. Big deal.
212 "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*)"
213 address))
214 (setq address (replace-match "" nil nil address 0))))
216 ;; strip surrounding whitespace
217 (string-match "\\`[ \t\n]*" address)
218 (setq address (substring address
219 (match-end 0)
220 (string-match "[ \t\n]*\\'" address
221 (match-end 0))))
223 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
224 (setq pos 0)
225 (while (setq pos (string-match
226 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
227 address pos))
228 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
229 (if (and (> (length address) (match-end 0))
230 (= (aref address (match-end 0)) ?@))
231 (setq pos (match-end 0))
232 ;; Otherwise discard the "..." part.
233 (setq address (replace-match "" nil nil address 2))))
234 ;; If this address contains <...>, replace it with just
235 ;; the part between the <...>.
236 (while (setq pos (string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
237 address))
238 (setq address (replace-match (match-string 3 address)
239 nil 'literal address 2)))
240 address))))
242 ;;; The following piece of ugliness is legacy code. The name was an
243 ;;; unfortunate choice --- a flagrant violation of the Emacs Lisp
244 ;;; coding conventions. `mail-dont-reply-to' would have been
245 ;;; infinitely better. Also, `rmail-dont-reply-to-names' might have
246 ;;; been better named `mail-dont-reply-to-names' and sourced from this
247 ;;; file instead of in rmail.el. Yuck. -pmr
248 (defun rmail-dont-reply-to (destinations)
249 "Prune addresses from DESTINATIONS, a list of recipient addresses.
250 All addresses matching `rmail-dont-reply-to-names' are removed from
251 the comma-separated list. The pruned list is returned."
252 ;; FIXME this (setting a user option the first time a command is used)
253 ;; is somewhat strange. Normally one would never set the option,
254 ;; but instead fall back to the default so long as it was nil.
255 ;; Or just set the default directly in the defcustom.
256 (if (null rmail-dont-reply-to-names)
257 (setq rmail-dont-reply-to-names
258 (concat (if rmail-default-dont-reply-to-names
259 (concat rmail-default-dont-reply-to-names "\\|")
261 (if (and user-mail-address
262 (not (equal user-mail-address user-login-name)))
263 ;; Anchor the login name and email address so
264 ;; that we don't match substrings: if the
265 ;; login name is "foo", we shouldn't match
266 ;; "barfoo@baz.com".
267 (concat "\\`"
268 (regexp-quote user-mail-address)
269 "\\'\\|")
271 (concat "\\`" (regexp-quote user-login-name) "@"))))
272 ;; Split up DESTINATIONS and match each element separately.
273 (let ((start-pos 0) (cur-pos 0)
274 (case-fold-search t))
275 (while start-pos
276 (setq cur-pos (string-match "[,\"]" destinations cur-pos))
277 (if (and cur-pos (equal (match-string 0 destinations) "\""))
278 ;; Search for matching quote.
279 (let ((next-pos (string-match "\"" destinations (1+ cur-pos))))
280 (if next-pos
281 (setq cur-pos (1+ next-pos))
282 ;; If the open-quote has no close-quote,
283 ;; delete the open-quote to get something well-defined.
284 ;; This case is not valid, but it can happen if things
285 ;; are weird elsewhere.
286 (setq destinations (concat (substring destinations 0 cur-pos)
287 (substring destinations (1+ cur-pos))))
288 (setq cur-pos start-pos)))
289 (let* ((address (substring destinations start-pos cur-pos))
290 (naked-address (mail-strip-quoted-names address)))
291 (if (string-match rmail-dont-reply-to-names naked-address)
292 (setq destinations (concat (substring destinations 0 start-pos)
293 (and cur-pos (substring destinations
294 (1+ cur-pos))))
295 cur-pos start-pos)
296 (setq cur-pos (and cur-pos (1+ cur-pos))
297 start-pos cur-pos))))))
298 ;; get rid of any trailing commas
299 (let ((pos (string-match "[ ,\t\n]*\\'" destinations)))
300 (if pos
301 (setq destinations (substring destinations 0 pos))))
302 ;; remove leading spaces. they bother me.
303 (if (string-match "\\(\\s \\|,\\)*" destinations)
304 (substring destinations (match-end 0))
305 destinations))
308 ;;;###autoload
309 (defun mail-fetch-field (field-name &optional last all list)
310 "Return the value of the header field whose type is FIELD-NAME.
311 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
312 If third arg ALL is non-nil, concatenate all such fields with commas between.
313 If 4th arg LIST is non-nil, return a list of all such fields.
314 The buffer should be narrowed to just the header, else false
315 matches may be returned from the message body."
316 (save-excursion
317 (goto-char (point-min))
318 (let ((case-fold-search t)
319 (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
320 (if (or all list)
321 (let ((value (if all "")))
322 (while (re-search-forward name nil t)
323 (let ((opoint (point)))
324 (while (progn (forward-line 1)
325 (looking-at "[ \t]")))
326 ;; Back up over newline, then trailing spaces or tabs
327 (forward-char -1)
328 (skip-chars-backward " \t" opoint)
329 (if list
330 (setq value (cons (buffer-substring-no-properties
331 opoint (point))
332 value))
333 (setq value (concat value
334 (if (string= value "") "" ", ")
335 (buffer-substring-no-properties
336 opoint (point)))))))
337 (if list
338 value
339 (and (not (string= value "")) value)))
340 (if (re-search-forward name nil t)
341 (progn
342 (if last (while (re-search-forward name nil t)))
343 (let ((opoint (point)))
344 (while (progn (forward-line 1)
345 (looking-at "[ \t]")))
346 ;; Back up over newline, then trailing spaces or tabs
347 (forward-char -1)
348 (skip-chars-backward " \t" opoint)
349 (buffer-substring-no-properties opoint (point)))))))))
351 ;; Parse a list of tokens separated by commas.
352 ;; It runs from point to the end of the visible part of the buffer.
353 ;; Whitespace before or after tokens is ignored,
354 ;; but whitespace within tokens is kept.
355 (defun mail-parse-comma-list ()
356 (let (accumulated
357 beg)
358 (skip-chars-forward " \t\n")
359 (while (not (eobp))
360 (setq beg (point))
361 (skip-chars-forward "^,")
362 (skip-chars-backward " \t\n")
363 (setq accumulated
364 (cons (buffer-substring-no-properties beg (point))
365 accumulated))
366 (skip-chars-forward "^,")
367 (skip-chars-forward ", \t\n"))
368 accumulated))
370 (defun mail-comma-list-regexp (labels)
371 (let (pos)
372 (setq pos (or (string-match "[^ \t]" labels) 0))
373 ;; Remove leading and trailing whitespace.
374 (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
375 ;; Change each comma to \|, and flush surrounding whitespace.
376 (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
377 (setq labels
378 (concat (substring labels 0 pos)
379 "\\|"
380 (substring labels (match-end 0))))))
381 labels)
383 (defun mail-rfc822-time-zone (time)
384 (let* ((sec (or (car (current-time-zone time)) 0))
385 (absmin (/ (abs sec) 60)))
386 (format "%c%02d%02d" (if (< sec 0) ?- ?+) (/ absmin 60) (% absmin 60))))
388 (defun mail-rfc822-date ()
389 (let* ((time (current-time))
390 (s (current-time-string time)))
391 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s)
392 (concat (substring s (match-beginning 2) (match-end 2)) " "
393 (substring s (match-beginning 1) (match-end 1)) " "
394 (substring s (match-beginning 4) (match-end 4)) " "
395 (substring s (match-beginning 3) (match-end 3)) " "
396 (mail-rfc822-time-zone time))))
398 (defun mail-mbox-from ()
399 "Return an mbox \"From \" line for the current message.
400 The buffer should be narrowed to just the header."
401 (let ((from (or (mail-fetch-field "from")
402 (mail-fetch-field "really-from")
403 (mail-fetch-field "sender")
404 "unknown"))
405 (date (mail-fetch-field "date")))
406 (format "From %s %s\n" (mail-strip-quoted-names from)
407 (or (and date
408 (ignore-errors
409 (current-time-string (date-to-time date))))
410 (current-time-string)))))
412 (provide 'mail-utils)
414 ;; arch-tag: b24aec2f-fd65-4ceb-9e39-3cc2827036fd
415 ;;; mail-utils.el ends here