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