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