e8e662a0621360ff7e52cf9ac85c54666878ff4a
[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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;  - How to use
29 ;;    1. bytecompile this file and copy it to the apropriate directory.
30 ;;    2. put the following lines to your ~/.emacs:
31 ;;             (add-hook 'mu-cite-load-hook
32 ;;                       (function
33 ;;                        (lambda ()
34 ;;                          (require 'mu-bbdb)
35 ;;                          )))
36
37 ;;; Code:
38
39 (require 'mu-cite)
40 (require 'bbdb)
41
42 (defvar mu-bbdb-history nil)
43
44
45 ;;; @ BBDB interface
46 ;;;
47
48 (defun mu-bbdb-get-attr (addr)
49   "Extract attribute information from BBDB."
50   (let ((record (bbdb-search-simple nil addr)))
51     (and record
52          (bbdb-record-getprop record 'attribution))))
53
54 (defun mu-bbdb-set-attr (attr addr)
55   "Add attribute information to BBDB."
56   (let* ((bbdb-notice-hook nil)
57          (record (bbdb-annotate-message-sender
58                   addr t
59                   (bbdb-invoke-hook-for-value
60                    bbdb/mail-auto-create-p)
61                   t)))
62     (if record
63         (progn
64           (bbdb-record-putprop record 'attribution attr)
65           (bbdb-change-record record nil)
66           ))))
67
68
69 ;;; @ methods
70 ;;;
71
72 ;;;###autoload
73 (defun mu-bbdb-get-prefix-method ()
74   "A mu-cite method to return a prefix from BBDB or \">\".
75 If an `attribution' value is found in BBDB, the value is returned.
76 Otherwise \">\" is returned.
77
78 Notice that please use (mu-cite-get-value 'bbdb-prefix)
79 instead of call the function directly."
80   (or (mu-bbdb-get-attr (mu-cite-get-value 'address))
81       ">"))
82
83 ;;;###autoload
84 (defun mu-bbdb-get-prefix-register-method ()
85   "A mu-cite method to return a prefix from BBDB or register it.
86 If an `attribution' value is found in BBDB, the value is returned.
87 Otherwise the function requests a prefix from a user.  The prefix will
88 be registered to BBDB if the user wants it.
89
90 Notice that please use (mu-cite-get-value 'bbdb-prefix-register)
91 instead of call the function directly."
92   (let ((addr (mu-cite-get-value 'address)))
93     (or (mu-bbdb-get-attr addr)
94         (let ((return
95                (read-string "Citation name? "
96                             (or (mu-cite-get-value 'x-attribution)
97                                 (mu-cite-get-value 'full-name))
98                             'mu-bbdb-history)))
99           (if (and (not (string-equal return ""))
100                    (y-or-n-p (format "Register \"%s\"? " return)))
101               (mu-bbdb-set-attr return addr))
102           return))))
103
104 ;;;###autoload
105 (defun mu-bbdb-get-prefix-register-verbose-method ()
106   "A mu-cite method to return a prefix using BBDB.
107
108 In this method, a user must specify a prefix unconditionally.  If an
109 `attribution' value is found in BBDB, the value is used as a initial
110 value to input the prefix.  The prefix will be registered to BBDB if
111 the user wants it.
112
113 Notice that please use (mu-cite-get-value 'bbdb-prefix-register-verbose)
114 instead of call the function directly."
115   (let* ((addr (mu-cite-get-value 'address))
116          (attr (mu-bbdb-get-attr addr))
117          (return (read-string "Citation name? "
118                               (or attr
119                                   (mu-cite-get-value 'x-attribution)
120                                   (mu-cite-get-value 'full-name))
121                               'mu-bbdb-history)))
122     (if (and (not (string-equal return ""))
123              (not (string-equal return attr))
124              (y-or-n-p (format "Register \"%s\"? " return)))
125         (mu-bbdb-set-attr return addr))
126     return))
127
128
129 ;;; @ end
130 ;;;
131
132 (provide 'mu-bbdb)
133
134 (run-hooks 'mu-bbdb-load-hook)
135
136 ;;; mu-bbdb.el ends here