e07e816f80e08a57e6daa475db1875704a82e8d1
[elisp/tm.git] / tm-bbdb.el
1 ;;; tm-bbdb.el --- tm shared module for BBDB
2
3 ;; Copyright (C) 1995,1996 Shuhei KOBAYASHI
4 ;; Copyright (C) 1996 Artur Pioro
5
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.22 1996/11/30 18:40:09 shuhei-k Exp $
10 ;; Keywords: mail, news, MIME, multimedia, multilingual, BBDB
11
12 ;; This file is part of tm (Tools for MIME).
13
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.
18
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.
23
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.
28
29 ;;; Code:
30
31 (require 'std11)
32 (require 'tm-ew-d)
33 (require 'tm-view)
34 (require 'bbdb-com) ; (require 'bbdb) implicitly
35
36 ;;; @ mail-extr
37 ;;;
38
39 (defvar tm-bbdb/use-mail-extr t)
40
41 (defun tm-bbdb/extract-address-components (str)
42   (let* ((ret     (std11-extract-address-components str))
43          (phrase  (car ret))
44          (address (car (cdr ret)))
45          (methods tm-bbdb/canonicalize-full-name-methods))
46     (while (and phrase methods)
47       (setq phrase  (funcall (car methods) phrase)
48             methods (cdr methods)))
49     (if (string= address "") (setq address nil))
50     (if (string= phrase "") (setq phrase nil))
51     (list phrase address)
52     ))
53
54 (or tm-bbdb/use-mail-extr
55     (progn
56       (require 'mail-extr) ; for `what-domain'
57       (or (fboundp 'tm:mail-extract-address-components)
58           (fset 'tm:mail-extract-address-components
59                 (symbol-function 'mail-extract-address-components)))
60       (fset 'mail-extract-address-components
61             (symbol-function 'tm-bbdb/extract-address-components))
62       ))
63
64
65 ;;; @ bbdb-extract-field-value
66 ;;;
67
68 (or (fboundp 'tm:bbdb-extract-field-value)
69     (progn
70       ;; (require 'bbdb-hooks) ; not provided.
71       ;; (or (fboundp 'bbdb-extract-field-value) ; defined as autoload
72       (or (fboundp 'bbdb-header-start)
73           (load "bbdb-hooks"))
74       (fset 'tm:bbdb-extract-field-value
75             (symbol-function 'bbdb-extract-field-value))
76       (defun bbdb-extract-field-value (field)
77         (let ((value (tm:bbdb-extract-field-value field)))
78           (and value
79                (mime-eword/decode-string value))))
80       ))
81
82
83 ;;; @ full-name canonicalization methods
84 ;;;
85
86 (defun tm-bbdb/canonicalize-spaces (str)
87   (let (dest)
88     (while (string-match "\\s +" str)
89       (setq dest (cons (substring str 0 (match-beginning 0)) dest))
90       (setq str (substring str (match-end 0)))
91       )
92     (or (string= str "")
93         (setq dest (cons str dest)))
94     (setq dest (nreverse dest))
95     (mapconcat 'identity dest " ")
96     ))
97
98 (defun tm-bbdb/canonicalize-dots (str)
99   (let (dest)
100     (while (string-match "\\." str)
101       (setq dest (cons (substring str 0 (match-end 0)) dest))
102       (setq str (substring str (match-end 0)))
103       )
104     (or (string= str "")
105         (setq dest (cons str dest)))
106     (setq dest (nreverse dest))
107     (mapconcat 'identity dest " ")
108     ))
109
110 (defvar tm-bbdb/canonicalize-full-name-methods
111   '(mime-eword/decode-string
112     tm-bbdb/canonicalize-dots
113     tm-bbdb/canonicalize-spaces))
114
115
116 ;;; @ BBDB functions for mime/viewer-mode
117 ;;;
118
119 (defvar tm-bbdb/auto-create-p nil)
120
121 (defun tm-bbdb/update-record (&optional offer-to-create)
122   "Return the record corresponding to the current MIME previewing message.
123 Creating or modifying it as necessary. A record will be created if
124 tm-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
125 the user confirms the creation."
126   (save-excursion
127     (if (and mime::article/preview-buffer
128              (get-buffer mime::article/preview-buffer))
129         (set-buffer mime::article/preview-buffer))
130     (if bbdb-use-pop-up
131         (tm-bbdb/pop-up-bbdb-buffer offer-to-create)
132       (let* ((from (std11-field-body "From"))
133              (addr (if from
134                        (car (cdr (mail-extract-address-components from))))))
135         (if (or (null from)
136                 (null addr)
137                 (string-match (bbdb-user-mail-names) addr))
138             (setq from (or (std11-field-body "To") from))
139           )
140         (if from
141             (bbdb-annotate-message-sender
142              from t
143              (or (bbdb-invoke-hook-for-value tm-bbdb/auto-create-p)
144                  offer-to-create)
145              offer-to-create))
146         ))))
147
148 (defun tm-bbdb/annotate-sender (string)
149   "Add a line to the end of the Notes field of the BBDB record
150 corresponding to the sender of this message."
151   (interactive
152    (list (if bbdb-readonly-p
153              (error "The Insidious Big Brother Database is read-only.")
154            (read-string "Comments: "))))
155   (bbdb-annotate-notes (tm-bbdb/update-record t) string))
156
157 (defun tm-bbdb/edit-notes (&optional arg)
158   "Edit the notes field or (with a prefix arg) a user-defined field
159 of the BBDB record corresponding to the sender of this message."
160   (interactive "P")
161   (let ((record (or (tm-bbdb/update-record t)
162                     (error ""))))
163     (bbdb-display-records (list record))
164     (if arg
165         (bbdb-record-edit-property record nil t)
166       (bbdb-record-edit-notes record t))))
167
168 (defun tm-bbdb/show-sender ()
169   "Display the contents of the BBDB for the sender of this message.
170 This buffer will be in bbdb-mode, with associated keybindings."
171   (interactive)
172   (let ((record (tm-bbdb/update-record t)))
173     (if record
174         (bbdb-display-records (list record))
175         (error "unperson"))))
176
177 (defun tm-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
178   "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
179 displaying the record corresponding to the sender of the current message."
180   (let ((framepop (eq temp-buffer-show-function 'framepop-display-buffer)))
181     (or framepop
182         (bbdb-pop-up-bbdb-buffer
183          (function
184           (lambda (w)
185             (let ((b (current-buffer)))
186               (set-buffer (window-buffer w))
187               (prog1 (eq major-mode 'mime/viewer-mode)
188                 (set-buffer b)))))))
189     (let ((bbdb-gag-messages t)
190           (bbdb-use-pop-up nil)
191           (bbdb-electric-p nil))
192       (let ((record (tm-bbdb/update-record offer-to-create))
193             (bbdb-elided-display (bbdb-pop-up-elided-display))
194             (b (current-buffer)))
195         (if framepop
196             (if record
197                 (bbdb-display-records (list record))
198               (framepop-banish))
199           (bbdb-display-records (if record (list record) nil)))
200         (set-buffer b)
201         record))))
202
203 (defun tm-bbdb/define-keys ()
204   (let ((mime/viewer-mode-map (current-local-map)))
205     (define-key mime/viewer-mode-map ";" 'tm-bbdb/edit-notes)
206     (define-key mime/viewer-mode-map ":" 'tm-bbdb/show-sender)
207     ))
208
209 (add-hook 'mime-viewer/define-keymap-hook 'tm-bbdb/define-keys)
210
211
212 ;;; @ for signature.el
213 ;;;
214
215 (defun signature/get-bbdb-sigtype (addr)
216   "Extract sigtype information from BBDB."
217   (let ((record (bbdb-search-simple nil addr)))
218     (and record
219          (bbdb-record-getprop record 'sigtype))
220     ))
221
222 (defun signature/set-bbdb-sigtype (sigtype addr)
223   "Add sigtype information to BBDB."
224   (let* ((bbdb-notice-hook nil)
225          (record (bbdb-annotate-message-sender
226                   addr t
227                   (bbdb-invoke-hook-for-value
228                    bbdb/mail-auto-create-p)
229                   t)))
230     (if record
231         (progn
232           (bbdb-record-putprop record 'sigtype sigtype)
233           (bbdb-change-record record nil))
234       )))
235
236 (defun signature/get-sigtype-from-bbdb (&optional verbose)
237   (let* ((to (std11-field-body "To"))
238          (addr (and to
239                     (car (cdr (mail-extract-address-components to)))))
240          (sigtype (signature/get-bbdb-sigtype addr))
241          return
242          )
243     (if addr
244         (if verbose
245             (progn
246               (setq return (signature/get-sigtype-interactively sigtype))
247               (if (and (not (string-equal return sigtype))
248                        (y-or-n-p
249                         (format "Register \"%s\" for <%s>? " return addr))
250                        )
251                   (signature/set-bbdb-sigtype return addr)
252                 )
253               return)
254           (or sigtype
255               (signature/get-signature-file-name))
256           ))
257     ))
258
259
260 ;;; @ end
261 ;;;
262
263 (provide 'tm-bbdb)
264
265 (run-hooks 'tm-bbdb-load-hook)
266
267 ;;; end of tm-bbdb.el