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