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