b208d28d8dfea2c1cf38fd09434ffcffc317c3a7
[elisp/tm.git] / tm-bbdb.el
1 ;;;
2 ;;; tm-bbdb.el --- tm shared module for BBDB
3 ;;;
4 ;;; Copyright (C) 1995 KOBAYASHI Shuhei <shuhei@cmpt01.phys.tohoku.ac.jp>
5 ;;;
6 ;;; Author: KOBAYASHI Shuhei <shuhei@cmpt01.phys.tohoku.ac.jp>
7 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Version: $Id: tm-bbdb.el,v 3.0 1996/01/24 02:20:25 morioka Exp $
9 ;;; Keywords: mail, news, MIME, multimedia, multilingual, BBDB
10 ;;;
11 ;;; This file is part of tm (Tools for MIME).
12 ;;;
13 ;;; This program is free software; you can redistribute it and/or
14 ;;; modify it under the terms of the GNU General Public License as
15 ;;; published by the Free Software Foundation; either version 2, or
16 ;;; (at your option) any later version.
17 ;;;
18 ;;; This program is distributed in the hope that it will be useful,
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;;; General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with This program.  If not, write to the Free Software
25 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;;
27 ;;; Code:
28
29 (require 'bbdb)
30 (require 'tl-822)
31 (require 'tm-ew-d)
32
33
34 ;;; @ mail-extr
35 ;;;
36
37 (defvar tm-bbdb/use-mail-extr t)
38
39 (defun tm-bbdb/extract-address-components (str)
40   (let* ((ret     (rfc822/extract-address-components str))
41          (phrase  (car ret))
42          (address (cdr ret))
43          (methods tm-bbdb/canonicalize-full-name-methods))
44     (while (and phrase methods)
45       (setq phrase  (funcall (car methods) phrase)
46             methods (cdr methods)))
47     (cons phrase address)
48     ))
49
50 (or tm-bbdb/use-mail-extr
51     (progn
52       (fset 'mail-extract-address-components
53             (symbol-function 'tm-bbdb/extract-address-components))
54       (provide 'mail-extr)
55       ))
56
57
58 ;;; @ bbdb-extract-field-value
59 ;;;
60
61 (or (fboundp 'tm:bbdb-extract-field-value)
62     (progn
63       ;; (require 'bbdb-hooks) ; not provided.
64       (or (fboundp 'bbdb-extract-field-value)
65           (load "bbdb-hooks"))
66       (fset 'tm:bbdb-extract-field-value
67             (symbol-function 'bbdb-extract-field-value))
68       (defun bbdb-extract-field-value (field)
69         (let ((value (rfc822/get-field-body field)))
70           (and value
71                (mime-eword/decode-string value))))
72       ))
73
74
75 ;;; @ full-name canonicalization methods
76 ;;;
77
78 (defun tm-bbdb/canonicalize-spaces (str)
79   (let (dest)
80     (while (string-match "\\s +" str)
81       (setq dest (cons (substring str 0 (match-beginning 0)) dest))
82       (setq str (substring str (match-end 0)))
83       )
84     (or (string= str "")
85         (setq dest (cons str dest)))
86     (setq dest (nreverse dest))
87     (mapconcat 'identity dest " ")
88     ))
89
90 (defun tm-bbdb/canonicalize-dots (str)
91   (let (dest)
92     (while (string-match "\\." str)
93       (setq dest (cons (substring str 0 (match-end 0)) dest))
94       (setq str (substring str (match-end 0)))
95       )
96     (or (string= str "")
97         (setq dest (cons str dest)))
98     (setq dest (nreverse dest))
99     (mapconcat 'identity dest " ")
100     ))
101
102 (defvar tm-bbdb/canonicalize-full-name-methods nil)
103
104 (setq tm-bbdb/canonicalize-full-name-methods
105       '(mime-eword/decode-string
106         tm-bbdb/canonicalize-dots
107         tm-bbdb/canonicalize-spaces))
108
109
110 ;;; @ BBDB functions for mime/viewer-mode
111 ;;;
112
113 (defvar tm-bbdb/auto-create-p nil)
114
115 (defun tm-bbdb/update-record (&optional offer-to-create)
116   "Return the record corresponding to the current MIME previewing message.
117 Creating or modifying it as necessary. A record will be created if
118 tm-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
119 the user confirms the creation."
120   (save-excursion
121     (and mime::article/preview-buffer
122          (get-buffer mime::article/preview-buffer)
123          (set-buffer mime::article/preview-buffer))
124     (if bbdb-use-pop-up
125         (tm-bbdb/pop-up-bbdb-buffer offer-to-create)
126     (let ((from (rfc822/get-field-body "From")))
127       (if (or (null from)
128               (string-match (bbdb-user-mail-names)
129                             (car
130                              (cdr
131                               ;; (tm-bbdb/extract-address-components from)
132                               (mail-extract-address-components from)
133                               ))))
134           (setq from (or (rfc822/get-field-body "To")
135                          from)))
136       (if from
137           (bbdb-annotate-message-sender
138            from t
139            (or (bbdb-invoke-hook-for-value tm-bbdb/auto-create-p)
140                offer-to-create)
141            offer-to-create))
142       ))))
143
144 (defun tm-bbdb/annotate-sender (string)
145   "Add a line to the end of the Notes field of the BBDB record 
146 corresponding to the sender of this message."
147   (interactive
148    (list (if bbdb-readonly-p
149              (error "The Insidious Big Brother Database is read-only.")
150            (read-string "Comments: "))))
151   (bbdb-annotate-notes (tm-bbdb/update-record t) string))
152
153 (defun tm-bbdb/edit-notes (&optional arg)
154   "Edit the notes field or (with a prefix arg) a user-defined field
155 of the BBDB record corresponding to the sender of this message."
156   (interactive "P")
157   (let ((record (or (tm-bbdb/update-record t)
158                     (error ""))))
159     (bbdb-display-records (list record))
160     (if arg
161         (bbdb-record-edit-property record nil t)
162       (bbdb-record-edit-notes record t))))
163
164 (defun tm-bbdb/show-sender ()
165   "Display the contents of the BBDB for the sender of this message.
166 This buffer will be in bbdb-mode, with associated keybindings."
167   (interactive)
168   (let ((record (tm-bbdb/update-record t)))
169     (if record
170         (bbdb-display-records (list record))
171         (error "unperson"))))
172
173 (defun tm-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
174   "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
175 displaying the record corresponding to the sender of the current message."
176   (bbdb-pop-up-bbdb-buffer
177     (function (lambda (w)
178       (let ((b (current-buffer)))
179         (set-buffer (window-buffer w))
180         (prog1 (eq major-mode 'mime/viewer-mode)
181           (set-buffer b))))))
182   (let ((bbdb-gag-messages t)
183         (bbdb-use-pop-up nil)
184         (bbdb-electric-p nil))
185     (let ((record (tm-bbdb/update-record offer-to-create))
186           (bbdb-elided-display (bbdb-pop-up-elided-display))
187           (b (current-buffer)))
188       (bbdb-display-records (if record (list record) nil))
189       (set-buffer b)
190       record)))
191
192
193 ;;; @ end
194 ;;;
195
196 (provide 'tm-bbdb)
197
198 (run-hooks 'tm-bbdb-load-hook)
199
200 ;;; end of tm-bbdb.el