Date UT
[elisp/mu-cite.git] / mu-bbdb.el
1 ;;; mu-bbdb.el --- registration feature of mu-cite using BBDB
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Maintainer: Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: BBDB, citation, mail, news
8
9 ;; This file is part of MU (Message Utilities).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Code:
27
28 (require 'mu-cite)
29 (require 'bbdb)
30
31 (defvar mu-bbdb-history nil)
32
33
34 ;;; @ BBDB interface
35 ;;;
36
37 (defun mu-bbdb-get-attr (addr)
38   "Extract attribute information from BBDB."
39   (let ((record (bbdb-search-simple nil addr)))
40     (if record
41         (bbdb-record-getprop record 'attribution))))
42
43 (defun mu-bbdb-set-attr (attr addr)
44   "Add attribute information to BBDB."
45   (let* ((bbdb-notice-hook nil)
46          (record (bbdb-annotate-message-sender
47                   addr t
48                   (bbdb-invoke-hook-for-value
49                    bbdb/mail-auto-create-p)
50                   t)))
51     (if record
52         (progn
53           (bbdb-record-putprop record 'attribution attr)
54           (bbdb-change-record record nil)))))
55
56
57 ;;; @ methods
58 ;;;
59
60 ;;;###autoload
61 (defun mu-bbdb-get-prefix-method ()
62   "A mu-cite method to return a prefix from BBDB or \">\".
63 If an `attribution' value is found in BBDB, the value is returned.
64 Otherwise \">\" is returned.
65
66 Notice that please use (mu-cite-get-value 'bbdb-prefix)
67 instead of call the function directly."
68   (or (mu-bbdb-get-attr (mu-cite-get-value 'address))
69       ">"))
70
71 ;;;###autoload
72 (defun mu-bbdb-get-prefix-register-method ()
73   "A mu-cite method to return a prefix from BBDB or register it.
74 If an `attribution' value is found in BBDB, the value is returned.
75 Otherwise the function requests a prefix from a user.  The prefix will
76 be registered to BBDB if the user wants it.
77
78 Notice that please use (mu-cite-get-value 'bbdb-prefix-register)
79 instead of call the function directly."
80   (let ((addr (mu-cite-get-value 'address)))
81     (or (mu-bbdb-get-attr addr)
82         (let* ((minibuffer-allow-text-properties nil)
83                (return
84                 (mu-cite-remove-text-properties
85                  (read-string "Citation name? "
86                               (or (mu-cite-get-value 'x-attribution)
87                                   (mu-cite-get-value 'x-cite-me)
88                                   (mu-cite-get-value 'full-name))
89                               'mu-bbdb-history))))
90           (if (and (not (string-equal return ""))
91                    (y-or-n-p (format "Register \"%s\"? " return)))
92               (mu-bbdb-set-attr return addr))
93           return))))
94
95 ;;;###autoload
96 (defun mu-bbdb-get-prefix-register-verbose-method ()
97   "A mu-cite method to return a prefix using BBDB.
98
99 In this method, a user must specify a prefix unconditionally.  If an
100 `attribution' value is found in BBDB, the value is used as a initial
101 value to input the prefix.  The prefix will be registered to BBDB if
102 the user wants it.
103
104 Notice that please use (mu-cite-get-value 'bbdb-prefix-register-verbose)
105 instead of call the function directly."
106   (let* ((addr (mu-cite-get-value 'address))
107          (attr (mu-bbdb-get-attr addr))
108          (minibuffer-allow-text-properties nil)
109          (return (mu-cite-remove-text-properties
110                   (read-string "Citation name? "
111                                (or attr
112                                    (mu-cite-get-value 'x-attribution)
113                                    (mu-cite-get-value 'x-cite-me)
114                                    (mu-cite-get-value 'full-name))
115                                'mu-bbdb-history))))
116     (if (and (not (string-equal return ""))
117              (not (string-equal return attr))
118              (y-or-n-p (format "Register \"%s\"? " return)))
119         (mu-bbdb-set-attr return addr))
120     return))
121
122
123 ;;; @ end
124 ;;;
125
126 (provide 'mu-bbdb)
127
128 (run-hooks 'mu-bbdb-load-hook)
129
130 ;;; mu-bbdb.el ends here