Mercurial > emacs
annotate lisp/mail/rfc822.el @ 1396:17365cdb1c10
(edit-picture): Run picture-mode-hook.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 12 Oct 1992 04:53:11 +0000 |
parents | 213978acbc1e |
children | 9e7ec92a4fdf |
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 |
845 | 3 ;; Copyright (C) 1986, 87, 1990 Free Software Foundation, Inc. |
4 | |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu> |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
6 ;; Maintainer: FSF |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
804
diff
changeset
|
7 ;; Keywords: mail |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
8 |
43 | 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 | |
804 | 13 ;; the Free Software Foundation; either version 2, or (at your option) |
43 | 14 ;; 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; see the file COPYING. If not, write to | |
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
25 ;;; Code: |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
26 |
43 | 27 ;; uses address-start free, throws to address |
28 (defun rfc822-bad-address (reason) | |
29 (save-restriction | |
30 (insert "_^_") | |
31 (narrow-to-region address-start | |
32 (if (re-search-forward "[,;]" nil t) | |
33 (max (point-min) (1- (point))) | |
34 (point-max))) | |
35 ;; make the error string be suitable for inclusion in (...) | |
36 (let ((losers '("\\" "(" ")" "\n"))) | |
37 (while losers | |
38 (goto-char (point-min)) | |
39 (while (search-forward (car losers) nil t) | |
40 (backward-char 1) | |
41 (insert ?\\) | |
42 (forward-char 1)) | |
43 (setq losers (cdr losers)))) | |
44 (goto-char (point-min)) (insert "(Unparsable address -- " | |
45 reason | |
46 ":\n\t \"") | |
47 (goto-char (point-max)) (insert "\")")) | |
48 (rfc822-nuke-whitespace) | |
49 (throw 'address (buffer-substring address-start (point)))) | |
50 | |
51 (defun rfc822-nuke-whitespace (&optional leave-space) | |
52 (let (ch) | |
53 (while (cond ((eobp) | |
54 nil) | |
55 ((= (setq ch (following-char)) ?\() | |
56 (forward-char 1) | |
57 (while (if (eobp) | |
58 (rfc822-bad-address "Unbalanced comment (...)") | |
59 (/= (setq ch (following-char)) ?\))) | |
60 (cond ((looking-at "[^()\\]+") | |
61 (replace-match "")) | |
62 ((= ch ?\() | |
63 (rfc822-nuke-whitespace)) | |
64 ((< (point) (1- (point-max))) | |
65 (delete-char 2)) | |
66 (t | |
67 (rfc822-bad-address "orphaned backslash")))) | |
68 ;; delete remaining "()" | |
69 (forward-char -1) | |
70 (delete-char 2) | |
71 t) | |
72 ((memq ch '(?\ ?\t ?\n)) | |
73 (delete-region (point) | |
74 (progn (skip-chars-forward " \t\n") (point))) | |
75 t) | |
76 (t | |
77 nil))) | |
78 (or (not leave-space) | |
79 (eobp) | |
80 (bobp) | |
81 (= (preceding-char) ?\ ) | |
82 (insert ?\ )))) | |
83 | |
84 (defun rfc822-looking-at (regex &optional leave-space) | |
85 (if (cond ((stringp regex) | |
86 (if (looking-at regex) | |
87 (progn (goto-char (match-end 0)) | |
88 t))) | |
89 (t | |
90 (if (and (not (eobp)) | |
91 (= (following-char) regex)) | |
92 (progn (forward-char 1) | |
93 t)))) | |
94 (let ((tem (match-data))) | |
95 (rfc822-nuke-whitespace leave-space) | |
96 (store-match-data tem) | |
97 t))) | |
98 | |
99 (defun rfc822-snarf-word () | |
100 ;; word is atom | quoted-string | |
101 (cond ((= (following-char) ?\") | |
102 ;; quoted-string | |
103 (or (rfc822-looking-at "\"\\([^\"\\\n]\\|\\\\.\\|\\\\\n\\)*\"") | |
104 (rfc822-bad-address "Unterminated quoted string"))) | |
105 ((rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\".]+") | |
106 ;; atom | |
107 ) | |
108 (t | |
109 (rfc822-bad-address "Rubbish in address")))) | |
110 | |
111 (defun rfc822-snarf-words () | |
112 (rfc822-snarf-word) | |
113 (while (rfc822-looking-at ?.) | |
114 (rfc822-snarf-word))) | |
115 | |
116 (defun rfc822-snarf-subdomain () | |
117 ;; sub-domain is domain-ref | domain-literal | |
118 (cond ((= (following-char) ?\[) | |
119 ;; domain-ref | |
120 (or (rfc822-looking-at "\\[\\([^][\\\n]\\|\\\\.\\|\\\\\n\\)*\\]") | |
121 (rfc822-bad-address "Unterminated domain literal [...]"))) | |
122 ((rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\".]+") | |
123 ;; domain-literal = atom | |
124 ) | |
125 (t | |
126 (rfc822-bad-address "Rubbish in host/domain specification")))) | |
127 | |
128 (defun rfc822-snarf-domain () | |
129 (rfc822-snarf-subdomain) | |
130 (while (rfc822-looking-at ?.) | |
131 (rfc822-snarf-subdomain))) | |
132 | |
133 (defun rfc822-snarf-frob-list (name separator terminator snarfer | |
134 &optional return) | |
135 (let ((first t) | |
136 (list ()) | |
137 tem) | |
138 (while (cond ((eobp) | |
139 (rfc822-bad-address | |
140 (format "End of addresses in middle of %s" name))) | |
141 ((rfc822-looking-at terminator) | |
142 nil) | |
143 ((rfc822-looking-at separator) | |
144 ;; multiple separators are allowed and do nothing. | |
145 (while (rfc822-looking-at separator)) | |
146 t) | |
147 (first | |
148 t) | |
149 (t | |
150 (rfc822-bad-address | |
151 (format "Gubbish in middle of %s" name)))) | |
152 (setq tem (funcall snarfer) | |
153 first nil) | |
154 (and return tem | |
155 (setq list (if (listp tem) | |
156 (nconc (reverse tem) list) | |
157 (cons tem list))))) | |
158 (nreverse list))) | |
159 | |
160 ;; return either an address (a string) or a list of addresses | |
161 (defun rfc822-addresses-1 (&optional allow-groups) | |
162 ;; Looking for an rfc822 `address' | |
163 ;; Either a group (1*word ":" [#mailbox] ";") | |
164 ;; or a mailbox (addr-spec | 1*word route-addr) | |
165 ;; addr-spec is (local-part "@" domain) | |
166 ;; route-addr is ("<" [1#("@" domain) ":"] addr-spec ">") | |
167 ;; local-part is (word *("." word)) | |
168 ;; word is (atom | quoted-string) | |
169 ;; quoted-string is ("\([^\"\\n]\|\\.\|\\\n\)") | |
170 ;; atom is [^\000-\037\177 ()<>@,;:\".[]]+ | |
171 ;; domain is sub-domain *("." sub-domain) | |
172 ;; sub-domain is domain-ref | domain-literal | |
173 ;; domain-literal is "[" *(dtext | quoted-pair) "]" | |
174 ;; dtext is "[^][\\n" | |
175 ;; domain-ref is atom | |
176 (let ((address-start (point)) | |
177 (n 0)) | |
178 (catch 'address | |
179 ;; optimize common cases: | |
180 ;; foo | |
181 ;; foo.bar@bar.zap | |
182 ;; followed by "\\'\\|,\\|([^()\\]*)\\'" | |
183 ;; other common cases are: | |
184 ;; foo bar <foo.bar@baz.zap> | |
185 ;; "foo bar" <foo.bar@baz.zap> | |
186 ;; those aren't hacked yet. | |
187 (if (and (rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\"]+\\(\\|@[^][\000-\037\177-\377 ()<>@,;:\\\"]+\\)" t) | |
188 (progn (or (eobp) | |
189 (rfc822-looking-at ?,)))) | |
190 (progn | |
191 ;; rfc822-looking-at may have inserted a space | |
192 (or (bobp) (/= (preceding-char) ?\ ) (delete-char -1)) | |
193 ;; relying on the fact that rfc822-looking-at <char> | |
194 ;; doesn't mung match-data | |
195 (throw 'address (buffer-substring address-start (match-end 0))))) | |
196 (goto-char address-start) | |
197 (while t | |
198 (cond ((and (= n 1) (rfc822-looking-at ?@)) | |
199 ;; local-part@domain | |
200 (rfc822-snarf-domain) | |
201 (throw 'address | |
202 (buffer-substring address-start (point)))) | |
203 ((rfc822-looking-at ?:) | |
204 (cond ((not allow-groups) | |
205 (rfc822-bad-address "A group name may not appear here")) | |
206 ((= n 0) | |
207 (rfc822-bad-address "No name for :...; group"))) | |
208 ;; group | |
209 (throw 'address | |
210 ;; return a list of addresses | |
211 (rfc822-snarf-frob-list ":...; group" ?\, ?\; | |
212 'rfc822-addresses-1 t))) | |
213 ((rfc822-looking-at ?<) | |
214 (let ((start (point)) | |
215 (strip t)) | |
216 (cond ((rfc822-looking-at ?>) | |
217 ;; empty path | |
218 ()) | |
219 ((and (not (eobp)) (= (following-char) ?\@)) | |
220 ;; <@foo.bar,@baz:quux@abcd.efg> | |
221 (rfc822-snarf-frob-list "<...> address" ?\, ?\: | |
222 (function (lambda () | |
223 (if (rfc822-looking-at ?\@) | |
224 (rfc822-snarf-domain) | |
225 (rfc822-bad-address | |
226 "Gubbish in route-addr"))))) | |
227 (rfc822-snarf-words) | |
228 (or (rfc822-looking-at ?@) | |
229 (rfc822-bad-address "Malformed <..@..> address")) | |
230 (rfc822-snarf-domain) | |
231 (setq strip nil)) | |
232 ((progn (rfc822-snarf-words) (rfc822-looking-at ?@)) | |
233 ; allow <foo> (losing unix seems to do this) | |
234 (rfc822-snarf-domain))) | |
235 (let ((end (point))) | |
236 (if (rfc822-looking-at ?\>) | |
237 (throw 'address | |
238 (buffer-substring (if strip start (1- start)) | |
239 (if strip end (1+ end)))) | |
240 (rfc822-bad-address "Unterminated <...> address"))))) | |
241 ((looking-at "[^][\000-\037\177-\377 ()<>@,;:\\.]") | |
242 ;; this allows "." to be part of the words preceding | |
243 ;; an addr-spec, since many broken mailers output | |
244 ;; "Hern K. Herklemeyer III | |
245 ;; <yank@megadeath.dod.gods-own-country>" | |
125 | 246 (let ((again t)) |
247 (while again | |
248 (or (= n 0) (bobp) (= (preceding-char) ?\ ) | |
249 (insert ?\ )) | |
126 | 250 (rfc822-snarf-words) |
125 | 251 (setq n (1+ n)) |
252 (setq again (or (rfc822-looking-at ?.) | |
253 (looking-at "[^][\000-\037\177-\377 ()<>@,;:\\.]")))))) | |
43 | 254 ((= n 0) |
255 (throw 'address nil)) | |
256 ((= n 1) ; allow "foo" (losing unix seems to do this) | |
257 (throw 'address | |
258 (buffer-substring address-start (point)))) | |
125 | 259 ((> n 1) |
260 (rfc822-bad-address "Missing comma between addresses or badly-formatted address")) | |
261 ((or (eobp) (= (following-char) ?,)) | |
43 | 262 (rfc822-bad-address "Missing comma or route-spec")) |
263 (t | |
264 (rfc822-bad-address "Strange character or missing comma"))))))) | |
265 | |
266 | |
267 (defun rfc822-addresses (header-text) | |
268 (if (string-match "\\`[ \t]*\\([^][\000-\037\177-\377 ()<>@,;:\\\".]+\\)[ \t]*\\'" | |
269 header-text) | |
270 ;; Make very simple case moderately fast. | |
271 (list (substring header-text (match-beginning 1) (match-end 1))) | |
272 (let ((buf (generate-new-buffer " rfc822"))) | |
273 (unwind-protect | |
274 (save-excursion | |
275 (set-buffer buf) | |
276 (make-local-variable 'case-fold-search) | |
277 (setq case-fold-search nil) ;For speed(?) | |
278 (insert header-text) | |
279 ;; unfold continuation lines | |
280 (goto-char (point-min)) | |
281 | |
282 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t) | |
283 (replace-match "\\1 " t)) | |
284 | |
285 (goto-char (point-min)) | |
286 (rfc822-nuke-whitespace) | |
287 (let ((list ()) | |
288 tem | |
289 address-start); this is for rfc822-bad-address | |
290 (while (not (eobp)) | |
291 (setq address-start (point)) | |
292 (setq tem | |
293 (catch 'address ; this is for rfc822-bad-address | |
294 (cond ((rfc822-looking-at ?\,) | |
295 nil) | |
122 | 296 ((looking-at "[][\000-\037\177-\377@;:\\.>)]") |
43 | 297 (forward-char) |
298 (rfc822-bad-address | |
299 (format "Strange character \\%c found" | |
300 (preceding-char)))) | |
301 (t | |
302 (rfc822-addresses-1 t))))) | |
303 (cond ((null tem)) | |
304 ((stringp tem) | |
305 (setq list (cons tem list))) | |
306 (t | |
307 (setq list (nconc (nreverse tem) list))))) | |
308 (nreverse list))) | |
309 (and buf (kill-buffer buf)))))) | |
310 | |
584 | 311 (provide 'rfc822) |
312 | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
313 ;;; rfc822.el ends here |