Commit | Line | Data |
---|---|---|
b1ccc1ce | 1 | ;;; mail-extr.el --- extract full name and address from RFC 822 mail header -*- coding: utf-8 -*- |
72c0ae01 | 2 | |
0b725d8c GM |
3 | ;; Copyright (C) 1991, 1992, 1993, 1994, 1997, 2001 |
4 | ;; Free Software Foundation, Inc. | |
3a801d0c | 5 | |
72c0ae01 | 6 | ;; Author: Joe Wells <jbw@cs.bu.edu> |
d1782bd8 | 7 | ;; Maintainer: FSF |
72c0ae01 ER |
8 | ;; Keywords: mail |
9 | ||
72c0ae01 ER |
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 | |
154b3e39 | 14 | ;; the Free Software Foundation; either version 2, or (at your option) |
72c0ae01 ER |
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 | |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
72c0ae01 ER |
26 | |
27 | ;;; Commentary: | |
28 | ||
154b3e39 RS |
29 | ;; The entry point of this code is |
30 | ;; | |
d1782bd8 | 31 | ;; mail-extract-address-components: (address &optional all) |
6c83d99f | 32 | ;; |
154b3e39 RS |
33 | ;; Given an RFC-822 ADDRESS, extract full name and canonical address. |
34 | ;; Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). | |
35 | ;; If no name can be extracted, FULL-NAME will be nil. | |
6c83d99f | 36 | ;; ADDRESS may be a string or a buffer. If it is a buffer, the visible |
154b3e39 RS |
37 | ;; (narrowed) portion of the buffer will be interpreted as the address. |
38 | ;; (This feature exists so that the clever caller might be able to avoid | |
39 | ;; consing a string.) | |
40 | ;; If ADDRESS contains more than one RFC-822 address, only the first is | |
41 | ;; returned. | |
42 | ;; | |
d1782bd8 KH |
43 | ;; If ALL is non-nil, that means return info about all the addresses |
44 | ;; that are found in ADDRESS. The value is a list of elements of | |
45 | ;; the form (FULL-NAME CANONICAL-ADDRESS), one per address. | |
46 | ;; | |
154b3e39 RS |
47 | ;; This code is more correct (and more heuristic) parser than the code in |
48 | ;; rfc822.el. And despite its size, it's fairly fast. | |
49 | ;; | |
72c0ae01 | 50 | ;; There are two main benefits: |
154b3e39 | 51 | ;; |
72c0ae01 | 52 | ;; 1. Higher probability of getting the correct full name for a human than |
154b3e39 | 53 | ;; any other package we know of. (On the other hand, it will cheerfully |
72c0ae01 ER |
54 | ;; mangle non-human names/comments.) |
55 | ;; 2. Address part is put in a canonical form. | |
154b3e39 RS |
56 | ;; |
57 | ;; The interface is not yet carved in stone; please give us suggestions. | |
58 | ;; | |
59 | ;; We have an extensive test-case collection of funny addresses if you want to | |
72c0ae01 | 60 | ;; work with the code. Developing this code requires frequent testing to |
154b3e39 RS |
61 | ;; make sure you're not breaking functionality. The test cases aren't included |
62 | ;; because they are over 100K. | |
63 | ;; | |
6c83d99f | 64 | ;; If you find an address that mail-extr fails on, please send it to the |
154b3e39 RS |
65 | ;; maintainer along with what you think the correct results should be. We do |
66 | ;; not consider it a bug if mail-extr mangles a comment that does not | |
6c83d99f | 67 | ;; correspond to a real human full name, although we would prefer that |
154b3e39 RS |
68 | ;; mail-extr would return the comment as-is. |
69 | ;; | |
72c0ae01 | 70 | ;; Features: |
154b3e39 | 71 | ;; |
72c0ae01 | 72 | ;; * Full name handling: |
154b3e39 | 73 | ;; |
72c0ae01 ER |
74 | ;; * knows where full names can be found in an address. |
75 | ;; * avoids using empty comments and quoted text. | |
76 | ;; * extracts full names from mailbox names. | |
77 | ;; * recognizes common formats for comments after a full name. | |
78 | ;; * puts a period and a space after each initial. | |
154b3e39 RS |
79 | ;; * understands & referring to the mailbox name, capitalized. |
80 | ;; * strips name prefixes like "Prof.", etc. | |
72c0ae01 ER |
81 | ;; * understands what characters can occur in names (not just letters). |
82 | ;; * figures out middle initial from mailbox name. | |
83 | ;; * removes funny nicknames. | |
84 | ;; * keeps suffixes such as Jr., Sr., III, etc. | |
85 | ;; * reorders "Last, First" type names. | |
154b3e39 | 86 | ;; |
72c0ae01 | 87 | ;; * Address handling: |
154b3e39 | 88 | ;; |
72c0ae01 ER |
89 | ;; * parses rfc822 quoted text, comments, and domain literals. |
90 | ;; * parses rfc822 multi-line headers. | |
91 | ;; * does something reasonable with rfc822 GROUP addresses. | |
92 | ;; * handles many rfc822 noncompliant and garbage addresses. | |
93 | ;; * canonicalizes addresses (after stripping comments/phrases outside <>). | |
94 | ;; * converts ! addresses into .UUCP and %-style addresses. | |
95 | ;; * converts rfc822 ROUTE addresses to %-style addresses. | |
96 | ;; * truncates %-style addresses at leftmost fully qualified domain name. | |
97 | ;; * handles local relative precedence of ! vs. % and @ (untested). | |
154b3e39 | 98 | ;; |
72c0ae01 ER |
99 | ;; It does almost no string creation. It primarily uses the built-in |
100 | ;; parsing routines with the appropriate syntax tables. This should | |
101 | ;; result in greater speed. | |
154b3e39 | 102 | ;; |
72c0ae01 | 103 | ;; TODO: |
154b3e39 | 104 | ;; |
72c0ae01 ER |
105 | ;; * handle all test cases. (This will take forever.) |
106 | ;; * software to pick the correct header to use (eg., "Senders-Name:"). | |
107 | ;; * multiple addresses in the "From:" header (almost all of the necessary | |
108 | ;; code is there). | |
109 | ;; * flag to not treat `,' as an address separator. (This is useful when | |
110 | ;; there is a "From:" header but no "Sender:" header, because then there | |
111 | ;; is only allowed to be one address.) | |
112 | ;; * mailbox name does not necessarily contain full name. | |
113 | ;; * fixing capitalization when it's all upper or lowercase. (Hard!) | |
114 | ;; * some of the domain literal handling is missing. (But I've never even | |
115 | ;; seen one of these in a mail address, so maybe no big deal.) | |
116 | ;; * arrange to have syntax tables byte-compiled. | |
117 | ;; * speed hacks. | |
118 | ;; * delete unused variables. | |
119 | ;; * arrange for testing with different relative precedences of ! vs. @ | |
120 | ;; and %. | |
72c0ae01 ER |
121 | ;; * insert documentation strings! |
122 | ;; * handle X.400-gatewayed addresses according to RFC 1148. | |
123 | ||
6c83d99f JB |
124 | ;;; Change Log: |
125 | ;; | |
154b3e39 RS |
126 | ;; Thu Feb 17 17:57:33 1994 Jamie Zawinski (jwz@lucid.com) |
127 | ;; | |
128 | ;; * merged with jbw's latest version | |
129 | ;; | |
130 | ;; Wed Feb 9 21:56:27 1994 Jamie Zawinski (jwz@lucid.com) | |
131 | ;; | |
132 | ;; * high-bit chars in comments weren't treated as word syntax | |
133 | ;; | |
134 | ;; Sat Feb 5 03:13:40 1994 Jamie Zawinski (jwz@lucid.com) | |
135 | ;; | |
136 | ;; * call replace-match with fixed-case arg | |
137 | ;; | |
138 | ;; Thu Dec 16 21:56:45 1993 Jamie Zawinski (jwz@lucid.com) | |
139 | ;; | |
140 | ;; * some more cleanup, doc, added provide | |
141 | ;; | |
142 | ;; Tue Mar 23 21:23:18 1993 Joe Wells (jbw at csd.bu.edu) | |
6c83d99f | 143 | ;; |
154b3e39 | 144 | ;; * Made mail-full-name-prefixes a user-customizable variable. |
79814626 | 145 | ;; Allow passing the address as a buffer as well as a string. |
154b3e39 | 146 | ;; Allow [ and ] as name characters (Finnish character set). |
6c83d99f | 147 | ;; |
154b3e39 | 148 | ;; Mon Mar 22 21:20:56 1993 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 149 | ;; |
154b3e39 RS |
150 | ;; * Handle "null" addresses. Handle = used for spacing in mailbox |
151 | ;; name. Fix bug in handling of ROUTE-ADDR-type addresses that are | |
152 | ;; missing their brackets. Handle uppercase "JR". Extract full | |
153 | ;; names from X.400 addresses encoded in RFC-822. Fix bug in | |
154 | ;; handling of multiple addresses where first has trailing comment. | |
155 | ;; Handle more kinds of telephone extension lead-ins. | |
6c83d99f | 156 | ;; |
154b3e39 | 157 | ;; Mon Mar 22 20:16:57 1993 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 158 | ;; |
154b3e39 | 159 | ;; * Handle HZ encoding for embedding GB encoded chinese characters. |
6c83d99f | 160 | ;; |
154b3e39 | 161 | ;; Mon Mar 22 00:46:12 1993 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 162 | ;; |
154b3e39 RS |
163 | ;; * Fixed too broad matching of ham radio call signs. Fixed bug in |
164 | ;; handling an unmatched ' in a name string. Enhanced recognition | |
165 | ;; of when . in the mailbox name terminates the name portion. | |
166 | ;; Narrowed conversion of . to space to only the necessary | |
167 | ;; situation. Deal with VMS's stupid date stamps. Handle a unique | |
168 | ;; way of introducing an alternate address. Fixed spacing bug I | |
169 | ;; introduced in switching last name order. Fixed bug in handling | |
170 | ;; address with ! and % but no @. Narrowed the cases in which | |
171 | ;; certain trailing words are discarded. | |
6c83d99f | 172 | ;; |
154b3e39 | 173 | ;; Sun Mar 21 21:41:06 1993 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 174 | ;; |
154b3e39 RS |
175 | ;; * Fixed bugs in handling GROUP addresses. Certain words in the |
176 | ;; middle of a name no longer terminate it. Handle LISTSERV list | |
177 | ;; names. Ignore comment field containing mailbox name. | |
6c83d99f | 178 | ;; |
154b3e39 | 179 | ;; Sun Mar 21 14:39:38 1993 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 180 | ;; |
154b3e39 RS |
181 | ;; * Moved variant-method code back into main function. Handle |
182 | ;; underscores as spaces in comments. Handle leading nickname. Add | |
183 | ;; flag to ignore single-word names. Other changes. | |
6c83d99f | 184 | ;; |
154b3e39 | 185 | ;; Mon Feb 1 22:23:31 1993 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 186 | ;; |
154b3e39 RS |
187 | ;; * Added in changes by Rod Whitby and Jamie Zawinski. This |
188 | ;; includes the flag mail-extr-guess-middle-initial and the fix for | |
6b07c06e RS |
189 | ;; handling multiple addresses correctly. (Whitby just changed |
190 | ;; a > to a <.) | |
6c83d99f | 191 | ;; |
72c0ae01 | 192 | ;; Mon Apr 6 23:59:09 1992 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 193 | ;; |
72c0ae01 | 194 | ;; * Cleaned up some more. Release version 1.0 to world. |
6c83d99f | 195 | ;; |
72c0ae01 | 196 | ;; Sun Apr 5 19:39:08 1992 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 197 | ;; |
72c0ae01 | 198 | ;; * Cleaned up full name extraction extensively. |
6c83d99f | 199 | ;; |
72c0ae01 | 200 | ;; Sun Feb 2 14:45:24 1992 Joe Wells (jbw at bigbird.bu.edu) |
6c83d99f | 201 | ;; |
72c0ae01 ER |
202 | ;; * Total rewrite. Integrated mail-canonicalize-address into |
203 | ;; mail-extract-address-components. Now handles GROUP addresses more | |
204 | ;; or less correctly. Better handling of lots of different cases. | |
6c83d99f | 205 | ;; |
72c0ae01 ER |
206 | ;; Fri Jun 14 19:39:50 1991 |
207 | ;; * Created. | |
208 | ||
209 | ;;; Code: | |
210 | \f | |
72c0ae01 | 211 | |
0b5bb3ec SE |
212 | (defgroup mail-extr nil |
213 | "Extract full name and address from RFC 822 mail header." | |
214 | :prefix "mail-extr-" | |
215 | :group 'mail) | |
216 | ||
154b3e39 RS |
217 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
218 | ;; | |
219 | ;; User configuration variable definitions. | |
220 | ;; | |
221 | ||
0b5bb3ec | 222 | (defcustom mail-extr-guess-middle-initial nil |
154b3e39 RS |
223 | "*Whether to try to guess middle initial from mail address. |
224 | If true, then when we see an address like \"John Smith <jqs@host.com>\" | |
0b5bb3ec SE |
225 | we will assume that \"John Q. Smith\" is the fellow's name." |
226 | :type 'boolean | |
227 | :group 'mail-extr) | |
154b3e39 | 228 | |
2a487c6c | 229 | (defcustom mail-extr-ignore-single-names nil |
154b3e39 RS |
230 | "*Whether to ignore a name that is just a single word. |
231 | If true, then when we see an address like \"Idiot <dumb@stupid.com>\" | |
0b5bb3ec SE |
232 | we will act as though we couldn't find a full name in the address." |
233 | :type 'boolean | |
234 | :group 'mail-extr) | |
154b3e39 RS |
235 | |
236 | ;; Matches a leading title that is not part of the name (does not | |
237 | ;; contribute to uniquely identifying the person). | |
0b5bb3ec | 238 | (defcustom mail-extr-full-name-prefixes |
154b3e39 RS |
239 | (purecopy |
240 | "\\(Prof\\|D[Rr]\\|Mrs?\\|Rev\\|Rabbi\\|SysOp\\|LCDR\\)\\.?[ \t\n]") | |
241 | "*Matches prefixes to the full name that identify a person's position. | |
242 | These are stripped from the full name because they do not contribute to | |
0b5bb3ec SE |
243 | uniquely identifying the person." |
244 | :type 'regexp | |
245 | :group 'mail-extr) | |
154b3e39 | 246 | |
0b5bb3ec SE |
247 | (defcustom mail-extr-@-binds-tighter-than-! nil |
248 | "*Whether the local mail transport agent looks at ! before @." | |
249 | :type 'boolean | |
250 | :group 'mail-extr) | |
154b3e39 | 251 | |
0b5bb3ec | 252 | (defcustom mail-extr-mangle-uucp nil |
154b3e39 | 253 | "*Whether to throw away information in UUCP addresses |
0b5bb3ec SE |
254 | by translating things like \"foo!bar!baz@host\" into \"baz@bar.UUCP\"." |
255 | :type 'boolean | |
256 | :group 'mail-extr) | |
72c0ae01 ER |
257 | |
258 | ;;---------------------------------------------------------------------- | |
259 | ;; what orderings are meaningful????? | |
260 | ;;(defvar mail-operator-precedence-list '(?! ?% ?@)) | |
261 | ;; Right operand of a % or a @ must be a domain name, period. No other | |
262 | ;; operators allowed. Left operand of a @ is an address relative to that | |
263 | ;; site. | |
264 | ||
265 | ;; Left operand of a ! must be a domain name. Right operand is an | |
266 | ;; arbitrary address. | |
267 | ;;---------------------------------------------------------------------- | |
268 | ||
154b3e39 | 269 | \f |
72c0ae01 | 270 | |
154b3e39 RS |
271 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
272 | ;; | |
273 | ;; Constant definitions. | |
274 | ;; | |
275 | ||
276 | ;; Codes in | |
277 | ;; Names in ISO 8859-1 Name | |
278 | ;; ISO 10XXX ISO 8859-2 in | |
279 | ;; ISO 6937 ISO 10646 RFC Swedish | |
280 | ;; etc. Hex Oct 1345 TeX Split ASCII Description | |
281 | ;; --------- ---------- ---- --- ----- ----- ------------------------------- | |
282 | ;; %a E4 344 a: \"a ae { latin small a + diaeresis d | |
283 | ;; %o F6 366 o: \"o oe | latin small o + diaeresis v | |
284 | ;; @a E5 345 aa \oa aa } latin small a + ring above e | |
285 | ;; %u FC 374 u: \"u ue ~ latin small u + diaeresis | | |
286 | ;; /e E9 351 e' \'e ` latin small e + acute i | |
287 | ;; %A C4 304 A: \"A AE [ latin capital a + diaeresis D | |
288 | ;; %O D6 326 O: \"O OE \ latin capital o + diaeresis V | |
289 | ;; @A C5 305 AA \oA AA ] latin capital a + ring above E | |
290 | ;; %U DC 334 U: \"U UE ^ latin capital u + diaeresis \ | |
291 | ;; /E C9 311 E' \'E @ latin capital e + acute I | |
292 | ||
293 | ;; NOTE: @a and @A are not in ISO 8859-2 (the codes mentioned above invoke | |
294 | ;; /l and /L). Some of this data was retrieved from | |
295 | ;; listserv@jhuvm.hcf.jhu.edu. | |
72c0ae01 ER |
296 | |
297 | ;; Any character that can occur in a name, not counting characters that | |
154b3e39 RS |
298 | ;; separate parts of a multipart name (hyphen and period). |
299 | ;; Yes, there are weird people with digits in their names. | |
300 | ;; You will also notice the consideration for the | |
301 | ;; Swedish/Finnish/Norwegian character set. | |
154b3e39 | 302 | (defconst mail-extr-all-letters-but-separators |
6e66e4c6 | 303 | (purecopy "][[:alnum:]{|}'~`")) |
154b3e39 RS |
304 | |
305 | ;; Any character that can occur in a name in an RFC822 address including | |
306 | ;; the separator (hyphen and possibly period) for multipart names. | |
307 | ;; #### should . be in here? | |
308 | (defconst mail-extr-all-letters | |
309 | (purecopy (concat mail-extr-all-letters-but-separators "---"))) | |
310 | ||
311 | ;; Any character that can start a name. | |
312 | ;; Keep this set as minimal as possible. | |
6e66e4c6 | 313 | (defconst mail-extr-first-letters (purecopy "[:alpha:]")) |
72c0ae01 ER |
314 | |
315 | ;; Any character that can end a name. | |
154b3e39 | 316 | ;; Keep this set as minimal as possible. |
6e66e4c6 | 317 | (defconst mail-extr-last-letters (purecopy "[:alpha:]`'.")) |
72c0ae01 | 318 | |
af604656 | 319 | (defconst mail-extr-leading-garbage "\\W+") |
72c0ae01 | 320 | |
6c83d99f | 321 | ;; (defconst mail-extr-non-name-chars |
154b3e39 RS |
322 | ;; (purecopy (concat "^" mail-extr-all-letters "."))) |
323 | ;; (defconst mail-extr-non-begin-name-chars | |
324 | ;; (purecopy (concat "^" mail-extr-first-letters))) | |
325 | ;; (defconst mail-extr-non-end-name-chars | |
326 | ;; (purecopy (concat "^" mail-extr-last-letters))) | |
72c0ae01 | 327 | |
6c83d99f | 328 | ;; Matches an initial not followed by both a period and a space. |
154b3e39 | 329 | ;; (defconst mail-extr-bad-initials-pattern |
6c83d99f | 330 | ;; (purecopy |
154b3e39 RS |
331 | ;; (format "\\(\\([^%s]\\|\\`\\)[%s]\\)\\(\\.\\([^ ]\\)\\| \\|\\([^%s .]\\)\\|\\'\\)" |
332 | ;; mail-extr-all-letters mail-extr-first-letters mail-extr-all-letters))) | |
72c0ae01 ER |
333 | |
334 | ;; Matches periods used instead of spaces. Must not match the period | |
335 | ;; following an initial. | |
154b3e39 RS |
336 | (defconst mail-extr-bad-dot-pattern |
337 | (purecopy | |
338 | (format "\\([%s][%s]\\)\\.+\\([%s]\\)" | |
339 | mail-extr-all-letters | |
340 | mail-extr-last-letters | |
341 | mail-extr-first-letters))) | |
72c0ae01 ER |
342 | |
343 | ;; Matches an embedded or leading nickname that should be removed. | |
154b3e39 RS |
344 | ;; (defconst mail-extr-nickname-pattern |
345 | ;; (purecopy | |
346 | ;; (format "\\([ .]\\|\\`\\)[\"'`\[\(]\\([ .%s]+\\)[\]\"'\)] " | |
347 | ;; mail-extr-all-letters))) | |
72c0ae01 ER |
348 | |
349 | ;; Matches the occurrence of a generational name suffix, and the last | |
154b3e39 RS |
350 | ;; character of the preceding name. This is important because we want to |
351 | ;; keep such suffixes: they help to uniquely identify the person. | |
352 | ;; *** Perhaps this should be a user-customizable variable. However, the | |
353 | ;; *** regular expression is fairly tricky to alter, so maybe not. | |
354 | (defconst mail-extr-full-name-suffix-pattern | |
355 | (purecopy | |
356 | (format | |
357 | "\\(,? ?\\([JjSs][Rr]\\.?\\|V?I+V?\\)\\)\\([^%s]\\([^%s]\\|\\'\\)\\|\\'\\)" | |
358 | mail-extr-all-letters mail-extr-all-letters))) | |
359 | ||
360 | (defconst mail-extr-roman-numeral-pattern (purecopy "V?I+V?\\b")) | |
72c0ae01 ER |
361 | |
362 | ;; Matches a trailing uppercase (with other characters possible) acronym. | |
363 | ;; Must not match a trailing uppercase last name or trailing initial | |
154b3e39 RS |
364 | (defconst mail-extr-weird-acronym-pattern |
365 | (purecopy "\\([A-Z]+[-_/]\\|[A-Z][A-Z][A-Z]?\\b\\)")) | |
6c83d99f | 366 | |
72c0ae01 | 367 | ;; Matches a mixed-case or lowercase name (not an initial). |
154b3e39 RS |
368 | ;; #### Match Latin1 lower case letters here too? |
369 | ;; (defconst mail-extr-mixed-case-name-pattern | |
370 | ;; (purecopy | |
371 | ;; (format | |
372 | ;; "\\b\\([a-z][%s]*[%s]\\|[%s][%s]*[a-z][%s]*[%s]\\|[%s][%s]*[a-z]\\)" | |
373 | ;; mail-extr-all-letters mail-extr-last-letters | |
374 | ;; mail-extr-first-letters mail-extr-all-letters mail-extr-all-letters | |
375 | ;; mail-extr-last-letters mail-extr-first-letters mail-extr-all-letters))) | |
72c0ae01 ER |
376 | |
377 | ;; Matches a trailing alternative address. | |
154b3e39 | 378 | ;; #### Match Latin1 letters here too? |
6c83d99f | 379 | ;; #### Match _ before @ here too? |
154b3e39 RS |
380 | (defconst mail-extr-alternative-address-pattern |
381 | (purecopy "\\(aka *\\)?[a-zA-Z.]+[!@][a-zA-Z.]")) | |
72c0ae01 ER |
382 | |
383 | ;; Matches a variety of trailing comments not including comma-delimited | |
384 | ;; comments. | |
154b3e39 RS |
385 | (defconst mail-extr-trailing-comment-start-pattern |
386 | (purecopy " [-{]\\|--\\|[+@#></\;]")) | |
72c0ae01 ER |
387 | |
388 | ;; Matches a name (not an initial). | |
389 | ;; This doesn't force a word boundary at the end because sometimes a | |
390 | ;; comment is separated by a `-' with no preceding space. | |
154b3e39 RS |
391 | (defconst mail-extr-name-pattern |
392 | (purecopy (format "\\b[%s][%s]*[%s]" | |
393 | mail-extr-first-letters | |
394 | mail-extr-all-letters | |
395 | mail-extr-last-letters))) | |
72c0ae01 | 396 | |
154b3e39 RS |
397 | (defconst mail-extr-initial-pattern |
398 | (purecopy (format "\\b[%s]\\([. ]\\|\\b\\)" mail-extr-first-letters))) | |
72c0ae01 ER |
399 | |
400 | ;; Matches a single name before a comma. | |
154b3e39 RS |
401 | ;; (defconst mail-extr-last-name-first-pattern |
402 | ;; (purecopy (concat "\\`" mail-extr-name-pattern ","))) | |
72c0ae01 ER |
403 | |
404 | ;; Matches telephone extensions. | |
154b3e39 RS |
405 | (defconst mail-extr-telephone-extension-pattern |
406 | (purecopy | |
407 | "\\(\\([Ee]xt\\|\\|[Tt]ph\\|[Tt]el\\|[Xx]\\).?\\)? *\\+?[0-9][- 0-9]+")) | |
72c0ae01 ER |
408 | |
409 | ;; Matches ham radio call signs. | |
154b3e39 RS |
410 | ;; Help from: Mat Maessen N2NJZ <maessm@rpi.edu>, Mark Feit |
411 | ;; <mark@era.com>, Michael Covington <mcovingt@ai.uga.edu>. | |
412 | ;; Examples: DX504 DX515 K5MRU K8DHK KA9WGN KA9WGN KD3FU KD6EUI KD6HBW | |
413 | ;; KE9TV KF0NV N1API N3FU N3GZE N3IGS N4KCC N7IKQ N9HHU W4YHF W6ANK WA2SUH | |
414 | ;; WB7VZI N2NJZ NR3G KJ4KK AB4UM AL7NI KH6OH WN3KBT N4TMI W1A N0NZO | |
415 | (defconst mail-extr-ham-call-sign-pattern | |
416 | (purecopy "\\b\\(DX[0-9]+\\|[AKNW][A-Z]?[0-9][A-Z][A-Z]?[A-Z]?\\)")) | |
417 | ||
418 | ;; Possible trailing suffixes: "\\(/\\(KT\\|A[AEG]\\|[R0-9]\\)\\)?" | |
419 | ;; /KT == Temporary Technician (has CSC but not "real" license) | |
420 | ;; /AA == Temporary Advanced | |
421 | ;; /AE == Temporary Extra | |
422 | ;; /AG == Temporary General | |
423 | ;; /R == repeater | |
424 | ;; /# == stations operating out of home district | |
425 | ;; I don't include these in the regexp above because I can't imagine | |
426 | ;; anyone putting them with their name in an e-mail address. | |
72c0ae01 ER |
427 | |
428 | ;; Matches normal single-part name | |
154b3e39 RS |
429 | (defconst mail-extr-normal-name-pattern |
430 | (purecopy (format "\\b[%s][%s]+[%s]" | |
431 | mail-extr-first-letters | |
432 | mail-extr-all-letters-but-separators | |
433 | mail-extr-last-letters))) | |
434 | ||
435 | ;; Matches a single word name. | |
436 | ;; (defconst mail-extr-one-name-pattern | |
437 | ;; (purecopy (concat "\\`" mail-extr-normal-name-pattern "\\'"))) | |
6c83d99f | 438 | |
72c0ae01 | 439 | ;; Matches normal two names with missing middle initial |
154b3e39 RS |
440 | ;; The first name is not allowed to have a hyphen because this can cause |
441 | ;; false matches where the "middle initial" is actually the first letter | |
442 | ;; of the second part of the first name. | |
443 | (defconst mail-extr-two-name-pattern | |
444 | (purecopy | |
445 | (concat "\\`\\(" mail-extr-normal-name-pattern | |
446 | "\\|" mail-extr-initial-pattern | |
447 | "\\) +\\(" mail-extr-name-pattern "\\)\\(,\\|\\'\\)"))) | |
448 | ||
449 | (defconst mail-extr-listserv-list-name-pattern | |
450 | (purecopy "Multiple recipients of list \\([-A-Z]+\\)")) | |
451 | ||
452 | (defconst mail-extr-stupid-vms-date-stamp-pattern | |
453 | (purecopy | |
454 | "[0-9][0-9]-[JFMASOND][aepuco][nbrylgptvc]-[0-9][0-9][0-9][0-9] [0-9]+ *")) | |
455 | ||
456 | ;;; HZ -- GB (PRC Chinese character encoding) in ASCII embedding protocol | |
457 | ;; | |
458 | ;; In ASCII mode, a byte is interpreted as an ASCII character, unless a '~' is | |
459 | ;; encountered. The character '~' is an escape character. By convention, it | |
460 | ;; must be immediately followed ONLY by '~', '{' or '\n' (<LF>), with the | |
461 | ;; following special meaning. | |
6c83d99f | 462 | ;; |
154b3e39 RS |
463 | ;; o The escape sequence '~~' is interpreted as a '~'. |
464 | ;; o The escape-to-GB sequence '~{' switches the mode from ASCII to GB. | |
465 | ;; o The escape sequence '~\n' is a line-continuation marker to be consumed | |
466 | ;; with no output produced. | |
6c83d99f | 467 | ;; |
154b3e39 RS |
468 | ;; In GB mode, characters are interpreted two bytes at a time as (pure) GB |
469 | ;; codes until the escape-from-GB code '~}' is read. This code switches the | |
470 | ;; mode from GB back to ASCII. (Note that the escape-from-GB code '~}' | |
471 | ;; ($7E7D) is outside the defined GB range.) | |
472 | (defconst mail-extr-hz-embedded-gb-encoded-chinese-pattern | |
473 | (purecopy "~{\\([^~].\\|~[^\}]\\)+~}")) | |
474 | ||
475 | ;; The leading optional lowercase letters are for a bastardized version of | |
476 | ;; the encoding, as is the optional nature of the final slash. | |
477 | (defconst mail-extr-x400-encoded-address-pattern | |
478 | (purecopy "[a-z]?[a-z]?\\(/[A-Za-z]+\\(\\.[A-Za-z]+\\)?=[^/]+\\)+/?\\'")) | |
479 | ||
480 | (defconst mail-extr-x400-encoded-address-field-pattern-format | |
481 | (purecopy "/%s=\\([^/]+\\)\\(/\\|\\'\\)")) | |
482 | ||
483 | (defconst mail-extr-x400-encoded-address-surname-pattern | |
484 | ;; S stands for Surname (family name). | |
485 | (purecopy | |
486 | (format mail-extr-x400-encoded-address-field-pattern-format "[Ss]"))) | |
487 | ||
488 | (defconst mail-extr-x400-encoded-address-given-name-pattern | |
489 | ;; G stands for Given name. | |
490 | (purecopy | |
491 | (format mail-extr-x400-encoded-address-field-pattern-format "[Gg]"))) | |
492 | ||
493 | (defconst mail-extr-x400-encoded-address-full-name-pattern | |
494 | ;; PN stands for Personal Name. When used it represents the combination | |
495 | ;; of the G and S fields. | |
496 | ;; "The one system I used having this field asked it with the prompt | |
497 | ;; `Personal Name'. But they mapped it into G and S on outgoing real | |
498 | ;; X.400 addresses. As they mapped G and S into PN on incoming..." | |
499 | (purecopy | |
500 | (format mail-extr-x400-encoded-address-field-pattern-format "[Pp][Nn]"))) | |
501 | ||
502 | \f | |
503 | ||
504 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
505 | ;; | |
506 | ;; Syntax tables used for quick parsing. | |
507 | ;; | |
508 | ||
509 | (defconst mail-extr-address-syntax-table (make-syntax-table)) | |
510 | (defconst mail-extr-address-comment-syntax-table (make-syntax-table)) | |
511 | (defconst mail-extr-address-domain-literal-syntax-table (make-syntax-table)) | |
512 | (defconst mail-extr-address-text-comment-syntax-table (make-syntax-table)) | |
513 | (defconst mail-extr-address-text-syntax-table (make-syntax-table)) | |
d980c402 SM |
514 | (mapc |
515 | (lambda (pair) | |
516 | (let ((syntax-table (symbol-value (car pair)))) | |
517 | (dolist (item (cdr pair)) | |
518 | (if (eq 2 (length item)) | |
519 | ;; modifying syntax of a single character | |
520 | (modify-syntax-entry (car item) (car (cdr item)) syntax-table) | |
521 | ;; modifying syntax of a range of characters | |
522 | (let ((char (nth 0 item)) | |
523 | (bound (nth 1 item)) | |
524 | (syntax (nth 2 item))) | |
525 | (while (<= char bound) | |
526 | (modify-syntax-entry char syntax syntax-table) | |
527 | (setq char (1+ char)))))))) | |
154b3e39 RS |
528 | '((mail-extr-address-syntax-table |
529 | (?\000 ?\037 "w") ;control characters | |
530 | (?\040 " ") ;SPC | |
531 | (?! ?~ "w") ;printable characters | |
532 | (?\177 "w") ;DEL | |
533 | (?\200 ?\377 "w") ;high-bit-on characters | |
534 | (?\240 " ") ;nobreakspace | |
72c0ae01 ER |
535 | (?\t " ") |
536 | (?\r " ") | |
537 | (?\n " ") | |
538 | (?\( ".") | |
539 | (?\) ".") | |
540 | (?< ".") | |
541 | (?> ".") | |
542 | (?@ ".") | |
543 | (?, ".") | |
544 | (?\; ".") | |
545 | (?: ".") | |
546 | (?\\ "\\") | |
547 | (?\" "\"") | |
548 | (?. ".") | |
549 | (?\[ ".") | |
550 | (?\] ".") | |
551 | ;; % and ! aren't RFC822 characters, but it is convenient to pretend | |
552 | (?% ".") | |
154b3e39 | 553 | (?! ".") ;; this needs to be word-constituent when not in .UUCP mode |
72c0ae01 | 554 | ) |
154b3e39 RS |
555 | (mail-extr-address-comment-syntax-table |
556 | (?\000 ?\377 "w") | |
557 | (?\040 " ") | |
558 | (?\240 " ") | |
559 | (?\t " ") | |
560 | (?\r " ") | |
561 | (?\n " ") | |
72c0ae01 ER |
562 | (?\( "\(\)") |
563 | (?\) "\)\(") | |
564 | (?\\ "\\")) | |
154b3e39 RS |
565 | (mail-extr-address-domain-literal-syntax-table |
566 | (?\000 ?\377 "w") | |
567 | (?\040 " ") | |
568 | (?\240 " ") | |
569 | (?\t " ") | |
570 | (?\r " ") | |
571 | (?\n " ") | |
72c0ae01 ER |
572 | (?\[ "\(\]") ;?????? |
573 | (?\] "\)\[") ;?????? | |
574 | (?\\ "\\")) | |
154b3e39 RS |
575 | (mail-extr-address-text-comment-syntax-table |
576 | (?\000 ?\377 "w") | |
577 | (?\040 " ") | |
578 | (?\240 " ") | |
579 | (?\t " ") | |
580 | (?\r " ") | |
581 | (?\n " ") | |
72c0ae01 ER |
582 | (?\( "\(\)") |
583 | (?\) "\)\(") | |
584 | (?\[ "\(\]") | |
585 | (?\] "\)\[") | |
586 | (?\{ "\(\}") | |
587 | (?\} "\)\{") | |
588 | (?\\ "\\") | |
589 | (?\" "\"") | |
590 | ;; (?\' "\)\`") | |
591 | ;; (?\` "\(\'") | |
592 | ) | |
154b3e39 RS |
593 | (mail-extr-address-text-syntax-table |
594 | (?\000 ?\177 ".") | |
595 | (?\200 ?\377 "w") | |
596 | (?\040 " ") | |
597 | (?\t " ") | |
598 | (?\r " ") | |
599 | (?\n " ") | |
72c0ae01 ER |
600 | (?A ?Z "w") |
601 | (?a ?z "w") | |
602 | (?- "w") | |
603 | (?\} "w") | |
604 | (?\{ "w") | |
605 | (?| "w") | |
606 | (?\' "w") | |
607 | (?~ "w") | |
608 | (?0 ?9 "w")) | |
609 | )) | |
610 | ||
611 | \f | |
154b3e39 RS |
612 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
613 | ;; | |
72c0ae01 | 614 | ;; Utility functions and macros. |
154b3e39 RS |
615 | ;; |
616 | ||
7a9ebd0b | 617 | (defsubst mail-extr-skip-whitespace-forward () |
154b3e39 | 618 | ;; v19 fn skip-syntax-forward is more tasteful, but not byte-coded. |
b1ccc1ce | 619 | (skip-chars-forward " \t\n\r ")) |
154b3e39 | 620 | |
7a9ebd0b | 621 | (defsubst mail-extr-skip-whitespace-backward () |
154b3e39 | 622 | ;; v19 fn skip-syntax-backward is more tasteful, but not byte-coded. |
b1ccc1ce | 623 | (skip-chars-backward " \t\n\r ")) |
7a9ebd0b | 624 | |
72c0ae01 | 625 | |
7a9ebd0b SM |
626 | (defsubst mail-extr-undo-backslash-quoting (beg end) |
627 | (save-excursion | |
628 | (save-restriction | |
629 | (narrow-to-region beg end) | |
630 | (goto-char (point-min)) | |
631 | ;; undo \ quoting | |
632 | (while (search-forward "\\" nil t) | |
d980c402 | 633 | (delete-char -1) |
7a9ebd0b SM |
634 | (or (eobp) |
635 | (forward-char 1)))))) | |
636 | ||
637 | (defsubst mail-extr-nuke-char-at (pos) | |
638 | (save-excursion | |
639 | (goto-char pos) | |
d980c402 | 640 | (delete-char 1) |
7a9ebd0b | 641 | (insert ?\ ))) |
154b3e39 RS |
642 | |
643 | (put 'mail-extr-nuke-outside-range | |
644 | 'edebug-form-spec '(symbolp &optional form form atom)) | |
645 | ||
646 | (defmacro mail-extr-nuke-outside-range (list-symbol | |
647 | beg-symbol end-symbol | |
648 | &optional no-replace) | |
d980c402 SM |
649 | "Delete all elements outside BEG..END in LIST. |
650 | LIST-SYMBOL names a variable holding a list of buffer positions | |
651 | BEG-SYMBOL and END-SYMBOL name variables delimiting a range | |
652 | Each element of LIST-SYMBOL which lies outside of the range is | |
653 | deleted from the list. | |
654 | Unless NO-REPLACE is true, at each of the positions in LIST-SYMBOL | |
655 | which lie outside of the range, one character at that position is | |
656 | replaced with a SPC." | |
154b3e39 | 657 | (or (memq no-replace '(t nil)) |
29565a87 | 658 | (error "no-replace must be t or nil, evaluable at macroexpand-time")) |
d980c402 | 659 | `(let ((temp ,list-symbol) |
154b3e39 | 660 | ch) |
72c0ae01 | 661 | (while temp |
154b3e39 | 662 | (setq ch (car temp)) |
d980c402 SM |
663 | (when (or (> ch ,end-symbol) |
664 | (< ch ,beg-symbol)) | |
665 | ,@(if no-replace | |
666 | nil | |
667 | `((mail-extr-nuke-char-at ch))) | |
668 | (setcar temp nil)) | |
72c0ae01 | 669 | (setq temp (cdr temp))) |
d980c402 | 670 | (setq ,list-symbol (delq nil ,list-symbol)))) |
72c0ae01 | 671 | |
154b3e39 RS |
672 | (defun mail-extr-demarkerize (marker) |
673 | ;; if arg is a marker, destroys the marker, then returns the old value. | |
674 | ;; otherwise returns the arg. | |
675 | (if (markerp marker) | |
676 | (let ((temp (marker-position marker))) | |
677 | (set-marker marker nil) | |
678 | temp) | |
679 | marker)) | |
680 | ||
681 | (defun mail-extr-markerize (pos) | |
682 | ;; coerces pos to a marker if non-nil. | |
683 | (if (or (markerp pos) (null pos)) | |
684 | pos | |
685 | (copy-marker pos))) | |
686 | ||
7a9ebd0b | 687 | (defsubst mail-extr-safe-move-sexp (arg) |
154b3e39 | 688 | ;; Safely skip over one balanced sexp, if there is one. Return t if success. |
7a9ebd0b SM |
689 | (condition-case error |
690 | (progn | |
691 | (goto-char (or (scan-sexps (point) arg) (point))) | |
692 | t) | |
693 | (error | |
694 | ;; #### kludge kludge kludge kludge kludge kludge kludge !!! | |
695 | (if (string-equal (nth 1 error) "Unbalanced parentheses") | |
696 | nil | |
697 | (while t | |
698 | (signal (car error) (cdr error))))))) | |
72c0ae01 | 699 | \f |
154b3e39 RS |
700 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
701 | ;; | |
72c0ae01 | 702 | ;; The main function to grind addresses |
154b3e39 RS |
703 | ;; |
704 | ||
705 | (defvar disable-initial-guessing-flag) ; dynamic assignment | |
706 | (defvar cbeg) ; dynamic assignment | |
707 | (defvar cend) ; dynamic assignment | |
72c0ae01 | 708 | |
154b3e39 | 709 | ;;;###autoload |
d1782bd8 KH |
710 | (defun mail-extract-address-components (address &optional all) |
711 | "Given an RFC-822 address ADDRESS, extract full name and canonical address. | |
154b3e39 | 712 | Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). |
2a487c6c SJ |
713 | If no name can be extracted, FULL-NAME will be nil. Also see |
714 | `mail-extr-ignore-single-names'. | |
d1782bd8 KH |
715 | |
716 | If the optional argument ALL is non-nil, then ADDRESS can contain zero | |
717 | or more recipients, separated by commas, and we return a list of | |
718 | the form ((FULL-NAME CANONICAL-ADDRESS) ...) with one element for | |
719 | each recipient. If ALL is nil, then if ADDRESS contains more than | |
720 | one recipients, all but the first is ignored. | |
721 | ||
7a9ebd0b | 722 | ADDRESS may be a string or a buffer. If it is a buffer, the visible |
2a487c6c SJ |
723 | \(narrowed) portion of the buffer will be interpreted as the address. |
724 | \(This feature exists so that the clever caller might be able to avoid | |
725 | consing a string.)" | |
154b3e39 RS |
726 | (let ((canonicalization-buffer (get-buffer-create " *canonical address*")) |
727 | (extraction-buffer (get-buffer-create " *extract address components*")) | |
d1782bd8 KH |
728 | value-list) |
729 | ||
7a9ebd0b | 730 | (with-current-buffer (get-buffer-create extraction-buffer) |
154b3e39 | 731 | (fundamental-mode) |
e8a57935 | 732 | (buffer-disable-undo extraction-buffer) |
154b3e39 | 733 | (set-syntax-table mail-extr-address-syntax-table) |
72c0ae01 ER |
734 | (widen) |
735 | (erase-buffer) | |
736 | (setq case-fold-search nil) | |
6c83d99f | 737 | |
72c0ae01 ER |
738 | ;; Insert extra space at beginning to allow later replacement with < |
739 | ;; without having to move markers. | |
154b3e39 RS |
740 | (insert ?\ ) |
741 | ||
742 | ;; Insert the address itself. | |
743 | (cond ((stringp address) | |
744 | (insert address)) | |
745 | ((bufferp address) | |
746 | (insert-buffer-substring address)) | |
747 | (t | |
29565a87 | 748 | (error "Invalid address: %s" address))) |
03007ccc RS |
749 | |
750 | (set-text-properties (point-min) (point-max) nil) | |
d1782bd8 | 751 | |
7a9ebd0b | 752 | (with-current-buffer (get-buffer-create canonicalization-buffer) |
d1782bd8 KH |
753 | (fundamental-mode) |
754 | (buffer-disable-undo canonicalization-buffer) | |
d1782bd8 KH |
755 | (setq case-fold-search nil)) |
756 | ||
6c83d99f | 757 | |
72c0ae01 ER |
758 | ;; Unfold multiple lines. |
759 | (goto-char (point-min)) | |
760 | (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t) | |
761 | (replace-match "\\1 " t)) | |
6c83d99f | 762 | |
d1782bd8 KH |
763 | ;; Loop over addresses until we have as many as we want. |
764 | (while (and (or all (null value-list)) | |
765 | (progn (goto-char (point-min)) | |
766 | (skip-chars-forward " \t") | |
767 | (not (eobp)))) | |
768 | (let (char | |
769 | end-of-address | |
f98b752f | 770 | <-pos >-pos @-pos colon-pos comma-pos !-pos %-pos \;-pos |
d1782bd8 KH |
771 | group-:-pos group-\;-pos route-addr-:-pos |
772 | record-pos-symbol | |
773 | first-real-pos last-real-pos | |
774 | phrase-beg phrase-end | |
775 | cbeg cend ; dynamically set from -voodoo | |
776 | quote-beg quote-end | |
777 | atom-beg atom-end | |
778 | mbox-beg mbox-end | |
779 | \.-ends-name | |
780 | temp | |
781 | ;; name-suffix | |
782 | fi mi li ; first, middle, last initial | |
783 | saved-%-pos saved-!-pos saved-@-pos | |
784 | domain-pos \.-pos insert-point | |
785 | ;; mailbox-name-processed-flag | |
786 | disable-initial-guessing-flag) ; dynamically set from -voodoo | |
787 | ||
7a9ebd0b | 788 | (set-syntax-table mail-extr-address-syntax-table) |
d1782bd8 KH |
789 | (goto-char (point-min)) |
790 | ||
791 | ;; Insert extra space at beginning to allow later replacement with < | |
792 | ;; without having to move markers. | |
793 | (or (eq (following-char) ?\ ) | |
794 | (insert ?\ )) | |
795 | ||
796 | ;; First pass grabs useful information about address. | |
797 | (while (progn | |
798 | (mail-extr-skip-whitespace-forward) | |
799 | (not (eobp))) | |
800 | (setq char (char-after (point))) | |
801 | (or first-real-pos | |
802 | (if (not (eq char ?\()) | |
803 | (setq first-real-pos (point)))) | |
804 | (cond | |
805 | ;; comment | |
806 | ((eq char ?\() | |
807 | (set-syntax-table mail-extr-address-comment-syntax-table) | |
808 | ;; only record the first non-empty comment's position | |
809 | (if (and (not cbeg) | |
810 | (save-excursion | |
811 | (forward-char 1) | |
812 | (mail-extr-skip-whitespace-forward) | |
813 | (not (eq ?\) (char-after (point)))))) | |
814 | (setq cbeg (point))) | |
815 | ;; TODO: don't record if unbalanced | |
816 | (or (mail-extr-safe-move-sexp 1) | |
817 | (forward-char 1)) | |
818 | (set-syntax-table mail-extr-address-syntax-table) | |
819 | (if (and cbeg | |
820 | (not cend)) | |
821 | (setq cend (point)))) | |
822 | ;; quoted text | |
823 | ((eq char ?\") | |
824 | ;; only record the first non-empty quote's position | |
825 | (if (and (not quote-beg) | |
826 | (save-excursion | |
827 | (forward-char 1) | |
828 | (mail-extr-skip-whitespace-forward) | |
829 | (not (eq ?\" (char-after (point)))))) | |
830 | (setq quote-beg (point))) | |
831 | ;; TODO: don't record if unbalanced | |
832 | (or (mail-extr-safe-move-sexp 1) | |
833 | (forward-char 1)) | |
834 | (if (and quote-beg | |
835 | (not quote-end)) | |
836 | (setq quote-end (point)))) | |
837 | ;; domain literals | |
838 | ((eq char ?\[) | |
839 | (set-syntax-table mail-extr-address-domain-literal-syntax-table) | |
840 | (or (mail-extr-safe-move-sexp 1) | |
841 | (forward-char 1)) | |
842 | (set-syntax-table mail-extr-address-syntax-table)) | |
843 | ;; commas delimit addresses when outside < > pairs. | |
844 | ((and (eq char ?,) | |
845 | (or (and (null <-pos) | |
846 | ;; Handle ROUTE-ADDR address that is missing its <. | |
847 | (not (eq ?@ (char-after (1+ (point)))))) | |
848 | (and >-pos | |
849 | ;; handle weird munged addresses | |
850 | ;; BUG FIX: This test was reversed. Thanks to the | |
851 | ;; brilliant Rod Whitby <rwhitby@research.canon.oz.au> | |
852 | ;; for discovering this! | |
7a9ebd0b | 853 | (< (car (last <-pos)) (car >-pos))))) |
d1782bd8 KH |
854 | ;; The argument contains more than one address. |
855 | ;; Temporarily hide everything after this one. | |
7a9ebd0b | 856 | (setq end-of-address (copy-marker (1+ (point)) t)) |
d1782bd8 | 857 | (narrow-to-region (point-min) (1+ (point))) |
7a9ebd0b | 858 | (delete-char 1) |
d1782bd8 KH |
859 | (setq char ?\() ; HAVE I NO SHAME?? |
860 | ) | |
861 | ;; record the position of various interesting chars, determine | |
862 | ;; legality later. | |
863 | ((setq record-pos-symbol | |
864 | (cdr (assq char | |
865 | '((?< . <-pos) (?> . >-pos) (?@ . @-pos) | |
f98b752f | 866 | (?: . colon-pos) (?, . comma-pos) (?! . !-pos) |
d1782bd8 KH |
867 | (?% . %-pos) (?\; . \;-pos))))) |
868 | (set record-pos-symbol | |
869 | (cons (point) (symbol-value record-pos-symbol))) | |
72c0ae01 | 870 | (forward-char 1)) |
d1782bd8 | 871 | ((eq char ?.) |
72c0ae01 | 872 | (forward-char 1)) |
d1782bd8 KH |
873 | ((memq char '( |
874 | ;; comment terminator illegal | |
875 | ?\) | |
876 | ;; domain literal terminator illegal | |
877 | ?\] | |
878 | ;; \ allowed only within quoted strings, | |
879 | ;; domain literals, and comments | |
880 | ?\\ | |
881 | )) | |
882 | (mail-extr-nuke-char-at (point)) | |
72c0ae01 | 883 | (forward-char 1)) |
d1782bd8 KH |
884 | (t |
885 | (forward-word 1))) | |
886 | (or (eq char ?\() | |
887 | ;; At the end of first address of a multiple address header. | |
888 | (and (eq char ?,) | |
889 | (eobp)) | |
890 | (setq last-real-pos (point)))) | |
891 | ||
892 | ;; Use only the leftmost <, if any. Replace all others with spaces. | |
893 | (while (cdr <-pos) | |
894 | (mail-extr-nuke-char-at (car <-pos)) | |
895 | (setq <-pos (cdr <-pos))) | |
896 | ||
897 | ;; Use only the rightmost >, if any. Replace all others with spaces. | |
898 | (while (cdr >-pos) | |
899 | (mail-extr-nuke-char-at (nth 1 >-pos)) | |
900 | (setcdr >-pos (nthcdr 2 >-pos))) | |
901 | ||
902 | ;; If multiple @s and a :, but no < and >, insert around buffer. | |
903 | ;; Example: @foo.bar.dom,@xxx.yyy.zzz:mailbox@aaa.bbb.ccc | |
904 | ;; This commonly happens on the UUCP "From " line. Ugh. | |
d980c402 | 905 | (when (and (> (length @-pos) 1) |
f98b752f | 906 | (eq 1 (length colon-pos)) ;TODO: check if between last two @s |
d1782bd8 KH |
907 | (not \;-pos) |
908 | (not <-pos)) | |
d980c402 SM |
909 | (goto-char (point-min)) |
910 | (delete-char 1) | |
911 | (setq <-pos (list (point))) | |
912 | (insert ?<)) | |
d1782bd8 KH |
913 | |
914 | ;; If < but no >, insert > in rightmost possible position | |
d980c402 SM |
915 | (when (and <-pos (null >-pos)) |
916 | (goto-char (point-max)) | |
917 | (setq >-pos (list (point))) | |
918 | (insert ?>)) | |
d1782bd8 KH |
919 | |
920 | ;; If > but no <, replace > with space. | |
d980c402 SM |
921 | (when (and >-pos (null <-pos)) |
922 | (mail-extr-nuke-char-at (car >-pos)) | |
923 | (setq >-pos nil)) | |
d1782bd8 KH |
924 | |
925 | ;; Turn >-pos and <-pos into non-lists | |
926 | (setq >-pos (car >-pos) | |
927 | <-pos (car <-pos)) | |
928 | ||
929 | ;; Trim other punctuation lists of items outside < > pair to handle | |
930 | ;; stupid MTAs. | |
d980c402 SM |
931 | (when <-pos ; don't need to check >-pos also |
932 | ;; handle bozo software that violates RFC 822 by sticking | |
933 | ;; punctuation marks outside of a < > pair | |
934 | (mail-extr-nuke-outside-range @-pos <-pos >-pos t) | |
935 | ;; RFC 822 says nothing about these two outside < >, but | |
936 | ;; remove those positions from the lists to make things | |
937 | ;; easier. | |
938 | (mail-extr-nuke-outside-range !-pos <-pos >-pos t) | |
939 | (mail-extr-nuke-outside-range %-pos <-pos >-pos t)) | |
d1782bd8 KH |
940 | |
941 | ;; Check for : that indicates GROUP list and for : part of | |
942 | ;; ROUTE-ADDR spec. | |
943 | ;; Can't possibly be more than two :. Nuke any extra. | |
f98b752f KH |
944 | (while colon-pos |
945 | (setq temp (car colon-pos) | |
946 | colon-pos (cdr colon-pos)) | |
d1782bd8 KH |
947 | (cond ((and <-pos >-pos |
948 | (> temp <-pos) | |
949 | (< temp >-pos)) | |
950 | (if (or route-addr-:-pos | |
951 | (< (length @-pos) 2) | |
952 | (> temp (car @-pos)) | |
953 | (< temp (nth 1 @-pos))) | |
954 | (mail-extr-nuke-char-at temp) | |
955 | (setq route-addr-:-pos temp))) | |
956 | ((or (not <-pos) | |
957 | (and <-pos | |
958 | (< temp <-pos))) | |
959 | (setq group-:-pos temp)))) | |
960 | ||
961 | ;; Nuke any ; that is in or to the left of a < > pair or to the left | |
962 | ;; of a GROUP starting :. Also, there may only be one ;. | |
963 | (while \;-pos | |
964 | (setq temp (car \;-pos) | |
965 | \;-pos (cdr \;-pos)) | |
966 | (cond ((and <-pos >-pos | |
967 | (> temp <-pos) | |
968 | (< temp >-pos)) | |
969 | (mail-extr-nuke-char-at temp)) | |
970 | ((and (or (not group-:-pos) | |
971 | (> temp group-:-pos)) | |
972 | (not group-\;-pos)) | |
973 | (setq group-\;-pos temp)))) | |
974 | ||
975 | ;; Nuke unmatched GROUP syntax characters. | |
d980c402 SM |
976 | (when (and group-:-pos (not group-\;-pos)) |
977 | ;; *** Do I really need to erase it? | |
978 | (mail-extr-nuke-char-at group-:-pos) | |
979 | (setq group-:-pos nil)) | |
980 | (when (and group-\;-pos (not group-:-pos)) | |
981 | ;; *** Do I really need to erase it? | |
982 | (mail-extr-nuke-char-at group-\;-pos) | |
983 | (setq group-\;-pos nil)) | |
d1782bd8 KH |
984 | |
985 | ;; Handle junk like ";@host.company.dom" that sendmail adds. | |
986 | ;; **** should I remember comment positions? | |
d980c402 | 987 | (when group-\;-pos |
d1782bd8 KH |
988 | ;; this is fine for now |
989 | (mail-extr-nuke-outside-range !-pos group-:-pos group-\;-pos t) | |
990 | (mail-extr-nuke-outside-range @-pos group-:-pos group-\;-pos t) | |
991 | (mail-extr-nuke-outside-range %-pos group-:-pos group-\;-pos t) | |
992 | (mail-extr-nuke-outside-range comma-pos group-:-pos group-\;-pos t) | |
993 | (and last-real-pos | |
994 | (> last-real-pos (1+ group-\;-pos)) | |
995 | (setq last-real-pos (1+ group-\;-pos))) | |
996 | ;; *** This may be wrong: | |
997 | (and cend | |
998 | (> cend group-\;-pos) | |
999 | (setq cend nil | |
1000 | cbeg nil)) | |
1001 | (and quote-end | |
1002 | (> quote-end group-\;-pos) | |
1003 | (setq quote-end nil | |
1004 | quote-beg nil)) | |
1005 | ;; This was both wrong and unnecessary: | |
1006 | ;;(narrow-to-region (point-min) group-\;-pos) | |
1007 | ||
1008 | ;; *** The entire handling of GROUP addresses seems rather lame. | |
1009 | ;; *** It deserves a complete rethink, except that these addresses | |
1010 | ;; *** are hardly ever seen. | |
d980c402 | 1011 | ) |
d1782bd8 KH |
1012 | |
1013 | ;; Any commas must be between < and : of ROUTE-ADDR. Nuke any | |
1014 | ;; others. | |
6c83d99f | 1015 | ;; Hell, go ahead and nuke all of the commas. |
d1782bd8 KH |
1016 | ;; **** This will cause problems when we start handling commas in |
1017 | ;; the PHRASE part .... no it won't ... yes it will ... ????? | |
1018 | (mail-extr-nuke-outside-range comma-pos 1 1) | |
1019 | ||
1020 | ;; can only have multiple @s inside < >. The fact that some MTAs | |
1021 | ;; put de-bracketed ROUTE-ADDRs in the UUCP-style "From " line is | |
1022 | ;; handled above. | |
1023 | ||
1024 | ;; Locate PHRASE part of ROUTE-ADDR. | |
d980c402 SM |
1025 | (when <-pos |
1026 | (goto-char <-pos) | |
1027 | (mail-extr-skip-whitespace-backward) | |
1028 | (setq phrase-end (point)) | |
1029 | (goto-char (or ;;group-:-pos | |
1030 | (point-min))) | |
1031 | (mail-extr-skip-whitespace-forward) | |
1032 | (if (< (point) phrase-end) | |
1033 | (setq phrase-beg (point)) | |
1034 | (setq phrase-end nil))) | |
d1782bd8 KH |
1035 | |
1036 | ;; handle ROUTE-ADDRS with real ROUTEs. | |
1037 | ;; If there are multiple @s, then we assume ROUTE-ADDR syntax, and | |
1038 | ;; any % or ! must be semantically meaningless. | |
1039 | ;; TODO: do this processing into canonicalization buffer | |
d980c402 SM |
1040 | (when route-addr-:-pos |
1041 | (setq !-pos nil | |
1042 | %-pos nil | |
1043 | >-pos (copy-marker >-pos) | |
1044 | route-addr-:-pos (copy-marker route-addr-:-pos)) | |
1045 | (goto-char >-pos) | |
1046 | (insert-before-markers ?X) | |
1047 | (goto-char (car @-pos)) | |
1048 | (while (setq @-pos (cdr @-pos)) | |
1049 | (delete-char 1) | |
1050 | (setq %-pos (cons (point-marker) %-pos)) | |
1051 | (insert "%") | |
1052 | (goto-char (1- >-pos)) | |
1053 | (save-excursion | |
1054 | (insert-buffer-substring extraction-buffer | |
1055 | (car @-pos) route-addr-:-pos) | |
1056 | (delete-region (car @-pos) route-addr-:-pos)) | |
1057 | (or (cdr @-pos) | |
1058 | (setq saved-@-pos (list (point))))) | |
1059 | (setq @-pos saved-@-pos) | |
1060 | (goto-char >-pos) | |
1061 | (delete-char -1) | |
1062 | (mail-extr-nuke-char-at route-addr-:-pos) | |
1063 | (mail-extr-demarkerize route-addr-:-pos) | |
1064 | (setq route-addr-:-pos nil | |
1065 | >-pos (mail-extr-demarkerize >-pos) | |
1066 | %-pos (mapcar 'mail-extr-demarkerize %-pos))) | |
d1782bd8 KH |
1067 | |
1068 | ;; de-listify @-pos | |
1069 | (setq @-pos (car @-pos)) | |
1070 | ||
1071 | ;; TODO: remove comments in the middle of an address | |
1072 | ||
d980c402 | 1073 | (with-current-buffer canonicalization-buffer |
d1782bd8 KH |
1074 | (widen) |
1075 | (erase-buffer) | |
1076 | (insert-buffer-substring extraction-buffer) | |
1077 | ||
1078 | (if <-pos | |
1079 | (narrow-to-region (progn | |
1080 | (goto-char (1+ <-pos)) | |
1081 | (mail-extr-skip-whitespace-forward) | |
1082 | (point)) | |
1083 | >-pos) | |
1084 | (if (and first-real-pos last-real-pos) | |
1085 | (narrow-to-region first-real-pos last-real-pos) | |
1086 | ;; ****** Oh no! What if the address is completely empty! | |
1087 | ;; *** Is this correct? | |
d980c402 | 1088 | (narrow-to-region (point-max) (point-max)))) |
d1782bd8 KH |
1089 | |
1090 | (and @-pos %-pos | |
1091 | (mail-extr-nuke-outside-range %-pos (point-min) @-pos)) | |
1092 | (and %-pos !-pos | |
1093 | (mail-extr-nuke-outside-range !-pos (point-min) (car %-pos))) | |
1094 | (and @-pos !-pos (not %-pos) | |
1095 | (mail-extr-nuke-outside-range !-pos (point-min) @-pos)) | |
1096 | ||
1097 | ;; Error condition:?? (and %-pos (not @-pos)) | |
1098 | ||
1099 | ;; WARNING: THIS CODE IS DUPLICATED BELOW. | |
d980c402 SM |
1100 | (when (and %-pos (not @-pos)) |
1101 | (goto-char (car %-pos)) | |
1102 | (delete-char 1) | |
1103 | (setq @-pos (point)) | |
1104 | (insert "@") | |
1105 | (setq %-pos (cdr %-pos))) | |
1106 | ||
1107 | (when (and mail-extr-mangle-uucp !-pos) | |
1108 | ;; **** I don't understand this save-restriction and the | |
1109 | ;; narrow-to-region inside it. Why did I do that? | |
1110 | (save-restriction | |
1111 | (cond ((and @-pos | |
1112 | mail-extr-@-binds-tighter-than-!) | |
1113 | (goto-char @-pos) | |
1114 | (setq %-pos (cons (point) %-pos) | |
1115 | @-pos nil) | |
1116 | (delete-char 1) | |
1117 | (insert "%") | |
1118 | (setq insert-point (point-max))) | |
1119 | (mail-extr-@-binds-tighter-than-! | |
1120 | (setq insert-point (point-max))) | |
1121 | (%-pos | |
1122 | (setq insert-point (car (last %-pos)) | |
1123 | saved-%-pos (mapcar 'mail-extr-markerize %-pos) | |
1124 | %-pos nil | |
1125 | @-pos (mail-extr-markerize @-pos))) | |
1126 | (@-pos | |
1127 | (setq insert-point @-pos) | |
1128 | (setq @-pos (mail-extr-markerize @-pos))) | |
1129 | (t | |
1130 | (setq insert-point (point-max)))) | |
1131 | (narrow-to-region (point-min) insert-point) | |
1132 | (setq saved-!-pos (car !-pos)) | |
1133 | (while !-pos | |
1134 | (goto-char (point-max)) | |
1135 | (cond ((and (not @-pos) | |
1136 | (not (cdr !-pos))) | |
1137 | (setq @-pos (point)) | |
1138 | (insert-before-markers "@ ")) | |
1139 | (t | |
1140 | (setq %-pos (cons (point) %-pos)) | |
1141 | (insert-before-markers "% "))) | |
1142 | (backward-char 1) | |
1143 | (insert-buffer-substring | |
1144 | (current-buffer) | |
1145 | (if (nth 1 !-pos) | |
1146 | (1+ (nth 1 !-pos)) | |
1147 | (point-min)) | |
1148 | (car !-pos)) | |
1149 | (delete-char 1) | |
1150 | (or (save-excursion | |
1151 | (mail-extr-safe-move-sexp -1) | |
1152 | (mail-extr-skip-whitespace-backward) | |
1153 | (eq ?. (preceding-char))) | |
1154 | (insert-before-markers | |
1155 | (if (save-excursion | |
1156 | (mail-extr-skip-whitespace-backward) | |
1157 | (eq ?. (preceding-char))) | |
1158 | "" | |
1159 | ".") | |
1160 | "uucp")) | |
1161 | (setq !-pos (cdr !-pos)))) | |
1162 | (and saved-%-pos | |
1163 | (setq %-pos (append (mapcar 'mail-extr-demarkerize | |
1164 | saved-%-pos) | |
1165 | %-pos))) | |
1166 | (setq @-pos (mail-extr-demarkerize @-pos)) | |
1167 | (narrow-to-region (1+ saved-!-pos) (point-max))) | |
d1782bd8 KH |
1168 | |
1169 | ;; WARNING: THIS CODE IS DUPLICATED ABOVE. | |
d980c402 SM |
1170 | (when (and %-pos (not @-pos)) |
1171 | (goto-char (car %-pos)) | |
1172 | (delete-char 1) | |
1173 | (setq @-pos (point)) | |
1174 | (insert "@") | |
1175 | (setq %-pos (cdr %-pos))) | |
1176 | ||
1177 | (when (setq %-pos (nreverse %-pos)) ; implies @-pos valid | |
1178 | (setq temp %-pos) | |
1179 | (catch 'truncated | |
1180 | (while temp | |
1181 | (goto-char (or (nth 1 temp) | |
1182 | @-pos)) | |
1183 | (mail-extr-skip-whitespace-backward) | |
1184 | (save-excursion | |
1185 | (mail-extr-safe-move-sexp -1) | |
1186 | (setq domain-pos (point)) | |
1187 | (mail-extr-skip-whitespace-backward) | |
1188 | (setq \.-pos (eq ?. (preceding-char)))) | |
1189 | (when (and \.-pos | |
1190 | ;; #### string consing | |
1191 | (let ((s (intern-soft | |
1192 | (buffer-substring domain-pos (point)) | |
1193 | mail-extr-all-top-level-domains))) | |
1194 | (and s (get s 'domain-name)))) | |
1195 | (narrow-to-region (point-min) (point)) | |
1196 | (goto-char (car temp)) | |
1197 | (delete-char 1) | |
1198 | (setq @-pos (point)) | |
1199 | (setcdr temp nil) | |
1200 | (setq %-pos (delq @-pos %-pos)) | |
1201 | (insert "@") | |
1202 | (throw 'truncated t)) | |
1203 | (setq temp (cdr temp))))) | |
d1782bd8 KH |
1204 | (setq mbox-beg (point-min) |
1205 | mbox-end (if %-pos (car %-pos) | |
1206 | (or @-pos | |
d980c402 SM |
1207 | (point-max)))) |
1208 | ||
1209 | (when @-pos | |
1210 | ;; Make the domain-name part lowercase since it's case | |
1211 | ;; insensitive anyway. | |
1212 | (downcase-region (1+ @-pos) (point-max)))) | |
d1782bd8 KH |
1213 | |
1214 | ;; Done canonicalizing address. | |
1215 | ;; We are now back in extraction-buffer. | |
1216 | ||
1217 | ;; Decide what part of the address to search to find the full name. | |
1218 | (cond ( | |
1219 | ;; Example: "First M. Last" <fml@foo.bar.dom> | |
1220 | (and phrase-beg | |
1221 | (eq quote-beg phrase-beg) | |
1222 | (<= quote-end phrase-end)) | |
1223 | (narrow-to-region (1+ quote-beg) (1- quote-end)) | |
1224 | (mail-extr-undo-backslash-quoting (point-min) (point-max))) | |
1225 | ||
1226 | ;; Example: First Last <fml@foo.bar.dom> | |
1227 | (phrase-beg | |
1228 | (narrow-to-region phrase-beg phrase-end)) | |
1229 | ||
1230 | ;; Example: fml@foo.bar.dom (First M. Last) | |
1231 | (cbeg | |
1232 | (narrow-to-region (1+ cbeg) (1- cend)) | |
1233 | (mail-extr-undo-backslash-quoting (point-min) (point-max)) | |
1234 | ||
1235 | ;; Deal with spacing problems | |
1236 | (goto-char (point-min)) | |
1237 | ;;; (cond ((not (search-forward " " nil t)) | |
1238 | ;;; (goto-char (point-min)) | |
1239 | ;;; (cond ((search-forward "_" nil t) | |
1240 | ;;; ;; Handle the *idiotic* use of underlines as spaces. | |
1241 | ;;; ;; Example: fml@foo.bar.dom (First_M._Last) | |
1242 | ;;; (goto-char (point-min)) | |
1243 | ;;; (while (search-forward "_" nil t) | |
1244 | ;;; (replace-match " " t))) | |
1245 | ;;; ((search-forward "." nil t) | |
1246 | ;;; ;; Fix . used as space | |
1247 | ;;; ;; Example: danj1@cb.att.com (daniel.jacobson) | |
1248 | ;;; (goto-char (point-min)) | |
1249 | ;;; (while (re-search-forward mail-extr-bad-dot-pattern nil t) | |
1250 | ;;; (replace-match "\\1 \\2" t)))))) | |
154b3e39 | 1251 | ) |
d1782bd8 KH |
1252 | |
1253 | ;; Otherwise we try to get the name from the mailbox portion | |
1254 | ;; of the address. | |
1255 | ;; Example: First_M_Last@foo.bar.dom | |
72c0ae01 | 1256 | (t |
d1782bd8 KH |
1257 | ;; *** Work in canon buffer instead? No, can't. Hmm. |
1258 | (goto-char (point-max)) | |
1259 | (narrow-to-region (point) (point)) | |
1260 | (insert-buffer-substring canonicalization-buffer | |
1261 | mbox-beg mbox-end) | |
1262 | (goto-char (point-min)) | |
1263 | ||
1264 | ;; Example: First_Last.XXX@foo.bar.dom | |
1265 | (setq \.-ends-name (re-search-forward "[_0-9]" nil t)) | |
1266 | ||
1267 | (goto-char (point-min)) | |
1268 | ||
1269 | (if (not mail-extr-mangle-uucp) | |
1270 | (modify-syntax-entry ?! "w" (syntax-table))) | |
1271 | ||
1272 | (while (progn | |
1273 | (mail-extr-skip-whitespace-forward) | |
1274 | (not (eobp))) | |
1275 | (setq char (char-after (point))) | |
154b3e39 | 1276 | (cond |
d1782bd8 KH |
1277 | ((eq char ?\") |
1278 | (setq quote-beg (point)) | |
1279 | (or (mail-extr-safe-move-sexp 1) | |
1280 | ;; TODO: handle this error condition!!!!! | |
1281 | (forward-char 1)) | |
1282 | ;; take into account deletions | |
1283 | (setq quote-end (- (point) 2)) | |
1284 | (save-excursion | |
1285 | (backward-char 1) | |
d980c402 | 1286 | (delete-char 1) |
d1782bd8 KH |
1287 | (goto-char quote-beg) |
1288 | (or (eobp) | |
d980c402 | 1289 | (delete-char 1))) |
d1782bd8 KH |
1290 | (mail-extr-undo-backslash-quoting quote-beg quote-end) |
1291 | (or (eq ?\ (char-after (point))) | |
1292 | (insert " ")) | |
1293 | ;; (setq mailbox-name-processed-flag t) | |
1294 | (setq \.-ends-name t)) | |
1295 | ((eq char ?.) | |
1296 | (if (memq (char-after (1+ (point))) '(?_ ?=)) | |
1297 | (progn | |
1298 | (forward-char 1) | |
d980c402 | 1299 | (delete-char 1) |
d1782bd8 KH |
1300 | (insert ?\ )) |
1301 | (if \.-ends-name | |
1302 | (narrow-to-region (point-min) (point)) | |
d980c402 | 1303 | (delete-char 1) |
d1782bd8 KH |
1304 | (insert " "))) |
1305 | ;; (setq mailbox-name-processed-flag t) | |
1306 | ) | |
1307 | ((memq (char-syntax char) '(?. ?\\)) | |
d980c402 | 1308 | (delete-char 1) |
d1782bd8 KH |
1309 | (insert " ") |
1310 | ;; (setq mailbox-name-processed-flag t) | |
154b3e39 | 1311 | ) |
154b3e39 | 1312 | (t |
d1782bd8 KH |
1313 | (setq atom-beg (point)) |
1314 | (forward-word 1) | |
1315 | (setq atom-end (point)) | |
1316 | (goto-char atom-beg) | |
1317 | (save-restriction | |
1318 | (narrow-to-region atom-beg atom-end) | |
1319 | (cond | |
1320 | ||
1321 | ;; Handle X.400 addresses encoded in RFC-822. | |
1322 | ;; *** Shit! This has to handle the case where it is | |
1323 | ;; *** embedded in a quote too! | |
1324 | ;; *** Shit! The input is being broken up into atoms | |
1325 | ;; *** by periods! | |
1326 | ((looking-at mail-extr-x400-encoded-address-pattern) | |
1327 | ||
1328 | ;; Copy the contents of the individual fields that | |
1329 | ;; might hold name data to the beginning. | |
d980c402 SM |
1330 | (mapc |
1331 | (lambda (field-pattern) | |
1332 | (when | |
1333 | (save-excursion | |
1334 | (re-search-forward field-pattern nil t)) | |
1335 | (insert-buffer-substring (current-buffer) | |
1336 | (match-beginning 1) | |
1337 | (match-end 1)) | |
1338 | (insert " "))) | |
d1782bd8 KH |
1339 | (list mail-extr-x400-encoded-address-given-name-pattern |
1340 | mail-extr-x400-encoded-address-surname-pattern | |
1341 | mail-extr-x400-encoded-address-full-name-pattern)) | |
1342 | ||
1343 | ;; Discard the rest, since it contains stuff like | |
1344 | ;; routing information, not part of a name. | |
1345 | (mail-extr-skip-whitespace-backward) | |
1346 | (delete-region (point) (point-max)) | |
1347 | ||
1348 | ;; Handle periods used for spacing. | |
1349 | (while (re-search-forward mail-extr-bad-dot-pattern nil t) | |
1350 | (replace-match "\\1 \\2" t)) | |
1351 | ||
1352 | ;; (setq mailbox-name-processed-flag t) | |
1353 | ) | |
1354 | ||
1355 | ;; Handle normal addresses. | |
1356 | (t | |
1357 | (goto-char (point-min)) | |
1358 | ;; Handle _ and = used for spacing. | |
1359 | (while (re-search-forward "\\([^_=]+\\)[_=]" nil t) | |
1360 | (replace-match "\\1 " t) | |
1361 | ;; (setq mailbox-name-processed-flag t) | |
1362 | ) | |
1363 | (goto-char (point-max)))))))) | |
1364 | ||
1365 | ;; undo the dirty deed | |
1366 | (if (not mail-extr-mangle-uucp) | |
1367 | (modify-syntax-entry ?! "." (syntax-table))) | |
1368 | ;; | |
1369 | ;; If we derived the name from the mailbox part of the address, | |
1370 | ;; and we only got one word out of it, don't treat that as a | |
1371 | ;; name. "foo@bar" --> (nil "foo@bar"), not ("foo" "foo@bar") | |
1372 | ;; (if (not mailbox-name-processed-flag) | |
1373 | ;; (delete-region (point-min) (point-max))) | |
1374 | )) | |
1375 | ||
1376 | (set-syntax-table mail-extr-address-text-syntax-table) | |
1377 | ||
1378 | (mail-extr-voodoo mbox-beg mbox-end canonicalization-buffer) | |
1379 | (goto-char (point-min)) | |
1380 | ||
1381 | ;; If name is "First Last" and userid is "F?L", then assume | |
1382 | ;; the middle initial is the second letter in the userid. | |
1383 | ;; Initial code by Jamie Zawinski <jwz@lucid.com> | |
1384 | ;; *** Make it work when there's a suffix as well. | |
1385 | (goto-char (point-min)) | |
d980c402 SM |
1386 | (when (and mail-extr-guess-middle-initial |
1387 | (not disable-initial-guessing-flag) | |
1388 | (eq 3 (- mbox-end mbox-beg)) | |
1389 | (progn | |
1390 | (goto-char (point-min)) | |
1391 | (looking-at mail-extr-two-name-pattern))) | |
1392 | (setq fi (char-after (match-beginning 0)) | |
1393 | li (char-after (match-beginning 3))) | |
1394 | (with-current-buffer canonicalization-buffer | |
1395 | ;; char-equal is ignoring case here, so no need to upcase | |
1396 | ;; or downcase. | |
1397 | (let ((case-fold-search t)) | |
1398 | (and (char-equal fi (char-after mbox-beg)) | |
1399 | (char-equal li (char-after (1- mbox-end))) | |
1400 | (setq mi (char-after (1+ mbox-beg)))))) | |
1401 | (when (and mi | |
1402 | ;; TODO: use better table than syntax table | |
1403 | (eq ?w (char-syntax mi))) | |
1404 | (goto-char (match-beginning 3)) | |
1405 | (insert (upcase mi) ". "))) | |
d1782bd8 KH |
1406 | |
1407 | ;; Nuke name if it is the same as mailbox name. | |
1408 | (let ((buffer-length (- (point-max) (point-min))) | |
1409 | (i 0) | |
1410 | (names-match-flag t)) | |
d980c402 SM |
1411 | (when (and (> buffer-length 0) |
1412 | (eq buffer-length (- mbox-end mbox-beg))) | |
1413 | (goto-char (point-max)) | |
1414 | (insert-buffer-substring canonicalization-buffer | |
1415 | mbox-beg mbox-end) | |
1416 | (while (and names-match-flag | |
1417 | (< i buffer-length)) | |
1418 | (or (eq (downcase (char-after (+ i (point-min)))) | |
1419 | (downcase | |
1420 | (char-after (+ i buffer-length (point-min))))) | |
1421 | (setq names-match-flag nil)) | |
1422 | (setq i (1+ i))) | |
1423 | (delete-region (+ (point-min) buffer-length) (point-max)) | |
1424 | (if names-match-flag | |
1425 | (narrow-to-region (point) (point))))) | |
d1782bd8 KH |
1426 | |
1427 | ;; Nuke name if it's just one word. | |
1428 | (goto-char (point-min)) | |
1429 | (and mail-extr-ignore-single-names | |
1430 | (not (re-search-forward "[- ]" nil t)) | |
1431 | (narrow-to-region (point) (point))) | |
1432 | ||
1433 | ;; Record the result | |
1434 | (setq value-list | |
1435 | (cons (list (if (not (= (point-min) (point-max))) | |
1436 | (buffer-string)) | |
d980c402 | 1437 | (with-current-buffer canonicalization-buffer |
d1782bd8 KH |
1438 | (if (not (= (point-min) (point-max))) |
1439 | (buffer-string)))) | |
1440 | value-list)) | |
1441 | ||
1442 | ;; Unless one address is all we wanted, | |
1443 | ;; delete this one from extraction-buffer | |
1444 | ;; and get ready to extract the next address. | |
1445 | (when all | |
1446 | (if end-of-address | |
1447 | (narrow-to-region 1 end-of-address) | |
1448 | (widen)) | |
1449 | (delete-region (point-min) (point-max)) | |
1450 | (widen)) | |
1451 | ))) | |
1452 | (if all (nreverse value-list) (car value-list)) | |
1453 | )) | |
72c0ae01 | 1454 | |
154b3e39 RS |
1455 | (defun mail-extr-voodoo (mbox-beg mbox-end canonicalization-buffer) |
1456 | (let ((word-count 0) | |
1457 | (case-fold-search nil) | |
1458 | mixed-case-flag lower-case-flag ;;upper-case-flag | |
72c0ae01 | 1459 | suffix-flag last-name-comma-flag |
154b3e39 RS |
1460 | ;;cbeg cend |
1461 | initial | |
1462 | begin-again-flag | |
1463 | drop-this-word-if-trailing-flag | |
1464 | drop-last-word-if-trailing-flag | |
1465 | word-found-flag | |
1466 | this-word-beg last-word-beg | |
1467 | name-beg name-end | |
1468 | name-done-flag | |
72c0ae01 ER |
1469 | ) |
1470 | (save-excursion | |
154b3e39 | 1471 | (set-syntax-table mail-extr-address-text-syntax-table) |
e432fc74 RS |
1472 | |
1473 | ;; Get rid of comments. | |
1474 | (goto-char (point-min)) | |
1475 | (while (not (eobp)) | |
1476 | ;; Initialize for this iteration of the loop. | |
1477 | (skip-chars-forward "^({[\"'`") | |
1478 | (let ((cbeg (point))) | |
1479 | (set-syntax-table mail-extr-address-text-comment-syntax-table) | |
d980c402 SM |
1480 | (if (memq (following-char) '(?\' ?\`)) |
1481 | (search-forward "'" nil 'move | |
1482 | (if (eq ?\' (following-char)) 2 1)) | |
1483 | (or (mail-extr-safe-move-sexp 1) | |
1484 | (goto-char (point-max)))) | |
e432fc74 RS |
1485 | (set-syntax-table mail-extr-address-text-syntax-table) |
1486 | (when (eq (char-after cbeg) ?\() | |
1487 | ;; Delete the comment itself. | |
1488 | (delete-region cbeg (point)) | |
1489 | ;; Canonicalize whitespace where the comment was. | |
1490 | (skip-chars-backward " \t") | |
1491 | (if (looking-at "\\([ \t]+$\\|[ \t]+,\\)") | |
1492 | (replace-match "") | |
1493 | (setq cbeg (point)) | |
1494 | (skip-chars-forward " \t") | |
1495 | (if (bobp) | |
1496 | (delete-region (point) cbeg) | |
1497 | (just-one-space)))))) | |
6c83d99f | 1498 | |
154b3e39 | 1499 | ;; This was moved above. |
72c0ae01 | 1500 | ;; Fix . used as space |
154b3e39 RS |
1501 | ;; But it belongs here because it occurs not only as |
1502 | ;; rypens@reks.uia.ac.be (Piet.Rypens) | |
1503 | ;; but also as | |
1504 | ;; "Piet.Rypens" <rypens@reks.uia.ac.be> | |
1505 | ;;(goto-char (point-min)) | |
1506 | ;;(while (re-search-forward mail-extr-bad-dot-pattern nil t) | |
1507 | ;; (replace-match "\\1 \\2" t)) | |
1508 | ||
d980c402 SM |
1509 | (unless (search-forward " " nil t) |
1510 | (goto-char (point-min)) | |
1511 | (cond ((search-forward "_" nil t) | |
1512 | ;; Handle the *idiotic* use of underlines as spaces. | |
1513 | ;; Example: fml@foo.bar.dom (First_M._Last) | |
1514 | (goto-char (point-min)) | |
1515 | (while (search-forward "_" nil t) | |
1516 | (replace-match " " t))) | |
1517 | ((search-forward "." nil t) | |
1518 | ;; Fix . used as space | |
1519 | ;; Example: danj1@cb.att.com (daniel.jacobson) | |
1520 | (goto-char (point-min)) | |
1521 | (while (re-search-forward mail-extr-bad-dot-pattern nil t) | |
1522 | (replace-match "\\1 \\2" t))))) | |
154b3e39 | 1523 | |
154b3e39 | 1524 | ;; Loop over the words (and other junk) in the name. |
72c0ae01 | 1525 | (goto-char (point-min)) |
154b3e39 | 1526 | (while (not name-done-flag) |
6c83d99f | 1527 | |
d980c402 SM |
1528 | (when word-found-flag |
1529 | ;; Last time through this loop we skipped over a word. | |
1530 | (setq last-word-beg this-word-beg) | |
1531 | (setq drop-last-word-if-trailing-flag | |
1532 | drop-this-word-if-trailing-flag) | |
1533 | (setq word-found-flag nil)) | |
1534 | ||
1535 | (when begin-again-flag | |
1536 | ;; Last time through the loop we found something that | |
1537 | ;; indicates we should pretend we are beginning again from | |
1538 | ;; the start. | |
1539 | (setq word-count 0) | |
1540 | (setq last-word-beg nil) | |
1541 | (setq drop-last-word-if-trailing-flag nil) | |
1542 | (setq mixed-case-flag nil) | |
1543 | (setq lower-case-flag nil) | |
1544 | ;; (setq upper-case-flag nil) | |
1545 | (setq begin-again-flag nil)) | |
6c83d99f | 1546 | |
154b3e39 RS |
1547 | ;; Initialize for this iteration of the loop. |
1548 | (mail-extr-skip-whitespace-forward) | |
1549 | (if (eq word-count 0) (narrow-to-region (point) (point-max))) | |
1550 | (setq this-word-beg (point)) | |
1551 | (setq drop-this-word-if-trailing-flag nil) | |
6c83d99f | 1552 | |
154b3e39 RS |
1553 | ;; Decide what to do based on what we are looking at. |
1554 | (cond | |
6c83d99f | 1555 | |
154b3e39 RS |
1556 | ;; Delete title |
1557 | ((and (eq word-count 0) | |
1558 | (looking-at mail-extr-full-name-prefixes)) | |
1559 | (goto-char (match-end 0)) | |
1560 | (narrow-to-region (point) (point-max))) | |
6c83d99f | 1561 | |
154b3e39 RS |
1562 | ;; Stop after name suffix |
1563 | ((and (>= word-count 2) | |
1564 | (looking-at mail-extr-full-name-suffix-pattern)) | |
1565 | (mail-extr-skip-whitespace-backward) | |
1566 | (setq suffix-flag (point)) | |
1567 | (if (eq ?, (following-char)) | |
1568 | (forward-char 1) | |
1569 | (insert ?,)) | |
1570 | ;; Enforce at least one space after comma | |
1571 | (or (eq ?\ (following-char)) | |
1572 | (insert ?\ )) | |
1573 | (mail-extr-skip-whitespace-forward) | |
1574 | (cond ((memq (following-char) '(?j ?J ?s ?S)) | |
1575 | (capitalize-word 1) | |
1576 | (if (eq (following-char) ?.) | |
1577 | (forward-char 1) | |
1578 | (insert ?.))) | |
1579 | (t | |
1580 | (upcase-word 1))) | |
1581 | (setq word-found-flag t) | |
1582 | (setq name-done-flag t)) | |
6c83d99f | 1583 | |
154b3e39 RS |
1584 | ;; Handle SCA names |
1585 | ((looking-at "MKA \\(.+\\)") ; "Mundanely Known As" | |
1586 | (goto-char (match-beginning 1)) | |
1587 | (narrow-to-region (point) (point-max)) | |
1588 | (setq begin-again-flag t)) | |
6c83d99f | 1589 | |
154b3e39 RS |
1590 | ;; Check for initial last name followed by comma |
1591 | ((and (eq ?, (following-char)) | |
1592 | (eq word-count 1)) | |
1593 | (forward-char 1) | |
1594 | (setq last-name-comma-flag t) | |
1595 | (or (eq ?\ (following-char)) | |
1596 | (insert ?\ ))) | |
6c83d99f | 1597 | |
154b3e39 RS |
1598 | ;; Stop before trailing comma-separated comment |
1599 | ;; THIS CASE MUST BE AFTER THE PRECEDING CASES. | |
1600 | ;; *** This case is redundant??? | |
1601 | ;;((eq ?, (following-char)) | |
1602 | ;; (setq name-done-flag t)) | |
6c83d99f | 1603 | |
154b3e39 RS |
1604 | ;; Delete parenthesized/quoted comment/nickname |
1605 | ((memq (following-char) '(?\( ?\{ ?\[ ?\" ?\' ?\`)) | |
1606 | (setq cbeg (point)) | |
1607 | (set-syntax-table mail-extr-address-text-comment-syntax-table) | |
1608 | (cond ((memq (following-char) '(?\' ?\`)) | |
1609 | (or (search-forward "'" nil t | |
1610 | (if (eq ?\' (following-char)) 2 1)) | |
d980c402 | 1611 | (delete-char 1))) |
154b3e39 RS |
1612 | (t |
1613 | (or (mail-extr-safe-move-sexp 1) | |
1614 | (goto-char (point-max))))) | |
1615 | (set-syntax-table mail-extr-address-text-syntax-table) | |
1616 | (setq cend (point)) | |
72c0ae01 | 1617 | (cond |
154b3e39 | 1618 | ;; Handle case of entire name being quoted |
72c0ae01 | 1619 | ((and (eq word-count 0) |
154b3e39 RS |
1620 | (looking-at " *\\'") |
1621 | (>= (- cend cbeg) 2)) | |
1622 | (narrow-to-region (1+ cbeg) (1- cend)) | |
1623 | (goto-char (point-min))) | |
1624 | (t | |
1625 | ;; Handle case of quoted initial | |
1626 | (if (and (or (= 3 (- cend cbeg)) | |
1627 | (and (= 4 (- cend cbeg)) | |
1628 | (eq ?. (char-after (+ 2 cbeg))))) | |
1629 | (not (looking-at " *\\'"))) | |
1630 | (setq initial (char-after (1+ cbeg))) | |
1631 | (setq initial nil)) | |
1632 | (delete-region cbeg cend) | |
1633 | (if initial | |
1634 | (insert initial ". "))))) | |
6c83d99f | 1635 | |
154b3e39 RS |
1636 | ;; Handle *Stupid* VMS date stamps |
1637 | ((looking-at mail-extr-stupid-vms-date-stamp-pattern) | |
1638 | (replace-match "" t)) | |
6c83d99f | 1639 | |
154b3e39 RS |
1640 | ;; Handle Chinese characters. |
1641 | ((looking-at mail-extr-hz-embedded-gb-encoded-chinese-pattern) | |
1642 | (goto-char (match-end 0)) | |
1643 | (setq word-found-flag t)) | |
6c83d99f | 1644 | |
154b3e39 RS |
1645 | ;; Skip initial garbage characters. |
1646 | ;; THIS CASE MUST BE AFTER THE PRECEDING CASES. | |
1647 | ((and (eq word-count 0) | |
1648 | (looking-at mail-extr-leading-garbage)) | |
1649 | (goto-char (match-end 0)) | |
1650 | ;; *** Skip backward over these??? | |
1651 | ;; (skip-chars-backward "& \"") | |
1652 | (narrow-to-region (point) (point-max))) | |
6c83d99f | 1653 | |
154b3e39 RS |
1654 | ;; Various stopping points |
1655 | ((or | |
6c83d99f | 1656 | |
154b3e39 RS |
1657 | ;; Stop before ALL CAPS acronyms, if preceded by mixed-case |
1658 | ;; words. Example: XT-DEM. | |
1659 | (and (>= word-count 2) | |
1660 | mixed-case-flag | |
1661 | (looking-at mail-extr-weird-acronym-pattern) | |
1662 | (not (looking-at mail-extr-roman-numeral-pattern))) | |
6c83d99f | 1663 | |
154b3e39 RS |
1664 | ;; Stop before trailing alternative address |
1665 | (looking-at mail-extr-alternative-address-pattern) | |
6c83d99f | 1666 | |
154b3e39 RS |
1667 | ;; Stop before trailing comment not introduced by comma |
1668 | ;; THIS CASE MUST BE AFTER AN EARLIER CASE. | |
1669 | (looking-at mail-extr-trailing-comment-start-pattern) | |
6c83d99f | 1670 | |
154b3e39 | 1671 | ;; Stop before telephone numbers |
af604656 RS |
1672 | (and (>= word-count 1) |
1673 | (looking-at mail-extr-telephone-extension-pattern))) | |
154b3e39 | 1674 | (setq name-done-flag t)) |
6c83d99f | 1675 | |
154b3e39 RS |
1676 | ;; Delete ham radio call signs |
1677 | ((looking-at mail-extr-ham-call-sign-pattern) | |
1678 | (delete-region (match-beginning 0) (match-end 0))) | |
6c83d99f | 1679 | |
154b3e39 RS |
1680 | ;; Fixup initials |
1681 | ((looking-at mail-extr-initial-pattern) | |
1682 | (or (eq (following-char) (upcase (following-char))) | |
1683 | (setq lower-case-flag t)) | |
1684 | (forward-char 1) | |
1685 | (if (eq ?. (following-char)) | |
1686 | (forward-char 1) | |
1687 | (insert ?.)) | |
1688 | (or (eq ?\ (following-char)) | |
1689 | (insert ?\ )) | |
1690 | (setq word-found-flag t)) | |
6c83d99f | 1691 | |
154b3e39 RS |
1692 | ;; Handle BITNET LISTSERV list names. |
1693 | ((and (eq word-count 0) | |
1694 | (looking-at mail-extr-listserv-list-name-pattern)) | |
1695 | (narrow-to-region (match-beginning 1) (match-end 1)) | |
1696 | (setq word-found-flag t) | |
1697 | (setq name-done-flag t)) | |
6c83d99f | 1698 | |
524d06f5 RS |
1699 | ;; Handle & substitution, when & is last and is not first. |
1700 | ((and (> word-count 0) | |
1701 | (eq ?\ (preceding-char)) | |
1702 | (eq (following-char) ?&) | |
1703 | (eq (1+ (point)) (point-max))) | |
d980c402 | 1704 | (delete-char 1) |
524d06f5 RS |
1705 | (capitalize-region |
1706 | (point) | |
1707 | (progn | |
1708 | (insert-buffer-substring canonicalization-buffer | |
1709 | mbox-beg mbox-end) | |
1710 | (point))) | |
1711 | (setq disable-initial-guessing-flag t) | |
1712 | (setq word-found-flag t)) | |
1713 | ||
1714 | ;; Handle & between names, as in "Bob & Susie". | |
1715 | ((and (> word-count 0) (eq (following-char) ?\&)) | |
1716 | (setq name-beg (point)) | |
1717 | (setq name-end (1+ name-beg)) | |
1718 | (setq word-found-flag t) | |
1719 | (goto-char name-end)) | |
1720 | ||
154b3e39 RS |
1721 | ;; Regular name words |
1722 | ((looking-at mail-extr-name-pattern) | |
1723 | (setq name-beg (point)) | |
1724 | (setq name-end (match-end 0)) | |
6c83d99f | 1725 | |
154b3e39 RS |
1726 | ;; Certain words will be dropped if they are at the end. |
1727 | (and (>= word-count 2) | |
1728 | (not lower-case-flag) | |
1729 | (or | |
1730 | ;; A trailing 4-or-more letter lowercase words preceded by | |
1731 | ;; mixed case or uppercase words will be dropped. | |
1732 | (looking-at "[a-z][a-z][a-z][a-z]+[ \t]*\\'") | |
1733 | ;; Drop a trailing word which is terminated with a period. | |
1734 | (eq ?. (char-after (1- name-end)))) | |
1735 | (setq drop-this-word-if-trailing-flag t)) | |
6c83d99f | 1736 | |
154b3e39 RS |
1737 | ;; Set the flags that indicate whether we have seen a lowercase |
1738 | ;; word, a mixed case word, and an uppercase word. | |
1739 | (if (re-search-forward "[a-z]" name-end t) | |
1740 | (if (progn | |
1741 | (goto-char name-beg) | |
1742 | (re-search-forward "[A-Z]" name-end t)) | |
1743 | (setq mixed-case-flag t) | |
72c0ae01 | 1744 | (setq lower-case-flag t)) |
154b3e39 RS |
1745 | ;; (setq upper-case-flag t) |
1746 | ) | |
6c83d99f | 1747 | |
154b3e39 RS |
1748 | (goto-char name-end) |
1749 | (setq word-found-flag t)) | |
72c0ae01 | 1750 | |
af604656 RS |
1751 | ;; Allow a number as a word, if it doesn't mean anything else. |
1752 | ((looking-at "[0-9]+\\>") | |
1753 | (setq name-beg (point)) | |
1754 | (setq name-end (match-end 0)) | |
1755 | (goto-char name-end) | |
1756 | (setq word-found-flag t)) | |
1757 | ||
154b3e39 RS |
1758 | (t |
1759 | (setq name-done-flag t) | |
1760 | )) | |
6c83d99f | 1761 | |
154b3e39 RS |
1762 | ;; Count any word that we skipped over. |
1763 | (if word-found-flag | |
1764 | (setq word-count (1+ word-count)))) | |
6c83d99f | 1765 | |
154b3e39 RS |
1766 | ;; If the last thing in the name is 2 or more periods, or one or more |
1767 | ;; other sentence terminators (but not a single period) then keep them | |
4802af76 | 1768 | ;; and the preceding word. This is for the benefit of whole sentences |
154b3e39 RS |
1769 | ;; in the name field: it's better behavior than dropping the last word |
1770 | ;; of the sentence... | |
1771 | (if (and (not suffix-flag) | |
1772 | (looking-at "\\(\\.+\\|[?!;:.][?!;:.]+\\|[?!;:][?!;:.]*\\)\\'")) | |
1773 | (goto-char (setq suffix-flag (point-max)))) | |
1774 | ||
1775 | ;; Drop everything after point and certain trailing words. | |
1776 | (narrow-to-region (point-min) | |
1777 | (or (and drop-last-word-if-trailing-flag | |
1778 | last-word-beg) | |
1779 | (point))) | |
6c83d99f | 1780 | |
154b3e39 RS |
1781 | ;; Xerox's mailers SUCK!!!!!! |
1782 | ;; We simply refuse to believe that any last name is PARC or ADOC. | |
1783 | ;; If it looks like that is the last name, that there is no meaningful | |
1784 | ;; here at all. Actually I guess it would be best to map patterns | |
1785 | ;; like foo.hoser@xerox.com into foo@hoser.xerox.com, but I don't | |
1786 | ;; actually know that that is what's going on. | |
d980c402 SM |
1787 | (unless suffix-flag |
1788 | (goto-char (point-min)) | |
1789 | (let ((case-fold-search t)) | |
1790 | (if (looking-at "[-A-Za-z_]+[. ]\\(PARC\\|ADOC\\)\\'") | |
1791 | (erase-buffer)))) | |
154b3e39 | 1792 | |
72c0ae01 | 1793 | ;; If last name first put it at end (but before suffix) |
d980c402 SM |
1794 | (when last-name-comma-flag |
1795 | (goto-char (point-min)) | |
1796 | (search-forward ",") | |
1797 | (setq name-end (1- (point))) | |
1798 | (goto-char (or suffix-flag (point-max))) | |
1799 | (or (eq ?\ (preceding-char)) | |
1800 | (insert ?\ )) | |
1801 | (insert-buffer-substring (current-buffer) (point-min) name-end) | |
1802 | (goto-char name-end) | |
1803 | (skip-chars-forward "\t ,") | |
1804 | (narrow-to-region (point) (point-max))) | |
6c83d99f | 1805 | |
154b3e39 | 1806 | ;; Delete leading and trailing junk characters. |
4802af76 | 1807 | ;; *** This is probably completely unneeded now. |
154b3e39 RS |
1808 | ;;(goto-char (point-max)) |
1809 | ;;(skip-chars-backward mail-extr-non-end-name-chars) | |
1810 | ;;(if (eq ?. (following-char)) | |
1811 | ;; (forward-char 1)) | |
1812 | ;;(narrow-to-region (point) | |
1813 | ;; (progn | |
1814 | ;; (goto-char (point-min)) | |
1815 | ;; (skip-chars-forward mail-extr-non-begin-name-chars) | |
1816 | ;; (point))) | |
6c83d99f | 1817 | |
72c0ae01 ER |
1818 | ;; Compress whitespace |
1819 | (goto-char (point-min)) | |
1820 | (while (re-search-forward "[ \t\n]+" nil t) | |
154b3e39 | 1821 | (replace-match (if (eobp) "" " ") t)) |
72c0ae01 ER |
1822 | ))) |
1823 | ||
154b3e39 | 1824 | \f |
72c0ae01 | 1825 | |
154b3e39 RS |
1826 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1827 | ;; | |
1828 | ;; Table of top-level domain names. | |
1829 | ;; | |
1830 | ;; This is used during address canonicalization; be careful of format changes. | |
72c0ae01 ER |
1831 | ;; Keep in mind that the country abbreviations follow ISO-3166. There is |
1832 | ;; a U.S. FIPS that specifies a different set of two-letter country | |
1833 | ;; abbreviations. | |
97f83272 SJ |
1834 | ;; |
1835 | ;; Updated by the RIPE Network Coordination Centre. | |
1836 | ;; | |
1837 | ;; Source: ISO 3166 Maintenance Agency | |
1838 | ;; http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1-semic.txt | |
1839 | ;; http://www.iana.org/domain-names.htm | |
1840 | ;; http://www.iana.org/cctld/cctld-whois.htm | |
1841 | ;; Latest change: Mon Jul 8 14:21:59 CEST 2002 | |
72c0ae01 | 1842 | |
154b3e39 | 1843 | (defconst mail-extr-all-top-level-domains |
0b725d8c | 1844 | (let ((ob (make-vector 739 0))) |
d980c402 SM |
1845 | (mapc |
1846 | (lambda (x) | |
1847 | (put (intern (downcase (car x)) ob) | |
1848 | 'domain-name | |
1849 | (if (nth 2 x) | |
1850 | (format (nth 2 x) (nth 1 x)) | |
1851 | (nth 1 x)))) | |
e1a34b58 RS |
1852 | '( |
1853 | ;; ISO 3166 codes: | |
44248b7f | 1854 | ("ad" "Andorra") |
e1a34b58 | 1855 | ("ae" "United Arab Emirates") |
0b725d8c | 1856 | ("af" "Afghanistan") |
e1a34b58 | 1857 | ("ag" "Antigua and Barbuda") |
0b725d8c | 1858 | ("ai" "Anguilla") |
e1a34b58 | 1859 | ("al" "Albania") |
44248b7f | 1860 | ("am" "Armenia") |
0b725d8c | 1861 | ("an" "Netherlands Antilles") |
e1a34b58 RS |
1862 | ("ao" "Angola") |
1863 | ("aq" "Antarctica") ; continent | |
154b3e39 | 1864 | ("ar" "Argentina" "Argentine Republic") |
0b725d8c | 1865 | ("as" "American Samoa") |
154b3e39 RS |
1866 | ("at" "Austria" "The Republic of %s") |
1867 | ("au" "Australia") | |
0b725d8c | 1868 | ("aw" "Aruba") |
e1a34b58 | 1869 | ("az" "Azerbaijan") |
44248b7f | 1870 | ("ba" "Bosnia-Herzegovina") |
154b3e39 | 1871 | ("bb" "Barbados") |
e1a34b58 | 1872 | ("bd" "Bangladesh") |
154b3e39 | 1873 | ("be" "Belgium" "The Kingdom of %s") |
e1a34b58 | 1874 | ("bf" "Burkina Faso") |
154b3e39 | 1875 | ("bg" "Bulgaria") |
e1a34b58 | 1876 | ("bh" "Bahrain") |
0b725d8c GM |
1877 | ("bi" "Burundi") |
1878 | ("bj" "Benin") | |
e1a34b58 | 1879 | ("bm" "Bermuda") |
0b725d8c | 1880 | ("bn" "Brunei Darussalam") |
154b3e39 RS |
1881 | ("bo" "Bolivia" "Republic of %s") |
1882 | ("br" "Brazil" "The Federative Republic of %s") | |
1883 | ("bs" "Bahamas") | |
0b725d8c GM |
1884 | ("bt" "Bhutan") |
1885 | ("bv" "Bouvet Island") | |
e1a34b58 RS |
1886 | ("bw" "Botswana") |
1887 | ("by" "Belarus") | |
154b3e39 RS |
1888 | ("bz" "Belize") |
1889 | ("ca" "Canada") | |
0b725d8c | 1890 | ("cc" "Cocos (Keeling) Islands") |
97f83272 | 1891 | ("cd" "Congo" "The Democratic Republic of the %s") |
0b725d8c | 1892 | ("cf" "Central African Republic") |
e1a34b58 | 1893 | ("cg" "Congo") |
154b3e39 | 1894 | ("ch" "Switzerland" "The Swiss Confederation") |
0b725d8c GM |
1895 | ("ci" "Ivory Coast") ; Cote D'ivoire |
1896 | ("ck" "Cook Islands") | |
154b3e39 | 1897 | ("cl" "Chile" "The Republic of %s") |
e1a34b58 | 1898 | ("cm" "Cameroon") ; In .fr domain |
154b3e39 | 1899 | ("cn" "China" "The People's Republic of %s") |
e1a34b58 | 1900 | ("co" "Colombia") |
154b3e39 | 1901 | ("cr" "Costa Rica" "The Republic of %s") |
e1a34b58 | 1902 | ("cu" "Cuba") |
0b725d8c GM |
1903 | ("cv" "Cape Verde") |
1904 | ("cx" "Christmas Island") | |
e1a34b58 RS |
1905 | ("cy" "Cyprus") |
1906 | ("cz" "Czech Republic") | |
154b3e39 | 1907 | ("de" "Germany") |
0b725d8c | 1908 | ("dj" "Djibouti") |
154b3e39 RS |
1909 | ("dk" "Denmark") |
1910 | ("dm" "Dominica") | |
1911 | ("do" "Dominican Republic" "The %s") | |
e1a34b58 | 1912 | ("dz" "Algeria") |
154b3e39 | 1913 | ("ec" "Ecuador" "The Republic of %s") |
e1a34b58 | 1914 | ("ee" "Estonia") |
154b3e39 | 1915 | ("eg" "Egypt" "The Arab Republic of %s") |
0b725d8c | 1916 | ("eh" "Western Sahara") |
e1a34b58 | 1917 | ("er" "Eritrea") |
154b3e39 | 1918 | ("es" "Spain" "The Kingdom of %s") |
44248b7f | 1919 | ("et" "Ethiopia") |
154b3e39 | 1920 | ("fi" "Finland" "The Republic of %s") |
0b725d8c GM |
1921 | ("fj" "Fiji") |
1922 | ("fk" "Falkland Islands (Malvinas)") | |
1923 | ("fm" "Micronesia" "Federated States of %s") | |
e1a34b58 | 1924 | ("fo" "Faroe Islands") |
154b3e39 | 1925 | ("fr" "France") |
44248b7f KH |
1926 | ("ga" "Gabon") |
1927 | ("gb" "United Kingdom") | |
e1a34b58 RS |
1928 | ("gd" "Grenada") |
1929 | ("ge" "Georgia") | |
0b725d8c GM |
1930 | ("gf" "French Guiana") |
1931 | ("gh" "Ghana") | |
1932 | ("gi" "Gibraltar") | |
44248b7f KH |
1933 | ("gl" "Greenland") |
1934 | ("gm" "Gambia") | |
0b725d8c | 1935 | ("gn" "Guinea") |
e1a34b58 | 1936 | ("gp" "Guadeloupe (Fr.)") |
0b725d8c | 1937 | ("gq" "Equatorial Guinea") |
154b3e39 | 1938 | ("gr" "Greece" "The Hellenic Republic (%s)") |
97f83272 | 1939 | ("gs" "South Georgia and The South Sandwich Islands") |
e1a34b58 RS |
1940 | ("gt" "Guatemala") |
1941 | ("gu" "Guam (U.S.)") | |
0b725d8c GM |
1942 | ("gw" "Guinea-Bissau") |
1943 | ("gy" "Guyana") | |
154b3e39 | 1944 | ("hk" "Hong Kong") |
97f83272 | 1945 | ("hm" "Heard Island and Mcdonald Islands") |
e1a34b58 | 1946 | ("hn" "Honduras") |
44248b7f | 1947 | ("hr" "Croatia" "Croatia (Hrvatska)") |
e1a34b58 | 1948 | ("ht" "Haiti") |
44248b7f | 1949 | ("hu" "Hungary" "The Hungarian Republic") |
e1a34b58 | 1950 | ("id" "Indonesia") |
154b3e39 RS |
1951 | ("ie" "Ireland") |
1952 | ("il" "Israel" "The State of %s") | |
0b725d8c | 1953 | ("im" "Isle of Man" "The %s") ; NOT in ISO 3166-1 of 2001-02-26 |
154b3e39 | 1954 | ("in" "India" "The Republic of %s") |
0b725d8c GM |
1955 | ("io" "British Indian Ocean Territory") |
1956 | ("iq" "Iraq") | |
1957 | ("ir" "Iran" "Islamic Republic of %s") | |
154b3e39 RS |
1958 | ("is" "Iceland" "The Republic of %s") |
1959 | ("it" "Italy" "The Italian Republic") | |
1960 | ("jm" "Jamaica") | |
44248b7f | 1961 | ("jo" "Jordan") |
154b3e39 | 1962 | ("jp" "Japan") |
e1a34b58 | 1963 | ("ke" "Kenya") |
0b725d8c GM |
1964 | ("kg" "Kyrgyzstan") |
1965 | ("kh" "Cambodia") | |
1966 | ("ki" "Kiribati") | |
1967 | ("km" "Comoros") | |
1968 | ("kn" "Saint Kitts and Nevis") | |
1969 | ("kp" "Korea (North)" "Democratic People's Republic of Korea") | |
1970 | ("kr" "Korea (South)" "Republic of Korea") | |
e1a34b58 | 1971 | ("kw" "Kuwait") |
0b725d8c | 1972 | ("ky" "Cayman Islands") |
97f83272 | 1973 | ("kz" "Kazakhstan") |
0b725d8c | 1974 | ("la" "Lao People's Democratic Republic") |
e1a34b58 | 1975 | ("lb" "Lebanon") |
0b725d8c | 1976 | ("lc" "Saint Lucia") |
e1a34b58 | 1977 | ("li" "Liechtenstein") |
154b3e39 | 1978 | ("lk" "Sri Lanka" "The Democratic Socialist Republic of %s") |
0b725d8c | 1979 | ("lr" "Liberia") |
e1a34b58 RS |
1980 | ("ls" "Lesotho") |
1981 | ("lt" "Lithuania") | |
1982 | ("lu" "Luxembourg") | |
1983 | ("lv" "Latvia") | |
0b725d8c | 1984 | ("ly" "Libyan Arab Jamahiriya") |
e1a34b58 | 1985 | ("ma" "Morocco") |
44248b7f KH |
1986 | ("mc" "Monaco") |
1987 | ("md" "Moldova" "The Republic of %s") | |
e1a34b58 | 1988 | ("mg" "Madagascar") |
0b725d8c GM |
1989 | ("mh" "Marshall Islands") |
1990 | ("mk" "Macedonia" "The Former Yugoslav Republic of %s") | |
e1a34b58 | 1991 | ("ml" "Mali") |
0b725d8c GM |
1992 | ("mm" "Myanmar") |
1993 | ("mn" "Mongolia") | |
97f83272 | 1994 | ("mo" "Macao") |
0b725d8c GM |
1995 | ("mp" "Northern Mariana Islands") |
1996 | ("mq" "Martinique") | |
1997 | ("mr" "Mauritania") | |
1998 | ("ms" "Montserrat") | |
e1a34b58 RS |
1999 | ("mt" "Malta") |
2000 | ("mu" "Mauritius") | |
44248b7f | 2001 | ("mv" "Maldives") |
e1a34b58 | 2002 | ("mw" "Malawi") |
154b3e39 | 2003 | ("mx" "Mexico" "The United Mexican States") |
97f83272 | 2004 | ("my" "Malaysia") |
e1a34b58 | 2005 | ("mz" "Mozambique") |
154b3e39 | 2006 | ("na" "Namibia") |
e1a34b58 RS |
2007 | ("nc" "New Caledonia (Fr.)") |
2008 | ("ne" "Niger") ; In .fr domain | |
0b725d8c GM |
2009 | ("nf" "Norfolk Island") |
2010 | ("ng" "Nigeria") | |
154b3e39 RS |
2011 | ("ni" "Nicaragua" "The Republic of %s") |
2012 | ("nl" "Netherlands" "The Kingdom of the %s") | |
2013 | ("no" "Norway" "The Kingdom of %s") | |
e1a34b58 | 2014 | ("np" "Nepal") ; Via .in domain |
0b725d8c | 2015 | ("nr" "Nauru") |
44248b7f | 2016 | ("nu" "Niue") |
154b3e39 | 2017 | ("nz" "New Zealand") |
0b725d8c | 2018 | ("om" "Oman") |
e1a34b58 | 2019 | ("pa" "Panama") |
154b3e39 | 2020 | ("pe" "Peru") |
97f83272 | 2021 | ("pf" "French Polynesia") |
154b3e39 RS |
2022 | ("pg" "Papua New Guinea") |
2023 | ("ph" "Philippines" "The Republic of the %s") | |
e1a34b58 | 2024 | ("pk" "Pakistan") |
154b3e39 | 2025 | ("pl" "Poland") |
0b725d8c GM |
2026 | ("pm" "Saint Pierre and Miquelon") |
2027 | ("pn" "Pitcairn") | |
e1a34b58 | 2028 | ("pr" "Puerto Rico (U.S.)") |
0b725d8c | 2029 | ("ps" "Palestinian Territory, Occupied") |
4802af76 | 2030 | ("pt" "Portugal" "The Portuguese Republic") |
0b725d8c | 2031 | ("pw" "Palau") |
154b3e39 | 2032 | ("py" "Paraguay") |
44248b7f | 2033 | ("qa" "Qatar") |
e1a34b58 RS |
2034 | ("re" "Reunion (Fr.)") ; In .fr domain |
2035 | ("ro" "Romania") | |
0b725d8c GM |
2036 | ("ru" "Russia" "Russian Federation") |
2037 | ("rw" "Rwanda") | |
e1a34b58 | 2038 | ("sa" "Saudi Arabia") |
0b725d8c | 2039 | ("sb" "Solomon Islands") |
e1a34b58 RS |
2040 | ("sc" "Seychelles") |
2041 | ("sd" "Sudan") | |
154b3e39 RS |
2042 | ("se" "Sweden" "The Kingdom of %s") |
2043 | ("sg" "Singapore" "The Republic of %s") | |
0b725d8c | 2044 | ("sh" "Saint Helena") |
e1a34b58 | 2045 | ("si" "Slovenia") |
0b725d8c | 2046 | ("sj" "Svalbard and Jan Mayen") ; In .no domain |
e1a34b58 | 2047 | ("sk" "Slovakia" "The Slovak Republic") |
0b725d8c | 2048 | ("sl" "Sierra Leone") |
44248b7f | 2049 | ("sm" "San Marino") |
e1a34b58 | 2050 | ("sn" "Senegal") |
0b725d8c | 2051 | ("so" "Somalia") |
154b3e39 | 2052 | ("sr" "Suriname") |
97f83272 | 2053 | ("st" "Sao Tome and Principe") |
0b725d8c GM |
2054 | ("su" "U.S.S.R." "The Union of Soviet Socialist Republics") |
2055 | ("sv" "El Salvador") | |
2056 | ("sy" "Syrian Arab Republic") | |
e1a34b58 | 2057 | ("sz" "Swaziland") |
97f83272 | 2058 | ("tc" "Turks and Caicos Islands") |
0b725d8c GM |
2059 | ("td" "Chad") |
2060 | ("tf" "French Southern Territories") | |
e1a34b58 | 2061 | ("tg" "Togo") |
154b3e39 | 2062 | ("th" "Thailand" "The Kingdom of %s") |
0b725d8c GM |
2063 | ("tj" "Tajikistan") |
2064 | ("tk" "Tokelau") | |
97f83272 | 2065 | ("tl" "East Timor") |
0b725d8c | 2066 | ("tm" "Turkmenistan") |
154b3e39 | 2067 | ("tn" "Tunisia") |
44248b7f | 2068 | ("to" "Tonga") |
0b725d8c | 2069 | ("tp" "East Timor") |
154b3e39 RS |
2070 | ("tr" "Turkey" "The Republic of %s") |
2071 | ("tt" "Trinidad and Tobago") | |
0b725d8c GM |
2072 | ("tv" "Tuvalu") |
2073 | ("tw" "Taiwan" "%s, Province of China") | |
2074 | ("tz" "Tanzania" "United Republic of %s") | |
e1a34b58 | 2075 | ("ua" "Ukraine") |
0b725d8c | 2076 | ("ug" "Uganda") |
44a1338a | 2077 | ("uk" "United Kingdom" "The %s of Great Britain and Northern Ireland") |
0b725d8c | 2078 | ("um" "United States Minor Outlying Islands") |
e1a34b58 | 2079 | ("us" "United States" "The %s of America") |
154b3e39 | 2080 | ("uy" "Uruguay" "The Eastern Republic of %s") |
0b725d8c GM |
2081 | ("uz" "Uzbekistan") |
2082 | ("va" "Holy See (Vatican City State)") | |
97f83272 | 2083 | ("vc" "Saint Vincent and the Grenadines") |
154b3e39 | 2084 | ("ve" "Venezuela" "The Republic of %s") |
0b725d8c GM |
2085 | ("vg" "Virgin Islands, British") |
2086 | ("vi" "Virgin Islands, U.S.") | |
e1a34b58 RS |
2087 | ("vn" "Vietnam") |
2088 | ("vu" "Vanuatu") | |
0b725d8c GM |
2089 | ("wf" "Wallis and Futuna") |
2090 | ("ws" "Samoa") | |
2091 | ("ye" "Yemen") | |
2092 | ("yt" "Mayotte") | |
44248b7f KH |
2093 | ("yu" "Yugoslavia" "Yugoslavia, AKA Serbia-Montenegro") |
2094 | ("za" "South Africa" "The Republic of %s") | |
0b725d8c | 2095 | ("zm" "Zambia") |
154b3e39 | 2096 | ("zw" "Zimbabwe" "Republic of %s") |
97f83272 SJ |
2097 | ;; Generic Domains: |
2098 | ("aero" t "Air Transport Industry") | |
2099 | ("biz" t "Businesses") | |
e1a34b58 | 2100 | ("com" t "Commercial") |
97f83272 SJ |
2101 | ("coop" t "Cooperative Associations") |
2102 | ("info" t "Info") | |
2103 | ("museum" t "Museums") | |
2104 | ("name" t "Individuals") | |
e1a34b58 RS |
2105 | ("net" t "Network") |
2106 | ("org" t "Non-profit Organization") | |
97f83272 SJ |
2107 | ;;("pro" t "Credentialed professionals") |
2108 | ;;("bitnet" t "Because It's Time NET") | |
2109 | ("gov" t "United States Government") | |
2110 | ("edu" t "Educational") | |
2111 | ("mil" t "United States Military") | |
2112 | ("int" t "International Treaties") | |
2113 | ;;("nato" t "North Atlantic Treaty Organization") | |
e1a34b58 | 2114 | ("uucp" t "Unix to Unix CoPy") |
97f83272 SJ |
2115 | ;; Infrastructure Domains: |
2116 | ("arpa" t "Advanced Research Projects Agency (U.S. DoD)") | |
154b3e39 RS |
2117 | )) |
2118 | ob)) | |
2119 | ||
2120 | ;;;###autoload | |
2121 | (defun what-domain (domain) | |
e1cd65fd | 2122 | "Convert mail domain DOMAIN to the country it corresponds to." |
154b3e39 RS |
2123 | (interactive |
2124 | (let ((completion-ignore-case t)) | |
2125 | (list (completing-read "Domain: " | |
2126 | mail-extr-all-top-level-domains nil t)))) | |
2127 | (or (setq domain (intern-soft (downcase domain) | |
2128 | mail-extr-all-top-level-domains)) | |
29565a87 | 2129 | (error "No such domain")) |
154b3e39 | 2130 | (message "%s: %s" (upcase (symbol-name domain)) (get domain 'domain-name))) |
72c0ae01 ER |
2131 | |
2132 | \f | |
154b3e39 RS |
2133 | ;(let ((all nil)) |
2134 | ; (mapatoms #'(lambda (x) | |
6c83d99f | 2135 | ; (if (and (boundp x) |
154b3e39 RS |
2136 | ; (string-match "^mail-extr-" (symbol-name x))) |
2137 | ; (setq all (cons x all))))) | |
2138 | ; (setq all (sort all #'string-lessp)) | |
2139 | ; (cons 'setq | |
2140 | ; (apply 'nconc (mapcar #'(lambda (x) | |
2141 | ; (list x (symbol-value x))) | |
2142 | ; all)))) | |
72c0ae01 ER |
2143 | |
2144 | \f | |
154b3e39 | 2145 | (provide 'mail-extr) |
72c0ae01 | 2146 | |
ab5796a9 | 2147 | ;;; arch-tag: 7785fade-1073-4ed6-b4f6-28db34a7982d |
72c0ae01 | 2148 | ;;; mail-extr.el ends here |