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=person)(|"
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 (defun wl-address-quote-specials (word)
284 "Make quoted string of WORD if needed."
285 (if (assq 'specials (std11-lexical-analyze word))
286 (prin1-to-string word)
289 (defun wl-address-make-completion-list (address-list)
292 (setq addr-tuple (car address-list))
295 (cons (nth 0 addr-tuple)
297 (wl-address-quote-specials
298 (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
300 ;; nickname completion.
301 (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
303 (assoc (nth 1 addr-tuple) cl))
306 (cons (nth 1 addr-tuple)
308 (wl-address-quote-specials
309 (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
311 (setq address-list (cdr address-list)))
314 (defun wl-complete-field-body-or-tab ()
316 (let ((case-fold-search t)
317 epand-char skip-chars
320 (if (wl-draft-on-field-p)
325 (goto-char (point-min))
326 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
331 (while (and (looking-at "^[ \t]")
332 (not (= (point) (point-min))))
334 (cond ((looking-at wl-address-complete-header-regexp)
335 (setq completion-list wl-address-completion-list)
338 (setq epand-char ?@))
339 ((looking-at wl-folder-complete-header-regexp)
340 (setq completion-list wl-folder-entity-hashtb)
341 (setq skip-chars "^, "))
342 ((looking-at wl-newsgroups-complete-header-regexp)
343 (setq completion-list wl-folder-newsgroups-hashtb)))))
344 (wl-complete-field-body completion-list
345 epand-char skip-chars use-ldap)
346 (indent-for-tab-command)))))
348 (defvar wl-completion-buf-name "*Completions*")
350 (defvar wl-complete-candidates nil)
352 (defun wl-complete-window-show (all)
353 (if (and (get-buffer-window wl-completion-buf-name)
354 (equal wl-complete-candidates all))
355 (let ((win (get-buffer-window wl-completion-buf-name)))
357 (set-buffer wl-completion-buf-name)
358 (if (pos-visible-in-window-p (point-max) win)
359 (set-window-start win 1)
360 (scroll-other-window))))
361 (message "Making completion list...")
362 (setq wl-complete-candidates all)
363 (with-output-to-temp-buffer
364 wl-completion-buf-name
365 (display-completion-list all))
366 (message "Making completion list... done")))
368 (defun wl-complete-window-delete ()
369 (let (comp-buf comp-win)
370 (if (setq comp-buf (get-buffer wl-completion-buf-name))
371 (if (setq comp-win (get-buffer-window comp-buf))
372 (delete-window comp-win)))))
374 (defun wl-complete-field ()
377 (start (save-excursion
378 (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
381 (pattern (buffer-substring start end))
382 (cl wl-draft-field-completion-list))
386 (let ((completion-ignore-case t))
387 (try-completion pattern cl)))
388 (cond ((eq completion t)
389 (let ((alias (assoc pattern cl)))
392 (delete-region start end)
394 ;;; (wl-highlight-message (point-min)(point-max) t)
396 (wl-complete-window-delete))
398 (message "Can't find completion for \"%s\"" pattern)
400 ((not (string= pattern completion))
401 (delete-region start end)
403 (wl-complete-window-delete)
404 (wl-highlight-message (point-min)(point-max) t))
406 (let ((list (all-completions pattern cl)))
407 (wl-complete-window-show list)))))))
409 (defun wl-complete-insert (start end pattern completion-list)
410 (let ((alias (and (consp completion-list)
411 (assoc pattern completion-list)))
415 (delete-region start end)
417 (if (setq comp-buf (get-buffer wl-completion-buf-name))
418 (if (setq comp-win (get-buffer-window comp-buf))
419 (delete-window comp-win)))))))
421 (defun wl-complete-field-body (completion-list
422 &optional epand-char skip-chars use-ldap)
425 (start (save-excursion
426 (skip-chars-backward (or skip-chars "^:,>\n"))
427 (skip-chars-forward " \t")
430 (pattern (buffer-substring start end))
431 (len (length pattern))
432 (cl completion-list))
434 (setq cl (wl-address-ldap-search pattern cl)))
437 (setq completion (try-completion pattern cl))
438 (cond ((eq completion t)
439 (if use-ldap (setq wl-address-ldap-search-hash nil))
440 (wl-complete-insert start end pattern cl)
441 (wl-complete-window-delete)
442 (message "Sole completion"))
445 (char-equal (aref pattern (1- len)) epand-char)
446 (assoc (substring pattern 0 (1- len)) cl))
449 (substring pattern 0 (1- len))
452 (message "Can't find completion for \"%s\"" pattern)
454 ((not (string= pattern completion))
455 (delete-region start end)
458 (let ((list (sort (all-completions pattern cl) 'string<)))
459 (wl-complete-window-show list)))))))
461 (defvar wl-address-init-func 'wl-local-address-init)
463 (defun wl-address-init ()
464 "Call `wl-address-init-func'."
465 (funcall wl-address-init-func))
467 (defun wl-local-address-init ()
468 "Reload `wl-address-file'.
469 Refresh `wl-address-list', `wl-address-completion-list', and
470 `wl-address-petname-hash'."
471 (message "Updating addresses...")
472 (setq wl-address-list
473 (wl-address-make-address-list wl-address-file))
474 (setq wl-address-completion-list
475 (wl-address-make-completion-list wl-address-list))
476 (if (file-readable-p wl-alias-file)
477 (setq wl-address-completion-list
478 (append wl-address-completion-list
479 (wl-address-make-alist-from-alias-file wl-alias-file))))
480 (setq wl-address-petname-hash (elmo-make-hash))
481 (let ((addresses wl-address-list))
483 (elmo-set-hash-val (downcase (car (car addresses)))
484 (cadr (car addresses))
485 wl-address-petname-hash)
486 (setq addresses (cdr addresses))))
487 (message "Updating addresses...done"))
490 (defun wl-address-expand-aliases (alist nest-count)
491 (when (< nest-count 5)
492 (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
493 (while (setq expn-str (cdr (nth n alist)))
494 (setq new-expn-str nil)
495 (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
496 (setq expn (elmo-match-string 1 expn-str))
497 (setq expn-str (wl-string-delete-match expn-str 0))
498 (if (string-match "^[ \t,]+" expn-str)
499 (setq expn-str (wl-string-delete-match expn-str 0)))
500 (if (string-match "[ \t,]+$" expn)
501 (setq expn (wl-string-delete-match expn 0)))
502 (setq new-expn (cdr (assoc expn alist)))
505 (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
506 (or new-expn expn))))
508 (setcdr (nth n alist) new-expn-str))
511 (wl-address-expand-aliases alist (1+ nest-count))))))
513 (defun wl-address-make-alist-from-alias-file (file)
515 (let ((case-fold-search t)
517 (insert-file-contents file)
518 (while (re-search-forward ",$" nil t)
521 (delete-backward-char 1))
522 (goto-char (point-min))
523 (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
524 (setq alias (wl-match-buffer 1)
525 expn (wl-match-buffer 2))
526 (setq alist (cons (cons alias expn) alist)))
527 (wl-address-expand-aliases alist 0)
528 (nreverse alist) ; return value
531 (defun wl-address-make-address-list (path)
532 (if (and path (file-readable-p path))
535 (coding-system-for-read wl-cs-autoconv))
536 (insert-file-contents path)
537 (goto-char (point-min))
540 "^\\([^#\n][^ \t\n]+\\)[ \t]+\"\\(.*\\)\"[ \t]+\"\\(.*\\)\"[ \t]*.*$")
544 (list (wl-match-buffer 1)
546 (wl-match-buffer 3)))))
550 (defsubst wl-address-get-petname-1 (string)
551 (let ((address (downcase (wl-address-header-extract-address string))))
552 (elmo-get-hash-val address wl-address-petname-hash)))
554 (defsubst wl-address-get-petname (string)
555 (or (wl-address-get-petname-1 string)
558 (defsubst wl-address-user-mail-address-p (address)
559 "Judge whether ADDRESS is user's or not."
560 (member (downcase (wl-address-header-extract-address address))
561 (or (mapcar 'downcase wl-user-mail-address-list)
563 (wl-address-header-extract-address
566 (defsubst wl-address-header-extract-address (str)
567 "Extracts a real e-mail address from STR and return it.
568 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
569 -> \"m-sakura@ccs.mt.nec.co.jp\".
570 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
571 -> \"m-sakura@ccs.mt.nec.co.jp\"."
572 (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
573 (wl-match-string 1 str))
574 ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
575 (wl-match-string 1 str))
578 (defsubst wl-address-header-extract-realname (str)
579 "Extracts a real name from STR and return it.
580 e.g. \"Mr. bar <hoge@foo.com>\"
582 (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
583 (wl-match-string 1 str))
586 (defmacro wl-address-concat-token (string token)
588 ((eq 'quoted-string (car (, token)))
589 (concat (, string) "\"" (cdr (, token)) "\""))
590 ((eq 'comment (car (, token)))
591 (concat (, string) "(" (cdr (, token)) ")"))
593 (concat (, string) (cdr (, token)))))))
595 (defun wl-address-string-without-group-list-contents (sequence)
596 "Return address string from lexical analyzed list SEQUENCE.
597 Group list contents is not included."
598 (let (address-string route-addr-end token seq group-end)
600 (setq token (car sequence))
602 ;; group = phrase ":" [#mailbox] ";"
603 ((and (eq 'specials (car token))
604 (string= (cdr token) ":"))
605 (setq address-string (concat address-string (cdr token))) ; ':'
606 (setq seq (cdr sequence))
607 (setq token (car seq))
609 (while (not group-end)
610 (setq token (car seq))
612 (setq group-end (and (eq 'specials (car token))
613 (string= (cdr token) ";"))))
614 (setq address-string (concat address-string (cdr token))) ; ';'
616 ;; route-addr = "<" [route] addr-spec ">"
617 ;; route = 1#("@" domain) ":" ; path-relative
618 ((and (eq 'specials (car token))
619 (string= (cdr token) "<"))
620 (setq seq (std11-parse-route-addr sequence))
621 (setq route-addr-end (car (cdr seq)))
622 (while (not (eq (car sequence) route-addr-end))
623 (setq address-string (wl-address-concat-token address-string
625 (setq sequence (cdr sequence))))
627 (setq address-string (wl-address-concat-token address-string token))
628 (setq sequence (cdr sequence)))))
631 (defun wl-address-petname-delete (the-email)
632 "Delete petname in `wl-address-file'."
633 (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
634 (output-coding-system
635 (mime-charset-to-coding-system wl-mime-charset)))
637 (message "Deleting Petname...")
639 (insert-file-contents wl-address-file)
640 (delete-matching-lines (concat "^[ \t]*" the-email))
641 (write-region (point-min) (point-max)
642 wl-address-file nil 'no-msg)
643 (message "Deleting Petname...done")
644 (kill-buffer tmp-buf)))
647 (defun wl-address-petname-add-or-change (the-email
650 &optional change-petname)
651 "Add petname to `wl-address-file', if not registerd.
652 If already registerd, change it."
653 (let (the-realname the-petname)
655 ;; setup output "petname"
656 ;; if null petname'd, let default-petname be the petname.
658 (read-from-minibuffer (format "Petname: ") default-petname))
659 (if (string= the-petname "")
660 (setq the-petname (or default-petname the-email)))
662 ;; setup output "realname"
664 (read-from-minibuffer (format "Real Name: ") default-realname))
665 ;;; (if (string= the-realname "")
666 ;;; (setq the-realname default-petname))
668 ;; writing to ~/.address
669 (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
670 (output-coding-system (mime-charset-to-coding-system wl-mime-charset)))
672 (message "Adding Petname...")
674 (if (file-exists-p wl-address-file)
675 (insert-file-contents wl-address-file))
676 (if (not change-petname)
679 (goto-char (point-max))
680 (if (and (> (buffer-size) 0)
681 (not (eq (char-after (1- (point-max))) ?\n)))
684 (if (re-search-forward (concat "^[ \t]*" the-email) nil t)
685 (delete-region (save-excursion (beginning-of-line)
687 (save-excursion (end-of-line)
689 (insert (format "%s\t\"%s\"\t\"%s\"\n"
690 the-email the-petname the-realname))
691 (write-region (point-min) (point-max)
692 wl-address-file nil 'no-msg)
693 (message "Adding Petname...done")
694 (kill-buffer tmp-buf))))
697 (product-provide (provide 'wl-address) (require 'wl-version))
699 ;;; wl-address.el ends here