1 ;;; mail-extr.el --- extract full name and address from RFC 822 mail header.
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@cs.bu.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The entry point of this code is
30 ;; mail-extract-address-components: (address &optional all)
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 ;; If ALL is non-nil, that means return info about all the addresses
43 ;; that are found in ADDRESS. The value is a list of elements of
44 ;; the form (FULL-NAME CANONICAL-ADDRESS), one per address.
46 ;; This code is more correct (and more heuristic) parser than the code in
47 ;; rfc822.el. And despite its size, it's fairly fast.
49 ;; There are two main benefits:
51 ;; 1. Higher probability of getting the correct full name for a human than
52 ;; any other package we know of. (On the other hand, it will cheerfully
53 ;; mangle non-human names/comments.)
54 ;; 2. Address part is put in a canonical form.
56 ;; The interface is not yet carved in stone; please give us suggestions.
58 ;; We have an extensive test-case collection of funny addresses if you want to
59 ;; work with the code. Developing this code requires frequent testing to
60 ;; make sure you're not breaking functionality. The test cases aren't included
61 ;; because they are over 100K.
63 ;; If you find an address that mail-extr fails on, please send it to the
64 ;; maintainer along with what you think the correct results should be. We do
65 ;; not consider it a bug if mail-extr mangles a comment that does not
66 ;; correspond to a real human full name, although we would prefer that
67 ;; mail-extr would return the comment as-is.
71 ;; * Full name handling:
73 ;; * knows where full names can be found in an address.
74 ;; * avoids using empty comments and quoted text.
75 ;; * extracts full names from mailbox names.
76 ;; * recognizes common formats for comments after a full name.
77 ;; * puts a period and a space after each initial.
78 ;; * understands & referring to the mailbox name, capitalized.
79 ;; * strips name prefixes like "Prof.", etc.
80 ;; * understands what characters can occur in names (not just letters).
81 ;; * figures out middle initial from mailbox name.
82 ;; * removes funny nicknames.
83 ;; * keeps suffixes such as Jr., Sr., III, etc.
84 ;; * reorders "Last, First" type names.
86 ;; * Address handling:
88 ;; * parses rfc822 quoted text, comments, and domain literals.
89 ;; * parses rfc822 multi-line headers.
90 ;; * does something reasonable with rfc822 GROUP addresses.
91 ;; * handles many rfc822 noncompliant and garbage addresses.
92 ;; * canonicalizes addresses (after stripping comments/phrases outside <>).
93 ;; * converts ! addresses into .UUCP and %-style addresses.
94 ;; * converts rfc822 ROUTE addresses to %-style addresses.
95 ;; * truncates %-style addresses at leftmost fully qualified domain name.
96 ;; * handles local relative precedence of ! vs. % and @ (untested).
98 ;; It does almost no string creation. It primarily uses the built-in
99 ;; parsing routines with the appropriate syntax tables. This should
100 ;; result in greater speed.
104 ;; * handle all test cases. (This will take forever.)
105 ;; * software to pick the correct header to use (eg., "Senders-Name:").
106 ;; * multiple addresses in the "From:" header (almost all of the necessary
108 ;; * flag to not treat `,' as an address separator. (This is useful when
109 ;; there is a "From:" header but no "Sender:" header, because then there
110 ;; is only allowed to be one address.)
111 ;; * mailbox name does not necessarily contain full name.
112 ;; * fixing capitalization when it's all upper or lowercase. (Hard!)
113 ;; * some of the domain literal handling is missing. (But I've never even
114 ;; seen one of these in a mail address, so maybe no big deal.)
115 ;; * arrange to have syntax tables byte-compiled.
117 ;; * delete unused variables.
118 ;; * arrange for testing with different relative precedences of ! vs. @
120 ;; * insert documentation strings!
121 ;; * handle X.400-gatewayed addresses according to RFC 1148.
125 ;; Thu Feb 17 17:57:33 1994 Jamie Zawinski (jwz@lucid.com)
127 ;; * merged with jbw's latest version
129 ;; Wed Feb 9 21:56:27 1994 Jamie Zawinski (jwz@lucid.com)
131 ;; * high-bit chars in comments weren't treated as word syntax
133 ;; Sat Feb 5 03:13:40 1994 Jamie Zawinski (jwz@lucid.com)
135 ;; * call replace-match with fixed-case arg
137 ;; Thu Dec 16 21:56:45 1993 Jamie Zawinski (jwz@lucid.com)
139 ;; * some more cleanup, doc, added provide
141 ;; Tue Mar 23 21:23:18 1993 Joe Wells (jbw at csd.bu.edu)
143 ;; * Made mail-full-name-prefixes a user-customizable variable.
144 ;; Allow passing the address as a buffer as well as as a string.
145 ;; Allow [ and ] as name characters (Finnish character set).
147 ;; Mon Mar 22 21:20:56 1993 Joe Wells (jbw at bigbird.bu.edu)
149 ;; * Handle "null" addresses. Handle = used for spacing in mailbox
150 ;; name. Fix bug in handling of ROUTE-ADDR-type addresses that are
151 ;; missing their brackets. Handle uppercase "JR". Extract full
152 ;; names from X.400 addresses encoded in RFC-822. Fix bug in
153 ;; handling of multiple addresses where first has trailing comment.
154 ;; Handle more kinds of telephone extension lead-ins.
156 ;; Mon Mar 22 20:16:57 1993 Joe Wells (jbw at bigbird.bu.edu)
158 ;; * Handle HZ encoding for embedding GB encoded chinese characters.
160 ;; Mon Mar 22 00:46:12 1993 Joe Wells (jbw at bigbird.bu.edu)
162 ;; * Fixed too broad matching of ham radio call signs. Fixed bug in
163 ;; handling an unmatched ' in a name string. Enhanced recognition
164 ;; of when . in the mailbox name terminates the name portion.
165 ;; Narrowed conversion of . to space to only the necessary
166 ;; situation. Deal with VMS's stupid date stamps. Handle a unique
167 ;; way of introducing an alternate address. Fixed spacing bug I
168 ;; introduced in switching last name order. Fixed bug in handling
169 ;; address with ! and % but no @. Narrowed the cases in which
170 ;; certain trailing words are discarded.
172 ;; Sun Mar 21 21:41:06 1993 Joe Wells (jbw at bigbird.bu.edu)
174 ;; * Fixed bugs in handling GROUP addresses. Certain words in the
175 ;; middle of a name no longer terminate it. Handle LISTSERV list
176 ;; names. Ignore comment field containing mailbox name.
178 ;; Sun Mar 21 14:39:38 1993 Joe Wells (jbw at bigbird.bu.edu)
180 ;; * Moved variant-method code back into main function. Handle
181 ;; underscores as spaces in comments. Handle leading nickname. Add
182 ;; flag to ignore single-word names. Other changes.
184 ;; Mon Feb 1 22:23:31 1993 Joe Wells (jbw at bigbird.bu.edu)
186 ;; * Added in changes by Rod Whitby and Jamie Zawinski. This
187 ;; includes the flag mail-extr-guess-middle-initial and the fix for
188 ;; handling multiple addresses correctly. (Whitby just changed
191 ;; Mon Apr 6 23:59:09 1992 Joe Wells (jbw at bigbird.bu.edu)
193 ;; * Cleaned up some more. Release version 1.0 to world.
195 ;; Sun Apr 5 19:39:08 1992 Joe Wells (jbw at bigbird.bu.edu)
197 ;; * Cleaned up full name extraction extensively.
199 ;; Sun Feb 2 14:45:24 1992 Joe Wells (jbw at bigbird.bu.edu)
201 ;; * Total rewrite. Integrated mail-canonicalize-address into
202 ;; mail-extract-address-components. Now handles GROUP addresses more
203 ;; or less correctly. Better handling of lots of different cases.
205 ;; Fri Jun 14 19:39:50 1991
211 (defgroup mail-extr nil
212 "Extract full name and address from RFC 822 mail header."
216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;; User configuration variable definitions.
221 (defcustom mail-extr-guess-middle-initial nil
222 "*Whether to try to guess middle initial from mail address.
223 If true, then when we see an address like \"John Smith <jqs@host.com>\"
224 we will assume that \"John Q. Smith\" is the fellow's name."
228 (defcustom mail-extr-ignore-single-names t
229 "*Whether to ignore a name that is just a single word.
230 If true, then when we see an address like \"Idiot <dumb@stupid.com>\"
231 we will act as though we couldn't find a full name in the address."
235 ;; Matches a leading title that is not part of the name (does not
236 ;; contribute to uniquely identifying the person).
237 (defcustom mail-extr-full-name-prefixes
239 "\\(Prof\\|D[Rr]\\|Mrs?\\|Rev\\|Rabbi\\|SysOp\\|LCDR\\)\\.?[ \t\n]")
240 "*Matches prefixes to the full name that identify a person's position.
241 These are stripped from the full name because they do not contribute to
242 uniquely identifying the person."
246 (defcustom mail-extr-
@-binds-tighter-than-
! nil
247 "*Whether the local mail transport agent looks at ! before @."
251 (defcustom mail-extr-mangle-uucp nil
252 "*Whether to throw away information in UUCP addresses
253 by translating things like \"foo!bar!baz@host\" into \"baz@bar.UUCP\"."
257 ;;----------------------------------------------------------------------
258 ;; what orderings are meaningful?????
259 ;;(defvar mail-operator-precedence-list '(?! ?% ?@))
260 ;; Right operand of a % or a @ must be a domain name, period. No other
261 ;; operators allowed. Left operand of a @ is an address relative to that
264 ;; Left operand of a ! must be a domain name. Right operand is an
265 ;; arbitrary address.
266 ;;----------------------------------------------------------------------
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 ;; Constant definitions.
276 ;; Names in ISO 8859-1 Name
277 ;; ISO 10XXX ISO 8859-2 in
278 ;; ISO 6937 ISO 10646 RFC Swedish
279 ;; etc. Hex Oct 1345 TeX Split ASCII Description
280 ;; --------- ---------- ---- --- ----- ----- -------------------------------
281 ;; %a E4 344 a: \"a ae { latin small a + diaeresis d
282 ;; %o F6 366 o: \"o oe | latin small o + diaeresis v
283 ;; @a E5 345 aa \oa aa } latin small a + ring above e
284 ;; %u FC 374 u: \"u ue ~ latin small u + diaeresis |
285 ;; /e E9 351 e' \'e ` latin small e + acute i
286 ;; %A C4 304 A: \"A AE [ latin capital a + diaeresis D
287 ;; %O D6 326 O: \"O OE \ latin capital o + diaeresis V
288 ;; @A C5 305 AA \oA AA ] latin capital a + ring above E
289 ;; %U DC 334 U: \"U UE ^ latin capital u + diaeresis \
290 ;; /E C9 311 E' \'E @ latin capital e + acute I
292 ;; NOTE: @a and @A are not in ISO 8859-2 (the codes mentioned above invoke
293 ;; /l and /L). Some of this data was retrieved from
294 ;; listserv@jhuvm.hcf.jhu.edu.
296 ;; Any character that can occur in a name, not counting characters that
297 ;; separate parts of a multipart name (hyphen and period).
298 ;; Yes, there are weird people with digits in their names.
299 ;; You will also notice the consideration for the
300 ;; Swedish/Finnish/Norwegian character set.
301 (defconst mail-extr-all-letters-but-separators
302 (purecopy "][[:alnum:]{|}'~`"))
304 ;; Any character that can occur in a name in an RFC822 address including
305 ;; the separator (hyphen and possibly period) for multipart names.
306 ;; #### should . be in here?
307 (defconst mail-extr-all-letters
308 (purecopy (concat mail-extr-all-letters-but-separators
"---")))
310 ;; Any character that can start a name.
311 ;; Keep this set as minimal as possible.
312 (defconst mail-extr-first-letters
(purecopy "[:alpha:]"))
314 ;; Any character that can end a name.
315 ;; Keep this set as minimal as possible.
316 (defconst mail-extr-last-letters
(purecopy "[:alpha:]`'."))
318 (defconst mail-extr-leading-garbage
319 (purecopy (format "[^%s]+" mail-extr-first-letters
)))
321 ;; (defconst mail-extr-non-name-chars
322 ;; (purecopy (concat "^" mail-extr-all-letters ".")))
323 ;; (defconst mail-extr-non-begin-name-chars
324 ;; (purecopy (concat "^" mail-extr-first-letters)))
325 ;; (defconst mail-extr-non-end-name-chars
326 ;; (purecopy (concat "^" mail-extr-last-letters)))
328 ;; Matches an initial not followed by both a period and a space.
329 ;; (defconst mail-extr-bad-initials-pattern
331 ;; (format "\\(\\([^%s]\\|\\`\\)[%s]\\)\\(\\.\\([^ ]\\)\\| \\|\\([^%s .]\\)\\|\\'\\)"
332 ;; mail-extr-all-letters mail-extr-first-letters mail-extr-all-letters)))
334 ;; Matches periods used instead of spaces. Must not match the period
335 ;; following an initial.
336 (defconst mail-extr-bad-dot-pattern
338 (format "\\([%s][%s]\\)\\.+\\([%s]\\)"
339 mail-extr-all-letters
340 mail-extr-last-letters
341 mail-extr-first-letters
)))
343 ;; Matches an embedded or leading nickname that should be removed.
344 ;; (defconst mail-extr-nickname-pattern
346 ;; (format "\\([ .]\\|\\`\\)[\"'`\[\(]\\([ .%s]+\\)[\]\"'\)] "
347 ;; mail-extr-all-letters)))
349 ;; Matches the occurrence of a generational name suffix, and the last
350 ;; character of the preceding name. This is important because we want to
351 ;; keep such suffixes: they help to uniquely identify the person.
352 ;; *** Perhaps this should be a user-customizable variable. However, the
353 ;; *** regular expression is fairly tricky to alter, so maybe not.
354 (defconst mail-extr-full-name-suffix-pattern
357 "\\(,? ?\\([JjSs][Rr]\\.?\\|V?I+V?\\)\\)\\([^%s]\\([^%s]\\|\\'\\)\\|\\'\\)"
358 mail-extr-all-letters mail-extr-all-letters
)))
360 (defconst mail-extr-roman-numeral-pattern
(purecopy "V?I+V?\\b"))
362 ;; Matches a trailing uppercase (with other characters possible) acronym.
363 ;; Must not match a trailing uppercase last name or trailing initial
364 (defconst mail-extr-weird-acronym-pattern
365 (purecopy "\\([A-Z]+[-_/]\\|[A-Z][A-Z][A-Z]?\\b\\)"))
367 ;; Matches a mixed-case or lowercase name (not an initial).
368 ;; #### Match Latin1 lower case letters here too?
369 ;; (defconst mail-extr-mixed-case-name-pattern
372 ;; "\\b\\([a-z][%s]*[%s]\\|[%s][%s]*[a-z][%s]*[%s]\\|[%s][%s]*[a-z]\\)"
373 ;; mail-extr-all-letters mail-extr-last-letters
374 ;; mail-extr-first-letters mail-extr-all-letters mail-extr-all-letters
375 ;; mail-extr-last-letters mail-extr-first-letters mail-extr-all-letters)))
377 ;; Matches a trailing alternative address.
378 ;; #### Match Latin1 letters here too?
379 ;; #### Match _ before @ here too?
380 (defconst mail-extr-alternative-address-pattern
381 (purecopy "\\(aka *\\)?[a-zA-Z.]+[!@][a-zA-Z.]"))
383 ;; Matches a variety of trailing comments not including comma-delimited
385 (defconst mail-extr-trailing-comment-start-pattern
386 (purecopy " [-{]\\|--\\|[+@#></\;]"))
388 ;; Matches a name (not an initial).
389 ;; This doesn't force a word boundary at the end because sometimes a
390 ;; comment is separated by a `-' with no preceding space.
391 (defconst mail-extr-name-pattern
392 (purecopy (format "\\b[%s][%s]*[%s]"
393 mail-extr-first-letters
394 mail-extr-all-letters
395 mail-extr-last-letters
)))
397 (defconst mail-extr-initial-pattern
398 (purecopy (format "\\b[%s]\\([. ]\\|\\b\\)" mail-extr-first-letters
)))
400 ;; Matches a single name before a comma.
401 ;; (defconst mail-extr-last-name-first-pattern
402 ;; (purecopy (concat "\\`" mail-extr-name-pattern ",")))
404 ;; Matches telephone extensions.
405 (defconst mail-extr-telephone-extension-pattern
407 "\\(\\([Ee]xt\\|\\|[Tt]ph\\|[Tt]el\\|[Xx]\\).?\\)? *\\+?[0-9][- 0-9]+"))
409 ;; Matches ham radio call signs.
410 ;; Help from: Mat Maessen N2NJZ <maessm@rpi.edu>, Mark Feit
411 ;; <mark@era.com>, Michael Covington <mcovingt@ai.uga.edu>.
412 ;; Examples: DX504 DX515 K5MRU K8DHK KA9WGN KA9WGN KD3FU KD6EUI KD6HBW
413 ;; KE9TV KF0NV N1API N3FU N3GZE N3IGS N4KCC N7IKQ N9HHU W4YHF W6ANK WA2SUH
414 ;; WB7VZI N2NJZ NR3G KJ4KK AB4UM AL7NI KH6OH WN3KBT N4TMI W1A N0NZO
415 (defconst mail-extr-ham-call-sign-pattern
416 (purecopy "\\b\\(DX[0-9]+\\|[AKNW][A-Z]?[0-9][A-Z][A-Z]?[A-Z]?\\)"))
418 ;; Possible trailing suffixes: "\\(/\\(KT\\|A[AEG]\\|[R0-9]\\)\\)?"
419 ;; /KT == Temporary Technician (has CSC but not "real" license)
420 ;; /AA == Temporary Advanced
421 ;; /AE == Temporary Extra
422 ;; /AG == Temporary General
424 ;; /# == stations operating out of home district
425 ;; I don't include these in the regexp above because I can't imagine
426 ;; anyone putting them with their name in an e-mail address.
428 ;; Matches normal single-part name
429 (defconst mail-extr-normal-name-pattern
430 (purecopy (format "\\b[%s][%s]+[%s]"
431 mail-extr-first-letters
432 mail-extr-all-letters-but-separators
433 mail-extr-last-letters
)))
435 ;; Matches a single word name.
436 ;; (defconst mail-extr-one-name-pattern
437 ;; (purecopy (concat "\\`" mail-extr-normal-name-pattern "\\'")))
439 ;; Matches normal two names with missing middle initial
440 ;; The first name is not allowed to have a hyphen because this can cause
441 ;; false matches where the "middle initial" is actually the first letter
442 ;; of the second part of the first name.
443 (defconst mail-extr-two-name-pattern
445 (concat "\\`\\(" mail-extr-normal-name-pattern
446 "\\|" mail-extr-initial-pattern
447 "\\) +\\(" mail-extr-name-pattern
"\\)\\(,\\|\\'\\)")))
449 (defconst mail-extr-listserv-list-name-pattern
450 (purecopy "Multiple recipients of list \\([-A-Z]+\\)"))
452 (defconst mail-extr-stupid-vms-date-stamp-pattern
454 "[0-9][0-9]-[JFMASOND][aepuco][nbrylgptvc]-[0-9][0-9][0-9][0-9] [0-9]+ *"))
456 ;;; HZ -- GB (PRC Chinese character encoding) in ASCII embedding protocol
458 ;; In ASCII mode, a byte is interpreted as an ASCII character, unless a '~' is
459 ;; encountered. The character '~' is an escape character. By convention, it
460 ;; must be immediately followed ONLY by '~', '{' or '\n' (<LF>), with the
461 ;; following special meaning.
463 ;; o The escape sequence '~~' is interpreted as a '~'.
464 ;; o The escape-to-GB sequence '~{' switches the mode from ASCII to GB.
465 ;; o The escape sequence '~\n' is a line-continuation marker to be consumed
466 ;; with no output produced.
468 ;; In GB mode, characters are interpreted two bytes at a time as (pure) GB
469 ;; codes until the escape-from-GB code '~}' is read. This code switches the
470 ;; mode from GB back to ASCII. (Note that the escape-from-GB code '~}'
471 ;; ($7E7D) is outside the defined GB range.)
472 (defconst mail-extr-hz-embedded-gb-encoded-chinese-pattern
473 (purecopy "~{\\([^~].\\|~[^\}]\\)+~}"))
475 ;; The leading optional lowercase letters are for a bastardized version of
476 ;; the encoding, as is the optional nature of the final slash.
477 (defconst mail-extr-x400-encoded-address-pattern
478 (purecopy "[a-z]?[a-z]?\\(/[A-Za-z]+\\(\\.[A-Za-z]+\\)?=[^/]+\\)+/?\\'"))
480 (defconst mail-extr-x400-encoded-address-field-pattern-format
481 (purecopy "/%s=\\([^/]+\\)\\(/\\|\\'\\)"))
483 (defconst mail-extr-x400-encoded-address-surname-pattern
484 ;; S stands for Surname (family name).
486 (format mail-extr-x400-encoded-address-field-pattern-format
"[Ss]")))
488 (defconst mail-extr-x400-encoded-address-given-name-pattern
489 ;; G stands for Given name.
491 (format mail-extr-x400-encoded-address-field-pattern-format
"[Gg]")))
493 (defconst mail-extr-x400-encoded-address-full-name-pattern
494 ;; PN stands for Personal Name. When used it represents the combination
495 ;; of the G and S fields.
496 ;; "The one system I used having this field asked it with the prompt
497 ;; `Personal Name'. But they mapped it into G and S on outgoing real
498 ;; X.400 addresses. As they mapped G and S into PN on incoming..."
500 (format mail-extr-x400-encoded-address-field-pattern-format
"[Pp][Nn]")))
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
506 ;; Syntax tables used for quick parsing.
509 (defconst mail-extr-address-syntax-table
(make-syntax-table))
510 (defconst mail-extr-address-comment-syntax-table
(make-syntax-table))
511 (defconst mail-extr-address-domain-literal-syntax-table
(make-syntax-table))
512 (defconst mail-extr-address-text-comment-syntax-table
(make-syntax-table))
513 (defconst mail-extr-address-text-syntax-table
(make-syntax-table))
517 (let ((syntax-table (symbol-value (car pair
))))
521 (if (eq 2 (length item
))
522 ;; modifying syntax of a single character
523 (modify-syntax-entry (car item
) (car (cdr item
)) syntax-table
)
524 ;; modifying syntax of a range of characters
525 (let ((char (nth 0 item
))
527 (syntax (nth 2 item
)))
528 (while (<= char bound
)
529 (modify-syntax-entry char syntax syntax-table
)
530 (setq char
(1+ char
)))))))
532 '((mail-extr-address-syntax-table
533 (?
\000 ?
\037 "w") ;control characters
535 (?
! ?~
"w") ;printable characters
537 (?
\200 ?
\377 "w") ;high-bit-on characters
538 (?
\240 " ") ;nobreakspace
555 ;; % and ! aren't RFC822 characters, but it is convenient to pretend
557 (?
! ".") ;; this needs to be word-constituent when not in .UUCP mode
559 (mail-extr-address-comment-syntax-table
569 (mail-extr-address-domain-literal-syntax-table
579 (mail-extr-address-text-comment-syntax-table
597 (mail-extr-address-text-syntax-table
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618 ;; Utility functions and macros.
621 (defmacro mail-extr-delete-char
(n)
622 ;; in v19, delete-char is compiled as a function call, but delete-region
623 ;; is byte-coded, so it's much much faster.
624 (list 'delete-region
'(point) (list '+ '(point) n
)))
626 (defmacro mail-extr-skip-whitespace-forward
()
627 ;; v19 fn skip-syntax-forward is more tasteful, but not byte-coded.
628 '(skip-chars-forward " \t\n\r\240"))
630 (defmacro mail-extr-skip-whitespace-backward
()
631 ;; v19 fn skip-syntax-backward is more tasteful, but not byte-coded.
632 '(skip-chars-backward " \t\n\r\240"))
635 (defmacro mail-extr-undo-backslash-quoting
(beg end
)
638 (narrow-to-region (, beg
) (, end
))
639 (goto-char (point-min))
641 (while (search-forward "\\" nil t
)
642 (mail-extr-delete-char -
1)
647 (defmacro mail-extr-nuke-char-at
(pos)
650 (mail-extr-delete-char 1)
653 (put 'mail-extr-nuke-outside-range
654 'edebug-form-spec
'(symbolp &optional form form atom
))
656 (defmacro mail-extr-nuke-outside-range
(list-symbol
657 beg-symbol end-symbol
658 &optional no-replace
)
659 ;; LIST-SYMBOL names a variable holding a list of buffer positions
660 ;; BEG-SYMBOL and END-SYMBOL name variables delimiting a range
661 ;; Each element of LIST-SYMBOL which lies outside of the range is
662 ;; deleted from the list.
663 ;; Unless NO-REPLACE is true, at each of the positions in LIST-SYMBOL
664 ;; which lie outside of the range, one character at that position is
665 ;; replaced with a SPC.
666 (or (memq no-replace
'(t nil
))
667 (error "no-replace must be t or nil, evaluable at macroexpand-time"))
668 (` (let ((temp (, list-symbol
))
672 (cond ((or (> ch
(, end-symbol
))
673 (< ch
(, beg-symbol
)))
676 (` ((mail-extr-nuke-char-at ch
)))))
678 (setq temp
(cdr temp
)))
679 (setq (, list-symbol
) (delq nil
(, list-symbol
))))))
681 (defun mail-extr-demarkerize (marker)
682 ;; if arg is a marker, destroys the marker, then returns the old value.
683 ;; otherwise returns the arg.
685 (let ((temp (marker-position marker
)))
686 (set-marker marker nil
)
690 (defun mail-extr-markerize (pos)
691 ;; coerces pos to a marker if non-nil.
692 (if (or (markerp pos
) (null pos
))
696 (defmacro mail-extr-last
(list)
697 ;; Returns last element of LIST.
699 (` (let ((list (, list
)))
700 (while (not (null (cdr list
)))
701 (setq list
(cdr list
)))
704 (defmacro mail-extr-safe-move-sexp
(arg)
705 ;; Safely skip over one balanced sexp, if there is one. Return t if success.
706 (` (condition-case error
708 (goto-char (or (scan-sexps (point) (, arg
)) (point)))
711 ;; #### kludge kludge kludge kludge kludge kludge kludge !!!
712 (if (string-equal (nth 1 error
) "Unbalanced parentheses")
715 (signal (car error
) (cdr error
))))))))
717 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
719 ;; The main function to grind addresses
722 (defvar disable-initial-guessing-flag
) ; dynamic assignment
723 (defvar cbeg
) ; dynamic assignment
724 (defvar cend
) ; dynamic assignment
727 (defun mail-extract-address-components (address &optional all
)
728 "Given an RFC-822 address ADDRESS, extract full name and canonical address.
729 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
730 If no name can be extracted, FULL-NAME will be nil.
732 If the optional argument ALL is non-nil, then ADDRESS can contain zero
733 or more recipients, separated by commas, and we return a list of
734 the form ((FULL-NAME CANONICAL-ADDRESS) ...) with one element for
735 each recipient. If ALL is nil, then if ADDRESS contains more than
736 one recipients, all but the first is ignored.
738 ADDRESS may be a string or a buffer. If it is a buffer, the visible
739 (narrowed) portion of the buffer will be interpreted as the address.
740 (This feature exists so that the clever caller might be able to avoid
742 (let ((canonicalization-buffer (get-buffer-create " *canonical address*"))
743 (extraction-buffer (get-buffer-create " *extract address components*"))
747 (set-buffer extraction-buffer
)
749 (buffer-disable-undo extraction-buffer
)
750 (set-syntax-table mail-extr-address-syntax-table
)
753 (setq case-fold-search nil
)
755 ;; Insert extra space at beginning to allow later replacement with <
756 ;; without having to move markers.
759 ;; Insert the address itself.
760 (cond ((stringp address
)
763 (insert-buffer-substring address
))
765 (error "Invalid address: %s" address
)))
767 (set-text-properties (point-min) (point-max) nil
)
770 (set-buffer canonicalization-buffer
)
772 (buffer-disable-undo canonicalization-buffer
)
773 (set-syntax-table mail-extr-address-syntax-table
)
774 (setq case-fold-search nil
))
777 ;; Unfold multiple lines.
778 (goto-char (point-min))
779 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t
)
780 (replace-match "\\1 " t
))
782 ;; Loop over addresses until we have as many as we want.
783 (while (and (or all
(null value-list
))
784 (progn (goto-char (point-min))
785 (skip-chars-forward " \t")
789 <-pos
>-pos
@-pos colon-pos comma-pos
!-pos %-pos \
;-pos
790 group-
:-pos group-\
;-pos route-addr-:-pos
792 first-real-pos last-real-pos
793 phrase-beg phrase-end
794 cbeg cend
; dynamically set from -voodoo
801 fi mi li
; first, middle, last initial
802 saved-%-pos saved-
!-pos saved-
@-pos
803 domain-pos \.-pos insert-point
804 ;; mailbox-name-processed-flag
805 disable-initial-guessing-flag
) ; dynamically set from -voodoo
807 (goto-char (point-min))
809 ;; Insert extra space at beginning to allow later replacement with <
810 ;; without having to move markers.
811 (or (eq (following-char) ?\
)
814 ;; First pass grabs useful information about address.
816 (mail-extr-skip-whitespace-forward)
818 (setq char
(char-after (point)))
820 (if (not (eq char ?\
())
821 (setq first-real-pos
(point))))
825 (set-syntax-table mail-extr-address-comment-syntax-table
)
826 ;; only record the first non-empty comment's position
830 (mail-extr-skip-whitespace-forward)
831 (not (eq ?\
) (char-after (point))))))
833 ;; TODO: don't record if unbalanced
834 (or (mail-extr-safe-move-sexp 1)
836 (set-syntax-table mail-extr-address-syntax-table
)
839 (setq cend
(point))))
842 ;; only record the first non-empty quote's position
843 (if (and (not quote-beg
)
846 (mail-extr-skip-whitespace-forward)
847 (not (eq ?
\" (char-after (point))))))
848 (setq quote-beg
(point)))
849 ;; TODO: don't record if unbalanced
850 (or (mail-extr-safe-move-sexp 1)
854 (setq quote-end
(point))))
857 (set-syntax-table mail-extr-address-domain-literal-syntax-table
)
858 (or (mail-extr-safe-move-sexp 1)
860 (set-syntax-table mail-extr-address-syntax-table
))
861 ;; commas delimit addresses when outside < > pairs.
863 (or (and (null <-pos
)
864 ;; Handle ROUTE-ADDR address that is missing its <.
865 (not (eq ?
@ (char-after (1+ (point))))))
867 ;; handle weird munged addresses
868 ;; BUG FIX: This test was reversed. Thanks to the
869 ;; brilliant Rod Whitby <rwhitby@research.canon.oz.au>
870 ;; for discovering this!
871 (< (mail-extr-last <-pos
) (car >-pos
)))))
872 ;; The argument contains more than one address.
873 ;; Temporarily hide everything after this one.
874 (setq end-of-address
(copy-marker (1+ (point))))
875 (narrow-to-region (point-min) (1+ (point)))
876 (mail-extr-delete-char 1)
877 (setq char ?\
() ; HAVE I NO SHAME??
879 ;; record the position of various interesting chars, determine
881 ((setq record-pos-symbol
883 '((?
< .
<-pos
) (?
> .
>-pos
) (?
@ .
@-pos
)
884 (?
: . colon-pos
) (?
, . comma-pos
) (?
! .
!-pos
)
885 (?% . %-pos
) (?\
; . \;-pos)))))
886 (set record-pos-symbol
887 (cons (point) (symbol-value record-pos-symbol
)))
892 ;; comment terminator illegal
894 ;; domain literal terminator illegal
896 ;; \ allowed only within quoted strings,
897 ;; domain literals, and comments
900 (mail-extr-nuke-char-at (point))
905 ;; At the end of first address of a multiple address header.
908 (setq last-real-pos
(point))))
910 ;; Use only the leftmost <, if any. Replace all others with spaces.
912 (mail-extr-nuke-char-at (car <-pos
))
913 (setq <-pos
(cdr <-pos
)))
915 ;; Use only the rightmost >, if any. Replace all others with spaces.
917 (mail-extr-nuke-char-at (nth 1 >-pos
))
918 (setcdr >-pos
(nthcdr 2 >-pos
)))
920 ;; If multiple @s and a :, but no < and >, insert around buffer.
921 ;; Example: @foo.bar.dom,@xxx.yyy.zzz:mailbox@aaa.bbb.ccc
922 ;; This commonly happens on the UUCP "From " line. Ugh.
923 (cond ((and (> (length @-pos
) 1)
924 (eq 1 (length colon-pos
)) ;TODO: check if between last two @s
927 (goto-char (point-min))
928 (mail-extr-delete-char 1)
929 (setq <-pos
(list (point)))
932 ;; If < but no >, insert > in rightmost possible position
935 (goto-char (point-max))
936 (setq >-pos
(list (point)))
939 ;; If > but no <, replace > with space.
942 (mail-extr-nuke-char-at (car >-pos
))
945 ;; Turn >-pos and <-pos into non-lists
946 (setq >-pos
(car >-pos
)
949 ;; Trim other punctuation lists of items outside < > pair to handle
951 (cond (<-pos
; don't need to check >-pos also
952 ;; handle bozo software that violates RFC 822 by sticking
953 ;; punctuation marks outside of a < > pair
954 (mail-extr-nuke-outside-range @-pos
<-pos
>-pos t
)
955 ;; RFC 822 says nothing about these two outside < >, but
956 ;; remove those positions from the lists to make things
958 (mail-extr-nuke-outside-range !-pos
<-pos
>-pos t
)
959 (mail-extr-nuke-outside-range %-pos
<-pos
>-pos t
)))
961 ;; Check for : that indicates GROUP list and for : part of
963 ;; Can't possibly be more than two :. Nuke any extra.
965 (setq temp
(car colon-pos
)
966 colon-pos
(cdr colon-pos
))
967 (cond ((and <-pos
>-pos
970 (if (or route-addr-
:-pos
973 (< temp
(nth 1 @-pos
)))
974 (mail-extr-nuke-char-at temp
)
975 (setq route-addr-
:-pos temp
)))
979 (setq group-
:-pos temp
))))
981 ;; Nuke any ; that is in or to the left of a < > pair or to the left
982 ;; of a GROUP starting :. Also, there may only be one ;.
984 (setq temp
(car \
;-pos)
986 (cond ((and <-pos
>-pos
989 (mail-extr-nuke-char-at temp
))
990 ((and (or (not group-
:-pos
)
991 (> temp group-
:-pos
))
993 (setq group-\
;-pos temp))))
995 ;; Nuke unmatched GROUP syntax characters.
996 (cond ((and group-
:-pos
(not group-\
;-pos))
997 ;; *** Do I really need to erase it?
998 (mail-extr-nuke-char-at group-
:-pos
)
999 (setq group-
:-pos nil
)))
1000 (cond ((and group-\
;-pos (not group-:-pos))
1001 ;; *** Do I really need to erase it?
1002 (mail-extr-nuke-char-at group-\
;-pos)
1003 (setq group-\
;-pos nil)))
1005 ;; Handle junk like ";@host.company.dom" that sendmail adds.
1006 ;; **** should I remember comment positions?
1009 ;; this is fine for now
1010 (mail-extr-nuke-outside-range !-pos group-
:-pos group-\
;-pos t)
1011 (mail-extr-nuke-outside-range @-pos group-
:-pos group-\
;-pos t)
1012 (mail-extr-nuke-outside-range %-pos group-
:-pos group-\
;-pos t)
1013 (mail-extr-nuke-outside-range comma-pos group-
:-pos group-\
;-pos t)
1015 (> last-real-pos
(1+ group-\
;-pos))
1016 (setq last-real-pos
(1+ group-\
;-pos)))
1017 ;; *** This may be wrong:
1019 (> cend group-\
;-pos)
1023 (> quote-end group-\
;-pos)
1026 ;; This was both wrong and unnecessary:
1027 ;;(narrow-to-region (point-min) group-\;-pos)
1029 ;; *** The entire handling of GROUP addresses seems rather lame.
1030 ;; *** It deserves a complete rethink, except that these addresses
1031 ;; *** are hardly ever seen.
1034 ;; Any commas must be between < and : of ROUTE-ADDR. Nuke any
1036 ;; Hell, go ahead an nuke all of the commas.
1037 ;; **** This will cause problems when we start handling commas in
1038 ;; the PHRASE part .... no it won't ... yes it will ... ?????
1039 (mail-extr-nuke-outside-range comma-pos
1 1)
1041 ;; can only have multiple @s inside < >. The fact that some MTAs
1042 ;; put de-bracketed ROUTE-ADDRs in the UUCP-style "From " line is
1045 ;; Locate PHRASE part of ROUTE-ADDR.
1048 (mail-extr-skip-whitespace-backward)
1049 (setq phrase-end
(point))
1050 (goto-char (or ;;group-:-pos
1052 (mail-extr-skip-whitespace-forward)
1053 (if (< (point) phrase-end
)
1054 (setq phrase-beg
(point))
1055 (setq phrase-end nil
))))
1057 ;; handle ROUTE-ADDRS with real ROUTEs.
1058 ;; If there are multiple @s, then we assume ROUTE-ADDR syntax, and
1059 ;; any % or ! must be semantically meaningless.
1060 ;; TODO: do this processing into canonicalization buffer
1061 (cond (route-addr-:-pos
1064 >-pos
(copy-marker >-pos
)
1065 route-addr-
:-pos
(copy-marker route-addr-
:-pos
))
1067 (insert-before-markers ?X
)
1068 (goto-char (car @-pos
))
1069 (while (setq @-pos
(cdr @-pos
))
1070 (mail-extr-delete-char 1)
1071 (setq %-pos
(cons (point-marker) %-pos
))
1073 (goto-char (1- >-pos
))
1075 (insert-buffer-substring extraction-buffer
1076 (car @-pos
) route-addr-
:-pos
)
1077 (delete-region (car @-pos
) route-addr-
:-pos
))
1079 (setq saved-
@-pos
(list (point)))))
1080 (setq @-pos saved-
@-pos
)
1082 (mail-extr-delete-char -
1)
1083 (mail-extr-nuke-char-at route-addr-
:-pos
)
1084 (mail-extr-demarkerize route-addr-
:-pos
)
1085 (setq route-addr-
:-pos nil
1086 >-pos
(mail-extr-demarkerize >-pos
)
1087 %-pos
(mapcar 'mail-extr-demarkerize %-pos
))))
1090 (setq @-pos
(car @-pos
))
1092 ;; TODO: remove comments in the middle of an address
1095 (set-buffer canonicalization-buffer
)
1099 (insert-buffer-substring extraction-buffer
)
1102 (narrow-to-region (progn
1103 (goto-char (1+ <-pos
))
1104 (mail-extr-skip-whitespace-forward)
1107 (if (and first-real-pos last-real-pos
)
1108 (narrow-to-region first-real-pos last-real-pos
)
1109 ;; ****** Oh no! What if the address is completely empty!
1110 ;; *** Is this correct?
1111 (narrow-to-region (point-max) (point-max))
1115 (mail-extr-nuke-outside-range %-pos
(point-min) @-pos
))
1117 (mail-extr-nuke-outside-range !-pos
(point-min) (car %-pos
)))
1118 (and @-pos
!-pos
(not %-pos
)
1119 (mail-extr-nuke-outside-range !-pos
(point-min) @-pos
))
1121 ;; Error condition:?? (and %-pos (not @-pos))
1123 ;; WARNING: THIS CODE IS DUPLICATED BELOW.
1126 (goto-char (car %-pos
))
1127 (mail-extr-delete-char 1)
1128 (setq @-pos
(point))
1130 (setq %-pos
(cdr %-pos
))))
1132 (if mail-extr-mangle-uucp
1134 ;; **** I don't understand this save-restriction and the
1135 ;; narrow-to-region inside it. Why did I do that?
1138 mail-extr-
@-binds-tighter-than-
!)
1140 (setq %-pos
(cons (point) %-pos
)
1142 (mail-extr-delete-char 1)
1144 (setq insert-point
(point-max)))
1145 (mail-extr-@-binds-tighter-than-
!
1146 (setq insert-point
(point-max)))
1148 (setq insert-point
(mail-extr-last %-pos
)
1149 saved-%-pos
(mapcar 'mail-extr-markerize %-pos
)
1151 @-pos
(mail-extr-markerize @-pos
)))
1153 (setq insert-point
@-pos
)
1154 (setq @-pos
(mail-extr-markerize @-pos
)))
1156 (setq insert-point
(point-max))))
1157 (narrow-to-region (point-min) insert-point
)
1158 (setq saved-
!-pos
(car !-pos
))
1160 (goto-char (point-max))
1161 (cond ((and (not @-pos
)
1163 (setq @-pos
(point))
1164 (insert-before-markers "@ "))
1166 (setq %-pos
(cons (point) %-pos
))
1167 (insert-before-markers "% ")))
1169 (insert-buffer-substring
1175 (mail-extr-delete-char 1)
1177 (mail-extr-safe-move-sexp -
1)
1178 (mail-extr-skip-whitespace-backward)
1179 (eq ?.
(preceding-char)))
1180 (insert-before-markers
1182 (mail-extr-skip-whitespace-backward)
1183 (eq ?.
(preceding-char)))
1187 (setq !-pos
(cdr !-pos
))))
1189 (setq %-pos
(append (mapcar 'mail-extr-demarkerize
1192 (setq @-pos
(mail-extr-demarkerize @-pos
))
1193 (narrow-to-region (1+ saved-
!-pos
) (point-max)))))
1195 ;; WARNING: THIS CODE IS DUPLICATED ABOVE.
1198 (goto-char (car %-pos
))
1199 (mail-extr-delete-char 1)
1200 (setq @-pos
(point))
1202 (setq %-pos
(cdr %-pos
))))
1204 (setq %-pos
(nreverse %-pos
))
1205 (cond (%-pos
; implies @-pos valid
1209 (goto-char (or (nth 1 temp
)
1211 (mail-extr-skip-whitespace-backward)
1213 (mail-extr-safe-move-sexp -
1)
1214 (setq domain-pos
(point))
1215 (mail-extr-skip-whitespace-backward)
1216 (setq \.-pos
(eq ?.
(preceding-char))))
1218 ;; #### string consing
1219 (let ((s (intern-soft
1220 (buffer-substring domain-pos
(point))
1221 mail-extr-all-top-level-domains
)))
1222 (and s
(get s
'domain-name
))))
1223 (narrow-to-region (point-min) (point))
1224 (goto-char (car temp
))
1225 (mail-extr-delete-char 1)
1226 (setq @-pos
(point))
1228 (setq %-pos
(delq @-pos %-pos
))
1230 (throw 'truncated t
)))
1231 (setq temp
(cdr temp
))))))
1232 (setq mbox-beg
(point-min)
1233 mbox-end
(if %-pos
(car %-pos
)
1237 ;; Done canonicalizing address.
1238 ;; We are now back in extraction-buffer.
1240 ;; Decide what part of the address to search to find the full name.
1242 ;; Example: "First M. Last" <fml@foo.bar.dom>
1244 (eq quote-beg phrase-beg
)
1245 (<= quote-end phrase-end
))
1246 (narrow-to-region (1+ quote-beg
) (1- quote-end
))
1247 (mail-extr-undo-backslash-quoting (point-min) (point-max)))
1249 ;; Example: First Last <fml@foo.bar.dom>
1251 (narrow-to-region phrase-beg phrase-end
))
1253 ;; Example: fml@foo.bar.dom (First M. Last)
1255 (narrow-to-region (1+ cbeg
) (1- cend
))
1256 (mail-extr-undo-backslash-quoting (point-min) (point-max))
1258 ;; Deal with spacing problems
1259 (goto-char (point-min))
1260 ;;; (cond ((not (search-forward " " nil t))
1261 ;;; (goto-char (point-min))
1262 ;;; (cond ((search-forward "_" nil t)
1263 ;;; ;; Handle the *idiotic* use of underlines as spaces.
1264 ;;; ;; Example: fml@foo.bar.dom (First_M._Last)
1265 ;;; (goto-char (point-min))
1266 ;;; (while (search-forward "_" nil t)
1267 ;;; (replace-match " " t)))
1268 ;;; ((search-forward "." nil t)
1269 ;;; ;; Fix . used as space
1270 ;;; ;; Example: danj1@cb.att.com (daniel.jacobson)
1271 ;;; (goto-char (point-min))
1272 ;;; (while (re-search-forward mail-extr-bad-dot-pattern nil t)
1273 ;;; (replace-match "\\1 \\2" t))))))
1276 ;; Otherwise we try to get the name from the mailbox portion
1278 ;; Example: First_M_Last@foo.bar.dom
1280 ;; *** Work in canon buffer instead? No, can't. Hmm.
1281 (goto-char (point-max))
1282 (narrow-to-region (point) (point))
1283 (insert-buffer-substring canonicalization-buffer
1285 (goto-char (point-min))
1287 ;; Example: First_Last.XXX@foo.bar.dom
1288 (setq \.-ends-name
(re-search-forward "[_0-9]" nil t
))
1290 (goto-char (point-min))
1292 (if (not mail-extr-mangle-uucp
)
1293 (modify-syntax-entry ?
! "w" (syntax-table)))
1296 (mail-extr-skip-whitespace-forward)
1298 (setq char
(char-after (point)))
1301 (setq quote-beg
(point))
1302 (or (mail-extr-safe-move-sexp 1)
1303 ;; TODO: handle this error condition!!!!!
1305 ;; take into account deletions
1306 (setq quote-end
(- (point) 2))
1309 (mail-extr-delete-char 1)
1310 (goto-char quote-beg
)
1312 (mail-extr-delete-char 1)))
1313 (mail-extr-undo-backslash-quoting quote-beg quote-end
)
1314 (or (eq ?\
(char-after (point)))
1316 ;; (setq mailbox-name-processed-flag t)
1317 (setq \.-ends-name t
))
1319 (if (memq (char-after (1+ (point))) '(?_ ?
=))
1322 (mail-extr-delete-char 1)
1325 (narrow-to-region (point-min) (point))
1326 (mail-extr-delete-char 1)
1328 ;; (setq mailbox-name-processed-flag t)
1330 ((memq (char-syntax char
) '(?. ?
\\))
1331 (mail-extr-delete-char 1)
1333 ;; (setq mailbox-name-processed-flag t)
1336 (setq atom-beg
(point))
1338 (setq atom-end
(point))
1339 (goto-char atom-beg
)
1341 (narrow-to-region atom-beg atom-end
)
1344 ;; Handle X.400 addresses encoded in RFC-822.
1345 ;; *** Shit! This has to handle the case where it is
1346 ;; *** embedded in a quote too!
1347 ;; *** Shit! The input is being broken up into atoms
1349 ((looking-at mail-extr-x400-encoded-address-pattern
)
1351 ;; Copy the contents of the individual fields that
1352 ;; might hold name data to the beginning.
1355 (lambda (field-pattern)
1358 (re-search-forward field-pattern nil t
))
1359 (insert-buffer-substring (current-buffer)
1363 (list mail-extr-x400-encoded-address-given-name-pattern
1364 mail-extr-x400-encoded-address-surname-pattern
1365 mail-extr-x400-encoded-address-full-name-pattern
))
1367 ;; Discard the rest, since it contains stuff like
1368 ;; routing information, not part of a name.
1369 (mail-extr-skip-whitespace-backward)
1370 (delete-region (point) (point-max))
1372 ;; Handle periods used for spacing.
1373 (while (re-search-forward mail-extr-bad-dot-pattern nil t
)
1374 (replace-match "\\1 \\2" t
))
1376 ;; (setq mailbox-name-processed-flag t)
1379 ;; Handle normal addresses.
1381 (goto-char (point-min))
1382 ;; Handle _ and = used for spacing.
1383 (while (re-search-forward "\\([^_=]+\\)[_=]" nil t
)
1384 (replace-match "\\1 " t
)
1385 ;; (setq mailbox-name-processed-flag t)
1387 (goto-char (point-max))))))))
1389 ;; undo the dirty deed
1390 (if (not mail-extr-mangle-uucp
)
1391 (modify-syntax-entry ?
! "." (syntax-table)))
1393 ;; If we derived the name from the mailbox part of the address,
1394 ;; and we only got one word out of it, don't treat that as a
1395 ;; name. "foo@bar" --> (nil "foo@bar"), not ("foo" "foo@bar")
1396 ;; (if (not mailbox-name-processed-flag)
1397 ;; (delete-region (point-min) (point-max)))
1400 (set-syntax-table mail-extr-address-text-syntax-table
)
1402 (mail-extr-voodoo mbox-beg mbox-end canonicalization-buffer
)
1403 (goto-char (point-min))
1405 ;; If name is "First Last" and userid is "F?L", then assume
1406 ;; the middle initial is the second letter in the userid.
1407 ;; Initial code by Jamie Zawinski <jwz@lucid.com>
1408 ;; *** Make it work when there's a suffix as well.
1409 (goto-char (point-min))
1410 (cond ((and mail-extr-guess-middle-initial
1411 (not disable-initial-guessing-flag
)
1412 (eq 3 (- mbox-end mbox-beg
))
1414 (goto-char (point-min))
1415 (looking-at mail-extr-two-name-pattern
)))
1416 (setq fi
(char-after (match-beginning 0))
1417 li
(char-after (match-beginning 3)))
1419 (set-buffer canonicalization-buffer
)
1420 ;; char-equal is ignoring case here, so no need to upcase
1422 (let ((case-fold-search t
))
1423 (and (char-equal fi
(char-after mbox-beg
))
1424 (char-equal li
(char-after (1- mbox-end
)))
1425 (setq mi
(char-after (1+ mbox-beg
))))))
1427 ;; TODO: use better table than syntax table
1428 (eq ?w
(char-syntax mi
)))
1429 (goto-char (match-beginning 3))
1430 (insert (upcase mi
) ". ")))))
1432 ;; Nuke name if it is the same as mailbox name.
1433 (let ((buffer-length (- (point-max) (point-min)))
1435 (names-match-flag t
))
1436 (cond ((and (> buffer-length
0)
1437 (eq buffer-length
(- mbox-end mbox-beg
)))
1438 (goto-char (point-max))
1439 (insert-buffer-substring canonicalization-buffer
1441 (while (and names-match-flag
1442 (< i buffer-length
))
1443 (or (eq (downcase (char-after (+ i
(point-min))))
1445 (char-after (+ i buffer-length
(point-min)))))
1446 (setq names-match-flag nil
))
1448 (delete-region (+ (point-min) buffer-length
) (point-max))
1449 (if names-match-flag
1450 (narrow-to-region (point) (point))))))
1452 ;; Nuke name if it's just one word.
1453 (goto-char (point-min))
1454 (and mail-extr-ignore-single-names
1455 (not (re-search-forward "[- ]" nil t
))
1456 (narrow-to-region (point) (point)))
1458 ;; Record the result
1460 (cons (list (if (not (= (point-min) (point-max)))
1463 (set-buffer canonicalization-buffer
)
1464 (if (not (= (point-min) (point-max)))
1468 ;; Unless one address is all we wanted,
1469 ;; delete this one from extraction-buffer
1470 ;; and get ready to extract the next address.
1473 (narrow-to-region 1 end-of-address
)
1475 (delete-region (point-min) (point-max))
1478 (if all
(nreverse value-list
) (car value-list
))
1481 (defun mail-extr-voodoo (mbox-beg mbox-end canonicalization-buffer
)
1482 (let ((word-count 0)
1483 (case-fold-search nil
)
1484 mixed-case-flag lower-case-flag
;;upper-case-flag
1485 suffix-flag last-name-comma-flag
1489 drop-this-word-if-trailing-flag
1490 drop-last-word-if-trailing-flag
1492 this-word-beg last-word-beg
1497 (set-syntax-table mail-extr-address-text-syntax-table
)
1499 ;; Get rid of comments.
1500 (goto-char (point-min))
1502 ;; Initialize for this iteration of the loop.
1503 (skip-chars-forward "^({[\"'`")
1504 (let ((cbeg (point)))
1505 (set-syntax-table mail-extr-address-text-comment-syntax-table
)
1506 (cond ((memq (following-char) '(?
\' ?\
`))
1507 (search-forward "'" nil
'move
1508 (if (eq ?
\' (following-char)) 2 1)))
1510 (or (mail-extr-safe-move-sexp 1)
1511 (goto-char (point-max)))))
1512 (set-syntax-table mail-extr-address-text-syntax-table
)
1513 (when (eq (char-after cbeg
) ?\
()
1514 ;; Delete the comment itself.
1515 (delete-region cbeg
(point))
1516 ;; Canonicalize whitespace where the comment was.
1517 (skip-chars-backward " \t")
1518 (if (looking-at "\\([ \t]+$\\|[ \t]+,\\)")
1521 (skip-chars-forward " \t")
1523 (delete-region (point) cbeg
)
1524 (just-one-space))))))
1526 ;; This was moved above.
1527 ;; Fix . used as space
1528 ;; But it belongs here because it occurs not only as
1529 ;; rypens@reks.uia.ac.be (Piet.Rypens)
1531 ;; "Piet.Rypens" <rypens@reks.uia.ac.be>
1532 ;;(goto-char (point-min))
1533 ;;(while (re-search-forward mail-extr-bad-dot-pattern nil t)
1534 ;; (replace-match "\\1 \\2" t))
1536 (cond ((not (search-forward " " nil t
))
1537 (goto-char (point-min))
1538 (cond ((search-forward "_" nil t
)
1539 ;; Handle the *idiotic* use of underlines as spaces.
1540 ;; Example: fml@foo.bar.dom (First_M._Last)
1541 (goto-char (point-min))
1542 (while (search-forward "_" nil t
)
1543 (replace-match " " t
)))
1544 ((search-forward "." nil t
)
1545 ;; Fix . used as space
1546 ;; Example: danj1@cb.att.com (daniel.jacobson)
1547 (goto-char (point-min))
1548 (while (re-search-forward mail-extr-bad-dot-pattern nil t
)
1549 (replace-match "\\1 \\2" t
))))))
1551 ;; Loop over the words (and other junk) in the name.
1552 (goto-char (point-min))
1553 (while (not name-done-flag
)
1555 (cond (word-found-flag
1556 ;; Last time through this loop we skipped over a word.
1557 (setq last-word-beg this-word-beg
)
1558 (setq drop-last-word-if-trailing-flag
1559 drop-this-word-if-trailing-flag
)
1560 (setq word-found-flag nil
)))
1562 (cond (begin-again-flag
1563 ;; Last time through the loop we found something that
1564 ;; indicates we should pretend we are beginning again from
1567 (setq last-word-beg nil
)
1568 (setq drop-last-word-if-trailing-flag nil
)
1569 (setq mixed-case-flag nil
)
1570 (setq lower-case-flag nil
)
1571 ;; (setq upper-case-flag nil)
1572 (setq begin-again-flag nil
)
1575 ;; Initialize for this iteration of the loop.
1576 (mail-extr-skip-whitespace-forward)
1577 (if (eq word-count
0) (narrow-to-region (point) (point-max)))
1578 (setq this-word-beg
(point))
1579 (setq drop-this-word-if-trailing-flag nil
)
1581 ;; Decide what to do based on what we are looking at.
1585 ((and (eq word-count
0)
1586 (looking-at mail-extr-full-name-prefixes
))
1587 (goto-char (match-end 0))
1588 (narrow-to-region (point) (point-max)))
1590 ;; Stop after name suffix
1591 ((and (>= word-count
2)
1592 (looking-at mail-extr-full-name-suffix-pattern
))
1593 (mail-extr-skip-whitespace-backward)
1594 (setq suffix-flag
(point))
1595 (if (eq ?
, (following-char))
1598 ;; Enforce at least one space after comma
1599 (or (eq ?\
(following-char))
1601 (mail-extr-skip-whitespace-forward)
1602 (cond ((memq (following-char) '(?j ?J ?s ?S
))
1604 (if (eq (following-char) ?.
)
1609 (setq word-found-flag t
)
1610 (setq name-done-flag t
))
1613 ((looking-at "MKA \\(.+\\)") ; "Mundanely Known As"
1614 (goto-char (match-beginning 1))
1615 (narrow-to-region (point) (point-max))
1616 (setq begin-again-flag t
))
1618 ;; Check for initial last name followed by comma
1619 ((and (eq ?
, (following-char))
1622 (setq last-name-comma-flag t
)
1623 (or (eq ?\
(following-char))
1626 ;; Stop before trailing comma-separated comment
1627 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1628 ;; *** This case is redundant???
1629 ;;((eq ?, (following-char))
1630 ;; (setq name-done-flag t))
1632 ;; Delete parenthesized/quoted comment/nickname
1633 ((memq (following-char) '(?\
( ?\
{ ?\
[ ?
\" ?
\' ?\
`))
1635 (set-syntax-table mail-extr-address-text-comment-syntax-table
)
1636 (cond ((memq (following-char) '(?
\' ?\
`))
1637 (or (search-forward "'" nil t
1638 (if (eq ?
\' (following-char)) 2 1))
1639 (mail-extr-delete-char 1)))
1641 (or (mail-extr-safe-move-sexp 1)
1642 (goto-char (point-max)))))
1643 (set-syntax-table mail-extr-address-text-syntax-table
)
1646 ;; Handle case of entire name being quoted
1647 ((and (eq word-count
0)
1648 (looking-at " *\\'")
1649 (>= (- cend cbeg
) 2))
1650 (narrow-to-region (1+ cbeg
) (1- cend
))
1651 (goto-char (point-min)))
1653 ;; Handle case of quoted initial
1654 (if (and (or (= 3 (- cend cbeg
))
1655 (and (= 4 (- cend cbeg
))
1656 (eq ?.
(char-after (+ 2 cbeg
)))))
1657 (not (looking-at " *\\'")))
1658 (setq initial
(char-after (1+ cbeg
)))
1660 (delete-region cbeg cend
)
1662 (insert initial
". ")))))
1664 ;; Handle *Stupid* VMS date stamps
1665 ((looking-at mail-extr-stupid-vms-date-stamp-pattern
)
1666 (replace-match "" t
))
1668 ;; Handle Chinese characters.
1669 ((looking-at mail-extr-hz-embedded-gb-encoded-chinese-pattern
)
1670 (goto-char (match-end 0))
1671 (setq word-found-flag t
))
1673 ;; Skip initial garbage characters.
1674 ;; THIS CASE MUST BE AFTER THE PRECEDING CASES.
1675 ((and (eq word-count
0)
1676 (looking-at mail-extr-leading-garbage
))
1677 (goto-char (match-end 0))
1678 ;; *** Skip backward over these???
1679 ;; (skip-chars-backward "& \"")
1680 (narrow-to-region (point) (point-max)))
1682 ;; Various stopping points
1685 ;; Stop before ALL CAPS acronyms, if preceded by mixed-case
1686 ;; words. Example: XT-DEM.
1687 (and (>= word-count
2)
1689 (looking-at mail-extr-weird-acronym-pattern
)
1690 (not (looking-at mail-extr-roman-numeral-pattern
)))
1692 ;; Stop before trailing alternative address
1693 (looking-at mail-extr-alternative-address-pattern
)
1695 ;; Stop before trailing comment not introduced by comma
1696 ;; THIS CASE MUST BE AFTER AN EARLIER CASE.
1697 (looking-at mail-extr-trailing-comment-start-pattern
)
1699 ;; Stop before telephone numbers
1700 (looking-at mail-extr-telephone-extension-pattern
))
1701 (setq name-done-flag t
))
1703 ;; Delete ham radio call signs
1704 ((looking-at mail-extr-ham-call-sign-pattern
)
1705 (delete-region (match-beginning 0) (match-end 0)))
1708 ((looking-at mail-extr-initial-pattern
)
1709 (or (eq (following-char) (upcase (following-char)))
1710 (setq lower-case-flag t
))
1712 (if (eq ?.
(following-char))
1715 (or (eq ?\
(following-char))
1717 (setq word-found-flag t
))
1719 ;; Handle BITNET LISTSERV list names.
1720 ((and (eq word-count
0)
1721 (looking-at mail-extr-listserv-list-name-pattern
))
1722 (narrow-to-region (match-beginning 1) (match-end 1))
1723 (setq word-found-flag t
)
1724 (setq name-done-flag t
))
1726 ;; Handle & substitution, when & is last and is not first.
1727 ((and (> word-count
0)
1728 (eq ?\
(preceding-char))
1729 (eq (following-char) ?
&)
1730 (eq (1+ (point)) (point-max)))
1731 (mail-extr-delete-char 1)
1735 (insert-buffer-substring canonicalization-buffer
1738 (setq disable-initial-guessing-flag t
)
1739 (setq word-found-flag t
))
1741 ;; Handle & between names, as in "Bob & Susie".
1742 ((and (> word-count
0) (eq (following-char) ?\
&))
1743 (setq name-beg
(point))
1744 (setq name-end
(1+ name-beg
))
1745 (setq word-found-flag t
)
1746 (goto-char name-end
))
1748 ;; Regular name words
1749 ((looking-at mail-extr-name-pattern
)
1750 (setq name-beg
(point))
1751 (setq name-end
(match-end 0))
1753 ;; Certain words will be dropped if they are at the end.
1754 (and (>= word-count
2)
1755 (not lower-case-flag
)
1757 ;; A trailing 4-or-more letter lowercase words preceded by
1758 ;; mixed case or uppercase words will be dropped.
1759 (looking-at "[a-z][a-z][a-z][a-z]+[ \t]*\\'")
1760 ;; Drop a trailing word which is terminated with a period.
1761 (eq ?.
(char-after (1- name-end
))))
1762 (setq drop-this-word-if-trailing-flag t
))
1764 ;; Set the flags that indicate whether we have seen a lowercase
1765 ;; word, a mixed case word, and an uppercase word.
1766 (if (re-search-forward "[a-z]" name-end t
)
1768 (goto-char name-beg
)
1769 (re-search-forward "[A-Z]" name-end t
))
1770 (setq mixed-case-flag t
)
1771 (setq lower-case-flag t
))
1772 ;; (setq upper-case-flag t)
1775 (goto-char name-end
)
1776 (setq word-found-flag t
))
1779 (setq name-done-flag t
)
1782 ;; Count any word that we skipped over.
1784 (setq word-count
(1+ word-count
))))
1786 ;; If the last thing in the name is 2 or more periods, or one or more
1787 ;; other sentence terminators (but not a single period) then keep them
1788 ;; and the preceding word. This is for the benefit of whole sentences
1789 ;; in the name field: it's better behavior than dropping the last word
1790 ;; of the sentence...
1791 (if (and (not suffix-flag
)
1792 (looking-at "\\(\\.+\\|[?!;:.][?!;:.]+\\|[?!;:][?!;:.]*\\)\\'"))
1793 (goto-char (setq suffix-flag
(point-max))))
1795 ;; Drop everything after point and certain trailing words.
1796 (narrow-to-region (point-min)
1797 (or (and drop-last-word-if-trailing-flag
1801 ;; Xerox's mailers SUCK!!!!!!
1802 ;; We simply refuse to believe that any last name is PARC or ADOC.
1803 ;; If it looks like that is the last name, that there is no meaningful
1804 ;; here at all. Actually I guess it would be best to map patterns
1805 ;; like foo.hoser@xerox.com into foo@hoser.xerox.com, but I don't
1806 ;; actually know that that is what's going on.
1807 (cond ((not suffix-flag
)
1808 (goto-char (point-min))
1809 (let ((case-fold-search t
))
1810 (if (looking-at "[-A-Za-z_]+[. ]\\(PARC\\|ADOC\\)\\'")
1813 ;; If last name first put it at end (but before suffix)
1814 (cond (last-name-comma-flag
1815 (goto-char (point-min))
1816 (search-forward ",")
1817 (setq name-end
(1- (point)))
1818 (goto-char (or suffix-flag
(point-max)))
1819 (or (eq ?\
(preceding-char))
1821 (insert-buffer-substring (current-buffer) (point-min) name-end
)
1822 (goto-char name-end
)
1823 (skip-chars-forward "\t ,")
1824 (narrow-to-region (point) (point-max))))
1826 ;; Delete leading and trailing junk characters.
1827 ;; *** This is probably completely unneeded now.
1828 ;;(goto-char (point-max))
1829 ;;(skip-chars-backward mail-extr-non-end-name-chars)
1830 ;;(if (eq ?. (following-char))
1831 ;; (forward-char 1))
1832 ;;(narrow-to-region (point)
1834 ;; (goto-char (point-min))
1835 ;; (skip-chars-forward mail-extr-non-begin-name-chars)
1838 ;; Compress whitespace
1839 (goto-char (point-min))
1840 (while (re-search-forward "[ \t\n]+" nil t
)
1841 (replace-match (if (eobp) "" " ") t
))
1846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1848 ;; Table of top-level domain names.
1850 ;; This is used during address canonicalization; be careful of format changes.
1851 ;; Keep in mind that the country abbreviations follow ISO-3166. There is
1852 ;; a U.S. FIPS that specifies a different set of two-letter country
1855 (defconst mail-extr-all-top-level-domains
1856 (let ((ob (make-vector 509 0)))
1860 (put (intern (downcase (car x
)) ob
)
1863 (format (nth 2 x
) (nth 1 x
))
1868 ("ae" "United Arab Emirates")
1869 ("ag" "Antigua and Barbuda")
1873 ("aq" "Antarctica") ; continent
1874 ("ar" "Argentina" "Argentine Republic")
1875 ("at" "Austria" "The Republic of %s")
1878 ("ba" "Bosnia-Herzegovina")
1881 ("be" "Belgium" "The Kingdom of %s")
1882 ("bf" "Burkina Faso")
1886 ("bo" "Bolivia" "Republic of %s")
1887 ("br" "Brazil" "The Federative Republic of %s")
1894 ("ch" "Switzerland" "The Swiss Confederation")
1895 ("ci" "Ivory Coast")
1896 ("cl" "Chile" "The Republic of %s")
1897 ("cm" "Cameroon") ; In .fr domain
1898 ("cn" "China" "The People's Republic of %s")
1900 ("cr" "Costa Rica" "The Republic of %s")
1901 ("cs" "Czechoslovakia")
1904 ("cz" "Czech Republic")
1908 ("do" "Dominican Republic" "The %s")
1910 ("ec" "Ecuador" "The Republic of %s")
1912 ("eg" "Egypt" "The Arab Republic of %s")
1914 ("es" "Spain" "The Kingdom of %s")
1916 ("fi" "Finland" "The Republic of %s")
1917 ("fo" "Faroe Islands")
1920 ("gb" "United Kingdom")
1923 ("gf" "Guyana (Fr.)")
1927 ("gp" "Guadeloupe (Fr.)")
1928 ("gr" "Greece" "The Hellenic Republic (%s)")
1930 ("gu" "Guam (U.S.)")
1933 ("hr" "Croatia" "Croatia (Hrvatska)")
1935 ("hu" "Hungary" "The Hungarian Republic")
1938 ("il" "Israel" "The State of %s")
1939 ("im" "Isle of Man" "The %s")
1940 ("in" "India" "The Republic of %s")
1942 ("is" "Iceland" "The Republic of %s")
1943 ("it" "Italy" "The Italian Republic")
1948 ("kn" "St. Kitts, Nevis, and Anguilla")
1949 ("kp" "Korea (North)")
1950 ("kr" "Korea (South)")
1955 ("li" "Liechtenstein")
1956 ("lk" "Sri Lanka" "The Democratic Socialist Republic of %s")
1963 ("md" "Moldova" "The Republic of %s")
1972 ("mx" "Mexico" "The United Mexican States")
1973 ("my" "Malaysia" "%s (changed to Myanmar?)") ;???
1976 ("nc" "New Caledonia (Fr.)")
1977 ("ne" "Niger") ; In .fr domain
1978 ("ni" "Nicaragua" "The Republic of %s")
1979 ("nl" "Netherlands" "The Kingdom of the %s")
1980 ("no" "Norway" "The Kingdom of %s")
1981 ("np" "Nepal") ; Via .in domain
1983 ("nz" "New Zealand")
1986 ("pf" "Polynesia (Fr.)")
1987 ("pg" "Papua New Guinea")
1988 ("ph" "Philippines" "The Republic of the %s")
1991 ("pr" "Puerto Rico (U.S.)")
1992 ("pt" "Portugal" "The Portuguese Republic")
1995 ("re" "Reunion (Fr.)") ; In .fr domain
1997 ("ru" "Russian Federation")
1998 ("sa" "Saudi Arabia")
2001 ("se" "Sweden" "The Kingdom of %s")
2002 ("sg" "Singapore" "The Republic of %s")
2004 ("sj" "Svalbard and Jan Mayen Is.") ; In .no domain
2005 ("sk" "Slovakia" "The Slovak Republic")
2009 ("su" "U.S.S.R." "The Union of Soviet Socialist Republics")
2012 ("th" "Thailand" "The Kingdom of %s")
2013 ("tm" "Turkmenistan") ; In .su domain
2016 ("tr" "Turkey" "The Republic of %s")
2017 ("tt" "Trinidad and Tobago")
2020 ("uk" "United Kingdom" "The %s of Great Britain and Northern Ireland")
2021 ("us" "United States" "The %s of America")
2022 ("uy" "Uruguay" "The Eastern Republic of %s")
2023 ("vc" "St. Vincent and the Grenadines")
2024 ("ve" "Venezuela" "The Republic of %s")
2025 ("vi" "Virgin Islands (U.S.)")
2028 ("yu" "Yugoslavia" "Yugoslavia, AKA Serbia-Montenegro")
2029 ("za" "South Africa" "The Republic of %s")
2030 ("zw" "Zimbabwe" "Republic of %s")
2031 ;; Special top-level domains:
2032 ("arpa" t
"Advanced Research Projects Agency (U.S. DoD)")
2033 ("bitnet" t
"Because It's Time NET")
2034 ("com" t
"Commercial")
2035 ("edu" t
"Educational")
2036 ("gov" t
"Government (U.S.)")
2037 ("int" t
"International (NATO)")
2038 ("mil" t
"Military (U.S.)")
2039 ("nato" t
"North Atlantic Treaty Organization")
2041 ("org" t
"Non-profit Organization")
2042 ;;("unter-dom" t "? (Ger.)")
2043 ("uucp" t
"Unix to Unix CoPy")
2044 ;;("fipnet" nil "?")
2049 (defun what-domain (domain)
2050 "Convert mail domain DOMAIN to the country it corresponds to."
2052 (let ((completion-ignore-case t
))
2053 (list (completing-read "Domain: "
2054 mail-extr-all-top-level-domains nil t
))))
2055 (or (setq domain
(intern-soft (downcase domain
)
2056 mail-extr-all-top-level-domains
))
2057 (error "No such domain"))
2058 (message "%s: %s" (upcase (symbol-name domain
)) (get domain
'domain-name
)))
2062 ; (mapatoms #'(lambda (x)
2063 ; (if (and (boundp x)
2064 ; (string-match "^mail-extr-" (symbol-name x)))
2065 ; (setq all (cons x all)))))
2066 ; (setq all (sort all #'string-lessp))
2068 ; (apply 'nconc (mapcar #'(lambda (x)
2069 ; (list x (symbol-value x)))
2073 (provide 'mail-extr
)
2075 ;;; mail-extr.el ends here