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