ptexinfmt.el; Fix last change
[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 (eval-when-compile (require 'cl))
40
41 (defvar wl-address-complete-header-list
42   '("To:" "From:" "Cc:" "Bcc:" "Mail-Followup-To:" "Reply-To:"
43     "Return-Receipt-To:"))
44 (defvar wl-address-complete-header-regexp nil) ; auto-generated.
45 (defvar wl-newsgroups-complete-header-regexp "^\\(Newsgroups\\|Followup-To\\):")
46 (defvar wl-folder-complete-header-regexp "^\\(Fcc\\):")
47 (defvar wl-address-list nil)
48 (defvar wl-address-completion-list nil)
49 (defvar wl-address-petname-hash nil)
50 (defvar wl-address-enable-strict-loading t)
51
52 (defvar wl-address-ldap-search-hash nil)
53
54 (eval-when-compile (require 'pldap))
55
56 (defvar wl-ldap-alias-dn-level nil
57 "Level of dn data to make alias postfix.
58 Valid value is nit, t, 1 or larget integer.
59
60 If this value nil, minimum alias postfix is made depends on uniqness
61 with other candidates.  In this implementation, it's same to 1.  If t,
62 always append all dn data.  If number, always append spcified level of
63 data but maybe appended more uniqness.  If invalid value, treat as
64 nil.
65
66 For example, following dn data is exsist, alias of each level is shown
67 bellow.
68
69 Match: Goto
70 dn: CN=Shun-ichi GOTO,OU=Mew,OU=Emacs,OU=Lisper,O=Programmers Inc.
71   nil => Goto/Shun-ichi_GOTO
72     1 => Goto/Shun-ichi_GOTO
73     2 => Goto/Shun-ichi_GOTO/Mew
74     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
75     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
76     5 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
77     6 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
78     t => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
79
80 If level 3 is required for uniqness with other candidates,
81   nil => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
82     1 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
83     2 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
84     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
85     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
86     (so on...)")
87
88 (defconst wl-ldap-alias-sep "/")
89
90 (defconst wl-ldap-search-attribute-type-list
91   '("sn" "cn" "mail" "email"))
92
93 (defun wl-ldap-get-value (type entry)
94   ""
95   (let* ((values (cdr (assoc type entry)))
96          (ret (car values)))
97     (if (and ret (not ldap-ignore-attribute-codings))
98         (while values
99           (if (not (string-match "^[\000-\177]*$" (car values)))
100               (setq ret (car values)
101                     values nil)
102             (setq values (cdr values)))))
103     ret))
104
105 (defun wl-ldap-get-value-list (type entry)
106   ""
107   (cdr (assoc type entry)))
108
109 (defun wl-ldap-make-filter (pat type-list)
110   "Make RFC1558 quiery filter for PAT from ATTR-LIST.
111 Each are \"OR\" combination, and PAT is beginning-match."
112   (concat "(|"
113           (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
114                      type-list
115                      "")
116           ")"))
117
118 (defun wl-ldap-make-matched-value-list (regexp type-list entry)
119   "Correct matching WORD with value of TYPE-LIST in ENTRY.
120 Returns matched uniq string list."
121   (let (type val values result)
122     ;; collect matching value
123     (while entry
124       (setq type (car (car entry))
125             values (mapcar (function wl-ldap-alias-safe-string)
126                            (cdr (car entry)))
127             values (elmo-flatten values)
128             entry (cdr entry))
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 "[ \t]+" 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 (or wl-ldap-server ldap-default-host "localhost"))
207         (ldap-default-port (or wl-ldap-port ldap-default-port 389))
208         (ldap-default-base (or wl-ldap-base ldap-default-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       (ignore-errors
227         (message "Searching in LDAP...")
228         (setq entries (ldap-search-entries
229                        (wl-ldap-make-filter
230                         pat wl-ldap-search-attribute-type-list)
231                        nil wl-ldap-search-attribute-type-list nil t))
232         (message "Searching in LDAP...done")
233         (elmo-set-hash-val pattern entries wl-address-ldap-search-hash)))
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 wl-ldap-search-attribute-type-list
245                     ent)
246             mails (or (wl-ldap-get-value-list "mail" ent)
247                       (wl-ldap-get-value-list "email" ent))
248             cn (wl-ldap-get-value "cn" ent)
249             dn (car (car entries))
250             dnstr (elmo-get-hash-val (upcase dn) dnhash))
251       ;; make alias list generated from LDAP data.
252       (while (and mails values)
253         ;; make alias like MATCHED/DN-STRING
254         (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
255             (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
256           ;; use DN-STRING if DN-STRING begin with MATCHED
257           (setq alias dnstr))
258         ;; check uniqness then add to list
259         (setq sym (intern (downcase alias) dnhash))
260         (when (not (boundp sym))
261           (set sym alias)
262           (setq result (cons (cons alias
263                                    (concat cn " <" (car mails) ">"))
264                              result)))
265         (setq values (cdr values)))
266       ;; make mail addrses list
267       (while mails
268         (if (null (assoc (car mails) cl)); Not already in cl.
269 ;;;         (string-match regexp (car mails))
270             ;; add mail address itself to completion list
271             (setq result (cons (cons (car mails)
272                                      (concat cn " <" (car mails) ">"))
273                                result)))
274         (setq mails (cdr mails)))
275       (setq entries (cdr entries)))
276     (append result cl)))
277
278 (defun wl-complete-address (string predicate flag)
279   "Completion function for completing-read (comma separated addresses)."
280   (if (string-match "^\\(.*,\\)\\(.*\\)$" string)
281       (let* ((str1 (match-string 1 string))
282              (str2 (match-string 2 string))
283              (str2-comp (wl-complete-address str2 predicate flag)))
284         (if (and (not flag) (stringp str2-comp))
285             (concat str1 str2-comp)
286           str2-comp))
287     (if (not flag)
288         (try-completion string wl-address-list)
289       (all-completions string wl-address-list))))
290
291 (defalias 'wl-address-quote-specials 'elmo-address-quote-specials)
292
293 (defun wl-address-make-completion-list (address-list)
294   (let (addr-tuple cl)
295     (while address-list
296       (setq addr-tuple (car address-list))
297       (setq cl
298             (cons
299              (wl-address-make-completion-entry 0 addr-tuple)
300              cl))
301       ;; nickname completion.
302       (if wl-address-enable-strict-loading
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                    (wl-address-make-completion-entry 1 addr-tuple)
309                    cl)))
310         (setq cl
311               (cons
312                (wl-address-make-completion-entry 1 addr-tuple)
313                cl)))
314       (setq address-list (cdr address-list)))
315     cl))
316
317 (defun wl-address-make-completion-entry (index addr-tuple)
318   (cons (nth index addr-tuple)
319         (if (or (string= (nth 2 addr-tuple) "")
320                 (string-match ".*:.*;$" (nth 0 addr-tuple)))
321             (nth 0 addr-tuple)
322           (concat
323            (wl-address-quote-specials
324             (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))))
325
326 (defun wl-complete-field-body-or-tab ()
327   (interactive)
328   (let ((case-fold-search t)
329         epand-char skip-chars
330         (use-ldap nil)
331         completion-list)
332     (if (wl-draft-on-field-p)
333         (wl-complete-field)
334       (if (and
335            (< (point)
336               (save-excursion
337                 (goto-char (point-min))
338                 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
339                 (point)))
340            (save-excursion
341              (beginning-of-line)
342              (setq use-ldap nil)
343              (while (and (looking-at "^[ \t]")
344                          (not (= (point) (point-min))))
345                (forward-line -1))
346              (cond ((looking-at wl-address-complete-header-regexp)
347                     (setq completion-list wl-address-completion-list)
348                     (if wl-use-ldap
349                         (setq use-ldap t))
350                     (setq epand-char ?@))
351                    ((looking-at wl-folder-complete-header-regexp)
352                     (setq completion-list wl-folder-entity-hashtb)
353                     (setq skip-chars "^, "))
354                    ((looking-at wl-newsgroups-complete-header-regexp)
355                     (setq completion-list wl-folder-newsgroups-hashtb)))))
356           (wl-complete-field-body completion-list
357                                   epand-char skip-chars use-ldap)
358         (indent-for-tab-command)))))
359
360 (defvar wl-completion-buf-name "*Completions*")
361
362 (defvar wl-complete-candidates nil)
363
364 (defun wl-complete-window-show (all)
365   (if (and (get-buffer-window wl-completion-buf-name)
366            (equal wl-complete-candidates all))
367       (let ((win (get-buffer-window wl-completion-buf-name)))
368         (with-current-buffer wl-completion-buf-name
369           (if (pos-visible-in-window-p (point-max) win)
370               (set-window-start win 1)
371             (scroll-other-window))))
372     (message "Making completion list...")
373     (setq wl-complete-candidates all)
374     (with-output-to-temp-buffer
375         wl-completion-buf-name
376       (display-completion-list all))
377     (message "Making completion list...done")))
378
379 (defun wl-complete-window-delete ()
380   (let (comp-buf comp-win)
381     (if (setq comp-buf (get-buffer wl-completion-buf-name))
382         (if (setq comp-win (get-buffer-window comp-buf))
383             (delete-window comp-win)))))
384
385 (defun wl-complete-field ()
386   (interactive)
387   (let* ((end (point))
388          (start (save-excursion
389                   (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
390                   (point)))
391          (completion)
392          (pattern (buffer-substring start end))
393          (cl wl-draft-field-completion-list))
394     (if (null cl)
395         nil
396       (setq completion
397             (let ((completion-ignore-case t))
398               (try-completion pattern cl)))
399       (cond ((eq completion t)
400              (let ((alias (assoc pattern cl)))
401                (if alias
402                    (progn
403                      (delete-region start end)
404                      (insert (cdr alias))
405 ;;;                  (wl-highlight-message (point-min)(point-max) t)
406                      )))
407              (wl-complete-window-delete))
408             ((null completion)
409              (message "Can't find completion for \"%s\"" pattern)
410              (ding))
411             ((not (string= pattern completion))
412              (delete-region start end)
413              (insert completion)
414              (wl-complete-window-delete)
415              (wl-highlight-message (point-min)(point-max) t))
416             (t
417              (let ((list (all-completions pattern cl)))
418                (wl-complete-window-show list)))))))
419
420 (defun wl-complete-insert (start end pattern completion-list)
421   (let ((alias (and (consp completion-list)
422                     (assoc pattern completion-list)))
423         comp-buf comp-win)
424     (if alias
425         (progn
426           (delete-region start end)
427           (insert (cdr alias))
428           (if (setq comp-buf (get-buffer wl-completion-buf-name))
429               (if (setq comp-win (get-buffer-window comp-buf))
430                   (delete-window comp-win)))))))
431
432 (defun wl-complete-field-body (completion-list
433                                &optional epand-char skip-chars use-ldap)
434   (interactive)
435   (let* ((end (point))
436          (start (save-excursion
437                   (skip-chars-backward (or skip-chars "^:,>\n"))
438                   (skip-chars-forward " \t")
439                   (point)))
440          (completion)
441          (pattern (buffer-substring start end))
442          (len (length pattern))
443          (completion-ignore-case t)
444          (cl completion-list))
445     (when use-ldap
446       (setq cl (wl-address-ldap-search pattern cl)))
447     (if (null cl)
448         nil
449       (setq completion (try-completion pattern cl))
450       (cond ((eq completion t)
451              (if use-ldap (setq wl-address-ldap-search-hash nil))
452              (wl-complete-insert start end pattern cl)
453              (wl-complete-window-delete)
454              (message "Sole completion"))
455             ((and epand-char
456                   (> len 0)
457                   (or (char-equal (aref pattern (1- len)) epand-char)
458                       (char-equal (aref pattern (1- len)) (string-to-char " ")))
459                   (assoc (substring pattern 0 (1- len)) cl))
460              (wl-complete-insert
461               start end
462               (substring pattern 0 (1- len))
463               cl))
464             ((null completion)
465              (message "Can't find completion for \"%s\"" pattern)
466              (ding))
467             ((not (string= pattern completion))
468              (delete-region start end)
469              (insert completion))
470             (t
471              (let ((list (sort (all-completions pattern cl) 'string<)))
472                (wl-complete-window-show list)))))))
473
474 (defvar wl-address-init-function 'wl-local-address-init)
475
476 (defun wl-address-init ()
477   "Call `wl-address-init-function'."
478   (funcall wl-address-init-function))
479
480 (defun wl-local-address-init ()
481   "Reload `wl-address-file'.
482 Refresh `wl-address-list', `wl-address-completion-list', and
483 `wl-address-petname-hash'."
484   (message "Updating addresses...")
485   (setq wl-address-list
486         (wl-address-make-address-list wl-address-file))
487   (setq wl-address-completion-list
488         (wl-address-make-completion-list wl-address-list))
489   (if (file-readable-p wl-alias-file)
490       (setq wl-address-completion-list
491             (append wl-address-completion-list
492                     (wl-address-make-alist-from-alias-file wl-alias-file))))
493   (setq wl-address-petname-hash (elmo-make-hash))
494   (let ((addresses wl-address-list))
495     (while addresses
496       (elmo-set-hash-val (downcase (car (car addresses)))
497                          (cadr (car addresses))
498                          wl-address-petname-hash)
499       (setq addresses (cdr addresses))))
500   (message "Updating addresses...done"))
501
502
503 (defun wl-address-expand-aliases (alist nest-count)
504   (when (< nest-count 5)
505     (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
506       (while (setq expn-str (cdr (nth n alist)))
507         (setq new-expn-str nil)
508         (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
509           (setq expn (elmo-match-string 1 expn-str))
510           (setq expn-str (wl-string-delete-match expn-str 0))
511           (if (string-match "^[ \t,]+" expn-str)
512               (setq expn-str (wl-string-delete-match expn-str 0)))
513           (if (string-match "[ \t,]+$" expn)
514               (setq expn (wl-string-delete-match expn 0)))
515           (setq new-expn (cdr (assoc expn alist)))
516           (if new-expn
517               (setq expanded t))
518           (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
519                                      (or new-expn expn))))
520         (when new-expn-str
521           (setcdr (nth n alist) new-expn-str))
522         (setq n (1+ n)))
523       (and expanded
524            (wl-address-expand-aliases alist (1+ nest-count))))))
525
526 (defun wl-address-make-alist-from-alias-file (file)
527   (with-temp-buffer
528     (let ((case-fold-search t)
529           alias expn alist)
530       (insert-file-contents file)
531       (while (re-search-forward ",$" nil t)
532         (end-of-line)
533         (forward-char 1)
534         (delete-char -1))
535       (goto-char (point-min))
536       (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
537         (setq alias (wl-match-buffer 1)
538               expn (wl-match-buffer 2))
539         (setq alist (cons (cons alias expn) alist)))
540       (wl-address-expand-aliases alist 0)
541       (nreverse alist) ; return value
542       )))
543
544 (defun wl-address-make-address-list (path)
545   (when (and path (file-readable-p path))
546     (with-temp-buffer
547       (let (ret
548             (coding-system-for-read wl-cs-autoconv))
549         (insert-file-contents path)
550         (goto-char (point-min))
551         (while (not (eobp))
552           (if (looking-at "\
553 ^\\([^#\n][^ \t\n]+\\)[ \t]+\\(\".*\"\\)[ \t]+\\(\".*\"\\)[ \t]*.*$")
554               (setq ret
555                     (cons
556                      (list (wl-match-buffer 1)
557                            (read (wl-match-buffer 2))
558                            (read (wl-match-buffer 3)))
559                      ret)))
560           (forward-line))
561         (nreverse ret)))))
562
563
564 (defsubst wl-address-header-extract-address (str)
565   "Extracts a real e-mail address from STR and return it.
566 e.g. \"Mine Sakurai <m-sakura@example.org>\"
567   ->  \"m-sakura@example.org\".
568 e.g. \"m-sakura@example.org (Mine Sakurai)\"
569   ->  \"m-sakura@example.org\"."
570   (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
571          (wl-match-string 1 str))
572         ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
573          (wl-match-string 1 str))
574         (t str)))
575
576 (defsubst wl-address-header-extract-realname (str)
577   "Extracts a real name from STR and return it.
578 e.g. \"Mr. bar <hoge@example.com>\"
579   ->  \"Mr. bar\"."
580   (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
581          (wl-match-string 1 str))
582         (t "")))
583
584
585 (defun wl-address-get-petname-1 (string)
586   (let ((address (downcase (wl-address-header-extract-address string))))
587     (elmo-get-hash-val address wl-address-petname-hash)))
588
589 (defsubst wl-address-get-petname (string)
590   (or (wl-address-get-petname-1 string)
591       string))
592
593 (defun wl-address-user-mail-address-p (address)
594   "Judge whether ADDRESS is user's or not."
595   (if wl-user-mail-address-regexp
596       (string-match wl-user-mail-address-regexp
597                     (wl-address-header-extract-address address))
598     (member (downcase (wl-address-header-extract-address address))
599             (or (mapcar 'downcase wl-user-mail-address-list)
600                 (list (downcase
601                        (wl-address-header-extract-address
602                         wl-from)))))))
603
604 (defun wl-address-delete-user-mail-addresses (address-list)
605   "Delete user mail addresses from list by side effect.
606 Deletion is done by using `elmo-list-delete'."
607   (if wl-user-mail-address-regexp
608       (elmo-list-delete (list wl-user-mail-address-regexp) address-list
609                         (lambda (elem list)
610                           (elmo-delete-if
611                            (lambda (item) (string-match elem item))
612                            list)))
613     (let ((myself (or wl-user-mail-address-list
614                       (list (wl-address-header-extract-address wl-from)))))
615       (elmo-list-delete myself address-list
616                         (lambda (elem list)
617                           (elmo-delete-if
618                            (lambda (item) (string= (downcase elem)
619                                                    (downcase item)))
620                            list))))))
621
622 (defmacro wl-address-concat-token (string token)
623   `(cond
624     ((eq 'quoted-string (car ,token))
625      (concat ,string "\"" (cdr ,token) "\""))
626     ((eq 'comment (car ,token))
627      (concat ,string "(" (cdr ,token) ")"))
628     (t
629      (concat ,string (cdr ,token)))))
630
631 (defun wl-address-string-without-group-list-contents (sequence)
632   "Return address string from lexical analyzed list SEQUENCE.
633 Group list contents is not included."
634   (let (address-string route-addr-end token seq group-end)
635   (while sequence
636     (setq token (car sequence))
637     (cond
638      ;;   group       =  phrase ":" [#mailbox] ";"
639      ((and (eq 'specials (car token))
640            (string= (cdr token) ":"))
641       (setq address-string (concat address-string (cdr token))) ; ':'
642       (setq seq (cdr sequence))
643       (setq token (car seq))
644       (setq group-end nil)
645       (while (not group-end)
646         (setq token (car seq))
647         (setq seq (cdr seq))
648         (setq group-end (and (eq 'specials (car token))
649                              (string= (cdr token) ";"))))
650       (setq address-string (concat address-string (cdr token))) ; ';'
651       (setq sequence seq))
652      ;;   route-addr  =  "<" [route] addr-spec ">"
653      ;;   route       =  1#("@" domain) ":"           ; path-relative
654      ((and (eq 'specials (car token))
655            (string= (cdr token) "<"))
656       (setq seq (std11-parse-route-addr sequence))
657       (setq route-addr-end (car (cdr seq)))
658       (while (not (eq (car sequence) route-addr-end))
659         (setq address-string (wl-address-concat-token address-string
660                                                       (car sequence)))
661         (setq sequence (cdr sequence))))
662      (t
663       (setq address-string (wl-address-concat-token address-string token))
664       (setq sequence (cdr sequence)))))
665   address-string))
666
667 (defun wl-address-delete (the-email)
668   "Delete address entry in the `wl-address-file'."
669   (let ((output-coding-system
670          (mime-charset-to-coding-system wl-mime-charset)))
671     (with-temp-buffer
672       (message "Deleting Address...")
673       (insert-file-contents wl-address-file)
674       (delete-matching-lines (concat "^[ \t]*" the-email "[ \t]+\".*\"[ \t]+\".*\"$"))
675       (write-region (point-min) (point-max)
676                     wl-address-file nil 'no-msg)
677       ;; Delete entries.
678       (dolist (entry (elmo-string-assoc-all the-email wl-address-list))
679         (setq wl-address-list (delete entry wl-address-list)))
680       (elmo-set-hash-val the-email nil wl-address-petname-hash)
681       (message "Deleting Address...done"))))
682
683 (defun wl-address-add-or-change (address
684                                  &optional default-realname
685                                  change-address)
686   "Add address entry to `wl-address-file', if not registerd.
687 If already registerd, change it."
688   (let ((entry (assoc address wl-address-list))
689         the-realname the-petname new-addr addr-changed)
690     (setq the-realname
691           (read-from-minibuffer "Real Name: " (or default-realname
692                                                   (nth 2 entry))))
693     (setq the-petname (read-from-minibuffer "Petname: "
694                                             (or (nth 1 entry)
695                                                 the-realname)))
696     (when change-address
697       (setq new-addr (read-from-minibuffer "E-Mail: " address))
698       (cond
699        ((or (not (stringp new-addr))
700             (string-match "^[ \t]*$" new-addr))
701         (error "empty address"))
702        ((and (not (string= address new-addr))
703              (assoc new-addr wl-address-list))
704         (error "'%s' already exists" new-addr))
705        (t
706         ;; do nothing
707         )))
708     ;; writing to ~/.address
709     (let ((output-coding-system
710            (mime-charset-to-coding-system wl-mime-charset)))
711       (with-temp-buffer
712         (if (file-exists-p wl-address-file)
713             (insert-file-contents wl-address-file))
714         (if (null entry)
715             ;; add
716             (progn
717               (goto-char (point-max))
718               (if (and (> (buffer-size) 0)
719                        (not (eq (char-after (1- (point-max))) ?\n)))
720                   (insert "\n")))
721           ;; override
722           (while (re-search-forward (concat "^[ \t]*" address) nil t)
723             (delete-region (point-at-bol) (1+ (point-at-eol)))))
724         (insert (format "%s\t%s\t%s\n"
725                         (or new-addr address)
726                         (prin1-to-string the-petname)
727                         (prin1-to-string the-realname)))
728         (write-region (point-min) (point-max)
729                       wl-address-file nil 'no-msg)
730         (wl-address-init)
731         (list (or new-addr address) the-petname the-realname)))))
732
733 ;; Read addresses from minibuffer with completion.
734 (defvar wl-address-minibuffer-history nil)
735 (defvar wl-address-minibuffer-local-map nil
736   "Keymap to use when reading address from the minibuffer.")
737
738 (unless wl-address-minibuffer-local-map
739   (let ((map (make-sparse-keymap)))
740     (set-keymap-parent map minibuffer-local-map)
741     (define-key map "\C-i"
742       (lambda ()
743         (interactive)
744         (wl-complete-field-body wl-address-completion-list
745                                 ?@ nil wl-use-ldap)))
746     (setq wl-address-minibuffer-local-map map)))
747
748 (defun wl-address-read-from-minibuffer (prompt &optional
749                                                initial-contents
750                                                default-value)
751   (read-from-minibuffer prompt
752                         initial-contents
753                         wl-address-minibuffer-local-map
754                         nil
755                         'wl-address-minibuffer-history
756                         default-value))
757
758 (require 'product)
759 (product-provide (provide 'wl-address) (require 'wl-version))
760
761 ;;; wl-address.el ends here
762