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
79 (defconst wl-ldap-alias-sep "/")
81 (defconst wl-ldap-search-attribute-type-list
84 (defun wl-ldap-get-value (type entry)
86 (let* ((values (cdr (assoc type entry)))
88 (if (and ret (not ldap-ignore-attribute-codings))
90 (if (not (string-match "^[\000-\177]*$" (car values)))
91 (setq ret (car values)
93 (setq values (cdr values)))))
96 (defun wl-ldap-get-value-list (type entry)
98 (cdr (assoc type entry)))
100 (defun wl-ldap-make-filter (pat type-list)
101 "Make RFC1558 quiery filter for PAT from ATTR-LIST.
102 Each are \"OR\" combination, and PAT is beginning-match."
103 (concat "(&(objectclass=person)(|"
104 (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
109 (defun wl-ldap-make-matched-value-list (regexp type-list entry)
110 "Correct matching WORD with value of TYPE-LIST in ENTRY.
111 Returns matched uniq string list."
112 (let (type val values result)
113 ;; collect matching value
115 (setq type (car (car entry))
116 values (mapcar (function wl-ldap-alias-safe-string)
119 (if (string-match "::?$" type)
120 (setq type (substring type 0 (match-beginning 0))))
121 (if (member type type-list)
123 (setq val (car values)
125 (if (and (string-match regexp val)
126 (not (member val result)))
127 (setq result (cons val result))))))
130 (defun wl-ldap-alias-safe-string (str)
131 "Modify STR for alias.
132 Replace space/tab in STR into '_' char.
133 And remove domain part of mail addr."
134 (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
135 (setq str (concat (substring str 0 (match-beginning 0))
137 (substring str (match-end 0)))))
138 (if (string-match "@[^/@]+" str)
139 (setq str (concat (substring str 0 (match-beginning 0))
140 (substring str (match-end 0)))))
143 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
145 (let (sym dnsym value level)
146 (setq dnsym (intern (upcase dn) hash))
147 (if (and (null str) (boundp dnsym))
148 () ; already processed
149 ;; make dn-list in fisrt time
151 (let ((case-fold-search t))
152 (setq dn-list (mapcar (lambda (str)
153 (if (string-match "[a-z]+=\\(.*\\)" str)
154 (wl-ldap-alias-safe-string
155 (wl-match-string 1 str))))
156 (split-string dn ",")))))
157 ;; prepare candidate for uniq str
159 (setq str (concat str wl-ldap-alias-sep (car dn-list))
160 dn-list (cdr dn-list))
161 ;; first entry, pre-build with given level
163 ((null wl-ldap-alias-dn-level) (setq level 1))
164 ((eq t wl-ldap-alias-dn-level) (setq level 1000)) ; xxx, big enough
165 ((numberp wl-ldap-alias-dn-level)
166 (if (< 0 wl-ldap-alias-dn-level)
167 (setq level wl-ldap-alias-dn-level)
171 (while (and (< 0 level) dn-list)
173 (setq str (car dn-list))
174 (setq str (concat str wl-ldap-alias-sep (car dn-list))))
175 (setq level (1- level)
176 dn-list (cdr dn-list))))
177 (setq sym (intern (upcase str) hash))
178 (if (not (boundp sym))
180 (progn (set sym (list dn str dn-list))
183 (if (not (eq (setq value (symbol-value sym)) t))
186 (apply (function wl-ldap-register-dn-string) hash value)))
187 (wl-ldap-register-dn-string hash dn str dn-list)))))
189 (defun wl-address-ldap-search (pattern cl)
190 "Make address completion-list matched for PATTERN by LDAP search.
191 Matched address lists are append to CL."
193 (unless wl-address-ldap-search-hash
194 (setq wl-address-ldap-search-hash (elmo-make-hash 7)))
195 (let ((pat (if (string-match wl-ldap-alias-sep pattern)
196 (substring pattern 0 (match-beginning 0))
198 (ldap-default-host wl-ldap-server)
199 (ldap-default-port (or wl-ldap-port 389))
200 (ldap-default-base wl-ldap-base)
201 (dnhash (elmo-make-hash))
202 cache len sym tmpl regexp entries ent values dn dnstr alias
205 (mapatoms (lambda (atom)
206 (if (and (string-match
207 (concat "^" (symbol-name atom) ".*") pat)
210 (setq len (length (symbol-name atom))))))
212 (or len (length (symbol-name atom)))
213 (symbol-value atom)))))
214 wl-address-ldap-search-hash)
215 ;; get matched entries
217 (setq entries (cdr cache))
220 (message "Searching in LDAP...")
221 (setq entries (ldap-search-entries
224 wl-ldap-search-attribute-type-list)
225 nil wl-ldap-search-attribute-type-list nil t))
226 (message "Searching in LDAP...done")
227 (elmo-set-hash-val pattern entries wl-address-ldap-search-hash))
228 (error (message "")))) ; ignore error: No such object
232 (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
233 (setq tmpl (cdr tmpl)))
235 (setq regexp (concat "^" pat))
237 (setq ent (cdar entries)
238 values (wl-ldap-make-matched-value-list
239 regexp '("mail" "sn" "cn") ent)
240 mails (wl-ldap-get-value-list "mail" ent)
241 cn (wl-ldap-get-value "cn" ent)
242 dn (car (car entries))
243 dnstr (elmo-get-hash-val (upcase dn) dnhash))
244 ;; make alias list generated from LDAP data.
245 (while (and mails values)
246 ;; make alias like MATCHED/DN-STRING
247 (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
248 (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
249 ;; use DN-STRING if DN-STRING begin with MATCHED
251 ;; check uniqness then add to list
252 (setq sym (intern (downcase alias) dnhash))
253 (when (not (boundp sym))
255 (setq result (cons (cons alias
256 (concat cn " <" (car mails) ">"))
258 (setq values (cdr values)))
259 ;; make mail addrses list
261 (if (null (assoc (car mails) cl)); Not already in cl.
262 ;; (string-match regexp (car mails))
263 ;; add mail address itself to completion list
264 (setq result (cons (cons (car mails)
265 (concat cn " <" (car mails) ">"))
267 (setq mails (cdr mails)))
268 (setq entries (cdr entries)))
271 (defun wl-complete-field-to ()
273 (let ((cl wl-address-completion-list))
275 (completing-read "To: " cl)
276 (read-string "To: "))))
278 (defun wl-address-quote-specials (word)
279 "Make quoted string of WORD if needed."
280 (if (assq 'specials (std11-lexical-analyze word))
281 (prin1-to-string word)
284 (defun wl-address-make-completion-list (address-list)
287 (setq addr-tuple (car address-list))
290 (cons (nth 0 addr-tuple)
292 (wl-address-quote-specials
293 (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
295 ;; nickname completion.
296 (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
298 (assoc (nth 1 addr-tuple) cl))
301 (cons (nth 1 addr-tuple)
303 (wl-address-quote-specials
304 (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
306 (setq address-list (cdr address-list)))
309 (defun wl-complete-field-body-or-tab ()
311 (let ((case-fold-search t)
312 epand-char skip-chars
315 (if (wl-draft-on-field-p)
320 (goto-char (point-min))
321 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
326 (while (and (looking-at "^[ \t]")
327 (not (= (point) (point-min))))
329 (cond ((looking-at wl-address-complete-header-regexp)
330 (setq completion-list wl-address-completion-list)
333 (setq epand-char ?@))
334 ((looking-at wl-folder-complete-header-regexp)
335 (setq completion-list wl-folder-entity-hashtb)
336 (setq skip-chars "^, "))
337 ((looking-at wl-newsgroups-complete-header-regexp)
338 (setq completion-list wl-folder-newsgroups-hashtb)))))
339 (wl-complete-field-body completion-list
340 epand-char skip-chars use-ldap)
341 (indent-for-tab-command)))))
343 (defvar wl-completion-buf-name "*Completions*")
345 (defvar wl-complete-candidates nil)
347 (defun wl-complete-window-show (all)
348 (if (and (get-buffer-window wl-completion-buf-name)
349 (equal wl-complete-candidates all))
350 (let ((win (get-buffer-window wl-completion-buf-name)))
352 (set-buffer wl-completion-buf-name)
353 (if (pos-visible-in-window-p (point-max) win)
354 (set-window-start win 1)
355 (scroll-other-window))))
356 (message "Making completion list...")
357 (setq wl-complete-candidates all)
358 (with-output-to-temp-buffer
359 wl-completion-buf-name
360 (display-completion-list all))
361 (message "Making completion list... done")))
363 (defun wl-complete-window-delete ()
364 (let (comp-buf comp-win)
365 (if (setq comp-buf (get-buffer wl-completion-buf-name))
366 (if (setq comp-win (get-buffer-window comp-buf))
367 (delete-window comp-win)))))
369 (defun wl-complete-field ()
372 (start (save-excursion
373 (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
376 (pattern (buffer-substring start end))
377 (cl wl-draft-field-completion-list))
381 (let ((completion-ignore-case t))
382 (try-completion pattern cl)))
383 (cond ((eq completion t)
384 (let ((alias (assoc pattern cl)))
387 (delete-region start end)
389 ;;; (wl-highlight-message (point-min)(point-max) t)
391 (wl-complete-window-delete))
393 (message "Can't find completion for \"%s\"" pattern)
395 ((not (string= pattern completion))
396 (delete-region start end)
398 (wl-complete-window-delete)
399 (wl-highlight-message (point-min)(point-max) t))
401 (let ((list (all-completions pattern cl)))
402 (wl-complete-window-show list)))))))
404 (defun wl-complete-insert (start end pattern completion-list)
405 (let ((alias (and (consp completion-list)
406 (assoc pattern completion-list)))
410 (delete-region start end)
412 (if (setq comp-buf (get-buffer wl-completion-buf-name))
413 (if (setq comp-win (get-buffer-window comp-buf))
414 (delete-window comp-win)))))))
416 (defun wl-complete-field-body (completion-list
417 &optional epand-char skip-chars use-ldap)
420 (start (save-excursion
421 (skip-chars-backward (or skip-chars "^:,>\n"))
422 (skip-chars-forward " \t")
425 (pattern (buffer-substring start end))
426 (len (length pattern))
427 (cl completion-list))
429 (setq cl (wl-address-ldap-search pattern cl)))
432 (setq completion (try-completion pattern cl))
433 (cond ((eq completion t)
434 (if use-ldap (setq wl-address-ldap-search-hash nil))
435 (wl-complete-insert start end pattern cl)
436 (wl-complete-window-delete)
437 (message "Sole completion"))
440 (char-equal (aref pattern (1- len)) epand-char)
441 (assoc (substring pattern 0 (1- len)) cl))
444 (substring pattern 0 (1- len))
447 (message "Can't find completion for \"%s\"" pattern)
449 ((not (string= pattern completion))
450 (delete-region start end)
453 (let ((list (sort (all-completions pattern cl) 'string<)))
454 (wl-complete-window-show list)))))))
456 (defvar wl-address-init-func 'wl-local-address-init)
458 (defun wl-address-init ()
459 "Call `wl-address-init-func'."
460 (funcall wl-address-init-func))
462 (defun wl-local-address-init ()
463 "Reload `wl-address-file'.
464 Refresh `wl-address-list', `wl-address-completion-list', and
465 `wl-address-petname-hash'."
466 (message "Updating addresses...")
467 (setq wl-address-list
468 (wl-address-make-address-list wl-address-file))
469 (setq wl-address-completion-list
470 (wl-address-make-completion-list wl-address-list))
471 (if (file-readable-p wl-alias-file)
472 (setq wl-address-completion-list
473 (append wl-address-completion-list
474 (wl-address-make-alist-from-alias-file wl-alias-file))))
475 (setq wl-address-petname-hash (elmo-make-hash))
476 (let ((addresses wl-address-list))
478 (elmo-set-hash-val (downcase (car (car addresses)))
479 (cadr (car addresses))
480 wl-address-petname-hash)
481 (setq addresses (cdr addresses))))
482 (message "Updating addresses...done"))
485 (defun wl-address-expand-aliases (alist nest-count)
486 (when (< nest-count 5)
487 (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
488 (while (setq expn-str (cdr (nth n alist)))
489 (setq new-expn-str nil)
490 (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
491 (setq expn (elmo-match-string 1 expn-str))
492 (setq expn-str (wl-string-delete-match expn-str 0))
493 (if (string-match "^[ \t,]+" expn-str)
494 (setq expn-str (wl-string-delete-match expn-str 0)))
495 (if (string-match "[ \t,]+$" expn)
496 (setq expn (wl-string-delete-match expn 0)))
497 (setq new-expn (cdr (assoc expn alist)))
500 (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
501 (or new-expn expn))))
503 (setcdr (nth n alist) new-expn-str))
506 (wl-address-expand-aliases alist (1+ nest-count))))))
508 (defun wl-address-make-alist-from-alias-file (file)
510 (let ((case-fold-search t)
512 (insert-file-contents file)
513 (while (re-search-forward ",$" nil t)
516 (delete-backward-char 1))
517 (goto-char (point-min))
518 (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
519 (setq alias (wl-match-buffer 1)
520 expn (wl-match-buffer 2))
521 (setq alist (cons (cons alias expn) alist)))
522 (wl-address-expand-aliases alist 0)
523 (nreverse alist) ; return value
526 (defun wl-address-make-address-list (path)
527 (if (and path (file-readable-p path))
530 (coding-system-for-read wl-cs-autoconv))
531 (insert-file-contents path)
532 (goto-char (point-min))
535 "^\\([^#\n][^ \t\n]+\\)[ \t]+\"\\(.*\\)\"[ \t]+\"\\(.*\\)\"[ \t]*.*$")
539 (list (wl-match-buffer 1)
541 (wl-match-buffer 3)))))
545 (defsubst wl-address-get-petname-1 (string)
546 (let ((address (downcase (wl-address-header-extract-address string))))
547 (elmo-get-hash-val address wl-address-petname-hash)))
549 (defsubst wl-address-get-petname (string)
550 (or (wl-address-get-petname-1 string)
553 (defsubst wl-address-user-mail-address-p (address)
554 "Judge whether ADDRESS is user's or not."
555 (member (downcase (wl-address-header-extract-address address))
556 (or (mapcar 'downcase wl-user-mail-address-list)
558 (wl-address-header-extract-address
561 (defsubst wl-address-header-extract-address (str)
562 "Extracts a real e-mail address from STR and return it.
563 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
564 -> \"m-sakura@ccs.mt.nec.co.jp\".
565 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
566 -> \"m-sakura@ccs.mt.nec.co.jp\"."
567 (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
568 (wl-match-string 1 str))
569 ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
570 (wl-match-string 1 str))
573 (defsubst wl-address-header-extract-realname (str)
574 "Extracts a real name from STR and return it.
575 e.g. \"Mr. bar <hoge@foo.com>\"
577 (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
578 (wl-match-string 1 str))
581 (defmacro wl-address-concat-token (string token)
583 ((eq 'quoted-string (car (, token)))
584 (concat (, string) "\"" (cdr (, token)) "\""))
585 ((eq 'comment (car (, token)))
586 (concat (, string) "(" (cdr (, token)) ")"))
588 (concat (, string) (cdr (, token)))))))
590 (defun wl-address-string-without-group-list-contents (sequence)
591 "Return address string from lexical analyzed list SEQUENCE.
592 Group list contents is not included."
593 (let (address-string route-addr-end token seq group-end)
595 (setq token (car sequence))
597 ;; group = phrase ":" [#mailbox] ";"
598 ((and (eq 'specials (car token))
599 (string= (cdr token) ":"))
600 (setq address-string (concat address-string (cdr token))) ; ':'
601 (setq seq (cdr sequence))
602 (setq token (car seq))
604 (while (not group-end)
605 (setq token (car seq))
607 (setq group-end (and (eq 'specials (car token))
608 (string= (cdr token) ";"))))
609 (setq address-string (concat address-string (cdr token))) ; ';'
611 ;; route-addr = "<" [route] addr-spec ">"
612 ;; route = 1#("@" domain) ":" ; path-relative
613 ((and (eq 'specials (car token))
614 (string= (cdr token) "<"))
615 (setq seq (std11-parse-route-addr sequence))
616 (setq route-addr-end (car (cdr seq)))
617 (while (not (eq (car sequence) route-addr-end))
618 (setq address-string (wl-address-concat-token address-string
620 (setq sequence (cdr sequence))))
622 (setq address-string (wl-address-concat-token address-string token))
623 (setq sequence (cdr sequence)))))
626 (defun wl-address-petname-delete (the-email)
627 "Delete petname in `wl-address-file'."
628 (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
629 (output-coding-system
630 (mime-charset-to-coding-system wl-mime-charset)))
632 (message "Deleting Petname...")
634 (insert-file-contents wl-address-file)
635 (delete-matching-lines (concat "^[ \t]*" the-email))
636 (write-region (point-min) (point-max)
637 wl-address-file nil 'no-msg)
638 (message "Deleting Petname...done")
639 (kill-buffer tmp-buf)))
642 (defun wl-address-petname-add-or-change (the-email
645 &optional change-petname)
646 "Add petname to `wl-address-file', if not registerd.
647 If already registerd, change it."
648 (let (the-realname the-petname)
650 ;; setup output "petname"
651 ;; if null petname'd, let default-petname be the petname.
653 (read-from-minibuffer (format "Petname: ") default-petname))
654 (if (string= the-petname "")
655 (setq the-petname (or default-petname the-email)))
657 ;; setup output "realname"
659 (read-from-minibuffer (format "Real Name: ") default-realname))
660 ;;; (if (string= the-realname "")
661 ;;; (setq the-realname default-petname))
663 ;; writing to ~/.address
664 (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
665 (output-coding-system (mime-charset-to-coding-system wl-mime-charset)))
667 (message "Adding Petname...")
669 (if (file-exists-p wl-address-file)
670 (insert-file-contents wl-address-file))
671 (if (not change-petname)
674 (goto-char (point-max))
675 (if (and (> (buffer-size) 0)
676 (not (eq (char-after (1- (point-max))) ?\n)))
679 (if (re-search-forward (concat "^[ \t]*" the-email) nil t)
680 (delete-region (save-excursion (beginning-of-line)
682 (save-excursion (end-of-line)
684 (insert (format "%s\t\"%s\"\t\"%s\"\n"
685 the-email the-petname the-realname))
686 (write-region (point-min) (point-max)
687 wl-address-file nil 'no-msg)
688 (message "Adding Petname...done")
689 (kill-buffer tmp-buf))))
692 (product-provide (provide 'wl-address) (require 'wl-version))
694 ;;; wl-address.el ends here