Mercurial > emacs
annotate lisp/mail/rfc822.el @ 84339:2397532a703a
*** empty log message ***
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 06 Sep 2007 06:53:59 +0000 |
parents | 8e27d63c45eb |
children | de499b20517a f55f9811f5d7 |
rev | line source |
---|---|
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; rfc822.el --- hairy rfc822 parser for mail and news and suchlike |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
74509 | 3 ;; Copyright (C) 1986, 1987, 1990, 2001, 2002, 2003, 2004, |
75347 | 4 ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
845 | 5 |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
6 ;; Author: Richard Mlynarik <mly@eddie.mit.edu> |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
7 ;; Maintainer: FSF |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
804
diff
changeset
|
8 ;; Keywords: mail |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
9 |
43 | 10 ;; This file is part of GNU Emacs. |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
78232
8e27d63c45eb
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
43 | 15 ;; any later version. |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
14169 | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
43 | 26 |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
27 ;;; Commentary: |
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
28 |
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
29 ;; Support functions for parsing RFC-822 headers, used by mail and news |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
21165
diff
changeset
|
30 ;; modes. |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
31 |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
32 ;;; Code: |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
33 |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
34 (defvar rfc822-address-start) |
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
35 |
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
36 ;; uses rfc822-address-start free, throws to address |
43 | 37 (defun rfc822-bad-address (reason) |
38 (save-restriction | |
39 (insert "_^_") | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
40 (narrow-to-region rfc822-address-start |
43 | 41 (if (re-search-forward "[,;]" nil t) |
42 (max (point-min) (1- (point))) | |
43 (point-max))) | |
44 ;; make the error string be suitable for inclusion in (...) | |
45 (let ((losers '("\\" "(" ")" "\n"))) | |
46 (while losers | |
47 (goto-char (point-min)) | |
48 (while (search-forward (car losers) nil t) | |
49 (backward-char 1) | |
50 (insert ?\\) | |
51 (forward-char 1)) | |
52 (setq losers (cdr losers)))) | |
53 (goto-char (point-min)) (insert "(Unparsable address -- " | |
54 reason | |
15882
874f0cc6c431
(rfc822-bad-address): Don't put a newline into the
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
55 ": \"") |
43 | 56 (goto-char (point-max)) (insert "\")")) |
57 (rfc822-nuke-whitespace) | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
58 (throw 'address (buffer-substring rfc822-address-start (point)))) |
43 | 59 |
60 (defun rfc822-nuke-whitespace (&optional leave-space) | |
61 (let (ch) | |
62 (while (cond ((eobp) | |
63 nil) | |
64 ((= (setq ch (following-char)) ?\() | |
65 (forward-char 1) | |
66 (while (if (eobp) | |
67 (rfc822-bad-address "Unbalanced comment (...)") | |
68 (/= (setq ch (following-char)) ?\))) | |
69 (cond ((looking-at "[^()\\]+") | |
70 (replace-match "")) | |
71 ((= ch ?\() | |
72 (rfc822-nuke-whitespace)) | |
73 ((< (point) (1- (point-max))) | |
74 (delete-char 2)) | |
75 (t | |
76 (rfc822-bad-address "orphaned backslash")))) | |
77 ;; delete remaining "()" | |
78 (forward-char -1) | |
79 (delete-char 2) | |
80 t) | |
49846
7e279209f48b
(rfc822-nuke-whitespace): Fix character constant.
Juanma Barranquero <lekktu@gmail.com>
parents:
49598
diff
changeset
|
81 ((memq ch '(?\ ?\t ?\n)) |
43 | 82 (delete-region (point) |
83 (progn (skip-chars-forward " \t\n") (point))) | |
84 t) | |
85 (t | |
86 nil))) | |
87 (or (not leave-space) | |
88 (eobp) | |
89 (bobp) | |
90 (= (preceding-char) ?\ ) | |
91 (insert ?\ )))) | |
92 | |
93 (defun rfc822-looking-at (regex &optional leave-space) | |
94 (if (cond ((stringp regex) | |
95 (if (looking-at regex) | |
96 (progn (goto-char (match-end 0)) | |
97 t))) | |
98 (t | |
99 (if (and (not (eobp)) | |
100 (= (following-char) regex)) | |
101 (progn (forward-char 1) | |
102 t)))) | |
103 (let ((tem (match-data))) | |
104 (rfc822-nuke-whitespace leave-space) | |
21165
480d7479ccac
(rfc822-looking-at): store-match-data => set-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
20683
diff
changeset
|
105 (set-match-data tem) |
43 | 106 t))) |
107 | |
108 (defun rfc822-snarf-word () | |
109 ;; word is atom | quoted-string | |
110 (cond ((= (following-char) ?\") | |
111 ;; quoted-string | |
112 (or (rfc822-looking-at "\"\\([^\"\\\n]\\|\\\\.\\|\\\\\n\\)*\"") | |
113 (rfc822-bad-address "Unterminated quoted string"))) | |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
114 ((rfc822-looking-at "[^][\000-\037 ()<>@,;:\\\".]+") |
43 | 115 ;; atom |
116 ) | |
117 (t | |
118 (rfc822-bad-address "Rubbish in address")))) | |
119 | |
120 (defun rfc822-snarf-words () | |
121 (rfc822-snarf-word) | |
122 (while (rfc822-looking-at ?.) | |
123 (rfc822-snarf-word))) | |
124 | |
125 (defun rfc822-snarf-subdomain () | |
126 ;; sub-domain is domain-ref | domain-literal | |
127 (cond ((= (following-char) ?\[) | |
128 ;; domain-ref | |
129 (or (rfc822-looking-at "\\[\\([^][\\\n]\\|\\\\.\\|\\\\\n\\)*\\]") | |
130 (rfc822-bad-address "Unterminated domain literal [...]"))) | |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
131 ((rfc822-looking-at "[^][\000-\037 ()<>@,;:\\\".]+") |
43 | 132 ;; domain-literal = atom |
133 ) | |
134 (t | |
135 (rfc822-bad-address "Rubbish in host/domain specification")))) | |
136 | |
137 (defun rfc822-snarf-domain () | |
138 (rfc822-snarf-subdomain) | |
139 (while (rfc822-looking-at ?.) | |
140 (rfc822-snarf-subdomain))) | |
141 | |
142 (defun rfc822-snarf-frob-list (name separator terminator snarfer | |
143 &optional return) | |
144 (let ((first t) | |
145 (list ()) | |
146 tem) | |
147 (while (cond ((eobp) | |
148 (rfc822-bad-address | |
149 (format "End of addresses in middle of %s" name))) | |
150 ((rfc822-looking-at terminator) | |
151 nil) | |
152 ((rfc822-looking-at separator) | |
153 ;; multiple separators are allowed and do nothing. | |
154 (while (rfc822-looking-at separator)) | |
155 t) | |
156 (first | |
157 t) | |
158 (t | |
159 (rfc822-bad-address | |
160 (format "Gubbish in middle of %s" name)))) | |
161 (setq tem (funcall snarfer) | |
162 first nil) | |
163 (and return tem | |
164 (setq list (if (listp tem) | |
165 (nconc (reverse tem) list) | |
166 (cons tem list))))) | |
167 (nreverse list))) | |
168 | |
169 ;; return either an address (a string) or a list of addresses | |
170 (defun rfc822-addresses-1 (&optional allow-groups) | |
171 ;; Looking for an rfc822 `address' | |
172 ;; Either a group (1*word ":" [#mailbox] ";") | |
173 ;; or a mailbox (addr-spec | 1*word route-addr) | |
174 ;; addr-spec is (local-part "@" domain) | |
175 ;; route-addr is ("<" [1#("@" domain) ":"] addr-spec ">") | |
176 ;; local-part is (word *("." word)) | |
177 ;; word is (atom | quoted-string) | |
178 ;; quoted-string is ("\([^\"\\n]\|\\.\|\\\n\)") | |
179 ;; atom is [^\000-\037\177 ()<>@,;:\".[]]+ | |
180 ;; domain is sub-domain *("." sub-domain) | |
181 ;; sub-domain is domain-ref | domain-literal | |
182 ;; domain-literal is "[" *(dtext | quoted-pair) "]" | |
183 ;; dtext is "[^][\\n" | |
184 ;; domain-ref is atom | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
185 (let ((rfc822-address-start (point)) |
43 | 186 (n 0)) |
187 (catch 'address | |
188 ;; optimize common cases: | |
189 ;; foo | |
190 ;; foo.bar@bar.zap | |
191 ;; followed by "\\'\\|,\\|([^()\\]*)\\'" | |
192 ;; other common cases are: | |
193 ;; foo bar <foo.bar@baz.zap> | |
194 ;; "foo bar" <foo.bar@baz.zap> | |
195 ;; those aren't hacked yet. | |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
196 (if (and (rfc822-looking-at "[^][\000-\037 ()<>@,;:\\\"]+\\(\\|@[^][\000-\037 ()<>@,;:\\\"]+\\)" t) |
43 | 197 (progn (or (eobp) |
198 (rfc822-looking-at ?,)))) | |
199 (progn | |
200 ;; rfc822-looking-at may have inserted a space | |
201 (or (bobp) (/= (preceding-char) ?\ ) (delete-char -1)) | |
202 ;; relying on the fact that rfc822-looking-at <char> | |
203 ;; doesn't mung match-data | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
204 (throw 'address (buffer-substring rfc822-address-start (match-end 0))))) |
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
205 (goto-char rfc822-address-start) |
43 | 206 (while t |
207 (cond ((and (= n 1) (rfc822-looking-at ?@)) | |
208 ;; local-part@domain | |
209 (rfc822-snarf-domain) | |
210 (throw 'address | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
211 (buffer-substring rfc822-address-start (point)))) |
43 | 212 ((rfc822-looking-at ?:) |
213 (cond ((not allow-groups) | |
214 (rfc822-bad-address "A group name may not appear here")) | |
215 ((= n 0) | |
216 (rfc822-bad-address "No name for :...; group"))) | |
217 ;; group | |
218 (throw 'address | |
219 ;; return a list of addresses | |
220 (rfc822-snarf-frob-list ":...; group" ?\, ?\; | |
221 'rfc822-addresses-1 t))) | |
222 ((rfc822-looking-at ?<) | |
223 (let ((start (point)) | |
224 (strip t)) | |
225 (cond ((rfc822-looking-at ?>) | |
226 ;; empty path | |
227 ()) | |
228 ((and (not (eobp)) (= (following-char) ?\@)) | |
229 ;; <@foo.bar,@baz:quux@abcd.efg> | |
230 (rfc822-snarf-frob-list "<...> address" ?\, ?\: | |
231 (function (lambda () | |
232 (if (rfc822-looking-at ?\@) | |
233 (rfc822-snarf-domain) | |
234 (rfc822-bad-address | |
235 "Gubbish in route-addr"))))) | |
236 (rfc822-snarf-words) | |
237 (or (rfc822-looking-at ?@) | |
238 (rfc822-bad-address "Malformed <..@..> address")) | |
239 (rfc822-snarf-domain) | |
240 (setq strip nil)) | |
241 ((progn (rfc822-snarf-words) (rfc822-looking-at ?@)) | |
242 ; allow <foo> (losing unix seems to do this) | |
243 (rfc822-snarf-domain))) | |
244 (let ((end (point))) | |
245 (if (rfc822-looking-at ?\>) | |
246 (throw 'address | |
247 (buffer-substring (if strip start (1- start)) | |
248 (if strip end (1+ end)))) | |
249 (rfc822-bad-address "Unterminated <...> address"))))) | |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
250 ((looking-at "[^][\000-\037 ()<>@,;:\\.]") |
43 | 251 ;; this allows "." to be part of the words preceding |
252 ;; an addr-spec, since many broken mailers output | |
253 ;; "Hern K. Herklemeyer III | |
254 ;; <yank@megadeath.dod.gods-own-country>" | |
125 | 255 (let ((again t)) |
256 (while again | |
257 (or (= n 0) (bobp) (= (preceding-char) ?\ ) | |
258 (insert ?\ )) | |
126 | 259 (rfc822-snarf-words) |
125 | 260 (setq n (1+ n)) |
261 (setq again (or (rfc822-looking-at ?.) | |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
262 (looking-at "[^][\000-\037 ()<>@,;:\\.]")))))) |
43 | 263 ((= n 0) |
264 (throw 'address nil)) | |
265 ((= n 1) ; allow "foo" (losing unix seems to do this) | |
266 (throw 'address | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
267 (buffer-substring rfc822-address-start (point)))) |
125 | 268 ((> n 1) |
269 (rfc822-bad-address "Missing comma between addresses or badly-formatted address")) | |
270 ((or (eobp) (= (following-char) ?,)) | |
43 | 271 (rfc822-bad-address "Missing comma or route-spec")) |
272 (t | |
273 (rfc822-bad-address "Strange character or missing comma"))))))) | |
274 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
21165
diff
changeset
|
275 |
43 | 276 (defun rfc822-addresses (header-text) |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
277 (if (string-match "\\`[ \t]*\\([^][\000-\037 ()<>@,;:\\\".]+\\)[ \t]*\\'" |
43 | 278 header-text) |
279 ;; Make very simple case moderately fast. | |
280 (list (substring header-text (match-beginning 1) (match-end 1))) | |
281 (let ((buf (generate-new-buffer " rfc822"))) | |
282 (unwind-protect | |
283 (save-excursion | |
284 (set-buffer buf) | |
285 (make-local-variable 'case-fold-search) | |
286 (setq case-fold-search nil) ;For speed(?) | |
287 (insert header-text) | |
288 ;; unfold continuation lines | |
289 (goto-char (point-min)) | |
290 | |
291 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t) | |
292 (replace-match "\\1 " t)) | |
293 | |
294 (goto-char (point-min)) | |
295 (let ((list ()) | |
296 tem | |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
297 rfc822-address-start); this is for rfc822-bad-address |
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
298 (rfc822-nuke-whitespace) |
43 | 299 (while (not (eobp)) |
53414
a2ead0e84644
(rfc822-address-start): Declare variable.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
300 (setq rfc822-address-start (point)) |
43 | 301 (setq tem |
302 (catch 'address ; this is for rfc822-bad-address | |
303 (cond ((rfc822-looking-at ?\,) | |
304 nil) | |
20683
5623c78a31e5
(rfc822-snarf-word): Don't reject non-ASCII chars.
Richard M. Stallman <rms@gnu.org>
parents:
15882
diff
changeset
|
305 ((looking-at "[][\000-\037@;:\\.>)]") |
43 | 306 (forward-char) |
307 (rfc822-bad-address | |
308 (format "Strange character \\%c found" | |
309 (preceding-char)))) | |
310 (t | |
311 (rfc822-addresses-1 t))))) | |
312 (cond ((null tem)) | |
313 ((stringp tem) | |
314 (setq list (cons tem list))) | |
315 (t | |
316 (setq list (nconc (nreverse tem) list))))) | |
317 (nreverse list))) | |
318 (and buf (kill-buffer buf)))))) | |
319 | |
584 | 320 (provide 'rfc822) |
321 | |
52401 | 322 ;;; arch-tag: 5d388a24-e173-40fb-9b8e-85269de44b37 |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
323 ;;; rfc822.el ends here |