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