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