1 ;;; tm-bbdb.el --- tm shared module for BBDB
3 ;; Copyright (C) 1995,1996 Shuhei KOBAYASHI
4 ;; Copyright (C) 1996 Artur Pioro
6 ;; Author: Shuhei KOBAYASHI <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.27 1996/12/10 14:24:23 morioka Exp $
10 ;; Keywords: mail, news, MIME, multimedia, multilingual, BBDB
12 ;; This file is part of tm (Tools for MIME).
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.
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.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
32 (require 'eword-decode)
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 tm-bbdb/use-mail-extr t
54 "*If non-nil, `mail-extract-address-components' is used.
55 Otherwise `tm-bbdb/extract-address-components' overrides it.")
57 (defvar tm-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 tm-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 tm-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 tm-bbdb/extract-address-components (str)
76 (let* ((ret (std11-extract-address-components str))
78 (address (car (cdr ret)))
79 (methods tm-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 tm-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 'tm-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 tm-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 tm-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 tm-bbdb/canonicalize-full-name-methods
145 '(eword-decode-string
146 tm-bbdb/canonicalize-dots
147 tm-bbdb/canonicalize-spaces))
150 ;;; @ BBDB functions for mime/viewer-mode
153 (defun tm-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 tm-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::article/preview-buffer
160 (get-buffer mime::article/preview-buffer))
161 (set-buffer mime::article/preview-buffer))
163 (tm-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 tm-bbdb/auto-create-p)
180 (defun tm-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 (tm-bbdb/update-record t) string))
189 (defun tm-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 (tm-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 tm-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 (tm-bbdb/update-record t)))
206 (bbdb-display-records (list record))
207 (error "unperson"))))
209 (defun tm-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/viewer-mode)
221 (let ((bbdb-gag-messages t)
222 (bbdb-use-pop-up nil)
223 (bbdb-electric-p nil))
224 (let ((record (tm-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 tm-bbdb/delete-empty-window)
234 (delete-windows-on (get-buffer "*BBDB*"))))
238 (defun tm-bbdb/define-keys ()
239 (let ((mime/viewer-mode-map (current-local-map)))
240 (define-key mime/viewer-mode-map ";" 'tm-bbdb/edit-notes)
241 (define-key mime/viewer-mode-map ":" 'tm-bbdb/show-sender)
244 (add-hook 'mime-viewer/define-keymap-hook 'tm-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 'tm-bbdb-load-hook)
302 ;;; end of tm-bbdb.el