]> code.delx.au - gnu-emacs/blob - lisp/mail/mail-extr.el
(report-emacs-bug-text-prompt): New variable.
[gnu-emacs] / lisp / mail / mail-extr.el
1 ;;; mail-extr.el --- extract full name and address from RFC 822 mail header
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1997, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Joe Wells <jbw@cs.bu.edu>
7 ;; Maintainer: FSF
8 ;; Keywords: mail
9
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
14 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
26
27 ;;; Commentary:
28
29 ;; The entry point of this code is
30 ;;
31 ;; mail-extract-address-components: (address &optional all)
32 ;;
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.
36 ;; ADDRESS may be a string or a buffer. If it is a buffer, the visible
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 ;;
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 ;;
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 ;;
50 ;; There are two main benefits:
51 ;;
52 ;; 1. Higher probability of getting the correct full name for a human than
53 ;; any other package we know of. (On the other hand, it will cheerfully
54 ;; mangle non-human names/comments.)
55 ;; 2. Address part is put in a canonical form.
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
60 ;; work with the code. Developing this code requires frequent testing to
61 ;; make sure you're not breaking functionality. The test cases aren't included
62 ;; because they are over 100K.
63 ;;
64 ;; If you find an address that mail-extr fails on, please send it to the
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
67 ;; correspond to a real human full name, although we would prefer that
68 ;; mail-extr would return the comment as-is.
69 ;;
70 ;; Features:
71 ;;
72 ;; * Full name handling:
73 ;;
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.
79 ;; * understands & referring to the mailbox name, capitalized.
80 ;; * strips name prefixes like "Prof.", etc.
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.
86 ;;
87 ;; * Address handling:
88 ;;
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).
98 ;;
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.
102 ;;
103 ;; TODO:
104 ;;
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 %.
121 ;; * insert documentation strings!
122 ;; * handle X.400-gatewayed addresses according to RFC 1148.
123
124 ;;; Change Log:
125 ;;
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)
143 ;;
144 ;; * Made mail-full-name-prefixes a user-customizable variable.
145 ;; Allow passing the address as a buffer as well as as a string.
146 ;; Allow [ and ] as name characters (Finnish character set).
147 ;;
148 ;; Mon Mar 22 21:20:56 1993 Joe Wells (jbw at bigbird.bu.edu)
149 ;;
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.
156 ;;
157 ;; Mon Mar 22 20:16:57 1993 Joe Wells (jbw at bigbird.bu.edu)
158 ;;
159 ;; * Handle HZ encoding for embedding GB encoded chinese characters.
160 ;;
161 ;; Mon Mar 22 00:46:12 1993 Joe Wells (jbw at bigbird.bu.edu)
162 ;;
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.
172 ;;
173 ;; Sun Mar 21 21:41:06 1993 Joe Wells (jbw at bigbird.bu.edu)
174 ;;
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.
178 ;;
179 ;; Sun Mar 21 14:39:38 1993 Joe Wells (jbw at bigbird.bu.edu)
180 ;;
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.
184 ;;
185 ;; Mon Feb 1 22:23:31 1993 Joe Wells (jbw at bigbird.bu.edu)
186 ;;
187 ;; * Added in changes by Rod Whitby and Jamie Zawinski. This
188 ;; includes the flag mail-extr-guess-middle-initial and the fix for
189 ;; handling multiple addresses correctly. (Whitby just changed
190 ;; a > to a <.)
191 ;;
192 ;; Mon Apr 6 23:59:09 1992 Joe Wells (jbw at bigbird.bu.edu)
193 ;;
194 ;; * Cleaned up some more. Release version 1.0 to world.
195 ;;
196 ;; Sun Apr 5 19:39:08 1992 Joe Wells (jbw at bigbird.bu.edu)
197 ;;
198 ;; * Cleaned up full name extraction extensively.
199 ;;
200 ;; Sun Feb 2 14:45:24 1992 Joe Wells (jbw at bigbird.bu.edu)
201 ;;
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.
205 ;;
206 ;; Fri Jun 14 19:39:50 1991
207 ;; * Created.
208
209 ;;; Code:
210 \f
211
212 (defgroup mail-extr nil
213 "Extract full name and address from RFC 822 mail header."
214 :prefix "mail-extr-"
215 :group 'mail)
216
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;;
219 ;; User configuration variable definitions.
220 ;;
221
222 (defcustom mail-extr-guess-middle-initial nil
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>\"
225 we will assume that \"John Q. Smith\" is the fellow's name."
226 :type 'boolean
227 :group 'mail-extr)
228
229 (defcustom mail-extr-ignore-single-names t
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>\"
232 we will act as though we couldn't find a full name in the address."
233 :type 'boolean
234 :group 'mail-extr)
235
236 ;; Matches a leading title that is not part of the name (does not
237 ;; contribute to uniquely identifying the person).
238 (defcustom mail-extr-full-name-prefixes
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
243 uniquely identifying the person."
244 :type 'regexp
245 :group 'mail-extr)
246
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)
251
252 (defcustom mail-extr-mangle-uucp nil
253 "*Whether to throw away information in UUCP addresses
254 by translating things like \"foo!bar!baz@host\" into \"baz@bar.UUCP\"."
255 :type 'boolean
256 :group 'mail-extr)
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
269 \f
270
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.
296
297 ;; Any character that can occur in a name, not counting characters that
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.
302 (defconst mail-extr-all-letters-but-separators
303 (purecopy "][[:alnum:]{|}'~`"))
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.
313 (defconst mail-extr-first-letters (purecopy "[:alpha:]"))
314
315 ;; Any character that can end a name.
316 ;; Keep this set as minimal as possible.
317 (defconst mail-extr-last-letters (purecopy "[:alpha:]`'."))
318
319 (defconst mail-extr-leading-garbage "\\W+")
320
321 ;; (defconst mail-extr-non-name-chars
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)))
327
328 ;; Matches an initial not followed by both a period and a space.
329 ;; (defconst mail-extr-bad-initials-pattern
330 ;; (purecopy
331 ;; (format "\\(\\([^%s]\\|\\`\\)[%s]\\)\\(\\.\\([^ ]\\)\\| \\|\\([^%s .]\\)\\|\\'\\)"
332 ;; mail-extr-all-letters mail-extr-first-letters mail-extr-all-letters)))
333
334 ;; Matches periods used instead of spaces. Must not match the period
335 ;; following an initial.
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)))
342
343 ;; Matches an embedded or leading nickname that should be removed.
344 ;; (defconst mail-extr-nickname-pattern
345 ;; (purecopy
346 ;; (format "\\([ .]\\|\\`\\)[\"'`\[\(]\\([ .%s]+\\)[\]\"'\)] "
347 ;; mail-extr-all-letters)))
348
349 ;; Matches the occurrence of a generational name suffix, and the last
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"))
361
362 ;; Matches a trailing uppercase (with other characters possible) acronym.
363 ;; Must not match a trailing uppercase last name or trailing initial
364 (defconst mail-extr-weird-acronym-pattern
365 (purecopy "\\([A-Z]+[-_/]\\|[A-Z][A-Z][A-Z]?\\b\\)"))
366
367 ;; Matches a mixed-case or lowercase name (not an initial).
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)))
376
377 ;; Matches a trailing alternative address.
378 ;; #### Match Latin1 letters here too?
379 ;; #### Match _ before @ here too?
380 (defconst mail-extr-alternative-address-pattern
381 (purecopy "\\(aka *\\)?[a-zA-Z.]+[!@][a-zA-Z.]"))
382
383 ;; Matches a variety of trailing comments not including comma-delimited
384 ;; comments.
385 (defconst mail-extr-trailing-comment-start-pattern
386 (purecopy " [-{]\\|--\\|[+@#></\;]"))
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.
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)))
396
397 (defconst mail-extr-initial-pattern
398 (purecopy (format "\\b[%s]\\([. ]\\|\\b\\)" mail-extr-first-letters)))
399
400 ;; Matches a single name before a comma.
401 ;; (defconst mail-extr-last-name-first-pattern
402 ;; (purecopy (concat "\\`" mail-extr-name-pattern ",")))
403
404 ;; Matches telephone extensions.
405 (defconst mail-extr-telephone-extension-pattern
406 (purecopy
407 "\\(\\([Ee]xt\\|\\|[Tt]ph\\|[Tt]el\\|[Xx]\\).?\\)? *\\+?[0-9][- 0-9]+"))
408
409 ;; Matches ham radio call signs.
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.
427
428 ;; Matches normal single-part name
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 "\\'")))
438
439 ;; Matches normal two names with missing middle initial
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.
462 ;;
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.
467 ;;
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))
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))))))))
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
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 (?% ".")
553 (?! ".") ;; this needs to be word-constituent when not in .UUCP mode
554 )
555 (mail-extr-address-comment-syntax-table
556 (?\000 ?\377 "w")
557 (?\040 " ")
558 (?\240 " ")
559 (?\t " ")
560 (?\r " ")
561 (?\n " ")
562 (?\( "\(\)")
563 (?\) "\)\(")
564 (?\\ "\\"))
565 (mail-extr-address-domain-literal-syntax-table
566 (?\000 ?\377 "w")
567 (?\040 " ")
568 (?\240 " ")
569 (?\t " ")
570 (?\r " ")
571 (?\n " ")
572 (?\[ "\(\]") ;??????
573 (?\] "\)\[") ;??????
574 (?\\ "\\"))
575 (mail-extr-address-text-comment-syntax-table
576 (?\000 ?\377 "w")
577 (?\040 " ")
578 (?\240 " ")
579 (?\t " ")
580 (?\r " ")
581 (?\n " ")
582 (?\( "\(\)")
583 (?\) "\)\(")
584 (?\[ "\(\]")
585 (?\] "\)\[")
586 (?\{ "\(\}")
587 (?\} "\)\{")
588 (?\\ "\\")
589 (?\" "\"")
590 ;; (?\' "\)\`")
591 ;; (?\` "\(\'")
592 )
593 (mail-extr-address-text-syntax-table
594 (?\000 ?\177 ".")
595 (?\200 ?\377 "w")
596 (?\040 " ")
597 (?\t " ")
598 (?\r " ")
599 (?\n " ")
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
612 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613 ;;
614 ;; Utility functions and macros.
615 ;;
616
617 (defsubst mail-extr-skip-whitespace-forward ()
618 ;; v19 fn skip-syntax-forward is more tasteful, but not byte-coded.
619 (skip-chars-forward " \t\n\r\240"))
620
621 (defsubst mail-extr-skip-whitespace-backward ()
622 ;; v19 fn skip-syntax-backward is more tasteful, but not byte-coded.
623 (skip-chars-backward " \t\n\r\240"))
624
625
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)
633 (delete-char -1)
634 (or (eobp)
635 (forward-char 1))))))
636
637 (defsubst mail-extr-nuke-char-at (pos)
638 (save-excursion
639 (goto-char pos)
640 (delete-char 1)
641 (insert ?\ )))
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)
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."
657 (or (memq no-replace '(t nil))
658 (error "no-replace must be t or nil, evaluable at macroexpand-time"))
659 `(let ((temp ,list-symbol)
660 ch)
661 (while temp
662 (setq ch (car temp))
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))
669 (setq temp (cdr temp)))
670 (setq ,list-symbol (delq nil ,list-symbol))))
671
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
687 (defsubst mail-extr-safe-move-sexp (arg)
688 ;; Safely skip over one balanced sexp, if there is one. Return t if success.
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)))))))
699 \f
700 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
701 ;;
702 ;; The main function to grind addresses
703 ;;
704
705 (defvar disable-initial-guessing-flag) ; dynamic assignment
706 (defvar cbeg) ; dynamic assignment
707 (defvar cend) ; dynamic assignment
708
709 ;;;###autoload
710 (defun mail-extract-address-components (address &optional all)
711 "Given an RFC-822 address ADDRESS, extract full name and canonical address.
712 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
713 If no name can be extracted, FULL-NAME will be nil.
714
715 If the optional argument ALL is non-nil, then ADDRESS can contain zero
716 or more recipients, separated by commas, and we return a list of
717 the form ((FULL-NAME CANONICAL-ADDRESS) ...) with one element for
718 each recipient. If ALL is nil, then if ADDRESS contains more than
719 one recipients, all but the first is ignored.
720
721 ADDRESS may be a string or a buffer. If it is a buffer, the visible
722 (narrowed) portion of the buffer will be interpreted as the address.
723 (This feature exists so that the clever caller might be able to avoid
724 consing a string.)"
725 (let ((canonicalization-buffer (get-buffer-create " *canonical address*"))
726 (extraction-buffer (get-buffer-create " *extract address components*"))
727 value-list)
728
729 (with-current-buffer (get-buffer-create extraction-buffer)
730 (fundamental-mode)
731 (buffer-disable-undo extraction-buffer)
732 (set-syntax-table mail-extr-address-syntax-table)
733 (widen)
734 (erase-buffer)
735 (setq case-fold-search nil)
736
737 ;; Insert extra space at beginning to allow later replacement with <
738 ;; without having to move markers.
739 (insert ?\ )
740
741 ;; Insert the address itself.
742 (cond ((stringp address)
743 (insert address))
744 ((bufferp address)
745 (insert-buffer-substring address))
746 (t
747 (error "Invalid address: %s" address)))
748
749 (set-text-properties (point-min) (point-max) nil)
750
751 (with-current-buffer (get-buffer-create canonicalization-buffer)
752 (fundamental-mode)
753 (buffer-disable-undo canonicalization-buffer)
754 (setq case-fold-search nil))
755
756
757 ;; Unfold multiple lines.
758 (goto-char (point-min))
759 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t)
760 (replace-match "\\1 " t))
761
762 ;; Loop over addresses until we have as many as we want.
763 (while (and (or all (null value-list))
764 (progn (goto-char (point-min))
765 (skip-chars-forward " \t")
766 (not (eobp))))
767 (let (char
768 end-of-address
769 <-pos >-pos @-pos colon-pos comma-pos !-pos %-pos \;-pos
770 group-:-pos group-\;-pos route-addr-:-pos
771 record-pos-symbol
772 first-real-pos last-real-pos
773 phrase-beg phrase-end
774 cbeg cend ; dynamically set from -voodoo
775 quote-beg quote-end
776 atom-beg atom-end
777 mbox-beg mbox-end
778 \.-ends-name
779 temp
780 ;; name-suffix
781 fi mi li ; first, middle, last initial
782 saved-%-pos saved-!-pos saved-@-pos
783 domain-pos \.-pos insert-point
784 ;; mailbox-name-processed-flag
785 disable-initial-guessing-flag) ; dynamically set from -voodoo
786
787 (set-syntax-table mail-extr-address-syntax-table)
788 (goto-char (point-min))
789
790 ;; Insert extra space at beginning to allow later replacement with <
791 ;; without having to move markers.
792 (or (eq (following-char) ?\ )
793 (insert ?\ ))
794
795 ;; First pass grabs useful information about address.
796 (while (progn
797 (mail-extr-skip-whitespace-forward)
798 (not (eobp)))
799 (setq char (char-after (point)))
800 (or first-real-pos
801 (if (not (eq char ?\())
802 (setq first-real-pos (point))))
803 (cond
804 ;; comment
805 ((eq char ?\()
806 (set-syntax-table mail-extr-address-comment-syntax-table)
807 ;; only record the first non-empty comment's position
808 (if (and (not cbeg)
809 (save-excursion
810 (forward-char 1)
811 (mail-extr-skip-whitespace-forward)
812 (not (eq ?\) (char-after (point))))))
813 (setq cbeg (point)))
814 ;; TODO: don't record if unbalanced
815 (or (mail-extr-safe-move-sexp 1)
816 (forward-char 1))
817 (set-syntax-table mail-extr-address-syntax-table)
818 (if (and cbeg
819 (not cend))
820 (setq cend (point))))
821 ;; quoted text
822 ((eq char ?\")
823 ;; only record the first non-empty quote's position
824 (if (and (not quote-beg)
825 (save-excursion
826 (forward-char 1)
827 (mail-extr-skip-whitespace-forward)
828 (not (eq ?\" (char-after (point))))))
829 (setq quote-beg (point)))
830 ;; TODO: don't record if unbalanced
831 (or (mail-extr-safe-move-sexp 1)
832 (forward-char 1))
833 (if (and quote-beg
834 (not quote-end))
835 (setq quote-end (point))))
836 ;; domain literals
837 ((eq char ?\[)
838 (set-syntax-table mail-extr-address-domain-literal-syntax-table)
839 (or (mail-extr-safe-move-sexp 1)
840 (forward-char 1))
841 (set-syntax-table mail-extr-address-syntax-table))
842 ;; commas delimit addresses when outside < > pairs.
843 ((and (eq char ?,)
844 (or (and (null <-pos)
845 ;; Handle ROUTE-ADDR address that is missing its <.
846 (not (eq ?@ (char-after (1+ (point))))))
847 (and >-pos
848 ;; handle weird munged addresses
849 ;; BUG FIX: This test was reversed. Thanks to the
850 ;; brilliant Rod Whitby <rwhitby@research.canon.oz.au>
851 ;; for discovering this!
852 (< (car (last <-pos)) (car >-pos)))))
853 ;; The argument contains more than one address.
854 ;; Temporarily hide everything after this one.
855 (setq end-of-address (copy-marker (1+ (point)) t))
856 (narrow-to-region (point-min) (1+ (point)))
857 (delete-char 1)
858 (setq char ?\() ; HAVE I NO SHAME??
859 )
860 ;; record the position of various interesting chars, determine
861 ;; legality later.
862 ((setq record-pos-symbol
863 (cdr (assq char
864 '((?< . <-pos) (?> . >-pos) (?@ . @-pos)
865 (?: . colon-pos) (?, . comma-pos) (?! . !-pos)
866 (?% . %-pos) (?\; . \;-pos)))))
867 (set record-pos-symbol
868 (cons (point) (symbol-value record-pos-symbol)))
869 (forward-char 1))
870 ((eq char ?.)
871 (forward-char 1))
872 ((memq char '(
873 ;; comment terminator illegal
874 ?\)
875 ;; domain literal terminator illegal
876 ?\]
877 ;; \ allowed only within quoted strings,
878 ;; domain literals, and comments
879 ?\\
880 ))
881 (mail-extr-nuke-char-at (point))
882 (forward-char 1))
883 (t
884 (forward-word 1)))
885 (or (eq char ?\()
886 ;; At the end of first address of a multiple address header.
887 (and (eq char ?,)
888 (eobp))
889 (setq last-real-pos (point))))
890
891 ;; Use only the leftmost <, if any. Replace all others with spaces.
892 (while (cdr <-pos)
893 (mail-extr-nuke-char-at (car <-pos))
894 (setq <-pos (cdr <-pos)))
895
896 ;; Use only the rightmost >, if any. Replace all others with spaces.
897 (while (cdr >-pos)
898 (mail-extr-nuke-char-at (nth 1 >-pos))
899 (setcdr >-pos (nthcdr 2 >-pos)))
900
901 ;; If multiple @s and a :, but no < and >, insert around buffer.
902 ;; Example: @foo.bar.dom,@xxx.yyy.zzz:mailbox@aaa.bbb.ccc
903 ;; This commonly happens on the UUCP "From " line. Ugh.
904 (when (and (> (length @-pos) 1)
905 (eq 1 (length colon-pos)) ;TODO: check if between last two @s
906 (not \;-pos)
907 (not <-pos))
908 (goto-char (point-min))
909 (delete-char 1)
910 (setq <-pos (list (point)))
911 (insert ?<))
912
913 ;; If < but no >, insert > in rightmost possible position
914 (when (and <-pos (null >-pos))
915 (goto-char (point-max))
916 (setq >-pos (list (point)))
917 (insert ?>))
918
919 ;; If > but no <, replace > with space.
920 (when (and >-pos (null <-pos))
921 (mail-extr-nuke-char-at (car >-pos))
922 (setq >-pos nil))
923
924 ;; Turn >-pos and <-pos into non-lists
925 (setq >-pos (car >-pos)
926 <-pos (car <-pos))
927
928 ;; Trim other punctuation lists of items outside < > pair to handle
929 ;; stupid MTAs.
930 (when <-pos ; don't need to check >-pos also
931 ;; handle bozo software that violates RFC 822 by sticking
932 ;; punctuation marks outside of a < > pair
933 (mail-extr-nuke-outside-range @-pos <-pos >-pos t)
934 ;; RFC 822 says nothing about these two outside < >, but
935 ;; remove those positions from the lists to make things
936 ;; easier.
937 (mail-extr-nuke-outside-range !-pos <-pos >-pos t)
938 (mail-extr-nuke-outside-range %-pos <-pos >-pos t))
939
940 ;; Check for : that indicates GROUP list and for : part of
941 ;; ROUTE-ADDR spec.
942 ;; Can't possibly be more than two :. Nuke any extra.
943 (while colon-pos
944 (setq temp (car colon-pos)
945 colon-pos (cdr colon-pos))
946 (cond ((and <-pos >-pos
947 (> temp <-pos)
948 (< temp >-pos))
949 (if (or route-addr-:-pos
950 (< (length @-pos) 2)
951 (> temp (car @-pos))
952 (< temp (nth 1 @-pos)))
953 (mail-extr-nuke-char-at temp)
954 (setq route-addr-:-pos temp)))
955 ((or (not <-pos)
956 (and <-pos
957 (< temp <-pos)))
958 (setq group-:-pos temp))))
959
960 ;; Nuke any ; that is in or to the left of a < > pair or to the left
961 ;; of a GROUP starting :. Also, there may only be one ;.
962 (while \;-pos
963 (setq temp (car \;-pos)
964 \;-pos (cdr \;-pos))
965 (cond ((and <-pos >-pos
966 (> temp <-pos)
967 (< temp >-pos))
968 (mail-extr-nuke-char-at temp))
969 ((and (or (not group-:-pos)
970 (> temp group-:-pos))
971 (not group-\;-pos))
972 (setq group-\;-pos temp))))
973
974 ;; Nuke unmatched GROUP syntax characters.
975 (when (and group-:-pos (not group-\;-pos))
976 ;; *** Do I really need to erase it?
977 (mail-extr-nuke-char-at group-:-pos)
978 (setq group-:-pos nil))
979 (when (and group-\;-pos (not group-:-pos))
980 ;; *** Do I really need to erase it?
981 (mail-extr-nuke-char-at group-\;-pos)
982 (setq group-\;-pos nil))
983
984 ;; Handle junk like ";@host.company.dom" that sendmail adds.
985 ;; **** should I remember comment positions?
986 (when group-\;-pos
987 ;; this is fine for now
988 (mail-extr-nuke-outside-range !-pos group-:-pos group-\;-pos t)
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 comma-pos group-:-pos group-\;-pos t)
992 (and last-real-pos
993 (> last-real-pos (1+ group-\;-pos))
994 (setq last-real-pos (1+ group-\;-pos)))
995 ;; *** This may be wrong:
996 (and cend
997 (> cend group-\;-pos)
998 (setq cend nil
999 cbeg nil))
1000 (and quote-end
1001 (> quote-end group-\;-pos)
1002 (setq quote-end nil
1003 quote-beg nil))
1004 ;; This was both wrong and unnecessary:
1005 ;;(narrow-to-region (point-min) group-\;-pos)
1006
1007 ;; *** The entire handling of GROUP addresses seems rather lame.
1008 ;; *** It deserves a complete rethink, except that these addresses
1009 ;; *** are hardly ever seen.
1010 )
1011
1012 ;; Any commas must be between < and : of ROUTE-ADDR. Nuke any
1013 ;; others.
1014 ;; Hell, go ahead an nuke all of the commas.
1015 ;; **** This will cause problems when we start handling commas in
1016 ;; the PHRASE part .... no it won't ... yes it will ... ?????
1017 (mail-extr-nuke-outside-range comma-pos 1 1)
1018
1019 ;; can only have multiple @s inside < >. The fact that some MTAs
1020 ;; put de-bracketed ROUTE-ADDRs in the UUCP-style "From " line is
1021 ;; handled above.
1022
1023 ;; Locate PHRASE part of ROUTE-ADDR.
1024 (when <-pos
1025 (goto-char <-pos)
1026 (mail-extr-skip-whitespace-backward)
1027 (setq phrase-end (point))
1028 (goto-char (or ;;group-:-pos
1029 (point-min)))
1030 (mail-extr-skip-whitespace-forward)
1031 (if (< (point) phrase-end)
1032 (setq phrase-beg (point))
1033 (setq phrase-end nil)))
1034
1035 ;; handle ROUTE-ADDRS with real ROUTEs.
1036 ;; If there are multiple @s, then we assume ROUTE-ADDR syntax, and
1037 ;; any % or ! must be semantically meaningless.
1038 ;; TODO: do this processing into canonicalization buffer
1039 (when route-addr-:-pos
1040 (setq !-pos nil
1041 %-pos nil
1042 >-pos (copy-marker >-pos)
1043 route-addr-:-pos (copy-marker route-addr-:-pos))
1044 (goto-char >-pos)
1045 (insert-before-markers ?X)
1046 (goto-char (car @-pos))
1047 (while (setq @-pos (cdr @-pos))
1048 (delete-char 1)
1049 (setq %-pos (cons (point-marker) %-pos))
1050 (insert "%")
1051 (goto-char (1- >-pos))
1052 (save-excursion
1053 (insert-buffer-substring extraction-buffer
1054 (car @-pos) route-addr-:-pos)
1055 (delete-region (car @-pos) route-addr-:-pos))
1056 (or (cdr @-pos)
1057 (setq saved-@-pos (list (point)))))
1058 (setq @-pos saved-@-pos)
1059 (goto-char >-pos)
1060 (delete-char -1)
1061 (mail-extr-nuke-char-at route-addr-:-pos)
1062 (mail-extr-demarkerize route-addr-:-pos)
1063 (setq route-addr-:-pos nil
1064 >-pos (mail-extr-demarkerize >-pos)
1065 %-pos (mapcar 'mail-extr-demarkerize %-pos)))
1066
1067 ;; de-listify @-pos
1068 (setq @-pos (car @-pos))
1069
1070 ;; TODO: remove comments in the middle of an address
1071
1072 (with-current-buffer canonicalization-buffer
1073 (widen)
1074 (erase-buffer)
1075 (insert-buffer-substring extraction-buffer)
1076
1077 (if <-pos
1078 (narrow-to-region (progn
1079 (goto-char (1+ <-pos))
1080 (mail-extr-skip-whitespace-forward)
1081 (point))
1082 >-pos)
1083 (if (and first-real-pos last-real-pos)
1084 (narrow-to-region first-real-pos last-real-pos)
1085 ;; ****** Oh no! What if the address is completely empty!
1086 ;; *** Is this correct?
1087 (narrow-to-region (point-max) (point-max))))
1088
1089 (and @-pos %-pos
1090 (mail-extr-nuke-outside-range %-pos (point-min) @-pos))
1091 (and %-pos !-pos
1092 (mail-extr-nuke-outside-range !-pos (point-min) (car %-pos)))
1093 (and @-pos !-pos (not %-pos)
1094 (mail-extr-nuke-outside-range !-pos (point-min) @-pos))
1095
1096 ;; Error condition:?? (and %-pos (not @-pos))
1097
1098 ;; WARNING: THIS CODE IS DUPLICATED BELOW.
1099 (when (and %-pos (not @-pos))
1100 (goto-char (car %-pos))
1101 (delete-char 1)
1102 (setq @-pos (point))
1103 (insert "@")
1104 (setq %-pos (cdr %-pos)))
1105
1106 (when (and mail-extr-mangle-uucp !-pos)
1107 ;; **** I don't understand this save-restriction and the
1108 ;; narrow-to-region inside it. Why did I do that?
1109 (save-restriction
1110 (cond ((and @-pos
1111 mail-extr-@-binds-tighter-than-!)
1112 (goto-char @-pos)
1113 (setq %-pos (cons (point) %-pos)
1114 @-pos nil)
1115 (delete-char 1)
1116 (insert "%")
1117 (setq insert-point (point-max)))
1118 (mail-extr-@-binds-tighter-than-!
1119 (setq insert-point (point-max)))
1120 (%-pos
1121 (setq insert-point (car (last %-pos))
1122 saved-%-pos (mapcar 'mail-extr-markerize %-pos)
1123 %-pos nil
1124 @-pos (mail-extr-markerize @-pos)))
1125 (@-pos
1126 (setq insert-point @-pos)
1127 (setq @-pos (mail-extr-markerize @-pos)))
1128 (t
1129 (setq insert-point (point-max))))
1130 (narrow-to-region (point-min) insert-point)
1131 (setq saved-!-pos (car !-pos))
1132 (while !-pos
1133 (goto-char (point-max))
1134 (cond ((and (not @-pos)
1135 (not (cdr !-pos)))
1136 (setq @-pos (point))
1137 (insert-before-markers "@ "))
1138 (t
1139 (setq %-pos (cons (point) %-pos))
1140 (insert-before-markers "% ")))
1141 (backward-char 1)
1142 (insert-buffer-substring
1143 (current-buffer)
1144 (if (nth 1 !-pos)
1145 (1+ (nth 1 !-pos))
1146 (point-min))
1147 (car !-pos))
1148 (delete-char 1)
1149 (or (save-excursion
1150 (mail-extr-safe-move-sexp -1)
1151 (mail-extr-skip-whitespace-backward)
1152 (eq ?. (preceding-char)))
1153 (insert-before-markers
1154 (if (save-excursion
1155 (mail-extr-skip-whitespace-backward)
1156 (eq ?. (preceding-char)))
1157 ""
1158 ".")
1159 "uucp"))
1160 (setq !-pos (cdr !-pos))))
1161 (and saved-%-pos
1162 (setq %-pos (append (mapcar 'mail-extr-demarkerize
1163 saved-%-pos)
1164 %-pos)))
1165 (setq @-pos (mail-extr-demarkerize @-pos))
1166 (narrow-to-region (1+ saved-!-pos) (point-max)))
1167
1168 ;; WARNING: THIS CODE IS DUPLICATED ABOVE.
1169 (when (and %-pos (not @-pos))
1170 (goto-char (car %-pos))
1171 (delete-char 1)
1172 (setq @-pos (point))
1173 (insert "@")
1174 (setq %-pos (cdr %-pos)))
1175
1176 (when (setq %-pos (nreverse %-pos)) ; implies @-pos valid
1177 (setq temp %-pos)
1178 (catch 'truncated
1179 (while temp
1180 (goto-char (or (nth 1 temp)
1181 @-pos))
1182 (mail-extr-skip-whitespace-backward)
1183 (save-excursion
1184 (mail-extr-safe-move-sexp -1)
1185 (setq domain-pos (point))
1186 (mail-extr-skip-whitespace-backward)
1187 (setq \.-pos (eq ?. (preceding-char))))
1188 (when (and \.-pos
1189 ;; #### string consing
1190 (let ((s (intern-soft
1191 (buffer-substring domain-pos (point))
1192 mail-extr-all-top-level-domains)))
1193 (and s (get s 'domain-name))))
1194 (narrow-to-region (point-min) (point))
1195 (goto-char (car temp))
1196 (delete-char 1)
1197 (setq @-pos (point))
1198 (setcdr temp nil)
1199 (setq %-pos (delq @-pos %-pos))
1200 (insert "@")
1201 (throw 'truncated t))
1202 (setq temp (cdr temp)))))
1203 (setq mbox-beg (point-min)
1204 mbox-end (if %-pos (car %-pos)
1205 (or @-pos
1206 (point-max))))
1207
1208 (when @-pos
1209 ;; Make the domain-name part lowercase since it's case
1210 ;; insensitive anyway.
1211 (downcase-region (1+ @-pos) (point-max))))
1212
1213 ;; Done canonicalizing address.
1214 ;; We are now back in extraction-buffer.
1215
1216 ;; Decide what part of the address to search to find the full name.
1217 (cond (
1218 ;; Example: "First M. Last" <fml@foo.bar.dom>
1219 (and phrase-beg
1220 (eq quote-beg phrase-beg)
1221 (<= quote-end phrase-end))
1222 (narrow-to-region (1+ quote-beg) (1- quote-end))
1223 (mail-extr-undo-backslash-quoting (point-min) (point-max)))
1224
1225 ;; Example: First Last <fml@foo.bar.dom>
1226 (phrase-beg
1227 (narrow-to-region phrase-beg phrase-end))
1228
1229 ;; Example: fml@foo.bar.dom (First M. Last)
1230 (cbeg
1231 (narrow-to-region (1+ cbeg) (1- cend))
1232 (mail-extr-undo-backslash-quoting (point-min) (point-max))
1233
1234 ;; Deal with spacing problems
1235 (goto-char (point-min))
1236 ;;; (cond ((not (search-forward " " nil t))
1237 ;;; (goto-char (point-min))
1238 ;;; (cond ((search-forward "_" nil t)
1239 ;;; ;; Handle the *idiotic* use of underlines as spaces.
1240 ;;; ;; Example: fml@foo.bar.dom (First_M._Last)
1241 ;;; (goto-char (point-min))
1242 ;;; (while (search-forward "_" nil t)
1243 ;;; (replace-match " " t)))
1244 ;;; ((search-forward "." nil t)
1245 ;;; ;; Fix . used as space
1246 ;;; ;; Example: danj1@cb.att.com (daniel.jacobson)
1247 ;;; (goto-char (point-min))
1248 ;;; (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1249 ;;; (replace-match "\\1 \\2" t))))))
1250 )
1251
1252 ;; Otherwise we try to get the name from the mailbox portion
1253 ;; of the address.
1254 ;; Example: First_M_Last@foo.bar.dom
1255 (t
1256 ;; *** Work in canon buffer instead? No, can't. Hmm.
1257 (goto-char (point-max))
1258 (narrow-to-region (point) (point))
1259 (insert-buffer-substring canonicalization-buffer
1260 mbox-beg mbox-end)
1261 (goto-char (point-min))
1262
1263 ;; Example: First_Last.XXX@foo.bar.dom
1264 (setq \.-ends-name (re-search-forward "[_0-9]" nil t))
1265
1266 (goto-char (point-min))
1267
1268 (if (not mail-extr-mangle-uucp)
1269 (modify-syntax-entry ?! "w" (syntax-table)))
1270
1271 (while (progn
1272 (mail-extr-skip-whitespace-forward)
1273 (not (eobp)))
1274 (setq char (char-after (point)))
1275 (cond
1276 ((eq char ?\")
1277 (setq quote-beg (point))
1278 (or (mail-extr-safe-move-sexp 1)
1279 ;; TODO: handle this error condition!!!!!
1280 (forward-char 1))
1281 ;; take into account deletions
1282 (setq quote-end (- (point) 2))
1283 (save-excursion
1284 (backward-char 1)
1285 (delete-char 1)
1286 (goto-char quote-beg)
1287 (or (eobp)
1288 (delete-char 1)))
1289 (mail-extr-undo-backslash-quoting quote-beg quote-end)
1290 (or (eq ?\ (char-after (point)))
1291 (insert " "))
1292 ;; (setq mailbox-name-processed-flag t)
1293 (setq \.-ends-name t))
1294 ((eq char ?.)
1295 (if (memq (char-after (1+ (point))) '(?_ ?=))
1296 (progn
1297 (forward-char 1)
1298 (delete-char 1)
1299 (insert ?\ ))
1300 (if \.-ends-name
1301 (narrow-to-region (point-min) (point))
1302 (delete-char 1)
1303 (insert " ")))
1304 ;; (setq mailbox-name-processed-flag t)
1305 )
1306 ((memq (char-syntax char) '(?. ?\\))
1307 (delete-char 1)
1308 (insert " ")
1309 ;; (setq mailbox-name-processed-flag t)
1310 )
1311 (t
1312 (setq atom-beg (point))
1313 (forward-word 1)
1314 (setq atom-end (point))
1315 (goto-char atom-beg)
1316 (save-restriction
1317 (narrow-to-region atom-beg atom-end)
1318 (cond
1319
1320 ;; Handle X.400 addresses encoded in RFC-822.
1321 ;; *** Shit! This has to handle the case where it is
1322 ;; *** embedded in a quote too!
1323 ;; *** Shit! The input is being broken up into atoms
1324 ;; *** by periods!
1325 ((looking-at mail-extr-x400-encoded-address-pattern)
1326
1327 ;; Copy the contents of the individual fields that
1328 ;; might hold name data to the beginning.
1329 (mapc
1330 (lambda (field-pattern)
1331 (when
1332 (save-excursion
1333 (re-search-forward field-pattern nil t))
1334 (insert-buffer-substring (current-buffer)
1335 (match-beginning 1)
1336 (match-end 1))
1337 (insert " ")))
1338 (list mail-extr-x400-encoded-address-given-name-pattern
1339 mail-extr-x400-encoded-address-surname-pattern
1340 mail-extr-x400-encoded-address-full-name-pattern))
1341
1342 ;; Discard the rest, since it contains stuff like
1343 ;; routing information, not part of a name.
1344 (mail-extr-skip-whitespace-backward)
1345 (delete-region (point) (point-max))
1346
1347 ;; Handle periods used for spacing.
1348 (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1349 (replace-match "\\1 \\2" t))
1350
1351 ;; (setq mailbox-name-processed-flag t)
1352 )
1353
1354 ;; Handle normal addresses.
1355 (t
1356 (goto-char (point-min))
1357 ;; Handle _ and = used for spacing.
1358 (while (re-search-forward "\\([^_=]+\\)[_=]" nil t)
1359 (replace-match "\\1 " t)
1360 ;; (setq mailbox-name-processed-flag t)
1361 )
1362 (goto-char (point-max))))))))
1363
1364 ;; undo the dirty deed
1365 (if (not mail-extr-mangle-uucp)
1366 (modify-syntax-entry ?! "." (syntax-table)))
1367 ;;
1368 ;; If we derived the name from the mailbox part of the address,
1369 ;; and we only got one word out of it, don't treat that as a
1370 ;; name. "foo@bar" --> (nil "foo@bar"), not ("foo" "foo@bar")
1371 ;; (if (not mailbox-name-processed-flag)
1372 ;; (delete-region (point-min) (point-max)))
1373 ))
1374
1375 (set-syntax-table mail-extr-address-text-syntax-table)
1376
1377 (mail-extr-voodoo mbox-beg mbox-end canonicalization-buffer)
1378 (goto-char (point-min))
1379
1380 ;; If name is "First Last" and userid is "F?L", then assume
1381 ;; the middle initial is the second letter in the userid.
1382 ;; Initial code by Jamie Zawinski <jwz@lucid.com>
1383 ;; *** Make it work when there's a suffix as well.
1384 (goto-char (point-min))
1385 (when (and mail-extr-guess-middle-initial
1386 (not disable-initial-guessing-flag)
1387 (eq 3 (- mbox-end mbox-beg))
1388 (progn
1389 (goto-char (point-min))
1390 (looking-at mail-extr-two-name-pattern)))
1391 (setq fi (char-after (match-beginning 0))
1392 li (char-after (match-beginning 3)))
1393 (with-current-buffer canonicalization-buffer
1394 ;; char-equal is ignoring case here, so no need to upcase
1395 ;; or downcase.
1396 (let ((case-fold-search t))
1397 (and (char-equal fi (char-after mbox-beg))
1398 (char-equal li (char-after (1- mbox-end)))
1399 (setq mi (char-after (1+ mbox-beg))))))
1400 (when (and mi
1401 ;; TODO: use better table than syntax table
1402 (eq ?w (char-syntax mi)))
1403 (goto-char (match-beginning 3))
1404 (insert (upcase mi) ". ")))
1405
1406 ;; Nuke name if it is the same as mailbox name.
1407 (let ((buffer-length (- (point-max) (point-min)))
1408 (i 0)
1409 (names-match-flag t))
1410 (when (and (> buffer-length 0)
1411 (eq buffer-length (- mbox-end mbox-beg)))
1412 (goto-char (point-max))
1413 (insert-buffer-substring canonicalization-buffer
1414 mbox-beg mbox-end)
1415 (while (and names-match-flag
1416 (< i buffer-length))
1417 (or (eq (downcase (char-after (+ i (point-min))))
1418 (downcase
1419 (char-after (+ i buffer-length (point-min)))))
1420 (setq names-match-flag nil))
1421 (setq i (1+ i)))
1422 (delete-region (+ (point-min) buffer-length) (point-max))
1423 (if names-match-flag
1424 (narrow-to-region (point) (point)))))
1425
1426 ;; Nuke name if it's just one word.
1427 (goto-char (point-min))
1428 (and mail-extr-ignore-single-names
1429 (not (re-search-forward "[- ]" nil t))
1430 (narrow-to-region (point) (point)))
1431
1432 ;; Record the result
1433 (setq value-list
1434 (cons (list (if (not (= (point-min) (point-max)))
1435 (buffer-string))
1436 (with-current-buffer canonicalization-buffer
1437 (if (not (= (point-min) (point-max)))
1438 (buffer-string))))
1439 value-list))
1440
1441 ;; Unless one address is all we wanted,
1442 ;; delete this one from extraction-buffer
1443 ;; and get ready to extract the next address.
1444 (when all
1445 (if end-of-address
1446 (narrow-to-region 1 end-of-address)
1447 (widen))
1448 (delete-region (point-min) (point-max))
1449 (widen))
1450 )))
1451 (if all (nreverse value-list) (car value-list))
1452 ))
1453
1454 (defun mail-extr-voodoo (mbox-beg mbox-end canonicalization-buffer)
1455 (let ((word-count 0)
1456 (case-fold-search nil)
1457 mixed-case-flag lower-case-flag ;;upper-case-flag
1458 suffix-flag last-name-comma-flag
1459 ;;cbeg cend
1460 initial
1461 begin-again-flag
1462 drop-this-word-if-trailing-flag
1463 drop-last-word-if-trailing-flag
1464 word-found-flag
1465 this-word-beg last-word-beg
1466 name-beg name-end
1467 name-done-flag
1468 )
1469 (save-excursion
1470 (set-syntax-table mail-extr-address-text-syntax-table)
1471
1472 ;; Get rid of comments.
1473 (goto-char (point-min))
1474 (while (not (eobp))
1475 ;; Initialize for this iteration of the loop.
1476 (skip-chars-forward "^({[\"'`")
1477 (let ((cbeg (point)))
1478 (set-syntax-table mail-extr-address-text-comment-syntax-table)
1479 (if (memq (following-char) '(?\' ?\`))
1480 (search-forward "'" nil 'move
1481 (if (eq ?\' (following-char)) 2 1))
1482 (or (mail-extr-safe-move-sexp 1)
1483 (goto-char (point-max))))
1484 (set-syntax-table mail-extr-address-text-syntax-table)
1485 (when (eq (char-after cbeg) ?\()
1486 ;; Delete the comment itself.
1487 (delete-region cbeg (point))
1488 ;; Canonicalize whitespace where the comment was.
1489 (skip-chars-backward " \t")
1490 (if (looking-at "\\([ \t]+$\\|[ \t]+,\\)")
1491 (replace-match "")
1492 (setq cbeg (point))
1493 (skip-chars-forward " \t")
1494 (if (bobp)
1495 (delete-region (point) cbeg)
1496 (just-one-space))))))
1497
1498 ;; This was moved above.
1499 ;; Fix . used as space
1500 ;; But it belongs here because it occurs not only as
1501 ;; rypens@reks.uia.ac.be (Piet.Rypens)
1502 ;; but also as
1503 ;; "Piet.Rypens" <rypens@reks.uia.ac.be>
1504 ;;(goto-char (point-min))
1505 ;;(while (re-search-forward mail-extr-bad-dot-pattern nil t)
1506 ;; (replace-match "\\1 \\2" t))
1507
1508 (unless (search-forward " " nil t)
1509 (goto-char (point-min))
1510 (cond ((search-forward "_" nil t)
1511 ;; Handle the *idiotic* use of underlines as spaces.
1512 ;; Example: fml@foo.bar.dom (First_M._Last)
1513 (goto-char (point-min))
1514 (while (search-forward "_" nil t)
1515 (replace-match " " t)))
1516 ((search-forward "." nil t)
1517 ;; Fix . used as space
1518 ;; Example: danj1@cb.att.com (daniel.jacobson)
1519 (goto-char (point-min))
1520 (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1521 (replace-match "\\1 \\2" t)))))
1522
1523 ;; Loop over the words (and other junk) in the name.
1524 (goto-char (point-min))
1525 (while (not name-done-flag)
1526
1527 (when word-found-flag
1528 ;; Last time through this loop we skipped over a word.
1529 (setq last-word-beg this-word-beg)
1530 (setq drop-last-word-if-trailing-flag
1531 drop-this-word-if-trailing-flag)
1532 (setq word-found-flag nil))
1533
1534 (when begin-again-flag
1535 ;; Last time through the loop we found something that
1536 ;; indicates we should pretend we are beginning again from
1537 ;; the start.
1538 (setq word-count 0)
1539 (setq last-word-beg nil)
1540 (setq drop-last-word-if-trailing-flag nil)
1541 (setq mixed-case-flag nil)
1542 (setq lower-case-flag nil)
1543 ;; (setq upper-case-flag nil)
1544 (setq begin-again-flag nil))
1545
1546 ;; Initialize for this iteration of the loop.
1547 (mail-extr-skip-whitespace-forward)
1548 (if (eq word-count 0) (narrow-to-region (point) (point-max)))
1549 (setq this-word-beg (point))
1550 (setq drop-this-word-if-trailing-flag nil)
1551
1552 ;; Decide what to do based on what we are looking at.
1553 (cond
1554
1555 ;; Delete title
1556 ((and (eq word-count 0)
1557 (looking-at mail-extr-full-name-prefixes))
1558 (goto-char (match-end 0))
1559 (narrow-to-region (point) (point-max)))
1560
1561 ;; Stop after name suffix
1562 ((and (>= word-count 2)
1563 (looking-at mail-extr-full-name-suffix-pattern))
1564 (mail-extr-skip-whitespace-backward)
1565 (setq suffix-flag (point))
1566 (if (eq ?, (following-char))
1567 (forward-char 1)
1568 (insert ?,))
1569 ;; Enforce at least one space after comma
1570 (or (eq ?\ (following-char))
1571 (insert ?\ ))
1572 (mail-extr-skip-whitespace-forward)
1573 (cond ((memq (following-char) '(?j ?J ?s ?S))
1574 (capitalize-word 1)
1575 (if (eq (following-char) ?.)
1576 (forward-char 1)
1577 (insert ?.)))
1578 (t
1579 (upcase-word 1)))
1580 (setq word-found-flag t)
1581 (setq name-done-flag t))
1582
1583 ;; Handle SCA names
1584 ((looking-at "MKA \\(.+\\)") ; "Mundanely Known As"
1585 (goto-char (match-beginning 1))
1586 (narrow-to-region (point) (point-max))
1587 (setq begin-again-flag t))
1588
1589 ;; Check for initial last name followed by comma
1590 ((and (eq ?, (following-char))
1591 (eq word-count 1))
1592 (forward-char 1)
1593 (setq last-name-comma-flag t)
1594 (or (eq ?\ (following-char))
1595 (insert ?\ )))
1596
1597 ;; Stop before trailing comma-separated comment
1598 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1599 ;; *** This case is redundant???
1600 ;;((eq ?, (following-char))
1601 ;; (setq name-done-flag t))
1602
1603 ;; Delete parenthesized/quoted comment/nickname
1604 ((memq (following-char) '(?\( ?\{ ?\[ ?\" ?\' ?\`))
1605 (setq cbeg (point))
1606 (set-syntax-table mail-extr-address-text-comment-syntax-table)
1607 (cond ((memq (following-char) '(?\' ?\`))
1608 (or (search-forward "'" nil t
1609 (if (eq ?\' (following-char)) 2 1))
1610 (delete-char 1)))
1611 (t
1612 (or (mail-extr-safe-move-sexp 1)
1613 (goto-char (point-max)))))
1614 (set-syntax-table mail-extr-address-text-syntax-table)
1615 (setq cend (point))
1616 (cond
1617 ;; Handle case of entire name being quoted
1618 ((and (eq word-count 0)
1619 (looking-at " *\\'")
1620 (>= (- cend cbeg) 2))
1621 (narrow-to-region (1+ cbeg) (1- cend))
1622 (goto-char (point-min)))
1623 (t
1624 ;; Handle case of quoted initial
1625 (if (and (or (= 3 (- cend cbeg))
1626 (and (= 4 (- cend cbeg))
1627 (eq ?. (char-after (+ 2 cbeg)))))
1628 (not (looking-at " *\\'")))
1629 (setq initial (char-after (1+ cbeg)))
1630 (setq initial nil))
1631 (delete-region cbeg cend)
1632 (if initial
1633 (insert initial ". ")))))
1634
1635 ;; Handle *Stupid* VMS date stamps
1636 ((looking-at mail-extr-stupid-vms-date-stamp-pattern)
1637 (replace-match "" t))
1638
1639 ;; Handle Chinese characters.
1640 ((looking-at mail-extr-hz-embedded-gb-encoded-chinese-pattern)
1641 (goto-char (match-end 0))
1642 (setq word-found-flag t))
1643
1644 ;; Skip initial garbage characters.
1645 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1646 ((and (eq word-count 0)
1647 (looking-at mail-extr-leading-garbage))
1648 (goto-char (match-end 0))
1649 ;; *** Skip backward over these???
1650 ;; (skip-chars-backward "& \"")
1651 (narrow-to-region (point) (point-max)))
1652
1653 ;; Various stopping points
1654 ((or
1655
1656 ;; Stop before ALL CAPS acronyms, if preceded by mixed-case
1657 ;; words. Example: XT-DEM.
1658 (and (>= word-count 2)
1659 mixed-case-flag
1660 (looking-at mail-extr-weird-acronym-pattern)
1661 (not (looking-at mail-extr-roman-numeral-pattern)))
1662
1663 ;; Stop before trailing alternative address
1664 (looking-at mail-extr-alternative-address-pattern)
1665
1666 ;; Stop before trailing comment not introduced by comma
1667 ;; THIS CASE MUST BE AFTER AN EARLIER CASE.
1668 (looking-at mail-extr-trailing-comment-start-pattern)
1669
1670 ;; Stop before telephone numbers
1671 (and (>= word-count 1)
1672 (looking-at mail-extr-telephone-extension-pattern)))
1673 (setq name-done-flag t))
1674
1675 ;; Delete ham radio call signs
1676 ((looking-at mail-extr-ham-call-sign-pattern)
1677 (delete-region (match-beginning 0) (match-end 0)))
1678
1679 ;; Fixup initials
1680 ((looking-at mail-extr-initial-pattern)
1681 (or (eq (following-char) (upcase (following-char)))
1682 (setq lower-case-flag t))
1683 (forward-char 1)
1684 (if (eq ?. (following-char))
1685 (forward-char 1)
1686 (insert ?.))
1687 (or (eq ?\ (following-char))
1688 (insert ?\ ))
1689 (setq word-found-flag t))
1690
1691 ;; Handle BITNET LISTSERV list names.
1692 ((and (eq word-count 0)
1693 (looking-at mail-extr-listserv-list-name-pattern))
1694 (narrow-to-region (match-beginning 1) (match-end 1))
1695 (setq word-found-flag t)
1696 (setq name-done-flag t))
1697
1698 ;; Handle & substitution, when & is last and is not first.
1699 ((and (> word-count 0)
1700 (eq ?\ (preceding-char))
1701 (eq (following-char) ?&)
1702 (eq (1+ (point)) (point-max)))
1703 (delete-char 1)
1704 (capitalize-region
1705 (point)
1706 (progn
1707 (insert-buffer-substring canonicalization-buffer
1708 mbox-beg mbox-end)
1709 (point)))
1710 (setq disable-initial-guessing-flag t)
1711 (setq word-found-flag t))
1712
1713 ;; Handle & between names, as in "Bob & Susie".
1714 ((and (> word-count 0) (eq (following-char) ?\&))
1715 (setq name-beg (point))
1716 (setq name-end (1+ name-beg))
1717 (setq word-found-flag t)
1718 (goto-char name-end))
1719
1720 ;; Regular name words
1721 ((looking-at mail-extr-name-pattern)
1722 (setq name-beg (point))
1723 (setq name-end (match-end 0))
1724
1725 ;; Certain words will be dropped if they are at the end.
1726 (and (>= word-count 2)
1727 (not lower-case-flag)
1728 (or
1729 ;; A trailing 4-or-more letter lowercase words preceded by
1730 ;; mixed case or uppercase words will be dropped.
1731 (looking-at "[a-z][a-z][a-z][a-z]+[ \t]*\\'")
1732 ;; Drop a trailing word which is terminated with a period.
1733 (eq ?. (char-after (1- name-end))))
1734 (setq drop-this-word-if-trailing-flag t))
1735
1736 ;; Set the flags that indicate whether we have seen a lowercase
1737 ;; word, a mixed case word, and an uppercase word.
1738 (if (re-search-forward "[a-z]" name-end t)
1739 (if (progn
1740 (goto-char name-beg)
1741 (re-search-forward "[A-Z]" name-end t))
1742 (setq mixed-case-flag t)
1743 (setq lower-case-flag t))
1744 ;; (setq upper-case-flag t)
1745 )
1746
1747 (goto-char name-end)
1748 (setq word-found-flag t))
1749
1750 ;; Allow a number as a word, if it doesn't mean anything else.
1751 ((looking-at "[0-9]+\\>")
1752 (setq name-beg (point))
1753 (setq name-end (match-end 0))
1754 (goto-char name-end)
1755 (setq word-found-flag t))
1756
1757 (t
1758 (setq name-done-flag t)
1759 ))
1760
1761 ;; Count any word that we skipped over.
1762 (if word-found-flag
1763 (setq word-count (1+ word-count))))
1764
1765 ;; If the last thing in the name is 2 or more periods, or one or more
1766 ;; other sentence terminators (but not a single period) then keep them
1767 ;; and the preceding word. This is for the benefit of whole sentences
1768 ;; in the name field: it's better behavior than dropping the last word
1769 ;; of the sentence...
1770 (if (and (not suffix-flag)
1771 (looking-at "\\(\\.+\\|[?!;:.][?!;:.]+\\|[?!;:][?!;:.]*\\)\\'"))
1772 (goto-char (setq suffix-flag (point-max))))
1773
1774 ;; Drop everything after point and certain trailing words.
1775 (narrow-to-region (point-min)
1776 (or (and drop-last-word-if-trailing-flag
1777 last-word-beg)
1778 (point)))
1779
1780 ;; Xerox's mailers SUCK!!!!!!
1781 ;; We simply refuse to believe that any last name is PARC or ADOC.
1782 ;; If it looks like that is the last name, that there is no meaningful
1783 ;; here at all. Actually I guess it would be best to map patterns
1784 ;; like foo.hoser@xerox.com into foo@hoser.xerox.com, but I don't
1785 ;; actually know that that is what's going on.
1786 (unless suffix-flag
1787 (goto-char (point-min))
1788 (let ((case-fold-search t))
1789 (if (looking-at "[-A-Za-z_]+[. ]\\(PARC\\|ADOC\\)\\'")
1790 (erase-buffer))))
1791
1792 ;; If last name first put it at end (but before suffix)
1793 (when last-name-comma-flag
1794 (goto-char (point-min))
1795 (search-forward ",")
1796 (setq name-end (1- (point)))
1797 (goto-char (or suffix-flag (point-max)))
1798 (or (eq ?\ (preceding-char))
1799 (insert ?\ ))
1800 (insert-buffer-substring (current-buffer) (point-min) name-end)
1801 (goto-char name-end)
1802 (skip-chars-forward "\t ,")
1803 (narrow-to-region (point) (point-max)))
1804
1805 ;; Delete leading and trailing junk characters.
1806 ;; *** This is probably completely unneeded now.
1807 ;;(goto-char (point-max))
1808 ;;(skip-chars-backward mail-extr-non-end-name-chars)
1809 ;;(if (eq ?. (following-char))
1810 ;; (forward-char 1))
1811 ;;(narrow-to-region (point)
1812 ;; (progn
1813 ;; (goto-char (point-min))
1814 ;; (skip-chars-forward mail-extr-non-begin-name-chars)
1815 ;; (point)))
1816
1817 ;; Compress whitespace
1818 (goto-char (point-min))
1819 (while (re-search-forward "[ \t\n]+" nil t)
1820 (replace-match (if (eobp) "" " ") t))
1821 )))
1822
1823 \f
1824
1825 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1826 ;;
1827 ;; Table of top-level domain names.
1828 ;;
1829 ;; This is used during address canonicalization; be careful of format changes.
1830 ;; Keep in mind that the country abbreviations follow ISO-3166. There is
1831 ;; a U.S. FIPS that specifies a different set of two-letter country
1832 ;; abbreviations.
1833
1834 (defconst mail-extr-all-top-level-domains
1835 (let ((ob (make-vector 739 0)))
1836 (mapc
1837 (lambda (x)
1838 (put (intern (downcase (car x)) ob)
1839 'domain-name
1840 (if (nth 2 x)
1841 (format (nth 2 x) (nth 1 x))
1842 (nth 1 x))))
1843 '(
1844 ;; ISO 3166 codes:
1845 ("ad" "Andorra")
1846 ("ae" "United Arab Emirates")
1847 ("af" "Afghanistan")
1848 ("ag" "Antigua and Barbuda")
1849 ("ai" "Anguilla")
1850 ("al" "Albania")
1851 ("am" "Armenia")
1852 ("an" "Netherlands Antilles")
1853 ("ao" "Angola")
1854 ("aq" "Antarctica") ; continent
1855 ("ar" "Argentina" "Argentine Republic")
1856 ("as" "American Samoa")
1857 ("at" "Austria" "The Republic of %s")
1858 ("au" "Australia")
1859 ("aw" "Aruba")
1860 ("az" "Azerbaijan")
1861 ("ba" "Bosnia-Herzegovina")
1862 ("bb" "Barbados")
1863 ("bd" "Bangladesh")
1864 ("be" "Belgium" "The Kingdom of %s")
1865 ("bf" "Burkina Faso")
1866 ("bg" "Bulgaria")
1867 ("bh" "Bahrain")
1868 ("bi" "Burundi")
1869 ("bj" "Benin")
1870 ("bm" "Bermuda")
1871 ("bn" "Brunei Darussalam")
1872 ("bo" "Bolivia" "Republic of %s")
1873 ("br" "Brazil" "The Federative Republic of %s")
1874 ("bs" "Bahamas")
1875 ("bt" "Bhutan")
1876 ("bv" "Bouvet Island")
1877 ("bw" "Botswana")
1878 ("by" "Belarus")
1879 ("bz" "Belize")
1880 ("ca" "Canada")
1881 ("cc" "Cocos (Keeling) Islands")
1882 ("cd" "The Democratic Republic of The Congo")
1883 ("cf" "Central African Republic")
1884 ("cg" "Congo")
1885 ("ch" "Switzerland" "The Swiss Confederation")
1886 ("ci" "Ivory Coast") ; Cote D'ivoire
1887 ("ck" "Cook Islands")
1888 ("cl" "Chile" "The Republic of %s")
1889 ("cm" "Cameroon") ; In .fr domain
1890 ("cn" "China" "The People's Republic of %s")
1891 ("co" "Colombia")
1892 ("cr" "Costa Rica" "The Republic of %s")
1893 ("cu" "Cuba")
1894 ("cv" "Cape Verde")
1895 ("cx" "Christmas Island")
1896 ("cy" "Cyprus")
1897 ("cz" "Czech Republic")
1898 ("de" "Germany")
1899 ("dj" "Djibouti")
1900 ("dk" "Denmark")
1901 ("dm" "Dominica")
1902 ("do" "Dominican Republic" "The %s")
1903 ("dz" "Algeria")
1904 ("ec" "Ecuador" "The Republic of %s")
1905 ("ee" "Estonia")
1906 ("eg" "Egypt" "The Arab Republic of %s")
1907 ("eh" "Western Sahara")
1908 ("er" "Eritrea")
1909 ("es" "Spain" "The Kingdom of %s")
1910 ("et" "Ethiopia")
1911 ("fi" "Finland" "The Republic of %s")
1912 ("fj" "Fiji")
1913 ("fk" "Falkland Islands (Malvinas)")
1914 ("fm" "Micronesia" "Federated States of %s")
1915 ("fo" "Faroe Islands")
1916 ("fr" "France")
1917 ("ga" "Gabon")
1918 ("gb" "United Kingdom")
1919 ("gd" "Grenada")
1920 ("ge" "Georgia")
1921 ("gf" "French Guiana")
1922 ("gh" "Ghana")
1923 ("gi" "Gibraltar")
1924 ("gl" "Greenland")
1925 ("gm" "Gambia")
1926 ("gn" "Guinea")
1927 ("gp" "Guadeloupe (Fr.)")
1928 ("gq" "Equatorial Guinea")
1929 ("gr" "Greece" "The Hellenic Republic (%s)")
1930 ("gs" "South Georgia And The South Sandwich Islands")
1931 ("gt" "Guatemala")
1932 ("gu" "Guam (U.S.)")
1933 ("gw" "Guinea-Bissau")
1934 ("gy" "Guyana")
1935 ("hk" "Hong Kong")
1936 ("hm" "Heard Island And Mcdonald Islands")
1937 ("hn" "Honduras")
1938 ("hr" "Croatia" "Croatia (Hrvatska)")
1939 ("ht" "Haiti")
1940 ("hu" "Hungary" "The Hungarian Republic")
1941 ("id" "Indonesia")
1942 ("ie" "Ireland")
1943 ("il" "Israel" "The State of %s")
1944 ("im" "Isle of Man" "The %s") ; NOT in ISO 3166-1 of 2001-02-26
1945 ("in" "India" "The Republic of %s")
1946 ("io" "British Indian Ocean Territory")
1947 ("iq" "Iraq")
1948 ("ir" "Iran" "Islamic Republic of %s")
1949 ("is" "Iceland" "The Republic of %s")
1950 ("it" "Italy" "The Italian Republic")
1951 ("jm" "Jamaica")
1952 ("jo" "Jordan")
1953 ("jp" "Japan")
1954 ("ke" "Kenya")
1955 ("kg" "Kyrgyzstan")
1956 ("kh" "Cambodia")
1957 ("ki" "Kiribati")
1958 ("km" "Comoros")
1959 ("kn" "Saint Kitts and Nevis")
1960 ("kp" "Korea (North)" "Democratic People's Republic of Korea")
1961 ("kr" "Korea (South)" "Republic of Korea")
1962 ("kw" "Kuwait")
1963 ("ky" "Cayman Islands")
1964 ("kz" "Kazakstan")
1965 ("la" "Lao People's Democratic Republic")
1966 ("lb" "Lebanon")
1967 ("lc" "Saint Lucia")
1968 ("li" "Liechtenstein")
1969 ("lk" "Sri Lanka" "The Democratic Socialist Republic of %s")
1970 ("lr" "Liberia")
1971 ("ls" "Lesotho")
1972 ("lt" "Lithuania")
1973 ("lu" "Luxembourg")
1974 ("lv" "Latvia")
1975 ("ly" "Libyan Arab Jamahiriya")
1976 ("ma" "Morocco")
1977 ("mc" "Monaco")
1978 ("md" "Moldova" "The Republic of %s")
1979 ("mg" "Madagascar")
1980 ("mh" "Marshall Islands")
1981 ("mk" "Macedonia" "The Former Yugoslav Republic of %s")
1982 ("ml" "Mali")
1983 ("mm" "Myanmar")
1984 ("mn" "Mongolia")
1985 ("mo" "Macau")
1986 ("mp" "Northern Mariana Islands")
1987 ("mq" "Martinique")
1988 ("mr" "Mauritania")
1989 ("ms" "Montserrat")
1990 ("mt" "Malta")
1991 ("mu" "Mauritius")
1992 ("mv" "Maldives")
1993 ("mw" "Malawi")
1994 ("mx" "Mexico" "The United Mexican States")
1995 ("my" "Malaysia" "%s (changed to Myanmar?)") ;???
1996 ("mz" "Mozambique")
1997 ("na" "Namibia")
1998 ("nc" "New Caledonia (Fr.)")
1999 ("ne" "Niger") ; In .fr domain
2000 ("nf" "Norfolk Island")
2001 ("ng" "Nigeria")
2002 ("ni" "Nicaragua" "The Republic of %s")
2003 ("nl" "Netherlands" "The Kingdom of the %s")
2004 ("no" "Norway" "The Kingdom of %s")
2005 ("np" "Nepal") ; Via .in domain
2006 ("nr" "Nauru")
2007 ("nu" "Niue")
2008 ("nz" "New Zealand")
2009 ("om" "Oman")
2010 ("pa" "Panama")
2011 ("pe" "Peru")
2012 ("pf" "Polynesia (Fr.)")
2013 ("pg" "Papua New Guinea")
2014 ("ph" "Philippines" "The Republic of the %s")
2015 ("pk" "Pakistan")
2016 ("pl" "Poland")
2017 ("pm" "Saint Pierre and Miquelon")
2018 ("pn" "Pitcairn")
2019 ("pr" "Puerto Rico (U.S.)")
2020 ("ps" "Palestinian Territory, Occupied")
2021 ("pt" "Portugal" "The Portuguese Republic")
2022 ("pw" "Palau")
2023 ("py" "Paraguay")
2024 ("qa" "Qatar")
2025 ("re" "Reunion (Fr.)") ; In .fr domain
2026 ("ro" "Romania")
2027 ("ru" "Russia" "Russian Federation")
2028 ("rw" "Rwanda")
2029 ("sa" "Saudi Arabia")
2030 ("sb" "Solomon Islands")
2031 ("sc" "Seychelles")
2032 ("sd" "Sudan")
2033 ("se" "Sweden" "The Kingdom of %s")
2034 ("sg" "Singapore" "The Republic of %s")
2035 ("sh" "Saint Helena")
2036 ("si" "Slovenia")
2037 ("sj" "Svalbard and Jan Mayen") ; In .no domain
2038 ("sk" "Slovakia" "The Slovak Republic")
2039 ("sl" "Sierra Leone")
2040 ("sm" "San Marino")
2041 ("sn" "Senegal")
2042 ("so" "Somalia")
2043 ("sr" "Suriname")
2044 ("st" "Sao Tome And Principe")
2045 ("su" "U.S.S.R." "The Union of Soviet Socialist Republics")
2046 ("sv" "El Salvador")
2047 ("sy" "Syrian Arab Republic")
2048 ("sz" "Swaziland")
2049 ("tc" "Turks And Caicos Islands")
2050 ("td" "Chad")
2051 ("tf" "French Southern Territories")
2052 ("tg" "Togo")
2053 ("th" "Thailand" "The Kingdom of %s")
2054 ("tj" "Tajikistan")
2055 ("tk" "Tokelau")
2056 ("tm" "Turkmenistan")
2057 ("tn" "Tunisia")
2058 ("to" "Tonga")
2059 ("tp" "East Timor")
2060 ("tr" "Turkey" "The Republic of %s")
2061 ("tt" "Trinidad and Tobago")
2062 ("tv" "Tuvalu")
2063 ("tw" "Taiwan" "%s, Province of China")
2064 ("tz" "Tanzania" "United Republic of %s")
2065 ("ua" "Ukraine")
2066 ("ug" "Uganda")
2067 ("uk" "United Kingdom" "The %s of Great Britain and Northern Ireland")
2068 ("um" "United States Minor Outlying Islands")
2069 ("us" "United States" "The %s of America")
2070 ("uy" "Uruguay" "The Eastern Republic of %s")
2071 ("uz" "Uzbekistan")
2072 ("va" "Holy See (Vatican City State)")
2073 ("vc" "St. Vincent and the Grenadines")
2074 ("ve" "Venezuela" "The Republic of %s")
2075 ("vg" "Virgin Islands, British")
2076 ("vi" "Virgin Islands, U.S.")
2077 ("vn" "Vietnam")
2078 ("vu" "Vanuatu")
2079 ("wf" "Wallis and Futuna")
2080 ("ws" "Samoa")
2081 ("ye" "Yemen")
2082 ("yt" "Mayotte")
2083 ("yu" "Yugoslavia" "Yugoslavia, AKA Serbia-Montenegro")
2084 ("za" "South Africa" "The Republic of %s")
2085 ("zm" "Zambia")
2086 ("zw" "Zimbabwe" "Republic of %s")
2087 ;; Special top-level domains:
2088 ("arpa" t "Advanced Research Projects Agency (U.S. DoD)")
2089 ("bitnet" t "Because It's Time NET")
2090 ("com" t "Commercial")
2091 ("edu" t "Educational")
2092 ("gov" t "Government (U.S.)")
2093 ("int" t "International (NATO)")
2094 ("mil" t "Military (U.S.)")
2095 ("nato" t "North Atlantic Treaty Organization")
2096 ("net" t "Network")
2097 ("org" t "Non-profit Organization")
2098 ;;("unter-dom" t "? (Ger.)")
2099 ("uucp" t "Unix to Unix CoPy")
2100 ;;("fipnet" nil "?")
2101 ))
2102 ob))
2103
2104 ;;;###autoload
2105 (defun what-domain (domain)
2106 "Convert mail domain DOMAIN to the country it corresponds to."
2107 (interactive
2108 (let ((completion-ignore-case t))
2109 (list (completing-read "Domain: "
2110 mail-extr-all-top-level-domains nil t))))
2111 (or (setq domain (intern-soft (downcase domain)
2112 mail-extr-all-top-level-domains))
2113 (error "No such domain"))
2114 (message "%s: %s" (upcase (symbol-name domain)) (get domain 'domain-name)))
2115
2116 \f
2117 ;(let ((all nil))
2118 ; (mapatoms #'(lambda (x)
2119 ; (if (and (boundp x)
2120 ; (string-match "^mail-extr-" (symbol-name x)))
2121 ; (setq all (cons x all)))))
2122 ; (setq all (sort all #'string-lessp))
2123 ; (cons 'setq
2124 ; (apply 'nconc (mapcar #'(lambda (x)
2125 ; (list x (symbol-value x)))
2126 ; all))))
2127
2128 \f
2129 (provide 'mail-extr)
2130
2131 ;;; mail-extr.el ends here