9d589f905e5ab348e89e12f569c12178f99088f1
[elisp/wanderlust.git] / wl / wl-address.el
1 ;;; wl-address.el -- Tiny address management for Wanderlust.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Shun-ichi GOTO <gotoh@taiyo.co.jp>
5 ;; Copyright (C) 1998,1999,2000 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=" wl-ldap-objectclass ")(|"
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             values (elmo-flatten values)
123             entry (cdr entry))
124       (if (string-match "::?$" type)
125           (setq type (substring type 0 (match-beginning 0))))
126       (if (member type type-list)
127           (while values
128             (setq val (car values)
129                   values (cdr values))
130             (if (and (string-match regexp val)
131                      (not (member val result)))
132                 (setq result (cons val result))))))
133     result))
134
135 (defun wl-ldap-alias-safe-string (str)
136   "Modify STR for alias.
137 Replace space/tab in STR into '_' char.
138 Replace '@' in STR into list of mailbox and sub-domains."
139   (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
140     (setq str (concat (substring str 0 (match-beginning 0))
141                       "_"
142                       (substring str (match-end 0)))))
143   (if (string-match "\\(@\\)[^/@]+" str)
144       (setq str (split-string str  "[@\\.]")))
145   str)
146
147 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
148   ""
149   (let (sym dnsym value level)
150     (setq dnsym (intern (upcase dn) hash))
151     (if (and (null str) (boundp dnsym))
152         ()                                      ; already processed
153       ;; make dn-list in fisrt time
154       (if (null dn-list)
155           (let ((case-fold-search t))
156             (setq dn-list (mapcar (lambda (str)
157                                     (if (string-match "[a-z]+=\\(.*\\)" str)
158                                         (wl-ldap-alias-safe-string
159                                          (wl-match-string 1 str))))
160                                   (split-string dn "[ \t]*,[ \t]*")))))
161       (setq dn-list (elmo-flatten dn-list))
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 (defconst wl-address-specials-regexp "[]\"(),.:;<>@[\\]")
284
285 (defun wl-address-quote-specials (word)
286   "Make quoted string of WORD if needed."
287   (if (string-match wl-address-specials-regexp word)
288       (prin1-to-string word)
289     word))
290
291 (defun wl-address-make-completion-list (address-list)
292   (let (addr-tuple cl)
293     (while address-list
294       (setq addr-tuple (car address-list))
295       (setq cl
296              (cons
297               (cons (nth 0 addr-tuple)
298                     (concat
299                      (wl-address-quote-specials
300                       (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
301               cl))
302       ;; nickname completion.
303       (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
304                   ;; already exists
305                   (assoc (nth 1 addr-tuple) cl))
306         (setq cl
307               (cons
308                (cons (nth 1 addr-tuple)
309                      (concat
310                       (wl-address-quote-specials
311                        (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
312                cl)))
313       (setq address-list (cdr address-list)))
314     cl))
315
316 (defun wl-complete-field-body-or-tab ()
317   (interactive)
318   (let ((case-fold-search t)
319         epand-char skip-chars
320         (use-ldap nil)
321         completion-list)
322     (if (wl-draft-on-field-p)
323         (wl-complete-field)
324       (if (and
325            (< (point)
326               (save-excursion
327                 (goto-char (point-min))
328                 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
329                 (point)))
330            (save-excursion
331              (beginning-of-line)
332              (setq use-ldap nil)
333              (while (and (looking-at "^[ \t]")
334                          (not (= (point) (point-min))))
335                (forward-line -1))
336              (cond ((looking-at wl-address-complete-header-regexp)
337                     (setq completion-list wl-address-completion-list)
338                     (if wl-use-ldap
339                         (setq use-ldap t))
340                     (setq epand-char ?@))
341                    ((looking-at wl-folder-complete-header-regexp)
342                     (setq completion-list wl-folder-entity-hashtb)
343                     (setq skip-chars "^, "))
344                    ((looking-at wl-newsgroups-complete-header-regexp)
345                     (setq completion-list wl-folder-newsgroups-hashtb)))))
346           (wl-complete-field-body completion-list
347                                   epand-char skip-chars use-ldap)
348         (indent-for-tab-command)))))
349
350 (defvar wl-completion-buf-name "*Completions*")
351
352 (defvar wl-complete-candidates nil)
353
354 (defun wl-complete-window-show (all)
355   (if (and (get-buffer-window wl-completion-buf-name)
356            (equal wl-complete-candidates all))
357       (let ((win (get-buffer-window wl-completion-buf-name)))
358         (save-excursion
359           (set-buffer wl-completion-buf-name)
360           (if (pos-visible-in-window-p (point-max) win)
361               (set-window-start win 1)
362             (scroll-other-window))))
363     (message "Making completion list...")
364     (setq wl-complete-candidates all)
365     (with-output-to-temp-buffer
366         wl-completion-buf-name
367       (display-completion-list all))
368     (message "Making completion list... done")))
369
370 (defun wl-complete-window-delete ()
371   (let (comp-buf comp-win)
372     (if (setq comp-buf (get-buffer wl-completion-buf-name))
373         (if (setq comp-win (get-buffer-window comp-buf))
374             (delete-window comp-win)))))
375
376 (defun wl-complete-field ()
377   (interactive)
378   (let* ((end (point))
379          (start (save-excursion
380                   (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
381                   (point)))
382          (completion)
383          (pattern (buffer-substring start end))
384          (cl wl-draft-field-completion-list))
385     (if (null cl)
386         nil
387       (setq completion
388             (let ((completion-ignore-case t))
389               (try-completion pattern cl)))
390       (cond ((eq completion t)
391              (let ((alias (assoc pattern cl)))
392                (if alias
393                    (progn
394                      (delete-region start end)
395                      (insert (cdr alias))
396 ;;;                  (wl-highlight-message (point-min)(point-max) t)
397                      )))
398              (wl-complete-window-delete))
399             ((null completion)
400              (message "Can't find completion for \"%s\"" pattern)
401              (ding))
402             ((not (string= pattern completion))
403              (delete-region start end)
404              (insert completion)
405              (wl-complete-window-delete)
406              (wl-highlight-message (point-min)(point-max) t))
407             (t
408              (let ((list (all-completions pattern cl)))
409                (wl-complete-window-show list)))))))
410
411 (defun wl-complete-insert (start end pattern completion-list)
412   (let ((alias (and (consp completion-list)
413                     (assoc pattern completion-list)))
414         comp-buf comp-win)
415     (if alias
416         (progn
417           (delete-region start end)
418           (insert (cdr alias))
419           (if (setq comp-buf (get-buffer wl-completion-buf-name))
420               (if (setq comp-win (get-buffer-window comp-buf))
421                   (delete-window comp-win)))))))
422
423 (defun wl-complete-field-body (completion-list
424                                &optional epand-char skip-chars use-ldap)
425   (interactive)
426   (let* ((end (point))
427          (start (save-excursion
428                   (skip-chars-backward (or skip-chars "^:,>\n"))
429                   (skip-chars-forward " \t")
430                   (point)))
431          (completion)
432          (pattern (buffer-substring start end))
433          (len (length pattern))
434          (cl completion-list))
435     (when use-ldap
436       (setq cl (wl-address-ldap-search pattern cl)))
437     (if (null cl)
438         nil
439       (setq completion (try-completion pattern cl))
440       (cond ((eq completion t)
441              (if use-ldap (setq wl-address-ldap-search-hash nil))
442              (wl-complete-insert start end pattern cl)
443              (wl-complete-window-delete)
444              (message "Sole completion"))
445             ((and epand-char
446                   (> len 0)
447                   (char-equal (aref pattern (1- len)) epand-char)
448                   (assoc (substring pattern 0 (1- len)) cl))
449              (wl-complete-insert
450               start end
451               (substring pattern 0 (1- len))
452               cl))
453             ((null completion)
454              (message "Can't find completion for \"%s\"" pattern)
455              (ding))
456             ((not (string= pattern completion))
457              (delete-region start end)
458              (insert completion))
459             (t
460              (let ((list (sort (all-completions pattern cl) 'string<)))
461                (wl-complete-window-show list)))))))
462
463 (defvar wl-address-init-func 'wl-local-address-init)
464
465 (defun wl-address-init ()
466   "Call `wl-address-init-func'."
467   (funcall wl-address-init-func))
468
469 (defun wl-local-address-init ()
470   "Reload `wl-address-file'.
471 Refresh `wl-address-list', `wl-address-completion-list', and
472 `wl-address-petname-hash'."
473   (message "Updating addresses...")
474   (setq wl-address-list
475         (wl-address-make-address-list wl-address-file))
476   (setq wl-address-completion-list
477         (wl-address-make-completion-list wl-address-list))
478   (if (file-readable-p wl-alias-file)
479       (setq wl-address-completion-list
480             (append wl-address-completion-list
481                     (wl-address-make-alist-from-alias-file wl-alias-file))))
482   (setq wl-address-petname-hash (elmo-make-hash))
483   (let ((addresses wl-address-list))
484     (while addresses
485       (elmo-set-hash-val (downcase (car (car addresses)))
486                          (cadr (car addresses))
487                          wl-address-petname-hash)
488       (setq addresses (cdr addresses))))
489   (message "Updating addresses...done"))
490
491
492 (defun wl-address-expand-aliases (alist nest-count)
493   (when (< nest-count 5)
494     (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
495       (while (setq expn-str (cdr (nth n alist)))
496         (setq new-expn-str nil)
497         (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
498           (setq expn (elmo-match-string 1 expn-str))
499           (setq expn-str (wl-string-delete-match expn-str 0))
500           (if (string-match "^[ \t,]+" expn-str)
501               (setq expn-str (wl-string-delete-match expn-str 0)))
502           (if (string-match "[ \t,]+$" expn)
503               (setq expn (wl-string-delete-match expn 0)))
504           (setq new-expn (cdr (assoc expn alist)))
505           (if new-expn
506               (setq expanded t))
507           (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
508                                      (or new-expn expn))))
509         (when new-expn-str
510           (setcdr (nth n alist) new-expn-str))
511         (setq n (1+ n)))
512       (and expanded
513            (wl-address-expand-aliases alist (1+ nest-count))))))
514
515 (defun wl-address-make-alist-from-alias-file (file)
516   (elmo-set-work-buf
517     (let ((case-fold-search t)
518           alias expn alist)
519       (insert-file-contents file)
520       (while (re-search-forward ",$" nil t)
521         (end-of-line)
522         (forward-char 1)
523         (delete-backward-char 1))
524       (goto-char (point-min))
525       (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
526         (setq alias (wl-match-buffer 1)
527               expn (wl-match-buffer 2))
528         (setq alist (cons (cons alias expn) alist)))
529       (wl-address-expand-aliases alist 0)
530       (nreverse alist) ; return value
531       )))
532         
533 (defun wl-address-make-address-list (path)
534   (if (and path (file-readable-p path))
535       (elmo-set-work-buf
536         (let (ret
537               (coding-system-for-read wl-cs-autoconv))
538           (insert-file-contents path)
539           (goto-char (point-min))
540           (while (not (eobp))
541             (if (looking-at
542  "^\\([^#\n][^ \t\n]+\\)[ \t]+\"\\(.*\\)\"[ \t]+\"\\(.*\\)\"[ \t]*.*$")
543                 (setq ret
544                       (wl-append-element
545                        ret
546                        (list (wl-match-buffer 1)
547                              (wl-match-buffer 2)
548                              (wl-match-buffer 3)))))
549             (forward-line))
550           ret))))
551
552 (defun wl-address-get-petname-1 (string)
553   (let ((address (downcase (wl-address-header-extract-address string))))
554     (elmo-get-hash-val address wl-address-petname-hash)))
555
556 (defsubst wl-address-get-petname (string)
557   (or (wl-address-get-petname-1 string)
558       string))
559
560 (defsubst wl-address-user-mail-address-p (address)
561   "Judge whether ADDRESS is user's or not."
562   (member (downcase (wl-address-header-extract-address address))
563           (or (mapcar 'downcase wl-user-mail-address-list)
564               (list (downcase
565                      (wl-address-header-extract-address
566                       wl-from))))))
567
568 (defsubst wl-address-header-extract-address (str)
569   "Extracts a real e-mail address from STR and return it.
570 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
571   ->  \"m-sakura@ccs.mt.nec.co.jp\".
572 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
573   ->  \"m-sakura@ccs.mt.nec.co.jp\"."
574   (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
575          (wl-match-string 1 str))
576         ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
577          (wl-match-string 1 str))
578         (t str)))
579
580 (defsubst wl-address-header-extract-realname (str)
581   "Extracts a real name from STR and return it.
582 e.g. \"Mr. bar <hoge@foo.com>\"
583   ->  \"Mr. bar\"."
584   (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
585          (wl-match-string 1 str))
586         (t "")))
587
588 (defmacro wl-address-concat-token (string token)
589   (` (cond
590       ((eq 'quoted-string (car (, token)))
591        (concat (, string) "\"" (cdr (, token)) "\""))
592       ((eq 'comment (car (, token)))
593        (concat (, string) "(" (cdr (, token)) ")"))
594       (t
595        (concat (, string) (cdr (, token)))))))
596
597 (defun wl-address-string-without-group-list-contents (sequence)
598   "Return address string from lexical analyzed list SEQUENCE.
599 Group list contents is not included."
600   (let (address-string route-addr-end token seq group-end)
601   (while sequence
602     (setq token (car sequence))
603     (cond
604      ;;   group       =  phrase ":" [#mailbox] ";"
605      ((and (eq 'specials (car token))
606            (string= (cdr token) ":"))
607       (setq address-string (concat address-string (cdr token))) ; ':'
608       (setq seq (cdr sequence))
609       (setq token (car seq))
610       (setq group-end nil)
611       (while (not group-end)
612         (setq token (car seq))
613         (setq seq (cdr seq))
614         (setq group-end (and (eq 'specials (car token))
615                              (string= (cdr token) ";"))))
616       (setq address-string (concat address-string (cdr token))) ; ';'
617       (setq sequence seq))
618      ;;   route-addr  =  "<" [route] addr-spec ">"
619      ;;   route       =  1#("@" domain) ":"           ; path-relative
620      ((and (eq 'specials (car token))
621            (string= (cdr token) "<"))
622       (setq seq (std11-parse-route-addr sequence))
623       (setq route-addr-end (car (cdr seq)))
624       (while (not (eq (car sequence) route-addr-end))
625         (setq address-string (wl-address-concat-token address-string
626                                                       (car sequence)))
627         (setq sequence (cdr sequence))))
628      (t
629       (setq address-string (wl-address-concat-token address-string token))
630       (setq sequence (cdr sequence)))))
631   address-string))
632
633 (defun wl-address-petname-delete (the-email)
634   "Delete petname in `wl-address-file'."
635   (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
636           (output-coding-system
637            (mime-charset-to-coding-system wl-mime-charset)))
638     (set-buffer tmp-buf)
639     (message "Deleting Petname...")
640     (erase-buffer)
641     (insert-file-contents wl-address-file)
642     (delete-matching-lines (concat "^[ \t]*" the-email))
643     (write-region (point-min) (point-max)
644                   wl-address-file nil 'no-msg)
645     (message "Deleting Petname...done")
646     (kill-buffer tmp-buf)))
647
648
649 (defun wl-address-petname-add-or-change (the-email
650                                          default-petname
651                                          default-realname
652                                          &optional change-petname)
653   "Add petname to `wl-address-file', if not registerd.
654 If already registerd, change it."
655   (let (the-realname the-petname)
656
657     ;; setup output "petname"
658     ;; if null petname'd, let default-petname be the petname.
659     (setq the-petname
660           (read-from-minibuffer (format "Petname: ") default-petname))
661     (if (string= the-petname "")
662         (setq the-petname (or default-petname the-email)))
663
664     ;; setup output "realname"
665     (setq the-realname
666         (read-from-minibuffer (format "Real Name: ") default-realname))
667 ;;;     (if (string= the-realname "")
668 ;;;         (setq the-realname default-petname))
669
670     ;; writing to ~/.address
671     (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
672            (output-coding-system (mime-charset-to-coding-system wl-mime-charset)))
673       (set-buffer tmp-buf)
674       (message "Adding Petname...")
675       (erase-buffer)
676       (if (file-exists-p wl-address-file)
677           (insert-file-contents wl-address-file))
678       (if (not change-petname)
679           ;; if only add
680           (progn
681             (goto-char (point-max))
682             (if (and (> (buffer-size) 0)
683                      (not (eq (char-after (1- (point-max))) ?\n)))
684                 (insert "\n")))
685         ;; if change
686         (if (re-search-forward (concat "^[ \t]*" the-email) nil t)
687             (delete-region (save-excursion (beginning-of-line)
688                                            (point))
689                            (save-excursion (end-of-line)
690                                            (+ 1 (point))))))
691       (insert (format "%s\t\"%s\"\t\"%s\"\n"
692                       the-email the-petname the-realname))
693       (write-region (point-min) (point-max)
694                     wl-address-file nil 'no-msg)
695       (message "Adding Petname...done")
696       (kill-buffer tmp-buf))))
697
698 (require 'product)
699 (product-provide (provide 'wl-address) (require 'wl-version))
700
701 ;;; wl-address.el ends here
702