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