e259933680d1edf2d8139222360013a46b121219
[gnus] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: network
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; This module addresses a few aspects of spam control under Gnus.  Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
29
30 ;;; The integration with Gnus is not yet complete.  See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
32
33 ;;; Several TODO items are marked as such
34
35 ;;; Code:
36
37 (require 'gnus-sum)
38
39 (require 'gnus-uu)                      ; because of key prefix issues
40 (require 'gnus) ; for the definitions of group content classification and spam processors
41 (require 'message)                      ;for the message-fetch-field functions
42
43 ;; autoload executable-find
44 (eval-and-compile
45   ;; executable-find is not autoloaded in Emacs 20
46   (autoload 'executable-find "executable"))
47
48 ;; autoload query-dig
49 (eval-and-compile
50   (autoload 'query-dig "dig"))
51
52 ;; autoload query-dns
53 (eval-and-compile
54   (autoload 'query-dns "dns"))
55
56 ;;; Main parameters.
57
58 (defgroup spam nil
59   "Spam configuration.")
60
61 (defcustom spam-directory "~/News/spam/"
62   "Directory for spam whitelists and blacklists."
63   :type 'directory
64   :group 'spam)
65
66 (defcustom spam-move-spam-nonspam-groups-only t
67   "Whether spam should be moved in non-spam groups only.
68 When nil, only ham and unclassified groups will have their spam moved
69 to the spam-process-destination.  When t, spam will also be moved from
70 spam groups."
71   :type 'boolean
72   :group 'spam-ifile)
73
74 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
75   "The location of the whitelist.
76 The file format is one regular expression per line.
77 The regular expression is matched against the address."
78   :type 'file
79   :group 'spam)
80
81 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
82   "The location of the blacklist.
83 The file format is one regular expression per line.
84 The regular expression is matched against the address."
85   :type 'file
86   :group 'spam)
87
88 (defcustom spam-use-dig t
89   "Whether query-dig should be used instead of query-dns."
90   :type 'boolean
91   :group 'spam)
92
93 (defcustom spam-use-blacklist nil
94   "Whether the blacklist should be used by spam-split."
95   :type 'boolean
96   :group 'spam)
97
98 (defcustom spam-use-whitelist nil
99   "Whether the whitelist should be used by spam-split."
100   :type 'boolean
101   :group 'spam)
102
103 (defcustom spam-use-whitelist-exclusive nil
104   "Whether whitelist-exclusive should be used by spam-split.
105 Exclusive whitelisting means that all messages from senders not in the whitelist
106 are considered spam."
107   :type 'boolean
108   :group 'spam)
109
110 (defcustom spam-use-blackholes nil
111   "Whether blackholes should be used by spam-split."
112   :type 'boolean
113   :group 'spam)
114
115 (defcustom spam-use-regex-headers nil
116   "Whether a header regular expression match should be used by spam-split.
117 Also see the variable `spam-spam-regex-headers' and `spam-ham-regex-headers'."
118   :type 'boolean
119   :group 'spam)
120
121 (defcustom spam-use-bogofilter-headers nil
122   "Whether bogofilter headers should be used by spam-split.
123 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
124   :type 'boolean
125   :group 'spam)
126
127 (defcustom spam-use-bogofilter nil
128   "Whether bogofilter should be invoked by spam-split.
129 Enable this if you want Gnus to invoke Bogofilter on new messages."
130   :type 'boolean
131   :group 'spam)
132
133 (defcustom spam-use-BBDB nil
134   "Whether BBDB should be used by spam-split."
135   :type 'boolean
136   :group 'spam)
137
138 (defcustom spam-use-BBDB-exclusive nil
139   "Whether BBDB-exclusive should be used by spam-split.
140 Exclusive BBDB means that all messages from senders not in the BBDB are 
141 considered spam."
142   :type 'boolean
143   :group 'spam)
144
145 (defcustom spam-use-ifile nil
146   "Whether ifile should be used by spam-split."
147   :type 'boolean
148   :group 'spam)
149
150 (defcustom spam-use-stat nil
151   "Whether spam-stat should be used by spam-split."
152   :type 'boolean
153   :group 'spam)
154
155 (defcustom spam-split-group "spam"
156   "Group name where incoming spam should be put by spam-split."
157   :type 'string
158   :group 'spam)
159
160 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
161   "Mailgroups with spam contents.
162 All unmarked article in such group receive the spam mark on group entry."
163   :type '(repeat (string :tag "Group"))
164   :group 'spam)
165
166 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org" 
167                                     "dev.null.dk" "relays.visi.com")
168   "List of blackhole servers."
169   :type '(repeat (string :tag "Server"))
170   :group 'spam)
171
172 (defcustom spam-blackhole-good-server-regex nil
173   "String matching IP addresses that should not be checked in the blackholes"
174   :type 'regexp
175   :group 'spam)
176
177 (defcustom spam-ham-marks (list 'gnus-del-mark 'gnus-read-mark 
178                                 'gnus-killed-mark 'gnus-kill-file-mark 
179                                 'gnus-low-score-mark)
180   "Marks considered as being ham (positively not spam).
181 Such articles will be processed as ham (non-spam) on group exit."
182   :type '(set
183           (variable-item gnus-del-mark)
184           (variable-item gnus-read-mark)
185           (variable-item gnus-killed-mark)
186           (variable-item gnus-kill-file-mark)
187           (variable-item gnus-low-score-mark))
188   :group 'spam)
189
190 (defcustom spam-spam-marks (list 'gnus-spam-mark)
191   "Marks considered as being spam (positively spam).
192 Such articles will be transmitted to `bogofilter -s' on group exit."
193   :type '(set 
194           (variable-item gnus-spam-mark)
195           (variable-item gnus-killed-mark)
196           (variable-item gnus-kill-file-mark)
197           (variable-item gnus-low-score-mark))
198   :group 'spam)
199
200 (defcustom spam-face 'gnus-splash-face
201   "Face for spam-marked articles"
202   :type 'face
203   :group 'spam)
204
205 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
206   "Regular expression for positive header spam matches"
207   :type '(repeat (regexp :tag "Regular expression to match spam header"))
208   :group 'spam)
209
210 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
211   "Regular expression for positive header ham matches"
212   :type '(repeat (regexp :tag "Regular expression to match ham header"))
213   :group 'spam)
214
215 (defgroup spam-ifile nil
216   "Spam ifile configuration."
217   :group 'spam)
218
219 (defcustom spam-ifile-path (executable-find "ifile")
220   "File path of the ifile executable program."
221   :type '(choice (file :tag "Location of ifile")
222                  (const :tag "ifile is not installed"))
223   :group 'spam-ifile)
224
225 (defcustom spam-ifile-database-path nil
226   "File path of the ifile database."
227   :type '(choice (file :tag "Location of the ifile database")
228                  (const :tag "Use the default"))
229   :group 'spam-ifile)
230
231 (defcustom spam-ifile-spam-category "spam"
232   "Name of the spam ifile category."  
233   :type 'string
234   :group 'spam-ifile)
235
236 (defcustom spam-ifile-ham-category nil
237   "Name of the ham ifile category.  If nil, the current group name will
238 be used."
239   :type '(choice (string :tag "Use a fixed category")
240                 (const :tag "Use the current group name"))
241   :group 'spam-ifile)
242
243 (defcustom spam-ifile-all-categories nil
244   "Whether the ifile check will return all categories, or just spam.
245 Set this to t if you want to use the spam-split invocation of ifile as
246 your main source of newsgroup names."
247   :type 'boolean
248   :group 'spam-ifile)
249
250 (defgroup spam-bogofilter nil
251   "Spam bogofilter configuration."
252   :group 'spam)
253
254 (defcustom spam-bogofilter-path (executable-find "bogofilter")
255   "File path of the Bogofilter executable program."
256   :type '(choice (file :tag "Location of bogofilter")
257                  (const :tag "Bogofilter is not installed"))
258   :group 'spam-bogofilter)
259
260 (defcustom spam-bogofilter-header "X-Bogosity"
261   "The header that Bogofilter inserts in messages."
262   :type 'string
263   :group 'spam-bogofilter)
264
265 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
266   "The regex on `spam-bogofilter-header' for positive spam identification."
267   :type 'regexp
268   :group 'spam-bogofilter)
269
270 (defcustom spam-bogofilter-database-directory nil
271   "Directory path of the Bogofilter databases."
272   :type '(choice (directory :tag "Location of the Bogofilter database directory")
273                  (const :tag "Use the default"))
274   :group 'spam-ifile)
275
276 ;;; Key bindings for spam control.
277
278 (gnus-define-keys gnus-summary-mode-map
279   "St" spam-bogofilter-score
280   "Sx" gnus-summary-mark-as-spam
281   "Mst" spam-bogofilter-score
282   "Msx" gnus-summary-mark-as-spam
283   "\M-d" gnus-summary-mark-as-spam)
284
285 ;;; How to highlight a spam summary line.
286
287 ;; TODO: How do we redo this every time spam-face is customized?
288
289 (push '((eq mark gnus-spam-mark) . spam-face)
290       gnus-summary-highlight)
291
292 ;; convenience functions
293 (defun spam-group-spam-contents-p (group)
294   (if (stringp group)
295       (or (member group spam-junk-mailgroups)
296           (memq 'gnus-group-spam-classification-spam 
297                 (gnus-parameter-spam-contents group)))
298     nil))
299   
300 (defun spam-group-ham-contents-p (group)
301   (if (stringp group)
302       (memq 'gnus-group-spam-classification-ham 
303             (gnus-parameter-spam-contents group))
304     nil))
305
306 (defun spam-group-processor-p (group processor)
307   (if (and (stringp group)
308            (symbolp processor))
309       (member processor (car (gnus-parameter-spam-process group)))
310     nil))
311
312 (defun spam-group-spam-processor-bogofilter-p (group)
313   (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
314
315 (defun spam-group-spam-processor-blacklist-p (group)
316   (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
317
318 (defun spam-group-spam-processor-ifile-p (group)
319   (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
320
321 (defun spam-group-ham-processor-ifile-p (group)
322   (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
323
324 (defun spam-group-ham-processor-bogofilter-p (group)
325   (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
326
327 (defun spam-group-spam-processor-stat-p (group)
328   (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
329
330 (defun spam-group-ham-processor-stat-p (group)
331   (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
332
333 (defun spam-group-ham-processor-whitelist-p (group)
334   (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
335
336 (defun spam-group-ham-processor-BBDB-p (group)
337   (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
338
339 (defun spam-group-ham-processor-copy-p (group)
340   (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
341
342 ;;; Summary entry and exit processing.
343
344 (defun spam-summary-prepare ()
345   (spam-mark-junk-as-spam-routine))
346
347 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
348
349 ;; The spam processors are invoked for any group, spam or ham or neither
350 (defun spam-summary-prepare-exit ()
351   (unless gnus-group-is-exiting-without-update-p
352     (gnus-message 6 "Exiting summary buffer and applying spam rules")
353     (when (and spam-bogofilter-path
354                (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
355       (gnus-message 5 "Registering spam with bogofilter")
356       (spam-bogofilter-register-spam-routine))
357   
358     (when (and spam-ifile-path
359                (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
360       (gnus-message 5 "Registering spam with ifile")
361       (spam-ifile-register-spam-routine))
362   
363     (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
364       (gnus-message 5 "Registering spam with spam-stat")
365       (spam-stat-register-spam-routine))
366
367     (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
368       (gnus-message 5 "Registering spam with the blacklist")
369       (spam-blacklist-register-routine))
370
371     (if spam-move-spam-nonspam-groups-only      
372         (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
373           (spam-mark-spam-as-expired-and-move-routine
374            (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
375       (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
376       (spam-mark-spam-as-expired-and-move-routine 
377        (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
378
379     ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
380     ;; expire spam, in case the above did not expire them
381     (gnus-message 5 "Marking spam as expired without moving it")
382     (spam-mark-spam-as-expired-and-move-routine nil)
383
384     (when (spam-group-ham-contents-p gnus-newsgroup-name)
385       (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
386         (gnus-message 5 "Registering ham with the whitelist")
387         (spam-whitelist-register-routine))
388       (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
389         (gnus-message 5 "Registering ham with ifile")
390         (spam-ifile-register-ham-routine))
391       (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
392         (gnus-message 5 "Registering ham with Bogofilter")
393         (spam-bogofilter-register-ham-routine))
394       (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
395         (gnus-message 5 "Registering ham with spam-stat")
396         (spam-stat-register-ham-routine))
397       (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
398         (gnus-message 5 "Registering ham with the BBDB")
399         (spam-BBDB-register-routine)))
400
401     (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
402       (gnus-message 5 "Copying ham")
403       (spam-ham-move-routine
404        (gnus-parameter-ham-process-destination gnus-newsgroup-name) t))
405
406     ;; now move all ham articles out of spam groups
407     (when (spam-group-spam-contents-p gnus-newsgroup-name)
408       (gnus-message 5 "Moving ham messages from spam group")
409       (spam-ham-move-routine
410        (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
411
412 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
413
414 (defun spam-mark-junk-as-spam-routine ()
415   ;; check the global list of group names spam-junk-mailgroups and the
416   ;; group parameters
417   (when (spam-group-spam-contents-p gnus-newsgroup-name)
418     (gnus-message 5 "Marking unread articles as spam")
419     (let ((articles gnus-newsgroup-articles)
420           article)
421       (while articles
422         (setq article (pop articles))
423         (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
424           (gnus-summary-mark-article article gnus-spam-mark))))))
425
426 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
427   (gnus-summary-kill-process-mark)
428   (let ((articles gnus-newsgroup-articles)
429         article tomove)
430     (dolist (article articles)
431       (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
432         (gnus-summary-mark-article article gnus-expirable-mark)
433         (push article tomove)))
434
435     ;; now do the actual move
436     (when (and tomove
437                (stringp group))
438       (dolist (article tomove)
439         (gnus-summary-set-process-mark article))
440       (when tomove (gnus-summary-move-article nil group))))
441   (gnus-summary-yank-process-mark))
442  
443 (defun spam-ham-move-routine (&optional group copy)
444   (gnus-summary-kill-process-mark)
445   (let ((articles gnus-newsgroup-articles)
446         article ham-mark-values mark tomove)
447     (when (stringp group)               ; this routine will do nothing
448                                         ; without a valid group
449       (dolist (mark spam-ham-marks)
450         (push (symbol-value mark) ham-mark-values))
451       (dolist (article articles)
452         (when (memq (gnus-summary-article-mark article) ham-mark-values)
453           (push article tomove)))
454
455       ;; now do the actual move
456       (when tomove
457         (dolist (article tomove)
458           (gnus-summary-set-process-mark article))
459         (if copy
460             (gnus-summary-copy-article nil group)
461           (gnus-summary-move-article nil group)))))
462   (gnus-summary-yank-process-mark))
463  
464 (defun spam-generic-register-routine (spam-func ham-func)
465   (let ((articles gnus-newsgroup-articles)
466         article mark ham-articles spam-articles spam-mark-values 
467         ham-mark-values)
468
469     ;; marks are stored as symbolic values, so we have to dereference
470     ;; them for memq to work.  we wouldn't have to do this if
471     ;; gnus-summary-article-mark returned a symbol.
472     (dolist (mark spam-ham-marks)
473       (push (symbol-value mark) ham-mark-values))
474
475     (dolist (mark spam-spam-marks)
476       (push (symbol-value mark) spam-mark-values))
477
478     (while articles
479       (setq article (pop articles)
480             mark (gnus-summary-article-mark article))
481       (cond ((memq mark spam-mark-values) (push article spam-articles))
482             ((memq article gnus-newsgroup-saved))
483             ((memq mark ham-mark-values) (push article ham-articles))))
484     (when (and ham-articles ham-func)
485       (mapc ham-func ham-articles))     ; we use mapc because unlike
486                                         ; mapcar it discards the
487                                         ; return values
488     (when (and spam-articles spam-func)
489       (mapc spam-func spam-articles)))) ; we use mapc because unlike
490                                         ; mapcar it discards the
491                                         ; return values
492
493 (eval-and-compile
494   (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
495                                    'point-at-eol
496                                  'line-end-position)))
497
498 (defun spam-get-article-as-string (article)
499   (let ((article-buffer (spam-get-article-as-buffer article))
500                         article-string)
501     (when article-buffer
502       (save-window-excursion
503         (set-buffer article-buffer)
504         (setq article-string (buffer-string))))
505   article-string))
506
507 (defun spam-get-article-as-buffer (article)
508   (let ((article-buffer))
509     (when (numberp article)
510       (save-window-excursion
511         (gnus-summary-goto-subject article)
512         (gnus-summary-show-article t)
513         (setq article-buffer (get-buffer gnus-article-buffer))))
514     article-buffer))
515
516 ;; disabled for now
517 ;; (defun spam-get-article-as-filename (article)
518 ;;   (let ((article-filename))
519 ;;     (when (numberp article)
520 ;;       (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
521 ;;       (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
522 ;;     (if (file-exists-p article-filename)
523 ;;      article-filename
524 ;;       nil)))
525
526 (defun spam-fetch-field-from-fast (article)
527   "Fetch the `from' field quickly, using the internal gnus-data-list function"
528   (if (and (numberp article)
529            (assoc article (gnus-data-list nil)))
530       (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
531     nil))
532
533 (defun spam-fetch-field-subject-fast (article)
534   "Fetch the `subject' field quickly, using the internal gnus-data-list function"
535   (if (and (numberp article)
536            (assoc article (gnus-data-list nil)))
537       (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
538     nil))
539
540 \f
541 ;;;; Spam determination.
542
543 (defvar spam-list-of-checks
544   '((spam-use-blacklist                 .       spam-check-blacklist)
545     (spam-use-regex-headers             .       spam-check-regex-headers)
546     (spam-use-whitelist                 .       spam-check-whitelist)
547     (spam-use-BBDB                      .       spam-check-BBDB)
548     (spam-use-ifile                     .       spam-check-ifile)
549     (spam-use-stat                      .       spam-check-stat)
550     (spam-use-blackholes                .       spam-check-blackholes)
551     (spam-use-bogofilter-headers        .       spam-check-bogofilter-headers)
552     (spam-use-bogofilter                .       spam-check-bogofilter))
553 "The spam-list-of-checks list contains pairs associating a parameter
554 variable with a spam checking function.  If the parameter variable is
555 true, then the checking function is called, and its value decides what
556 happens.  Each individual check may return nil, t, or a mailgroup
557 name.  The value nil means that the check does not yield a decision,
558 and so, that further checks are needed.  The value t means that the
559 message is definitely not spam, and that further spam checks should be
560 inhibited.  Otherwise, a mailgroup name is returned where the mail
561 should go, and further checks are also inhibited.  The usual mailgroup
562 name is the value of `spam-split-group', meaning that the message is
563 definitely a spam.")
564
565 (defun spam-split ()
566   "Split this message into the `spam' group if it is spam.
567 This function can be used as an entry in `nnmail-split-fancy', for
568 example like this: (: spam-split)
569
570 See the Info node `(gnus)Fancy Mail Splitting' for more details."
571   (interactive)
572   
573   ;; load the spam-stat tables if needed
574   (when spam-use-stat (spam-stat-load))
575
576   (let ((list-of-checks spam-list-of-checks)
577         decision)
578     (while (and list-of-checks (not decision))
579       (let ((pair (pop list-of-checks)))
580         (when (symbol-value (car pair))
581           (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
582           (setq decision (funcall (cdr pair))))))
583     (if (eq decision t)
584         nil
585       decision)))
586 \f
587 ;;;; Regex headers
588
589 (defun spam-check-regex-headers ()
590   (let (ret found)
591     (dolist (h-regex spam-regex-headers-ham)
592       (unless found
593         (goto-char (point-min))
594         (when (re-search-forward h-regex nil t)
595           (message "Ham regex header search positive.")
596           (setq found t))))
597     (dolist (s-regex spam-regex-headers-spam)
598       (unless found
599         (goto-char (point-min))
600         (when (re-search-forward s-regex nil t)
601           (message "Spam regex header search positive." (match-string 1))
602           (setq found t)
603           (setq ret spam-split-group))))
604     ret))
605
606 \f
607 ;;;; Blackholes.
608
609 (defun spam-check-blackholes ()
610   "Check the Received headers for blackholed relays."
611   (let ((headers (message-fetch-field "received"))
612         ips matches)
613     (when headers
614       (with-temp-buffer
615         (insert headers)
616         (goto-char (point-min))
617         (gnus-message 5 "Checking headers for relay addresses")
618         (while (re-search-forward
619                 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
620           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
621           (push (mapconcat 'identity
622                            (nreverse (split-string (match-string 1) "\\."))
623                            ".")
624                 ips)))
625       (dolist (server spam-blackhole-servers)
626         (dolist (ip ips)
627           (unless (and spam-blackhole-good-server-regex
628                        (string-match spam-blackhole-good-server-regex ip))
629             (let ((query-string (concat ip "." server)))
630               (if spam-use-dig
631                   (let ((query-result (query-dig query-string)))
632                     (when query-result
633                       (gnus-message 5 "(DIG): positive blackhole check '%s'" query-result)
634                       (push (list ip server query-result)
635                             matches)))
636                 ;; else, if not using dig.el
637                 (when (query-dns query-string)
638                   (gnus-message 5 "positive blackhole check")
639                   (push (list ip server (query-dns query-string 'TXT))
640                         matches))))))))
641     (when matches
642       spam-split-group)))
643 \f
644 ;;;; BBDB 
645
646 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
647 ;;; <sacha@giotto.sj.ru>
648
649 ;; all this is done inside a condition-case to trap errors
650
651 (condition-case nil
652     (progn
653       (require 'bbdb)
654       (require 'bbdb-com)
655       
656   (defun spam-enter-ham-BBDB (from)
657     "Enter an address into the BBDB; implies ham (non-spam) sender"
658     (when (stringp from)
659       (let* ((parsed-address (gnus-extract-address-components from))
660              (name (or (car parsed-address) "Ham Sender"))
661              (net-address (car (cdr parsed-address))))
662         (gnus-message 5 "Adding address %s to BBDB" from)
663         (when (and net-address
664                    (not (bbdb-search-simple nil net-address)))
665           (bbdb-create-internal name nil net-address nil nil 
666                                 "ham sender added by spam.el")))))
667
668   (defun spam-BBDB-register-routine ()
669     (spam-generic-register-routine 
670      ;; spam function
671      nil
672      ;; ham function
673      (lambda (article)
674        (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
675
676   (defun spam-check-BBDB ()
677     "Mail from people in the BBDB is classified as ham or non-spam"
678     (let ((who (message-fetch-field "from")))
679       (when who
680         (setq who (cadr (gnus-extract-address-components who)))
681         (if (bbdb-search-simple nil who)
682             t 
683           (if spam-use-BBDB-exclusive
684               spam-split-group
685             nil))))))
686
687   (file-error (progn
688                 (defalias 'bbdb-search-simple 'ignore)
689                 (defalias 'spam-check-BBDB 'ignore)
690                 (defalias 'spam-BBDB-register-routine 'ignore)
691                 (defalias 'spam-enter-ham-BBDB 'ignore)
692                 (defalias 'bbdb-create-internal 'ignore)
693                 (defalias 'bbdb-records 'ignore))))
694
695 \f
696 ;;;; ifile
697
698 ;;; check the ifile backend; return nil if the mail was NOT classified
699 ;;; as spam
700
701 (defun spam-get-ifile-database-parameter ()
702   "Get the command-line parameter for ifile's database from spam-ifile-database-path."
703   (if spam-ifile-database-path
704       (format "--db-file=%s" spam-ifile-database-path)
705     nil))
706     
707 (defun spam-check-ifile ()
708   "Check the ifile backend for the classification of this message"
709   (let ((article-buffer-name (buffer-name)) 
710         category return)
711     (with-temp-buffer
712       (let ((temp-buffer-name (buffer-name))
713             (db-param (spam-get-ifile-database-parameter)))
714         (save-excursion
715           (set-buffer article-buffer-name)
716           (if db-param
717               (call-process-region (point-min) (point-max) spam-ifile-path
718                                    nil temp-buffer-name nil "-q" "-c" db-param)
719             (call-process-region (point-min) (point-max) spam-ifile-path
720                                  nil temp-buffer-name nil "-q" "-c")))
721         (goto-char (point-min))
722         (if (not (eobp))
723             (setq category (buffer-substring (point) (spam-point-at-eol))))
724         (when (not (zerop (length category))) ; we need a category here
725           (if spam-ifile-all-categories
726               (setq return category)
727             ;; else, if spam-ifile-all-categories is not set...
728             (when (string-equal spam-ifile-spam-category category)
729               (setq return spam-split-group))))))
730     return))
731
732 (defun spam-ifile-register-with-ifile (article-string category)
733   "Register an article, given as a string, with a category.
734 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
735   (when (stringp article-string)
736     (let ((category (or category gnus-newsgroup-name))
737           (db-param (spam-get-ifile-database-parameter)))
738       (with-temp-buffer
739         (insert article-string)
740         (if db-param
741             (call-process-region (point-min) (point-max) spam-ifile-path 
742                                  nil nil nil 
743                                  "-h" "-i" category db-param)
744           (call-process-region (point-min) (point-max) spam-ifile-path 
745                                nil nil nil 
746                                "-h" "-i" category))))))
747
748 (defun spam-ifile-register-spam-routine ()
749   (spam-generic-register-routine 
750    (lambda (article)
751      (spam-ifile-register-with-ifile 
752       (spam-get-article-as-string article) spam-ifile-spam-category))
753    nil))
754
755 (defun spam-ifile-register-ham-routine ()
756   (spam-generic-register-routine 
757    nil
758    (lambda (article)
759      (spam-ifile-register-with-ifile 
760       (spam-get-article-as-string article) spam-ifile-ham-category))))
761
762 \f
763 ;;;; spam-stat
764
765 (condition-case nil
766     (progn
767       (let ((spam-stat-install-hooks nil))
768         (require 'spam-stat))
769       
770       (defun spam-check-stat ()
771         "Check the spam-stat backend for the classification of this message"
772         (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
773               (spam-stat-buffer (buffer-name)) ; stat the current buffer
774               category return)
775           (spam-stat-split-fancy)))
776
777       (defun spam-stat-register-spam-routine ()
778         (spam-generic-register-routine 
779          (lambda (article)
780            (let ((article-string (spam-get-article-as-string article)))
781              (with-temp-buffer
782                (insert article-string)
783                (spam-stat-buffer-is-spam))))
784          nil))
785
786       (defun spam-stat-register-ham-routine ()
787         (spam-generic-register-routine 
788          nil
789          (lambda (article)
790            (let ((article-string (spam-get-article-as-string article)))
791              (with-temp-buffer
792                (insert article-string)
793                (spam-stat-buffer-is-non-spam))))))
794
795       (when spam-use-stat
796         (add-hook 'gnus-save-newsrc-hook 'spam-stat-save)))
797
798   (file-error (progn
799                 (defalias 'spam-stat-register-ham-routine 'ignore)
800                 (defalias 'spam-stat-register-spam-routine 'ignore)
801                 (defalias 'spam-stat-buffer-is-spam 'ignore)
802                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
803                 (defalias 'spam-stat-split-fancy 'ignore)
804                 (defalias 'spam-stat-load 'ignore)
805                 (defalias 'spam-stat-save 'ignore)
806                 (defalias 'spam-check-stat 'ignore))))
807
808 \f
809
810 ;;;; Blacklists and whitelists.
811
812 (defvar spam-whitelist-cache nil)
813 (defvar spam-blacklist-cache nil)
814
815 (defun spam-enter-whitelist (address)
816   "Enter ADDRESS into the whitelist."
817   (interactive "sAddress: ")
818   (spam-enter-list address spam-whitelist)
819   (setq spam-whitelist-cache nil))
820
821 (defun spam-enter-blacklist (address)
822   "Enter ADDRESS into the blacklist."
823   (interactive "sAddress: ")
824   (spam-enter-list address spam-blacklist)
825   (setq spam-blacklist-cache nil))
826
827 (defun spam-enter-list (address file)
828   "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
829   (unless (file-exists-p (file-name-directory file))
830     (make-directory (file-name-directory file) t))
831   (save-excursion
832     (set-buffer
833      (find-file-noselect file))
834     (goto-char (point-max))
835     (unless (bobp)
836       (insert "\n"))
837     (insert address "\n")
838     (save-buffer)))
839
840 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
841 (defun spam-check-whitelist ()
842   ;; FIXME!  Should it detect when file timestamps change?
843   (unless spam-whitelist-cache
844     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
845   (if (spam-from-listed-p spam-whitelist-cache) 
846       t
847     (if spam-use-whitelist-exclusive
848         spam-split-group
849       nil)))
850
851 (defun spam-check-blacklist ()
852   ;; FIXME!  Should it detect when file timestamps change?
853   (unless spam-blacklist-cache
854     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
855   (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
856
857 (defun spam-parse-list (file)
858   (when (file-readable-p file)
859     (let (contents address)
860       (with-temp-buffer
861         (insert-file-contents file)
862         (while (not (eobp))
863           (setq address (buffer-substring (point) (spam-point-at-eol)))
864           (forward-line 1)
865           (unless (zerop (length address))
866             (setq address (regexp-quote address))
867             (while (string-match "\\\\\\*" address)
868               (setq address (replace-match ".*" t t address)))
869             (push address contents))))
870       (nreverse contents))))
871
872 (defun spam-from-listed-p (cache)
873   (let ((from (message-fetch-field "from"))
874         found)
875     (while cache
876       (when (string-match (pop cache) from)
877         (setq found t
878               cache nil)))
879     found))
880
881 (defun spam-blacklist-register-routine ()
882   (spam-generic-register-routine 
883    ;; the spam function
884    (lambda (article)
885      (let ((from (spam-fetch-field-from-fast article)))
886        (when (stringp from)
887            (spam-enter-blacklist from))))
888    ;; the ham function
889    nil))
890
891 (defun spam-whitelist-register-routine ()
892   (spam-generic-register-routine 
893    ;; the spam function
894    nil 
895    ;; the ham function
896    (lambda (article)
897      (let ((from (spam-fetch-field-from-fast article)))
898        (when (stringp from)
899            (spam-enter-whitelist from))))))
900
901 \f
902 ;;;; Bogofilter
903
904 (defun spam-check-bogofilter-headers (&optional score)
905   (let ((header (message-fetch-field spam-bogofilter-header)))
906       (when (and header
907                  (string-match spam-bogofilter-bogosity-positive-spam-header
908                                header))
909           (if score
910               (when (string-match "spamicity=\\([0-9.]+\\)" header)
911                 (match-string 1 header))
912             spam-split-group))))
913
914 ;; return something sensible if the score can't be determined
915 (defun spam-bogofilter-score ()
916   "Get the Bogofilter spamicity score"
917   (interactive)
918   (save-window-excursion
919     (gnus-summary-show-article t)
920     (set-buffer gnus-article-buffer)
921     (let ((score (spam-check-bogofilter t)))
922       (message "Spamicity score %s" score)
923       (or score "0"))))
924
925 (defun spam-check-bogofilter (&optional score)
926   "Check the Bogofilter backend for the classification of this message"
927   (let ((article-buffer-name (buffer-name)) 
928         return)
929     (with-temp-buffer
930       (let ((temp-buffer-name (buffer-name)))
931         (save-excursion
932           (set-buffer article-buffer-name)
933           (if spam-bogofilter-database-directory
934               (call-process-region (point-min) (point-max) 
935                                    spam-bogofilter-path
936                                    nil temp-buffer-name nil "-v"
937                                    "-d" spam-bogofilter-database-directory)
938             (call-process-region (point-min) (point-max) spam-bogofilter-path
939                                  nil temp-buffer-name nil "-v")))
940         (setq return (spam-check-bogofilter-headers score))))
941     return))
942
943 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
944   "Register an article, given as a string, as spam or non-spam."
945   (when (stringp article-string)
946     (let ((switch (if spam "-s" "-n")))
947       (with-temp-buffer
948         (insert article-string)
949         (if spam-bogofilter-database-directory
950             (call-process-region (point-min) (point-max) 
951                                  spam-bogofilter-path
952                                  nil nil nil "-v" switch
953                                  "-d" spam-bogofilter-database-directory)
954           (call-process-region (point-min) (point-max) spam-bogofilter-path
955                                nil nil nil "-v" switch))))))
956
957 (defun spam-bogofilter-register-spam-routine ()
958   (spam-generic-register-routine 
959    (lambda (article)
960      (spam-bogofilter-register-with-bogofilter
961       (spam-get-article-as-string article) t))
962    nil))
963
964 (defun spam-bogofilter-register-ham-routine ()
965   (spam-generic-register-routine 
966    nil
967    (lambda (article)
968      (spam-bogofilter-register-with-bogofilter
969       (spam-get-article-as-string article) nil))))
970
971 (provide 'spam)
972
973 ;;; spam.el ends here.