1 ;;; mime-bbdb.el --- SEMI shared module for BBDB
3 ;; Copyright (C) 1995,1996,1997 Shuhei KOBAYASHI
4 ;; Copyright (C) 1997 MORIOKA Tomohiko
6 ;; Author: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
7 ;; Maintainer: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;; Version: $Id: mime-bbdb.el,v 0.5 1997-11-06 16:21:15 morioka Exp $
9 ;; Keywords: BBDB, MIME, multimedia, multilingual, mail, news
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
34 (if (module-installed-p 'bbdb-com)
37 ;; imported from bbdb-1.51
38 (defmacro bbdb-pop-up-elided-display ()
39 '(if (boundp 'bbdb-pop-up-elided-display)
40 bbdb-pop-up-elided-display
42 (defmacro bbdb-user-mail-names ()
43 "Returns a regexp matching the address of the logged-in user"
44 '(or bbdb-user-mail-names
45 (setq bbdb-user-mail-names
46 (concat "\\b" (regexp-quote (user-login-name)) "\\b"))))
53 (defvar mime-bbdb/use-mail-extr t
54 "*If non-nil, `mail-extract-address-components' is used.
55 Otherwise `mime-bbdb/extract-address-components' overrides it.")
57 (defvar mime-bbdb/auto-create-p nil
58 "*If t, create new BBDB records automatically.
59 If function, then it is called with no arguments to decide whether an
60 entry should be automatically creaded.
62 mime-bbdb uses this variable instead of `bbdb/mail-auto-create-p' or
63 `bbdb/news-auto-create-p' unless other tm-MUA overrides it.")
65 (defvar mime-bbdb/delete-empty-window nil
66 "*If non-nil, delete empty BBDB window.
67 All bbdb-MUAs but bbdb-gnus display BBDB window even if it is empty.
68 If you prefer behavior of bbdb-gnus, set this variable to t.
70 For framepop users: If empty, `framepop-banish' is used instead.")
75 (defun mime-bbdb/extract-address-components (str)
76 (let* ((ret (std11-extract-address-components str))
78 (address (car (cdr ret)))
79 (methods mime-bbdb/canonicalize-full-name-methods))
80 (while (and phrase methods)
81 (setq phrase (funcall (car methods) phrase)
82 methods (cdr methods)))
83 (if (string= address "") (setq address nil))
84 (if (string= phrase "") (setq phrase nil))
88 (or mime-bbdb/use-mail-extr
90 (require 'mail-extr) ; for `what-domain'
91 (or (fboundp 'tm:mail-extract-address-components)
92 (fset 'tm:mail-extract-address-components
93 (symbol-function 'mail-extract-address-components)))
94 (fset 'mail-extract-address-components
95 (symbol-function 'mime-bbdb/extract-address-components))
99 ;;; @ bbdb-extract-field-value
102 (or (fboundp 'tm:bbdb-extract-field-value)
104 ;; (require 'bbdb-hooks) ; not provided.
105 ;; (or (fboundp 'bbdb-extract-field-value) ; defined as autoload
106 (or (fboundp 'bbdb-header-start)
108 (fset 'tm:bbdb-extract-field-value
109 (symbol-function 'bbdb-extract-field-value))
110 (defun bbdb-extract-field-value (field)
111 (let ((value (tm:bbdb-extract-field-value field)))
113 (eword-decode-string value))))
117 ;;; @ full-name canonicalization methods
120 (defun mime-bbdb/canonicalize-spaces (str)
122 (while (string-match "\\s +" str)
123 (setq dest (cons (substring str 0 (match-beginning 0)) dest))
124 (setq str (substring str (match-end 0)))
127 (setq dest (cons str dest)))
128 (setq dest (nreverse dest))
129 (mapconcat 'identity dest " ")
132 (defun mime-bbdb/canonicalize-dots (str)
134 (while (string-match "\\." str)
135 (setq dest (cons (substring str 0 (match-end 0)) dest))
136 (setq str (substring str (match-end 0)))
139 (setq dest (cons str dest)))
140 (setq dest (nreverse dest))
141 (mapconcat 'identity dest " ")
144 (defvar mime-bbdb/canonicalize-full-name-methods
145 '(eword-decode-string
146 mime-bbdb/canonicalize-dots
147 mime-bbdb/canonicalize-spaces))
150 ;;; @ BBDB functions for mime-view-mode
153 (defun mime-bbdb/update-record (&optional offer-to-create)
154 "Return the record corresponding to the current MIME previewing message.
155 Creating or modifying it as necessary. A record will be created if
156 mime-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
157 the user confirms the creation."
159 (if (and mime-view-buffer
160 (get-buffer mime-view-buffer))
161 (set-buffer mime-view-buffer))
163 (mime-bbdb/pop-up-bbdb-buffer offer-to-create)
164 (let* ((from (std11-field-body "From"))
166 (car (cdr (mail-extract-address-components from))))))
169 (string-match (bbdb-user-mail-names) addr))
170 (setq from (or (std11-field-body "To") from))
173 (bbdb-annotate-message-sender
175 (or (bbdb-invoke-hook-for-value mime-bbdb/auto-create-p)
180 (defun mime-bbdb/annotate-sender (string)
181 "Add a line to the end of the Notes field of the BBDB record
182 corresponding to the sender of this message."
184 (list (if bbdb-readonly-p
185 (error "The Insidious Big Brother Database is read-only.")
186 (read-string "Comments: "))))
187 (bbdb-annotate-notes (mime-bbdb/update-record t) string))
189 (defun mime-bbdb/edit-notes (&optional arg)
190 "Edit the notes field or (with a prefix arg) a user-defined field
191 of the BBDB record corresponding to the sender of this message."
193 (let ((record (or (mime-bbdb/update-record t)
195 (bbdb-display-records (list record))
197 (bbdb-record-edit-property record nil t)
198 (bbdb-record-edit-notes record t))))
200 (defun mime-bbdb/show-sender ()
201 "Display the contents of the BBDB for the sender of this message.
202 This buffer will be in bbdb-mode, with associated keybindings."
204 (let ((record (mime-bbdb/update-record t)))
206 (bbdb-display-records (list record))
207 (error "unperson"))))
209 (defun mime-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
210 "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
211 displaying the record corresponding to the sender of the current message."
212 (let ((framepop (eq temp-buffer-show-function 'framepop-display-buffer)))
214 (bbdb-pop-up-bbdb-buffer
217 (let ((b (current-buffer)))
218 (set-buffer (window-buffer w))
219 (prog1 (eq major-mode 'mime-view-mode)
221 (let ((bbdb-gag-messages t)
222 (bbdb-use-pop-up nil)
223 (bbdb-electric-p nil))
224 (let ((record (mime-bbdb/update-record offer-to-create))
225 (bbdb-elided-display (bbdb-pop-up-elided-display))
226 (b (current-buffer)))
229 (bbdb-display-records (list record))
231 (bbdb-display-records (if record (list record) nil))
232 (if (and (null record)
233 mime-bbdb/delete-empty-window)
234 (delete-windows-on (get-buffer "*BBDB*"))))
238 (defun mime-bbdb/define-keys ()
239 (let ((mime-view-mode-map (current-local-map)))
240 (define-key mime-view-mode-map ";" 'mime-bbdb/edit-notes)
241 (define-key mime-view-mode-map ":" 'mime-bbdb/show-sender)
244 (add-hook 'mime-view-define-keymap-hook 'mime-bbdb/define-keys)
247 ;;; @ for signature.el
250 (defun signature/get-bbdb-sigtype (addr)
251 "Extract sigtype information from BBDB."
252 (let ((record (bbdb-search-simple nil addr)))
254 (bbdb-record-getprop record 'sigtype))
257 (defun signature/set-bbdb-sigtype (sigtype addr)
258 "Add sigtype information to BBDB."
259 (let* ((bbdb-notice-hook nil)
260 (record (bbdb-annotate-message-sender
262 (bbdb-invoke-hook-for-value
263 bbdb/mail-auto-create-p)
267 (bbdb-record-putprop record 'sigtype sigtype)
268 (bbdb-change-record record nil))
271 (defun signature/get-sigtype-from-bbdb (&optional verbose)
272 (let* ((to (std11-field-body "To"))
274 (car (cdr (mail-extract-address-components to)))))
275 (sigtype (signature/get-bbdb-sigtype addr))
281 (setq return (signature/get-sigtype-interactively sigtype))
282 (if (and (not (string-equal return sigtype))
284 (format "Register \"%s\" for <%s>? " return addr))
286 (signature/set-bbdb-sigtype return addr)
290 (signature/get-signature-file-name))
300 (run-hooks 'mime-bbdb-load-hook)
302 ;;; end of mime-bbdb.el