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