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