(mail-extr-voodoo): Get rid of comments at an early stage.
[emacs.git] / lisp / mail / mail-extr.el
blob4b5fa650e6242dcfa6b30c140f51d9a168ccc566
1 ;;; mail-extr.el --- extract full name and address from RFC 822 mail header.
3 ;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@cs.bu.edu>
6 ;; Maintainer: Jamie Zawinski <jwz@lucid.com>
7 ;; Version: 1.8
8 ;; Keywords: mail
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; The entry point of this code is
31 ;; mail-extract-address-components: (address)
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.
43 ;; This code is more correct (and more heuristic) parser than the code in
44 ;; rfc822.el. And despite its size, it's fairly fast.
46 ;; There are two main benefits:
48 ;; 1. Higher probability of getting the correct full name for a human than
49 ;; any other package we know of. (On the other hand, it will cheerfully
50 ;; mangle non-human names/comments.)
51 ;; 2. Address part is put in a canonical form.
53 ;; The interface is not yet carved in stone; please give us suggestions.
55 ;; We have an extensive test-case collection of funny addresses if you want to
56 ;; work with the code. Developing this code requires frequent testing to
57 ;; make sure you're not breaking functionality. The test cases aren't included
58 ;; because they are over 100K.
60 ;; If you find an address that mail-extr fails on, please send it to the
61 ;; maintainer along with what you think the correct results should be. We do
62 ;; not consider it a bug if mail-extr mangles a comment that does not
63 ;; correspond to a real human full name, although we would prefer that
64 ;; mail-extr would return the comment as-is.
66 ;; Features:
68 ;; * Full name handling:
70 ;; * knows where full names can be found in an address.
71 ;; * avoids using empty comments and quoted text.
72 ;; * extracts full names from mailbox names.
73 ;; * recognizes common formats for comments after a full name.
74 ;; * puts a period and a space after each initial.
75 ;; * understands & referring to the mailbox name, capitalized.
76 ;; * strips name prefixes like "Prof.", etc.
77 ;; * understands what characters can occur in names (not just letters).
78 ;; * figures out middle initial from mailbox name.
79 ;; * removes funny nicknames.
80 ;; * keeps suffixes such as Jr., Sr., III, etc.
81 ;; * reorders "Last, First" type names.
83 ;; * Address handling:
85 ;; * parses rfc822 quoted text, comments, and domain literals.
86 ;; * parses rfc822 multi-line headers.
87 ;; * does something reasonable with rfc822 GROUP addresses.
88 ;; * handles many rfc822 noncompliant and garbage addresses.
89 ;; * canonicalizes addresses (after stripping comments/phrases outside <>).
90 ;; * converts ! addresses into .UUCP and %-style addresses.
91 ;; * converts rfc822 ROUTE addresses to %-style addresses.
92 ;; * truncates %-style addresses at leftmost fully qualified domain name.
93 ;; * handles local relative precedence of ! vs. % and @ (untested).
95 ;; It does almost no string creation. It primarily uses the built-in
96 ;; parsing routines with the appropriate syntax tables. This should
97 ;; result in greater speed.
99 ;; TODO:
101 ;; * handle all test cases. (This will take forever.)
102 ;; * software to pick the correct header to use (eg., "Senders-Name:").
103 ;; * multiple addresses in the "From:" header (almost all of the necessary
104 ;; code is there).
105 ;; * flag to not treat `,' as an address separator. (This is useful when
106 ;; there is a "From:" header but no "Sender:" header, because then there
107 ;; is only allowed to be one address.)
108 ;; * mailbox name does not necessarily contain full name.
109 ;; * fixing capitalization when it's all upper or lowercase. (Hard!)
110 ;; * some of the domain literal handling is missing. (But I've never even
111 ;; seen one of these in a mail address, so maybe no big deal.)
112 ;; * arrange to have syntax tables byte-compiled.
113 ;; * speed hacks.
114 ;; * delete unused variables.
115 ;; * arrange for testing with different relative precedences of ! vs. @
116 ;; and %.
117 ;; * insert documentation strings!
118 ;; * handle X.400-gatewayed addresses according to RFC 1148.
120 ;;; Change Log:
122 ;; Thu Feb 17 17:57:33 1994 Jamie Zawinski (jwz@lucid.com)
124 ;; * merged with jbw's latest version
126 ;; Wed Feb 9 21:56:27 1994 Jamie Zawinski (jwz@lucid.com)
128 ;; * high-bit chars in comments weren't treated as word syntax
130 ;; Sat Feb 5 03:13:40 1994 Jamie Zawinski (jwz@lucid.com)
132 ;; * call replace-match with fixed-case arg
134 ;; Thu Dec 16 21:56:45 1993 Jamie Zawinski (jwz@lucid.com)
136 ;; * some more cleanup, doc, added provide
138 ;; Tue Mar 23 21:23:18 1993 Joe Wells (jbw at csd.bu.edu)
140 ;; * Made mail-full-name-prefixes a user-customizable variable.
141 ;; Allow passing the address as a buffer as well as as a string.
142 ;; Allow [ and ] as name characters (Finnish character set).
144 ;; Mon Mar 22 21:20:56 1993 Joe Wells (jbw at bigbird.bu.edu)
146 ;; * Handle "null" addresses. Handle = used for spacing in mailbox
147 ;; name. Fix bug in handling of ROUTE-ADDR-type addresses that are
148 ;; missing their brackets. Handle uppercase "JR". Extract full
149 ;; names from X.400 addresses encoded in RFC-822. Fix bug in
150 ;; handling of multiple addresses where first has trailing comment.
151 ;; Handle more kinds of telephone extension lead-ins.
153 ;; Mon Mar 22 20:16:57 1993 Joe Wells (jbw at bigbird.bu.edu)
155 ;; * Handle HZ encoding for embedding GB encoded chinese characters.
157 ;; Mon Mar 22 00:46:12 1993 Joe Wells (jbw at bigbird.bu.edu)
159 ;; * Fixed too broad matching of ham radio call signs. Fixed bug in
160 ;; handling an unmatched ' in a name string. Enhanced recognition
161 ;; of when . in the mailbox name terminates the name portion.
162 ;; Narrowed conversion of . to space to only the necessary
163 ;; situation. Deal with VMS's stupid date stamps. Handle a unique
164 ;; way of introducing an alternate address. Fixed spacing bug I
165 ;; introduced in switching last name order. Fixed bug in handling
166 ;; address with ! and % but no @. Narrowed the cases in which
167 ;; certain trailing words are discarded.
169 ;; Sun Mar 21 21:41:06 1993 Joe Wells (jbw at bigbird.bu.edu)
171 ;; * Fixed bugs in handling GROUP addresses. Certain words in the
172 ;; middle of a name no longer terminate it. Handle LISTSERV list
173 ;; names. Ignore comment field containing mailbox name.
175 ;; Sun Mar 21 14:39:38 1993 Joe Wells (jbw at bigbird.bu.edu)
177 ;; * Moved variant-method code back into main function. Handle
178 ;; underscores as spaces in comments. Handle leading nickname. Add
179 ;; flag to ignore single-word names. Other changes.
181 ;; Mon Feb 1 22:23:31 1993 Joe Wells (jbw at bigbird.bu.edu)
183 ;; * Added in changes by Rod Whitby and Jamie Zawinski. This
184 ;; includes the flag mail-extr-guess-middle-initial and the fix for
185 ;; handling multiple addresses correctly. (Whitby just changed
186 ;; a > to a <.)
188 ;; Mon Apr 6 23:59:09 1992 Joe Wells (jbw at bigbird.bu.edu)
190 ;; * Cleaned up some more. Release version 1.0 to world.
192 ;; Sun Apr 5 19:39:08 1992 Joe Wells (jbw at bigbird.bu.edu)
194 ;; * Cleaned up full name extraction extensively.
196 ;; Sun Feb 2 14:45:24 1992 Joe Wells (jbw at bigbird.bu.edu)
198 ;; * Total rewrite. Integrated mail-canonicalize-address into
199 ;; mail-extract-address-components. Now handles GROUP addresses more
200 ;; or less correctly. Better handling of lots of different cases.
202 ;; Fri Jun 14 19:39:50 1991
203 ;; * Created.
205 ;;; Code:
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;; User configuration variable definitions.
213 (defvar mail-extr-guess-middle-initial nil
214 "*Whether to try to guess middle initial from mail address.
215 If true, then when we see an address like \"John Smith <jqs@host.com>\"
216 we will assume that \"John Q. Smith\" is the fellow's name.")
218 (defvar mail-extr-ignore-single-names t
219 "*Whether to ignore a name that is just a single word.
220 If true, then when we see an address like \"Idiot <dumb@stupid.com>\"
221 we will act as though we couldn't find a full name in the address.")
223 ;; Matches a leading title that is not part of the name (does not
224 ;; contribute to uniquely identifying the person).
225 (defvar mail-extr-full-name-prefixes
226 (purecopy
227 "\\(Prof\\|D[Rr]\\|Mrs?\\|Rev\\|Rabbi\\|SysOp\\|LCDR\\)\\.?[ \t\n]")
228 "*Matches prefixes to the full name that identify a person's position.
229 These are stripped from the full name because they do not contribute to
230 uniquely identifying the person.")
232 (defvar mail-extr-@-binds-tighter-than-! nil
233 "*Whether the local mail transport agent looks at ! before @.")
235 (defvar mail-extr-mangle-uucp nil
236 "*Whether to throw away information in UUCP addresses
237 by translating things like \"foo!bar!baz@host\" into \"baz@bar.UUCP\".")
239 ;;----------------------------------------------------------------------
240 ;; what orderings are meaningful?????
241 ;;(defvar mail-operator-precedence-list '(?! ?% ?@))
242 ;; Right operand of a % or a @ must be a domain name, period. No other
243 ;; operators allowed. Left operand of a @ is an address relative to that
244 ;; site.
246 ;; Left operand of a ! must be a domain name. Right operand is an
247 ;; arbitrary address.
248 ;;----------------------------------------------------------------------
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;; Constant definitions.
257 ;; Codes in
258 ;; Names in ISO 8859-1 Name
259 ;; ISO 10XXX ISO 8859-2 in
260 ;; ISO 6937 ISO 10646 RFC Swedish
261 ;; etc. Hex Oct 1345 TeX Split ASCII Description
262 ;; --------- ---------- ---- --- ----- ----- -------------------------------
263 ;; %a E4 344 a: \"a ae { latin small a + diaeresis d
264 ;; %o F6 366 o: \"o oe | latin small o + diaeresis v
265 ;; @a E5 345 aa \oa aa } latin small a + ring above e
266 ;; %u FC 374 u: \"u ue ~ latin small u + diaeresis |
267 ;; /e E9 351 e' \'e ` latin small e + acute i
268 ;; %A C4 304 A: \"A AE [ latin capital a + diaeresis D
269 ;; %O D6 326 O: \"O OE \ latin capital o + diaeresis V
270 ;; @A C5 305 AA \oA AA ] latin capital a + ring above E
271 ;; %U DC 334 U: \"U UE ^ latin capital u + diaeresis \
272 ;; /E C9 311 E' \'E @ latin capital e + acute I
274 ;; NOTE: @a and @A are not in ISO 8859-2 (the codes mentioned above invoke
275 ;; /l and /L). Some of this data was retrieved from
276 ;; listserv@jhuvm.hcf.jhu.edu.
278 ;; Any character that can occur in a name, not counting characters that
279 ;; separate parts of a multipart name (hyphen and period).
280 ;; Yes, there are weird people with digits in their names.
281 ;; You will also notice the consideration for the
282 ;; Swedish/Finnish/Norwegian character set.
283 (defconst mail-extr-all-letters-but-separators
284 (purecopy "][A-Za-z{|}'~0-9`\200-\377"))
286 ;; Any character that can occur in a name in an RFC822 address including
287 ;; the separator (hyphen and possibly period) for multipart names.
288 ;; #### should . be in here?
289 (defconst mail-extr-all-letters
290 (purecopy (concat mail-extr-all-letters-but-separators "---")))
292 ;; Any character that can start a name.
293 ;; Keep this set as minimal as possible.
294 (defconst mail-extr-first-letters (purecopy "A-Za-z\200-\377"))
296 ;; Any character that can end a name.
297 ;; Keep this set as minimal as possible.
298 (defconst mail-extr-last-letters (purecopy "A-Za-z\200-\377`'."))
300 (defconst mail-extr-leading-garbage
301 (purecopy (format "[^%s]+" mail-extr-first-letters)))
303 ;; (defconst mail-extr-non-name-chars
304 ;; (purecopy (concat "^" mail-extr-all-letters ".")))
305 ;; (defconst mail-extr-non-begin-name-chars
306 ;; (purecopy (concat "^" mail-extr-first-letters)))
307 ;; (defconst mail-extr-non-end-name-chars
308 ;; (purecopy (concat "^" mail-extr-last-letters)))
310 ;; Matches an initial not followed by both a period and a space.
311 ;; (defconst mail-extr-bad-initials-pattern
312 ;; (purecopy
313 ;; (format "\\(\\([^%s]\\|\\`\\)[%s]\\)\\(\\.\\([^ ]\\)\\| \\|\\([^%s .]\\)\\|\\'\\)"
314 ;; mail-extr-all-letters mail-extr-first-letters mail-extr-all-letters)))
316 ;; Matches periods used instead of spaces. Must not match the period
317 ;; following an initial.
318 (defconst mail-extr-bad-dot-pattern
319 (purecopy
320 (format "\\([%s][%s]\\)\\.+\\([%s]\\)"
321 mail-extr-all-letters
322 mail-extr-last-letters
323 mail-extr-first-letters)))
325 ;; Matches an embedded or leading nickname that should be removed.
326 ;; (defconst mail-extr-nickname-pattern
327 ;; (purecopy
328 ;; (format "\\([ .]\\|\\`\\)[\"'`\[\(]\\([ .%s]+\\)[\]\"'\)] "
329 ;; mail-extr-all-letters)))
331 ;; Matches the occurrence of a generational name suffix, and the last
332 ;; character of the preceding name. This is important because we want to
333 ;; keep such suffixes: they help to uniquely identify the person.
334 ;; *** Perhaps this should be a user-customizable variable. However, the
335 ;; *** regular expression is fairly tricky to alter, so maybe not.
336 (defconst mail-extr-full-name-suffix-pattern
337 (purecopy
338 (format
339 "\\(,? ?\\([JjSs][Rr]\\.?\\|V?I+V?\\)\\)\\([^%s]\\([^%s]\\|\\'\\)\\|\\'\\)"
340 mail-extr-all-letters mail-extr-all-letters)))
342 (defconst mail-extr-roman-numeral-pattern (purecopy "V?I+V?\\b"))
344 ;; Matches a trailing uppercase (with other characters possible) acronym.
345 ;; Must not match a trailing uppercase last name or trailing initial
346 (defconst mail-extr-weird-acronym-pattern
347 (purecopy "\\([A-Z]+[-_/]\\|[A-Z][A-Z][A-Z]?\\b\\)"))
349 ;; Matches a mixed-case or lowercase name (not an initial).
350 ;; #### Match Latin1 lower case letters here too?
351 ;; (defconst mail-extr-mixed-case-name-pattern
352 ;; (purecopy
353 ;; (format
354 ;; "\\b\\([a-z][%s]*[%s]\\|[%s][%s]*[a-z][%s]*[%s]\\|[%s][%s]*[a-z]\\)"
355 ;; mail-extr-all-letters mail-extr-last-letters
356 ;; mail-extr-first-letters mail-extr-all-letters mail-extr-all-letters
357 ;; mail-extr-last-letters mail-extr-first-letters mail-extr-all-letters)))
359 ;; Matches a trailing alternative address.
360 ;; #### Match Latin1 letters here too?
361 ;; #### Match _ before @ here too?
362 (defconst mail-extr-alternative-address-pattern
363 (purecopy "\\(aka *\\)?[a-zA-Z.]+[!@][a-zA-Z.]"))
365 ;; Matches a variety of trailing comments not including comma-delimited
366 ;; comments.
367 (defconst mail-extr-trailing-comment-start-pattern
368 (purecopy " [-{]\\|--\\|[+@#></\;]"))
370 ;; Matches a name (not an initial).
371 ;; This doesn't force a word boundary at the end because sometimes a
372 ;; comment is separated by a `-' with no preceding space.
373 (defconst mail-extr-name-pattern
374 (purecopy (format "\\b[%s][%s]*[%s]"
375 mail-extr-first-letters
376 mail-extr-all-letters
377 mail-extr-last-letters)))
379 (defconst mail-extr-initial-pattern
380 (purecopy (format "\\b[%s]\\([. ]\\|\\b\\)" mail-extr-first-letters)))
382 ;; Matches a single name before a comma.
383 ;; (defconst mail-extr-last-name-first-pattern
384 ;; (purecopy (concat "\\`" mail-extr-name-pattern ",")))
386 ;; Matches telephone extensions.
387 (defconst mail-extr-telephone-extension-pattern
388 (purecopy
389 "\\(\\([Ee]xt\\|\\|[Tt]ph\\|[Tt]el\\|[Xx]\\).?\\)? *\\+?[0-9][- 0-9]+"))
391 ;; Matches ham radio call signs.
392 ;; Help from: Mat Maessen N2NJZ <maessm@rpi.edu>, Mark Feit
393 ;; <mark@era.com>, Michael Covington <mcovingt@ai.uga.edu>.
394 ;; Examples: DX504 DX515 K5MRU K8DHK KA9WGN KA9WGN KD3FU KD6EUI KD6HBW
395 ;; KE9TV KF0NV N1API N3FU N3GZE N3IGS N4KCC N7IKQ N9HHU W4YHF W6ANK WA2SUH
396 ;; WB7VZI N2NJZ NR3G KJ4KK AB4UM AL7NI KH6OH WN3KBT N4TMI W1A N0NZO
397 (defconst mail-extr-ham-call-sign-pattern
398 (purecopy "\\b\\(DX[0-9]+\\|[AKNW][A-Z]?[0-9][A-Z][A-Z]?[A-Z]?\\)"))
400 ;; Possible trailing suffixes: "\\(/\\(KT\\|A[AEG]\\|[R0-9]\\)\\)?"
401 ;; /KT == Temporary Technician (has CSC but not "real" license)
402 ;; /AA == Temporary Advanced
403 ;; /AE == Temporary Extra
404 ;; /AG == Temporary General
405 ;; /R == repeater
406 ;; /# == stations operating out of home district
407 ;; I don't include these in the regexp above because I can't imagine
408 ;; anyone putting them with their name in an e-mail address.
410 ;; Matches normal single-part name
411 (defconst mail-extr-normal-name-pattern
412 (purecopy (format "\\b[%s][%s]+[%s]"
413 mail-extr-first-letters
414 mail-extr-all-letters-but-separators
415 mail-extr-last-letters)))
417 ;; Matches a single word name.
418 ;; (defconst mail-extr-one-name-pattern
419 ;; (purecopy (concat "\\`" mail-extr-normal-name-pattern "\\'")))
421 ;; Matches normal two names with missing middle initial
422 ;; The first name is not allowed to have a hyphen because this can cause
423 ;; false matches where the "middle initial" is actually the first letter
424 ;; of the second part of the first name.
425 (defconst mail-extr-two-name-pattern
426 (purecopy
427 (concat "\\`\\(" mail-extr-normal-name-pattern
428 "\\|" mail-extr-initial-pattern
429 "\\) +\\(" mail-extr-name-pattern "\\)\\(,\\|\\'\\)")))
431 (defconst mail-extr-listserv-list-name-pattern
432 (purecopy "Multiple recipients of list \\([-A-Z]+\\)"))
434 (defconst mail-extr-stupid-vms-date-stamp-pattern
435 (purecopy
436 "[0-9][0-9]-[JFMASOND][aepuco][nbrylgptvc]-[0-9][0-9][0-9][0-9] [0-9]+ *"))
438 ;;; HZ -- GB (PRC Chinese character encoding) in ASCII embedding protocol
440 ;; In ASCII mode, a byte is interpreted as an ASCII character, unless a '~' is
441 ;; encountered. The character '~' is an escape character. By convention, it
442 ;; must be immediately followed ONLY by '~', '{' or '\n' (<LF>), with the
443 ;; following special meaning.
445 ;; o The escape sequence '~~' is interpreted as a '~'.
446 ;; o The escape-to-GB sequence '~{' switches the mode from ASCII to GB.
447 ;; o The escape sequence '~\n' is a line-continuation marker to be consumed
448 ;; with no output produced.
450 ;; In GB mode, characters are interpreted two bytes at a time as (pure) GB
451 ;; codes until the escape-from-GB code '~}' is read. This code switches the
452 ;; mode from GB back to ASCII. (Note that the escape-from-GB code '~}'
453 ;; ($7E7D) is outside the defined GB range.)
454 (defconst mail-extr-hz-embedded-gb-encoded-chinese-pattern
455 (purecopy "~{\\([^~].\\|~[^\}]\\)+~}"))
457 ;; The leading optional lowercase letters are for a bastardized version of
458 ;; the encoding, as is the optional nature of the final slash.
459 (defconst mail-extr-x400-encoded-address-pattern
460 (purecopy "[a-z]?[a-z]?\\(/[A-Za-z]+\\(\\.[A-Za-z]+\\)?=[^/]+\\)+/?\\'"))
462 (defconst mail-extr-x400-encoded-address-field-pattern-format
463 (purecopy "/%s=\\([^/]+\\)\\(/\\|\\'\\)"))
465 (defconst mail-extr-x400-encoded-address-surname-pattern
466 ;; S stands for Surname (family name).
467 (purecopy
468 (format mail-extr-x400-encoded-address-field-pattern-format "[Ss]")))
470 (defconst mail-extr-x400-encoded-address-given-name-pattern
471 ;; G stands for Given name.
472 (purecopy
473 (format mail-extr-x400-encoded-address-field-pattern-format "[Gg]")))
475 (defconst mail-extr-x400-encoded-address-full-name-pattern
476 ;; PN stands for Personal Name. When used it represents the combination
477 ;; of the G and S fields.
478 ;; "The one system I used having this field asked it with the prompt
479 ;; `Personal Name'. But they mapped it into G and S on outgoing real
480 ;; X.400 addresses. As they mapped G and S into PN on incoming..."
481 (purecopy
482 (format mail-extr-x400-encoded-address-field-pattern-format "[Pp][Nn]")))
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;; Syntax tables used for quick parsing.
491 (defconst mail-extr-address-syntax-table (make-syntax-table))
492 (defconst mail-extr-address-comment-syntax-table (make-syntax-table))
493 (defconst mail-extr-address-domain-literal-syntax-table (make-syntax-table))
494 (defconst mail-extr-address-text-comment-syntax-table (make-syntax-table))
495 (defconst mail-extr-address-text-syntax-table (make-syntax-table))
496 (mapcar
497 (function
498 (lambda (pair)
499 (let ((syntax-table (symbol-value (car pair))))
500 (mapcar
501 (function
502 (lambda (item)
503 (if (eq 2 (length item))
504 ;; modifying syntax of a single character
505 (modify-syntax-entry (car item) (car (cdr item)) syntax-table)
506 ;; modifying syntax of a range of characters
507 (let ((char (nth 0 item))
508 (bound (nth 1 item))
509 (syntax (nth 2 item)))
510 (while (<= char bound)
511 (modify-syntax-entry char syntax syntax-table)
512 (setq char (1+ char)))))))
513 (cdr pair)))))
514 '((mail-extr-address-syntax-table
515 (?\000 ?\037 "w") ;control characters
516 (?\040 " ") ;SPC
517 (?! ?~ "w") ;printable characters
518 (?\177 "w") ;DEL
519 (?\200 ?\377 "w") ;high-bit-on characters
520 (?\240 " ") ;nobreakspace
521 (?\t " ")
522 (?\r " ")
523 (?\n " ")
524 (?\( ".")
525 (?\) ".")
526 (?< ".")
527 (?> ".")
528 (?@ ".")
529 (?, ".")
530 (?\; ".")
531 (?: ".")
532 (?\\ "\\")
533 (?\" "\"")
534 (?. ".")
535 (?\[ ".")
536 (?\] ".")
537 ;; % and ! aren't RFC822 characters, but it is convenient to pretend
538 (?% ".")
539 (?! ".") ;; this needs to be word-constituent when not in .UUCP mode
541 (mail-extr-address-comment-syntax-table
542 (?\000 ?\377 "w")
543 (?\040 " ")
544 (?\240 " ")
545 (?\t " ")
546 (?\r " ")
547 (?\n " ")
548 (?\( "\(\)")
549 (?\) "\)\(")
550 (?\\ "\\"))
551 (mail-extr-address-domain-literal-syntax-table
552 (?\000 ?\377 "w")
553 (?\040 " ")
554 (?\240 " ")
555 (?\t " ")
556 (?\r " ")
557 (?\n " ")
558 (?\[ "\(\]") ;??????
559 (?\] "\)\[") ;??????
560 (?\\ "\\"))
561 (mail-extr-address-text-comment-syntax-table
562 (?\000 ?\377 "w")
563 (?\040 " ")
564 (?\240 " ")
565 (?\t " ")
566 (?\r " ")
567 (?\n " ")
568 (?\( "\(\)")
569 (?\) "\)\(")
570 (?\[ "\(\]")
571 (?\] "\)\[")
572 (?\{ "\(\}")
573 (?\} "\)\{")
574 (?\\ "\\")
575 (?\" "\"")
576 ;; (?\' "\)\`")
577 ;; (?\` "\(\'")
579 (mail-extr-address-text-syntax-table
580 (?\000 ?\177 ".")
581 (?\200 ?\377 "w")
582 (?\040 " ")
583 (?\t " ")
584 (?\r " ")
585 (?\n " ")
586 (?A ?Z "w")
587 (?a ?z "w")
588 (?- "w")
589 (?\} "w")
590 (?\{ "w")
591 (?| "w")
592 (?\' "w")
593 (?~ "w")
594 (?0 ?9 "w"))
598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
600 ;; Utility functions and macros.
603 (defmacro mail-extr-delete-char (n)
604 ;; in v19, delete-char is compiled as a function call, but delete-region
605 ;; is byte-coded, so it's much much faster.
606 (list 'delete-region '(point) (list '+ '(point) n)))
608 (defmacro mail-extr-skip-whitespace-forward ()
609 ;; v19 fn skip-syntax-forward is more tasteful, but not byte-coded.
610 '(skip-chars-forward " \t\n\r\240"))
612 (defmacro mail-extr-skip-whitespace-backward ()
613 ;; v19 fn skip-syntax-backward is more tasteful, but not byte-coded.
614 '(skip-chars-backward " \t\n\r\240"))
617 (defmacro mail-extr-undo-backslash-quoting (beg end)
618 (`(save-excursion
619 (save-restriction
620 (narrow-to-region (, beg) (, end))
621 (goto-char (point-min))
622 ;; undo \ quoting
623 (while (search-forward "\\" nil t)
624 (mail-extr-delete-char -1)
625 (or (eobp)
626 (forward-char 1))
627 )))))
629 (defmacro mail-extr-nuke-char-at (pos)
630 (` (save-excursion
631 (goto-char (, pos))
632 (mail-extr-delete-char 1)
633 (insert ?\ ))))
635 (put 'mail-extr-nuke-outside-range
636 'edebug-form-spec '(symbolp &optional form form atom))
638 (defmacro mail-extr-nuke-outside-range (list-symbol
639 beg-symbol end-symbol
640 &optional no-replace)
641 ;; LIST-SYMBOL names a variable holding a list of buffer positions
642 ;; BEG-SYMBOL and END-SYMBOL name variables delimiting a range
643 ;; Each element of LIST-SYMBOL which lies outside of the range is
644 ;; deleted from the list.
645 ;; Unless NO-REPLACE is true, at each of the positions in LIST-SYMBOL
646 ;; which lie outside of the range, one character at that position is
647 ;; replaced with a SPC.
648 (or (memq no-replace '(t nil))
649 (error "no-replace must be t or nil, evaluable at macroexpand-time"))
650 (` (let ((temp (, list-symbol))
652 (while temp
653 (setq ch (car temp))
654 (cond ((or (> ch (, end-symbol))
655 (< ch (, beg-symbol)))
656 (,@ (if no-replace
658 (` ((mail-extr-nuke-char-at ch)))))
659 (setcar temp nil)))
660 (setq temp (cdr temp)))
661 (setq (, list-symbol) (delq nil (, list-symbol))))))
663 (defun mail-extr-demarkerize (marker)
664 ;; if arg is a marker, destroys the marker, then returns the old value.
665 ;; otherwise returns the arg.
666 (if (markerp marker)
667 (let ((temp (marker-position marker)))
668 (set-marker marker nil)
669 temp)
670 marker))
672 (defun mail-extr-markerize (pos)
673 ;; coerces pos to a marker if non-nil.
674 (if (or (markerp pos) (null pos))
676 (copy-marker pos)))
678 (defmacro mail-extr-last (list)
679 ;; Returns last element of LIST.
680 ;; Could be a subst.
681 (` (let ((list (, list)))
682 (while (not (null (cdr list)))
683 (setq list (cdr list)))
684 (car list))))
686 (defmacro mail-extr-safe-move-sexp (arg)
687 ;; Safely skip over one balanced sexp, if there is one. Return t if success.
688 (` (condition-case error
689 (progn
690 (goto-char (or (scan-sexps (point) (, arg)) (point)))
692 (error
693 ;; #### kludge kludge kludge kludge kludge kludge kludge !!!
694 (if (string-equal (nth 1 error) "Unbalanced parentheses")
696 (while t
697 (signal (car error) (cdr error))))))))
699 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
701 ;; The main function to grind addresses
704 (defvar disable-initial-guessing-flag) ; dynamic assignment
705 (defvar cbeg) ; dynamic assignment
706 (defvar cend) ; dynamic assignment
708 ;;;###autoload
709 (defun mail-extract-address-components (address)
710 "Given an RFC-822 ADDRESS, extract full name and canonical address.
711 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
712 If no name can be extracted, FULL-NAME will be nil.
713 ADDRESS 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
716 consing a string.)
717 If ADDRESS contains more than one RFC-822 address, only the first is
718 returned. Some day this function may be extended to extract multiple
719 addresses, or perhaps return the position at which parsing stopped."
720 (let ((canonicalization-buffer (get-buffer-create " *canonical address*"))
721 (extraction-buffer (get-buffer-create " *extract address components*"))
722 char
723 ;; multiple-addresses
724 <-pos >-pos @-pos :-pos comma-pos !-pos %-pos \;-pos
725 group-:-pos group-\;-pos route-addr-:-pos
726 record-pos-symbol
727 first-real-pos last-real-pos
728 phrase-beg phrase-end
729 cbeg cend ; dynamically set from -voodoo
730 quote-beg quote-end
731 atom-beg atom-end
732 mbox-beg mbox-end
733 \.-ends-name
734 temp
735 ;; name-suffix
736 fi mi li ; first, middle, last initial
737 saved-%-pos saved-!-pos saved-@-pos
738 domain-pos \.-pos insert-point
739 ;; mailbox-name-processed-flag
740 disable-initial-guessing-flag ; dynamically set from -voodoo
743 (save-excursion
744 (set-buffer extraction-buffer)
745 (fundamental-mode)
746 (kill-all-local-variables)
747 (buffer-disable-undo extraction-buffer)
748 (set-syntax-table mail-extr-address-syntax-table)
749 (widen)
750 (erase-buffer)
751 (setq case-fold-search nil)
753 ;; Insert extra space at beginning to allow later replacement with <
754 ;; without having to move markers.
755 (insert ?\ )
757 ;; Insert the address itself.
758 (cond ((stringp address)
759 (insert address))
760 ((bufferp address)
761 (insert-buffer-substring address))
763 (error "Invalid address: %s" address)))
765 (set-text-properties (point-min) (point-max) nil)
767 ;; stolen from rfc822.el
768 ;; Unfold multiple lines.
769 (goto-char (point-min))
770 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t)
771 (replace-match "\\1 " t))
773 ;; first pass grabs useful information about address
774 (goto-char (point-min))
775 (while (progn
776 (mail-extr-skip-whitespace-forward)
777 (not (eobp)))
778 (setq char (char-after (point)))
779 (or first-real-pos
780 (if (not (eq char ?\())
781 (setq first-real-pos (point))))
782 (cond
783 ;; comment
784 ((eq char ?\()
785 (set-syntax-table mail-extr-address-comment-syntax-table)
786 ;; only record the first non-empty comment's position
787 (if (and (not cbeg)
788 (save-excursion
789 (forward-char 1)
790 (mail-extr-skip-whitespace-forward)
791 (not (eq ?\) (char-after (point))))))
792 (setq cbeg (point)))
793 ;; TODO: don't record if unbalanced
794 (or (mail-extr-safe-move-sexp 1)
795 (forward-char 1))
796 (set-syntax-table mail-extr-address-syntax-table)
797 (if (and cbeg
798 (not cend))
799 (setq cend (point))))
800 ;; quoted text
801 ((eq char ?\")
802 ;; only record the first non-empty quote's position
803 (if (and (not quote-beg)
804 (save-excursion
805 (forward-char 1)
806 (mail-extr-skip-whitespace-forward)
807 (not (eq ?\" (char-after (point))))))
808 (setq quote-beg (point)))
809 ;; TODO: don't record if unbalanced
810 (or (mail-extr-safe-move-sexp 1)
811 (forward-char 1))
812 (if (and quote-beg
813 (not quote-end))
814 (setq quote-end (point))))
815 ;; domain literals
816 ((eq char ?\[)
817 (set-syntax-table mail-extr-address-domain-literal-syntax-table)
818 (or (mail-extr-safe-move-sexp 1)
819 (forward-char 1))
820 (set-syntax-table mail-extr-address-syntax-table))
821 ;; commas delimit addresses when outside < > pairs.
822 ((and (eq char ?,)
823 (or (and (null <-pos)
824 ;; Handle ROUTE-ADDR address that is missing its <.
825 (not (eq ?@ (char-after (1+ (point))))))
826 (and >-pos
827 ;; handle weird munged addresses
828 ;; BUG FIX: This test was reversed. Thanks to the
829 ;; brilliant Rod Whitby <rwhitby@research.canon.oz.au>
830 ;; for discovering this!
831 (< (mail-extr-last <-pos) (car >-pos)))))
832 ;; It'd be great if some day this worked, but for now, punt.
833 ;; (setq multiple-addresses t)
834 ;; ;; *** Why do I want this:
835 ;; (mail-extr-delete-char 1)
836 ;; (narrow-to-region (point-min) (point))
837 (delete-region (point) (point-max))
838 (setq char ?\() ; HAVE I NO SHAME??
840 ;; record the position of various interesting chars, determine
841 ;; legality later.
842 ((setq record-pos-symbol
843 (cdr (assq char
844 '((?< . <-pos) (?> . >-pos) (?@ . @-pos)
845 (?: . :-pos) (?, . comma-pos) (?! . !-pos)
846 (?% . %-pos) (?\; . \;-pos)))))
847 (set record-pos-symbol
848 (cons (point) (symbol-value record-pos-symbol)))
849 (forward-char 1))
850 ((eq char ?.)
851 (forward-char 1))
852 ((memq char '(
853 ;; comment terminator illegal
855 ;; domain literal terminator illegal
857 ;; \ allowed only within quoted strings,
858 ;; domain literals, and comments
861 (mail-extr-nuke-char-at (point))
862 (forward-char 1))
864 (forward-word 1)))
865 (or (eq char ?\()
866 ;; At the end of first address of a multiple address header.
867 (and (eq char ?,)
868 (eobp))
869 (setq last-real-pos (point))))
871 ;; Use only the leftmost <, if any. Replace all others with spaces.
872 (while (cdr <-pos)
873 (mail-extr-nuke-char-at (car <-pos))
874 (setq <-pos (cdr <-pos)))
876 ;; Use only the rightmost >, if any. Replace all others with spaces.
877 (while (cdr >-pos)
878 (mail-extr-nuke-char-at (nth 1 >-pos))
879 (setcdr >-pos (nthcdr 2 >-pos)))
881 ;; If multiple @s and a :, but no < and >, insert around buffer.
882 ;; Example: @foo.bar.dom,@xxx.yyy.zzz:mailbox@aaa.bbb.ccc
883 ;; This commonly happens on the UUCP "From " line. Ugh.
884 (cond ((and (> (length @-pos) 1)
885 (eq 1 (length :-pos)) ;TODO: check if between last two @s
886 (not \;-pos)
887 (not <-pos))
888 (goto-char (point-min))
889 (mail-extr-delete-char 1)
890 (setq <-pos (list (point)))
891 (insert ?<)))
893 ;; If < but no >, insert > in rightmost possible position
894 (cond ((and <-pos
895 (null >-pos))
896 (goto-char (point-max))
897 (setq >-pos (list (point)))
898 (insert ?>)))
900 ;; If > but no <, replace > with space.
901 (cond ((and >-pos
902 (null <-pos))
903 (mail-extr-nuke-char-at (car >-pos))
904 (setq >-pos nil)))
906 ;; Turn >-pos and <-pos into non-lists
907 (setq >-pos (car >-pos)
908 <-pos (car <-pos))
910 ;; Trim other punctuation lists of items outside < > pair to handle
911 ;; stupid MTAs.
912 (cond (<-pos ; don't need to check >-pos also
913 ;; handle bozo software that violates RFC 822 by sticking
914 ;; punctuation marks outside of a < > pair
915 (mail-extr-nuke-outside-range @-pos <-pos >-pos t)
916 ;; RFC 822 says nothing about these two outside < >, but
917 ;; remove those positions from the lists to make things
918 ;; easier.
919 (mail-extr-nuke-outside-range !-pos <-pos >-pos t)
920 (mail-extr-nuke-outside-range %-pos <-pos >-pos t)))
922 ;; Check for : that indicates GROUP list and for : part of
923 ;; ROUTE-ADDR spec.
924 ;; Can't possibly be more than two :. Nuke any extra.
925 (while :-pos
926 (setq temp (car :-pos)
927 :-pos (cdr :-pos))
928 (cond ((and <-pos >-pos
929 (> temp <-pos)
930 (< temp >-pos))
931 (if (or route-addr-:-pos
932 (< (length @-pos) 2)
933 (> temp (car @-pos))
934 (< temp (nth 1 @-pos)))
935 (mail-extr-nuke-char-at temp)
936 (setq route-addr-:-pos temp)))
937 ((or (not <-pos)
938 (and <-pos
939 (< temp <-pos)))
940 (setq group-:-pos temp))))
942 ;; Nuke any ; that is in or to the left of a < > pair or to the left
943 ;; of a GROUP starting :. Also, there may only be one ;.
944 (while \;-pos
945 (setq temp (car \;-pos)
946 \;-pos (cdr \;-pos))
947 (cond ((and <-pos >-pos
948 (> temp <-pos)
949 (< temp >-pos))
950 (mail-extr-nuke-char-at temp))
951 ((and (or (not group-:-pos)
952 (> temp group-:-pos))
953 (not group-\;-pos))
954 (setq group-\;-pos temp))))
956 ;; Nuke unmatched GROUP syntax characters.
957 (cond ((and group-:-pos (not group-\;-pos))
958 ;; *** Do I really need to erase it?
959 (mail-extr-nuke-char-at group-:-pos)
960 (setq group-:-pos nil)))
961 (cond ((and group-\;-pos (not group-:-pos))
962 ;; *** Do I really need to erase it?
963 (mail-extr-nuke-char-at group-\;-pos)
964 (setq group-\;-pos nil)))
966 ;; Handle junk like ";@host.company.dom" that sendmail adds.
967 ;; **** should I remember comment positions?
968 (cond
969 (group-\;-pos
970 ;; this is fine for now
971 (mail-extr-nuke-outside-range !-pos group-:-pos group-\;-pos t)
972 (mail-extr-nuke-outside-range @-pos group-:-pos group-\;-pos t)
973 (mail-extr-nuke-outside-range %-pos group-:-pos group-\;-pos t)
974 (mail-extr-nuke-outside-range comma-pos group-:-pos group-\;-pos t)
975 (and last-real-pos
976 (> last-real-pos (1+ group-\;-pos))
977 (setq last-real-pos (1+ group-\;-pos)))
978 ;; *** This may be wrong:
979 (and cend
980 (> cend group-\;-pos)
981 (setq cend nil
982 cbeg nil))
983 (and quote-end
984 (> quote-end group-\;-pos)
985 (setq quote-end nil
986 quote-beg nil))
987 ;; This was both wrong and unnecessary:
988 ;;(narrow-to-region (point-min) group-\;-pos)
990 ;; *** The entire handling of GROUP addresses seems rather lame.
991 ;; *** It deserves a complete rethink, except that these addresses
992 ;; *** are hardly ever seen.
995 ;; Any commas must be between < and : of ROUTE-ADDR. Nuke any
996 ;; others.
997 ;; Hell, go ahead an nuke all of the commas.
998 ;; **** This will cause problems when we start handling commas in
999 ;; the PHRASE part .... no it won't ... yes it will ... ?????
1000 (mail-extr-nuke-outside-range comma-pos 1 1)
1002 ;; can only have multiple @s inside < >. The fact that some MTAs
1003 ;; put de-bracketed ROUTE-ADDRs in the UUCP-style "From " line is
1004 ;; handled above.
1006 ;; Locate PHRASE part of ROUTE-ADDR.
1007 (cond (<-pos
1008 (goto-char <-pos)
1009 (mail-extr-skip-whitespace-backward)
1010 (setq phrase-end (point))
1011 (goto-char (or ;;group-:-pos
1012 (point-min)))
1013 (mail-extr-skip-whitespace-forward)
1014 (if (< (point) phrase-end)
1015 (setq phrase-beg (point))
1016 (setq phrase-end nil))))
1018 ;; handle ROUTE-ADDRS with real ROUTEs.
1019 ;; If there are multiple @s, then we assume ROUTE-ADDR syntax, and
1020 ;; any % or ! must be semantically meaningless.
1021 ;; TODO: do this processing into canonicalization buffer
1022 (cond (route-addr-:-pos
1023 (setq !-pos nil
1024 %-pos nil
1025 >-pos (copy-marker >-pos)
1026 route-addr-:-pos (copy-marker route-addr-:-pos))
1027 (goto-char >-pos)
1028 (insert-before-markers ?X)
1029 (goto-char (car @-pos))
1030 (while (setq @-pos (cdr @-pos))
1031 (mail-extr-delete-char 1)
1032 (setq %-pos (cons (point-marker) %-pos))
1033 (insert "%")
1034 (goto-char (1- >-pos))
1035 (save-excursion
1036 (insert-buffer-substring extraction-buffer
1037 (car @-pos) route-addr-:-pos)
1038 (delete-region (car @-pos) route-addr-:-pos))
1039 (or (cdr @-pos)
1040 (setq saved-@-pos (list (point)))))
1041 (setq @-pos saved-@-pos)
1042 (goto-char >-pos)
1043 (mail-extr-delete-char -1)
1044 (mail-extr-nuke-char-at route-addr-:-pos)
1045 (mail-extr-demarkerize route-addr-:-pos)
1046 (setq route-addr-:-pos nil
1047 >-pos (mail-extr-demarkerize >-pos)
1048 %-pos (mapcar 'mail-extr-demarkerize %-pos))))
1050 ;; de-listify @-pos
1051 (setq @-pos (car @-pos))
1053 ;; TODO: remove comments in the middle of an address
1055 (set-buffer canonicalization-buffer)
1056 (fundamental-mode)
1057 (kill-all-local-variables)
1058 (buffer-disable-undo canonicalization-buffer)
1059 (set-syntax-table mail-extr-address-syntax-table)
1060 (setq case-fold-search nil)
1062 (widen)
1063 (erase-buffer)
1064 (insert-buffer-substring extraction-buffer)
1066 (if <-pos
1067 (narrow-to-region (progn
1068 (goto-char (1+ <-pos))
1069 (mail-extr-skip-whitespace-forward)
1070 (point))
1071 >-pos)
1072 (if (and first-real-pos last-real-pos)
1073 (narrow-to-region first-real-pos last-real-pos)
1074 ;; ****** Oh no! What if the address is completely empty!
1075 ;; *** Is this correct?
1076 (narrow-to-region (point-max) (point-max))
1079 (and @-pos %-pos
1080 (mail-extr-nuke-outside-range %-pos (point-min) @-pos))
1081 (and %-pos !-pos
1082 (mail-extr-nuke-outside-range !-pos (point-min) (car %-pos)))
1083 (and @-pos !-pos (not %-pos)
1084 (mail-extr-nuke-outside-range !-pos (point-min) @-pos))
1086 ;; Error condition:?? (and %-pos (not @-pos))
1088 ;; WARNING: THIS CODE IS DUPLICATED BELOW.
1089 (cond ((and %-pos
1090 (not @-pos))
1091 (goto-char (car %-pos))
1092 (mail-extr-delete-char 1)
1093 (setq @-pos (point))
1094 (insert "@")
1095 (setq %-pos (cdr %-pos))))
1097 (if mail-extr-mangle-uucp
1098 (cond (!-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 (mail-extr-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 (mail-extr-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)))
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 "@ "))
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 (mail-extr-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)))
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)))))
1160 ;; WARNING: THIS CODE IS DUPLICATED ABOVE.
1161 (cond ((and %-pos
1162 (not @-pos))
1163 (goto-char (car %-pos))
1164 (mail-extr-delete-char 1)
1165 (setq @-pos (point))
1166 (insert "@")
1167 (setq %-pos (cdr %-pos))))
1169 (setq %-pos (nreverse %-pos))
1170 (cond (%-pos ; implies @-pos valid
1171 (setq temp %-pos)
1172 (catch 'truncated
1173 (while temp
1174 (goto-char (or (nth 1 temp)
1175 @-pos))
1176 (mail-extr-skip-whitespace-backward)
1177 (save-excursion
1178 (mail-extr-safe-move-sexp -1)
1179 (setq domain-pos (point))
1180 (mail-extr-skip-whitespace-backward)
1181 (setq \.-pos (eq ?. (preceding-char))))
1182 (cond ((and \.-pos
1183 ;; #### string consing
1184 (let ((s (intern-soft
1185 (buffer-substring domain-pos (point))
1186 mail-extr-all-top-level-domains)))
1187 (and s (get s 'domain-name))))
1188 (narrow-to-region (point-min) (point))
1189 (goto-char (car temp))
1190 (mail-extr-delete-char 1)
1191 (setq @-pos (point))
1192 (setcdr temp nil)
1193 (setq %-pos (delq @-pos %-pos))
1194 (insert "@")
1195 (throw 'truncated t)))
1196 (setq temp (cdr temp))))))
1197 (setq mbox-beg (point-min)
1198 mbox-end (if %-pos (car %-pos)
1199 (or @-pos
1200 (point-max))))
1202 ;; Done canonicalizing address.
1204 (set-buffer extraction-buffer)
1206 ;; Decide what part of the address to search to find the full name.
1207 (cond (
1208 ;; Example: "First M. Last" <fml@foo.bar.dom>
1209 (and phrase-beg
1210 (eq quote-beg phrase-beg)
1211 (<= quote-end phrase-end))
1212 (narrow-to-region (1+ quote-beg) (1- quote-end))
1213 (mail-extr-undo-backslash-quoting (point-min) (point-max)))
1215 ;; Example: First Last <fml@foo.bar.dom>
1216 (phrase-beg
1217 (narrow-to-region phrase-beg phrase-end))
1219 ;; Example: fml@foo.bar.dom (First M. Last)
1220 (cbeg
1221 (narrow-to-region (1+ cbeg) (1- cend))
1222 (mail-extr-undo-backslash-quoting (point-min) (point-max))
1224 ;; Deal with spacing problems
1225 (goto-char (point-min))
1226 ; (cond ((not (search-forward " " nil t))
1227 ; (goto-char (point-min))
1228 ; (cond ((search-forward "_" nil t)
1229 ; ;; Handle the *idiotic* use of underlines as spaces.
1230 ; ;; Example: fml@foo.bar.dom (First_M._Last)
1231 ; (goto-char (point-min))
1232 ; (while (search-forward "_" nil t)
1233 ; (replace-match " " t)))
1234 ; ((search-forward "." nil t)
1235 ; ;; Fix . used as space
1236 ; ;; Example: danj1@cb.att.com (daniel.jacobson)
1237 ; (goto-char (point-min))
1238 ; (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1239 ; (replace-match "\\1 \\2" t))))))
1242 ;; Otherwise we try to get the name from the mailbox portion
1243 ;; of the address.
1244 ;; Example: First_M_Last@foo.bar.dom
1246 ;; *** Work in canon buffer instead? No, can't. Hmm.
1247 (goto-char (point-max))
1248 (narrow-to-region (point) (point))
1249 (insert-buffer-substring canonicalization-buffer
1250 mbox-beg mbox-end)
1251 (goto-char (point-min))
1253 ;; Example: First_Last.XXX@foo.bar.dom
1254 (setq \.-ends-name (re-search-forward "[_0-9]" nil t))
1256 (goto-char (point-min))
1258 (if (not mail-extr-mangle-uucp)
1259 (modify-syntax-entry ?! "w" (syntax-table)))
1261 (while (progn
1262 (mail-extr-skip-whitespace-forward)
1263 (not (eobp)))
1264 (setq char (char-after (point)))
1265 (cond
1266 ((eq char ?\")
1267 (setq quote-beg (point))
1268 (or (mail-extr-safe-move-sexp 1)
1269 ;; TODO: handle this error condition!!!!!
1270 (forward-char 1))
1271 ;; take into account deletions
1272 (setq quote-end (- (point) 2))
1273 (save-excursion
1274 (backward-char 1)
1275 (mail-extr-delete-char 1)
1276 (goto-char quote-beg)
1277 (or (eobp)
1278 (mail-extr-delete-char 1)))
1279 (mail-extr-undo-backslash-quoting quote-beg quote-end)
1280 (or (eq ?\ (char-after (point)))
1281 (insert " "))
1282 ;; (setq mailbox-name-processed-flag t)
1283 (setq \.-ends-name t))
1284 ((eq char ?.)
1285 (if (memq (char-after (1+ (point))) '(?_ ?=))
1286 (progn
1287 (forward-char 1)
1288 (mail-extr-delete-char 1)
1289 (insert ?\ ))
1290 (if \.-ends-name
1291 (narrow-to-region (point-min) (point))
1292 (mail-extr-delete-char 1)
1293 (insert " ")))
1294 ;; (setq mailbox-name-processed-flag t)
1296 ((memq (char-syntax char) '(?. ?\\))
1297 (mail-extr-delete-char 1)
1298 (insert " ")
1299 ;; (setq mailbox-name-processed-flag t)
1302 (setq atom-beg (point))
1303 (forward-word 1)
1304 (setq atom-end (point))
1305 (goto-char atom-beg)
1306 (save-restriction
1307 (narrow-to-region atom-beg atom-end)
1308 (cond
1310 ;; Handle X.400 addresses encoded in RFC-822.
1311 ;; *** Shit! This has to handle the case where it is
1312 ;; *** embedded in a quote too!
1313 ;; *** Shit! The input is being broken up into atoms
1314 ;; *** by periods!
1315 ((looking-at mail-extr-x400-encoded-address-pattern)
1317 ;; Copy the contents of the individual fields that
1318 ;; might hold name data to the beginning.
1319 (mapcar
1320 (function
1321 (lambda (field-pattern)
1322 (cond
1323 ((save-excursion
1324 (re-search-forward field-pattern nil t))
1325 (insert-buffer-substring (current-buffer)
1326 (match-beginning 1)
1327 (match-end 1))
1328 (insert " ")))))
1329 (list mail-extr-x400-encoded-address-given-name-pattern
1330 mail-extr-x400-encoded-address-surname-pattern
1331 mail-extr-x400-encoded-address-full-name-pattern))
1333 ;; Discard the rest, since it contains stuff like
1334 ;; routing information, not part of a name.
1335 (mail-extr-skip-whitespace-backward)
1336 (delete-region (point) (point-max))
1338 ;; Handle periods used for spacing.
1339 (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1340 (replace-match "\\1 \\2" t))
1342 ;; (setq mailbox-name-processed-flag t)
1345 ;; Handle normal addresses.
1347 (goto-char (point-min))
1348 ;; Handle _ and = used for spacing.
1349 (while (re-search-forward "\\([^_=]+\\)[_=]" nil t)
1350 (replace-match "\\1 " t)
1351 ;; (setq mailbox-name-processed-flag t)
1353 (goto-char (point-max))))))))
1355 ;; undo the dirty deed
1356 (if (not mail-extr-mangle-uucp)
1357 (modify-syntax-entry ?! "." (syntax-table)))
1359 ;; If we derived the name from the mailbox part of the address,
1360 ;; and we only got one word out of it, don't treat that as a
1361 ;; name. "foo@bar" --> (nil "foo@bar"), not ("foo" "foo@bar")
1362 ;; (if (not mailbox-name-processed-flag)
1363 ;; (delete-region (point-min) (point-max)))
1366 (set-syntax-table mail-extr-address-text-syntax-table)
1368 (mail-extr-voodoo mbox-beg mbox-end canonicalization-buffer)
1369 (goto-char (point-min))
1371 ;; If name is "First Last" and userid is "F?L", then assume
1372 ;; the middle initial is the second letter in the userid.
1373 ;; Initial code by Jamie Zawinski <jwz@lucid.com>
1374 ;; *** Make it work when there's a suffix as well.
1375 (goto-char (point-min))
1376 (cond ((and mail-extr-guess-middle-initial
1377 (not disable-initial-guessing-flag)
1378 (eq 3 (- mbox-end mbox-beg))
1379 (progn
1380 (goto-char (point-min))
1381 (looking-at mail-extr-two-name-pattern)))
1382 (setq fi (char-after (match-beginning 0))
1383 li (char-after (match-beginning 3)))
1384 (save-excursion
1385 (set-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 (cond ((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) ". ")))))
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 (cond ((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 (if names-match-flag
1416 (narrow-to-region (point) (point))))))
1418 ;; Nuke name if it's just one word.
1419 (goto-char (point-min))
1420 (and mail-extr-ignore-single-names
1421 (not (re-search-forward "[- ]" nil t))
1422 (narrow-to-region (point) (point)))
1424 ;; Result
1425 (list (if (not (= (point-min) (point-max)))
1426 (buffer-string))
1427 (progn
1428 (set-buffer canonicalization-buffer)
1429 (if (not (= (point-min) (point-max)))
1430 (buffer-string))))
1433 (defun mail-extr-voodoo (mbox-beg mbox-end canonicalization-buffer)
1434 (let ((word-count 0)
1435 (case-fold-search nil)
1436 mixed-case-flag lower-case-flag ;;upper-case-flag
1437 suffix-flag last-name-comma-flag
1438 ;;cbeg cend
1439 initial
1440 begin-again-flag
1441 drop-this-word-if-trailing-flag
1442 drop-last-word-if-trailing-flag
1443 word-found-flag
1444 this-word-beg last-word-beg
1445 name-beg name-end
1446 name-done-flag
1448 (save-excursion
1449 (set-syntax-table mail-extr-address-text-syntax-table)
1451 ;; Get rid of comments.
1452 (goto-char (point-min))
1453 (while (not (eobp))
1454 ;; Initialize for this iteration of the loop.
1455 (skip-chars-forward "^({[\"'`")
1456 (let ((cbeg (point)))
1457 (set-syntax-table mail-extr-address-text-comment-syntax-table)
1458 (cond ((memq (following-char) '(?\' ?\`))
1459 (search-forward "'" nil t
1460 (if (eq ?\' (following-char)) 2 1)))
1462 (or (mail-extr-safe-move-sexp 1)
1463 (goto-char (point-max)))))
1464 (set-syntax-table mail-extr-address-text-syntax-table)
1465 (when (eq (char-after cbeg) ?\()
1466 ;; Delete the comment itself.
1467 (delete-region cbeg (point))
1468 ;; Canonicalize whitespace where the comment was.
1469 (skip-chars-backward " \t")
1470 (if (looking-at "\\([ \t]+$\\|[ \t]+,\\)")
1471 (replace-match "")
1472 (setq cbeg (point))
1473 (skip-chars-forward " \t")
1474 (if (bobp)
1475 (delete-region (point) cbeg)
1476 (just-one-space))))))
1478 ;; This was moved above.
1479 ;; Fix . used as space
1480 ;; But it belongs here because it occurs not only as
1481 ;; rypens@reks.uia.ac.be (Piet.Rypens)
1482 ;; but also as
1483 ;; "Piet.Rypens" <rypens@reks.uia.ac.be>
1484 ;;(goto-char (point-min))
1485 ;;(while (re-search-forward mail-extr-bad-dot-pattern nil t)
1486 ;; (replace-match "\\1 \\2" t))
1488 (cond ((not (search-forward " " nil t))
1489 (goto-char (point-min))
1490 (cond ((search-forward "_" nil t)
1491 ;; Handle the *idiotic* use of underlines as spaces.
1492 ;; Example: fml@foo.bar.dom (First_M._Last)
1493 (goto-char (point-min))
1494 (while (search-forward "_" nil t)
1495 (replace-match " " t)))
1496 ((search-forward "." nil t)
1497 ;; Fix . used as space
1498 ;; Example: danj1@cb.att.com (daniel.jacobson)
1499 (goto-char (point-min))
1500 (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1501 (replace-match "\\1 \\2" t))))))
1504 ;; Loop over the words (and other junk) in the name.
1505 (goto-char (point-min))
1506 (while (not name-done-flag)
1508 (cond (word-found-flag
1509 ;; Last time through this loop we skipped over a word.
1510 (setq last-word-beg this-word-beg)
1511 (setq drop-last-word-if-trailing-flag
1512 drop-this-word-if-trailing-flag)
1513 (setq word-found-flag nil)))
1515 (cond (begin-again-flag
1516 ;; Last time through the loop we found something that
1517 ;; indicates we should pretend we are beginning again from
1518 ;; the start.
1519 (setq word-count 0)
1520 (setq last-word-beg nil)
1521 (setq drop-last-word-if-trailing-flag nil)
1522 (setq mixed-case-flag nil)
1523 (setq lower-case-flag nil)
1524 ;; (setq upper-case-flag nil)
1525 (setq begin-again-flag nil)
1528 ;; Initialize for this iteration of the loop.
1529 (mail-extr-skip-whitespace-forward)
1530 (if (eq word-count 0) (narrow-to-region (point) (point-max)))
1531 (setq this-word-beg (point))
1532 (setq drop-this-word-if-trailing-flag nil)
1534 ;; Decide what to do based on what we are looking at.
1535 (cond
1537 ;; Delete title
1538 ((and (eq word-count 0)
1539 (looking-at mail-extr-full-name-prefixes))
1540 (goto-char (match-end 0))
1541 (narrow-to-region (point) (point-max)))
1543 ;; Stop after name suffix
1544 ((and (>= word-count 2)
1545 (looking-at mail-extr-full-name-suffix-pattern))
1546 (mail-extr-skip-whitespace-backward)
1547 (setq suffix-flag (point))
1548 (if (eq ?, (following-char))
1549 (forward-char 1)
1550 (insert ?,))
1551 ;; Enforce at least one space after comma
1552 (or (eq ?\ (following-char))
1553 (insert ?\ ))
1554 (mail-extr-skip-whitespace-forward)
1555 (cond ((memq (following-char) '(?j ?J ?s ?S))
1556 (capitalize-word 1)
1557 (if (eq (following-char) ?.)
1558 (forward-char 1)
1559 (insert ?.)))
1561 (upcase-word 1)))
1562 (setq word-found-flag t)
1563 (setq name-done-flag t))
1565 ;; Handle SCA names
1566 ((looking-at "MKA \\(.+\\)") ; "Mundanely Known As"
1567 (goto-char (match-beginning 1))
1568 (narrow-to-region (point) (point-max))
1569 (setq begin-again-flag t))
1571 ;; Check for initial last name followed by comma
1572 ((and (eq ?, (following-char))
1573 (eq word-count 1))
1574 (forward-char 1)
1575 (setq last-name-comma-flag t)
1576 (or (eq ?\ (following-char))
1577 (insert ?\ )))
1579 ;; Stop before trailing comma-separated comment
1580 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1581 ;; *** This case is redundant???
1582 ;;((eq ?, (following-char))
1583 ;; (setq name-done-flag t))
1585 ;; Delete parenthesized/quoted comment/nickname
1586 ((memq (following-char) '(?\( ?\{ ?\[ ?\" ?\' ?\`))
1587 (setq cbeg (point))
1588 (set-syntax-table mail-extr-address-text-comment-syntax-table)
1589 (cond ((memq (following-char) '(?\' ?\`))
1590 (or (search-forward "'" nil t
1591 (if (eq ?\' (following-char)) 2 1))
1592 (mail-extr-delete-char 1)))
1594 (or (mail-extr-safe-move-sexp 1)
1595 (goto-char (point-max)))))
1596 (set-syntax-table mail-extr-address-text-syntax-table)
1597 (setq cend (point))
1598 (cond
1599 ;; Handle case of entire name being quoted
1600 ((and (eq word-count 0)
1601 (looking-at " *\\'")
1602 (>= (- cend cbeg) 2))
1603 (narrow-to-region (1+ cbeg) (1- cend))
1604 (goto-char (point-min)))
1606 ;; Handle case of quoted initial
1607 (if (and (or (= 3 (- cend cbeg))
1608 (and (= 4 (- cend cbeg))
1609 (eq ?. (char-after (+ 2 cbeg)))))
1610 (not (looking-at " *\\'")))
1611 (setq initial (char-after (1+ cbeg)))
1612 (setq initial nil))
1613 (delete-region cbeg cend)
1614 (if initial
1615 (insert initial ". ")))))
1617 ;; Handle & substitution
1618 ((and (or (bobp)
1619 (eq ?\ (preceding-char)))
1620 (looking-at "&\\( \\|\\'\\)"))
1621 (mail-extr-delete-char 1)
1622 (capitalize-region
1623 (point)
1624 (progn
1625 (insert-buffer-substring canonicalization-buffer
1626 mbox-beg mbox-end)
1627 (point)))
1628 (setq disable-initial-guessing-flag t)
1629 (setq word-found-flag t))
1631 ;; Handle *Stupid* VMS date stamps
1632 ((looking-at mail-extr-stupid-vms-date-stamp-pattern)
1633 (replace-match "" t))
1635 ;; Handle Chinese characters.
1636 ((looking-at mail-extr-hz-embedded-gb-encoded-chinese-pattern)
1637 (goto-char (match-end 0))
1638 (setq word-found-flag t))
1640 ;; Skip initial garbage characters.
1641 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1642 ((and (eq word-count 0)
1643 (looking-at mail-extr-leading-garbage))
1644 (goto-char (match-end 0))
1645 ;; *** Skip backward over these???
1646 ;; (skip-chars-backward "& \"")
1647 (narrow-to-region (point) (point-max)))
1649 ;; Various stopping points
1650 ((or
1652 ;; Stop before ALL CAPS acronyms, if preceded by mixed-case
1653 ;; words. Example: XT-DEM.
1654 (and (>= word-count 2)
1655 mixed-case-flag
1656 (looking-at mail-extr-weird-acronym-pattern)
1657 (not (looking-at mail-extr-roman-numeral-pattern)))
1659 ;; Stop before trailing alternative address
1660 (looking-at mail-extr-alternative-address-pattern)
1662 ;; Stop before trailing comment not introduced by comma
1663 ;; THIS CASE MUST BE AFTER AN EARLIER CASE.
1664 (looking-at mail-extr-trailing-comment-start-pattern)
1666 ;; Stop before telephone numbers
1667 (looking-at mail-extr-telephone-extension-pattern))
1668 (setq name-done-flag t))
1670 ;; Delete ham radio call signs
1671 ((looking-at mail-extr-ham-call-sign-pattern)
1672 (delete-region (match-beginning 0) (match-end 0)))
1674 ;; Fixup initials
1675 ((looking-at mail-extr-initial-pattern)
1676 (or (eq (following-char) (upcase (following-char)))
1677 (setq lower-case-flag t))
1678 (forward-char 1)
1679 (if (eq ?. (following-char))
1680 (forward-char 1)
1681 (insert ?.))
1682 (or (eq ?\ (following-char))
1683 (insert ?\ ))
1684 (setq word-found-flag t))
1686 ;; Handle BITNET LISTSERV list names.
1687 ((and (eq word-count 0)
1688 (looking-at mail-extr-listserv-list-name-pattern))
1689 (narrow-to-region (match-beginning 1) (match-end 1))
1690 (setq word-found-flag t)
1691 (setq name-done-flag t))
1693 ;; Regular name words
1694 ((looking-at mail-extr-name-pattern)
1695 (setq name-beg (point))
1696 (setq name-end (match-end 0))
1698 ;; Certain words will be dropped if they are at the end.
1699 (and (>= word-count 2)
1700 (not lower-case-flag)
1702 ;; A trailing 4-or-more letter lowercase words preceded by
1703 ;; mixed case or uppercase words will be dropped.
1704 (looking-at "[a-z][a-z][a-z][a-z]+[ \t]*\\'")
1705 ;; Drop a trailing word which is terminated with a period.
1706 (eq ?. (char-after (1- name-end))))
1707 (setq drop-this-word-if-trailing-flag t))
1709 ;; Set the flags that indicate whether we have seen a lowercase
1710 ;; word, a mixed case word, and an uppercase word.
1711 (if (re-search-forward "[a-z]" name-end t)
1712 (if (progn
1713 (goto-char name-beg)
1714 (re-search-forward "[A-Z]" name-end t))
1715 (setq mixed-case-flag t)
1716 (setq lower-case-flag t))
1717 ;; (setq upper-case-flag t)
1720 (goto-char name-end)
1721 (setq word-found-flag t))
1724 (setq name-done-flag t)
1727 ;; Count any word that we skipped over.
1728 (if word-found-flag
1729 (setq word-count (1+ word-count))))
1731 ;; If the last thing in the name is 2 or more periods, or one or more
1732 ;; other sentence terminators (but not a single period) then keep them
1733 ;; and the preceding word. This is for the benefit of whole sentences
1734 ;; in the name field: it's better behavior than dropping the last word
1735 ;; of the sentence...
1736 (if (and (not suffix-flag)
1737 (looking-at "\\(\\.+\\|[?!;:.][?!;:.]+\\|[?!;:][?!;:.]*\\)\\'"))
1738 (goto-char (setq suffix-flag (point-max))))
1740 ;; Drop everything after point and certain trailing words.
1741 (narrow-to-region (point-min)
1742 (or (and drop-last-word-if-trailing-flag
1743 last-word-beg)
1744 (point)))
1746 ;; Xerox's mailers SUCK!!!!!!
1747 ;; We simply refuse to believe that any last name is PARC or ADOC.
1748 ;; If it looks like that is the last name, that there is no meaningful
1749 ;; here at all. Actually I guess it would be best to map patterns
1750 ;; like foo.hoser@xerox.com into foo@hoser.xerox.com, but I don't
1751 ;; actually know that that is what's going on.
1752 (cond ((not suffix-flag)
1753 (goto-char (point-min))
1754 (let ((case-fold-search t))
1755 (if (looking-at "[-A-Za-z_]+[. ]\\(PARC\\|ADOC\\)\\'")
1756 (erase-buffer)))))
1758 ;; If last name first put it at end (but before suffix)
1759 (cond (last-name-comma-flag
1760 (goto-char (point-min))
1761 (search-forward ",")
1762 (setq name-end (1- (point)))
1763 (goto-char (or suffix-flag (point-max)))
1764 (or (eq ?\ (preceding-char))
1765 (insert ?\ ))
1766 (insert-buffer-substring (current-buffer) (point-min) name-end)
1767 (goto-char name-end)
1768 (skip-chars-forward "\t ,")
1769 (narrow-to-region (point) (point-max))))
1771 ;; Delete leading and trailing junk characters.
1772 ;; *** This is probably completely unneeded now.
1773 ;;(goto-char (point-max))
1774 ;;(skip-chars-backward mail-extr-non-end-name-chars)
1775 ;;(if (eq ?. (following-char))
1776 ;; (forward-char 1))
1777 ;;(narrow-to-region (point)
1778 ;; (progn
1779 ;; (goto-char (point-min))
1780 ;; (skip-chars-forward mail-extr-non-begin-name-chars)
1781 ;; (point)))
1783 ;; Compress whitespace
1784 (goto-char (point-min))
1785 (while (re-search-forward "[ \t\n]+" nil t)
1786 (replace-match (if (eobp) "" " ") t))
1791 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1793 ;; Table of top-level domain names.
1795 ;; This is used during address canonicalization; be careful of format changes.
1796 ;; Keep in mind that the country abbreviations follow ISO-3166. There is
1797 ;; a U.S. FIPS that specifies a different set of two-letter country
1798 ;; abbreviations.
1800 (defconst mail-extr-all-top-level-domains
1801 (let ((ob (make-vector 509 0)))
1802 (mapcar
1803 (function
1804 (lambda (x)
1805 (put (intern (downcase (car x)) ob)
1806 'domain-name
1807 (if (nth 2 x)
1808 (format (nth 2 x) (nth 1 x))
1809 (nth 1 x)))))
1811 ;; ISO 3166 codes:
1812 ("ae" "United Arab Emirates")
1813 ("ag" "Antigua and Barbuda")
1814 ("al" "Albania")
1815 ("ao" "Angola")
1816 ("aq" "Antarctica") ; continent
1817 ("ar" "Argentina" "Argentine Republic")
1818 ("at" "Austria" "The Republic of %s")
1819 ("au" "Australia")
1820 ("az" "Azerbaijan")
1821 ("bb" "Barbados")
1822 ("bd" "Bangladesh")
1823 ("be" "Belgium" "The Kingdom of %s")
1824 ("bf" "Burkina Faso")
1825 ("bg" "Bulgaria")
1826 ("bh" "Bahrain")
1827 ("bm" "Bermuda")
1828 ("bo" "Bolivia" "Republic of %s")
1829 ("br" "Brazil" "The Federative Republic of %s")
1830 ("bs" "Bahamas")
1831 ("bw" "Botswana")
1832 ("by" "Belarus")
1833 ("bz" "Belize")
1834 ("ca" "Canada")
1835 ("cg" "Congo")
1836 ("ch" "Switzerland" "The Swiss Confederation")
1837 ("ci" "Ivory Coast")
1838 ("cl" "Chile" "The Republic of %s")
1839 ("cm" "Cameroon") ; In .fr domain
1840 ("cn" "China" "The People's Republic of %s")
1841 ("co" "Colombia")
1842 ("cr" "Costa Rica" "The Republic of %s")
1843 ("cs" "Czechoslovakia")
1844 ("cu" "Cuba")
1845 ("cy" "Cyprus")
1846 ("cz" "Czech Republic")
1847 ("de" "Germany")
1848 ("dk" "Denmark")
1849 ("dm" "Dominica")
1850 ("do" "Dominican Republic" "The %s")
1851 ("dz" "Algeria")
1852 ("ec" "Ecuador" "The Republic of %s")
1853 ("ee" "Estonia")
1854 ("eg" "Egypt" "The Arab Republic of %s")
1855 ("er" "Eritrea")
1856 ("es" "Spain" "The Kingdom of %s")
1857 ("fi" "Finland" "The Republic of %s")
1858 ("fj" "Fiji")
1859 ("fo" "Faroe Islands")
1860 ("fr" "France")
1861 ("gb" "Great Britain")
1862 ("gd" "Grenada")
1863 ("ge" "Georgia")
1864 ("gf" "Guyana (Fr.)")
1865 ("gp" "Guadeloupe (Fr.)")
1866 ("gr" "Greece" "The Hellenic Republic (%s)")
1867 ("gt" "Guatemala")
1868 ("gu" "Guam (U.S.)")
1869 ("hk" "Hong Kong")
1870 ("hn" "Honduras")
1871 ("hr" "Croatia")
1872 ("ht" "Haiti")
1873 ("hu" "Hungary" "The Hungarian Republic") ;???
1874 ("id" "Indonesia")
1875 ("ie" "Ireland")
1876 ("il" "Israel" "The State of %s")
1877 ("in" "India" "The Republic of %s")
1878 ("ir" "Iran")
1879 ("is" "Iceland" "The Republic of %s")
1880 ("it" "Italy" "The Italian Republic")
1881 ("jm" "Jamaica")
1882 ("jp" "Japan")
1883 ("ke" "Kenya")
1884 ("kn" "St. Kitts, Nevis, and Anguilla")
1885 ("kp" "Korea (North)")
1886 ("kr" "Korea (South)")
1887 ("kw" "Kuwait")
1888 ("kz" "Kazakhstan")
1889 ("lb" "Lebanon")
1890 ("lc" "St. Lucia")
1891 ("li" "Liechtenstein")
1892 ("lk" "Sri Lanka" "The Democratic Socialist Republic of %s")
1893 ("ls" "Lesotho")
1894 ("lt" "Lithuania")
1895 ("lu" "Luxembourg")
1896 ("lv" "Latvia")
1897 ("ma" "Morocco")
1898 ("md" "Moldova")
1899 ("mg" "Madagascar")
1900 ("mk" "Macedonia")
1901 ("ml" "Mali")
1902 ("mo" "Macau")
1903 ("mt" "Malta")
1904 ("mu" "Mauritius")
1905 ("mw" "Malawi")
1906 ("mx" "Mexico" "The United Mexican States")
1907 ("my" "Malaysia" "%s (changed to Myanmar?)") ;???
1908 ("mz" "Mozambique")
1909 ("na" "Namibia")
1910 ("nc" "New Caledonia (Fr.)")
1911 ("ne" "Niger") ; In .fr domain
1912 ("ni" "Nicaragua" "The Republic of %s")
1913 ("nl" "Netherlands" "The Kingdom of the %s")
1914 ("no" "Norway" "The Kingdom of %s")
1915 ("np" "Nepal") ; Via .in domain
1916 ("nz" "New Zealand")
1917 ("pa" "Panama")
1918 ("pe" "Peru")
1919 ("pf" "Polynesia (Fr.)")
1920 ("pg" "Papua New Guinea")
1921 ("ph" "Philippines" "The Republic of the %s")
1922 ("pk" "Pakistan")
1923 ("pl" "Poland")
1924 ("pr" "Puerto Rico (U.S.)")
1925 ("pt" "Portugal" "The Portuguese Republic")
1926 ("py" "Paraguay")
1927 ("re" "Reunion (Fr.)") ; In .fr domain
1928 ("ro" "Romania")
1929 ("ru" "Russian Federation")
1930 ("sa" "Saudi Arabia")
1931 ("sc" "Seychelles")
1932 ("sd" "Sudan")
1933 ("se" "Sweden" "The Kingdom of %s")
1934 ("sg" "Singapore" "The Republic of %s")
1935 ("si" "Slovenia")
1936 ("sj" "Svalbard and Jan Mayen Is.") ; In .no domain
1937 ("sk" "Slovakia" "The Slovak Republic")
1938 ("sn" "Senegal")
1939 ("sr" "Suriname")
1940 ("su" "Soviet Union")
1941 ("sz" "Swaziland")
1942 ("tg" "Togo")
1943 ("th" "Thailand" "The Kingdom of %s")
1944 ("tm" "Turkmenistan") ; In .su domain
1945 ("tn" "Tunisia")
1946 ("tr" "Turkey" "The Republic of %s")
1947 ("tt" "Trinidad and Tobago")
1948 ("tw" "Taiwan")
1949 ("ua" "Ukraine")
1950 ("uk" "United Kingdom" "The %s of Great Britain and Northern Ireland")
1951 ("us" "United States" "The %s of America")
1952 ("uy" "Uruguay" "The Eastern Republic of %s")
1953 ("vc" "St. Vincent and the Grenadines")
1954 ("ve" "Venezuela" "The Republic of %s")
1955 ("vi" "Virgin Islands (U.S.)")
1956 ("vn" "Vietnam")
1957 ("vu" "Vanuatu")
1958 ("yu" "Yugoslavia" "The Socialist Federal Republic of %s")
1959 ("za" "South Africa" "The Republic of %s (or Zambia? Zaire?)")
1960 ("zw" "Zimbabwe" "Republic of %s")
1961 ;; Special top-level domains:
1962 ("arpa" t "Advanced Research Projects Agency (U.S. DoD)")
1963 ("bitnet" t "Because It's Time NET")
1964 ("com" t "Commercial")
1965 ("edu" t "Educational")
1966 ("gov" t "Government (U.S.)")
1967 ("int" t "International (NATO)")
1968 ("mil" t "Military (U.S.)")
1969 ("nato" t "North Atlantic Treaty Organization")
1970 ("net" t "Network")
1971 ("org" t "Non-profit Organization")
1972 ;;("unter-dom" t "? (Ger.)")
1973 ("uucp" t "Unix to Unix CoPy")
1974 ;;("fipnet" nil "?")
1976 ob))
1978 ;;;###autoload
1979 (defun what-domain (domain)
1980 "Convert mail domain DOMAIN to the country it corresponds to."
1981 (interactive
1982 (let ((completion-ignore-case t))
1983 (list (completing-read "Domain: "
1984 mail-extr-all-top-level-domains nil t))))
1985 (or (setq domain (intern-soft (downcase domain)
1986 mail-extr-all-top-level-domains))
1987 (error "No such domain"))
1988 (message "%s: %s" (upcase (symbol-name domain)) (get domain 'domain-name)))
1991 ;(let ((all nil))
1992 ; (mapatoms #'(lambda (x)
1993 ; (if (and (boundp x)
1994 ; (string-match "^mail-extr-" (symbol-name x)))
1995 ; (setq all (cons x all)))))
1996 ; (setq all (sort all #'string-lessp))
1997 ; (cons 'setq
1998 ; (apply 'nconc (mapcar #'(lambda (x)
1999 ; (list x (symbol-value x)))
2000 ; all))))
2003 (provide 'mail-extr)
2005 ;;; mail-extr.el ends here