* wl-draft.el (wl-message-mail-p): Test resent-to: field.
[elisp/wanderlust.git] / wl / wl-address.el
index 1aca6b9..259ae2b 100644 (file)
@@ -1,8 +1,12 @@
 ;;; wl-address.el -- Tiny address management for Wanderlust.
 
-;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 1998,1999,2000 Shun-ichi GOTO <gotoh@taiyo.co.jp>
+;; Copyright (C) 1998,1999,2000 Takeshi Chiba <chiba@d3.bs1.fc.nec.co.jp>
 
 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
+;;     Shun-ichi GOTO <gotoh@taiyo.co.jp>
+;;     Takeshi Chiba <chiba@d3.bs1.fc.nec.co.jp>
 ;; Keywords: mail, net news
 
 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
@@ -30,6 +34,7 @@
 ;; 
 
 (require 'wl-util)
+(require 'wl-vars)
 (require 'std11)
 
 (defvar wl-address-complete-header-regexp "^\\(To\\|From\\|Cc\\|Bcc\\|Mail-Followup-To\\|Reply-To\\|Return-Receipt-To\\):")
 (defvar wl-address-completion-list nil)
 (defvar wl-address-petname-hash nil)
 
+(defvar wl-address-ldap-search-hash nil)
+
+(eval-when-compile (require 'pldap))
+
+(defvar wl-ldap-alias-dn-level nil
+"Level of dn data to make alias postfix.
+Valid value is nit, t, 1 or larget integer.
+
+If this value nil, minimum alias postfix is made depends on uniqness
+with other candidates.  In this implementation, it's same to 1.  If t,
+always append all dn data.  If number, always append spcified level of
+data but maybe appended more uniqness.  If invalid value, treat as
+nil.
+
+For example, following dn data is exsist, alias of each level is shown
+bellow.
+
+Match: Goto
+dn: CN=Shun-ichi GOTO,OU=Mew,OU=Emacs,OU=Lisper,O=Programmers Inc.
+  nil => Goto/Shun-ichi_GOTO
+    1 => Goto/Shun-ichi_GOTO
+    2 => Goto/Shun-ichi_GOTO/Mew
+    3 => Goto/Shun-ichi_GOTO/Mew/Emacs
+    4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
+    5 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
+    6 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
+    t => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
+
+If level 3 is required for uniqness with other candidates,
+  nil => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
+    1 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
+    2 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
+    3 => Goto/Shun-ichi_GOTO/Mew/Emacs
+    4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
+    (so on...)")
+
+(defconst wl-ldap-alias-sep "/")
+
+(defconst wl-ldap-search-attribute-type-list
+  '("sn" "cn" "mail"))
+
+(defun wl-ldap-get-value (type entry)
+  ""
+  (let* ((values (cdr (assoc type entry)))
+        (ret (car values)))
+    (if (and ret (not ldap-ignore-attribute-codings))
+       (while values
+         (if (not (string-match "^[\000-\177]*$" (car values)))
+             (setq ret (car values)
+                   values nil)
+           (setq values (cdr values)))))
+    ret))
+         
+(defun wl-ldap-get-value-list (type entry)
+  ""
+  (cdr (assoc type entry)))
+
+(defun wl-ldap-make-filter (pat type-list)
+  "Make RFC1558 quiery filter for PAT from ATTR-LIST.
+Each are \"OR\" combination, and PAT is beginning-match."
+  (concat "(&(objectclass=person)(|"
+         (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
+                    type-list
+                    "")
+         "))"))
+
+(defun wl-ldap-make-matched-value-list (regexp type-list entry)
+  "Correct matching WORD with value of TYPE-LIST in ENTRY.
+Returns matched uniq string list."
+  (let (type val values result)
+    ;; collect matching value
+    (while entry
+      (setq type (car (car entry))
+           values (mapcar (function wl-ldap-alias-safe-string)
+                          (cdr (car entry)))
+           values (elmo-flatten values)
+           entry (cdr entry))
+      (if (string-match "::?$" type)
+         (setq type (substring type 0 (match-beginning 0))))
+      (if (member type type-list)
+         (while values
+           (setq val (car values)
+                 values (cdr values))
+           (if (and (string-match regexp val)
+                    (not (member val result)))
+               (setq result (cons val result))))))
+    result))
+
+(defun wl-ldap-alias-safe-string (str)
+  "Modify STR for alias.
+Replace space/tab in STR into '_' char.
+Replace '@' in STR into list of mailbox and sub-domains."
+  (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
+    (setq str (concat (substring str 0 (match-beginning 0))
+                     "_"
+                     (substring str (match-end 0)))))
+  (if (string-match "\\(@\\)[^/@]+" str)
+      (setq str (split-string str  "[@\\.]")))
+  str)
+
+(defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
+  ""
+  (let (sym dnsym value level)
+    (setq dnsym (intern (upcase dn) hash))
+    (if (and (null str) (boundp dnsym))
+       ()                                      ; already processed
+      ;; make dn-list in fisrt time
+      (if (null dn-list)
+         (let ((case-fold-search t))
+           (setq dn-list (mapcar (lambda (str)
+                                   (if (string-match "[a-z]+=\\(.*\\)" str)
+                                       (wl-ldap-alias-safe-string
+                                        (wl-match-string 1 str))))
+                                 (split-string dn "[ \t]*,[ \t]*")))))
+      (setq dn-list (elmo-flatten dn-list))
+      ;; prepare candidate for uniq str
+      (if str
+         (setq str (concat str wl-ldap-alias-sep (car dn-list))
+               dn-list (cdr dn-list))
+       ;; first entry, pre-build with given level
+       (cond
+        ((null wl-ldap-alias-dn-level) (setq level 1))
+        ((eq t wl-ldap-alias-dn-level) (setq level 1000)) ; xxx, big enough
+        ((numberp wl-ldap-alias-dn-level)
+         (if (< 0 wl-ldap-alias-dn-level)
+             (setq level  wl-ldap-alias-dn-level)
+           (setq level 1)))
+        (t
+         (setq level 1)))
+       (while (and (< 0 level) dn-list)
+         (if (null str)
+             (setq str (car dn-list))
+           (setq str (concat str wl-ldap-alias-sep (car dn-list))))
+         (setq level (1- level)
+               dn-list (cdr dn-list))))
+      (setq sym (intern (upcase str) hash))
+      (if (not (boundp sym))
+         ;; good
+         (progn (set sym (list dn str dn-list))
+                (set dnsym str))
+       ;; conflict
+       (if (not (eq (setq value (symbol-value sym)) t))
+           ;; move away deeper
+           (progn (set sym t)
+                  (apply (function wl-ldap-register-dn-string) hash value)))
+       (wl-ldap-register-dn-string hash dn str dn-list)))))
+
+(defun wl-address-ldap-search (pattern cl)
+  "Make address completion-list matched for PATTERN by LDAP search.
+Matched address lists are append to CL."
+  (require 'pldap)
+  (unless wl-address-ldap-search-hash
+    (setq wl-address-ldap-search-hash (elmo-make-hash 7)))
+  (let ((pat (if (string-match wl-ldap-alias-sep pattern)
+                (substring pattern 0 (match-beginning 0))
+              pattern))
+       (ldap-default-host wl-ldap-server)
+       (ldap-default-port (or wl-ldap-port 389))
+       (ldap-default-base wl-ldap-base)
+       (dnhash (elmo-make-hash))
+       cache len sym tmpl regexp entries ent values dn dnstr alias
+       result cn mails)
+    ;; check cache
+    (mapatoms (lambda (atom)
+               (if (and (string-match
+                         (concat "^" (symbol-name atom) ".*") pat)
+                        (or (null cache)
+                            (< (car cache)
+                               (setq len (length (symbol-name atom))))))
+                   (setq cache (cons
+                                (or len (length (symbol-name atom)))
+                                (symbol-value atom)))))
+             wl-address-ldap-search-hash)
+    ;; get matched entries
+    (if cache
+       (setq entries (cdr cache))
+      (condition-case nil
+         (progn
+           (message "Searching in LDAP...")
+           (setq entries (ldap-search-entries
+                          (wl-ldap-make-filter
+                           (concat pat "*")
+                           wl-ldap-search-attribute-type-list)
+                          nil wl-ldap-search-attribute-type-list nil t))
+           (message "Searching in LDAP...done")
+           (elmo-set-hash-val pattern entries wl-address-ldap-search-hash))
+       (error (message ""))))                  ; ignore error: No such object
+    ;;
+    (setq tmpl entries)
+    (while tmpl
+      (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
+      (setq tmpl (cdr tmpl)))
+    ;;
+    (setq regexp (concat "^" pat))
+    (while entries
+      (setq ent (cdar entries)
+           values (wl-ldap-make-matched-value-list
+                   regexp '("mail" "sn" "cn") ent)
+           mails (wl-ldap-get-value-list "mail" ent)
+           cn (wl-ldap-get-value "cn" ent)
+           dn (car (car entries))
+           dnstr (elmo-get-hash-val (upcase dn) dnhash))
+      ;; make alias list generated from LDAP data.
+      (while (and mails values)
+       ;; make alias like MATCHED/DN-STRING
+       (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
+           (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
+         ;; use DN-STRING if DN-STRING begin with MATCHED
+         (setq alias dnstr))
+       ;; check uniqness then add to list
+       (setq sym (intern (downcase alias) dnhash))
+       (when (not (boundp sym))
+         (set sym alias)
+         (setq result (cons (cons alias
+                                  (concat cn " <" (car mails) ">"))
+                            result)))
+       (setq values (cdr values)))
+      ;; make mail addrses list
+      (while mails
+       (if (null (assoc (car mails) cl)); Not already in cl.
+           ;; (string-match regexp (car mails))
+           ;; add mail address itself to completion list
+           (setq result (cons (cons (car mails)
+                                    (concat cn " <" (car mails) ">"))
+                              result)))
+       (setq mails (cdr mails)))
+      (setq entries (cdr entries)))
+    (append result cl)))
+
 (defun wl-complete-field-to ()
   (interactive)
   (let ((cl wl-address-completion-list))
        (completing-read "To: " cl)
       (read-string "To: "))))
 
+(defconst wl-address-specials-regexp "[]\"(),.:;<>@[\\]")
+
+(defun wl-address-quote-specials (word)
+  "Make quoted string of WORD if needed."
+  (if (string-match wl-address-specials-regexp word)
+      (prin1-to-string word)
+    word))
+
+(defun wl-address-make-completion-list (address-list)
+  (let (addr-tuple cl)
+    (while address-list
+      (setq addr-tuple (car address-list))
+      (setq cl
+            (cons
+             (cons (nth 0 addr-tuple)
+                   (concat
+                    (wl-address-quote-specials
+                     (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
+             cl))
+      ;; nickname completion.
+      (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
+                 ;; already exists
+                 (assoc (nth 1 addr-tuple) cl))
+       (setq cl
+             (cons
+              (cons (nth 1 addr-tuple)
+                    (concat
+                     (wl-address-quote-specials
+                      (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
+              cl)))
+      (setq address-list (cdr address-list)))
+    cl))
+
 (defun wl-complete-field-body-or-tab ()
   (interactive)
   (let ((case-fold-search t)
        epand-char skip-chars
+       (use-ldap nil)
        completion-list)
     (if (wl-draft-on-field-p)
        (wl-complete-field)
                (point)))
           (save-excursion
             (beginning-of-line)
+            (setq use-ldap nil)
             (while (and (looking-at "^[ \t]")
                         (not (= (point) (point-min))))
               (forward-line -1))
             (cond ((looking-at wl-address-complete-header-regexp)
                    (setq completion-list wl-address-completion-list)
+                   (if wl-use-ldap
+                       (setq use-ldap t))
                    (setq epand-char ?@))
                   ((looking-at wl-folder-complete-header-regexp)
                    (setq completion-list wl-folder-entity-hashtb)
                   ((looking-at wl-newsgroups-complete-header-regexp)
                    (setq completion-list wl-folder-newsgroups-hashtb)))))
          (wl-complete-field-body completion-list
-                                 epand-char skip-chars)
+                                 epand-char skip-chars use-ldap)
        (indent-for-tab-command)))))
 
 (defvar wl-completion-buf-name "*Completions*")
                   (progn
                     (delete-region start end)
                     (insert (cdr alias))
-               ;     (wl-highlight-message (point-min)(point-max) t)
+;;;                 (wl-highlight-message (point-min)(point-max) t)
                     )))
             (wl-complete-window-delete))
            ((null completion)
              (if (setq comp-win (get-buffer-window comp-buf))
                  (delete-window comp-win)))))))
 
-(defun wl-complete-field-body (completion-list &optional epand-char skip-chars)
+(defun wl-complete-field-body (completion-list
+                              &optional epand-char skip-chars use-ldap)
   (interactive)
   (let* ((end (point))
         (start (save-excursion
-;                (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
-                 (skip-chars-backward (or skip-chars
-                                          "_a-zA-Z0-9+@%.!\\-/"))
+                 (skip-chars-backward (or skip-chars "^:,>\n"))
+                 (skip-chars-forward " \t")
                  (point)))
         (completion)
         (pattern (buffer-substring start end))
         (len (length pattern))
         (cl completion-list))
+    (when use-ldap
+      (setq cl (wl-address-ldap-search pattern cl)))
     (if (null cl)
        nil
       (setq completion (try-completion pattern cl))
       (cond ((eq completion t)
-            (wl-complete-insert start end pattern completion-list)
+            (if use-ldap (setq wl-address-ldap-search-hash nil))
+            (wl-complete-insert start end pattern cl)
             (wl-complete-window-delete)
             (message "Sole completion"))
            ((and epand-char
 (defvar wl-address-init-func 'wl-local-address-init)
 
 (defun wl-address-init ()
+  "Call `wl-address-init-func'."
   (funcall wl-address-init-func))
 
 (defun wl-local-address-init ()
+  "Reload `wl-address-file'.
+Refresh `wl-address-list', `wl-address-completion-list', and
+`wl-address-petname-hash'."
   (message "Updating addresses...")
   (setq wl-address-list
        (wl-address-make-address-list wl-address-file))
            (append wl-address-completion-list
                    (wl-address-make-alist-from-alias-file wl-alias-file))))
   (setq wl-address-petname-hash (elmo-make-hash))
-  (mapcar
-   (function
-       (lambda (x)
-         (elmo-set-hash-val (downcase (car x))
-                                                (cadr x)
-                                                wl-address-petname-hash)))
-   wl-address-list)
-  (message "Updating addresses...done."))
+  (let ((addresses wl-address-list))
+    (while addresses
+      (elmo-set-hash-val (downcase (car (car addresses)))
+                        (cadr (car addresses))
+                        wl-address-petname-hash)
+      (setq addresses (cdr addresses))))
+  (message "Updating addresses...done"))
 
 
 (defun wl-address-expand-aliases (alist nest-count)
            (forward-line))
          ret))))
 
-(defsubst wl-address-get-petname (str)
-  (let ((addr (downcase (wl-address-header-extract-address str))))
-    (or (elmo-get-hash-val addr wl-address-petname-hash)
-       str)))
+(defun wl-address-get-petname-1 (string)
+  (let ((address (downcase (wl-address-header-extract-address string))))
+    (elmo-get-hash-val address wl-address-petname-hash)))
 
-(defsubst wl-address-make-completion-list (address-list)
-  (mapcar '(lambda (entity)
-            (cons (nth 0 entity)
-                  (concat (nth 2 entity) " <"(nth 0 entity)">"))) address-list))
+(defsubst wl-address-get-petname (string)
+  (or (wl-address-get-petname-1 string)
+      string))
 
 (defsubst wl-address-user-mail-address-p (address)
   "Judge whether ADDRESS is user's or not."
                      wl-from))))))
 
 (defsubst wl-address-header-extract-address (str)
-  "Extracts a real e-mail address from STR and returns it.
+  "Extracts a real e-mail address from STR and return it.
 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
   ->  \"m-sakura@ccs.mt.nec.co.jp\".
 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
@@ -303,15 +578,60 @@ e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
         (t str)))
 
 (defsubst wl-address-header-extract-realname (str)
-  "Extracts a real name from STR and returns it.
+  "Extracts a real name from STR and return it.
 e.g. \"Mr. bar <hoge@foo.com>\"
   ->  \"Mr. bar\"."
   (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
          (wl-match-string 1 str))
         (t "")))
 
+(defmacro wl-address-concat-token (string token)
+  (` (cond
+      ((eq 'quoted-string (car (, token)))
+       (concat (, string) "\"" (cdr (, token)) "\""))
+      ((eq 'comment (car (, token)))
+       (concat (, string) "(" (cdr (, token)) ")"))
+      (t
+       (concat (, string) (cdr (, token)))))))
+
+(defun wl-address-string-without-group-list-contents (sequence)
+  "Return address string from lexical analyzed list SEQUENCE.
+Group list contents is not included."
+  (let (address-string route-addr-end token seq group-end)
+  (while sequence
+    (setq token (car sequence))
+    (cond
+     ;;   group       =  phrase ":" [#mailbox] ";"
+     ((and (eq 'specials (car token))
+          (string= (cdr token) ":"))
+      (setq address-string (concat address-string (cdr token))) ; ':'
+      (setq seq (cdr sequence))
+      (setq token (car seq))
+      (setq group-end nil)
+      (while (not group-end)
+       (setq token (car seq))
+       (setq seq (cdr seq))
+       (setq group-end (and (eq 'specials (car token))
+                            (string= (cdr token) ";"))))
+      (setq address-string (concat address-string (cdr token))) ; ';'
+      (setq sequence seq))
+     ;;   route-addr  =  "<" [route] addr-spec ">"
+     ;;   route       =  1#("@" domain) ":"           ; path-relative
+     ((and (eq 'specials (car token))
+          (string= (cdr token) "<"))
+      (setq seq (std11-parse-route-addr sequence))
+      (setq route-addr-end (car (cdr seq)))
+      (while (not (eq (car sequence) route-addr-end))
+       (setq address-string (wl-address-concat-token address-string
+                                                     (car sequence)))
+       (setq sequence (cdr sequence))))
+     (t
+      (setq address-string (wl-address-concat-token address-string token))
+      (setq sequence (cdr sequence)))))
+  address-string))
+
 (defun wl-address-petname-delete (the-email)
-  "Delete petname in wl-address-file."
+  "Delete petname in `wl-address-file'."
   (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
          (output-coding-system
           (mime-charset-to-coding-system wl-mime-charset)))
@@ -330,7 +650,7 @@ e.g. \"Mr. bar <hoge@foo.com>\"
                                         default-petname
                                         default-realname
                                         &optional change-petname)
-  "Add petname to wl-address-file, if not registerd.
+  "Add petname to `wl-address-file', if not registerd.
 If already registerd, change it."
   (let (the-realname the-petname)
 
@@ -344,8 +664,8 @@ If already registerd, change it."
     ;; setup output "realname"
     (setq the-realname
        (read-from-minibuffer (format "Real Name: ") default-realname))
-;;     (if (string= the-realname "")
-;;         (setq the-realname default-petname))
+;;;    (if (string= the-realname "")
+;;;        (setq the-realname default-petname))
 
     ;; writing to ~/.address
     (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
@@ -375,6 +695,8 @@ If already registerd, change it."
       (message "Adding Petname...done")
       (kill-buffer tmp-buf))))
 
-(provide 'wl-address)
+(require 'product)
+(product-provide (provide 'wl-address) (require 'wl-version))
 
 ;;; wl-address.el ends here
+