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)
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.
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)
47 (defvar wl-address-ldap-search-hash nil)
49 (eval-when-compile (require 'pldap))
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.
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
61 For example, following dn data is exsist, alias of each level is shown
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_
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
83 (defconst wl-ldap-alias-sep "/")
85 (defconst wl-ldap-search-attribute-type-list
88 (defun wl-ldap-get-value (type entry)
90 (let* ((values (cdr (assoc type entry)))
92 (if (and ret (not ldap-ignore-attribute-codings))
94 (if (not (string-match "^[\000-\177]*$" (car values)))
95 (setq ret (car values)
97 (setq values (cdr values)))))
100 (defun wl-ldap-get-value-list (type entry)
102 (cdr (assoc type entry)))
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
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
119 (setq type (car (car entry))
120 values (mapcar (function wl-ldap-alias-safe-string)
122 values (elmo-flatten values)
124 (if (string-match "::?$" type)
125 (setq type (substring type 0 (match-beginning 0))))
126 (if (member type type-list)
128 (setq val (car values)
130 (if (and (string-match regexp val)
131 (not (member val result)))
132 (setq result (cons val result))))))
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))
142 (substring str (match-end 0)))))
143 (if (string-match "\\(@\\)[^/@]+" str)
144 (setq str (split-string str "[@\\.]")))
147 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
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
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
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
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)
176 (while (and (< 0 level) dn-list)
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))
185 (progn (set sym (list dn str dn-list))
188 (if (not (eq (setq value (symbol-value sym)) t))
191 (apply (function wl-ldap-register-dn-string) hash value)))
192 (wl-ldap-register-dn-string hash dn str dn-list)))))
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."
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))
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
210 (mapatoms (lambda (atom)
211 (if (and (string-match
212 (concat "^" (symbol-name atom) ".*") pat)
215 (setq len (length (symbol-name atom))))))
217 (or len (length (symbol-name atom)))
218 (symbol-value atom)))))
219 wl-address-ldap-search-hash)
220 ;; get matched entries
222 (setq entries (cdr cache))
225 (message "Searching in LDAP...")
226 (setq entries (ldap-search-entries
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
237 (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
238 (setq tmpl (cdr tmpl)))
240 (setq regexp (concat "^" pat))
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
256 ;; check uniqness then add to list
257 (setq sym (intern (downcase alias) dnhash))
258 (when (not (boundp sym))
260 (setq result (cons (cons alias
261 (concat cn " <" (car mails) ">"))
263 (setq values (cdr values)))
264 ;; make mail addrses list
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) ">"))
272 (setq mails (cdr mails)))
273 (setq entries (cdr entries)))
276 (defun wl-complete-field-to ()
278 (let ((cl wl-address-completion-list))
280 (completing-read "To: " cl)
281 (read-string "To: "))))
283 (defalias 'wl-address-quote-specials 'elmo-address-quote-specials)
285 (defun wl-address-make-completion-list (address-list)
288 (setq addr-tuple (car address-list))
291 (cons (nth 0 addr-tuple)
293 (wl-address-quote-specials
294 (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
296 ;; nickname completion.
297 (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
299 (assoc (nth 1 addr-tuple) cl))
302 (cons (nth 1 addr-tuple)
304 (wl-address-quote-specials
305 (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
307 (setq address-list (cdr address-list)))
310 (defun wl-complete-field-body-or-tab ()
312 (let ((case-fold-search t)
313 epand-char skip-chars
316 (if (wl-draft-on-field-p)
321 (goto-char (point-min))
322 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
327 (while (and (looking-at "^[ \t]")
328 (not (= (point) (point-min))))
330 (cond ((looking-at wl-address-complete-header-regexp)
331 (setq completion-list wl-address-completion-list)
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)))))
344 (defvar wl-completion-buf-name "*Completions*")
346 (defvar wl-complete-candidates nil)
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)))
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")))
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)))))
370 (defun wl-complete-field ()
373 (start (save-excursion
374 (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
377 (pattern (buffer-substring start end))
378 (cl wl-draft-field-completion-list))
382 (let ((completion-ignore-case t))
383 (try-completion pattern cl)))
384 (cond ((eq completion t)
385 (let ((alias (assoc pattern cl)))
388 (delete-region start end)
390 ;;; (wl-highlight-message (point-min)(point-max) t)
392 (wl-complete-window-delete))
394 (message "Can't find completion for \"%s\"" pattern)
396 ((not (string= pattern completion))
397 (delete-region start end)
399 (wl-complete-window-delete)
400 (wl-highlight-message (point-min)(point-max) t))
402 (let ((list (all-completions pattern cl)))
403 (wl-complete-window-show list)))))))
405 (defun wl-complete-insert (start end pattern completion-list)
406 (let ((alias (and (consp completion-list)
407 (assoc pattern completion-list)))
411 (delete-region start end)
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)))))))
417 (defun wl-complete-field-body (completion-list
418 &optional epand-char skip-chars use-ldap)
421 (start (save-excursion
422 (skip-chars-backward (or skip-chars "^:,>\n"))
423 (skip-chars-forward " \t")
426 (pattern (buffer-substring start end))
427 (len (length pattern))
428 (cl completion-list))
430 (setq cl (wl-address-ldap-search pattern cl)))
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"))
441 (char-equal (aref pattern (1- len)) epand-char)
442 (assoc (substring pattern 0 (1- len)) cl))
445 (substring pattern 0 (1- len))
448 (message "Can't find completion for \"%s\"" pattern)
450 ((not (string= pattern completion))
451 (delete-region start end)
454 (let ((list (sort (all-completions pattern cl) 'string<)))
455 (wl-complete-window-show list)))))))
457 (defvar wl-address-init-function 'wl-local-address-init)
459 (defun wl-address-init ()
460 "Call `wl-address-init-function'."
461 (funcall wl-address-init-function))
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))
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"))
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)))
501 (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
502 (or new-expn expn))))
504 (setcdr (nth n alist) new-expn-str))
507 (wl-address-expand-aliases alist (1+ nest-count))))))
509 (defun wl-address-make-alist-from-alias-file (file)
511 (let ((case-fold-search t)
513 (insert-file-contents file)
514 (while (re-search-forward ",$" nil t)
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
527 (defun wl-address-make-address-list (path)
528 (if (and path (file-readable-p path))
531 (coding-system-for-read wl-cs-autoconv))
532 (insert-file-contents path)
533 (goto-char (point-min))
536 "^\\([^#\n][^ \t\n]+\\)[ \t]+\\(\".*\"\\)[ \t]+\\(\".*\"\\)[ \t]*.*$")
540 (list (wl-match-buffer 1)
541 (read (wl-match-buffer 2))
542 (read (wl-match-buffer 3))))))
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)))
550 (defsubst wl-address-get-petname (string)
551 (or (wl-address-get-petname-1 string)
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)
559 (wl-address-header-extract-address
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))
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>\"
578 (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
579 (wl-match-string 1 str))
582 (defmacro wl-address-concat-token (string token)
584 ((eq 'quoted-string (car (, token)))
585 (concat (, string) "\"" (cdr (, token)) "\""))
586 ((eq 'comment (car (, token)))
587 (concat (, string) "(" (cdr (, token)) ")"))
589 (concat (, string) (cdr (, token)))))))
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)
596 (setq token (car sequence))
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))
605 (while (not group-end)
606 (setq token (car seq))
608 (setq group-end (and (eq 'specials (car token))
609 (string= (cdr token) ";"))))
610 (setq address-string (concat address-string (cdr token))) ; ';'
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
621 (setq sequence (cdr sequence))))
623 (setq address-string (wl-address-concat-token address-string token))
624 (setq sequence (cdr sequence)))))
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)))
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)
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"))))
643 (defun wl-address-add-or-change (address
644 &optional default-realname
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)
651 (read-from-minibuffer "Real Name: " (or default-realname
653 (setq the-petname (read-from-minibuffer "Petname: "
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)))
665 (if (file-exists-p wl-address-file)
666 (insert-file-contents wl-address-file))
670 (goto-char (point-max))
671 (if (and (> (buffer-size) 0)
672 (not (eq (char-after (1- (point-max))) ?\n)))
675 (while (re-search-forward (concat "^[ \t]*" address) nil t)
676 (delete-region (save-excursion (beginning-of-line)
678 (save-excursion (end-of-line)
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)
687 (list (or new-addr address) the-petname the-realname)))))
690 (product-provide (provide 'wl-address) (require 'wl-version))
692 ;;; wl-address.el ends here