Synch with `semi-1_14_0'.
[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 ;;; @ User Variables
49 ;;;
50
51 (defvar mime-bbdb/use-mail-extr t
52   "*If non-nil, `mail-extract-address-components' is used.
53 Otherwise `mime-bbdb/extract-address-components' overrides it.")
54
55 (defvar mime-bbdb/auto-create-p nil
56   "*If t, create new BBDB records automatically.
57 If function, then it is called with no arguments to decide whether an
58 entry should be automatically creaded.
59
60 mime-bbdb uses this variable instead of `bbdb/mail-auto-create-p' or
61 `bbdb/news-auto-create-p' unless other tm-MUA overrides it.")
62
63 (defvar mime-bbdb/delete-empty-window nil
64   "*If non-nil, delete empty BBDB window.
65 All bbdb-MUAs but bbdb-gnus display BBDB window even if it is empty.
66 If you prefer behavior of bbdb-gnus, set this variable to t.
67
68 For framepop users: If empty, `framepop-banish' is used instead.")
69
70 ;;; @ mail-extr
71 ;;;
72
73 (defun mime-bbdb/extract-address-components (str)
74   (let* ((ret     (std11-extract-address-components str))
75          (phrase  (car ret))
76          (address (car (cdr ret)))
77          (methods mime-bbdb/canonicalize-full-name-methods))
78     (while (and phrase methods)
79       (setq phrase  (funcall (car methods) phrase)
80             methods (cdr methods)))
81     (if (string= address "") (setq address nil))
82     (if (string= phrase "") (setq phrase nil))
83     (list phrase address)))
84
85 (or mime-bbdb/use-mail-extr
86     (progn
87       (require 'mail-extr) ; for `what-domain'
88       (or (fboundp 'tm:mail-extract-address-components)
89           (fset 'tm:mail-extract-address-components
90                 (symbol-function 'mail-extract-address-components)))
91       (fset 'mail-extract-address-components
92             (symbol-function 'mime-bbdb/extract-address-components))))
93
94
95 ;;; @ bbdb-extract-field-value
96 ;;;
97
98 (or (fboundp 'tm:bbdb-extract-field-value)
99     (progn
100       ;; (require 'bbdb-hooks) ; not provided.
101       ;; (or (fboundp 'bbdb-extract-field-value) ; defined as autoload
102       (or (fboundp 'bbdb-header-start)
103           (load "bbdb-hooks"))
104       (fset 'tm:bbdb-extract-field-value
105             (symbol-function 'bbdb-extract-field-value))
106       (defun bbdb-extract-field-value (field)
107         (let ((value (tm:bbdb-extract-field-value field)))
108           (and value
109                (eword-decode-string value))))))
110
111
112 ;;; @ full-name canonicalization methods
113 ;;;
114
115 (defun mime-bbdb/canonicalize-spaces (str)
116   (let (dest)
117     (while (string-match "\\s +" str)
118       (setq dest (cons (substring str 0 (match-beginning 0)) dest))
119       (setq str (substring str (match-end 0))))
120     (or (string= str "")
121         (setq dest (cons str dest)))
122     (setq dest (nreverse dest))
123     (mapconcat 'identity dest " ")))
124
125 (defun mime-bbdb/canonicalize-dots (str)
126   (let (dest)
127     (while (string-match "\\." str)
128       (setq dest (cons (substring str 0 (match-end 0)) dest))
129       (setq str (substring str (match-end 0))))
130     (or (string= str "")
131         (setq dest (cons str dest)))
132     (setq dest (nreverse dest))
133     (mapconcat 'identity dest " ")))
134
135 (defvar mime-bbdb/canonicalize-full-name-methods
136   '(eword-decode-string
137     mime-bbdb/canonicalize-dots
138     mime-bbdb/canonicalize-spaces))
139
140
141 ;;; @ BBDB functions for mime-view-mode
142 ;;;
143
144 (defun mime-bbdb/update-record (&optional offer-to-create)
145   "Return the record corresponding to the current MIME previewing message.
146 Creating or modifying it as necessary. A record will be created if
147 mime-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
148 the user confirms the creation."
149   (save-excursion
150     (if (and mime-preview-buffer
151              (get-buffer mime-preview-buffer))
152         (set-buffer mime-preview-buffer))
153     (if bbdb-use-pop-up
154         (mime-bbdb/pop-up-bbdb-buffer offer-to-create)
155       (let* ((message (get-text-property (point-min) 'mime-view-entity))
156              (from (mime-entity-fetch-field message 'From))
157              addr)
158         (if (or (null from)
159                 (null (setq addr (car (mime-entity-read-field message 'From))))
160                 (string-match (bbdb-user-mail-names)
161                               (std11-address-string addr)))
162             (setq from (or (mime-entity-fetch-field message 'To)
163                            from)))
164         (if from
165             (bbdb-annotate-message-sender
166              (mime-decode-field-body from 'From) t
167              (or (bbdb-invoke-hook-for-value mime-bbdb/auto-create-p)
168                  offer-to-create)
169              offer-to-create))))))
170
171 (defun mime-bbdb/annotate-sender (string)
172   "Add a line to the end of the Notes field of the BBDB record
173 corresponding to the sender of this message."
174   (interactive
175    (list (if bbdb-readonly-p
176              (error "The Insidious Big Brother Database is read-only.")
177            (read-string "Comments: "))))
178   (bbdb-annotate-notes (mime-bbdb/update-record t) string))
179
180 (defun mime-bbdb/edit-notes (&optional arg)
181   "Edit the notes field or (with a prefix arg) a user-defined field
182 of the BBDB record corresponding to the sender of this message."
183   (interactive "P")
184   (let ((record (or (mime-bbdb/update-record t)
185                     (error ""))))
186     (bbdb-display-records (list record))
187     (if arg
188         (bbdb-record-edit-property record nil t)
189       (bbdb-record-edit-notes record t))))
190
191 (defun mime-bbdb/show-sender ()
192   "Display the contents of the BBDB for the sender of this message.
193 This buffer will be in bbdb-mode, with associated keybindings."
194   (interactive)
195   (let ((record (mime-bbdb/update-record t)))
196     (if record
197         (bbdb-display-records (list record))
198         (error "unperson"))))
199
200 (defun mime-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
201   "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
202 displaying the record corresponding to the sender of the current message."
203   (let ((framepop (eq temp-buffer-show-function 'framepop-display-buffer)))
204     (or framepop
205         (bbdb-pop-up-bbdb-buffer
206          (function
207           (lambda (w)
208             (let ((b (current-buffer)))
209               (set-buffer (window-buffer w))
210               (prog1 (eq major-mode 'mime-view-mode)
211                 (set-buffer b)))))))
212     (let ((bbdb-gag-messages t)
213           (bbdb-use-pop-up nil)
214           (bbdb-electric-p nil))
215       (let ((record (mime-bbdb/update-record offer-to-create))
216             (bbdb-elided-display (bbdb-pop-up-elided-display))
217             (b (current-buffer)))
218         (if framepop
219             (if record
220                 (bbdb-display-records (list record))
221               (framepop-banish))
222           (bbdb-display-records (if record (list record) nil))
223           (if (and (null record)
224                    mime-bbdb/delete-empty-window)
225               (delete-windows-on (get-buffer "*BBDB*"))))
226         (set-buffer b)
227         record))))
228
229 (defun mime-bbdb/define-keys ()
230   (let ((mime-view-mode-map (current-local-map)))
231     (define-key mime-view-mode-map ";" 'mime-bbdb/edit-notes)
232     (define-key mime-view-mode-map ":" 'mime-bbdb/show-sender)))
233
234 (add-hook 'mime-view-define-keymap-hook 'mime-bbdb/define-keys)
235
236
237 ;;; @ for signature.el
238 ;;;
239
240 (defun signature/get-bbdb-sigtype (addr)
241   "Extract sigtype information from BBDB."
242   (let ((record (bbdb-search-simple nil addr)))
243     (and record
244          (bbdb-record-getprop record 'sigtype))))
245
246 (defun signature/set-bbdb-sigtype (sigtype addr)
247   "Add sigtype information to BBDB."
248   (let* ((bbdb-notice-hook nil)
249          (record (bbdb-annotate-message-sender
250                   addr t
251                   (bbdb-invoke-hook-for-value
252                    bbdb/mail-auto-create-p)
253                   t)))
254     (if record
255         (progn
256           (bbdb-record-putprop record 'sigtype sigtype)
257           (bbdb-change-record record nil)))))
258
259 (defun signature/get-sigtype-from-bbdb (&optional verbose)
260   (let* ((to (std11-field-body "To"))
261          (addr (and to
262                     (car (cdr (mail-extract-address-components to)))))
263          (sigtype (signature/get-bbdb-sigtype addr))
264          return)
265     (if addr
266         (if verbose
267             (progn
268               (setq return (signature/get-sigtype-interactively sigtype))
269               (if (and (not (string-equal return sigtype))
270                        (y-or-n-p
271                         (format "Register \"%s\" for <%s>? " return addr)))
272                   (signature/set-bbdb-sigtype return addr))
273               return)
274           (or sigtype
275               (signature/get-signature-file-name))))))
276
277
278 ;;; @ end
279 ;;;
280
281 (provide 'mime-bbdb)
282
283 (run-hooks 'mime-bbdb-load-hook)
284
285 ;;; end of mime-bbdb.el