Import Oort Gnus v0.13.
[elisp/gnus.git-] / 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-blacklist-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-buffer (spam-get-article-as-buffer article))
416                         article-string)
417     (when article-buffer
418       (save-window-excursion
419         (set-buffer article-buffer)
420         (setq article-string (buffer-string))))
421   article-string))
422
423 (defun spam-get-article-as-buffer (article)
424   (let ((article-buffer))
425     (when (numberp article)
426       (save-window-excursion
427         (gnus-summary-goto-subject article)
428         (gnus-summary-show-article t)
429         (setq article-buffer (get-buffer gnus-article-buffer))))
430     article-buffer))
431
432 (defun spam-get-article-as-filename (article)
433   (let ((article-filename))
434     (when (numberp article)
435       (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
436       (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
437     (if (file-exists-p article-filename)
438         article-filename
439       nil)))
440
441 (defun spam-fetch-field-from-fast (article)
442   "Fetch the `from' field quickly, using the internal gnus-data-list function"
443   (if (and (numberp article)
444            (assoc article (gnus-data-list nil)))
445       (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
446     nil))
447
448 (defun spam-fetch-field-subject-fast (article)
449   "Fetch the `subject' field quickly, using the internal gnus-data-list function"
450   (if (and (numberp article)
451            (assoc article (gnus-data-list nil)))
452       (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
453     nil))
454
455 \f
456 ;;;; Spam determination.
457
458 (defvar spam-list-of-checks
459   '((spam-use-blacklist                 .       spam-check-blacklist)
460     (spam-use-whitelist                 .       spam-check-whitelist)
461     (spam-use-BBDB                      .       spam-check-BBDB)
462     (spam-use-ifile                     .       spam-check-ifile)
463     (spam-use-stat                      .       spam-check-stat)
464     (spam-use-blackholes                .       spam-check-blackholes)
465     (spam-use-bogofilter-headers        .       spam-check-bogofilter-headers)
466     (spam-use-bogofilter                .       spam-check-bogofilter))
467 "The spam-list-of-checks list contains pairs associating a parameter
468 variable with a spam checking function.  If the parameter variable is
469 true, then the checking function is called, and its value decides what
470 happens.  Each individual check may return `nil', `t', or a mailgroup
471 name.  The value `nil' means that the check does not yield a decision,
472 and so, that further checks are needed.  The value `t' means that the
473 message is definitely not spam, and that further spam checks should be
474 inhibited.  Otherwise, a mailgroup name is returned where the mail
475 should go, and further checks are also inhibited.  The usual mailgroup
476 name is the value of `spam-split-group', meaning that the message is
477 definitely a spam.")
478
479 (defun spam-split ()
480   "Split this message into the `spam' group if it is spam.
481 This function can be used as an entry in `nnmail-split-fancy', for
482 example like this: (: spam-split)
483
484 See the Info node `(gnus)Fancy Mail Splitting' for more details."
485   (interactive)
486   
487   ;; load the spam-stat tables if needed
488   (when spam-use-stat (spam-stat-load))
489
490   (let ((list-of-checks spam-list-of-checks)
491         decision)
492     (while (and list-of-checks (not decision))
493       (let ((pair (pop list-of-checks)))
494         (when (symbol-value (car pair))
495           (setq decision (funcall (cdr pair))))))
496     (if (eq decision t)
497         nil
498       decision)))
499 \f
500 ;;;; Blackholes.
501
502 (defun spam-check-blackholes ()
503   "Check the Received headers for blackholed relays."
504   (let ((headers (message-fetch-field "received"))
505         ips matches)
506     (when headers
507       (with-temp-buffer
508         (insert headers)
509         (goto-char (point-min))
510         (while (re-search-forward
511                 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
512           (message "Blackhole search found host IP %s." (match-string 1))
513           (push (mapconcat 'identity
514                            (nreverse (split-string (match-string 1) "\\."))
515                            ".")
516                 ips)))
517       (dolist (server spam-blackhole-servers)
518         (dolist (ip ips)
519           (let ((query-string (concat ip "." server)))
520             (if spam-use-dig
521                 (let ((query-result (query-dig query-string)))
522                   (when query-result
523                     (message "spam: positive blackhole check '%s'" query-result)
524                     (push (list ip server query-result)
525                           matches)))
526               ;; else, if not using dig.el
527               (when (query-dns query-string)
528                 (push (list ip server (query-dns query-string 'TXT))
529                       matches)))))))
530     (when matches
531       spam-split-group)))
532 \f
533 ;;;; BBDB 
534
535 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
536 ;;; <sacha@giotto.sj.ru>
537
538 ;; all this is done inside a condition-case to trap errors
539
540 (condition-case nil
541     (progn
542       (require 'bbdb)
543       (require 'bbdb-com)
544       
545   (defun spam-enter-ham-BBDB (from)
546     "Enter an address into the BBDB; implies ham (non-spam) sender"
547     (when (stringp from)
548       (let* ((parsed-address (gnus-extract-address-components from))
549              (name (or (car parsed-address) "Ham Sender"))
550              (net-address (car (cdr parsed-address))))
551         (message "Adding address %s to BBDB" from)
552         (when (and net-address
553                    (not (bbdb-search-simple nil net-address)))
554           (bbdb-create-internal name nil net-address nil nil 
555                                 "ham sender added by spam.el")))))
556
557   (defun spam-BBDB-register-routine ()
558     (spam-generic-register-routine 
559      ;; spam function
560      nil
561      ;; ham function
562      (lambda (article)
563        (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
564
565   (defun spam-check-BBDB ()
566     "Mail from people in the BBDB is never considered spam"
567     (let ((who (message-fetch-field "from")))
568       (when who
569         (setq who (regexp-quote (cadr
570                                  (gnus-extract-address-components who))))
571         (if (bbdb-search-simple nil who)
572             nil spam-split-group)))))
573
574   (file-error (progn
575                 (defalias 'bbdb-search-simple 'ignore)
576                 (defalias 'spam-check-BBDB 'ignore)
577                 (defalias 'spam-BBDB-register-routine 'ignore)
578                 (defalias 'spam-enter-ham-BBDB 'ignore)
579                 (defalias 'bbdb-create-internal 'ignore)
580                 (defalias 'bbdb-records 'ignore))))
581
582 \f
583 ;;;; ifile
584
585 ;;; check the ifile backend; return nil if the mail was NOT classified
586 ;;; as spam
587
588 (defun spam-get-ifile-database-parameter ()
589   "Get the command-line parameter for ifile's database from spam-ifile-database-path."
590   (if spam-ifile-database-path
591       (format "--db-file=%s" spam-ifile-database-path)
592     nil))
593     
594 (defun spam-check-ifile ()
595   "Check the ifile backend for the classification of this message"
596   (let ((article-buffer-name (buffer-name)) 
597         category return)
598     (with-temp-buffer
599       (let ((temp-buffer-name (buffer-name))
600             (db-param (spam-get-ifile-database-parameter)))
601         (save-excursion
602           (set-buffer article-buffer-name)
603           (if db-param
604               (call-process-region (point-min) (point-max) spam-ifile-path
605                                    nil temp-buffer-name nil "-q" "-c" db-param)
606             (call-process-region (point-min) (point-max) spam-ifile-path
607                                  nil temp-buffer-name nil "-q" "-c")))
608         (goto-char (point-min))
609         (if (not (eobp))
610             (setq category (buffer-substring (point) (spam-point-at-eol))))
611         (when (not (zerop (length category))) ; we need a category here
612           (if spam-ifile-all-categories
613               (setq return category)
614             ;; else, if spam-ifile-all-categories is not set...
615             (when (string-equal spam-ifile-spam-category category)
616               (setq return spam-split-group))))))
617     return))
618
619 (defun spam-ifile-register-with-ifile (article-string category)
620   "Register an article, given as a string, with a category.
621 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
622   (when (stringp article-string)
623     (let ((category (or category gnus-newsgroup-name))
624           (db-param (spam-get-ifile-database-parameter)))
625       (with-temp-buffer
626         (insert-string article-string)
627         (if db-param
628             (call-process-region (point-min) (point-max) spam-ifile-path 
629                                  nil nil nil 
630                                  "-h" "-i" category db-param)
631           (call-process-region (point-min) (point-max) spam-ifile-path 
632                                nil nil nil 
633                                "-h" "-i" category))))))
634
635 (defun spam-ifile-register-spam-routine ()
636   (spam-generic-register-routine 
637    (lambda (article)
638      (spam-ifile-register-with-ifile 
639       (spam-get-article-as-string article) spam-ifile-spam-category))
640    nil))
641
642 (defun spam-ifile-register-ham-routine ()
643   (spam-generic-register-routine 
644    nil
645    (lambda (article)
646      (spam-ifile-register-with-ifile 
647       (spam-get-article-as-string article) nil))))
648
649 \f
650 ;;;; spam-stat
651
652 (condition-case nil
653     (progn
654       (let ((spam-stat-install-hooks nil))
655         (require 'spam-stat))
656       
657       (defun spam-check-stat ()
658         "Check the spam-stat backend for the classification of this message"
659         (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
660               (spam-stat-buffer (buffer-name)) ; stat the current buffer
661               category return)
662           (spam-stat-split-fancy)))
663
664       (defun spam-stat-register-spam-routine ()
665         (spam-generic-register-routine 
666          (lambda (article)
667            (let ((article-string (spam-get-article-as-string article)))
668              (with-temp-buffer
669                (insert-string article-string)
670                (spam-stat-buffer-is-spam))))
671          nil)
672         (spam-stat-save))
673
674       (defun spam-stat-register-ham-routine ()
675         (spam-generic-register-routine 
676          nil
677          (lambda (article)
678            (let ((article-string (spam-get-article-as-string article)))
679              (with-temp-buffer
680                (insert-string article-string)
681                (spam-stat-buffer-is-non-spam)))))
682         (spam-stat-save)))
683
684   (file-error (progn
685                 (defalias 'spam-stat-register-ham-routine 'ignore)
686                 (defalias 'spam-stat-register-spam-routine 'ignore)
687                 (defalias 'spam-stat-buffer-is-spam 'ignore)
688                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
689                 (defalias 'spam-stat-split-fancy 'ignore)
690                 (defalias 'spam-stat-load 'ignore)
691                 (defalias 'spam-stat-save 'ignore)
692                 (defalias 'spam-check-stat 'ignore))))
693
694 \f
695
696 ;;;; Blacklists and whitelists.
697
698 (defvar spam-whitelist-cache nil)
699 (defvar spam-blacklist-cache nil)
700
701 (defun spam-enter-whitelist (address)
702   "Enter ADDRESS into the whitelist."
703   (interactive "sAddress: ")
704   (spam-enter-list address spam-whitelist)
705   (setq spam-whitelist-cache nil))
706
707 (defun spam-enter-blacklist (address)
708   "Enter ADDRESS into the blacklist."
709   (interactive "sAddress: ")
710   (spam-enter-list address spam-blacklist)
711   (setq spam-blacklist-cache nil))
712
713 (defun spam-enter-list (address file)
714   "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
715   (unless (file-exists-p (file-name-directory file))
716     (make-directory (file-name-directory file) t))
717   (save-excursion
718     (set-buffer
719      (find-file-noselect file))
720     (goto-char (point-max))
721     (unless (bobp)
722       (insert "\n"))
723     (insert address "\n")
724     (save-buffer)))
725
726 ;;; returns nil if the sender is in the whitelist, spam-split-group otherwise
727 (defun spam-check-whitelist ()
728   ;; FIXME!  Should it detect when file timestamps change?
729   (unless spam-whitelist-cache
730     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
731   (if (spam-from-listed-p spam-whitelist-cache) nil spam-split-group))
732
733 (defun spam-check-blacklist ()
734   ;; FIXME!  Should it detect when file timestamps change?
735   (unless spam-blacklist-cache
736     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
737   (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
738
739 (defun spam-parse-list (file)
740   (when (file-readable-p file)
741     (let (contents address)
742       (with-temp-buffer
743         (insert-file-contents file)
744         (while (not (eobp))
745           (setq address (buffer-substring (point) (spam-point-at-eol)))
746           (forward-line 1)
747           (unless (zerop (length address))
748             (setq address (regexp-quote address))
749             (while (string-match "\\\\\\*" address)
750               (setq address (replace-match ".*" t t address)))
751             (push address contents))))
752       (nreverse contents))))
753
754 (defun spam-from-listed-p (cache)
755   (let ((from (message-fetch-field "from"))
756         found)
757     (while cache
758       (when (string-match (pop cache) from)
759         (setq found t
760               cache nil)))
761     found))
762
763 (defun spam-blacklist-register-routine ()
764   (spam-generic-register-routine 
765    ;; the spam function
766    (lambda (article)
767      (let ((from (spam-fetch-field-from-fast article)))
768        (when (stringp from)
769            (spam-enter-blacklist from))))
770    ;; the ham function
771    nil))
772
773 (defun spam-whitelist-register-routine ()
774   (spam-generic-register-routine 
775    ;; the spam function
776    nil 
777    ;; the ham function
778    (lambda (article)
779      (let ((from (spam-fetch-field-from-fast article)))
780        (when (stringp from)
781            (spam-enter-whitelist from))))))
782
783 \f
784 ;;;; Bogofilter
785
786 (defun spam-check-bogofilter-headers (&optional score)
787   (let ((header (message-fetch-field spam-bogofilter-header)))
788       (when (and header
789                (string-match "^Yes" header))
790           (if score
791               (when (string-match "spamicity=\\([0-9.]+\\)" header)
792                 (match-string 1 header))
793             spam-split-group))))
794           
795
796 ;; return something sensible if the score can't be determined
797 (defun spam-bogofilter-score ()
798   "Get the Bogofilter spamicity score"
799   (interactive)
800   (save-window-excursion
801     (gnus-summary-show-article t)
802     (set-buffer gnus-article-buffer)
803     (let ((score (spam-check-bogofilter t)))
804       (message "Spamicity score %s" score)
805       (or score "0"))))
806
807 (defun spam-check-bogofilter (&optional score)
808   "Check the Bogofilter backend for the classification of this message"
809   (let ((article-buffer-name (buffer-name)) 
810         return)
811     (with-temp-buffer
812       (let ((temp-buffer-name (buffer-name)))
813         (save-excursion
814           (set-buffer article-buffer-name)
815           (if spam-bogofilter-database-directory
816               (call-process-region (point-min) (point-max) 
817                                    spam-bogofilter-path
818                                    nil temp-buffer-name nil "-v"
819                                    "-d" spam-bogofilter-database-directory)
820             (call-process-region (point-min) (point-max) spam-bogofilter-path
821                                  nil temp-buffer-name nil "-v")))
822         (setq return (spam-check-bogofilter-headers score))))
823     return))
824
825 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
826   "Register an article, given as a string, as spam or non-spam."
827   (when (stringp article-string)
828     (let ((switch (if spam "-s" "-n")))
829       (with-temp-buffer
830         (insert-string article-string)
831         (if spam-bogofilter-database-directory
832             (call-process-region (point-min) (point-max) 
833                                  spam-bogofilter-path
834                                  nil nil nil "-v" switch
835                                  "-d" spam-bogofilter-database-directory)
836           (call-process-region (point-min) (point-max) spam-bogofilter-path
837                                nil nil nil "-v" switch))))))
838
839 (defun spam-bogofilter-register-spam-routine ()
840   (spam-generic-register-routine 
841    (lambda (article)
842      (spam-bogofilter-register-with-bogofilter
843       (spam-get-article-as-string article) t))
844    nil))
845
846 (defun spam-bogofilter-register-ham-routine ()
847   (spam-generic-register-routine 
848    nil
849    (lambda (article)
850      (spam-bogofilter-register-with-bogofilter
851       (spam-get-article-as-string article) nil))))
852
853 (provide 'spam)
854
855 ;;; spam.el ends here.