d04b59ea3e758955eeebce607b7b1aacbdbd182e
[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 ;;; Summary entry and exit processing.
340
341 (defun spam-summary-prepare ()
342   (spam-mark-junk-as-spam-routine))
343
344 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
345
346 (defun spam-summary-prepare-exit ()
347   ;; The spam processors are invoked for any group, spam or ham or neither
348   (gnus-message 6 "Exiting summary buffer and applying spam rules")
349   (when (and spam-bogofilter-path
350              (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
351     (gnus-message 5 "Registering spam with bogofilter")
352     (spam-bogofilter-register-spam-routine))
353   
354   (when (and spam-ifile-path
355              (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
356     (gnus-message 5 "Registering spam with ifile")
357     (spam-ifile-register-spam-routine))
358   
359   (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
360     (gnus-message 5 "Registering spam with spam-stat")
361     (spam-stat-register-spam-routine))
362
363   (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
364     (gnus-message 5 "Registering spam with the blacklist")
365     (spam-blacklist-register-routine))
366
367   (if spam-move-spam-nonspam-groups-only      
368       (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
369         (spam-mark-spam-as-expired-and-move-routine
370          (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
371     (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
372     (spam-mark-spam-as-expired-and-move-routine 
373      (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
374
375   ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
376   ;; expire spam, in case the above did not expire them
377   (gnus-message 5 "Marking spam as expired without moving it")
378   (spam-mark-spam-as-expired-and-move-routine nil)
379
380   (when (spam-group-ham-contents-p gnus-newsgroup-name)
381     (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
382       (gnus-message 5 "Registering ham with the whitelist")
383       (spam-whitelist-register-routine))
384     (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
385       (gnus-message 5 "Registering ham with ifile")
386       (spam-ifile-register-ham-routine))
387     (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
388       (gnus-message 5 "Registering ham with Bogofilter")
389       (spam-bogofilter-register-ham-routine))
390     (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
391       (gnus-message 5 "Registering ham with spam-stat")
392       (spam-stat-register-ham-routine))
393     (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
394       (gnus-message 5 "Registering ham with the BBDB")
395       (spam-BBDB-register-routine)))
396
397   ;; now move all ham articles out of spam groups
398   (when (spam-group-spam-contents-p gnus-newsgroup-name)
399     (gnus-message 5 "Moving ham messages from spam group")
400     (spam-ham-move-routine
401      (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
402
403 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
404
405 (defun spam-mark-junk-as-spam-routine ()
406   ;; check the global list of group names spam-junk-mailgroups and the
407   ;; group parameters
408   (when (spam-group-spam-contents-p gnus-newsgroup-name)
409     (gnus-message 5 "Marking unread articles as spam")
410     (let ((articles gnus-newsgroup-articles)
411           article)
412       (while articles
413         (setq article (pop articles))
414         (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
415           (gnus-summary-mark-article article gnus-spam-mark))))))
416
417 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
418   (gnus-summary-kill-process-mark)
419   (let ((articles gnus-newsgroup-articles)
420         article tomove)
421     (dolist (article articles)
422       (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
423         (gnus-summary-mark-article article gnus-expirable-mark)
424         (push article tomove)))
425
426     ;; now do the actual move
427     (when (stringp group)
428       (dolist (article tomove)
429         (gnus-summary-set-process-mark article))
430       (when tomove (gnus-summary-move-article nil group))))
431   (gnus-summary-yank-process-mark))
432  
433 (defun spam-ham-move-routine (&optional group)
434   (gnus-summary-kill-process-mark)
435   (let ((articles gnus-newsgroup-articles)
436         article ham-mark-values mark tomove)
437     (when (stringp group)               ; this routine will do nothing
438                                         ; without a valid group
439       (dolist (mark spam-ham-marks)
440         (push (symbol-value mark) ham-mark-values))
441       (dolist (article articles)
442         (when (memq (gnus-summary-article-mark article) ham-mark-values)
443           (push article tomove)))
444
445       ;; now do the actual move
446       (dolist (article tomove)
447         (gnus-summary-set-process-mark article))
448       (when tomove (gnus-summary-move-article nil group))))
449   (gnus-summary-yank-process-mark))
450  
451 (defun spam-generic-register-routine (spam-func ham-func)
452   (let ((articles gnus-newsgroup-articles)
453         article mark ham-articles spam-articles spam-mark-values 
454         ham-mark-values)
455
456     ;; marks are stored as symbolic values, so we have to dereference
457     ;; them for memq to work.  we wouldn't have to do this if
458     ;; gnus-summary-article-mark returned a symbol.
459     (dolist (mark spam-ham-marks)
460       (push (symbol-value mark) ham-mark-values))
461
462     (dolist (mark spam-spam-marks)
463       (push (symbol-value mark) spam-mark-values))
464
465     (while articles
466       (setq article (pop articles)
467             mark (gnus-summary-article-mark article))
468       (cond ((memq mark spam-mark-values) (push article spam-articles))
469             ((memq article gnus-newsgroup-saved))
470             ((memq mark ham-mark-values) (push article ham-articles))))
471     (when (and ham-articles ham-func)
472       (mapc ham-func ham-articles))     ; we use mapc because unlike
473                                         ; mapcar it discards the
474                                         ; return values
475     (when (and spam-articles spam-func)
476       (mapc spam-func spam-articles)))) ; we use mapc because unlike
477                                         ; mapcar it discards the
478                                         ; return values
479
480 (eval-and-compile
481   (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
482                                    'point-at-eol
483                                  'line-end-position)))
484
485 (defun spam-get-article-as-string (article)
486   (let ((article-buffer (spam-get-article-as-buffer article))
487                         article-string)
488     (when article-buffer
489       (save-window-excursion
490         (set-buffer article-buffer)
491         (setq article-string (buffer-string))))
492   article-string))
493
494 (defun spam-get-article-as-buffer (article)
495   (let ((article-buffer))
496     (when (numberp article)
497       (save-window-excursion
498         (gnus-summary-goto-subject article)
499         (gnus-summary-show-article t)
500         (setq article-buffer (get-buffer gnus-article-buffer))))
501     article-buffer))
502
503 (defun spam-get-article-as-filename (article)
504   (let ((article-filename))
505     (when (numberp article)
506       (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
507       (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
508     (if (file-exists-p article-filename)
509         article-filename
510       nil)))
511
512 (defun spam-fetch-field-from-fast (article)
513   "Fetch the `from' field quickly, using the internal gnus-data-list function"
514   (if (and (numberp article)
515            (assoc article (gnus-data-list nil)))
516       (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
517     nil))
518
519 (defun spam-fetch-field-subject-fast (article)
520   "Fetch the `subject' field quickly, using the internal gnus-data-list function"
521   (if (and (numberp article)
522            (assoc article (gnus-data-list nil)))
523       (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
524     nil))
525
526 \f
527 ;;;; Spam determination.
528
529 (defvar spam-list-of-checks
530   '((spam-use-blacklist                 .       spam-check-blacklist)
531     (spam-use-regex-headers             .       spam-check-regex-headers)
532     (spam-use-whitelist                 .       spam-check-whitelist)
533     (spam-use-BBDB                      .       spam-check-BBDB)
534     (spam-use-ifile                     .       spam-check-ifile)
535     (spam-use-stat                      .       spam-check-stat)
536     (spam-use-blackholes                .       spam-check-blackholes)
537     (spam-use-bogofilter-headers        .       spam-check-bogofilter-headers)
538     (spam-use-bogofilter                .       spam-check-bogofilter))
539 "The spam-list-of-checks list contains pairs associating a parameter
540 variable with a spam checking function.  If the parameter variable is
541 true, then the checking function is called, and its value decides what
542 happens.  Each individual check may return nil, t, or a mailgroup
543 name.  The value nil means that the check does not yield a decision,
544 and so, that further checks are needed.  The value t means that the
545 message is definitely not spam, and that further spam checks should be
546 inhibited.  Otherwise, a mailgroup name is returned where the mail
547 should go, and further checks are also inhibited.  The usual mailgroup
548 name is the value of `spam-split-group', meaning that the message is
549 definitely a spam.")
550
551 (defun spam-split ()
552   "Split this message into the `spam' group if it is spam.
553 This function can be used as an entry in `nnmail-split-fancy', for
554 example like this: (: spam-split)
555
556 See the Info node `(gnus)Fancy Mail Splitting' for more details."
557   (interactive)
558   
559   ;; load the spam-stat tables if needed
560   (when spam-use-stat (spam-stat-load))
561
562   (let ((list-of-checks spam-list-of-checks)
563         decision)
564     (while (and list-of-checks (not decision))
565       (let ((pair (pop list-of-checks)))
566         (when (symbol-value (car pair))
567           (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
568           (setq decision (funcall (cdr pair))))))
569     (if (eq decision t)
570         nil
571       decision)))
572 \f
573 ;;;; Regex headers
574
575 (defun spam-check-regex-headers ()
576   (let (ret found)
577     (dolist (h-regex spam-regex-headers-ham)
578       (unless found
579         (goto-char (point-min))
580         (when (re-search-forward h-regex nil t)
581           (message "Ham regex header search positive.")
582           (setq found t))))
583     (dolist (s-regex spam-regex-headers-spam)
584       (unless found
585         (goto-char (point-min))
586         (when (re-search-forward s-regex nil t)
587           (message "Spam regex header search positive." (match-string 1))
588           (setq found t)
589           (setq ret spam-split-group))))
590     ret))
591
592 \f
593 ;;;; Blackholes.
594
595 (defun spam-check-blackholes ()
596   "Check the Received headers for blackholed relays."
597   (let ((headers (message-fetch-field "received"))
598         ips matches)
599     (when headers
600       (with-temp-buffer
601         (insert headers)
602         (goto-char (point-min))
603         (gnus-message 5 "Checking headers for relay addresses")
604         (while (re-search-forward
605                 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
606           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
607           (push (mapconcat 'identity
608                            (nreverse (split-string (match-string 1) "\\."))
609                            ".")
610                 ips)))
611       (dolist (server spam-blackhole-servers)
612         (dolist (ip ips)
613           (unless (and spam-blackhole-good-server-regex
614                        (string-match spam-blackhole-good-server-regex ip))
615             (let ((query-string (concat ip "." server)))
616               (if spam-use-dig
617                   (let ((query-result (query-dig query-string)))
618                     (when query-result
619                       (gnus-message 5 "(DIG): positive blackhole check '%s'" query-result)
620                       (push (list ip server query-result)
621                             matches)))
622                 ;; else, if not using dig.el
623                 (when (query-dns query-string)
624                   (gnus-message 5 "positive blackhole check")
625                   (push (list ip server (query-dns query-string 'TXT))
626                         matches))))))))
627     (when matches
628       spam-split-group)))
629 \f
630 ;;;; BBDB 
631
632 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
633 ;;; <sacha@giotto.sj.ru>
634
635 ;; all this is done inside a condition-case to trap errors
636
637 (condition-case nil
638     (progn
639       (require 'bbdb)
640       (require 'bbdb-com)
641       
642   (defun spam-enter-ham-BBDB (from)
643     "Enter an address into the BBDB; implies ham (non-spam) sender"
644     (when (stringp from)
645       (let* ((parsed-address (gnus-extract-address-components from))
646              (name (or (car parsed-address) "Ham Sender"))
647              (net-address (car (cdr parsed-address))))
648         (gnus-message 5 "Adding address %s to BBDB" from)
649         (when (and net-address
650                    (not (bbdb-search-simple nil net-address)))
651           (bbdb-create-internal name nil net-address nil nil 
652                                 "ham sender added by spam.el")))))
653
654   (defun spam-BBDB-register-routine ()
655     (spam-generic-register-routine 
656      ;; spam function
657      nil
658      ;; ham function
659      (lambda (article)
660        (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
661
662   (defun spam-check-BBDB ()
663     "Mail from people in the BBDB is classified as ham or non-spam"
664     (let ((who (message-fetch-field "from")))
665       (when who
666         (setq who (cadr (gnus-extract-address-components who)))
667         (if (bbdb-search-simple nil who)
668             t 
669           (if spam-use-BBDB-exclusive
670               spam-split-group
671             nil))))))
672
673   (file-error (progn
674                 (defalias 'bbdb-search-simple 'ignore)
675                 (defalias 'spam-check-BBDB 'ignore)
676                 (defalias 'spam-BBDB-register-routine 'ignore)
677                 (defalias 'spam-enter-ham-BBDB 'ignore)
678                 (defalias 'bbdb-create-internal 'ignore)
679                 (defalias 'bbdb-records 'ignore))))
680
681 \f
682 ;;;; ifile
683
684 ;;; check the ifile backend; return nil if the mail was NOT classified
685 ;;; as spam
686
687 (defun spam-get-ifile-database-parameter ()
688   "Get the command-line parameter for ifile's database from spam-ifile-database-path."
689   (if spam-ifile-database-path
690       (format "--db-file=%s" spam-ifile-database-path)
691     nil))
692     
693 (defun spam-check-ifile ()
694   "Check the ifile backend for the classification of this message"
695   (let ((article-buffer-name (buffer-name)) 
696         category return)
697     (with-temp-buffer
698       (let ((temp-buffer-name (buffer-name))
699             (db-param (spam-get-ifile-database-parameter)))
700         (save-excursion
701           (set-buffer article-buffer-name)
702           (if db-param
703               (call-process-region (point-min) (point-max) spam-ifile-path
704                                    nil temp-buffer-name nil "-q" "-c" db-param)
705             (call-process-region (point-min) (point-max) spam-ifile-path
706                                  nil temp-buffer-name nil "-q" "-c")))
707         (goto-char (point-min))
708         (if (not (eobp))
709             (setq category (buffer-substring (point) (spam-point-at-eol))))
710         (when (not (zerop (length category))) ; we need a category here
711           (if spam-ifile-all-categories
712               (setq return category)
713             ;; else, if spam-ifile-all-categories is not set...
714             (when (string-equal spam-ifile-spam-category category)
715               (setq return spam-split-group))))))
716     return))
717
718 (defun spam-ifile-register-with-ifile (article-string category)
719   "Register an article, given as a string, with a category.
720 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
721   (when (stringp article-string)
722     (let ((category (or category gnus-newsgroup-name))
723           (db-param (spam-get-ifile-database-parameter)))
724       (with-temp-buffer
725         (insert article-string)
726         (if db-param
727             (call-process-region (point-min) (point-max) spam-ifile-path 
728                                  nil nil nil 
729                                  "-h" "-i" category db-param)
730           (call-process-region (point-min) (point-max) spam-ifile-path 
731                                nil nil nil 
732                                "-h" "-i" category))))))
733
734 (defun spam-ifile-register-spam-routine ()
735   (spam-generic-register-routine 
736    (lambda (article)
737      (spam-ifile-register-with-ifile 
738       (spam-get-article-as-string article) spam-ifile-spam-category))
739    nil))
740
741 (defun spam-ifile-register-ham-routine ()
742   (spam-generic-register-routine 
743    nil
744    (lambda (article)
745      (spam-ifile-register-with-ifile 
746       (spam-get-article-as-string article) spam-ifile-ham-category))))
747
748 \f
749 ;;;; spam-stat
750
751 (condition-case nil
752     (progn
753       (let ((spam-stat-install-hooks nil))
754         (require 'spam-stat))
755       
756       (defun spam-check-stat ()
757         "Check the spam-stat backend for the classification of this message"
758         (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
759               (spam-stat-buffer (buffer-name)) ; stat the current buffer
760               category return)
761           (spam-stat-split-fancy)))
762
763       (defun spam-stat-register-spam-routine ()
764         (spam-generic-register-routine 
765          (lambda (article)
766            (let ((article-string (spam-get-article-as-string article)))
767              (with-temp-buffer
768                (insert article-string)
769                (spam-stat-buffer-is-spam))))
770          nil)
771         (spam-stat-save))
772
773       (defun spam-stat-register-ham-routine ()
774         (spam-generic-register-routine 
775          nil
776          (lambda (article)
777            (let ((article-string (spam-get-article-as-string article)))
778              (with-temp-buffer
779                (insert article-string)
780                (spam-stat-buffer-is-non-spam)))))
781         (spam-stat-save)))
782
783   (file-error (progn
784                 (defalias 'spam-stat-register-ham-routine 'ignore)
785                 (defalias 'spam-stat-register-spam-routine 'ignore)
786                 (defalias 'spam-stat-buffer-is-spam 'ignore)
787                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
788                 (defalias 'spam-stat-split-fancy 'ignore)
789                 (defalias 'spam-stat-load 'ignore)
790                 (defalias 'spam-stat-save 'ignore)
791                 (defalias 'spam-check-stat 'ignore))))
792
793 \f
794
795 ;;;; Blacklists and whitelists.
796
797 (defvar spam-whitelist-cache nil)
798 (defvar spam-blacklist-cache nil)
799
800 (defun spam-enter-whitelist (address)
801   "Enter ADDRESS into the whitelist."
802   (interactive "sAddress: ")
803   (spam-enter-list address spam-whitelist)
804   (setq spam-whitelist-cache nil))
805
806 (defun spam-enter-blacklist (address)
807   "Enter ADDRESS into the blacklist."
808   (interactive "sAddress: ")
809   (spam-enter-list address spam-blacklist)
810   (setq spam-blacklist-cache nil))
811
812 (defun spam-enter-list (address file)
813   "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
814   (unless (file-exists-p (file-name-directory file))
815     (make-directory (file-name-directory file) t))
816   (save-excursion
817     (set-buffer
818      (find-file-noselect file))
819     (goto-char (point-max))
820     (unless (bobp)
821       (insert "\n"))
822     (insert address "\n")
823     (save-buffer)))
824
825 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
826 (defun spam-check-whitelist ()
827   ;; FIXME!  Should it detect when file timestamps change?
828   (unless spam-whitelist-cache
829     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
830   (if (spam-from-listed-p spam-whitelist-cache) 
831       t
832     (if spam-use-whitelist-exclusive
833         spam-split-group
834       nil)))
835
836 (defun spam-check-blacklist ()
837   ;; FIXME!  Should it detect when file timestamps change?
838   (unless spam-blacklist-cache
839     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
840   (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
841
842 (defun spam-parse-list (file)
843   (when (file-readable-p file)
844     (let (contents address)
845       (with-temp-buffer
846         (insert-file-contents file)
847         (while (not (eobp))
848           (setq address (buffer-substring (point) (spam-point-at-eol)))
849           (forward-line 1)
850           (unless (zerop (length address))
851             (setq address (regexp-quote address))
852             (while (string-match "\\\\\\*" address)
853               (setq address (replace-match ".*" t t address)))
854             (push address contents))))
855       (nreverse contents))))
856
857 (defun spam-from-listed-p (cache)
858   (let ((from (message-fetch-field "from"))
859         found)
860     (while cache
861       (when (string-match (pop cache) from)
862         (setq found t
863               cache nil)))
864     found))
865
866 (defun spam-blacklist-register-routine ()
867   (spam-generic-register-routine 
868    ;; the spam function
869    (lambda (article)
870      (let ((from (spam-fetch-field-from-fast article)))
871        (when (stringp from)
872            (spam-enter-blacklist from))))
873    ;; the ham function
874    nil))
875
876 (defun spam-whitelist-register-routine ()
877   (spam-generic-register-routine 
878    ;; the spam function
879    nil 
880    ;; the ham function
881    (lambda (article)
882      (let ((from (spam-fetch-field-from-fast article)))
883        (when (stringp from)
884            (spam-enter-whitelist from))))))
885
886 \f
887 ;;;; Bogofilter
888
889 (defun spam-check-bogofilter-headers (&optional score)
890   (let ((header (message-fetch-field spam-bogofilter-header)))
891       (when (and header
892                  (string-match spam-bogofilter-bogosity-positive-spam-header
893                                header))
894           (if score
895               (when (string-match "spamicity=\\([0-9.]+\\)" header)
896                 (match-string 1 header))
897             spam-split-group))))
898
899 ;; return something sensible if the score can't be determined
900 (defun spam-bogofilter-score ()
901   "Get the Bogofilter spamicity score"
902   (interactive)
903   (save-window-excursion
904     (gnus-summary-show-article t)
905     (set-buffer gnus-article-buffer)
906     (let ((score (spam-check-bogofilter t)))
907       (message "Spamicity score %s" score)
908       (or score "0"))))
909
910 (defun spam-check-bogofilter (&optional score)
911   "Check the Bogofilter backend for the classification of this message"
912   (let ((article-buffer-name (buffer-name)) 
913         return)
914     (with-temp-buffer
915       (let ((temp-buffer-name (buffer-name)))
916         (save-excursion
917           (set-buffer article-buffer-name)
918           (if spam-bogofilter-database-directory
919               (call-process-region (point-min) (point-max) 
920                                    spam-bogofilter-path
921                                    nil temp-buffer-name nil "-v"
922                                    "-d" spam-bogofilter-database-directory)
923             (call-process-region (point-min) (point-max) spam-bogofilter-path
924                                  nil temp-buffer-name nil "-v")))
925         (setq return (spam-check-bogofilter-headers score))))
926     return))
927
928 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
929   "Register an article, given as a string, as spam or non-spam."
930   (when (stringp article-string)
931     (let ((switch (if spam "-s" "-n")))
932       (with-temp-buffer
933         (insert article-string)
934         (if spam-bogofilter-database-directory
935             (call-process-region (point-min) (point-max) 
936                                  spam-bogofilter-path
937                                  nil nil nil "-v" switch
938                                  "-d" spam-bogofilter-database-directory)
939           (call-process-region (point-min) (point-max) spam-bogofilter-path
940                                nil nil nil "-v" switch))))))
941
942 (defun spam-bogofilter-register-spam-routine ()
943   (spam-generic-register-routine 
944    (lambda (article)
945      (spam-bogofilter-register-with-bogofilter
946       (spam-get-article-as-string article) t))
947    nil))
948
949 (defun spam-bogofilter-register-ham-routine ()
950   (spam-generic-register-routine 
951    nil
952    (lambda (article)
953      (spam-bogofilter-register-with-bogofilter
954       (spam-get-article-as-string article) nil))))
955
956 (provide 'spam)
957
958 ;;; spam.el ends here.