02957b8e6366c222d4ccddb2ac3b984040abf3b5
[elisp/wanderlust.git] / wl / wl-address.el
1 ;;; wl-address.el -- Tiny address management for Wanderlust.
2
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;;                          Shun-ichi GOTO <gotoh@taiyo.co.jp>
5 ;;                          Takeshi Chiba <chiba@d3.bs1.fc.nec.co.jp>
6
7 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
8 ;;      Shun-ichi GOTO <gotoh@taiyo.co.jp>
9 ;;      Takeshi Chiba <chiba@d3.bs1.fc.nec.co.jp>
10 ;; Keywords: mail, net news
11
12 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
13
14 ;; This program is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18 ;;
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23 ;;
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28 ;;
29
30 ;;; Commentary:
31 ;; 
32
33 ;;; Code:
34 ;; 
35
36 (require 'wl-util)
37 (require 'wl-vars)
38 (require 'std11)
39
40 (defvar wl-address-complete-header-regexp "^\\(To\\|From\\|Cc\\|Bcc\\|Mail-Followup-To\\|Reply-To\\|Return-Receipt-To\\):")
41 (defvar wl-newsgroups-complete-header-regexp "^\\(Newsgroups\\|Followup-To\\):")
42 (defvar wl-folder-complete-header-regexp "^\\(FCC\\):")
43 (defvar wl-address-list nil)
44 (defvar wl-address-completion-list nil)
45 (defvar wl-address-petname-hash nil)
46
47 (defvar wl-address-ldap-search-hash nil)
48
49 (eval-when-compile (require 'pldap))
50
51 (defvar wl-ldap-alias-dn-level nil
52 "Level of dn data to make alias postfix.
53 Valid value is nit, t, 1 or larget integer.
54
55 If this value nil, minimum alias postfix is made depends on uniqness
56 with other candidates.  In this implementation, it's same to 1.  If t,
57 always append all dn data.  If number, always append spcified level of
58 data but maybe appended more uniqness.  If invalid value, treat as
59 nil.
60
61 For example, following dn data is exsist, alias of each level is shown
62 bellow.
63
64 Match: Goto
65 dn: CN=Shun-ichi GOTO,OU=Mew,OU=Emacs,OU=Lisper,O=Programmers Inc.
66   nil => Goto/Shun-ichi_GOTO
67     1 => Goto/Shun-ichi_GOTO
68     2 => Goto/Shun-ichi_GOTO/Mew
69     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
70     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
71     5 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
72     6 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
73     t => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
74
75 If level 3 is required for uniqness with other candidates,
76   nil => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
77     1 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
78     2 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
79     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
80     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
81     (so on...)")
82
83 (defconst wl-ldap-alias-sep "/")
84
85 (defconst wl-ldap-search-attribute-type-list
86   '("sn" "cn" "mail"))
87
88 (defun wl-ldap-get-value (type entry)
89   ""
90   (let* ((values (cdr (assoc type entry)))
91          (ret (car values)))
92     (if (and ret (not ldap-ignore-attribute-codings))
93         (while values
94           (if (not (string-match "^[\000-\177]*$" (car values)))
95               (setq ret (car values)
96                     values nil)
97             (setq values (cdr values)))))
98     ret))
99           
100 (defun wl-ldap-get-value-list (type entry)
101   ""
102   (cdr (assoc type entry)))
103
104 (defun wl-ldap-make-filter (pat type-list)
105   "Make RFC1558 quiery filter for PAT from ATTR-LIST.
106 Each are \"OR\" combination, and PAT is beginning-match."
107   (concat "(&(objectclass=person)(|"
108           (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
109                      type-list
110                      "")
111           "))"))
112
113 (defun wl-ldap-make-matched-value-list (regexp type-list entry)
114   "Correct matching WORD with value of TYPE-LIST in ENTRY.
115 Returns matched uniq string list."
116   (let (type val values result)
117     ;; collect matching value
118     (while entry
119       (setq type (car (car entry))
120             values (mapcar (function wl-ldap-alias-safe-string)
121                            (cdr (car entry)))
122             entry (cdr entry))
123       (if (string-match "::?$" type)
124           (setq type (substring type 0 (match-beginning 0))))
125       (if (member type type-list)
126           (while values
127             (setq val (car values)
128                   values (cdr values))
129             (if (and (string-match regexp val)
130                      (not (member val result)))
131                 (setq result (cons val result))))))
132     result))
133
134 (defun wl-ldap-alias-safe-string (str)
135   "Modify STR for alias.
136 Replace space/tab in STR into '_' char.
137 Replace '@' in STR into '/' char."
138   (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
139     (setq str (concat (substring str 0 (match-beginning 0))
140                       "_"
141                       (substring str (match-end 0)))))
142   (if (string-match "\\(@\\)[^/@]+" str)
143       (setq str (concat (substring str 0 (match-beginning 1))
144                         "/"
145                         (substring str (match-end 1)))))
146   str)
147
148 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
149   ""
150   (let (sym dnsym value level)
151     (setq dnsym (intern (upcase dn) hash))
152     (if (and (null str) (boundp dnsym))
153         ()                                      ; already processed
154       ;; make dn-list in fisrt time
155       (if (null dn-list)
156           (let ((case-fold-search t))
157             (setq dn-list (mapcar (lambda (str)
158                                     (if (string-match "[a-z]+=\\(.*\\)" str)
159                                         (wl-ldap-alias-safe-string
160                                          (wl-match-string 1 str))))
161                                   (split-string dn ",")))))
162       ;; prepare candidate for uniq str
163       (if str
164           (setq str (concat str wl-ldap-alias-sep (car dn-list))
165                 dn-list (cdr dn-list))
166         ;; first entry, pre-build with given level
167         (cond
168          ((null wl-ldap-alias-dn-level) (setq level 1))
169          ((eq t wl-ldap-alias-dn-level) (setq level 1000)) ; xxx, big enough
170          ((numberp wl-ldap-alias-dn-level)
171           (if (< 0 wl-ldap-alias-dn-level)
172               (setq level  wl-ldap-alias-dn-level)
173             (setq level 1)))
174          (t
175           (setq level 1)))
176         (while (and (< 0 level) dn-list)
177           (if (null str)
178               (setq str (car dn-list))
179             (setq str (concat str wl-ldap-alias-sep (car dn-list))))
180           (setq level (1- level)
181                 dn-list (cdr dn-list))))
182       (setq sym (intern (upcase str) hash))
183       (if (not (boundp sym))
184           ;; good
185           (progn (set sym (list dn str dn-list))
186                  (set dnsym str))
187         ;; conflict
188         (if (not (eq (setq value (symbol-value sym)) t))
189             ;; move away deeper
190             (progn (set sym t)
191                    (apply (function wl-ldap-register-dn-string) hash value)))
192         (wl-ldap-register-dn-string hash dn str dn-list)))))
193
194 (defun wl-address-ldap-search (pattern cl)
195   "Make address completion-list matched for PATTERN by LDAP search.
196 Matched address lists are append to CL."
197   (require 'pldap)
198   (unless wl-address-ldap-search-hash
199     (setq wl-address-ldap-search-hash (elmo-make-hash 7)))
200   (let ((pat (if (string-match wl-ldap-alias-sep pattern)
201                  (substring pattern 0 (match-beginning 0))
202                pattern))
203         (ldap-default-host wl-ldap-server)
204         (ldap-default-port (or wl-ldap-port 389))
205         (ldap-default-base wl-ldap-base)
206         (dnhash (elmo-make-hash))
207         cache len sym tmpl regexp entries ent values dn dnstr alias
208         result cn mails)
209     ;; check cache
210     (mapatoms (lambda (atom)
211                 (if (and (string-match
212                           (concat "^" (symbol-name atom) ".*") pat)
213                          (or (null cache)
214                              (< (car cache)
215                                 (setq len (length (symbol-name atom))))))
216                     (setq cache (cons
217                                  (or len (length (symbol-name atom)))
218                                  (symbol-value atom)))))
219               wl-address-ldap-search-hash)
220     ;; get matched entries
221     (if cache
222         (setq entries (cdr cache))
223       (condition-case nil
224           (progn
225             (message "Searching in LDAP...")
226             (setq entries (ldap-search-entries
227                            (wl-ldap-make-filter
228                             (concat pat "*")
229                             wl-ldap-search-attribute-type-list)
230                            nil wl-ldap-search-attribute-type-list nil t))
231             (message "Searching in LDAP...done")
232             (elmo-set-hash-val pattern entries wl-address-ldap-search-hash))
233         (error (message ""))))                  ; ignore error: No such object
234     ;;
235     (setq tmpl entries)
236     (while tmpl
237       (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
238       (setq tmpl (cdr tmpl)))
239     ;;
240     (setq regexp (concat "^" pat))
241     (while entries
242       (setq ent (cdar entries)
243             values (wl-ldap-make-matched-value-list
244                     regexp '("mail" "sn" "cn") ent)
245             mails (wl-ldap-get-value-list "mail" ent)
246             cn (wl-ldap-get-value "cn" ent)
247             dn (car (car entries))
248             dnstr (elmo-get-hash-val (upcase dn) dnhash))
249       ;; make alias list generated from LDAP data.
250       (while (and mails values)
251         ;; make alias like MATCHED/DN-STRING
252         (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
253             (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
254           ;; use DN-STRING if DN-STRING begin with MATCHED
255           (setq alias dnstr))
256         ;; check uniqness then add to list
257         (setq sym (intern (downcase alias) dnhash))
258         (when (not (boundp sym))
259           (set sym alias)
260           (setq result (cons (cons alias
261                                    (concat cn " <" (car mails) ">"))
262                              result)))
263         (setq values (cdr values)))
264       ;; make mail addrses list
265       (while mails
266         (if (null (assoc (car mails) cl)); Not already in cl.
267             ;; (string-match regexp (car mails))
268             ;; add mail address itself to completion list
269             (setq result (cons (cons (car mails)
270                                      (concat cn " <" (car mails) ">"))
271                                result)))
272         (setq mails (cdr mails)))
273       (setq entries (cdr entries)))
274     (append result cl)))
275
276 (defun wl-complete-field-to ()
277   (interactive)
278   (let ((cl wl-address-completion-list))
279     (if cl
280         (completing-read "To: " cl)
281       (read-string "To: "))))
282
283 (defun wl-address-quote-specials (word)
284   "Make quoted string of WORD if needed."
285   (if (assq 'specials (std11-lexical-analyze word))
286       (prin1-to-string word)
287     word))
288
289 (defun wl-address-make-completion-list (address-list)
290   (let (addr-tuple cl)
291     (while address-list
292       (setq addr-tuple (car address-list))
293       (setq cl
294              (cons
295               (cons (nth 0 addr-tuple)
296                     (concat
297                      (wl-address-quote-specials
298                       (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
299               cl))
300       ;; nickname completion.
301       (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
302                   ;; already exists
303                   (assoc (nth 1 addr-tuple) cl))
304         (setq cl
305               (cons
306                (cons (nth 1 addr-tuple)
307                      (concat
308                       (wl-address-quote-specials
309                        (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
310                cl)))
311       (setq address-list (cdr address-list)))
312     cl))
313
314 (defun wl-complete-field-body-or-tab ()
315   (interactive)
316   (let ((case-fold-search t)
317         epand-char skip-chars
318         (use-ldap nil)
319         completion-list)
320     (if (wl-draft-on-field-p)
321         (wl-complete-field)
322       (if (and
323            (< (point)
324               (save-excursion
325                 (goto-char (point-min))
326                 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
327                 (point)))
328            (save-excursion
329              (beginning-of-line)
330              (setq use-ldap nil)
331              (while (and (looking-at "^[ \t]")
332                          (not (= (point) (point-min))))
333                (forward-line -1))
334              (cond ((looking-at wl-address-complete-header-regexp)
335                     (setq completion-list wl-address-completion-list)
336                     (if wl-use-ldap
337                         (setq use-ldap t))
338                     (setq epand-char ?@))
339                    ((looking-at wl-folder-complete-header-regexp)
340                     (setq completion-list wl-folder-entity-hashtb)
341                     (setq skip-chars "^, "))
342                    ((looking-at wl-newsgroups-complete-header-regexp)
343                     (setq completion-list wl-folder-newsgroups-hashtb)))))
344           (wl-complete-field-body completion-list
345                                   epand-char skip-chars use-ldap)
346         (indent-for-tab-command)))))
347
348 (defvar wl-completion-buf-name "*Completions*")
349
350 (defvar wl-complete-candidates nil)
351
352 (defun wl-complete-window-show (all)
353   (if (and (get-buffer-window wl-completion-buf-name)
354            (equal wl-complete-candidates all))
355       (let ((win (get-buffer-window wl-completion-buf-name)))
356         (save-excursion
357           (set-buffer wl-completion-buf-name)
358           (if (pos-visible-in-window-p (point-max) win)
359               (set-window-start win 1)
360             (scroll-other-window))))
361     (message "Making completion list...")
362     (setq wl-complete-candidates all)
363     (with-output-to-temp-buffer
364         wl-completion-buf-name
365       (display-completion-list all))
366     (message "Making completion list... done")))
367
368 (defun wl-complete-window-delete ()
369   (let (comp-buf comp-win)
370     (if (setq comp-buf (get-buffer wl-completion-buf-name))
371         (if (setq comp-win (get-buffer-window comp-buf))
372             (delete-window comp-win)))))
373
374 (defun wl-complete-field ()
375   (interactive)
376   (let* ((end (point))
377          (start (save-excursion
378                   (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
379                   (point)))
380          (completion)
381          (pattern (buffer-substring start end))
382          (cl wl-draft-field-completion-list))
383     (if (null cl)
384         nil
385       (setq completion
386             (let ((completion-ignore-case t))
387               (try-completion pattern cl)))
388       (cond ((eq completion t)
389              (let ((alias (assoc pattern cl)))
390                (if alias
391                    (progn
392                      (delete-region start end)
393                      (insert (cdr alias))
394 ;;;                  (wl-highlight-message (point-min)(point-max) t)
395                      )))
396              (wl-complete-window-delete))
397             ((null completion)
398              (message "Can't find completion for \"%s\"" pattern)
399              (ding))
400             ((not (string= pattern completion))
401              (delete-region start end)
402              (insert completion)
403              (wl-complete-window-delete)
404              (wl-highlight-message (point-min)(point-max) t))
405             (t
406              (let ((list (all-completions pattern cl)))
407                (wl-complete-window-show list)))))))
408
409 (defun wl-complete-insert (start end pattern completion-list)
410   (let ((alias (and (consp completion-list)
411                     (assoc pattern completion-list)))
412         comp-buf comp-win)
413     (if alias
414         (progn
415           (delete-region start end)
416           (insert (cdr alias))
417           (if (setq comp-buf (get-buffer wl-completion-buf-name))
418               (if (setq comp-win (get-buffer-window comp-buf))
419                   (delete-window comp-win)))))))
420
421 (defun wl-complete-field-body (completion-list
422                                &optional epand-char skip-chars use-ldap)
423   (interactive)
424   (let* ((end (point))
425          (start (save-excursion
426                   (skip-chars-backward (or skip-chars "^:,>\n"))
427                   (skip-chars-forward " \t")
428                   (point)))
429          (completion)
430          (pattern (buffer-substring start end))
431          (len (length pattern))
432          (cl completion-list))
433     (when use-ldap
434       (setq cl (wl-address-ldap-search pattern cl)))
435     (if (null cl)
436         nil
437       (setq completion (try-completion pattern cl))
438       (cond ((eq completion t)
439              (if use-ldap (setq wl-address-ldap-search-hash nil))
440              (wl-complete-insert start end pattern cl)
441              (wl-complete-window-delete)
442              (message "Sole completion"))
443             ((and epand-char
444                   (> len 0)
445                   (char-equal (aref pattern (1- len)) epand-char)
446                   (assoc (substring pattern 0 (1- len)) cl))
447              (wl-complete-insert
448               start end
449               (substring pattern 0 (1- len))
450               cl))
451             ((null completion)
452              (message "Can't find completion for \"%s\"" pattern)
453              (ding))
454             ((not (string= pattern completion))
455              (delete-region start end)
456              (insert completion))
457             (t
458              (let ((list (sort (all-completions pattern cl) 'string<)))
459                (wl-complete-window-show list)))))))
460
461 (defvar wl-address-init-func 'wl-local-address-init)
462
463 (defun wl-address-init ()
464   "Call `wl-address-init-func'."
465   (funcall wl-address-init-func))
466
467 (defun wl-local-address-init ()
468   "Reload `wl-address-file'.
469 Refresh `wl-address-list', `wl-address-completion-list', and
470 `wl-address-petname-hash'."
471   (message "Updating addresses...")
472   (setq wl-address-list
473         (wl-address-make-address-list wl-address-file))
474   (setq wl-address-completion-list
475         (wl-address-make-completion-list wl-address-list))
476   (if (file-readable-p wl-alias-file)
477       (setq wl-address-completion-list
478             (append wl-address-completion-list
479                     (wl-address-make-alist-from-alias-file wl-alias-file))))
480   (setq wl-address-petname-hash (elmo-make-hash))
481   (let ((addresses wl-address-list))
482     (while addresses
483       (elmo-set-hash-val (downcase (car (car addresses)))
484                          (cadr (car addresses))
485                          wl-address-petname-hash)
486       (setq addresses (cdr addresses))))
487   (message "Updating addresses...done"))
488
489
490 (defun wl-address-expand-aliases (alist nest-count)
491   (when (< nest-count 5)
492     (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
493       (while (setq expn-str (cdr (nth n alist)))
494         (setq new-expn-str nil)
495         (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
496           (setq expn (elmo-match-string 1 expn-str))
497           (setq expn-str (wl-string-delete-match expn-str 0))
498           (if (string-match "^[ \t,]+" expn-str)
499               (setq expn-str (wl-string-delete-match expn-str 0)))
500           (if (string-match "[ \t,]+$" expn)
501               (setq expn (wl-string-delete-match expn 0)))
502           (setq new-expn (cdr (assoc expn alist)))
503           (if new-expn
504               (setq expanded t))
505           (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
506                                      (or new-expn expn))))
507         (when new-expn-str
508           (setcdr (nth n alist) new-expn-str))
509         (setq n (1+ n)))
510       (and expanded
511            (wl-address-expand-aliases alist (1+ nest-count))))))
512
513 (defun wl-address-make-alist-from-alias-file (file)
514   (elmo-set-work-buf
515     (let ((case-fold-search t)
516           alias expn alist)
517       (insert-file-contents file)
518       (while (re-search-forward ",$" nil t)
519         (end-of-line)
520         (forward-char 1)
521         (delete-backward-char 1))
522       (goto-char (point-min))
523       (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
524         (setq alias (wl-match-buffer 1)
525               expn (wl-match-buffer 2))
526         (setq alist (cons (cons alias expn) alist)))
527       (wl-address-expand-aliases alist 0)
528       (nreverse alist) ; return value
529       )))
530         
531 (defun wl-address-make-address-list (path)
532   (if (and path (file-readable-p path))
533       (elmo-set-work-buf
534         (let (ret
535               (coding-system-for-read wl-cs-autoconv))
536           (insert-file-contents path)
537           (goto-char (point-min))
538           (while (not (eobp))
539             (if (looking-at
540  "^\\([^#\n][^ \t\n]+\\)[ \t]+\"\\(.*\\)\"[ \t]+\"\\(.*\\)\"[ \t]*.*$")
541                 (setq ret
542                       (wl-append-element
543                        ret
544                        (list (wl-match-buffer 1)
545                              (wl-match-buffer 2)
546                              (wl-match-buffer 3)))))
547             (forward-line))
548           ret))))
549
550 (defsubst wl-address-get-petname-1 (string)
551   (let ((address (downcase (wl-address-header-extract-address string))))
552     (elmo-get-hash-val address wl-address-petname-hash)))
553
554 (defsubst wl-address-get-petname (string)
555   (or (wl-address-get-petname-1 string)
556       string))
557
558 (defsubst wl-address-user-mail-address-p (address)
559   "Judge whether ADDRESS is user's or not."
560   (member (downcase (wl-address-header-extract-address address))
561           (or (mapcar 'downcase wl-user-mail-address-list)
562               (list (downcase
563                      (wl-address-header-extract-address
564                       wl-from))))))
565
566 (defsubst wl-address-header-extract-address (str)
567   "Extracts a real e-mail address from STR and return it.
568 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
569   ->  \"m-sakura@ccs.mt.nec.co.jp\".
570 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
571   ->  \"m-sakura@ccs.mt.nec.co.jp\"."
572   (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
573          (wl-match-string 1 str))
574         ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
575          (wl-match-string 1 str))
576         (t str)))
577
578 (defsubst wl-address-header-extract-realname (str)
579   "Extracts a real name from STR and return it.
580 e.g. \"Mr. bar <hoge@foo.com>\"
581   ->  \"Mr. bar\"."
582   (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
583          (wl-match-string 1 str))
584         (t "")))
585
586 (defmacro wl-address-concat-token (string token)
587   (` (cond
588       ((eq 'quoted-string (car (, token)))
589        (concat (, string) "\"" (cdr (, token)) "\""))
590       ((eq 'comment (car (, token)))
591        (concat (, string) "(" (cdr (, token)) ")"))
592       (t
593        (concat (, string) (cdr (, token)))))))
594
595 (defun wl-address-string-without-group-list-contents (sequence)
596   "Return address string from lexical analyzed list SEQUENCE.
597 Group list contents is not included."
598   (let (address-string route-addr-end token seq group-end)
599   (while sequence
600     (setq token (car sequence))
601     (cond
602      ;;   group       =  phrase ":" [#mailbox] ";"
603      ((and (eq 'specials (car token))
604            (string= (cdr token) ":"))
605       (setq address-string (concat address-string (cdr token))) ; ':'
606       (setq seq (cdr sequence))
607       (setq token (car seq))
608       (setq group-end nil)
609       (while (not group-end)
610         (setq token (car seq))
611         (setq seq (cdr seq))
612         (setq group-end (and (eq 'specials (car token))
613                              (string= (cdr token) ";"))))
614       (setq address-string (concat address-string (cdr token))) ; ';'
615       (setq sequence seq))
616      ;;   route-addr  =  "<" [route] addr-spec ">"
617      ;;   route       =  1#("@" domain) ":"           ; path-relative
618      ((and (eq 'specials (car token))
619            (string= (cdr token) "<"))
620       (setq seq (std11-parse-route-addr sequence))
621       (setq route-addr-end (car (cdr seq)))
622       (while (not (eq (car sequence) route-addr-end))
623         (setq address-string (wl-address-concat-token address-string
624                                                       (car sequence)))
625         (setq sequence (cdr sequence))))
626      (t
627       (setq address-string (wl-address-concat-token address-string token))
628       (setq sequence (cdr sequence)))))
629   address-string))
630
631 (defun wl-address-petname-delete (the-email)
632   "Delete petname in `wl-address-file'."
633   (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
634           (output-coding-system
635            (mime-charset-to-coding-system wl-mime-charset)))
636     (set-buffer tmp-buf)
637     (message "Deleting Petname...")
638     (erase-buffer)
639     (insert-file-contents wl-address-file)
640     (delete-matching-lines (concat "^[ \t]*" the-email))
641     (write-region (point-min) (point-max)
642                   wl-address-file nil 'no-msg)
643     (message "Deleting Petname...done")
644     (kill-buffer tmp-buf)))
645
646
647 (defun wl-address-petname-add-or-change (the-email
648                                          default-petname
649                                          default-realname
650                                          &optional change-petname)
651   "Add petname to `wl-address-file', if not registerd.
652 If already registerd, change it."
653   (let (the-realname the-petname)
654
655     ;; setup output "petname"
656     ;; if null petname'd, let default-petname be the petname.
657     (setq the-petname
658           (read-from-minibuffer (format "Petname: ") default-petname))
659     (if (string= the-petname "")
660         (setq the-petname (or default-petname the-email)))
661
662     ;; setup output "realname"
663     (setq the-realname
664         (read-from-minibuffer (format "Real Name: ") default-realname))
665 ;;;     (if (string= the-realname "")
666 ;;;         (setq the-realname default-petname))
667
668     ;; writing to ~/.address
669     (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
670            (output-coding-system (mime-charset-to-coding-system wl-mime-charset)))
671       (set-buffer tmp-buf)
672       (message "Adding Petname...")
673       (erase-buffer)
674       (if (file-exists-p wl-address-file)
675           (insert-file-contents wl-address-file))
676       (if (not change-petname)
677           ;; if only add
678           (progn
679             (goto-char (point-max))
680             (if (and (> (buffer-size) 0)
681                      (not (eq (char-after (1- (point-max))) ?\n)))
682                 (insert "\n")))
683         ;; if change
684         (if (re-search-forward (concat "^[ \t]*" the-email) nil t)
685             (delete-region (save-excursion (beginning-of-line)
686                                            (point))
687                            (save-excursion (end-of-line)
688                                            (+ 1 (point))))))
689       (insert (format "%s\t\"%s\"\t\"%s\"\n"
690                       the-email the-petname the-realname))
691       (write-region (point-min) (point-max)
692                     wl-address-file nil 'no-msg)
693       (message "Adding Petname...done")
694       (kill-buffer tmp-buf))))
695
696 (require 'product)
697 (product-provide (provide 'wl-address) (require 'wl-version))
698
699 ;;; wl-address.el ends here
700