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