Update message count on command "select <folder>"
[more-wl.git] / wl / wl-address.el
blob69abb20c91893fe927e7d8abfc5b9d53a698842e
1 ;;; wl-address.el --- Tiny address management for Wanderlust.
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>
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
12 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
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.
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.
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.
30 ;;; Commentary:
33 ;;; Code:
36 (require 'wl-util)
37 (require 'wl-vars)
38 (require 'std11)
39 (eval-when-compile (require 'cl))
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)
52 (defvar wl-address-ldap-search-hash nil)
54 (eval-when-compile (require 'pldap))
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.
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.
66 For example, following dn data is exsist, alias of each level is shown
67 bellow.
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_
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...)")
88 (defconst wl-ldap-alias-sep "/")
90 (defconst wl-ldap-search-attribute-type-list
91 '("sn" "cn" "mail" "email"))
93 (defun wl-ldap-get-value (type entry)
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))
105 (defun wl-ldap-get-value-list (type entry)
107 (cdr (assoc type entry)))
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
116 ")"))
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))
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))
145 (substring str (match-end 0)))))
146 (if (string-match "\\(@\\)[^/@]+" str)
147 (setq str (split-string str "[@\\.]")))
148 str)
150 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
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)))
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)))))
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)))
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)))
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)))
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))))
291 (defalias 'wl-address-quote-specials 'elmo-address-quote-specials)
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))
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)">"))))
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)))))
360 (defvar wl-completion-buf-name "*Completions*")
362 (defvar wl-complete-candidates nil)
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 (save-excursion
369 (set-buffer wl-completion-buf-name)
370 (if (pos-visible-in-window-p (point-max) win)
371 (set-window-start win 1)
372 (scroll-other-window))))
373 (message "Making completion list...")
374 (setq wl-complete-candidates all)
375 (with-output-to-temp-buffer
376 wl-completion-buf-name
377 (display-completion-list all))
378 (message "Making completion list...done")))
380 (defun wl-complete-window-delete ()
381 (let (comp-buf comp-win)
382 (if (setq comp-buf (get-buffer wl-completion-buf-name))
383 (if (setq comp-win (get-buffer-window comp-buf))
384 (delete-window comp-win)))))
386 (defun wl-complete-field ()
387 (interactive)
388 (let* ((end (point))
389 (start (save-excursion
390 (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
391 (point)))
392 (completion)
393 (pattern (buffer-substring start end))
394 (cl wl-draft-field-completion-list))
395 (if (null cl)
397 (setq completion
398 (let ((completion-ignore-case t))
399 (try-completion pattern cl)))
400 (cond ((eq completion t)
401 (let ((alias (assoc pattern cl)))
402 (if alias
403 (progn
404 (delete-region start end)
405 (insert (cdr alias))
406 ;;; (wl-highlight-message (point-min)(point-max) t)
408 (wl-complete-window-delete))
409 ((null completion)
410 (message "Can't find completion for \"%s\"" pattern)
411 (ding))
412 ((not (string= pattern completion))
413 (delete-region start end)
414 (insert completion)
415 (wl-complete-window-delete)
416 (wl-highlight-message (point-min)(point-max) t))
418 (let ((list (all-completions pattern cl)))
419 (wl-complete-window-show list)))))))
421 (defun wl-complete-insert (start end pattern completion-list)
422 (let ((alias (and (consp completion-list)
423 (assoc pattern completion-list)))
424 comp-buf comp-win)
425 (if alias
426 (progn
427 (delete-region start end)
428 (insert (cdr alias))
429 (if (setq comp-buf (get-buffer wl-completion-buf-name))
430 (if (setq comp-win (get-buffer-window comp-buf))
431 (delete-window comp-win)))))))
433 (defun wl-complete-field-body (completion-list
434 &optional epand-char skip-chars use-ldap)
435 (interactive)
436 (let* ((end (point))
437 (start (save-excursion
438 (skip-chars-backward (or skip-chars "^:,>\n"))
439 (skip-chars-forward " \t")
440 (point)))
441 (completion)
442 (pattern (buffer-substring start end))
443 (len (length pattern))
444 (completion-ignore-case t)
445 (cl completion-list))
446 (when use-ldap
447 (setq cl (wl-address-ldap-search pattern cl)))
448 (if (null cl)
450 (setq completion (try-completion pattern cl))
451 (cond ((eq completion t)
452 (if use-ldap (setq wl-address-ldap-search-hash nil))
453 (wl-complete-insert start end pattern cl)
454 (wl-complete-window-delete)
455 (message "Sole completion"))
456 ((and epand-char
457 (> len 0)
458 (or (char-equal (aref pattern (1- len)) epand-char)
459 (char-equal (aref pattern (1- len)) ?\ ))
460 (assoc (substring pattern 0 (1- len)) cl))
461 (wl-complete-insert
462 start end
463 (substring pattern 0 (1- len))
464 cl))
465 ((null completion)
466 (message "Can't find completion for \"%s\"" pattern)
467 (ding))
468 ((not (string= pattern completion))
469 (delete-region start end)
470 (insert completion))
472 (let ((list (sort (all-completions pattern cl) 'string<)))
473 (wl-complete-window-show list)))))))
475 (defvar wl-address-init-function 'wl-local-address-init)
477 (defun wl-address-init ()
478 "Call `wl-address-init-function'."
479 (funcall wl-address-init-function))
481 (defun wl-local-address-init ()
482 "Reload `wl-address-file'.
483 Refresh `wl-address-list', `wl-address-completion-list', and
484 `wl-address-petname-hash'."
485 (message "Updating addresses...")
486 (setq wl-address-list
487 (wl-address-make-address-list wl-address-file))
488 (setq wl-address-completion-list
489 (wl-address-make-completion-list wl-address-list))
490 (if (file-readable-p wl-alias-file)
491 (setq wl-address-completion-list
492 (append wl-address-completion-list
493 (wl-address-make-alist-from-alias-file wl-alias-file))))
494 (setq wl-address-petname-hash (elmo-make-hash))
495 (let ((addresses wl-address-list))
496 (while addresses
497 (elmo-set-hash-val (downcase (car (car addresses)))
498 (cadr (car addresses))
499 wl-address-petname-hash)
500 (setq addresses (cdr addresses))))
501 (message "Updating addresses...done"))
504 (defun wl-address-expand-aliases (alist nest-count)
505 (when (< nest-count 5)
506 (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
507 (while (setq expn-str (cdr (nth n alist)))
508 (setq new-expn-str nil)
509 (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
510 (setq expn (elmo-match-string 1 expn-str))
511 (setq expn-str (wl-string-delete-match expn-str 0))
512 (if (string-match "^[ \t,]+" expn-str)
513 (setq expn-str (wl-string-delete-match expn-str 0)))
514 (if (string-match "[ \t,]+$" expn)
515 (setq expn (wl-string-delete-match expn 0)))
516 (setq new-expn (cdr (assoc expn alist)))
517 (if new-expn
518 (setq expanded t))
519 (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
520 (or new-expn expn))))
521 (when new-expn-str
522 (setcdr (nth n alist) new-expn-str))
523 (setq n (1+ n)))
524 (and expanded
525 (wl-address-expand-aliases alist (1+ nest-count))))))
527 (defun wl-address-make-alist-from-alias-file (file)
528 (with-temp-buffer
529 (let ((case-fold-search t)
530 alias expn alist)
531 (insert-file-contents file)
532 (while (re-search-forward ",$" nil t)
533 (end-of-line)
534 (forward-char 1)
535 (delete-backward-char 1))
536 (goto-char (point-min))
537 (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
538 (setq alias (wl-match-buffer 1)
539 expn (wl-match-buffer 2))
540 (setq alist (cons (cons alias expn) alist)))
541 (wl-address-expand-aliases alist 0)
542 (nreverse alist) ; return value
545 (defun wl-address-make-address-list (path)
546 (when (and path (file-readable-p path))
547 (with-temp-buffer
548 (let (ret
549 (coding-system-for-read wl-cs-autoconv))
550 (insert-file-contents path)
551 (goto-char (point-min))
552 (while (not (eobp))
553 (if (looking-at "\
554 ^\\([^#\n][^ \t\n]+\\)[ \t]+\\(\".*\"\\)[ \t]+\\(\".*\"\\)[ \t]*.*$")
555 (setq ret
556 (cons
557 (list (wl-match-buffer 1)
558 (read (wl-match-buffer 2))
559 (read (wl-match-buffer 3)))
560 ret)))
561 (forward-line))
562 (nreverse ret)))))
565 (defsubst wl-address-header-extract-address (str)
566 "Extracts a real e-mail address from STR and return it.
567 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
568 -> \"m-sakura@ccs.mt.nec.co.jp\".
569 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
570 -> \"m-sakura@ccs.mt.nec.co.jp\"."
571 (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
572 (wl-match-string 1 str))
573 ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
574 (wl-match-string 1 str))
575 (t str)))
577 (defsubst wl-address-header-extract-realname (str)
578 "Extracts a real name from STR and return it.
579 e.g. \"Mr. bar <hoge@foo.com>\"
580 -> \"Mr. bar\"."
581 (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
582 (wl-match-string 1 str))
583 (t "")))
586 (defun wl-address-get-petname-1 (string)
587 (let ((address (downcase (wl-address-header-extract-address string))))
588 (elmo-get-hash-val address wl-address-petname-hash)))
590 (defsubst wl-address-get-petname (string)
591 (or (wl-address-get-petname-1 string)
592 string))
594 (defun wl-address-user-mail-address-p (address)
595 "Judge whether ADDRESS is user's or not."
596 (if wl-user-mail-address-regexp
597 (string-match wl-user-mail-address-regexp
598 (wl-address-header-extract-address address))
599 (member (downcase (wl-address-header-extract-address address))
600 (or (mapcar 'downcase wl-user-mail-address-list)
601 (list (downcase
602 (wl-address-header-extract-address
603 wl-from)))))))
605 (defun wl-address-delete-user-mail-addresses (address-list)
606 "Delete user mail addresses from list by side effect.
607 Deletion is done by using `elmo-list-delete'."
608 (if wl-user-mail-address-regexp
609 (elmo-list-delete (list wl-user-mail-address-regexp) address-list
610 (lambda (elem list)
611 (elmo-delete-if
612 (lambda (item) (string-match elem item))
613 list)))
614 (let ((myself (or wl-user-mail-address-list
615 (list (wl-address-header-extract-address wl-from)))))
616 (elmo-list-delete myself address-list
617 (lambda (elem list)
618 (elmo-delete-if
619 (lambda (item) (string= (downcase elem)
620 (downcase item)))
621 list))))))
623 (defmacro wl-address-concat-token (string token)
624 `(cond
625 ((eq 'quoted-string (car ,token))
626 (concat ,string "\"" (cdr ,token) "\""))
627 ((eq 'comment (car ,token))
628 (concat ,string "(" (cdr ,token) ")"))
630 (concat ,string (cdr ,token)))))
632 (defun wl-address-string-without-group-list-contents (sequence)
633 "Return address string from lexical analyzed list SEQUENCE.
634 Group list contents is not included."
635 (let (address-string route-addr-end token seq group-end)
636 (while sequence
637 (setq token (car sequence))
638 (cond
639 ;; group = phrase ":" [#mailbox] ";"
640 ((and (eq 'specials (car token))
641 (string= (cdr token) ":"))
642 (setq address-string (concat address-string (cdr token))) ; ':'
643 (setq seq (cdr sequence))
644 (setq token (car seq))
645 (setq group-end nil)
646 (while (not group-end)
647 (setq token (car seq))
648 (setq seq (cdr seq))
649 (setq group-end (and (eq 'specials (car token))
650 (string= (cdr token) ";"))))
651 (setq address-string (concat address-string (cdr token))) ; ';'
652 (setq sequence seq))
653 ;; route-addr = "<" [route] addr-spec ">"
654 ;; route = 1#("@" domain) ":" ; path-relative
655 ((and (eq 'specials (car token))
656 (string= (cdr token) "<"))
657 (setq seq (std11-parse-route-addr sequence))
658 (setq route-addr-end (car (cdr seq)))
659 (while (not (eq (car sequence) route-addr-end))
660 (setq address-string (wl-address-concat-token address-string
661 (car sequence)))
662 (setq sequence (cdr sequence))))
664 (setq address-string (wl-address-concat-token address-string token))
665 (setq sequence (cdr sequence)))))
666 address-string))
668 (defun wl-address-delete (the-email)
669 "Delete address entry in the `wl-address-file'."
670 (let ((output-coding-system
671 (mime-charset-to-coding-system wl-mime-charset)))
672 (with-temp-buffer
673 (message "Deleting Address...")
674 (insert-file-contents wl-address-file)
675 (delete-matching-lines (concat "^[ \t]*" the-email "[ \t]+\".*\"[ \t]+\".*\"$"))
676 (write-region (point-min) (point-max)
677 wl-address-file nil 'no-msg)
678 ;; Delete entries.
679 (dolist (entry (elmo-string-assoc-all the-email wl-address-list))
680 (setq wl-address-list (delete entry wl-address-list)))
681 (elmo-set-hash-val the-email nil wl-address-petname-hash)
682 (message "Deleting Address...done"))))
684 (defun wl-address-add-or-change (address
685 &optional default-realname
686 change-address)
687 "Add address entry to `wl-address-file', if not registerd.
688 If already registerd, change it."
689 (let ((entry (assoc address wl-address-list))
690 the-realname the-petname new-addr addr-changed)
691 (setq the-realname
692 (read-from-minibuffer "Real Name: " (or default-realname
693 (nth 2 entry))))
694 (setq the-petname (read-from-minibuffer "Petname: "
695 (or (nth 1 entry)
696 the-realname)))
697 (when change-address
698 (setq new-addr (read-from-minibuffer "E-Mail: " address))
699 (cond
700 ((or (not (stringp new-addr))
701 (string-match "^[ \t]*$" new-addr))
702 (error "empty address"))
703 ((and (not (string= address new-addr))
704 (assoc new-addr wl-address-list))
705 (error "'%s' already exists" new-addr))
707 ;; do nothing
709 ;; writing to ~/.address
710 (let ((output-coding-system
711 (mime-charset-to-coding-system wl-mime-charset)))
712 (with-temp-buffer
713 (if (file-exists-p wl-address-file)
714 (insert-file-contents wl-address-file))
715 (if (null entry)
716 ;; add
717 (progn
718 (goto-char (point-max))
719 (if (and (> (buffer-size) 0)
720 (not (eq (char-after (1- (point-max))) ?\n)))
721 (insert "\n")))
722 ;; override
723 (while (re-search-forward (concat "^[ \t]*" address) nil t)
724 (delete-region (save-excursion (beginning-of-line)
725 (point))
726 (save-excursion (end-of-line)
727 (+ 1 (point))))))
728 (insert (format "%s\t%s\t%s\n"
729 (or new-addr address)
730 (prin1-to-string the-petname)
731 (prin1-to-string the-realname)))
732 (write-region (point-min) (point-max)
733 wl-address-file nil 'no-msg)
734 (wl-address-init)
735 (list (or new-addr address) the-petname the-realname)))))
737 ;; Read addresses from minibuffer with completion.
738 (defvar wl-address-minibuffer-history nil)
739 (defvar wl-address-minibuffer-local-map nil
740 "Keymap to use when reading address from the minibuffer.")
742 (unless wl-address-minibuffer-local-map
743 (let ((map (make-sparse-keymap)))
744 (set-keymap-parent map minibuffer-local-map)
745 (define-key map "\C-i"
746 (lambda ()
747 (interactive)
748 (wl-complete-field-body wl-address-completion-list
749 ?@ nil wl-use-ldap)))
750 (setq wl-address-minibuffer-local-map map)))
752 (defun wl-address-read-from-minibuffer (prompt &optional
753 initial-contents
754 default-value)
755 (read-from-minibuffer prompt
756 initial-contents
757 wl-address-minibuffer-local-map
759 'wl-address-minibuffer-history
760 default-value))
762 (require 'product)
763 (product-provide (provide 'wl-address) (require 'wl-version))
765 ;;; wl-address.el ends here