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