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