annotate lisp/utils/rfc822.el @ 0:376386a54a3c r19-14

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