tm 7.70.
[elisp/tm.git] / tm-bbdb.el
1 ;;;
2 ;;; tm-bbdb.el --- tm shared module for BBDB
3 ;;;
4 ;;; Copyright (C) 1995,1996 KOBAYASHI Shuhei
5 ;;; Copyright (C) 1996 Artur Pioro
6 ;;;
7 ;;; Author: KOBAYASHI Shuhei <shuhei-k@jaist.ac.jp>
8 ;;;         Artur Pioro <artur@flugor.if.uj.edu.pl>
9 ;;; modified by Pekka Marjola <pema@iki.fi>
10 ;;; Maintainer: KOBAYASHI Shuhei <shuhei-k@jaist.ac.jp>
11 ;;; Version: $Id: tm-bbdb.el,v 7.10 1996/07/01 23:43:25 morioka Exp $
12 ;;; Keywords: mail, news, MIME, multimedia, multilingual, BBDB
13 ;;;
14 ;;; This file is part of tm (Tools for MIME).
15 ;;;
16 ;;; This program is free software; you can redistribute it and/or
17 ;;; modify it under the terms of the GNU General Public License as
18 ;;; published by the Free Software Foundation; either version 2, or
19 ;;; (at your option) any later version.
20 ;;;
21 ;;; This program is distributed in the hope that it will be useful,
22 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 ;;; General Public License for more details.
25 ;;;
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with This program.  If not, write to the Free Software
28 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;;;
30 ;;; Code:
31
32 (require 'bbdb)
33 (require 'bbdb-com)
34 (require 'tl-822)
35 (require 'tm-ew-d)
36 (require 'tm-view)
37
38
39 ;;; @ mail-extr
40 ;;;
41
42 (defvar tm-bbdb/use-mail-extr t)
43
44 (defun tm-bbdb/extract-address-components (str)
45   (let* ((ret     (rfc822/extract-address-components str))
46          (phrase  (car ret))
47          (address (car (cdr ret)))
48          (methods tm-bbdb/canonicalize-full-name-methods))
49     (while (and phrase methods)
50       (setq phrase  (funcall (car methods) phrase)
51             methods (cdr methods)))
52     (if (string= address "") (setq address nil))
53     (if (string= phrase "") (setq phrase nil))
54     (list phrase address)
55     ))
56
57 (or tm-bbdb/use-mail-extr
58     (progn
59       (require 'mail-extr) ; for `what-domain'
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 (rfc822/get-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 (rfc822/get-field-body "To")
139                            from)))
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   (bbdb-pop-up-bbdb-buffer
181     (function (lambda (w)
182       (let ((b (current-buffer)))
183         (set-buffer (window-buffer w))
184         (prog1 (eq major-mode 'mime/viewer-mode)
185           (set-buffer b))))))
186   (let ((bbdb-gag-messages t)
187         (bbdb-use-pop-up nil)
188         (bbdb-electric-p nil))
189     (let ((record (tm-bbdb/update-record offer-to-create))
190           (bbdb-elided-display (bbdb-pop-up-elided-display))
191           (b (current-buffer)))
192       (bbdb-display-records (if record (list record) nil))
193       (set-buffer b)
194       record)))
195
196 (defun tm-bbdb/define-keys ()
197   (let ((mime/viewer-mode-map (current-local-map)))
198     (define-key mime/viewer-mode-map ";" 'tm-bbdb/edit-notes)
199     (define-key mime/viewer-mode-map ":" 'tm-bbdb/show-sender)
200     ))
201
202 (add-hook 'mime-viewer/define-keymap-hook 'tm-bbdb/define-keys)
203
204
205 ;;; @ for signature.el
206 ;;;
207
208 (defun signature/get-bbdb-sigtype (addr)
209   "Extract sigtype information from BBDB."
210   (let ((record (bbdb-search-simple nil addr)))
211     (and record
212          (bbdb-record-getprop record 'sigtype))
213     ))
214
215 (defun signature/set-bbdb-sigtype (sigtype addr)
216   "Add sigtype information to BBDB."
217   (let* ((bbdb-notice-hook nil)
218          (record (bbdb-annotate-message-sender 
219                   addr t
220                   (bbdb-invoke-hook-for-value 
221                    bbdb/mail-auto-create-p)
222                   t)))
223     (if record
224         (progn
225           (bbdb-record-putprop record 'sigtype sigtype)
226           (bbdb-change-record record nil))
227       )))
228
229 (defun signature/get-sigtype-from-bbdb (&optional verbose)
230   (let* ((to (rfc822/get-field-body "To"))
231          (addr (and to
232                     (car (cdr (mail-extract-address-components to)))))
233          (sigtype (signature/get-bbdb-sigtype addr))
234          return  
235          )
236     (if addr
237         (if verbose
238             (progn
239               (setq return (signature/get-sigtype-interactively sigtype))
240               (if (and (not (string-equal return sigtype))
241                        (y-or-n-p
242                         (format "Register \"%s\" for <%s>? " return addr))
243                        )
244                   (signature/set-bbdb-sigtype return addr)
245                 )
246               return)
247           (or sigtype
248               (signature/get-signature-file-name))
249           ))
250     ))
251
252
253 ;;; @ end
254 ;;;
255
256 (provide 'tm-bbdb)
257
258 (run-hooks 'tm-bbdb-load-hook)
259
260 ;;; end of tm-bbdb.el