1 ;;; wl-address.el -- Tiny address management for Wanderlust.
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
36 (defvar wl-address-complete-header-regexp "^\\(To\\|From\\|Cc\\|Bcc\\|Mail-Followup-To\\|Reply-To\\|Return-Receipt-To\\):")
37 (defvar wl-newsgroups-complete-header-regexp "^\\(Newsgroups\\|Followup-To\\):")
38 (defvar wl-folder-complete-header-regexp "^\\(FCC\\):")
39 (defvar wl-address-list nil)
40 (defvar wl-address-completion-list nil)
41 (defvar wl-address-petname-hash nil)
43 (defvar wl-address-ldap-search-hash nil)
45 (eval-when-compile (require 'pldap))
47 (defvar wl-ldap-alias-dn-level nil
48 "Level of dn data to make alias postfix.
49 Valid value is nit, t, 1 or larget integer.
51 If this value nil, minimum alias postfix is made depends on uniqness
52 with other candidates. In this implementation, it's same to 1. If t,
53 always append all dn data. If number, always append spcified level of
54 data but maybe appended more uniqness. If invalid value, treat as
57 For example, following dn data is exsist, alias of each level is shown
61 dn: CN=Shun-ichi GOTO,OU=Mew,OU=Emacs,OU=Lisper,O=Programmers Inc.
62 nil => Goto/Shun-ichi_GOTO
63 1 => Goto/Shun-ichi_GOTO
64 2 => Goto/Shun-ichi_GOTO/Mew
65 3 => Goto/Shun-ichi_GOTO/Mew/Emacs
66 4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
67 5 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
68 6 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
69 t => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
71 If level 3 is required for uniqness with other candidates,
72 nil => Goto/Shun-ichi_GOTO/Mew/Emacs ... appended more
73 1 => Goto/Shun-ichi_GOTO/Mew/Emacs ... appended more
74 2 => Goto/Shun-ichi_GOTO/Mew/Emacs ... appended more
75 3 => Goto/Shun-ichi_GOTO/Mew/Emacs
76 4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
80 (defconst wl-ldap-alias-sep "/")
82 (defconst wl-ldap-search-attribute-type-list
85 (defun wl-ldap-get-value (type entry)
87 (let* ((values (cdr (assoc type entry)))
89 (if (and ret (not ldap-ignore-attribute-codings))
91 (if (not (string-match "^[\000-\177]*$" (car values)))
92 (setq ret (car values)
94 (setq values (cdr values)))))
97 (defun wl-ldap-get-value-list (type entry)
99 (cdr (assoc type entry)))
101 (defun wl-ldap-make-filter (pat type-list)
102 "Make RFC1558 quiery filter for PAT from ATTR-LIST.
103 Each are \"OR\" combination, and PAT is beginning-match."
104 (concat "(&(objectclass=person)(|"
105 (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
110 (defun wl-ldap-make-matched-value-list (regexp type-list entry)
111 "Correct matching WORD with value of TYPE-LIST in ENTRY.
112 Returns matched uniq string list."
113 (let (type val values result)
114 ;; collect matching value
116 (setq type (car (car entry))
117 values (mapcar (function wl-ldap-alias-safe-string)
120 (if (string-match "::?$" type)
121 (setq type (substring type 0 (match-beginning 0))))
122 (if (member type type-list)
124 (setq val (car values)
126 (if (and (string-match regexp val)
127 (not (member val result)))
128 (setq result (cons val result))))))
131 (defun wl-ldap-alias-safe-string (str)
132 "Modify STR for alias.
133 Replace space/tab in STR into '_' char.
134 And remove domain part of mail addr."
135 (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
136 (setq str (concat (substring str 0 (match-beginning 0))
138 (substring str (match-end 0)))))
139 (if (string-match "@[^/@]+" str)
140 (setq str (concat (substring str 0 (match-beginning 0))
141 (substring str (match-end 0)))))
144 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
146 (let (sym dnsym value level)
147 (setq dnsym (intern (upcase dn) hash))
148 (if (and (null str) (boundp dnsym))
149 () ; already processed
150 ;; make dn-list in fisrt time
152 (let ((case-fold-search t))
153 (setq dn-list (mapcar (lambda (str)
154 (if (string-match "[a-z]+=\\(.*\\)" str)
155 (wl-ldap-alias-safe-string
156 (wl-match-string 1 str))))
157 (split-string dn ",")))))
158 ;; prepare candidate for uniq str
160 (setq str (concat str wl-ldap-alias-sep (car dn-list))
161 dn-list (cdr dn-list))
162 ;; first entry, pre-build with given level
164 ((null wl-ldap-alias-dn-level) (setq level 1))
165 ((eq t wl-ldap-alias-dn-level) (setq level 1000)) ; xxx, big enough
166 ((numberp wl-ldap-alias-dn-level)
167 (if (< 0 wl-ldap-alias-dn-level)
168 (setq level wl-ldap-alias-dn-level)
172 (while (and (< 0 level) dn-list)
174 (setq str (car dn-list))
175 (setq str (concat str wl-ldap-alias-sep (car dn-list))))
176 (setq level (1- level)
177 dn-list (cdr dn-list))))
178 (setq sym (intern (upcase str) hash))
179 (if (not (boundp sym))
181 (progn (set sym (list dn str dn-list))
184 (if (not (eq (setq value (symbol-value sym)) t))
187 (apply (function wl-ldap-register-dn-string) hash value)))
188 (wl-ldap-register-dn-string hash dn str dn-list)))))
190 (defun wl-address-ldap-search (pattern cl)
191 "Make address completion-list matched for PATTERN by LDAP search.
192 Matched address lists are append to CL."
194 (unless wl-address-ldap-search-hash
195 (setq wl-address-ldap-search-hash (elmo-make-hash 7)))
196 (let ((pat (if (string-match wl-ldap-alias-sep pattern)
197 (substring pattern 0 (match-beginning 0))
199 (ldap-default-host wl-ldap-server)
200 (ldap-default-port (or wl-ldap-port 389))
201 (ldap-default-base wl-ldap-base)
202 (dnhash (elmo-make-hash))
203 cache len sym tmpl regexp entries ent values dn dnstr alias
206 (mapatoms (lambda (atom)
207 (if (and (string-match
208 (concat "^" (symbol-name atom) ".*") pat)
211 (setq len (length (symbol-name atom))))))
213 (or len (length (symbol-name atom)))
214 (symbol-value atom)))))
215 wl-address-ldap-search-hash)
216 ;; get matched entries
218 (setq entries (cdr cache))
221 (message "Searching in LDAP...")
222 (setq entries (ldap-search-entries
225 wl-ldap-search-attribute-type-list)
226 nil wl-ldap-search-attribute-type-list nil t))
227 (message "Searching in LDAP...done")
228 (elmo-set-hash-val pattern entries wl-address-ldap-search-hash))
229 (error (message "")))) ; ignore error: No such object
233 (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
234 (setq tmpl (cdr tmpl)))
236 (setq regexp (concat "^" pat))
238 (setq ent (cdar entries)
239 values (wl-ldap-make-matched-value-list
240 regexp '("mail" "sn" "cn") ent)
241 mails (wl-ldap-get-value-list "mail" ent)
242 cn (wl-ldap-get-value "cn" ent)
243 dn (car (car entries))
244 dnstr (elmo-get-hash-val (upcase dn) dnhash))
245 ;; make alias list generated from LDAP data.
246 (while (and mails values)
247 ;; make alias like MATCHED/DN-STRING
248 (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
249 (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
250 ;; use DN-STRING if DN-STRING begin with MATCHED
252 ;; check uniqness then add to list
253 (setq sym (intern (downcase alias) dnhash))
254 (when (not (boundp sym))
256 (setq result (cons (cons alias
257 (concat cn " <" (car mails) ">"))
259 (setq values (cdr values)))
260 ;; make mail addrses list
262 (if (null (assoc (car mails) cl)); Not already in cl.
263 ;; (string-match regexp (car mails))
264 ;; add mail address itself to completion list
265 (setq result (cons (cons (car mails)
266 (concat cn " <" (car mails) ">"))
268 (setq mails (cdr mails)))
269 (setq entries (cdr entries)))
272 (defun wl-complete-field-to ()
274 (let ((cl wl-address-completion-list))
276 (completing-read "To: " cl)
277 (read-string "To: "))))
279 (defun wl-address-quote-specials (word)
280 "Make quoted string of WORD if needed."
281 (if (assq 'specials (std11-lexical-analyze word))
282 (prin1-to-string word)
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-func 'wl-local-address-init)
459 (defun wl-address-init ()
460 (funcall wl-address-init-func))
462 (defun wl-local-address-init ()
463 (message "Updating addresses...")
464 (setq wl-address-list
465 (wl-address-make-address-list wl-address-file))
466 (setq wl-address-completion-list
467 (wl-address-make-completion-list wl-address-list))
468 (if (file-readable-p wl-alias-file)
469 (setq wl-address-completion-list
470 (append wl-address-completion-list
471 (wl-address-make-alist-from-alias-file wl-alias-file))))
472 (setq wl-address-petname-hash (elmo-make-hash))
473 (let ((addresses wl-address-list))
475 (elmo-set-hash-val (downcase (car (car addresses)))
476 (cadr (car addresses))
477 wl-address-petname-hash)
478 (setq addresses (cdr addresses))))
479 (message "Updating addresses...done."))
482 (defun wl-address-expand-aliases (alist nest-count)
483 (when (< nest-count 5)
484 (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
485 (while (setq expn-str (cdr (nth n alist)))
486 (setq new-expn-str nil)
487 (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
488 (setq expn (elmo-match-string 1 expn-str))
489 (setq expn-str (wl-string-delete-match expn-str 0))
490 (if (string-match "^[ \t,]+" expn-str)
491 (setq expn-str (wl-string-delete-match expn-str 0)))
492 (if (string-match "[ \t,]+$" expn)
493 (setq expn (wl-string-delete-match expn 0)))
494 (setq new-expn (cdr (assoc expn alist)))
497 (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
498 (or new-expn expn))))
500 (setcdr (nth n alist) new-expn-str))
503 (wl-address-expand-aliases alist (1+ nest-count))))))
505 (defun wl-address-make-alist-from-alias-file (file)
507 (let ((case-fold-search t)
509 (insert-file-contents file)
510 (while (re-search-forward ",$" nil t)
513 (delete-backward-char 1))
514 (goto-char (point-min))
515 (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
516 (setq alias (wl-match-buffer 1)
517 expn (wl-match-buffer 2))
518 (setq alist (cons (cons alias expn) alist)))
519 (wl-address-expand-aliases alist 0)
520 (nreverse alist) ; return value
523 (defun wl-address-make-address-list (path)
524 (if (and path (file-readable-p path))
527 (coding-system-for-read wl-cs-autoconv))
528 (insert-file-contents path)
529 (goto-char (point-min))
532 "^\\([^#\n][^ \t\n]+\\)[ \t]+\"\\(.*\\)\"[ \t]+\"\\(.*\\)\"[ \t]*.*$")
536 (list (wl-match-buffer 1)
538 (wl-match-buffer 3)))))
542 (defsubst wl-address-get-petname-1 (string)
543 (let ((address (downcase (wl-address-header-extract-address string))))
544 (elmo-get-hash-val address wl-address-petname-hash)))
546 (defsubst wl-address-get-petname (string)
547 (or (wl-address-get-petname-1 string)
550 (defsubst wl-address-user-mail-address-p (address)
551 "Judge whether ADDRESS is user's or not."
552 (member (downcase (wl-address-header-extract-address address))
553 (or (mapcar 'downcase wl-user-mail-address-list)
555 (wl-address-header-extract-address
558 (defsubst wl-address-header-extract-address (str)
559 "Extracts a real e-mail address from STR and returns it.
560 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
561 -> \"m-sakura@ccs.mt.nec.co.jp\".
562 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
563 -> \"m-sakura@ccs.mt.nec.co.jp\"."
564 (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
565 (wl-match-string 1 str))
566 ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
567 (wl-match-string 1 str))
570 (defsubst wl-address-header-extract-realname (str)
571 "Extracts a real name from STR and returns it.
572 e.g. \"Mr. bar <hoge@foo.com>\"
574 (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
575 (wl-match-string 1 str))
578 (defmacro wl-address-concat-token (string token)
580 ((eq 'quoted-string (car (, token)))
581 (concat (, string) "\"" (cdr (, token)) "\""))
582 ((eq 'comment (car (, token)))
583 (concat (, string) "(" (cdr (, token)) ")"))
585 (concat (, string) (cdr (, token)))))))
587 (defun wl-address-string-without-group-list-contents (sequence)
588 "Return address string from lexical analyzed list SEQUENCE.
589 Group list contents is not included."
590 (let (address-string route-addr-end token seq)
592 (setq token (car sequence))
594 ;; group = phrase ":" [#mailbox] ";"
595 ((and (eq 'specials (car token))
596 (string= (cdr token) ":"))
597 (setq address-string (concat address-string (cdr token))) ; ':'
598 (setq seq (cdr sequence))
599 (setq token (car seq))
600 (while (not (and (eq 'specials (car token))
601 (string= (cdr token) ";")))
602 (setq token (car seq))
603 (setq seq (cdr seq)))
604 (setq address-string (concat address-string (cdr token))) ; ';'
606 ;; route-addr = "<" [route] addr-spec ">"
607 ;; route = 1#("@" domain) ":" ; path-relative
608 ((and (eq 'specials (car token))
609 (string= (cdr token) "<"))
610 (setq seq (std11-parse-route-addr sequence))
611 (setq route-addr-end (car (cdr seq)))
612 (while (not (eq (car sequence) route-addr-end))
613 (setq address-string (wl-address-concat-token address-string
615 (setq sequence (cdr sequence))))
617 (setq address-string (wl-address-concat-token address-string token))
618 (setq sequence (cdr sequence)))))
621 (defun wl-address-petname-delete (the-email)
622 "Delete petname in wl-address-file."
623 (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
624 (output-coding-system
625 (mime-charset-to-coding-system wl-mime-charset)))
627 (message "Deleting Petname...")
629 (insert-file-contents wl-address-file)
630 (delete-matching-lines (concat "^[ \t]*" the-email))
631 (write-region (point-min) (point-max)
632 wl-address-file nil 'no-msg)
633 (message "Deleting Petname...done")
634 (kill-buffer tmp-buf)))
637 (defun wl-address-petname-add-or-change (the-email
640 &optional change-petname)
641 "Add petname to wl-address-file, if not registerd.
642 If already registerd, change it."
643 (let (the-realname the-petname)
645 ;; setup output "petname"
646 ;; if null petname'd, let default-petname be the petname.
648 (read-from-minibuffer (format "Petname: ") default-petname))
649 (if (string= the-petname "")
650 (setq the-petname (or default-petname the-email)))
652 ;; setup output "realname"
654 (read-from-minibuffer (format "Real Name: ") default-realname))
655 ;; (if (string= the-realname "")
656 ;; (setq the-realname default-petname))
658 ;; writing to ~/.address
659 (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
660 (output-coding-system (mime-charset-to-coding-system wl-mime-charset)))
662 (message "Adding Petname...")
664 (if (file-exists-p wl-address-file)
665 (insert-file-contents wl-address-file))
666 (if (not change-petname)
669 (goto-char (point-max))
670 (if (and (> (buffer-size) 0)
671 (not (eq (char-after (1- (point-max))) ?\n)))
674 (if (re-search-forward (concat "^[ \t]*" the-email) nil t)
675 (delete-region (save-excursion (beginning-of-line)
677 (save-excursion (end-of-line)
679 (insert (format "%s\t\"%s\"\t\"%s\"\n"
680 the-email the-petname the-realname))
681 (write-region (point-min) (point-max)
682 wl-address-file nil 'no-msg)
683 (message "Adding Petname...done")
684 (kill-buffer tmp-buf))))
687 (product-provide (provide 'wl-address) (require 'wl-version))
689 ;;; wl-address.el ends here