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