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