b78439dff3122b745c97c738a633cf3269a5ed44
[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
50 (defvar wl-address-ldap-search-hash nil)
51
52 (eval-when-compile (require 'pldap))
53
54 (defvar wl-ldap-alias-dn-level nil
55 "Level of dn data to make alias postfix.
56 Valid value is nit, t, 1 or larget integer.
57
58 If this value nil, minimum alias postfix is made depends on uniqness
59 with other candidates.  In this implementation, it's same to 1.  If t,
60 always append all dn data.  If number, always append spcified level of
61 data but maybe appended more uniqness.  If invalid value, treat as
62 nil.
63
64 For example, following dn data is exsist, alias of each level is shown
65 bellow.
66
67 Match: Goto
68 dn: CN=Shun-ichi GOTO,OU=Mew,OU=Emacs,OU=Lisper,O=Programmers Inc.
69   nil => Goto/Shun-ichi_GOTO
70     1 => Goto/Shun-ichi_GOTO
71     2 => Goto/Shun-ichi_GOTO/Mew
72     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
73     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
74     5 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
75     6 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
76     t => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
77
78 If level 3 is required for uniqness with other candidates,
79   nil => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
80     1 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
81     2 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
82     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
83     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
84     (so on...)")
85
86 (defconst wl-ldap-alias-sep "/")
87
88 (defconst wl-ldap-search-attribute-type-list
89   '("sn" "cn" "mail"))
90
91 (defun wl-ldap-get-value (type entry)
92   ""
93   (let* ((values (cdr (assoc type entry)))
94          (ret (car values)))
95     (if (and ret (not ldap-ignore-attribute-codings))
96         (while values
97           (if (not (string-match "^[\000-\177]*$" (car values)))
98               (setq ret (car values)
99                     values nil)
100             (setq values (cdr values)))))
101     ret))
102
103 (defun wl-ldap-get-value-list (type entry)
104   ""
105   (cdr (assoc type entry)))
106
107 (defun wl-ldap-make-filter (pat type-list)
108   "Make RFC1558 quiery filter for PAT from ATTR-LIST.
109 Each are \"OR\" combination, and PAT is beginning-match."
110   (concat "(&(objectclass=" wl-ldap-objectclass ")(|"
111           (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
112                      type-list
113                      "")
114           "))"))
115
116 (defun wl-ldap-make-matched-value-list (regexp type-list entry)
117   "Correct matching WORD with value of TYPE-LIST in ENTRY.
118 Returns matched uniq string list."
119   (let (type val values result)
120     ;; collect matching value
121     (while entry
122       (setq type (car (car entry))
123             values (mapcar (function wl-ldap-alias-safe-string)
124                            (cdr (car entry)))
125             values (elmo-flatten values)
126             entry (cdr entry))
127       (if (string-match "::?$" type)
128           (setq type (substring type 0 (match-beginning 0))))
129       (if (member type type-list)
130           (while values
131             (setq val (car values)
132                   values (cdr values))
133             (if (and (string-match regexp val)
134                      (not (member val result)))
135                 (setq result (cons val result))))))
136     result))
137
138 (defun wl-ldap-alias-safe-string (str)
139   "Modify STR for alias.
140 Replace space/tab in STR into '_' char.
141 Replace '@' in STR into list of mailbox and sub-domains."
142   (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
143     (setq str (concat (substring str 0 (match-beginning 0))
144                       "_"
145                       (substring str (match-end 0)))))
146   (if (string-match "\\(@\\)[^/@]+" str)
147       (setq str (split-string str  "[@\\.]")))
148   str)
149
150 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
151   ""
152   (let (sym dnsym value level)
153     (setq dnsym (intern (upcase dn) hash))
154     (if (and (null str) (boundp dnsym))
155         ()                                      ; already processed
156       ;; make dn-list in fisrt time
157       (if (null dn-list)
158           (let ((case-fold-search t))
159             (setq dn-list (mapcar (lambda (str)
160                                     (if (string-match "[a-z]+=\\(.*\\)" str)
161                                         (wl-ldap-alias-safe-string
162                                          (wl-match-string 1 str))))
163                                   (split-string dn "[ \t]*,[ \t]*")))))
164       (setq dn-list (elmo-flatten dn-list))
165       ;; prepare candidate for uniq str
166       (if str
167           (setq str (concat str wl-ldap-alias-sep (car dn-list))
168                 dn-list (cdr dn-list))
169         ;; first entry, pre-build with given level
170         (cond
171          ((null wl-ldap-alias-dn-level) (setq level 1))
172          ((eq t wl-ldap-alias-dn-level) (setq level 1000)) ; xxx, big enough
173          ((numberp wl-ldap-alias-dn-level)
174           (if (< 0 wl-ldap-alias-dn-level)
175               (setq level  wl-ldap-alias-dn-level)
176             (setq level 1)))
177          (t
178           (setq level 1)))
179         (while (and (< 0 level) dn-list)
180           (if (null str)
181               (setq str (car dn-list))
182             (setq str (concat str wl-ldap-alias-sep (car dn-list))))
183           (setq level (1- level)
184                 dn-list (cdr dn-list))))
185       (setq sym (intern (upcase str) hash))
186       (if (not (boundp sym))
187           ;; good
188           (progn (set sym (list dn str dn-list))
189                  (set dnsym str))
190         ;; conflict
191         (if (not (eq (setq value (symbol-value sym)) t))
192             ;; move away deeper
193             (progn (set sym t)
194                    (apply (function wl-ldap-register-dn-string) hash value)))
195         (wl-ldap-register-dn-string hash dn str dn-list)))))
196
197 (defun wl-address-ldap-search (pattern cl)
198   "Make address completion-list matched for PATTERN by LDAP search.
199 Matched address lists are append to CL."
200   (require 'pldap)
201   (unless wl-address-ldap-search-hash
202     (setq wl-address-ldap-search-hash (elmo-make-hash 7)))
203   (let ((pat (if (string-match wl-ldap-alias-sep pattern)
204                  (substring pattern 0 (match-beginning 0))
205                pattern))
206         (ldap-default-host wl-ldap-server)
207         (ldap-default-port (or wl-ldap-port 389))
208         (ldap-default-base wl-ldap-base)
209         (dnhash (elmo-make-hash))
210         cache len sym tmpl regexp entries ent values dn dnstr alias
211         result cn mails)
212     ;; check cache
213     (mapatoms (lambda (atom)
214                 (if (and (string-match
215                           (concat "^" (symbol-name atom) ".*") pat)
216                          (or (null cache)
217                              (< (car cache)
218                                 (setq len (length (symbol-name atom))))))
219                     (setq cache (cons
220                                  (or len (length (symbol-name atom)))
221                                  (symbol-value atom)))))
222               wl-address-ldap-search-hash)
223     ;; get matched entries
224     (if cache
225         (setq entries (cdr cache))
226       (condition-case nil
227           (progn
228             (message "Searching in LDAP...")
229             (setq entries (ldap-search-entries
230                            (wl-ldap-make-filter
231                             (concat pat "*")
232                             wl-ldap-search-attribute-type-list)
233                            nil wl-ldap-search-attribute-type-list nil t))
234             (message "Searching in LDAP...done")
235             (elmo-set-hash-val pattern entries wl-address-ldap-search-hash))
236         (error (message ""))))                  ; ignore error: No such object
237     ;;
238     (setq tmpl entries)
239     (while tmpl
240       (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
241       (setq tmpl (cdr tmpl)))
242     ;;
243     (setq regexp (concat "^" pat))
244     (while entries
245       (setq ent (cdar entries)
246             values (wl-ldap-make-matched-value-list
247                     regexp '("mail" "sn" "cn") ent)
248             mails (wl-ldap-get-value-list "mail" ent)
249             cn (wl-ldap-get-value "cn" ent)
250             dn (car (car entries))
251             dnstr (elmo-get-hash-val (upcase dn) dnhash))
252       ;; make alias list generated from LDAP data.
253       (while (and mails values)
254         ;; make alias like MATCHED/DN-STRING
255         (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
256             (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
257           ;; use DN-STRING if DN-STRING begin with MATCHED
258           (setq alias dnstr))
259         ;; check uniqness then add to list
260         (setq sym (intern (downcase alias) dnhash))
261         (when (not (boundp sym))
262           (set sym alias)
263           (setq result (cons (cons alias
264                                    (concat cn " <" (car mails) ">"))
265                              result)))
266         (setq values (cdr values)))
267       ;; make mail addrses list
268       (while mails
269         (if (null (assoc (car mails) cl)); Not already in cl.
270             ;; (string-match regexp (car mails))
271             ;; add mail address itself to completion list
272             (setq result (cons (cons (car mails)
273                                      (concat cn " <" (car mails) ">"))
274                                result)))
275         (setq mails (cdr mails)))
276       (setq entries (cdr entries)))
277     (append result cl)))
278
279 (defun wl-complete-field-to ()
280   (interactive)
281   (let ((cl wl-address-completion-list))
282     (if cl
283         (completing-read "To: " cl)
284       (read-string "To: "))))
285
286 (defalias 'wl-address-quote-specials 'elmo-address-quote-specials)
287
288 (defun wl-address-make-completion-list (address-list)
289   (let (addr-tuple cl)
290     (while address-list
291       (setq addr-tuple (car address-list))
292       (setq cl
293             (cons
294              (cons (nth 0 addr-tuple)
295                    (if (or (string= (nth 2 addr-tuple) "")
296                            (string-match ".*:.*;$" (nth 0 addr-tuple)))
297                        (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                      (if (or (string= (nth 2 addr-tuple) "")
310                              (string-match ".*:.*;$" (nth 0 addr-tuple)))
311                          (nth 0 addr-tuple)
312                        (concat
313                         (wl-address-quote-specials
314                          (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">")))
315                cl)))
316       (setq address-list (cdr address-list)))
317     cl))
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                      (wl-append-element
549                       ret
550                       (list (wl-match-buffer 1)
551                             (read (wl-match-buffer 2))
552                             (read (wl-match-buffer 3))))))
553            (forward-line))
554          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