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