Initial Commit
[packages] / xemacs-packages / bbdb / lisp / bbdb-reportmail.el
1 ;; bbdb-reportmail.el --- Hooks the Insidious Big Brother Database
2 ;;                        into the Reportmail package
3
4 ;; Copyright (C) 1997 Christopher Kline
5
6 ;; Author: Christopher Kline <ckline@media.mit.edu>
7 ;; Maintainer: Christopher Kline <ckline@media.mit.edu>
8 ;; Version: 1.01
9 ;; Created: 25 Jun 1997
10 ;; Date: 26 Jun 1997
11
12 ;; Bbdb-reportmail is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; Bbdb-reportmail is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Bbdb-reportmail advises the reportmail package function
30 ;; display-time-get-field so that it attempts to replace the reported
31 ;; "from" and "to" fields with the name field (or mail-name, if it
32 ;; exists) of the corresponding BBDB record, if such a correspondence
33 ;; can be made.
34
35 ;; To use this, simply add the following lines AFTER you load in your
36 ;; bbdb, set bbdb variables, etc.
37 ;;
38 ;;  (bbdb-insinuate-reportmail)
39 ;;
40 ;; (A require used to be necessary - it is no longer needed as long as
41 ;; bbdb-insinuate-reportmail is called)
42
43 ;;; History:
44
45 ;; v1.01 26 June 1997
46 ;;       Fixed the advice so that if we are the message recipient, do
47 ;;       nothing so that display-time-process-new-mail will correctly
48 ;;       trap this case.
49 ;;
50 ;; v1.00 26 June 1997
51 ;;       Initial release.
52
53 ;;
54 ;; $Id: bbdb-reportmail.el,v 1.7 2007-02-23 20:24:08 fenk Exp $
55 ;;
56
57 ;;-----------------------------------------------------------------------
58
59 (require 'bbdb)
60 (require 'reportmail)
61 (require 'advice)
62 (require 'mail-extr)
63
64 (defun bbdb/reportmail-alternate-full-name (address)
65   (if address
66       (let ((entry (bbdb-search-simple nil address)))
67     (if entry
68         (or (bbdb-record-getprop entry 'mail-name)
69         (bbdb-record-name entry))))))
70
71 (defadvice display-time-get-field
72   (around bbdb/reportmail-hack-display-time-get-field disable activate)
73   "Advises the `display-time-get-field' function in the reportmail package.
74 If the field is \"from\" or \"to\", it tries to replace the value of the field
75 with the name field of the corresponding BBDB entry, if one can be found.
76
77 If no corresponding record can be found, the field value is left unaltered."
78   (let (gf-field)
79     ;; Get the original argument to display-time-get-field
80     (setq gf-field (ad-get-arg 0))
81     ;; Call the original display-time-get-field
82     ad-do-it
83     (if (or (string= gf-field "To") (string= gf-field "From"))
84     (setq ad-return-value
85           (or
86            ;; If this message is to me, then do nothing so
87            ;; reportmail can trap this case in
88            ;; display-time-process-new-mail
89            (if (display-time-member ad-return-value
90                       display-time-my-addresses)
91            ad-return-value
92          nil)
93            ;; Is the sender/recipient in our BBDB?
94            (bbdb/reportmail-alternate-full-name
95         (car (cdr (mail-extract-address-components ad-return-value))))
96            ;; Can't find sender/recipient in BBDB; do nothing.
97            ad-return-value)
98           ))))
99
100 ;;;###autoload
101 (defun bbdb-insinuate-reportmail ()
102   "Call this function to hook BBDB into reportmail."
103   (ad-enable-advice 'display-time-get-field 'around
104             'bbdb/reportmail-hack-display-time-get-field)
105   (ad-activate 'display-time-get-field)
106   (message "Insinuated BBDB into Reportmail.")
107 )
108
109 (provide 'bbdb-reportmail)
110
111