Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; mail-utils.el --- utility functions used both by rmail and rnews |
2 | ||
78f086e4 | 3 | ;; Copyright (C) 1985, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
8de31eec | 4 | ;; 2009, 2010, 2011 Free Software Foundation, Inc. |
9750e079 | 5 | |
e5167999 | 6 | ;; Maintainer: FSF |
fd7fa35a | 7 | ;; Keywords: mail, news |
e5167999 | 8 | |
a2535589 JA |
9 | ;; This file is part of GNU Emacs. |
10 | ||
b1fc2b50 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
a2535589 | 12 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
a2535589 JA |
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 | |
b1fc2b50 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
a2535589 | 23 | |
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
ca10c3d1 | 26 | ;; Utility functions for mail and netnews handling. These handle fine |
e41b2db1 ER |
27 | ;; points of header parsing. |
28 | ||
e5167999 | 29 | ;;; Code: |
a2535589 | 30 | |
ecca85de JB |
31 | ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has |
32 | ;;; been initialized. | |
33 | (require 'lisp-mode) | |
a1506d29 | 34 | |
73fa8346 | 35 | ;;;###autoload |
7dbed484 GM |
36 | (defcustom mail-use-rfc822 nil |
37 | "If non-nil, use a full, hairy RFC822 parser on mail addresses. | |
73fa8346 | 38 | Otherwise, (the default) use a smaller, somewhat faster, and |
7aa122f4 SE |
39 | often correct parser." |
40 | :type 'boolean | |
41 | :group 'mail) | |
a2535589 | 42 | |
996792b1 RS |
43 | ;; Returns t if file FILE is an Rmail file. |
44 | ;;;###autoload | |
45 | (defun mail-file-babyl-p (file) | |
7dbed484 GM |
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:"))) | |
996792b1 | 50 | |
a2535589 JA |
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 | ||
60ba61bb | 58 | ;;;###autoload |
1d6a4283 RS |
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, | |
444dd0b5 | 62 | we add the wrapper characters =?ISO-8859-1?Q?....?=." |
1d6a4283 RS |
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 | |
444dd0b5 | 72 | (concat "=?ISO-8859-1?Q?" |
1d6a4283 | 73 | result (substring string i) |
444dd0b5 | 74 | "?=") |
1d6a4283 RS |
75 | (concat result (substring string i)))))) |
76 | ||
367aa646 RS |
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 | ||
1d6a4283 | 97 | (defun mail-unquote-printable-hexdigit (char) |
1c81a393 | 98 | (setq char (upcase char)) |
1d6a4283 RS |
99 | (if (>= char ?A) |
100 | (+ (- char ?A) 10) | |
101 | (- char ?0))) | |
102 | ||
60ba61bb | 103 | ;;;###autoload |
1d6a4283 RS |
104 | (defun mail-unquote-printable (string &optional wrapper) |
105 | "Undo the \"quoted printable\" encoding. | |
106 | If the optional argument WRAPPER is non-nil, | |
444dd0b5 | 107 | we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=." |
1d6a4283 RS |
108 | (save-match-data |
109 | (and wrapper | |
444dd0b5 | 110 | (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string) |
1d6a4283 | 111 | (setq string (match-string 1 string))) |
60ba61bb KH |
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 | |
1d6a4283 RS |
118 | (+ (* 16 (mail-unquote-printable-hexdigit |
119 | (aref string (match-beginning 1)))) | |
120 | (mail-unquote-printable-hexdigit | |
60ba61bb KH |
121 | (aref string (1+ (match-beginning 1)))))) |
122 | strings))) | |
1d6a4283 | 123 | (setq i (match-end 0))) |
60ba61bb KH |
124 | (apply 'concat (nreverse (cons (substring string i) strings)))))) |
125 | ||
4d01b827 | 126 | ;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el. |
60ba61bb | 127 | ;;;###autoload |
926f2004 RS |
128 | (defun mail-unquote-printable-region (beg end &optional wrapper noerror |
129 | unibyte) | |
60ba61bb KH |
130 | "Undo the \"quoted printable\" encoding in buffer from BEG to END. |
131 | If the optional argument WRAPPER is non-nil, | |
1c81a393 | 132 | we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=. |
4d01b827 GM |
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. | |
926f2004 RS |
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." | |
c893016b SM |
139 | ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1 |
140 | ;; specific handling should be removed (or moved elsewhere and generalized). | |
60ba61bb | 141 | (interactive "r\nP") |
1c81a393 RS |
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) | |
926f2004 RS |
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 "") | |
f45de83b EZ |
166 | ;; insert-byte will insert this as a |
167 | ;; corresponding eight-bit character. | |
168 | (insert-byte char 1)) | |
926f2004 | 169 | (replace-match (make-string 1 char) t t)))) |
1c81a393 RS |
170 | (noerror |
171 | (setq failed t)) | |
172 | (t | |
173 | (error "Malformed MIME quoted-printable message")))) | |
174 | (not failed)))))) | |
1d6a4283 | 175 | |
7fab5ded PR |
176 | (eval-when-compile (require 'rfc822)) |
177 | ||
a2535589 JA |
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." | |
10a4c11f JB |
182 | (if (null address) |
183 | nil | |
184 | (if mail-use-rfc822 | |
185 | (progn (require 'rfc822) | |
186 | (mapconcat 'identity (rfc822-addresses address) ", ")) | |
187 | (let (pos) | |
a2535589 | 188 | |
10a4c11f | 189 | ;; Detect nested comments. |
2c9d345f | 190 | (if (string-match "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*(" address) |
10a4c11f | 191 | ;; Strip nested comments. |
9430e988 | 192 | (with-temp-buffer |
10a4c11f JB |
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) | |
c5fb599f RS |
200 | (save-excursion |
201 | (condition-case () | |
202 | (forward-sexp 1) | |
203 | (error (goto-char (point-max)))) | |
204 | (point)))) | |
9430e988 | 205 | (setq address (buffer-string))) |
10a4c11f | 206 | ;; Strip non-nested comments an easier way. |
a1506d29 | 207 | (while (setq pos (string-match |
10a4c11f JB |
208 | ;; This doesn't hack rfc822 nested comments |
209 | ;; `(xyzzy (foo) whinge)' properly. Big deal. | |
2c9d345f | 210 | "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*)" |
10a4c11f | 211 | address)) |
82d22193 | 212 | (setq address (replace-match "" nil nil address 0)))) |
a2535589 | 213 | |
ee019c84 RS |
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)))) | |
220 | ||
10a4c11f JB |
221 | ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>') |
222 | (setq pos 0) | |
223 | (while (setq pos (string-match | |
82d22193 | 224 | "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)" |
10a4c11f JB |
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)) | |
82d22193 RS |
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-*,\\|\\'\\)" | |
10a4c11f | 235 | address)) |
82d22193 | 236 | (setq address (replace-match (match-string 3 address) |
87449711 | 237 | nil 'literal address 2))) |
10a4c11f | 238 | address)))) |
a2535589 | 239 | |
bb0974cf PR |
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." | |
4d01b827 GM |
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. | |
a2535589 JA |
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 "\\|") | |
bb0974cf PR |
258 | "") |
259 | (if (and user-mail-address | |
260 | (not (equal user-mail-address user-login-name))) | |
ca06718d EZ |
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 | "\\'\\|") | |
bb0974cf | 268 | "") |
ca06718d | 269 | (concat "\\`" (regexp-quote user-login-name) "@")))) |
bb0974cf PR |
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)) | |
82d22193 RS |
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. | |
bb0974cf PR |
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) | |
a1506d29 | 291 | (and cur-pos (substring destinations |
bb0974cf PR |
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 | |
7fab5ded PR |
297 | (let ((pos (string-match "[ ,\t\n]*\\'" destinations))) |
298 | (if pos | |
299 | (setq destinations (substring destinations 0 pos)))) | |
bb0974cf PR |
300 | ;; remove leading spaces. they bother me. |
301 | (if (string-match "\\(\\s \\|,\\)*" destinations) | |
302 | (substring destinations (match-end 0)) | |
303 | destinations)) | |
a0b796e3 | 304 | |
a2535589 | 305 | \f |
9b9f9c9d | 306 | ;;;###autoload |
b7bf1cef | 307 | (defun mail-fetch-field (field-name &optional last all list) |
da427818 | 308 | "Return the value of the header field whose type is FIELD-NAME. |
da427818 | 309 | If second arg LAST is non-nil, use the last field of type FIELD-NAME. |
b7bf1cef | 310 | If third arg ALL is non-nil, concatenate all such fields with commas between. |
78f086e4 | 311 | If 4th arg LIST is non-nil, return a list of all such fields. |
869692c0 GM |
312 | The buffer should be narrowed to just the header, else false |
313 | matches may be returned from the message body." | |
a2535589 JA |
314 | (save-excursion |
315 | (goto-char (point-min)) | |
316 | (let ((case-fold-search t) | |
317 | (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*"))) | |
b7bf1cef RS |
318 | (if (or all list) |
319 | (let ((value (if all ""))) | |
a2535589 JA |
320 | (while (re-search-forward name nil t) |
321 | (let ((opoint (point))) | |
322 | (while (progn (forward-line 1) | |
323 | (looking-at "[ \t]"))) | |
6f759698 RS |
324 | ;; Back up over newline, then trailing spaces or tabs |
325 | (forward-char -1) | |
fb83f550 | 326 | (skip-chars-backward " \t" opoint) |
b7bf1cef RS |
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))) | |
a2535589 JA |
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]"))) | |
6f759698 RS |
344 | ;; Back up over newline, then trailing spaces or tabs |
345 | (forward-char -1) | |
fb83f550 | 346 | (skip-chars-backward " \t" opoint) |
b94f28ee | 347 | (buffer-substring-no-properties opoint (point))))))))) |
a2535589 JA |
348 | \f |
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) | |
869fc1d9 | 356 | (skip-chars-forward " \t\n") |
a2535589 JA |
357 | (while (not (eobp)) |
358 | (setq beg (point)) | |
359 | (skip-chars-forward "^,") | |
869fc1d9 | 360 | (skip-chars-backward " \t\n") |
a2535589 | 361 | (setq accumulated |
698f40ca | 362 | (cons (buffer-substring-no-properties beg (point)) |
a2535589 JA |
363 | accumulated)) |
364 | (skip-chars-forward "^,") | |
869fc1d9 | 365 | (skip-chars-forward ", \t\n")) |
a2535589 JA |
366 | accumulated)) |
367 | ||
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) | |
e6a4a267 RS |
380 | \f |
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)))) | |
385 | ||
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)))) | |
49116ac0 | 395 | |
1bfadfb2 GM |
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." | |
8de31eec GM |
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) | |
1bfadfb2 GM |
412 | (or (and date |
413 | (ignore-errors | |
414 | (current-time-string (date-to-time date)))) | |
415 | (current-time-string))))) | |
416 | ||
49116ac0 JB |
417 | (provide 'mail-utils) |
418 | ||
6594deb0 | 419 | ;;; mail-utils.el ends here |