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>
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)
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; The entry point of this code is
30 ;; mail-extract-address-components: (address)
32 ;; Given an RFC-822 ADDRESS, extract full name and canonical address.
33 ;; Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
34 ;; If no name can be extracted, FULL-NAME will be nil.
35 ;; ADDRESS may be a string or a buffer. If it is a buffer, the visible
36 ;; (narrowed) portion of the buffer will be interpreted as the address.
37 ;; (This feature exists so that the clever caller might be able to avoid
39 ;; If ADDRESS contains more than one RFC-822 address, only the first is
42 ;; This code is more correct (and more heuristic) parser than the code in
43 ;; rfc822.el. And despite its size, it's fairly fast.
45 ;; There are two main benefits:
47 ;; 1. Higher probability of getting the correct full name for a human than
48 ;; any other package we know of. (On the other hand, it will cheerfully
49 ;; mangle non-human names/comments.)
50 ;; 2. Address part is put in a canonical form.
52 ;; The interface is not yet carved in stone; please give us suggestions.
54 ;; We have an extensive test-case collection of funny addresses if you want to
55 ;; work with the code. Developing this code requires frequent testing to
56 ;; make sure you're not breaking functionality. The test cases aren't included
57 ;; because they are over 100K.
59 ;; If you find an address that mail-extr fails on, please send it to the
60 ;; maintainer along with what you think the correct results should be. We do
61 ;; not consider it a bug if mail-extr mangles a comment that does not
62 ;; correspond to a real human full name, although we would prefer that
63 ;; mail-extr would return the comment as-is.
67 ;; * Full name handling:
69 ;; * knows where full names can be found in an address.
70 ;; * avoids using empty comments and quoted text.
71 ;; * extracts full names from mailbox names.
72 ;; * recognizes common formats for comments after a full name.
73 ;; * puts a period and a space after each initial.
74 ;; * understands & referring to the mailbox name, capitalized.
75 ;; * strips name prefixes like "Prof.", etc.
76 ;; * understands what characters can occur in names (not just letters).
77 ;; * figures out middle initial from mailbox name.
78 ;; * removes funny nicknames.
79 ;; * keeps suffixes such as Jr., Sr., III, etc.
80 ;; * reorders "Last, First" type names.
82 ;; * Address handling:
84 ;; * parses rfc822 quoted text, comments, and domain literals.
85 ;; * parses rfc822 multi-line headers.
86 ;; * does something reasonable with rfc822 GROUP addresses.
87 ;; * handles many rfc822 noncompliant and garbage addresses.
88 ;; * canonicalizes addresses (after stripping comments/phrases outside <>).
89 ;; * converts ! addresses into .UUCP and %-style addresses.
90 ;; * converts rfc822 ROUTE addresses to %-style addresses.
91 ;; * truncates %-style addresses at leftmost fully qualified domain name.
92 ;; * handles local relative precedence of ! vs. % and @ (untested).
94 ;; It does almost no string creation. It primarily uses the built-in
95 ;; parsing routines with the appropriate syntax tables. This should
96 ;; result in greater speed.
100 ;; * handle all test cases. (This will take forever.)
101 ;; * software to pick the correct header to use (eg., "Senders-Name:").
102 ;; * multiple addresses in the "From:" header (almost all of the necessary
104 ;; * flag to not treat `,' as an address separator. (This is useful when
105 ;; there is a "From:" header but no "Sender:" header, because then there
106 ;; is only allowed to be one address.)
107 ;; * mailbox name does not necessarily contain full name.
108 ;; * fixing capitalization when it's all upper or lowercase. (Hard!)
109 ;; * some of the domain literal handling is missing. (But I've never even
110 ;; seen one of these in a mail address, so maybe no big deal.)
111 ;; * arrange to have syntax tables byte-compiled.
113 ;; * delete unused variables.
114 ;; * arrange for testing with different relative precedences of ! vs. @
116 ;; * insert documentation strings!
117 ;; * handle X.400-gatewayed addresses according to RFC 1148.
121 ;; Thu Feb 17 17:57:33 1994 Jamie Zawinski (jwz@lucid.com)
123 ;; * merged with jbw's latest version
125 ;; Wed Feb 9 21:56:27 1994 Jamie Zawinski (jwz@lucid.com)
127 ;; * high-bit chars in comments weren't treated as word syntax
129 ;; Sat Feb 5 03:13:40 1994 Jamie Zawinski (jwz@lucid.com)
131 ;; * call replace-match with fixed-case arg
133 ;; Thu Dec 16 21:56:45 1993 Jamie Zawinski (jwz@lucid.com)
135 ;; * some more cleanup, doc, added provide
137 ;; Tue Mar 23 21:23:18 1993 Joe Wells (jbw at csd.bu.edu)
139 ;; * Made mail-full-name-prefixes a user-customizable variable.
140 ;; Allow passing the address as a buffer as well as as a string.
141 ;; Allow [ and ] as name characters (Finnish character set).
143 ;; Mon Mar 22 21:20:56 1993 Joe Wells (jbw at bigbird.bu.edu)
145 ;; * Handle "null" addresses. Handle = used for spacing in mailbox
146 ;; name. Fix bug in handling of ROUTE-ADDR-type addresses that are
147 ;; missing their brackets. Handle uppercase "JR". Extract full
148 ;; names from X.400 addresses encoded in RFC-822. Fix bug in
149 ;; handling of multiple addresses where first has trailing comment.
150 ;; Handle more kinds of telephone extension lead-ins.
152 ;; Mon Mar 22 20:16:57 1993 Joe Wells (jbw at bigbird.bu.edu)
154 ;; * Handle HZ encoding for embedding GB encoded chinese characters.
156 ;; Mon Mar 22 00:46:12 1993 Joe Wells (jbw at bigbird.bu.edu)
158 ;; * Fixed too broad matching of ham radio call signs. Fixed bug in
159 ;; handling an unmatched ' in a name string. Enhanced recognition
160 ;; of when . in the mailbox name terminates the name portion.
161 ;; Narrowed conversion of . to space to only the necessary
162 ;; situation. Deal with VMS's stupid date stamps. Handle a unique
163 ;; way of introducing an alternate address. Fixed spacing bug I
164 ;; introduced in switching last name order. Fixed bug in handling
165 ;; address with ! and % but no @. Narrowed the cases in which
166 ;; certain trailing words are discarded.
168 ;; Sun Mar 21 21:41:06 1993 Joe Wells (jbw at bigbird.bu.edu)
170 ;; * Fixed bugs in handling GROUP addresses. Certain words in the
171 ;; middle of a name no longer terminate it. Handle LISTSERV list
172 ;; names. Ignore comment field containing mailbox name.
174 ;; Sun Mar 21 14:39:38 1993 Joe Wells (jbw at bigbird.bu.edu)
176 ;; * Moved variant-method code back into main function. Handle
177 ;; underscores as spaces in comments. Handle leading nickname. Add
178 ;; flag to ignore single-word names. Other changes.
180 ;; Mon Feb 1 22:23:31 1993 Joe Wells (jbw at bigbird.bu.edu)
182 ;; * Added in changes by Rod Whitby and Jamie Zawinski. This
183 ;; includes the flag mail-extr-guess-middle-initial and the fix for
184 ;; handling multiple addresses correctly. (Whitby just changed
187 ;; Mon Apr 6 23:59:09 1992 Joe Wells (jbw at bigbird.bu.edu)
189 ;; * Cleaned up some more. Release version 1.0 to world.
191 ;; Sun Apr 5 19:39:08 1992 Joe Wells (jbw at bigbird.bu.edu)
193 ;; * Cleaned up full name extraction extensively.
195 ;; Sun Feb 2 14:45:24 1992 Joe Wells (jbw at bigbird.bu.edu)
197 ;; * Total rewrite. Integrated mail-canonicalize-address into
198 ;; mail-extract-address-components. Now handles GROUP addresses more
199 ;; or less correctly. Better handling of lots of different cases.
201 ;; Fri Jun 14 19:39:50 1991
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 ;; User configuration variable definitions.
212 (defvar mail-extr-guess-middle-initial nil
213 "*Whether to try to guess middle initial from mail address.
214 If true, then when we see an address like \"John Smith <jqs@host.com>\"
215 we will assume that \"John Q. Smith\" is the fellow's name.")
217 (defvar mail-extr-ignore-single-names t
218 "*Whether to ignore a name that is just a single word.
219 If true, then when we see an address like \"Idiot <dumb@stupid.com>\"
220 we will act as though we couldn't find a full name in the address.")
222 ;; Matches a leading title that is not part of the name (does not
223 ;; contribute to uniquely identifying the person).
224 (defvar mail-extr-full-name-prefixes
226 "\\(Prof\\|D[Rr]\\|Mrs?\\|Rev\\|Rabbi\\|SysOp\\|LCDR\\)\\.?[ \t\n]")
227 "*Matches prefixes to the full name that identify a person's position.
228 These are stripped from the full name because they do not contribute to
229 uniquely identifying the person.")
231 (defvar mail-extr-@-binds-tighter-than-! nil
232 "*Whether the local mail transport agent looks at ! before @.")
234 (defvar mail-extr-mangle-uucp nil
235 "*Whether to throw away information in UUCP addresses
236 by translating things like \"foo!bar!baz@host\" into \"baz@bar.UUCP\".")
238 ;;----------------------------------------------------------------------
239 ;; what orderings are meaningful?????
240 ;;(defvar mail-operator-precedence-list '(?! ?% ?@))
241 ;; Right operand of a % or a @ must be a domain name, period. No other
242 ;; operators allowed. Left operand of a @ is an address relative to that
245 ;; Left operand of a ! must be a domain name. Right operand is an
246 ;; arbitrary address.
247 ;;----------------------------------------------------------------------
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;; Constant definitions.
257 ;; Names in ISO 8859-1 Name
258 ;; ISO 10XXX ISO 8859-2 in
259 ;; ISO 6937 ISO 10646 RFC Swedish
260 ;; etc. Hex Oct 1345 TeX Split ASCII Description
261 ;; --------- ---------- ---- --- ----- ----- -------------------------------
262 ;; %a E4 344 a: \"a ae { latin small a + diaeresis d
263 ;; %o F6 366 o: \"o oe | latin small o + diaeresis v
264 ;; @a E5 345 aa \oa aa } latin small a + ring above e
265 ;; %u FC 374 u: \"u ue ~ latin small u + diaeresis |
266 ;; /e E9 351 e' \'e ` latin small e + acute i
267 ;; %A C4 304 A: \"A AE [ latin capital a + diaeresis D
268 ;; %O D6 326 O: \"O OE \ latin capital o + diaeresis V
269 ;; @A C5 305 AA \oA AA ] latin capital a + ring above E
270 ;; %U DC 334 U: \"U UE ^ latin capital u + diaeresis \
271 ;; /E C9 311 E' \'E @ latin capital e + acute I
273 ;; NOTE: @a and @A are not in ISO 8859-2 (the codes mentioned above invoke
274 ;; /l and /L). Some of this data was retrieved from
275 ;; listserv@jhuvm.hcf.jhu.edu.
277 ;; Any character that can occur in a name, not counting characters that
278 ;; separate parts of a multipart name (hyphen and period).
279 ;; Yes, there are weird people with digits in their names.
280 ;; You will also notice the consideration for the
281 ;; Swedish/Finnish/Norwegian character set.
282 ;; #### (go to \376 instead of \377 to work around bug in search.c...)
283 (defconst mail-extr-all-letters-but-separators
284 (purecopy "][A-Za-z{|}'~0-9`\200-\376"))
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"))
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`'."))
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
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
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
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
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
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
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
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
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
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
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).
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.
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..."
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))
499 (let ((syntax-table (symbol-value (car pair))))
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))
509 (syntax (nth 2 item)))
510 (while (<= char bound)
511 (modify-syntax-entry char syntax syntax-table)
512 (setq char (1+ char)))))))
514 '((mail-extr-address-syntax-table
515 (?\000 ?\037 "w") ;control characters
517 (?! ?~ "w") ;printable characters
519 (?\200 ?\377 "w") ;high-bit-on characters
520 (?\240 " ") ;nobreakspace
537 ;; % and ! aren't RFC822 characters, but it is convenient to pretend
539 (?! ".") ;; this needs to be word-constituent when not in .UUCP mode
541 (mail-extr-address-comment-syntax-table
551 (mail-extr-address-domain-literal-syntax-table
561 (mail-extr-address-text-comment-syntax-table
579 (mail-extr-address-text-syntax-table
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)
620 (narrow-to-region (, beg) (, end))
621 (goto-char (point-min))
623 (while (search-forward "\\" nil t)
624 (mail-extr-delete-char -1)
629 (defmacro mail-extr-nuke-char-at (pos)
632 (mail-extr-delete-char 1)
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, evalable at macroexpand-time."))
650 (` (let ((temp (, list-symbol))
654 (cond ((or (> ch (, end-symbol))
655 (< ch (, beg-symbol)))
658 (` ((mail-extr-nuke-char-at ch)))))
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.
667 (let ((temp (marker-position marker)))
668 (set-marker marker nil)
672 (defun mail-extr-markerize (pos)
673 ;; coerces pos to a marker if non-nil.
674 (if (or (markerp pos) (null pos))
678 (defmacro mail-extr-last (list)
679 ;; Returns last element of LIST.
681 (` (let ((list (, list)))
682 (while (not (null (cdr list)))
683 (setq list (cdr 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
690 (goto-char (scan-sexps (point) (, arg)))
693 ;; #### kludge kludge kludge kludge kludge kludge kludge !!!
694 (if (string-equal (nth 1 error) "Unbalanced parentheses")
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
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
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*"))
723 ;; multiple-addresses
724 <-pos >-pos @-pos :-pos ,-pos !-pos %-pos \;-pos
725 group-:-pos group-\;-pos route-addr-:-pos
727 first-real-pos last-real-pos
728 phrase-beg phrase-end
729 cbeg cend ; dynamically set from -voodoo
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
744 (set-buffer extraction-buffer)
746 (kill-all-local-variables)
747 (buffer-disable-undo extraction-buffer)
748 (set-syntax-table mail-extr-address-syntax-table)
751 (setq case-fold-search nil)
753 ;; Insert extra space at beginning to allow later replacement with <
754 ;; without having to move markers.
757 ;; Insert the address itself.
758 (cond ((stringp address)
761 (insert-buffer-substring address))
763 (error "Illegal address: %s" address)))
765 ;; stolen from rfc822.el
766 ;; Unfold multiple lines.
767 (goto-char (point-min))
768 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t)
769 (replace-match "\\1 " t))
771 ;; first pass grabs useful information about address
772 (goto-char (point-min))
774 (mail-extr-skip-whitespace-forward)
776 (setq char (char-after (point)))
778 (if (not (eq char ?\())
779 (setq first-real-pos (point))))
783 (set-syntax-table mail-extr-address-comment-syntax-table)
784 ;; only record the first non-empty comment's position
788 (mail-extr-skip-whitespace-forward)
789 (not (eq ?\) (char-after (point))))))
791 ;; TODO: don't record if unbalanced
792 (or (mail-extr-safe-move-sexp 1)
794 (set-syntax-table mail-extr-address-syntax-table)
797 (setq cend (point))))
800 ;; only record the first non-empty quote's position
801 (if (and (not quote-beg)
804 (mail-extr-skip-whitespace-forward)
805 (not (eq ?\" (char-after (point))))))
806 (setq quote-beg (point)))
807 ;; TODO: don't record if unbalanced
808 (or (mail-extr-safe-move-sexp 1)
812 (setq quote-end (point))))
815 (set-syntax-table mail-extr-address-domain-literal-syntax-table)
816 (or (mail-extr-safe-move-sexp 1)
818 (set-syntax-table mail-extr-address-syntax-table))
819 ;; commas delimit addresses when outside < > pairs.
821 (or (and (null <-pos)
822 ;; Handle ROUTE-ADDR address that is missing its <.
823 (not (eq ?@ (char-after (1+ (point))))))
825 ;; handle weird munged addresses
826 ;; BUG FIX: This test was reversed. Thanks to the
827 ;; brilliant Rod Whitby <rwhitby@research.canon.oz.au>
828 ;; for discovering this!
829 (< (mail-extr-last <-pos) (car >-pos)))))
830 ;; It'd be great if some day this worked, but for now, punt.
831 ;; (setq multiple-addresses t)
832 ;; ;; *** Why do I want this:
833 ;; (mail-extr-delete-char 1)
834 ;; (narrow-to-region (point-min) (point))
835 (delete-region (point) (point-max))
836 (setq char ?\() ; HAVE I NO SHAME??
838 ;; record the position of various interesting chars, determine
840 ((setq record-pos-symbol
842 '((?< . <-pos) (?> . >-pos) (?@ . @-pos)
843 (?: . :-pos) (?, . ,-pos) (?! . !-pos)
844 (?% . %-pos) (?\; . \;-pos)))))
845 (set record-pos-symbol
846 (cons (point) (symbol-value record-pos-symbol)))
851 ;; comment terminator illegal
853 ;; domain literal terminator illegal
855 ;; \ allowed only within quoted strings,
856 ;; domain literals, and comments
859 (mail-extr-nuke-char-at (point))
864 ;; At the end of first address of a multiple address header.
867 (setq last-real-pos (point))))
869 ;; Use only the leftmost <, if any. Replace all others with spaces.
871 (mail-extr-nuke-char-at (car <-pos))
872 (setq <-pos (cdr <-pos)))
874 ;; Use only the rightmost >, if any. Replace all others with spaces.
876 (mail-extr-nuke-char-at (nth 1 >-pos))
877 (setcdr >-pos (nthcdr 2 >-pos)))
879 ;; If multiple @s and a :, but no < and >, insert around buffer.
880 ;; Example: @foo.bar.dom,@xxx.yyy.zzz:mailbox@aaa.bbb.ccc
881 ;; This commonly happens on the UUCP "From " line. Ugh.
882 (cond ((and (> (length @-pos) 1)
883 (eq 1 (length :-pos)) ;TODO: check if between last two @s
886 (goto-char (point-min))
887 (mail-extr-delete-char 1)
888 (setq <-pos (list (point)))
891 ;; If < but no >, insert > in rightmost possible position
894 (goto-char (point-max))
895 (setq >-pos (list (point)))
898 ;; If > but no <, replace > with space.
901 (mail-extr-nuke-char-at (car >-pos))
904 ;; Turn >-pos and <-pos into non-lists
905 (setq >-pos (car >-pos)
908 ;; Trim other punctuation lists of items outside < > pair to handle
910 (cond (<-pos ; don't need to check >-pos also
911 ;; handle bozo software that violates RFC 822 by sticking
912 ;; punctuation marks outside of a < > pair
913 (mail-extr-nuke-outside-range @-pos <-pos >-pos t)
914 ;; RFC 822 says nothing about these two outside < >, but
915 ;; remove those positions from the lists to make things
917 (mail-extr-nuke-outside-range !-pos <-pos >-pos t)
918 (mail-extr-nuke-outside-range %-pos <-pos >-pos t)))
920 ;; Check for : that indicates GROUP list and for : part of
922 ;; Can't possibly be more than two :. Nuke any extra.
924 (setq temp (car :-pos)
926 (cond ((and <-pos >-pos
929 (if (or route-addr-:-pos
932 (< temp (nth 1 @-pos)))
933 (mail-extr-nuke-char-at temp)
934 (setq route-addr-:-pos temp)))
938 (setq group-:-pos temp))))
940 ;; Nuke any ; that is in or to the left of a < > pair or to the left
941 ;; of a GROUP starting :. Also, there may only be one ;.
943 (setq temp (car \;-pos)
945 (cond ((and <-pos >-pos
948 (mail-extr-nuke-char-at temp))
949 ((and (or (not group-:-pos)
950 (> temp group-:-pos))
952 (setq group-\;-pos temp))))
954 ;; Nuke unmatched GROUP syntax characters.
955 (cond ((and group-:-pos (not group-\;-pos))
956 ;; *** Do I really need to erase it?
957 (mail-extr-nuke-char-at group-:-pos)
958 (setq group-:-pos nil)))
959 (cond ((and group-\;-pos (not group-:-pos))
960 ;; *** Do I really need to erase it?
961 (mail-extr-nuke-char-at group-\;-pos)
962 (setq group-\;-pos nil)))
964 ;; Handle junk like ";@host.company.dom" that sendmail adds.
965 ;; **** should I remember comment positions?
968 ;; this is fine for now
969 (mail-extr-nuke-outside-range !-pos group-:-pos group-\;-pos t)
970 (mail-extr-nuke-outside-range @-pos group-:-pos group-\;-pos t)
971 (mail-extr-nuke-outside-range %-pos group-:-pos group-\;-pos t)
972 (mail-extr-nuke-outside-range ,-pos group-:-pos group-\;-pos t)
974 (> last-real-pos (1+ group-\;-pos))
975 (setq last-real-pos (1+ group-\;-pos)))
976 ;; *** This may be wrong:
978 (> cend group-\;-pos)
982 (> quote-end group-\;-pos)
985 ;; This was both wrong and unnecessary:
986 ;;(narrow-to-region (point-min) group-\;-pos)
988 ;; *** The entire handling of GROUP addresses seems rather lame.
989 ;; *** It deserves a complete rethink, except that these addresses
990 ;; *** are hardly ever seen.
993 ;; Any commas must be between < and : of ROUTE-ADDR. Nuke any
995 ;; Hell, go ahead an nuke all of the commas.
996 ;; **** This will cause problems when we start handling commas in
997 ;; the PHRASE part .... no it won't ... yes it will ... ?????
998 (mail-extr-nuke-outside-range ,-pos 1 1)
1000 ;; can only have multiple @s inside < >. The fact that some MTAs
1001 ;; put de-bracketed ROUTE-ADDRs in the UUCP-style "From " line is
1004 ;; Locate PHRASE part of ROUTE-ADDR.
1007 (mail-extr-skip-whitespace-backward)
1008 (setq phrase-end (point))
1009 (goto-char (or ;;group-:-pos
1011 (mail-extr-skip-whitespace-forward)
1012 (if (< (point) phrase-end)
1013 (setq phrase-beg (point))
1014 (setq phrase-end nil))))
1016 ;; handle ROUTE-ADDRS with real ROUTEs.
1017 ;; If there are multiple @s, then we assume ROUTE-ADDR syntax, and
1018 ;; any % or ! must be semantically meaningless.
1019 ;; TODO: do this processing into canonicalization buffer
1020 (cond (route-addr-:-pos
1023 >-pos (copy-marker >-pos)
1024 route-addr-:-pos (copy-marker route-addr-:-pos))
1026 (insert-before-markers ?X)
1027 (goto-char (car @-pos))
1028 (while (setq @-pos (cdr @-pos))
1029 (mail-extr-delete-char 1)
1030 (setq %-pos (cons (point-marker) %-pos))
1032 (goto-char (1- >-pos))
1034 (insert-buffer-substring extraction-buffer
1035 (car @-pos) route-addr-:-pos)
1036 (delete-region (car @-pos) route-addr-:-pos))
1038 (setq saved-@-pos (list (point)))))
1039 (setq @-pos saved-@-pos)
1041 (mail-extr-delete-char -1)
1042 (mail-extr-nuke-char-at route-addr-:-pos)
1043 (mail-extr-demarkerize route-addr-:-pos)
1044 (setq route-addr-:-pos nil
1045 >-pos (mail-extr-demarkerize >-pos)
1046 %-pos (mapcar 'mail-extr-demarkerize %-pos))))
1049 (setq @-pos (car @-pos))
1051 ;; TODO: remove comments in the middle of an address
1053 (set-buffer canonicalization-buffer)
1055 (kill-all-local-variables)
1056 (buffer-disable-undo canonicalization-buffer)
1057 (set-syntax-table mail-extr-address-syntax-table)
1058 (setq case-fold-search nil)
1062 (insert-buffer-substring extraction-buffer)
1065 (narrow-to-region (progn
1066 (goto-char (1+ <-pos))
1067 (mail-extr-skip-whitespace-forward)
1070 (if (and first-real-pos last-real-pos)
1071 (narrow-to-region first-real-pos last-real-pos)
1072 ;; ****** Oh no! What if the address is completely empty!
1073 ;; *** Is this correct?
1074 (narrow-to-region (point-max) (point-max))
1078 (mail-extr-nuke-outside-range %-pos (point-min) @-pos))
1080 (mail-extr-nuke-outside-range !-pos (point-min) (car %-pos)))
1081 (and @-pos !-pos (not %-pos)
1082 (mail-extr-nuke-outside-range !-pos (point-min) @-pos))
1084 ;; Error condition:?? (and %-pos (not @-pos))
1086 ;; WARNING: THIS CODE IS DUPLICATED BELOW.
1089 (goto-char (car %-pos))
1090 (mail-extr-delete-char 1)
1091 (setq @-pos (point))
1093 (setq %-pos (cdr %-pos))))
1095 (if mail-extr-mangle-uucp
1097 ;; **** I don't understand this save-restriction and the
1098 ;; narrow-to-region inside it. Why did I do that?
1101 mail-extr-@-binds-tighter-than-!)
1103 (setq %-pos (cons (point) %-pos)
1105 (mail-extr-delete-char 1)
1107 (setq insert-point (point-max)))
1108 (mail-extr-@-binds-tighter-than-!
1109 (setq insert-point (point-max)))
1111 (setq insert-point (mail-extr-last %-pos)
1112 saved-%-pos (mapcar 'mail-extr-markerize %-pos)
1114 @-pos (mail-extr-markerize @-pos)))
1116 (setq insert-point @-pos)
1117 (setq @-pos (mail-extr-markerize @-pos)))
1119 (setq insert-point (point-max))))
1120 (narrow-to-region (point-min) insert-point)
1121 (setq saved-!-pos (car !-pos))
1123 (goto-char (point-max))
1124 (cond ((and (not @-pos)
1126 (setq @-pos (point))
1127 (insert-before-markers "@ "))
1129 (setq %-pos (cons (point) %-pos))
1130 (insert-before-markers "% ")))
1132 (insert-buffer-substring
1138 (mail-extr-delete-char 1)
1140 (mail-extr-safe-move-sexp -1)
1141 (mail-extr-skip-whitespace-backward)
1142 (eq ?. (preceding-char)))
1143 (insert-before-markers
1145 (mail-extr-skip-whitespace-backward)
1146 (eq ?. (preceding-char)))
1150 (setq !-pos (cdr !-pos))))
1152 (setq %-pos (append (mapcar 'mail-extr-demarkerize
1155 (setq @-pos (mail-extr-demarkerize @-pos))
1156 (narrow-to-region (1+ saved-!-pos) (point-max)))))
1158 ;; WARNING: THIS CODE IS DUPLICATED ABOVE.
1161 (goto-char (car %-pos))
1162 (mail-extr-delete-char 1)
1163 (setq @-pos (point))
1165 (setq %-pos (cdr %-pos))))
1167 (setq %-pos (nreverse %-pos))
1168 ;; RFC 1034 doesn't approve of this, oh well:
1169 (downcase-region (or (car %-pos) @-pos (point-max)) (point-max))
1170 (cond (%-pos ; implies @-pos valid
1174 (goto-char (or (nth 1 temp)
1176 (mail-extr-skip-whitespace-backward)
1178 (mail-extr-safe-move-sexp -1)
1179 (setq domain-pos (point))
1180 (mail-extr-skip-whitespace-backward)
1181 (setq \.-pos (eq ?. (preceding-char))))
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))
1193 (setq %-pos (delq @-pos %-pos))
1195 (throw 'truncated t)))
1196 (setq temp (cdr temp))))))
1197 (setq mbox-beg (point-min)
1198 mbox-end (if %-pos (car %-pos)
1202 ;; Done canonicalizing address.
1204 (set-buffer extraction-buffer)
1206 ;; Decide what part of the address to search to find the full name.
1208 ;; Example: "First M. Last" <fml@foo.bar.dom>
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>
1217 (narrow-to-region phrase-beg phrase-end))
1219 ;; Example: fml@foo.bar.dom (First M. Last)
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
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
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)))
1262 (mail-extr-skip-whitespace-forward)
1264 (setq char (char-after (point)))
1267 (setq quote-beg (point))
1268 (or (mail-extr-safe-move-sexp 1)
1269 ;; TODO: handle this error condition!!!!!
1271 ;; take into account deletions
1272 (setq quote-end (- (point) 2))
1275 (mail-extr-delete-char 1)
1276 (goto-char quote-beg)
1277 (mail-extr-delete-char 1))
1278 (mail-extr-undo-backslash-quoting quote-beg quote-end)
1279 (or (eq ?\ (char-after (point)))
1281 ;; (setq mailbox-name-processed-flag t)
1282 (setq \.-ends-name t))
1284 (if (memq (char-after (1+ (point))) '(?_ ?=))
1287 (mail-extr-delete-char 1)
1290 (narrow-to-region (point-min) (point))
1291 (mail-extr-delete-char 1)
1293 ;; (setq mailbox-name-processed-flag t)
1295 ((memq (char-syntax char) '(?. ?\\))
1296 (mail-extr-delete-char 1)
1298 ;; (setq mailbox-name-processed-flag t)
1301 (setq atom-beg (point))
1303 (setq atom-end (point))
1304 (goto-char atom-beg)
1306 (narrow-to-region atom-beg atom-end)
1309 ;; Handle X.400 addresses encoded in RFC-822.
1310 ;; *** Shit! This has to handle the case where it is
1311 ;; *** embedded in a quote too!
1312 ;; *** Shit! The input is being broken up into atoms
1314 ((looking-at mail-extr-x400-encoded-address-pattern)
1316 ;; Copy the contents of the individual fields that
1317 ;; might hold name data to the beginning.
1320 (lambda (field-pattern)
1323 (re-search-forward field-pattern nil t))
1324 (insert-buffer-substring (current-buffer)
1328 (list mail-extr-x400-encoded-address-given-name-pattern
1329 mail-extr-x400-encoded-address-surname-pattern
1330 mail-extr-x400-encoded-address-full-name-pattern))
1332 ;; Discard the rest, since it contains stuff like
1333 ;; routing information, not part of a name.
1334 (mail-extr-skip-whitespace-backward)
1335 (delete-region (point) (point-max))
1337 ;; Handle periods used for spacing.
1338 (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1339 (replace-match "\\1 \\2" t))
1341 ;; (setq mailbox-name-processed-flag t)
1344 ;; Handle normal addresses.
1346 (goto-char (point-min))
1347 ;; Handle _ and = used for spacing.
1348 (while (re-search-forward "\\([^_=]+\\)[_=]" nil t)
1349 (replace-match "\\1 " t)
1350 ;; (setq mailbox-name-processed-flag t)
1352 (goto-char (point-max))))))))
1354 ;; undo the dirty deed
1355 (if (not mail-extr-mangle-uucp)
1356 (modify-syntax-entry ?! "." (syntax-table)))
1358 ;; If we derived the name from the mailbox part of the address,
1359 ;; and we only got one word out of it, don't treat that as a
1360 ;; name. "foo@bar" --> (nil "foo@bar"), not ("foo" "foo@bar")
1361 ;; (if (not mailbox-name-processed-flag)
1362 ;; (delete-region (point-min) (point-max)))
1365 (set-syntax-table mail-extr-address-text-syntax-table)
1367 (mail-extr-voodoo mbox-beg mbox-end canonicalization-buffer)
1368 (goto-char (point-min))
1370 ;; If name is "First Last" and userid is "F?L", then assume
1371 ;; the middle initial is the second letter in the userid.
1372 ;; Initial code by Jamie Zawinski <jwz@lucid.com>
1373 ;; *** Make it work when there's a suffix as well.
1374 (goto-char (point-min))
1375 (cond ((and mail-extr-guess-middle-initial
1376 (not disable-initial-guessing-flag)
1377 (eq 3 (- mbox-end mbox-beg))
1379 (goto-char (point-min))
1380 (looking-at mail-extr-two-name-pattern)))
1381 (setq fi (char-after (match-beginning 0))
1382 li (char-after (match-beginning 3)))
1384 (set-buffer canonicalization-buffer)
1385 ;; char-equal is ignoring case here, so no need to upcase
1387 (let ((case-fold-search t))
1388 (and (char-equal fi (char-after mbox-beg))
1389 (char-equal li (char-after (1- mbox-end)))
1390 (setq mi (char-after (1+ mbox-beg))))))
1392 ;; TODO: use better table than syntax table
1393 (eq ?w (char-syntax mi)))
1394 (goto-char (match-beginning 3))
1395 (insert (upcase mi) ". ")))))
1397 ;; Nuke name if it is the same as mailbox name.
1398 (let ((buffer-length (- (point-max) (point-min)))
1400 (names-match-flag t))
1401 (cond ((and (> buffer-length 0)
1402 (eq buffer-length (- mbox-end mbox-beg)))
1403 (goto-char (point-max))
1404 (insert-buffer-substring canonicalization-buffer
1406 (while (and names-match-flag
1407 (< i buffer-length))
1408 (or (eq (downcase (char-after (+ i (point-min))))
1410 (char-after (+ i buffer-length (point-min)))))
1411 (setq names-match-flag nil))
1413 (delete-region (+ (point-min) buffer-length) (point-max))
1414 (if names-match-flag
1415 (narrow-to-region (point) (point))))))
1417 ;; Nuke name if it's just one word.
1418 (goto-char (point-min))
1419 (and mail-extr-ignore-single-names
1420 (not (re-search-forward "[- ]" nil t))
1421 (narrow-to-region (point) (point)))
1424 (list (if (not (= (point-min) (point-max)))
1427 (set-buffer canonicalization-buffer)
1428 (if (not (= (point-min) (point-max)))
1432 (defun mail-extr-voodoo (mbox-beg mbox-end canonicalization-buffer)
1433 (let ((word-count 0)
1434 (case-fold-search nil)
1435 mixed-case-flag lower-case-flag ;;upper-case-flag
1436 suffix-flag last-name-comma-flag
1440 drop-this-word-if-trailing-flag
1441 drop-last-word-if-trailing-flag
1443 this-word-beg last-word-beg
1448 (set-syntax-table mail-extr-address-text-syntax-table)
1450 ;; This was moved above.
1451 ;; Fix . used as space
1452 ;; But it belongs here because it occurs not only as
1453 ;; rypens@reks.uia.ac.be (Piet.Rypens)
1455 ;; "Piet.Rypens" <rypens@reks.uia.ac.be>
1456 ;;(goto-char (point-min))
1457 ;;(while (re-search-forward mail-extr-bad-dot-pattern nil t)
1458 ;; (replace-match "\\1 \\2" t))
1460 (cond ((not (search-forward " " nil t))
1461 (goto-char (point-min))
1462 (cond ((search-forward "_" nil t)
1463 ;; Handle the *idiotic* use of underlines as spaces.
1464 ;; Example: fml@foo.bar.dom (First_M._Last)
1465 (goto-char (point-min))
1466 (while (search-forward "_" nil t)
1467 (replace-match " " t)))
1468 ((search-forward "." nil t)
1469 ;; Fix . used as space
1470 ;; Example: danj1@cb.att.com (daniel.jacobson)
1471 (goto-char (point-min))
1472 (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1473 (replace-match "\\1 \\2" t))))))
1476 ;; Loop over the words (and other junk) in the name.
1477 (goto-char (point-min))
1478 (while (not name-done-flag)
1480 (cond (word-found-flag
1481 ;; Last time through this loop we skipped over a word.
1482 (setq last-word-beg this-word-beg)
1483 (setq drop-last-word-if-trailing-flag
1484 drop-this-word-if-trailing-flag)
1485 (setq word-found-flag nil)))
1487 (cond (begin-again-flag
1488 ;; Last time through the loop we found something that
1489 ;; indicates we should pretend we are beginning again from
1492 (setq last-word-beg nil)
1493 (setq drop-last-word-if-trailing-flag nil)
1494 (setq mixed-case-flag nil)
1495 (setq lower-case-flag nil)
1496 ;; (setq upper-case-flag nil)
1497 (setq begin-again-flag nil)
1500 ;; Initialize for this iteration of the loop.
1501 (mail-extr-skip-whitespace-forward)
1502 (if (eq word-count 0) (narrow-to-region (point) (point-max)))
1503 (setq this-word-beg (point))
1504 (setq drop-this-word-if-trailing-flag nil)
1506 ;; Decide what to do based on what we are looking at.
1510 ((and (eq word-count 0)
1511 (looking-at mail-extr-full-name-prefixes))
1512 (goto-char (match-end 0))
1513 (narrow-to-region (point) (point-max)))
1515 ;; Stop after name suffix
1516 ((and (>= word-count 2)
1517 (looking-at mail-extr-full-name-suffix-pattern))
1518 (mail-extr-skip-whitespace-backward)
1519 (setq suffix-flag (point))
1520 (if (eq ?, (following-char))
1523 ;; Enforce at least one space after comma
1524 (or (eq ?\ (following-char))
1526 (mail-extr-skip-whitespace-forward)
1527 (cond ((memq (following-char) '(?j ?J ?s ?S))
1529 (if (eq (following-char) ?.)
1534 (setq word-found-flag t)
1535 (setq name-done-flag t))
1538 ((looking-at "MKA \\(.+\\)") ; "Mundanely Known As"
1539 (goto-char (match-beginning 1))
1540 (narrow-to-region (point) (point-max))
1541 (setq begin-again-flag t))
1543 ;; Check for initial last name followed by comma
1544 ((and (eq ?, (following-char))
1547 (setq last-name-comma-flag t)
1548 (or (eq ?\ (following-char))
1551 ;; Stop before trailing comma-separated comment
1552 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1553 ;; *** This case is redundant???
1554 ;;((eq ?, (following-char))
1555 ;; (setq name-done-flag t))
1557 ;; Delete parenthesized/quoted comment/nickname
1558 ((memq (following-char) '(?\( ?\{ ?\[ ?\" ?\' ?\`))
1560 (set-syntax-table mail-extr-address-text-comment-syntax-table)
1561 (cond ((memq (following-char) '(?\' ?\`))
1562 (or (search-forward "'" nil t
1563 (if (eq ?\' (following-char)) 2 1))
1564 (mail-extr-delete-char 1)))
1566 (or (mail-extr-safe-move-sexp 1)
1567 (goto-char (point-max)))))
1568 (set-syntax-table mail-extr-address-text-syntax-table)
1571 ;; Handle case of entire name being quoted
1572 ((and (eq word-count 0)
1573 (looking-at " *\\'")
1574 (>= (- cend cbeg) 2))
1575 (narrow-to-region (1+ cbeg) (1- cend))
1576 (goto-char (point-min)))
1578 ;; Handle case of quoted initial
1579 (if (and (or (= 3 (- cend cbeg))
1580 (and (= 4 (- cend cbeg))
1581 (eq ?. (char-after (+ 2 cbeg)))))
1582 (not (looking-at " *\\'")))
1583 (setq initial (char-after (1+ cbeg)))
1585 (delete-region cbeg cend)
1587 (insert initial ". ")))))
1589 ;; Handle & substitution
1591 (eq ?\ (preceding-char)))
1592 (looking-at "&\\( \\|\\'\\)"))
1593 (mail-extr-delete-char 1)
1597 (insert-buffer-substring canonicalization-buffer
1600 (setq disable-initial-guessing-flag t)
1601 (setq word-found-flag t))
1603 ;; Handle *Stupid* VMS date stamps
1604 ((looking-at mail-extr-stupid-vms-date-stamp-pattern)
1605 (replace-match "" t))
1607 ;; Handle Chinese characters.
1608 ((looking-at mail-extr-hz-embedded-gb-encoded-chinese-pattern)
1609 (goto-char (match-end 0))
1610 (setq word-found-flag t))
1612 ;; Skip initial garbage characters.
1613 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1614 ((and (eq word-count 0)
1615 (looking-at mail-extr-leading-garbage))
1616 (goto-char (match-end 0))
1617 ;; *** Skip backward over these???
1618 ;; (skip-chars-backward "& \"")
1619 (narrow-to-region (point) (point-max)))
1621 ;; Various stopping points
1624 ;; Stop before ALL CAPS acronyms, if preceded by mixed-case
1625 ;; words. Example: XT-DEM.
1626 (and (>= word-count 2)
1628 (looking-at mail-extr-weird-acronym-pattern)
1629 (not (looking-at mail-extr-roman-numeral-pattern)))
1631 ;; Stop before trailing alternative address
1632 (looking-at mail-extr-alternative-address-pattern)
1634 ;; Stop before trailing comment not introduced by comma
1635 ;; THIS CASE MUST BE AFTER AN EARLIER CASE.
1636 (looking-at mail-extr-trailing-comment-start-pattern)
1638 ;; Stop before telephone numbers
1639 (looking-at mail-extr-telephone-extension-pattern))
1640 (setq name-done-flag t))
1642 ;; Delete ham radio call signs
1643 ((looking-at mail-extr-ham-call-sign-pattern)
1644 (delete-region (match-beginning 0) (match-end 0)))
1647 ((looking-at mail-extr-initial-pattern)
1648 (or (eq (following-char) (upcase (following-char)))
1649 (setq lower-case-flag t))
1651 (if (eq ?. (following-char))
1654 (or (eq ?\ (following-char))
1656 (setq word-found-flag t))
1658 ;; Handle BITNET LISTSERV list names.
1659 ((and (eq word-count 0)
1660 (looking-at mail-extr-listserv-list-name-pattern))
1661 (narrow-to-region (match-beginning 1) (match-end 1))
1662 (setq word-found-flag t)
1663 (setq name-done-flag t))
1665 ;; Regular name words
1666 ((looking-at mail-extr-name-pattern)
1667 (setq name-beg (point))
1668 (setq name-end (match-end 0))
1670 ;; Certain words will be dropped if they are at the end.
1671 (and (>= word-count 2)
1672 (not lower-case-flag)
1674 ;; A trailing 4-or-more letter lowercase words preceded by
1675 ;; mixed case or uppercase words will be dropped.
1676 (looking-at "[a-z][a-z][a-z][a-z]+[ \t]*\\'")
1677 ;; Drop a trailing word which is terminated with a period.
1678 (eq ?. (char-after (1- name-end))))
1679 (setq drop-this-word-if-trailing-flag t))
1681 ;; Set the flags that indicate whether we have seen a lowercase
1682 ;; word, a mixed case word, and an uppercase word.
1683 (if (re-search-forward "[a-z]" name-end t)
1685 (goto-char name-beg)
1686 (re-search-forward "[A-Z]" name-end t))
1687 (setq mixed-case-flag t)
1688 (setq lower-case-flag t))
1689 ;; (setq upper-case-flag t)
1692 (goto-char name-end)
1693 (setq word-found-flag t))
1696 (setq name-done-flag t)
1699 ;; Count any word that we skipped over.
1701 (setq word-count (1+ word-count))))
1703 ;; If the last thing in the name is 2 or more periods, or one or more
1704 ;; other sentence terminators (but not a single period) then keep them
1705 ;; and the preceeding word. This is for the benefit of whole sentences
1706 ;; in the name field: it's better behavior than dropping the last word
1707 ;; of the sentence...
1708 (if (and (not suffix-flag)
1709 (looking-at "\\(\\.+\\|[?!;:.][?!;:.]+\\|[?!;:][?!;:.]*\\)\\'"))
1710 (goto-char (setq suffix-flag (point-max))))
1712 ;; Drop everything after point and certain trailing words.
1713 (narrow-to-region (point-min)
1714 (or (and drop-last-word-if-trailing-flag
1718 ;; Xerox's mailers SUCK!!!!!!
1719 ;; We simply refuse to believe that any last name is PARC or ADOC.
1720 ;; If it looks like that is the last name, that there is no meaningful
1721 ;; here at all. Actually I guess it would be best to map patterns
1722 ;; like foo.hoser@xerox.com into foo@hoser.xerox.com, but I don't
1723 ;; actually know that that is what's going on.
1724 (cond ((not suffix-flag)
1725 (goto-char (point-min))
1726 (let ((case-fold-search t))
1727 (if (looking-at "[-A-Za-z_]+[. ]\\(PARC\\|ADOC\\)\\'")
1730 ;; If last name first put it at end (but before suffix)
1731 (cond (last-name-comma-flag
1732 (goto-char (point-min))
1733 (search-forward ",")
1734 (setq name-end (1- (point)))
1735 (goto-char (or suffix-flag (point-max)))
1736 (or (eq ?\ (preceding-char))
1738 (insert-buffer-substring (current-buffer) (point-min) name-end)
1739 (goto-char name-end)
1740 (skip-chars-forward "\t ,")
1741 (narrow-to-region (point) (point-max))))
1743 ;; Delete leading and trailing junk characters.
1744 ;; *** This is probably completly unneeded now.
1745 ;;(goto-char (point-max))
1746 ;;(skip-chars-backward mail-extr-non-end-name-chars)
1747 ;;(if (eq ?. (following-char))
1748 ;; (forward-char 1))
1749 ;;(narrow-to-region (point)
1751 ;; (goto-char (point-min))
1752 ;; (skip-chars-forward mail-extr-non-begin-name-chars)
1755 ;; Compress whitespace
1756 (goto-char (point-min))
1757 (while (re-search-forward "[ \t\n]+" nil t)
1758 (replace-match (if (eobp) "" " ") t))
1763 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1765 ;; Table of top-level domain names.
1767 ;; This is used during address canonicalization; be careful of format changes.
1768 ;; Keep in mind that the country abbreviations follow ISO-3166. There is
1769 ;; a U.S. FIPS that specifies a different set of two-letter country
1772 (defconst mail-extr-all-top-level-domains
1773 (let ((ob (make-vector 509 0)))
1777 (put (intern (downcase (car x)) ob)
1780 (format (nth 2 x) (nth 1 x))
1783 ("ar" "Argentina" "Argentine Republic")
1784 ("arpa" t "Advanced Projects Research Agency")
1785 ("at" "Austria" "The Republic of %s")
1788 ("be" "Belgium" "The Kingdom of %s")
1790 ("bitnet" t "Because It's Time NET")
1791 ("bo" "Bolivia" "Republic of %s")
1792 ("br" "Brazil" "The Federative Republic of %s")
1796 ("ch" "Switzerland" "The Swiss Confederation")
1797 ("cl" "Chile" "The Republic of %s")
1798 ("cn" "China" "The People's Republic of %s")
1800 ("com" t "Commercial organizations (U.S.A.)")
1801 ("cr" "Costa Rica" "The Republic of %s")
1802 ("cs" "Czechoslovakia")
1806 ("do" "Dominican Republic" "The %s")
1807 ("ec" "Ecuador" "The Republic of %s")
1808 ("edu" t "Educational institutions (U.S.A.)")
1809 ("eg" "Egypt" "The Arab Republic of %s")
1810 ("es" "Spain" "The Kingdom of %s")
1811 ("fi" "Finland" "The Republic of %s")
1814 ("gov" t "Government (U.S.A.)")
1815 ("gr" "Greece" "The Hellenic Republic (%s)")
1817 ("hu" "Hungary" "The Hungarian People's Republic") ;???
1819 ("il" "Israel" "The State of %s")
1820 ("in" "India" "The Republic of %s")
1821 ("int" t "(something British, don't know what)")
1822 ("is" "Iceland" "The Republic of %s")
1823 ("it" "Italy" "The Italian Republic")
1826 ("kn" "St. Kitts and Nevis")
1827 ("kr" "South Korea")
1829 ("lk" "Sri Lanka" "The Democratic Socialist Republic of %s")
1830 ("mil" t "Military (U.S.A.)")
1831 ("mx" "Mexico" "The United Mexican States")
1832 ("my" "Malaysia" "%s (changed to Myanmar?)") ;???
1834 ("nato" t "North Atlantic Treaty Organization")
1836 ("ni" "Nicaragua" "The Republic of %s")
1837 ("nl" "Netherlands" "The Kingdom of the %s")
1838 ("no" "Norway" "The Kingdom of %s")
1839 ("nz" "New Zealand")
1840 ("org" t "Non-commercial organizations (U.S.A.)")
1842 ("pg" "Papua New Guinea")
1843 ("ph" "Philippines" "The Republic of the %s")
1845 ("pr" "Puerto Rico")
1846 ("pt" "Portugal" "The Portugese Republic")
1848 ("se" "Sweden" "The Kingdom of %s")
1849 ("sg" "Singapore" "The Republic of %s")
1851 ("su" "Soviet Union")
1852 ("th" "Thailand" "The Kingdom of %s")
1854 ("tr" "Turkey" "The Republic of %s")
1855 ("tt" "Trinidad and Tobago")
1857 ("uk" "United Kingdom" "The %s of Great Britain and Northern Ireland")
1858 ("unter-dom" t "(something German)")
1859 ("us" "U.S.A." "The United States of America")
1860 ("uucp" t "Unix to Unix CoPy")
1861 ("uy" "Uruguay" "The Eastern Republic of %s")
1862 ("vc" "St. Vincent and the Grenadines")
1863 ("ve" "Venezuela" "The Republic of %s")
1864 ("yu" "Yugoslavia" "The Socialist Federal Republic of %s")
1865 ;; Also said to be Zambia ... (why not Zaire???)
1866 ("za" "South Africa" "The Republic of %s (or Zambia? Zaire?)")
1867 ("zw" "Zimbabwe" "Republic of %s")
1873 (defun what-domain (domain)
1874 "Convert mail domain DOMAIN to the country it corresponds to."
1876 (let ((completion-ignore-case t))
1877 (list (completing-read "Domain: "
1878 mail-extr-all-top-level-domains nil t))))
1879 (or (setq domain (intern-soft (downcase domain)
1880 mail-extr-all-top-level-domains))
1881 (error "no such domain"))
1882 (message "%s: %s" (upcase (symbol-name domain)) (get domain 'domain-name)))
1886 ; (mapatoms #'(lambda (x)
1887 ; (if (and (boundp x)
1888 ; (string-match "^mail-extr-" (symbol-name x)))
1889 ; (setq all (cons x all)))))
1890 ; (setq all (sort all #'string-lessp))
1892 ; (apply 'nconc (mapcar #'(lambda (x)
1893 ; (list x (symbol-value x)))
1897 (provide 'mail-extr)
1899 ;;; mail-extr.el ends here