922975a456de670ea9d13b320bab00216bbcb1af
[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       (setq cl
304             (cons
305              (cons (nth 1 addr-tuple)
306                    (if (or (string= (nth 2 addr-tuple) "")
307                            (string-match ".*:.*;$" (nth 0 addr-tuple)))
308                        (nth 0 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          (completion-ignore-case t)
435          (cl completion-list))
436     (when use-ldap
437       (setq cl (wl-address-ldap-search pattern cl)))
438     (if (null cl)
439         nil
440       (setq completion (try-completion pattern cl))
441       (cond ((eq completion t)
442              (if use-ldap (setq wl-address-ldap-search-hash nil))
443              (wl-complete-insert start end pattern cl)
444              (wl-complete-window-delete)
445              (message "Sole completion"))
446             ((and epand-char
447                   (> len 0)
448                   (char-equal (aref pattern (1- len)) epand-char)
449                   (assoc (substring pattern 0 (1- len)) cl))
450              (wl-complete-insert
451               start end
452               (substring pattern 0 (1- len))
453               cl))
454             ((null completion)
455              (message "Can't find completion for \"%s\"" pattern)
456              (ding))
457             ((not (string= pattern completion))
458              (delete-region start end)
459              (insert completion))
460             (t
461              (let ((list (sort (all-completions pattern cl) 'string<)))
462                (wl-complete-window-show list)))))))
463
464 (defvar wl-address-init-function 'wl-local-address-init)
465
466 (defun wl-address-init ()
467   "Call `wl-address-init-function'."
468   (funcall wl-address-init-function))
469
470 (defun wl-local-address-init ()
471   "Reload `wl-address-file'.
472 Refresh `wl-address-list', `wl-address-completion-list', and
473 `wl-address-petname-hash'."
474   (message "Updating addresses...")
475   (setq wl-address-list
476         (wl-address-make-address-list wl-address-file))
477   (setq wl-address-completion-list
478         (wl-address-make-completion-list wl-address-list))
479   (if (file-readable-p wl-alias-file)
480       (setq wl-address-completion-list
481             (append wl-address-completion-list
482                     (wl-address-make-alist-from-alias-file wl-alias-file))))
483   (setq wl-address-petname-hash (elmo-make-hash))
484   (let ((addresses wl-address-list))
485     (while addresses
486       (elmo-set-hash-val (downcase (car (car addresses)))
487                          (cadr (car addresses))
488                          wl-address-petname-hash)
489       (setq addresses (cdr addresses))))
490   (message "Updating addresses...done"))
491
492
493 (defun wl-address-expand-aliases (alist nest-count)
494   (when (< nest-count 5)
495     (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
496       (while (setq expn-str (cdr (nth n alist)))
497         (setq new-expn-str nil)
498         (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
499           (setq expn (elmo-match-string 1 expn-str))
500           (setq expn-str (wl-string-delete-match expn-str 0))
501           (if (string-match "^[ \t,]+" expn-str)
502               (setq expn-str (wl-string-delete-match expn-str 0)))
503           (if (string-match "[ \t,]+$" expn)
504               (setq expn (wl-string-delete-match expn 0)))
505           (setq new-expn (cdr (assoc expn alist)))
506           (if new-expn
507               (setq expanded t))
508           (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
509                                      (or new-expn expn))))
510         (when new-expn-str
511           (setcdr (nth n alist) new-expn-str))
512         (setq n (1+ n)))
513       (and expanded
514            (wl-address-expand-aliases alist (1+ nest-count))))))
515
516 (defun wl-address-make-alist-from-alias-file (file)
517   (elmo-set-work-buf
518     (let ((case-fold-search t)
519           alias expn alist)
520       (insert-file-contents file)
521       (while (re-search-forward ",$" nil t)
522         (end-of-line)
523         (forward-char 1)
524         (delete-backward-char 1))
525       (goto-char (point-min))
526       (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
527         (setq alias (wl-match-buffer 1)
528               expn (wl-match-buffer 2))
529         (setq alist (cons (cons alias expn) alist)))
530       (wl-address-expand-aliases alist 0)
531       (nreverse alist) ; return value
532       )))
533
534 (defun wl-address-make-address-list (path)
535   (if (and path (file-readable-p path))
536       (elmo-set-work-buf
537        (let (ret
538              (coding-system-for-read wl-cs-autoconv))
539          (insert-file-contents path)
540          (goto-char (point-min))
541          (while (not (eobp))
542            (if (looking-at
543                 "^\\([^#\n][^ \t\n]+\\)[ \t]+\\(\".*\"\\)[ \t]+\\(\".*\"\\)[ \t]*.*$")
544                (setq ret
545                      (cons
546                       (list (wl-match-buffer 1)
547                             (read (wl-match-buffer 2))
548                             (read (wl-match-buffer 3)))
549                       ret)))
550            (forward-line))
551          (nreverse ret)))))
552
553 (defun wl-address-get-petname-1 (string)
554   (let ((address (downcase (wl-address-header-extract-address string))))
555     (elmo-get-hash-val address wl-address-petname-hash)))
556
557 (defsubst wl-address-get-petname (string)
558   (or (wl-address-get-petname-1 string)
559       string))
560
561 (defsubst wl-address-user-mail-address-p (address)
562   "Judge whether ADDRESS is user's or not."
563   (member (downcase (wl-address-header-extract-address address))
564           (or (mapcar 'downcase wl-user-mail-address-list)
565               (list (downcase
566                      (wl-address-header-extract-address
567                       wl-from))))))
568
569 (defsubst wl-address-header-extract-address (str)
570   "Extracts a real e-mail address from STR and return it.
571 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
572   ->  \"m-sakura@ccs.mt.nec.co.jp\".
573 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
574   ->  \"m-sakura@ccs.mt.nec.co.jp\"."
575   (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
576          (wl-match-string 1 str))
577         ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
578          (wl-match-string 1 str))
579         (t str)))
580
581 (defsubst wl-address-header-extract-realname (str)
582   "Extracts a real name from STR and return it.
583 e.g. \"Mr. bar <hoge@foo.com>\"
584   ->  \"Mr. bar\"."
585   (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
586          (wl-match-string 1 str))
587         (t "")))
588
589 (defmacro wl-address-concat-token (string token)
590   (` (cond
591       ((eq 'quoted-string (car (, token)))
592        (concat (, string) "\"" (cdr (, token)) "\""))
593       ((eq 'comment (car (, token)))
594        (concat (, string) "(" (cdr (, token)) ")"))
595       (t
596        (concat (, string) (cdr (, token)))))))
597
598 (defun wl-address-string-without-group-list-contents (sequence)
599   "Return address string from lexical analyzed list SEQUENCE.
600 Group list contents is not included."
601   (let (address-string route-addr-end token seq group-end)
602   (while sequence
603     (setq token (car sequence))
604     (cond
605      ;;   group       =  phrase ":" [#mailbox] ";"
606      ((and (eq 'specials (car token))
607            (string= (cdr token) ":"))
608       (setq address-string (concat address-string (cdr token))) ; ':'
609       (setq seq (cdr sequence))
610       (setq token (car seq))
611       (setq group-end nil)
612       (while (not group-end)
613         (setq token (car seq))
614         (setq seq (cdr seq))
615         (setq group-end (and (eq 'specials (car token))
616                              (string= (cdr token) ";"))))
617       (setq address-string (concat address-string (cdr token))) ; ';'
618       (setq sequence seq))
619      ;;   route-addr  =  "<" [route] addr-spec ">"
620      ;;   route       =  1#("@" domain) ":"           ; path-relative
621      ((and (eq 'specials (car token))
622            (string= (cdr token) "<"))
623       (setq seq (std11-parse-route-addr sequence))
624       (setq route-addr-end (car (cdr seq)))
625       (while (not (eq (car sequence) route-addr-end))
626         (setq address-string (wl-address-concat-token address-string
627                                                       (car sequence)))
628         (setq sequence (cdr sequence))))
629      (t
630       (setq address-string (wl-address-concat-token address-string token))
631       (setq sequence (cdr sequence)))))
632   address-string))
633
634 (defun wl-address-delete (the-email)
635   "Delete address entry in the `wl-address-file'."
636   (let ((output-coding-system
637          (mime-charset-to-coding-system wl-mime-charset)))
638     (with-temp-buffer
639       (message "Deleting Address...")
640       (insert-file-contents wl-address-file)
641       (delete-matching-lines (concat "^[ \t]*" the-email))
642       (write-region (point-min) (point-max)
643                     wl-address-file nil 'no-msg)
644       ;; Delete entries.
645       (dolist (entry (elmo-string-assoc-all the-email wl-address-list))
646         (setq wl-address-list (delete entry wl-address-list)))
647       (elmo-set-hash-val the-email nil wl-address-petname-hash)
648       (message "Deleting Address...done"))))
649
650 (defun wl-address-add-or-change (address
651                                  &optional default-realname
652                                  change-address)
653   "Add address entry to `wl-address-file', if not registerd.
654 If already registerd, change it."
655   (let ((entry (assoc address wl-address-list))
656         the-realname the-petname new-addr addr-changed)
657     (setq the-realname
658           (read-from-minibuffer "Real Name: " (or default-realname
659                                                   (nth 2 entry))))
660     (setq the-petname (read-from-minibuffer "Petname: "
661                                             (or (nth 1 entry)
662                                                 the-realname)))
663     (when change-address
664       (setq new-addr (read-from-minibuffer "E-Mail: " address))
665       (if (and (not (string= address new-addr))
666                (assoc new-addr wl-address-list))
667           (error "'%s' already exists" new-addr)))
668     ;; writing to ~/.address
669     (let ((output-coding-system
670            (mime-charset-to-coding-system wl-mime-charset)))
671       (with-temp-buffer
672         (if (file-exists-p wl-address-file)
673             (insert-file-contents wl-address-file))
674         (if (null entry)
675             ;; add
676             (progn
677               (goto-char (point-max))
678               (if (and (> (buffer-size) 0)
679                        (not (eq (char-after (1- (point-max))) ?\n)))
680                   (insert "\n")))
681           ;; override
682           (while (re-search-forward (concat "^[ \t]*" address) nil t)
683             (delete-region (save-excursion (beginning-of-line)
684                                            (point))
685                            (save-excursion (end-of-line)
686                                            (+ 1 (point))))))
687         (insert (format "%s\t%s\t%s\n"
688                         (or new-addr address)
689                         (prin1-to-string the-petname)
690                         (prin1-to-string the-realname)))
691         (write-region (point-min) (point-max)
692                       wl-address-file nil 'no-msg)
693         (wl-address-init)
694         (list (or new-addr address) the-petname the-realname)))))
695
696 (require 'product)
697 (product-provide (provide 'wl-address) (require 'wl-version))
698
699 ;;; wl-address.el ends here
700