summaryrefslogtreecommitdiff
path: root/lisp/bbdb-gnus.el
blob: 417f34da300741bd547af941ac687470b97107e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
;;; -*- Mode:Emacs-Lisp -*-

;;; This file is part of the Insidious Big Brother Database (aka BBDB),
;;; copyright (c) 1991, 1992, 1993 Jamie Zawinski <jwz@netscape.com>.
;;; Interface to GNUS version 3.12 or greater.  See bbdb.texinfo.

;;; The Insidious Big Brother Database is free software; you can redistribute
;;; it and/or modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 1, or (at your
;;; option) any later version.
;;;
;;; BBDB is distributed in the hope that it will be useful, but WITHOUT ANY
;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;;; FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
;;; details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Emacs; see the file COPYING.  If not, write to
;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;
;; $Id$
;;

(require 'bbdb)
(require 'gnus)

;;;###autoload
(defun bbdb/gnus-update-record (&optional offer-to-create)
  "returns the record corresponding to the current GNUS message, creating
or modifying it as necessary.  A record will be created if
bbdb/news-auto-create-p is non-nil, or if OFFER-TO-CREATE is true and
the user confirms the creation."
    (set-buffer gnus-article-buffer)
    (save-restriction
      (widen)
      ;;(gnus-article-show-all-headers)
      (narrow-to-region (point-min)
                        (progn (goto-char (point-min))
                   (if (search-forward "\n\n" nil 'force)
                   (- (point) 2)
                 (point))))
      (let ((from (mail-fetch-field "from"))
            name net)
        (if (or (null from)
                (string-match (bbdb-user-mail-names)
                              (mail-strip-quoted-names from)))
            ;; if logged-in user sent this, use recipients.
            (setq from (or (mail-fetch-field "to") from)))
      (if from
      (bbdb-annotate-message-sender from t
                    (or (bbdb-invoke-hook-for-value
                         bbdb/news-auto-create-p)
                        offer-to-create)
                    offer-to-create)))))

;;;###autoload
(defun bbdb/gnus-annotate-sender (string &optional replace)
  "Add a line to the end of the Notes field of the BBDB record
corresponding to the sender of this message.  If REPLACE is non-nil,
replace the existing notes entry (if any)."
  (interactive (list (if bbdb-readonly-p
             (error "The Insidious Big Brother Database is read-only.")
             (read-string "Comments: "))))
  (gnus-summary-select-article)
  (bbdb-annotate-notes (bbdb/gnus-update-record t) string 'notes replace))

(defun bbdb/gnus-edit-notes (&optional arg)
  "Edit the notes field or (with a prefix arg) a user-defined field
of the BBDB record corresponding to the sender of this message."
  (interactive "P")
  (gnus-summary-select-article)
  (let ((record (or (bbdb/gnus-update-record t) (error ""))))
    (bbdb-display-records (list record))
    (if arg
    (bbdb-record-edit-property record nil t)
      (bbdb-record-edit-notes record t))))

;;;###autoload
(defun bbdb/gnus-show-sender ()
  "Display the contents of the BBDB for the sender of this message.
This buffer will be in bbdb-mode, with associated keybindings."
  (interactive)
  (gnus-summary-select-article)
  (let ((record (bbdb/gnus-update-record t)))
    (if record
    (bbdb-display-records (list record))
    (error "unperson"))))


(defun bbdb/gnus-pop-up-bbdb-buffer (&optional offer-to-create)
  "Make the *BBDB* buffer be displayed along with the GNUS windows,
displaying the record corresponding to the sender of the current message."
  (let ((bbdb-gag-messages t)
    (record (bbdb/gnus-update-record offer-to-create))
    (bbdb-electric-p nil))

    (if bbdb-use-pop-up
    (let ((bbdb-elided-display (bbdb-pop-up-elided-display))
          (b (current-buffer)))
      ;; display the bbdb buffer iff there is a record for this article.
      (cond (record
         (bbdb-pop-up-bbdb-buffer
          (function (lambda (w)
                  (let ((b (current-buffer)))
                (set-buffer (window-buffer w))
                (prog1 (or (eq major-mode 'gnus-Article-mode)
                       (eq major-mode 'gnus-article-mode))
                  (set-buffer b)))))))
        (t
         (or bbdb-inside-electric-display
             (not (get-buffer-window bbdb-buffer-name))
             (let (w)
               (delete-other-windows)
               (if (assq 'article gnus-window-configuration) ; 3.15+
               (gnus-configure-windows 'article)
             (gnus-configure-windows 'SelectArticle))
               (if (setq w (get-buffer-window
                    (if (boundp 'gnus-summary-buffer)
                    gnus-summary-buffer
                      gnus-Subject-buffer)))
               (select-window w))
               ))))
      (set-buffer b)))
    (if record (bbdb-display-records (list record)))
    record))

;;
;; Announcing BBDB entries in the summary buffer
;;

(defcustom bbdb/gnus-lines-and-from-length 18
  "*The number of characters used to display From: info in Gnus, if you have
set gnus-optional-headers to 'bbdb/gnus-lines-and-from."
  :group 'bbdb-mua-specific-gnus
  :type 'integer)

(defcustom bbdb/gnus-summary-mark-known-posters t
  "*If t, mark messages created by people with records in the BBDB.
In GNUS, this marking will take place in the subject list (assuming
`gnus-optional-headers' contains `bbdb/gnus-lines-and-from').  In Gnus, the
marking will take place in the Summary buffer if the format code defined by
`bbdb/gnus-summary-user-format-letter' is used in `gnus-summary-line-format'.
This variable has no effect on the marking controlled by
`bbdb/gnus-summary-in-bbdb-format-letter'."
  :group 'bbdb-mua-specific-gnus
  :type '(choice (const :tag "Mark known posters" t)
         (const :tag "Do not mark known posters" nil)))
(defvaralias 'bbdb/gnus-mark-known-posters
  'bbdb/gnus-summary-mark-known-posters)

(defcustom bbdb/gnus-summary-known-poster-mark "+"
  "This is the default character to prefix author names with if
bbdb/gnus-summary-mark-known-posters is t.  If the poster's record has
an entry in the field named by bbdb-message-marker-field, then that will
be used instead."
  :group 'bbdb-mua-specific-gnus
  :type 'character)

(defcustom bbdb/gnus-summary-show-bbdb-names t
  "*If both this variable and `bbdb/gnus-summary-prefer-real-names' are true,
then for messages from authors who are in your database, the name
displayed will be the primary name in the database, rather than the
one in the From line of the message.  This doesn't affect the names of
people who aren't in the database, of course.  (`gnus-optional-headers'
must be `bbdb/gnus-lines-and-from' for GNUS users.)"
  :group 'bbdb-mua-specific-gnus
  :type 'boolean)
(defvaralias 'bbdb/gnus-header-show-bbdb-names
  'bbdb/gnus-summary-show-bbdb-names)

(defcustom bbdb/gnus-summary-prefer-bbdb-data t
  "If t, then for posters who are in our BBDB, replace the information
provided in the From header with data from the BBDB."
  :group 'bbdb-mua-specific-gnus
  :type 'boolean)

(defcustom bbdb/gnus-summary-prefer-real-names t
  "If t, then display the poster's name from the BBDB if we have one,
otherwise display his/her primary net address if we have one.  If it
is set to the symbol bbdb, then real names will be used from the BBDB
if present, otherwise the net address in the post will be used.  If
bbdb/gnus-summary-prefer-bbdb-data is nil, then this has no effect.
See `bbdb/gnus-lines-and-from' for GNUS users, or
`bbdb/gnus-summary-user-format-letter' for Gnus users."
  :group 'bbdb-mua-specific-gnus
  :type '(choice (const :tag "Prefer real names" t)
         (const :tag "Prefer network addresses" nil)))
(defvaralias 'bbdb/gnus-header-prefer-real-names
  'bbdb/gnus-summary-prefer-real-names)

(defcustom bbdb/gnus-summary-user-format-letter "B"
  "This is the gnus-user-format-function- that will be used to insert
the information from the BBDB in the summary buffer (using
`bbdb/gnus-summary-get-author').  This format code is meant to replace
codes that insert sender names or addresses (like %A or %n). Unless
you've already got other code using user format B, you might as well
stick with the default.  Additionally, if the value of this variable
is nil, no format function will be installed for
`bbdb/gnus-summary-get-author'.  See also
`bbdb/gnus-summary-in-bbdb-format-letter', which installs a format
code for `bbdb/gnus-summary-author-in-bbdb'"
  :group 'bbdb-mua-specific-gnus
  :type 'character)

(defcustom bbdb/gnus-summary-in-bbdb-format-letter "b"
  "This is the gnus-user-format-function- that will be used to insert
`bbdb/gnus-summary-known-poster-mark' (using
`bbdb/gnus-summary-author-in-bbdb') if the poster is in the BBDB, and
\" \" if not.  If the value of this variable is nil, no format code
will be installed for `bbdb/gnus-summary-author-in-bbdb'.  See also
`bbdb/gnus-summary-user-format-letter', which installs a format code
for `bbdb/gnus-summary-get-author'."
  :group 'bbdb-mua-specific-gnus
  :type 'character)

(defcustom bbdb-message-marker-field 'mark-char
  "*The field whose value will be used to mark messages by this user in Gnus."
  :group 'bbdb-mua-specific-gnus
  :type 'symbol)

;;;###autoload
(defun bbdb/gnus-lines-and-from (header)
  "Useful as the value of `gnus-optional-headers' in *GNUS* (not Gnus).
NOTE: This variable no longer seems to be present in Gnus.  It seems
to have been replaced by `message-default-headers', which only takes
strings.  In the future this should change."
  (let* ((length bbdb/gnus-lines-and-from-length)
     (lines (nntp-header-lines header))
     (from (nntp-header-from header))
     (data (and (or bbdb/gnus-summary-mark-known-posters
            bbdb/gnus-summary-show-bbdb-names)
            (condition-case ()
            (mail-extract-address-components from)
              (error nil))))
     (name (car data))
     (net (car (cdr data)))
     (record (and data
              (bbdb-search-simple name
               (if (and net bbdb-canonicalize-net-hook)
               (bbdb-canonicalize-address net)
             net))))
     string L)

    (if (and record name (member (downcase name) (bbdb-record-net record)))
    ;; bogon!
    (setq record nil))

    (setq name
      (or (and bbdb/gnus-summary-prefer-bbdb-data
           (or (and bbdb/gnus-summary-prefer-real-names
                (and record (bbdb-record-name record)))
               (and record (bbdb-record-net record)
                (nth 0 (bbdb-record-net record)))))
          (and bbdb/gnus-summary-prefer-real-names
           (or (and (equal bbdb/gnus-summary-prefer-real-names 'bbdb)
                net)
               name))
          net from "**UNKNOWN**"))
      ;; GNUS can't cope with extra square-brackets appearing in the summary.
      (if (and name (string-match "[][]" name))
      (progn (setq name (copy-sequence name))
         (while (string-match "[][]" name)
           (aset name (match-beginning 0) ? ))))
      (setq string (format "%s%3d:%s"
               (if (and record bbdb/gnus-summary-mark-known-posters)
                   (or (bbdb-record-getprop
                    record bbdb-message-marker-field)
                   "*")
                 " ")
               lines (or name from))
        L (length string))
      (cond ((> L length) (substring string 0 length))
        ((< L length) (concat string (make-string (- length L) ? )))
        (t string))))

(defun bbdb/gnus-summary-get-author (header)
  "Given a Gnus message header, returns the appropriate piece of
information to identify the author in a Gnus summary line, depending on
the settings of the various configuration variables.  See the
documentation for the following variables for more details:
  `bbdb/gnus-summary-mark-known-posters'
  `bbdb/gnus-summary-known-poster-mark'
  `bbdb/gnus-summary-prefer-bbdb-data'
  `bbdb/gnus-summary-prefer-real-names'
This function is meant to be used with the user function defined in
  `bbdb/gnus-summary-user-format-letter'"
  (let* ((from (mail-header-from header))
     (data (and bbdb/gnus-summary-show-bbdb-names
            (condition-case ()
            (mail-extract-address-components from)
              (error nil))))
     (name (car data))
     (net (car (cdr data)))
     (record (and data
              (bbdb-search-simple name
               (if (and net bbdb-canonicalize-net-hook)
               (bbdb-canonicalize-address net)
             net)))))
    (if (and record name (member (downcase name) (bbdb-record-net record)))
    ;; bogon!
    (setq record nil))
    (setq name
      (or (and bbdb/gnus-summary-prefer-bbdb-data
           (or (and bbdb/gnus-summary-prefer-real-names
                (and record (bbdb-record-name record)))
               (and record (bbdb-record-net record)
                (nth 0 (bbdb-record-net record)))))
          (and bbdb/gnus-summary-prefer-real-names
           (or (and (equal bbdb/gnus-summary-prefer-real-names 'bbdb)
                net)
               name))
          net from "**UNKNOWN**"))
    (format "%s%s"
        (or (and record bbdb/gnus-summary-mark-known-posters
             (or (bbdb-record-getprop
              record bbdb-message-marker-field)
             bbdb/gnus-summary-known-poster-mark))
        " ")
        name)))

;; DEBUG: (bbdb/gnus-summary-author-in-bbdb "From: simmonmt@acm.org")
(defun bbdb/gnus-summary-author-in-bbdb (header)
  "Given a Gnus message header, returns a mark if the poster is in the BBDB, \" \" otherwise.  The mark itself is the value of the field indicated by `bbdb-message-marker-field' (`mark-char' by default) if the indicated field is in the poster's record, and `bbdb/gnus-summary-known-poster-mark' otherwise."
  (let* ((from (mail-header-from header))
     (data (condition-case ()
           (mail-extract-address-components from)
         (error nil)))
     (name (car data))
     (net (cadr data))
     record)
    (if (and data
         (setq record
           (bbdb-search-simple
            name (if (and net bbdb-canonicalize-net-hook)
                 (bbdb-canonicalize-address net)
               net))))
    (or (bbdb-record-getprop
         record bbdb-message-marker-field)
        bbdb/gnus-summary-known-poster-mark) " ")))

;;
;; Gnus-specific snarfing (see also bbdb-snarf.el)
;;

;;;###autoload
(defun bbdb/gnus-snarf-signature ()
  "Snarf signature from the corresponding *Article* buffer."
  (interactive)
  (save-excursion
    (or gnus-article-buffer (error "Not in Gnus!"))
    (set-buffer gnus-article-buffer)
    (save-restriction
      (or (gnus-article-narrow-to-signature) (error "No signature!"))
      (bbdb-snarf-region (point-min) (point-max)))))

;;
;; Scoring
;;

(defcustom bbdb/gnus-score-field 'gnus-score
  "This variable contains the name of the BBDB field which should be
checked for a score to add to the net addresses in the same record."
  :group 'bbdb-mua-specific-gnus-scoring
  :type 'symbol)

(defcustom bbdb/gnus-score-default nil
  "If this is set, then every net address in the BBDB that does not have
an associated score field will be assigned this score.  A value of nil
implies a default score of zero."
  :group 'bbdb-mua-specific-gnus-scoring
  :type '(choice (const :tag "Do not assign default score")
         (integer :tag "Assign this default score" 0)))

(defvar bbdb/gnus-score-default-internal nil
  "Internal variable for detecting changes to
`bbdb/gnus-score-default'.  You should not set this variable directly -
set `bbdb/gnus-score-default' instead.")

(defvar bbdb/gnus-score-alist nil
  "The text version of the scoring structure returned by
bbdb/gnus-score.  This is built automatically from the BBDB.")

(defvar bbdb/gnus-score-rebuild-alist t
  "Set to t to rebuild bbdb/gnus-score-alist on the next call to
bbdb/gnus-score.  This will be set automatically if you change a BBDB
record which contains a gnus-score field.")

(defun bbdb/gnus-score-invalidate-alist (rec)
  "This function is called through `bbdb-after-change-hook',
and sets `bbdb/gnus-score-rebuild-alist' to t if the changed
record contains a gnus-score field."
  (if (bbdb-record-getprop rec bbdb/gnus-score-field)
      (setq bbdb/gnus-score-rebuild-alist t)))

;;;###autoload
(defun bbdb/gnus-score (group)
  "This returns a score alist for GNUS.  A score pair will be made for
every member of the net field in records which also have a gnus-score
field.  This allows the BBDB to serve as a supplemental global score
file, with the advantage that it can keep up with multiple and changing
addresses better than the traditionally static global scorefile."
  (list (list
   (condition-case nil
       (read (bbdb/gnus-score-as-text group))
     (error (setq bbdb/gnus-score-rebuild-alist t)
        (message "Problem building BBDB score table.")
        (ding) (sit-for 2)
        nil)))))

(defun bbdb/gnus-score-as-text (group)
  "Returns a SCORE file format string built from the BBDB."
  (cond ((or (cond ((/= (or bbdb/gnus-score-default 0)
            (or bbdb/gnus-score-default-internal 0))
            (setq bbdb/gnus-score-default-internal
              bbdb/gnus-score-default)
            t))
        (not bbdb/gnus-score-alist)
        bbdb/gnus-score-rebuild-alist)
    (setq bbdb/gnus-score-rebuild-alist nil)
    (setq bbdb/gnus-score-alist
      (concat "((touched nil) (\"from\"\n"
          (mapconcat
           (lambda (rec)
             (let ((score (or (bbdb-record-getprop rec
                               bbdb/gnus-score-field)
                      bbdb/gnus-score-default))
               (net (bbdb-record-net rec)))
               (if (not (and score net)) nil
             (mapconcat
              (lambda (addr)
                (format "(\"%s\" %s)\n" addr score))
              net ""))))
           (bbdb-records) "")
          "))"))))
  bbdb/gnus-score-alist)

;;; Posted originally by Colin Rafferty on the <bbdb-info> mailing list
(defun bbdb/gnus-summary-show-all-recipients (not-elided)
  "Display BBDB records for all recipients of the message."
  (interactive "P")
  (gnus-summary-select-article)
  (let ((bbdb-elided-display (or (not not-elided)
                                 (and (boundp 'bbdb-pop-up-elided-display)
                                      bbdb-pop-up-elided-display)))
        (bbdb-auto-notes-alist nil))
    (bbdb/gnus-pop-up-bbdb-buffer nil)
    (set-buffer gnus-article-buffer)
    (bbdb-show-all-recipients)
    ))

;;
;; Insinuation
;;

;;;###autoload
(defun bbdb-insinuate-gnus ()
  "Call this function to hook BBDB into GNUS."
  (setq gnus-optional-headers 'bbdb/gnus-lines-and-from)
  (cond ((boundp 'gnus-Article-prepare-hook) ; 3.14 or lower
     (add-hook 'gnus-Article-prepare-hook 'bbdb/gnus-pop-up-bbdb-buffer)
     (add-hook 'gnus-Save-newsrc-hook 'bbdb-offer-save)
     (define-key gnus-Subject-mode-map ":" 'bbdb/gnus-show-sender)
     (define-key gnus-Subject-mode-map [(control :)]
       'bbdb/gnus-summary-show-all-recipients)
     (define-key gnus-Subject-mode-map ";" 'bbdb/gnus-edit-notes))
    (t                                   ; 3.15 or higher
     (add-hook 'gnus-article-prepare-hook 'bbdb/gnus-pop-up-bbdb-buffer)
     (add-hook 'gnus-save-newsrc-hook 'bbdb-offer-save)
     (define-key gnus-summary-mode-map ":" 'bbdb/gnus-show-sender)
     (define-key gnus-summary-mode-map ";" 'bbdb/gnus-edit-notes)))

  ;; Set up user field for use in gnus-summary-line-format
  (let ((get-author-user-fun (intern
                  (concat "gnus-user-format-function-"
                      bbdb/gnus-summary-user-format-letter)))
    (in-bbdb-user-fun (intern
               (concat "gnus-user-format-function-"
                   bbdb/gnus-summary-in-bbdb-format-letter))))
                    ; The big one - whole name
    (cond (bbdb/gnus-summary-user-format-letter
       (if (and (fboundp get-author-user-fun)
            (not (eq (symbol-function get-author-user-fun)
                 'bbdb/gnus-summary-get-author)))
           (bbdb-warn
        (format "`gnus-user-format-function-%s' already seems to be in use.
Please redefine `bbdb/gnus-summary-user-format-letter' to a different letter."
            bbdb/gnus-summary-user-format-letter))
         (fset get-author-user-fun 'bbdb/gnus-summary-get-author))))

    ; One tick.  One tick only, please
    (cond (bbdb/gnus-summary-in-bbdb-format-letter
       (if (and (fboundp in-bbdb-user-fun)
            (not (eq (symbol-function in-bbdb-user-fun)
                 'bbdb/gnus-summary-author-in-bbdb)))
           (bbdb-warn
        (format "`gnus-user-format-function-%s' already seems to be in use.
Redefine `bbdb/gnus-summary-in-bbdb-format-letter' to a different letter."
            bbdb/gnus-summary-in-bbdb-format-letter))
         (fset in-bbdb-user-fun 'bbdb/gnus-summary-author-in-bbdb)))))

  ;; Scoring
  (add-hook 'bbdb-after-change-hook 'bbdb/gnus-score-invalidate-alist)
;  (setq gnus-score-find-score-files-function
;   (if (boundp 'gnus-score-find-score-files-function)
;       (cond ((functionp gnus-score-find-score-files-function)
;          (list gnus-score-find-score-files-function
;            'bbdb/gnus-score))
;         ((listp gnus-score-find-score-files-function)
;          (append gnus-score-find-score-files-function
;              'bbdb/gnus-score))
;         (t 'bbdb/gnus-score))
;     'bbdb/gnus-score))
  )

(provide 'bbdb-gnus)