tm 7.80.
[elisp/tm.git] / tm-bbdb.el
1 ;;; tm-bbdb.el --- tm shared module for BBDB
2
3 ;; Copyright (C) 1995,1996 KOBAYASHI Shuhei
4 ;; Copyright (C) 1996 Artur Pioro
5
6 ;; Author: KOBAYASHI Shuhei <shuhei-k@jaist.ac.jp>
7 ;;         Artur Pioro <artur@flugor.if.uj.edu.pl>
8 ;; Maintainer: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;; Version: $Id: tm-bbdb.el,v 7.13 1996/08/30 17:09:46 morioka Exp $
10 ;; Keywords: mail, news, MIME, multimedia, multilingual, BBDB
11
12 ;; This file is part of tm (Tools for MIME).
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; see the file COPYING.  If not, write to
26 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Code:
30
31 (require 'bbdb)
32 (require 'bbdb-com)
33 (require 'tl-822)
34 (require 'tm-ew-d)
35 (require 'tm-view)
36
37
38 ;;; @ mail-extr
39 ;;;
40
41 (defvar tm-bbdb/use-mail-extr t)
42
43 (defun tm-bbdb/extract-address-components (str)
44   (let* ((ret     (rfc822/extract-address-components str))
45          (phrase  (car ret))
46          (address (car (cdr ret)))
47          (methods tm-bbdb/canonicalize-full-name-methods))
48     (while (and phrase methods)
49       (setq phrase  (funcall (car methods) phrase)
50             methods (cdr methods)))
51     (if (string= address "") (setq address nil))
52     (if (string= phrase "") (setq phrase nil))
53     (list phrase address)
54     ))
55
56 (or tm-bbdb/use-mail-extr
57     (progn
58       (require 'mail-extr) ; for `what-domain'
59       (fset 'mail-extract-address-components
60             (symbol-function 'tm-bbdb/extract-address-components))
61       ))
62
63
64 ;;; @ bbdb-extract-field-value
65 ;;;
66
67 (or (fboundp 'tm:bbdb-extract-field-value)
68     (progn
69       ;; (require 'bbdb-hooks) ; not provided.
70       ;; (or (fboundp 'bbdb-extract-field-value) ; defined as autoload
71       (or (fboundp 'bbdb-header-start)
72           (load "bbdb-hooks"))
73       (fset 'tm:bbdb-extract-field-value
74             (symbol-function 'bbdb-extract-field-value))
75       (defun bbdb-extract-field-value (field)
76         (let ((value (tm:bbdb-extract-field-value field)))
77           (and value
78                (mime-eword/decode-string value))))
79       ))
80
81
82 ;;; @ full-name canonicalization methods
83 ;;;
84
85 (defun tm-bbdb/canonicalize-spaces (str)
86   (let (dest)
87     (while (string-match "\\s +" str)
88       (setq dest (cons (substring str 0 (match-beginning 0)) dest))
89       (setq str (substring str (match-end 0)))
90       )
91     (or (string= str "")
92         (setq dest (cons str dest)))
93     (setq dest (nreverse dest))
94     (mapconcat 'identity dest " ")
95     ))
96
97 (defun tm-bbdb/canonicalize-dots (str)
98   (let (dest)
99     (while (string-match "\\." str)
100       (setq dest (cons (substring str 0 (match-end 0)) dest))
101       (setq str (substring str (match-end 0)))
102       )
103     (or (string= str "")
104         (setq dest (cons str dest)))
105     (setq dest (nreverse dest))
106     (mapconcat 'identity dest " ")
107     ))
108
109 (defvar tm-bbdb/canonicalize-full-name-methods
110   '(mime-eword/decode-string
111     tm-bbdb/canonicalize-dots
112     tm-bbdb/canonicalize-spaces))
113
114
115 ;;; @ BBDB functions for mime/viewer-mode
116 ;;;
117
118 (defvar tm-bbdb/auto-create-p nil)
119
120 (defun tm-bbdb/update-record (&optional offer-to-create)
121   "Return the record corresponding to the current MIME previewing message.
122 Creating or modifying it as necessary. A record will be created if
123 tm-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
124 the user confirms the creation."
125   (save-excursion
126     (if (and mime::article/preview-buffer
127              (get-buffer mime::article/preview-buffer))
128         (set-buffer mime::article/preview-buffer))
129     (if bbdb-use-pop-up
130         (tm-bbdb/pop-up-bbdb-buffer offer-to-create)
131       (let* ((from (std11-field-body "From"))
132              (addr (if from
133                        (car (cdr (mail-extract-address-components from))))))
134         (if (or (null from)
135                 (null addr)
136                 (string-match (bbdb-user-mail-names) addr))
137             (setq from (or (std11-field-body "To") from))
138           )
139         (if from
140             (bbdb-annotate-message-sender
141              from t
142              (or (bbdb-invoke-hook-for-value tm-bbdb/auto-create-p)
143                  offer-to-create)
144              offer-to-create))
145         ))))
146
147 (defun tm-bbdb/annotate-sender (string)
148   "Add a line to the end of the Notes field of the BBDB record 
149 corresponding to the sender of this message."
150   (interactive
151    (list (if bbdb-readonly-p
152              (error "The Insidious Big Brother Database is read-only.")
153            (read-string "Comments: "))))
154   (bbdb-annotate-notes (tm-bbdb/update-record t) string))
155
156 (defun tm-bbdb/edit-notes (&optional arg)
157   "Edit the notes field or (with a prefix arg) a user-defined field
158 of the BBDB record corresponding to the sender of this message."
159   (interactive "P")
160   (let ((record (or (tm-bbdb/update-record t)
161                     (error ""))))
162     (bbdb-display-records (list record))
163     (if arg
164         (bbdb-record-edit-property record nil t)
165       (bbdb-record-edit-notes record t))))
166
167 (defun tm-bbdb/show-sender ()
168   "Display the contents of the BBDB for the sender of this message.
169 This buffer will be in bbdb-mode, with associated keybindings."
170   (interactive)
171   (let ((record (tm-bbdb/update-record t)))
172     (if record
173         (bbdb-display-records (list record))
174         (error "unperson"))))
175
176 (defun tm-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
177   "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
178 displaying the record corresponding to the sender of the current message."
179   (bbdb-pop-up-bbdb-buffer
180     (function (lambda (w)
181       (let ((b (current-buffer)))
182         (set-buffer (window-buffer w))
183         (prog1 (eq major-mode 'mime/viewer-mode)
184           (set-buffer b))))))
185   (let ((bbdb-gag-messages t)
186         (bbdb-use-pop-up nil)
187         (bbdb-electric-p nil))
188     (let ((record (tm-bbdb/update-record offer-to-create))
189           (bbdb-elided-display (bbdb-pop-up-elided-display))
190           (b (current-buffer)))
191       (bbdb-display-records (if record (list record) nil))
192       (if (not record)
193           (progn
194             (set-buffer "*BBDB*")
195             (delete-window)))
196       (set-buffer b)
197       record)))
198
199 (defun tm-bbdb/define-keys ()
200   (let ((mime/viewer-mode-map (current-local-map)))
201     (define-key mime/viewer-mode-map ";" 'tm-bbdb/edit-notes)
202     (define-key mime/viewer-mode-map ":" 'tm-bbdb/show-sender)
203     ))
204
205 (add-hook 'mime-viewer/define-keymap-hook 'tm-bbdb/define-keys)
206
207
208 ;;; @ for signature.el
209 ;;;
210
211 (defun signature/get-bbdb-sigtype (addr)
212   "Extract sigtype information from BBDB."
213   (let ((record (bbdb-search-simple nil addr)))
214     (and record
215          (bbdb-record-getprop record 'sigtype))
216     ))
217
218 (defun signature/set-bbdb-sigtype (sigtype addr)
219   "Add sigtype information to BBDB."
220   (let* ((bbdb-notice-hook nil)
221          (record (bbdb-annotate-message-sender 
222                   addr t
223                   (bbdb-invoke-hook-for-value 
224                    bbdb/mail-auto-create-p)
225                   t)))
226     (if record
227         (progn
228           (bbdb-record-putprop record 'sigtype sigtype)
229           (bbdb-change-record record nil))
230       )))
231
232 (defun signature/get-sigtype-from-bbdb (&optional verbose)
233   (let* ((to (std11-field-body "To"))
234          (addr (and to
235                     (car (cdr (mail-extract-address-components to)))))
236          (sigtype (signature/get-bbdb-sigtype addr))
237          return  
238          )
239     (if addr
240         (if verbose
241             (progn
242               (setq return (signature/get-sigtype-interactively sigtype))
243               (if (and (not (string-equal return sigtype))
244                        (y-or-n-p
245                         (format "Register \"%s\" for <%s>? " return addr))
246                        )
247                   (signature/set-bbdb-sigtype return addr)
248                 )
249               return)
250           (or sigtype
251               (signature/get-signature-file-name))
252           ))
253     ))
254
255
256 ;;; @ end
257 ;;;
258
259 (provide 'tm-bbdb)
260
261 (run-hooks 'tm-bbdb-load-hook)
262
263 ;;; end of tm-bbdb.el