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