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