tm 7.46.1.
[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 6.0 1996/03/04 11:58:55 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 (require 'tm-view)
33
34
35 ;;; @ mail-extr
36 ;;;
37
38 (defvar tm-bbdb/use-mail-extr t)
39
40 (defun tm-bbdb/extract-address-components (str)
41   (let* ((ret     (rfc822/extract-address-components str))
42          (phrase  (car ret))
43          (address (cdr ret))
44          (methods tm-bbdb/canonicalize-full-name-methods))
45     (while (and phrase methods)
46       (setq phrase  (funcall (car methods) phrase)
47             methods (cdr methods)))
48     (if (string= phrase "")
49         (setq phrase nil))
50     (cons phrase address)
51     ))
52
53 (or tm-bbdb/use-mail-extr
54     (progn
55       (fset 'mail-extract-address-components
56             (symbol-function 'tm-bbdb/extract-address-components))
57       (provide 'mail-extr)
58       ))
59
60
61 ;;; @ bbdb-extract-field-value
62 ;;;
63
64 (or (fboundp 'tm:bbdb-extract-field-value)
65     (progn
66       ;; (require 'bbdb-hooks) ; not provided.
67       ;; (or (fboundp 'bbdb-extract-field-value) ; defined as autoload
68       (or (fboundp 'bbdb-header-start)
69           (load "bbdb-hooks"))
70       (fset 'tm:bbdb-extract-field-value
71             (symbol-function 'bbdb-extract-field-value))
72       (defun bbdb-extract-field-value (field)
73         (let ((value (tm:bbdb-extract-field-value field)))
74           (and value
75                (mime-eword/decode-string value))))
76       ))
77
78
79 ;;; @ full-name canonicalization methods
80 ;;;
81
82 (defun tm-bbdb/canonicalize-spaces (str)
83   (let (dest)
84     (while (string-match "\\s +" str)
85       (setq dest (cons (substring str 0 (match-beginning 0)) dest))
86       (setq str (substring str (match-end 0)))
87       )
88     (or (string= str "")
89         (setq dest (cons str dest)))
90     (setq dest (nreverse dest))
91     (mapconcat 'identity dest " ")
92     ))
93
94 (defun tm-bbdb/canonicalize-dots (str)
95   (let (dest)
96     (while (string-match "\\." str)
97       (setq dest (cons (substring str 0 (match-end 0)) dest))
98       (setq str (substring str (match-end 0)))
99       )
100     (or (string= str "")
101         (setq dest (cons str dest)))
102     (setq dest (nreverse dest))
103     (mapconcat 'identity dest " ")
104     ))
105
106 (defvar tm-bbdb/canonicalize-full-name-methods
107   '(mime-eword/decode-string
108     tm-bbdb/canonicalize-dots
109     tm-bbdb/canonicalize-spaces))
110
111
112 ;;; @ BBDB functions for mime/viewer-mode
113 ;;;
114
115 (defvar tm-bbdb/auto-create-p nil)
116
117 (defun tm-bbdb/update-record (&optional offer-to-create)
118   "Return the record corresponding to the current MIME previewing message.
119 Creating or modifying it as necessary. A record will be created if
120 tm-bbdb/auto-create-p is non-nil, or if OFFER-TO-CREATE is non-nil and
121 the user confirms the creation."
122   (save-excursion
123     (if (and mime::article/preview-buffer
124              (get-buffer mime::article/preview-buffer))
125         (set-buffer mime::article/preview-buffer))
126     (if bbdb-use-pop-up
127         (tm-bbdb/pop-up-bbdb-buffer offer-to-create)
128     (let ((from (rfc822/get-field-body "From")))
129       (if (or (null from)
130               (string-match (bbdb-user-mail-names)
131                             (car
132                              (cdr
133                               ;; (tm-bbdb/extract-address-components from)
134                               (mail-extract-address-components from)
135                               ))))
136           (setq from (or (rfc822/get-field-body "To")
137                          from)))
138       (if from
139           (bbdb-annotate-message-sender
140            from t
141            (or (bbdb-invoke-hook-for-value tm-bbdb/auto-create-p)
142                offer-to-create)
143            offer-to-create))
144       ))))
145
146 (defun tm-bbdb/annotate-sender (string)
147   "Add a line to the end of the Notes field of the BBDB record 
148 corresponding to the sender of this message."
149   (interactive
150    (list (if bbdb-readonly-p
151              (error "The Insidious Big Brother Database is read-only.")
152            (read-string "Comments: "))))
153   (bbdb-annotate-notes (tm-bbdb/update-record t) string))
154
155 (defun tm-bbdb/edit-notes (&optional arg)
156   "Edit the notes field or (with a prefix arg) a user-defined field
157 of the BBDB record corresponding to the sender of this message."
158   (interactive "P")
159   (let ((record (or (tm-bbdb/update-record t)
160                     (error ""))))
161     (bbdb-display-records (list record))
162     (if arg
163         (bbdb-record-edit-property record nil t)
164       (bbdb-record-edit-notes record t))))
165
166 (defun tm-bbdb/show-sender ()
167   "Display the contents of the BBDB for the sender of this message.
168 This buffer will be in bbdb-mode, with associated keybindings."
169   (interactive)
170   (let ((record (tm-bbdb/update-record t)))
171     (if record
172         (bbdb-display-records (list record))
173         (error "unperson"))))
174
175 (defun tm-bbdb/pop-up-bbdb-buffer (&optional offer-to-create)
176   "Make the *BBDB* buffer be displayed along with the MIME preview window(s),
177 displaying the record corresponding to the sender of the current message."
178   (bbdb-pop-up-bbdb-buffer
179     (function (lambda (w)
180       (let ((b (current-buffer)))
181         (set-buffer (window-buffer w))
182         (prog1 (eq major-mode 'mime/viewer-mode)
183           (set-buffer b))))))
184   (let ((bbdb-gag-messages t)
185         (bbdb-use-pop-up nil)
186         (bbdb-electric-p nil))
187     (let ((record (tm-bbdb/update-record offer-to-create))
188           (bbdb-elided-display (bbdb-pop-up-elided-display))
189           (b (current-buffer)))
190       (bbdb-display-records (if record (list record) nil))
191       (set-buffer b)
192       record)))
193
194
195 ;;; @ end
196 ;;;
197
198 (provide 'tm-bbdb)
199
200 (run-hooks 'tm-bbdb-load-hook)
201
202 ;;; end of tm-bbdb.el