1 ;;; mime-bbdb.el --- SEMI shared module for BBDB
3 ;; Copyright (C) 1995,1996,1997 Shuhei KOBAYASHI
4 ;; Copyright (C) 1997,1998 MORIOKA Tomohiko
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
10 ;; This file is part of SEMI (Suite of Emacs MIME Interfaces).
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.
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.
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.
33 (if (module-installed-p 'bbdb-com)
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
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"))))))
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.")
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.
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.")
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.
68 For framepop users: If empty, `framepop-banish' is used instead.")
73 (defun mime-bbdb/extract-address-components (str)
74 (let* ((ret (std11-extract-address-components str))
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)))
85 (or mime-bbdb/use-mail-extr
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))))
95 ;;; @ bbdb-extract-field-value
98 (or (fboundp 'tm:bbdb-extract-field-value)
100 ;; (require 'bbdb-hooks) ; not provided.
101 ;; (or (fboundp 'bbdb-extract-field-value) ; defined as autoload
103 ;; almost BBDB functions are autoloaded.
104 ;; (or (fboundp 'bbdb-header-start)
105 (or (and (fboundp 'bbdb-extract-field-value)
106 (not (eq 'autoload (car-safe (symbol-function
107 'bbdb-extract-field-value)))))
109 (fset 'tm:bbdb-extract-field-value
110 (symbol-function 'bbdb-extract-field-value))
111 (defun bbdb-extract-field-value (field)
112 (let ((value (tm:bbdb-extract-field-value field)))
114 (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))))
126 (setq dest (cons str dest)))
127 (setq dest (nreverse dest))
128 (mapconcat 'identity dest " ")))
130 (defun mime-bbdb/canonicalize-dots (str)
132 (while (string-match "\\." str)
133 (setq dest (cons (substring str 0 (match-end 0)) dest))
134 (setq str (substring str (match-end 0))))
136 (setq dest (cons str dest)))
137 (setq dest (nreverse dest))
138 (mapconcat 'identity dest " ")))
140 (defvar mime-bbdb/canonicalize-full-name-methods
141 '(eword-decode-string
142 mime-bbdb/canonicalize-dots
143 mime-bbdb/canonicalize-spaces))
146 ;;; @ BBDB functions for mime-view-mode
149 (defun mime-bbdb/update-record (&optional offer-to-create)
150 "Return the record corresponding to the current MIME previewing message.
151 Creating or modifying it as necessary. A record will be created if
152 mime-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
153 the user confirms the creation."
155 (if (and mime-preview-buffer
156 (get-buffer mime-preview-buffer))
157 (set-buffer mime-preview-buffer))
159 (mime-bbdb/pop-up-bbdb-buffer offer-to-create)
160 (let* ((message (get-text-property (point-min) 'mime-view-entity))
161 (from (mime-entity-fetch-field message 'From))
164 (null (setq addr (car (mime-entity-read-field message 'From))))
165 (string-match (bbdb-user-mail-names)
166 (std11-address-string addr)))
167 (setq from (or (mime-entity-fetch-field message 'To)
170 (bbdb-annotate-message-sender
171 (mime-decode-field-body from 'From) t
172 (or (bbdb-invoke-hook-for-value mime-bbdb/auto-create-p)
174 offer-to-create))))))
176 (defun mime-bbdb/annotate-sender (string)
177 "Add a line to the end of the Notes field of the BBDB record
178 corresponding to the sender of this message."
180 (list (if bbdb-readonly-p
181 (error "The Insidious Big Brother Database is read-only.")
182 (read-string "Comments: "))))
183 (bbdb-annotate-notes (mime-bbdb/update-record t) string))
185 (defun mime-bbdb/edit-notes (&optional arg)
186 "Edit the notes field or (with a prefix arg) a user-defined field
187 of the BBDB record corresponding to the sender of this message."
189 (let ((record (or (mime-bbdb/update-record t)
191 (bbdb-display-records (list record))
193 (bbdb-record-edit-property record nil t)
194 (bbdb-record-edit-notes record t))))
196 (defun mime-bbdb/show-sender ()
197 "Display the contents of the BBDB for the sender of this message.
198 This buffer will be in bbdb-mode, with associated keybindings."
200 (let ((record (mime-bbdb/update-record t)))
202 (bbdb-display-records (list record))
203 (error "unperson"))))
205 (defun mime-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
206 "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
207 displaying the record corresponding to the sender of the current message."
208 (let ((framepop (eq temp-buffer-show-function 'framepop-display-buffer)))
210 (bbdb-pop-up-bbdb-buffer
213 (let ((b (current-buffer)))
214 (set-buffer (window-buffer w))
215 (prog1 (eq major-mode 'mime-view-mode)
217 (let ((bbdb-gag-messages t)
218 (bbdb-use-pop-up nil)
219 (bbdb-electric-p nil))
220 (let ((record (mime-bbdb/update-record offer-to-create))
221 (bbdb-elided-display (bbdb-pop-up-elided-display))
222 (b (current-buffer)))
225 (bbdb-display-records (list record))
227 (bbdb-display-records (if record (list record) nil))
228 (if (and (null record)
229 mime-bbdb/delete-empty-window)
230 (delete-windows-on (get-buffer "*BBDB*"))))
234 (defun mime-bbdb/define-keys ()
235 (let ((mime-view-mode-map (current-local-map)))
236 (define-key mime-view-mode-map ";" 'mime-bbdb/edit-notes)
237 (define-key mime-view-mode-map ":" 'mime-bbdb/show-sender)))
239 (add-hook 'mime-view-define-keymap-hook 'mime-bbdb/define-keys)
242 ;;; @ for signature.el
245 (defun signature/get-bbdb-sigtype (addr)
246 "Extract sigtype information from BBDB."
247 (let ((record (bbdb-search-simple nil addr)))
249 (bbdb-record-getprop record 'sigtype))))
251 (defun signature/set-bbdb-sigtype (sigtype addr)
252 "Add sigtype information to BBDB."
253 (let* ((bbdb-notice-hook nil)
254 (record (bbdb-annotate-message-sender
256 (bbdb-invoke-hook-for-value
257 bbdb/mail-auto-create-p)
261 (bbdb-record-putprop record 'sigtype sigtype)
262 (bbdb-change-record record nil)))))
264 (defun signature/get-sigtype-from-bbdb (&optional verbose)
265 (let* ((to (std11-field-body "To"))
267 (car (cdr (mail-extract-address-components to)))))
268 (sigtype (signature/get-bbdb-sigtype addr))
273 (setq return (signature/get-sigtype-interactively sigtype))
274 (if (and (not (string-equal return sigtype))
276 (format "Register \"%s\" for <%s>? " return addr)))
277 (signature/set-bbdb-sigtype return addr))
280 (signature/get-signature-file-name))))))
288 (run-hooks 'mime-bbdb-load-hook)
290 ;;; end of mime-bbdb.el