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