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