1 ;;; rfc822.el --- hairy rfc822 parser for mail and news and suchlike
3 ;; Copyright (C) 1986, 87, 1990 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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.
27 ;; Support functions for parsing RFC-822 headers, used by mail and news
32 ;; uses address-start free, throws to address
33 (defun rfc822-bad-address (reason)
36 (narrow-to-region address-start
37 (if (re-search-forward "[,;]" nil t
)
38 (max (point-min) (1- (point)))
40 ;; make the error string be suitable for inclusion in (...)
41 (let ((losers '("\\" "(" ")" "\n")))
43 (goto-char (point-min))
44 (while (search-forward (car losers
) nil t
)
48 (setq losers
(cdr losers
))))
49 (goto-char (point-min)) (insert "(Unparsable address -- "
52 (goto-char (point-max)) (insert "\")"))
53 (rfc822-nuke-whitespace)
54 (throw 'address
(buffer-substring address-start
(point))))
56 (defun rfc822-nuke-whitespace (&optional leave-space
)
60 ((= (setq ch
(following-char)) ?\
()
63 (rfc822-bad-address "Unbalanced comment (...)")
64 (/= (setq ch
(following-char)) ?\
)))
65 (cond ((looking-at "[^()\\]+")
68 (rfc822-nuke-whitespace))
69 ((< (point) (1- (point-max)))
72 (rfc822-bad-address "orphaned backslash"))))
73 ;; delete remaining "()"
77 ((memq ch
'(?\ ?
\t ?
\n))
78 (delete-region (point)
79 (progn (skip-chars-forward " \t\n") (point)))
86 (= (preceding-char) ?\
)
89 (defun rfc822-looking-at (regex &optional leave-space
)
90 (if (cond ((stringp regex
)
91 (if (looking-at regex
)
92 (progn (goto-char (match-end 0))
96 (= (following-char) regex
))
97 (progn (forward-char 1)
99 (let ((tem (match-data)))
100 (rfc822-nuke-whitespace leave-space
)
101 (store-match-data tem
)
104 (defun rfc822-snarf-word ()
105 ;; word is atom | quoted-string
106 (cond ((= (following-char) ?
\")
108 (or (rfc822-looking-at "\"\\([^\"\\\n]\\|\\\\.\\|\\\\\n\\)*\"")
109 (rfc822-bad-address "Unterminated quoted string")))
110 ((rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\".]+")
114 (rfc822-bad-address "Rubbish in address"))))
116 (defun rfc822-snarf-words ()
118 (while (rfc822-looking-at ?.
)
119 (rfc822-snarf-word)))
121 (defun rfc822-snarf-subdomain ()
122 ;; sub-domain is domain-ref | domain-literal
123 (cond ((= (following-char) ?\
[)
125 (or (rfc822-looking-at "\\[\\([^][\\\n]\\|\\\\.\\|\\\\\n\\)*\\]")
126 (rfc822-bad-address "Unterminated domain literal [...]")))
127 ((rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\".]+")
128 ;; domain-literal = atom
131 (rfc822-bad-address "Rubbish in host/domain specification"))))
133 (defun rfc822-snarf-domain ()
134 (rfc822-snarf-subdomain)
135 (while (rfc822-looking-at ?.
)
136 (rfc822-snarf-subdomain)))
138 (defun rfc822-snarf-frob-list (name separator terminator snarfer
145 (format "End of addresses in middle of %s" name
)))
146 ((rfc822-looking-at terminator
)
148 ((rfc822-looking-at separator
)
149 ;; multiple separators are allowed and do nothing.
150 (while (rfc822-looking-at separator
))
156 (format "Gubbish in middle of %s" name
))))
157 (setq tem
(funcall snarfer
)
160 (setq list
(if (listp tem
)
161 (nconc (reverse tem
) list
)
165 ;; return either an address (a string) or a list of addresses
166 (defun rfc822-addresses-1 (&optional allow-groups
)
167 ;; Looking for an rfc822 `address'
168 ;; Either a group (1*word ":" [#mailbox] ";")
169 ;; or a mailbox (addr-spec | 1*word route-addr)
170 ;; addr-spec is (local-part "@" domain)
171 ;; route-addr is ("<" [1#("@" domain) ":"] addr-spec ">")
172 ;; local-part is (word *("." word))
173 ;; word is (atom | quoted-string)
174 ;; quoted-string is ("\([^\"\\n]\|\\.\|\\\n\)")
175 ;; atom is [^\000-\037\177 ()<>@,;:\".[]]+
176 ;; domain is sub-domain *("." sub-domain)
177 ;; sub-domain is domain-ref | domain-literal
178 ;; domain-literal is "[" *(dtext | quoted-pair) "]"
179 ;; dtext is "[^][\\n"
180 ;; domain-ref is atom
181 (let ((address-start (point))
184 ;; optimize common cases:
187 ;; followed by "\\'\\|,\\|([^()\\]*)\\'"
188 ;; other common cases are:
189 ;; foo bar <foo.bar@baz.zap>
190 ;; "foo bar" <foo.bar@baz.zap>
191 ;; those aren't hacked yet.
192 (if (and (rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\"]+\\(\\|@[^][\000-\037\177-\377 ()<>@,;:\\\"]+\\)" t
)
194 (rfc822-looking-at ?
,))))
196 ;; rfc822-looking-at may have inserted a space
197 (or (bobp) (/= (preceding-char) ?\
) (delete-char -
1))
198 ;; relying on the fact that rfc822-looking-at <char>
199 ;; doesn't mung match-data
200 (throw 'address
(buffer-substring address-start
(match-end 0)))))
201 (goto-char address-start
)
203 (cond ((and (= n
1) (rfc822-looking-at ?
@))
205 (rfc822-snarf-domain)
207 (buffer-substring address-start
(point))))
208 ((rfc822-looking-at ?
:)
209 (cond ((not allow-groups
)
210 (rfc822-bad-address "A group name may not appear here"))
212 (rfc822-bad-address "No name for :...; group")))
215 ;; return a list of addresses
216 (rfc822-snarf-frob-list ":...; group" ?\
, ?\
;
217 'rfc822-addresses-1 t
)))
218 ((rfc822-looking-at ?
<)
219 (let ((start (point))
221 (cond ((rfc822-looking-at ?
>)
224 ((and (not (eobp)) (= (following-char) ?\
@))
225 ;; <@foo.bar,@baz:quux@abcd.efg>
226 (rfc822-snarf-frob-list "<...> address" ?\
, ?\
:
228 (if (rfc822-looking-at ?\
@)
229 (rfc822-snarf-domain)
231 "Gubbish in route-addr")))))
233 (or (rfc822-looking-at ?
@)
234 (rfc822-bad-address "Malformed <..@..> address"))
235 (rfc822-snarf-domain)
237 ((progn (rfc822-snarf-words) (rfc822-looking-at ?
@))
238 ; allow <foo> (losing unix seems to do this)
239 (rfc822-snarf-domain)))
241 (if (rfc822-looking-at ?\
>)
243 (buffer-substring (if strip start
(1- start
))
244 (if strip end
(1+ end
))))
245 (rfc822-bad-address "Unterminated <...> address")))))
246 ((looking-at "[^][\000-\037\177-\377 ()<>@,;:\\.]")
247 ;; this allows "." to be part of the words preceding
248 ;; an addr-spec, since many broken mailers output
249 ;; "Hern K. Herklemeyer III
250 ;; <yank@megadeath.dod.gods-own-country>"
253 (or (= n
0) (bobp) (= (preceding-char) ?\
)
257 (setq again
(or (rfc822-looking-at ?.
)
258 (looking-at "[^][\000-\037\177-\377 ()<>@,;:\\.]"))))))
260 (throw 'address nil
))
261 ((= n
1) ; allow "foo" (losing unix seems to do this)
263 (buffer-substring address-start
(point))))
265 (rfc822-bad-address "Missing comma between addresses or badly-formatted address"))
266 ((or (eobp) (= (following-char) ?
,))
267 (rfc822-bad-address "Missing comma or route-spec"))
269 (rfc822-bad-address "Strange character or missing comma")))))))
272 (defun rfc822-addresses (header-text)
273 (if (string-match "\\`[ \t]*\\([^][\000-\037\177-\377 ()<>@,;:\\\".]+\\)[ \t]*\\'"
275 ;; Make very simple case moderately fast.
276 (list (substring header-text
(match-beginning 1) (match-end 1)))
277 (let ((buf (generate-new-buffer " rfc822")))
281 (make-local-variable 'case-fold-search
)
282 (setq case-fold-search nil
) ;For speed(?)
284 ;; unfold continuation lines
285 (goto-char (point-min))
287 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t
)
288 (replace-match "\\1 " t
))
290 (goto-char (point-min))
291 (rfc822-nuke-whitespace)
294 address-start
); this is for rfc822-bad-address
296 (setq address-start
(point))
298 (catch 'address
; this is for rfc822-bad-address
299 (cond ((rfc822-looking-at ?\
,)
301 ((looking-at "[][\000-\037\177-\377@;:\\.>)]")
304 (format "Strange character \\%c found"
307 (rfc822-addresses-1 t
)))))
310 (setq list
(cons tem list
)))
312 (setq list
(nconc (nreverse tem
) list
)))))
314 (and buf
(kill-buffer buf
))))))
318 ;;; rfc822.el ends here