Synch to No Gnus 200401062338.
[elisp/gnus.git-] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004 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 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting,
36 ;; remote processing, training through files
37
38 ;;; Code:
39
40 (require 'path-util)
41
42 (eval-when-compile (require 'cl))
43
44 (require 'gnus-sum)
45
46 (require 'gnus-uu)                      ; because of key prefix issues
47 ;;; for the definitions of group content classification and spam processors
48 (require 'gnus)
49 (require 'message)              ;for the message-fetch-field functions
50
51 ;; for nnimap-split-download-body-default
52 (eval-when-compile (require 'nnimap))
53
54 ;; autoload query-dig
55 (eval-and-compile
56   (autoload 'query-dig "dig"))
57
58 ;; autoload spam-report
59 (eval-and-compile
60   (autoload 'spam-report-gmane "spam-report"))
61
62 ;; autoload gnus-registry
63 (eval-and-compile
64   (autoload 'gnus-registry-group-count "gnus-registry")
65   (autoload 'gnus-registry-add-group "gnus-registry")
66   (autoload 'gnus-registry-store-extra-entry "gnus-registry")
67   (autoload 'gnus-registry-fetch-extra "gnus-registry"))
68
69 ;; autoload query-dns
70 (eval-and-compile
71   (autoload 'query-dns "dns"))
72
73 ;;; Main parameters.
74
75 (defgroup spam nil
76   "Spam configuration.")
77
78 (defcustom spam-directory "~/News/spam/"
79   "Directory for spam whitelists and blacklists."
80   :type 'directory
81   :group 'spam)
82
83 (defcustom spam-move-spam-nonspam-groups-only t
84   "Whether spam should be moved in non-spam groups only.
85 When t, only ham and unclassified groups will have their spam moved
86 to the spam-process-destination.  When nil, spam will also be moved from
87 spam groups."
88   :type 'boolean
89   :group 'spam)
90
91 (defcustom spam-process-ham-in-nonham-groups nil
92   "Whether ham should be processed in non-ham groups."
93   :type 'boolean
94   :group 'spam)
95
96 (defcustom spam-log-to-registry nil
97   "Whether spam/ham processing should be logged in the registry."
98   :type 'boolean
99   :group 'spam)
100
101 (defcustom spam-split-symbolic-return nil
102   "Whether `spam-split' should work with symbols or group names."
103   :type 'boolean
104   :group 'spam)
105
106 (defcustom spam-split-symbolic-return-positive nil
107   "Whether `spam-split' should ALWAYS work with symbols or group names.
108 Do not set this if you use `spam-split' in a fancy split
109   method."
110   :type 'boolean
111   :group 'spam)
112
113 (defcustom spam-process-ham-in-spam-groups nil
114   "Whether ham should be processed in spam groups."
115   :type 'boolean
116   :group 'spam)
117
118 (defcustom spam-mark-only-unseen-as-spam t
119   "Whether only unseen articles should be marked as spam in spam groups.
120 When nil, all unread articles in a spam group are marked as
121 spam.  Set this if you want to leave an article unread in a spam group
122 without losing it to the automatic spam-marking process."
123   :type 'boolean
124   :group 'spam)
125
126 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
127   "Whether ham should be marked unread before it's moved.
128 The article is moved out of a spam group according to ham-process-destination.
129 This variable is an official entry in the international Longest Variable Name
130 Competition."
131   :type 'boolean
132   :group 'spam)
133
134 (defcustom spam-disable-spam-split-during-ham-respool nil
135   "Whether `spam-split' should be ignored while resplitting ham in a process
136 destination.  This is useful to prevent ham from ending up in the same spam
137 group after the resplit.  Don't set this to t if you have spam-split as the
138 last rule in your split configuration."
139   :type 'boolean
140   :group 'spam)
141
142 (defcustom spam-autodetect-recheck-messages nil
143   "Should spam.el recheck all meessages when autodetecting?
144 Normally this is nil, so only unseen messages will be checked."
145   :type 'boolean
146   :group 'spam)
147
148 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
149   "The location of the whitelist.
150 The file format is one regular expression per line.
151 The regular expression is matched against the address."
152   :type 'file
153   :group 'spam)
154
155 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
156   "The location of the blacklist.
157 The file format is one regular expression per line.
158 The regular expression is matched against the address."
159   :type 'file
160   :group 'spam)
161
162 (defcustom spam-use-dig t
163   "Whether `query-dig' should be used instead of `query-dns'."
164   :type 'boolean
165   :group 'spam)
166
167 (defcustom spam-use-blacklist nil
168   "Whether the blacklist should be used by `spam-split'."
169   :type 'boolean
170   :group 'spam)
171
172 (defcustom spam-blacklist-ignored-regexes nil
173   "Regular expressions that the blacklist should ignore."
174   :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
175   :group 'spam)
176
177 (defcustom spam-use-whitelist nil
178   "Whether the whitelist should be used by `spam-split'."
179   :type 'boolean
180   :group 'spam)
181
182 (defcustom spam-use-whitelist-exclusive nil
183   "Whether whitelist-exclusive should be used by `spam-split'.
184 Exclusive whitelisting means that all messages from senders not in the whitelist
185 are considered spam."
186   :type 'boolean
187   :group 'spam)
188
189 (defcustom spam-use-blackholes nil
190   "Whether blackholes should be used by `spam-split'."
191   :type 'boolean
192   :group 'spam)
193
194 (defcustom spam-use-hashcash nil
195   "Whether hashcash payments should be detected by `spam-split'."
196   :type 'boolean
197   :group 'spam)
198
199 (defcustom spam-use-regex-headers nil
200   "Whether a header regular expression match should be used by `spam-split'.
201 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
202   :type 'boolean
203   :group 'spam)
204
205 (defcustom spam-use-regex-body nil
206   "Whether a body regular expression match should be used by `spam-split'.
207 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
208   :type 'boolean
209   :group 'spam)
210
211 (defcustom spam-use-bogofilter-headers nil
212   "Whether bogofilter headers should be used by `spam-split'.
213 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
214   :type 'boolean
215   :group 'spam)
216
217 (defcustom spam-use-bogofilter nil
218   "Whether bogofilter should be invoked by `spam-split'.
219 Enable this if you want Gnus to invoke Bogofilter on new messages."
220   :type 'boolean
221   :group 'spam)
222
223 (defcustom spam-use-BBDB nil
224   "Whether BBDB should be used by `spam-split'."
225   :type 'boolean
226   :group 'spam)
227
228 (defcustom spam-use-BBDB-exclusive nil
229   "Whether BBDB-exclusive should be used by `spam-split'.
230 Exclusive BBDB means that all messages from senders not in the BBDB are
231 considered spam."
232   :type 'boolean
233   :group 'spam)
234
235 (defcustom spam-use-ifile nil
236   "Whether ifile should be used by `spam-split'."
237   :type 'boolean
238   :group 'spam)
239
240 (defcustom spam-use-stat nil
241   "Whether `spam-stat' should be used by `spam-split'."
242   :type 'boolean
243   :group 'spam)
244
245 (defcustom spam-use-spamoracle nil
246   "Whether spamoracle should be used by `spam-split'."
247   :type 'boolean
248   :group 'spam)
249
250 (defcustom spam-install-hooks (or
251                                spam-use-dig
252                                spam-use-blacklist
253                                spam-use-whitelist
254                                spam-use-whitelist-exclusive
255                                spam-use-blackholes
256                                spam-use-hashcash
257                                spam-use-regex-headers
258                                spam-use-regex-body
259                                spam-use-bogofilter-headers
260                                spam-use-bogofilter
261                                spam-use-BBDB
262                                spam-use-BBDB-exclusive
263                                spam-use-ifile
264                                spam-use-stat
265                                spam-use-spamoracle)
266   "Whether the spam hooks should be installed.
267 Default to t if one of the spam-use-* variables is set."
268   :group 'spam
269   :type 'boolean)
270
271 (defcustom spam-split-group "spam"
272   "Group name where incoming spam should be put by `spam-split'."
273   :type 'string
274   :group 'spam)
275
276 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
277 ;;; not regular expressions
278 (defcustom spam-junk-mailgroups (cons
279                                  spam-split-group
280                                  '("mail.junk" "poste.pourriel"))
281   "Mailgroups with spam contents.
282 All unmarked article in such group receive the spam mark on group entry."
283   :type '(repeat (string :tag "Group"))
284   :group 'spam)
285
286 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
287                                     "dev.null.dk" "relays.visi.com")
288   "List of blackhole servers."
289   :type '(repeat (string :tag "Server"))
290   :group 'spam)
291
292 (defcustom spam-blackhole-good-server-regex nil
293   "String matching IP addresses that should not be checked in the blackholes."
294   :type '(radio (const nil)
295                 (regexp :format "%t: %v\n" :size 0))
296   :group 'spam)
297
298 (defcustom spam-face 'gnus-splash-face
299   "Face for spam-marked articles."
300   :type 'face
301   :group 'spam)
302
303 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
304   "Regular expression for positive header spam matches."
305   :type '(repeat (regexp :tag "Regular expression to match spam header"))
306   :group 'spam)
307
308 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
309   "Regular expression for positive header ham matches."
310   :type '(repeat (regexp :tag "Regular expression to match ham header"))
311   :group 'spam)
312
313 (defcustom spam-regex-body-spam '()
314   "Regular expression for positive body spam matches."
315   :type '(repeat (regexp :tag "Regular expression to match spam body"))
316   :group 'spam)
317
318 (defcustom spam-regex-body-ham '()
319   "Regular expression for positive body ham matches."
320   :type '(repeat (regexp :tag "Regular expression to match ham body"))
321   :group 'spam)
322
323 (defgroup spam-ifile nil
324   "Spam ifile configuration."
325   :group 'spam)
326
327 (defcustom spam-ifile-path (exec-installed-p "ifile")
328   "File path of the ifile executable program."
329   :type '(choice (file :tag "Location of ifile")
330                  (const :tag "ifile is not installed"))
331   :group 'spam-ifile)
332
333 (defcustom spam-ifile-database-path nil
334   "File path of the ifile database."
335   :type '(choice (file :tag "Location of the ifile database")
336                  (const :tag "Use the default"))
337   :group 'spam-ifile)
338
339 (defcustom spam-ifile-spam-category "spam"
340   "Name of the spam ifile category."
341   :type 'string
342   :group 'spam-ifile)
343
344 (defcustom spam-ifile-ham-category nil
345   "Name of the ham ifile category.
346 If nil, the current group name will be used."
347   :type '(choice (string :tag "Use a fixed category")
348                  (const :tag "Use the current group name"))
349   :group 'spam-ifile)
350
351 (defcustom spam-ifile-all-categories nil
352   "Whether the ifile check will return all categories, or just spam.
353 Set this to t if you want to use the `spam-split' invocation of ifile as
354 your main source of newsgroup names."
355   :type 'boolean
356   :group 'spam-ifile)
357
358 (defgroup spam-bogofilter nil
359   "Spam bogofilter configuration."
360   :group 'spam)
361
362 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
363   "File path of the Bogofilter executable program."
364   :type '(choice (file :tag "Location of bogofilter")
365                  (const :tag "Bogofilter is not installed"))
366   :group 'spam-bogofilter)
367
368 (defcustom spam-bogofilter-header "X-Bogosity"
369   "The header that Bogofilter inserts in messages."
370   :type 'string
371   :group 'spam-bogofilter)
372
373 (defcustom spam-bogofilter-spam-switch "-s"
374   "The switch that Bogofilter uses to register spam messages."
375   :type 'string
376   :group 'spam-bogofilter)
377
378 (defcustom spam-bogofilter-ham-switch "-n"
379   "The switch that Bogofilter uses to register ham messages."
380   :type 'string
381   :group 'spam-bogofilter)
382
383 (defcustom spam-bogofilter-spam-strong-switch "-S"
384   "The switch that Bogofilter uses to unregister ham messages."
385   :type 'string
386   :group 'spam-bogofilter)
387
388 (defcustom spam-bogofilter-ham-strong-switch "-N"
389   "The switch that Bogofilter uses to unregister spam messages."
390   :type 'string
391   :group 'spam-bogofilter)
392
393 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
394   "The regex on `spam-bogofilter-header' for positive spam identification."
395   :type 'regexp
396   :group 'spam-bogofilter)
397
398 (defcustom spam-bogofilter-database-directory nil
399   "Directory path of the Bogofilter databases."
400   :type '(choice (directory
401                   :tag "Location of the Bogofilter database directory")
402                  (const :tag "Use the default"))
403   :group 'spam-bogofilter)
404
405 (defgroup spam-spamoracle nil
406   "Spam spamoracle configuration."
407   :group 'spam)
408
409 (defcustom spam-spamoracle-database nil
410   "Location of spamoracle database file. When nil, use the default
411 spamoracle database."
412   :type '(choice (directory :tag "Location of spamoracle database file.")
413                  (const :tag "Use the default"))
414   :group 'spam-spamoracle)
415
416 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
417   "Location of the spamoracle binary."
418   :type '(choice (directory :tag "Location of the spamoracle binary")
419                  (const :tag "Use the default"))
420   :group 'spam-spamoracle)
421
422 ;;; Key bindings for spam control.
423
424 (gnus-define-keys gnus-summary-mode-map
425   "St" spam-bogofilter-score
426   "Sx" gnus-summary-mark-as-spam
427   "Mst" spam-bogofilter-score
428   "Msx" gnus-summary-mark-as-spam
429   "\M-d" gnus-summary-mark-as-spam)
430
431 (defvar spam-cache-lookups nil
432   "Whether spam.el will try to cache lookups using spam-caches.")
433
434 (defvar spam-caches (make-hash-table
435                      :size 10
436                      :test 'equal)
437   "Cache of spam detection entries.")
438
439 (defvar spam-old-ham-articles nil
440   "List of old ham articles, generated when a group is entered.")
441
442 (defvar spam-old-spam-articles nil
443   "List of old spam articles, generated when a group is entered.")
444
445 (defvar spam-split-disabled nil
446   "If non-nil, `spam-split' is disabled, and always returns nil.")
447
448 (defvar spam-split-last-successful-check nil
449   "`spam-split' will set this to nil or a spam-use-XYZ check if it
450   finds ham or spam.")
451
452 ;; convenience functions
453 (defun spam-clear-cache (symbol)
454   (remhash symbol spam-caches))
455
456 (defun spam-xor (a b)
457   "Logical exclusive `or'."
458   (and (or a b) (not (and a b))))
459
460 (defun spam-group-ham-mark-p (group mark &optional spam)
461   (when (stringp group)
462     (let* ((marks (spam-group-ham-marks group spam))
463            (marks (if (symbolp mark)
464                       marks
465                     (mapcar 'symbol-value marks))))
466       (memq mark marks))))
467
468 (defun spam-group-spam-mark-p (group mark)
469   (spam-group-ham-mark-p group mark t))
470
471 (defun spam-group-ham-marks (group &optional spam)
472   (when (stringp group)
473     (let* ((marks (if spam
474                       (gnus-parameter-spam-marks group)
475                     (gnus-parameter-ham-marks group)))
476            (marks (car marks))
477            (marks (if (listp (car marks)) (car marks) marks)))
478       marks)))
479
480 (defun spam-group-spam-marks (group)
481   (spam-group-ham-marks group t))
482
483 (defun spam-group-spam-contents-p (group)
484   (if (stringp group)
485       (or (member group spam-junk-mailgroups)
486           (memq 'gnus-group-spam-classification-spam
487                 (gnus-parameter-spam-contents group)))
488     nil))
489
490 (defun spam-group-ham-contents-p (group)
491   (if (stringp group)
492       (memq 'gnus-group-spam-classification-ham
493             (gnus-parameter-spam-contents group))
494     nil))
495
496 (defvar spam-list-of-processors
497   '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
498     (gnus-group-spam-exit-processor-bogofilter   spam spam-use-bogofilter)
499     (gnus-group-spam-exit-processor-blacklist    spam spam-use-blacklist)
500     (gnus-group-spam-exit-processor-ifile        spam spam-use-ifile)
501     (gnus-group-spam-exit-processor-stat         spam spam-use-stat)
502     (gnus-group-spam-exit-processor-spamoracle   spam spam-use-spamoracle)
503     (gnus-group-ham-exit-processor-ifile         ham spam-use-ifile)
504     (gnus-group-ham-exit-processor-bogofilter    ham spam-use-bogofilter)
505     (gnus-group-ham-exit-processor-stat          ham spam-use-stat)
506     (gnus-group-ham-exit-processor-whitelist     ham spam-use-whitelist)
507     (gnus-group-ham-exit-processor-BBDB          ham spam-use-BBDB)
508     (gnus-group-ham-exit-processor-copy          ham spam-use-ham-copy)
509     (gnus-group-ham-exit-processor-spamoracle    ham spam-use-spamoracle))
510   "The spam-list-of-processors list contains pairs associating a
511 ham/spam exit processor variable with a classification and a
512 spam-use-* variable.")
513
514 (defun spam-group-processor-p (group processor)
515   (if (and (stringp group)
516            (symbolp processor))
517       (or (member processor (nth 0 (gnus-parameter-spam-process group)))
518           (spam-group-processor-multiple-p
519            group
520            (cdr-safe (assoc processor spam-list-of-processors))))
521     nil))
522
523 (defun spam-group-processor-multiple-p (group processor-info)
524   (let* ((classification (nth 0 processor-info))
525          (check (nth 1 processor-info))
526          (parameters (nth 0 (gnus-parameter-spam-process group)))
527          found)
528     (dolist (parameter parameters)
529       (when (and (null found)
530                  (listp parameter)
531                  (eq classification (nth 0 parameter))
532                  (eq check (nth 1 parameter)))
533         (setq found t)))
534     found))
535
536 (defun spam-group-spam-processor-report-gmane-p (group)
537   (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
538
539 (defun spam-group-spam-processor-bogofilter-p (group)
540   (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
541
542 (defun spam-group-spam-processor-blacklist-p (group)
543   (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
544
545 (defun spam-group-spam-processor-ifile-p (group)
546   (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
547
548 (defun spam-group-ham-processor-ifile-p (group)
549   (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
550
551 (defun spam-group-spam-processor-spamoracle-p (group)
552   (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
553
554 (defun spam-group-ham-processor-bogofilter-p (group)
555   (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
556
557 (defun spam-group-spam-processor-stat-p (group)
558   (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
559
560 (defun spam-group-ham-processor-stat-p (group)
561   (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
562
563 (defun spam-group-ham-processor-whitelist-p (group)
564   (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
565
566 (defun spam-group-ham-processor-BBDB-p (group)
567   (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
568
569 (defun spam-group-ham-processor-copy-p (group)
570   (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
571
572 (defun spam-group-ham-processor-spamoracle-p (group)
573   (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
574
575 ;;; Summary entry and exit processing.
576
577 (defun spam-summary-prepare ()
578   (setq spam-old-ham-articles
579         (spam-list-articles gnus-newsgroup-articles 'ham))
580   (setq spam-old-spam-articles
581         (spam-list-articles gnus-newsgroup-articles 'spam))
582   (spam-mark-junk-as-spam-routine))
583
584 ;; The spam processors are invoked for any group, spam or ham or neither
585 (defun spam-summary-prepare-exit ()
586   (unless gnus-group-is-exiting-without-update-p
587     (gnus-message 6 "Exiting summary buffer and applying spam rules")
588
589     ;; first of all, unregister any articles that are no longer ham or spam
590     ;; we have to iterate over the processors, or else we'll be too slow
591     (dolist (classification '(spam ham))
592       (let* ((old-articles (if (eq classification 'spam)
593                                spam-old-spam-articles
594                              spam-old-ham-articles))
595              (new-articles (spam-list-articles
596                             gnus-newsgroup-articles
597                             classification))
598              (changed-articles (gnus-set-difference old-articles new-articles)))
599         ;; now that we have the changed articles, we go through the processors
600         (dolist (processor-param spam-list-of-processors)
601           (let ((processor (nth 0 processor-param))
602                 (processor-classification (nth 1 processor-param))
603                 (check (nth 2 processor-param))
604                 unregister-list)
605             (dolist (article changed-articles)
606               (let ((id (spam-fetch-field-message-id-fast article)))
607                 (when (spam-log-unregistration-needed-p
608                        id 'process classification check)
609                   (push article unregister-list))))
610             ;; call spam-register-routine with specific articles to unregister,
611             ;; when there are articles to unregister and the check is enabled
612             (when (and unregister-list (symbol-value check))
613               (spam-register-routine classification check t unregister-list))))))
614
615     ;; find all the spam processors applicable to this group
616     (dolist (processor-param spam-list-of-processors)
617       (let ((processor (nth 0 processor-param))
618             (classification (nth 1 processor-param))
619             (check (nth 2 processor-param)))
620         (when (and (eq 'spam classification)
621                    (spam-group-processor-p gnus-newsgroup-name processor))
622           (spam-register-routine classification check))))
623
624     (if spam-move-spam-nonspam-groups-only
625         (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
626           (spam-mark-spam-as-expired-and-move-routine
627            (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
628       (gnus-message 5 "Marking spam as expired and moving it to %s"
629                     gnus-newsgroup-name)
630       (spam-mark-spam-as-expired-and-move-routine
631        (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
632
633     ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
634     ;; expire spam, in case the above did not expire them
635     (gnus-message 5 "Marking spam as expired without moving it")
636     (spam-mark-spam-as-expired-and-move-routine nil)
637
638     (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
639               (and (spam-group-spam-contents-p gnus-newsgroup-name)
640                    spam-process-ham-in-spam-groups)
641               spam-process-ham-in-nonham-groups)
642       ;; find all the ham processors applicable to this group
643       (dolist (processor-param spam-list-of-processors)
644         (let ((processor (nth 0 processor-param))
645               (classification (nth 1 processor-param))
646               (check (nth 2 processor-param)))
647           (when (and (eq 'ham classification)
648                      (spam-group-processor-p gnus-newsgroup-name processor))
649             (spam-register-routine classification check)))))
650
651     (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
652       (gnus-message 5 "Copying ham")
653       (spam-ham-copy-routine
654        (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
655
656     ;; now move all ham articles out of spam groups
657     (when (spam-group-spam-contents-p gnus-newsgroup-name)
658       (gnus-message 5 "Moving ham messages from spam group")
659       (spam-ham-move-routine
660        (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
661
662   (setq spam-old-ham-articles nil)
663   (setq spam-old-spam-articles nil))
664
665 (defun spam-mark-junk-as-spam-routine ()
666   ;; check the global list of group names spam-junk-mailgroups and the
667   ;; group parameters
668   (when (spam-group-spam-contents-p gnus-newsgroup-name)
669     (gnus-message 5 "Marking %s articles as spam"
670                   (if spam-mark-only-unseen-as-spam
671                       "unseen"
672                     "unread"))
673     (let ((articles (if spam-mark-only-unseen-as-spam
674                         gnus-newsgroup-unseen
675                       gnus-newsgroup-unreads)))
676       (dolist (article articles)
677         (gnus-summary-mark-article article gnus-spam-mark)))))
678
679 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
680   (if (and (car-safe groups) (listp (car-safe groups)))
681       (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
682     (gnus-summary-kill-process-mark)
683     (let ((articles gnus-newsgroup-articles)
684           (backend-supports-deletions
685            (gnus-check-backend-function
686             'request-move-article gnus-newsgroup-name))
687           article tomove deletep)
688       (dolist (article articles)
689         (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
690           (gnus-summary-mark-article article gnus-expirable-mark)
691           (push article tomove)))
692
693       ;; now do the actual copies
694       (dolist (group groups)
695         (when (and tomove
696                    (stringp group))
697           (dolist (article tomove)
698             (gnus-summary-set-process-mark article))
699           (when tomove
700             (if (or (not backend-supports-deletions)
701                     (> (length groups) 1))
702                 (progn
703                   (gnus-summary-copy-article nil group)
704                   (setq deletep t))
705               (gnus-summary-move-article nil group)))))
706
707       ;; now delete the articles, if there was a copy done, and the
708       ;; backend allows it
709       (when (and deletep backend-supports-deletions)
710         (dolist (article tomove)
711           (gnus-summary-set-process-mark article))
712         (when tomove
713           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
714             (gnus-summary-delete-article nil))))
715
716       (gnus-summary-yank-process-mark))))
717
718 (defun spam-ham-copy-or-move-routine (copy groups)
719   (gnus-summary-kill-process-mark)
720   (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
721         (backend-supports-deletions
722          (gnus-check-backend-function
723           'request-move-article gnus-newsgroup-name))
724         (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
725         article mark todo deletep respool)
726
727     (when (member 'respool groups)
728       (setq respool t)                  ; boolean for later
729       (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
730
731     ;; now do the actual move
732     (dolist (group groups)
733       (when (and todo (stringp group))
734         (dolist (article todo)
735           (when spam-mark-ham-unread-before-move-from-spam-group
736             (gnus-summary-mark-article article gnus-unread-mark))
737           (gnus-summary-set-process-mark article))
738
739         (if respool                        ; respooling is with a "fake" group
740             (let ((spam-split-disabled
741                    (or spam-split-disabled
742                        spam-disable-spam-split-during-ham-respool)))
743               (gnus-summary-respool-article nil respool-method))
744           (if (or (not backend-supports-deletions) ; else, we are not respooling
745                   (> (length groups) 1))
746               (progn                ; if copying, copy and set deletep
747                 (gnus-summary-copy-article nil group)
748                 (setq deletep t))
749             (gnus-summary-move-article nil group))))) ; else move articles
750
751     ;; now delete the articles, unless a) copy is t, and there was a copy done
752     ;;                                 b) a move was done to a single group
753     ;;                                 c) backend-supports-deletions is nil
754     (unless copy
755       (when (and deletep backend-supports-deletions)
756         (dolist (article todo)
757           (gnus-summary-set-process-mark article))
758         (when todo
759           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
760             (gnus-summary-delete-article nil))))))
761
762   (gnus-summary-yank-process-mark))
763
764 (defun spam-ham-copy-routine (&rest groups)
765   (if (and (car-safe groups) (listp (car-safe groups)))
766       (apply 'spam-ham-copy-routine (car groups))
767     (spam-ham-copy-or-move-routine t groups)))
768
769 (defun spam-ham-move-routine (&rest groups)
770   (if (and (car-safe groups) (listp (car-safe groups)))
771       (apply 'spam-ham-move-routine (car groups))
772     (spam-ham-copy-or-move-routine nil groups)))
773
774 (defun spam-get-article-as-string (article)
775   (let ((article-buffer (spam-get-article-as-buffer article))
776         article-string)
777     (when article-buffer
778       (save-window-excursion
779         (set-buffer article-buffer)
780         (setq article-string (buffer-string))))
781     article-string))
782
783 (defun spam-get-article-as-buffer (article)
784   (let ((article-buffer))
785     (when (numberp article)
786       (save-window-excursion
787         (gnus-summary-goto-subject article)
788         (gnus-summary-show-article t)
789         (setq article-buffer (get-buffer gnus-article-buffer))))
790     article-buffer))
791
792 ;; disabled for now
793 ;; (defun spam-get-article-as-filename (article)
794 ;;   (let ((article-filename))
795 ;;     (when (numberp article)
796 ;;       (nnml-possibly-change-directory
797 ;;        (gnus-group-real-name gnus-newsgroup-name))
798 ;;       (setq article-filename (expand-file-name
799 ;;                              (int-to-string article) nnml-current-directory)))
800 ;;     (if (file-exists-p article-filename)
801 ;;      article-filename
802 ;;       nil)))
803
804 (defun spam-fetch-field-from-fast (article)
805   "Fetch the `from' field quickly, using the internal gnus-data-list function"
806   (if (and (numberp article)
807            (assoc article (gnus-data-list nil)))
808       (mail-header-from
809        (gnus-data-header (assoc article (gnus-data-list nil))))
810     nil))
811
812 (defun spam-fetch-field-subject-fast (article)
813   "Fetch the `subject' field quickly, using the internal
814   gnus-data-list function"
815   (if (and (numberp article)
816            (assoc article (gnus-data-list nil)))
817       (mail-header-subject
818        (gnus-data-header (assoc article (gnus-data-list nil))))
819     nil))
820
821 (defun spam-fetch-field-message-id-fast (article)
822   "Fetch the `Message-ID' field quickly, using the internal
823   gnus-data-list function"
824   (if (and (numberp article)
825            (assoc article (gnus-data-list nil)))
826       (mail-header-message-id
827        (gnus-data-header (assoc article (gnus-data-list nil))))
828     nil))
829
830 \f
831 ;;;; Spam determination.
832
833 (defvar spam-list-of-checks
834   '((spam-use-blacklist          . spam-check-blacklist)
835     (spam-use-regex-headers      . spam-check-regex-headers)
836     (spam-use-regex-body         . spam-check-regex-body)
837     (spam-use-whitelist          . spam-check-whitelist)
838     (spam-use-BBDB               . spam-check-BBDB)
839     (spam-use-ifile              . spam-check-ifile)
840     (spam-use-spamoracle         . spam-check-spamoracle)
841     (spam-use-stat               . spam-check-stat)
842     (spam-use-blackholes         . spam-check-blackholes)
843     (spam-use-hashcash           . spam-check-hashcash)
844     (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
845     (spam-use-bogofilter         . spam-check-bogofilter))
846   "The spam-list-of-checks list contains pairs associating a
847 parameter variable with a spam checking function.  If the
848 parameter variable is true, then the checking function is called,
849 and its value decides what happens.  Each individual check may
850 return nil, t, or a mailgroup name.  The value nil means that the
851 check does not yield a decision, and so, that further checks are
852 needed.  The value t means that the message is definitely not
853 spam, and that further spam checks should be inhibited.
854 Otherwise, a mailgroup name or the symbol 'spam (depending on
855 spam-split-symbolic-return) is returned where the mail should go,
856 and further checks are also inhibited.  The usual mailgroup name
857 is the value of `spam-split-group', meaning that the message is
858 definitely a spam.")
859
860 (defvar spam-list-of-statistical-checks
861   '(spam-use-ifile
862     spam-use-regex-body
863     spam-use-stat
864     spam-use-bogofilter
865     spam-use-spamoracle)
866   "The spam-list-of-statistical-checks list contains all the mail
867 splitters that need to have the full message body available.")
868
869 ;;;TODO: modify to invoke self with each check if invoked without specifics
870 (defun spam-split (&rest specific-checks)
871   "Split this message into the `spam' group if it is spam.
872 This function can be used as an entry in the variable `nnmail-split-fancy',
873 for example like this: (: spam-split).  It can take checks as
874 parameters.  A string as a parameter will set the
875 spam-split-group to that string.
876
877 See the Info node `(gnus)Fancy Mail Splitting' for more details."
878   (interactive)
879   (setq spam-split-last-successful-check nil)
880   (unless spam-split-disabled
881     (let ((spam-split-group-choice spam-split-group))
882       (dolist (check specific-checks)
883         (when (stringp check)
884           (setq spam-split-group-choice check)
885           (setq specific-checks (delq check specific-checks))))
886
887       (let ((spam-split-group spam-split-group-choice))
888         (save-excursion
889           (save-restriction
890             (dolist (check spam-list-of-statistical-checks)
891               (when (and (symbolp check) (symbol-value check))
892                 (widen)
893                 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
894                               (symbol-name check))
895                 (return)))
896             ;;   (progn (widen) (debug (buffer-string)))
897             (let ((list-of-checks spam-list-of-checks)
898                   decision)
899               (while (and list-of-checks (not decision))
900                 (let ((pair (pop list-of-checks)))
901                   (when (and (symbol-value (car pair))
902                              (or (null specific-checks)
903                                  (memq (car pair) specific-checks)))
904                     (gnus-message 5 "spam-split: calling the %s function"
905                                   (symbol-name (cdr pair)))
906                     (setq decision (funcall (cdr pair)))
907                     ;; if we got a decision at all, save the current check
908                     (when decision
909                       (setq spam-split-last-successful-check (car pair)))
910
911                     (when (eq decision 'spam)
912                       (if spam-split-symbolic-return
913                           (setq decision spam-split-group)
914                         (gnus-error
915                          5
916                          (format "spam-split got %s but %s is nil"
917                                  (symbol-name decision)
918                                  (symbol-name spam-split-symbolic-return))))))))
919               (if (eq decision t)
920                   (if spam-split-symbolic-return-positive 'ham nil)
921                 decision))))))))
922
923 (defun spam-find-spam ()
924   "This function will detect spam in the current newsgroup using spam-split."
925   (interactive)
926
927   (let* ((group gnus-newsgroup-name)
928          (autodetect (gnus-parameter-spam-autodetect group))
929          (methods (gnus-parameter-spam-autodetect-methods group))
930          (first-method (nth 0 methods))
931          (articles (if spam-autodetect-recheck-messages
932                        gnus-newsgroup-articles
933                      gnus-newsgroup-unseen))
934          (spam-cache-lookups (< 2 (length articles))))
935
936     (when (and autodetect
937                (not (equal first-method 'none)))
938     (mapcar
939      (lambda (article)
940        (let ((id (spam-fetch-field-message-id-fast article))
941              (subject (spam-fetch-field-subject-fast article))
942              (sender (spam-fetch-field-from-fast article)))
943          (unless (and spam-log-to-registry
944                       (spam-log-registered-p id 'incoming))
945            (let* ((spam-split-symbolic-return t)
946                   (spam-split-symbolic-return-positive t)
947                   (split-return
948                    (with-temp-buffer
949                      (gnus-request-article-this-buffer
950                       article
951                       group)
952                      (if (or (null first-method)
953                              (equal first-method 'default))
954                          (spam-split)
955                        (apply 'spam-split methods)))))
956              (if (equal split-return 'spam)
957                  (gnus-summary-mark-article article gnus-spam-mark))
958
959              (when (and split-return spam-log-to-registry)
960                (when (zerop (gnus-registry-group-count id))
961                  (gnus-registry-add-group
962                   id group subject sender))
963                
964                (spam-log-processing-to-registry
965                 id
966                 'incoming
967                 split-return
968                 spam-split-last-successful-check
969                 group))))))
970      articles))))
971
972 (defvar spam-registration-functions
973   ;; first the ham register, second the spam register function
974   ;; third the ham unregister, fourth the spam unregister function
975   '((spam-use-blacklist  nil
976                          spam-blacklist-register-routine
977                          nil
978                          spam-blacklist-unregister-routine)
979     (spam-use-whitelist  spam-whitelist-register-routine
980                          nil
981                          spam-whitelist-unregister-routine
982                          nil)
983     (spam-use-BBDB       spam-BBDB-register-routine
984                          nil
985                          spam-BBDB-unregister-routine
986                          nil)
987     (spam-use-ifile      spam-ifile-register-ham-routine
988                          spam-ifile-register-spam-routine
989                          spam-ifile-unregister-ham-routine
990                          spam-ifile-unregister-spam-routine)
991     (spam-use-spamoracle spam-spamoracle-learn-ham
992                          spam-spamoracle-learn-spam
993                          spam-spamoracle-unlearn-ham
994                          spam-spamoracle-unlearn-spam)
995     (spam-use-stat       spam-stat-register-ham-routine
996                          spam-stat-register-spam-routine
997                          spam-stat-unregister-ham-routine
998                          spam-stat-unregister-spam-routine)
999     ;; note that spam-use-gmane is not a legitimate check
1000     (spam-use-gmane      nil
1001                          spam-report-gmane-register-routine
1002                          ;; does Gmane support unregistration?
1003                          nil
1004                          nil)
1005     (spam-use-bogofilter spam-bogofilter-register-ham-routine
1006                          spam-bogofilter-register-spam-routine
1007                          spam-bogofilter-unregister-ham-routine
1008                          spam-bogofilter-unregister-spam-routine))
1009   "The spam-registration-functions list contains pairs
1010 associating a parameter variable with the ham and spam
1011 registration functions, and the ham and spam unregistration
1012 functions")
1013
1014 (defun spam-classification-valid-p (classification)
1015   (or  (eq classification 'spam)
1016        (eq classification 'ham)))
1017
1018 (defun spam-process-type-valid-p (process-type)
1019   (or  (eq process-type 'incoming)
1020        (eq process-type 'process)))
1021
1022 (defun spam-registration-check-valid-p (check)
1023   (assoc check spam-registration-functions))
1024
1025 (defun spam-unregistration-check-valid-p (check)
1026   (assoc check spam-registration-functions))
1027
1028 (defun spam-registration-function (classification check)
1029   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1030     (if (eq classification 'spam)
1031         (nth 1 flist)
1032       (nth 0 flist))))
1033
1034 (defun spam-unregistration-function (classification check)
1035   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1036     (if (eq classification 'spam)
1037         (nth 3 flist)
1038       (nth 2 flist))))
1039
1040 (defun spam-list-articles (articles classification)
1041   (let ((mark-check (if (eq classification 'spam)
1042                         'spam-group-spam-mark-p
1043                       'spam-group-ham-mark-p))
1044         list mark-cache-yes mark-cache-no)
1045     (dolist (article articles)
1046       (let ((mark (gnus-summary-article-mark article)))
1047         (unless (memq mark mark-cache-no)
1048           (if (memq mark mark-cache-yes)
1049               (push article list)
1050             ;; else, we have to actually check the mark
1051             (if (funcall mark-check
1052                          gnus-newsgroup-name
1053                          mark)
1054                 (progn
1055                   (push article list)
1056                   (push mark mark-cache-yes))
1057               (push mark mark-cache-no))))))
1058     list))
1059
1060 (defun spam-register-routine (classification
1061                               check
1062                               &optional unregister
1063                               specific-articles)
1064   (when (and (spam-classification-valid-p classification)
1065              (spam-registration-check-valid-p check))
1066     (let* ((register-function
1067             (spam-registration-function classification check))
1068            (unregister-function
1069             (spam-unregistration-function classification check))
1070            (run-function (if unregister
1071                              unregister-function
1072                            register-function))
1073            (log-function (if unregister
1074                              'spam-log-undo-registration
1075                            'spam-log-processing-to-registry))
1076            article articles)
1077
1078       (when run-function
1079         ;; make list of articles, using specific-articles if given
1080         (setq articles (or specific-articles
1081                            (spam-list-articles
1082                             gnus-newsgroup-articles
1083                             classification)))
1084         ;; process them
1085         (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1086                       (if unregister "Unregistering" "Registering")
1087                       (length articles)
1088                       (if specific-articles "specific" "")
1089                       (symbol-name classification)
1090                       (symbol-name check))
1091         (funcall run-function articles)
1092         ;; now log all the registrations (or undo them, depending on unregister)
1093         (dolist (article articles)
1094           (funcall log-function
1095                    (spam-fetch-field-message-id-fast article)
1096                    'process
1097                    classification
1098                    check
1099                    gnus-newsgroup-name))))))
1100
1101 ;;; log a ham- or spam-processor invocation to the registry
1102 (defun spam-log-processing-to-registry (id type classification check group)
1103   (when spam-log-to-registry
1104     (if (and (stringp id)
1105              (stringp group)
1106              (spam-process-type-valid-p type)
1107              (spam-classification-valid-p classification)
1108              (spam-registration-check-valid-p check))
1109         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1110               (cell (list classification check group)))
1111           (push cell cell-list)
1112           (gnus-registry-store-extra-entry
1113            id
1114            type
1115            cell-list))
1116
1117       (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1118                               "spam-log-processing-to-registry")))))
1119
1120 ;;; check if a ham- or spam-processor registration has been done
1121 (defun spam-log-registered-p (id type)
1122   (when spam-log-to-registry
1123     (if (and (stringp id)
1124              (spam-process-type-valid-p type))
1125         (cdr-safe (gnus-registry-fetch-extra id type))
1126       (progn
1127         (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1128                                 "spam-log-registered-p"))
1129         nil))))
1130
1131 ;;; check if a ham- or spam-processor registration needs to be undone
1132 (defun spam-log-unregistration-needed-p (id type classification check)
1133   (when spam-log-to-registry
1134     (if (and (stringp id)
1135              (spam-process-type-valid-p type)
1136              (spam-classification-valid-p classification)
1137              (spam-registration-check-valid-p check))
1138         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1139               found)
1140           (dolist (cell cell-list)
1141             (unless found
1142               (when (and (eq classification (nth 0 cell))
1143                          (eq check (nth 1 cell)))
1144                 (setq found t))))
1145           found)
1146       (progn
1147         (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1148                                 "spam-log-unregistration-needed-p"))
1149         nil))))
1150
1151
1152 ;;; undo a ham- or spam-processor registration (the group is not used)
1153 (defun spam-log-undo-registration (id type classification check &optional group)
1154   (when (and spam-log-to-registry
1155              (spam-log-unregistration-needed-p id type classification check))
1156     (if (and (stringp id)
1157              (spam-process-type-valid-p type)
1158              (spam-classification-valid-p classification)
1159              (spam-registration-check-valid-p check))
1160         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1161               new-cell-list found)
1162           (dolist (cell cell-list)
1163             (unless (and (eq classification (nth 0 cell))
1164                          (eq check (nth 1 cell)))
1165               (push cell new-cell-list)))
1166           (gnus-registry-store-extra-entry
1167            id
1168            type
1169            new-cell-list))
1170       (progn
1171         (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1172                                 "spam-log-undo-registration"))
1173         nil))))
1174
1175 ;;; set up IMAP widening if it's necessary
1176 (defun spam-setup-widening ()
1177   (dolist (check spam-list-of-statistical-checks)
1178     (when (symbol-value check)
1179       (setq nnimap-split-download-body-default t))))
1180
1181 \f
1182 ;;;; Regex body
1183
1184 (defun spam-check-regex-body ()
1185   (let ((spam-regex-headers-ham spam-regex-body-ham)
1186         (spam-regex-headers-spam spam-regex-body-spam))
1187     (spam-check-regex-headers t)))
1188
1189 \f
1190 ;;;; Regex headers
1191
1192 (defun spam-check-regex-headers (&optional body)
1193   (let ((type (if body "body" "header"))
1194         (spam-split-group (if spam-split-symbolic-return
1195                               'spam
1196                             spam-split-group))
1197         ret found)
1198     (dolist (h-regex spam-regex-headers-ham)
1199       (unless found
1200         (goto-char (point-min))
1201         (when (re-search-forward h-regex nil t)
1202           (message "Ham regex %s search positive." type)
1203           (setq found t))))
1204     (dolist (s-regex spam-regex-headers-spam)
1205       (unless found
1206         (goto-char (point-min))
1207         (when (re-search-forward s-regex nil t)
1208           (message "Spam regex %s search positive." type)
1209           (setq found t)
1210           (setq ret spam-split-group))))
1211     ret))
1212
1213 \f
1214 ;;;; Blackholes.
1215
1216 (defun spam-reverse-ip-string (ip)
1217   (when (stringp ip)
1218     (mapconcat 'identity
1219                (nreverse (split-string ip "\\."))
1220                ".")))
1221
1222 (defun spam-check-blackholes ()
1223   "Check the Received headers for blackholed relays."
1224   (let ((headers (nnmail-fetch-field "received"))
1225         (spam-split-group (if spam-split-symbolic-return
1226                               'spam
1227                             spam-split-group))
1228         ips matches)
1229     (when headers
1230       (with-temp-buffer
1231         (insert headers)
1232         (goto-char (point-min))
1233         (gnus-message 5 "Checking headers for relay addresses")
1234         (while (re-search-forward
1235                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1236           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1237           (push (spam-reverse-ip-string (match-string 1))
1238                 ips)))
1239       (dolist (server spam-blackhole-servers)
1240         (dolist (ip ips)
1241           (unless (and spam-blackhole-good-server-regex
1242                        ;; match the good-server-regex against the reversed (again) IP string
1243                        (string-match
1244                         spam-blackhole-good-server-regex
1245                         (spam-reverse-ip-string ip)))
1246             (unless matches
1247               (let ((query-string (concat ip "." server)))
1248                 (if spam-use-dig
1249                     (let ((query-result (query-dig query-string)))
1250                       (when query-result
1251                         (gnus-message 5 "(DIG): positive blackhole check '%s'"
1252                                       query-result)
1253                         (push (list ip server query-result)
1254                               matches)))
1255                   ;; else, if not using dig.el
1256                   (when (query-dns query-string)
1257                     (gnus-message 5 "positive blackhole check")
1258                     (push (list ip server (query-dns query-string 'TXT))
1259                           matches)))))))))
1260     (when matches
1261       spam-split-group)))
1262 \f
1263 ;;;; Hashcash.
1264
1265 (condition-case nil
1266     (progn
1267       (require 'hashcash)
1268
1269       (defun spam-check-hashcash ()
1270         "Check the headers for hashcash payments."
1271         (mail-check-payment)))   ;mail-check-payment returns a boolean
1272
1273   (file-error (progn
1274                 (defalias 'mail-check-payment 'ignore)
1275                 (defalias 'spam-check-hashcash 'ignore))))
1276 \f
1277 ;;;; BBDB
1278
1279 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1280 ;;; <sacha@giotto.sj.ru>
1281
1282 ;; all this is done inside a condition-case to trap errors
1283
1284 (condition-case nil
1285     (progn
1286       (require 'bbdb)
1287       (require 'bbdb-com)
1288
1289       ;; when the BBDB changes, we want to clear out our cache
1290       (defun spam-clear-cache-BBDB (&rest immaterial)
1291         (spam-clear-cache 'spam-use-BBDB))
1292
1293       (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1294
1295       (defun spam-enter-ham-BBDB (addresses &optional remove)
1296         "Enter an address into the BBDB; implies ham (non-spam) sender"
1297         (dolist (from addresses)
1298           (when (stringp from)
1299             (let* ((parsed-address (gnus-extract-address-components from))
1300                    (name (or (nth 0 parsed-address) "Ham Sender"))
1301                    (remove-function (if remove
1302                                         'bbdb-delete-record-internal
1303                                       'ignore))
1304                    (net-address (nth 1 parsed-address))
1305                    (record (and net-address
1306                                 (bbdb-search-simple nil net-address))))
1307               (when net-address
1308                 (gnus-message 5 "%s address %s %s BBDB"
1309                               (if remove "Deleting" "Adding")
1310                               from
1311                               (if remove "from" "to"))
1312                 (if record
1313                     (funcall remove-function record)
1314                   (bbdb-create-internal name nil net-address nil nil
1315                                         "ham sender added by spam.el")))))))
1316
1317       (defun spam-BBDB-register-routine (articles &optional unregister)
1318         (let (addresses)
1319           (dolist (article articles)
1320             (when (stringp (spam-fetch-field-from-fast article))
1321               (push (spam-fetch-field-from-fast article) addresses)))
1322           ;; now do the register/unregister action
1323           (spam-enter-ham-BBDB addresses unregister)))
1324
1325       (defun spam-BBDB-unregister-routine (articles)
1326         (spam-BBDB-register-routine articles t))
1327
1328       (defun spam-check-BBDB ()
1329         "Mail from people in the BBDB is classified as ham or non-spam"
1330         (let ((who (nnmail-fetch-field "from"))
1331               (spam-split-group (if spam-split-symbolic-return
1332                                     'spam
1333                                   spam-split-group))
1334               bbdb-cache)
1335           
1336           (when spam-cache-lookups
1337             (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1338             (unless bbdb-cache
1339               (setq bbdb-cache (bbdb-hashtable))
1340               (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1341
1342           (when who
1343             (setq who (nth 1 (gnus-extract-address-components who)))
1344             (if
1345                 (if spam-cache-lookups
1346                     (bbdb-gethash who bbdb-cache)
1347                   (bbdb-search-simple nil who))
1348                 t
1349               (if spam-use-BBDB-exclusive
1350                   spam-split-group
1351                 nil))))))
1352
1353   (file-error (progn
1354                 (defalias 'bbdb-search-simple 'ignore)
1355                 (defalias 'bbdb-hashtable 'ignore)
1356                 (defalias 'bbdb-gethash 'ignore)
1357                 (defalias 'spam-check-BBDB 'ignore)
1358                 (defalias 'spam-BBDB-register-routine 'ignore)
1359                 (defalias 'spam-enter-ham-BBDB 'ignore)
1360                 (defalias 'bbdb-create-internal 'ignore)
1361                 (defalias 'bbdb-delete-record-internal 'ignore)
1362                 (defalias 'bbdb-records 'ignore))))
1363
1364 \f
1365 ;;;; ifile
1366
1367 ;;; check the ifile backend; return nil if the mail was NOT classified
1368 ;;; as spam
1369
1370 (defun spam-get-ifile-database-parameter ()
1371   "Get the command-line parameter for ifile's database from
1372   spam-ifile-database-path."
1373   (if spam-ifile-database-path
1374       (format "--db-file=%s" spam-ifile-database-path)
1375     nil))
1376
1377 (defun spam-check-ifile ()
1378   "Check the ifile backend for the classification of this message."
1379   (let ((article-buffer-name (buffer-name))
1380         (spam-split-group (if spam-split-symbolic-return
1381                               'spam
1382                             spam-split-group))
1383         category return)
1384     (with-temp-buffer
1385       (let ((temp-buffer-name (buffer-name))
1386             (db-param (spam-get-ifile-database-parameter)))
1387         (save-excursion
1388           (set-buffer article-buffer-name)
1389           (apply 'call-process-region
1390                  (point-min) (point-max) spam-ifile-path
1391                  nil temp-buffer-name nil "-c"
1392                  (if db-param `(,db-param "-q") `("-q"))))
1393         ;; check the return now (we're back in the temp buffer)
1394         (goto-char (point-min))
1395         (if (not (eobp))
1396             (setq category (buffer-substring (point) (point-at-eol))))
1397         (when (not (zerop (length category))) ; we need a category here
1398           (if spam-ifile-all-categories
1399               (setq return category)
1400             ;; else, if spam-ifile-all-categories is not set...
1401             (when (string-equal spam-ifile-spam-category category)
1402               (setq return spam-split-group)))))) ; note return is nil otherwise
1403     return))
1404
1405 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1406   "Register an article, given as a string, with a category.
1407 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1408   (let ((category (or category gnus-newsgroup-name))
1409         (add-or-delete-option (if unregister "-d" "-i"))
1410         (db (spam-get-ifile-database-parameter))
1411         parameters)
1412     (with-temp-buffer
1413       (dolist (article articles)
1414         (let ((article-string (spam-get-article-as-string article)))
1415           (when (stringp article-string)
1416             (insert article-string))))
1417       (apply 'call-process-region
1418              (point-min) (point-max) spam-ifile-path
1419              nil nil nil
1420              add-or-delete-option category
1421              (if db `(,db "-h") `("-h"))))))
1422
1423 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1424   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1425
1426 (defun spam-ifile-unregister-spam-routine (articles)
1427   (spam-ifile-register-spam-routine articles t))
1428
1429 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1430   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1431
1432 (defun spam-ifile-unregister-ham-routine (articles)
1433   (spam-ifile-register-ham-routine articles t))
1434
1435 \f
1436 ;;;; spam-stat
1437
1438 (condition-case nil
1439     (progn
1440       (let ((spam-stat-install-hooks nil))
1441         (require 'spam-stat))
1442
1443       (defun spam-check-stat ()
1444         "Check the spam-stat backend for the classification of this message"
1445         (let ((spam-split-group (if spam-split-symbolic-return
1446                                     'spam
1447                                   spam-split-group))
1448               (spam-stat-split-fancy-spam-group spam-split-group) ; override
1449               (spam-stat-buffer (buffer-name)) ; stat the current buffer
1450               category return)
1451           (spam-stat-split-fancy)))
1452
1453       (defun spam-stat-register-spam-routine (articles &optional unregister)
1454         (dolist (article articles)
1455           (let ((article-string (spam-get-article-as-string article)))
1456             (with-temp-buffer
1457               (insert article-string)
1458               (if unregister
1459                   (spam-stat-buffer-change-to-non-spam)
1460               (spam-stat-buffer-is-spam))))))
1461
1462       (defun spam-stat-unregister-spam-routine (articles)
1463         (spam-stat-register-spam-routine articles t))
1464
1465       (defun spam-stat-register-ham-routine (articles &optional unregister)
1466         (dolist (article articles)
1467           (let ((article-string (spam-get-article-as-string article)))
1468             (with-temp-buffer
1469               (insert article-string)
1470               (if unregister
1471                   (spam-stat-buffer-change-to-spam)
1472               (spam-stat-buffer-is-non-spam))))))
1473
1474       (defun spam-stat-unregister-ham-routine (articles)
1475         (spam-stat-register-ham-routine articles t))
1476
1477       (defun spam-maybe-spam-stat-load ()
1478         (when spam-use-stat (spam-stat-load)))
1479
1480       (defun spam-maybe-spam-stat-save ()
1481         (when spam-use-stat (spam-stat-save))))
1482
1483   (file-error (progn
1484                 (defalias 'spam-stat-load 'ignore)
1485                 (defalias 'spam-stat-save 'ignore)
1486                 (defalias 'spam-maybe-spam-stat-load 'ignore)
1487                 (defalias 'spam-maybe-spam-stat-save 'ignore)
1488                 (defalias 'spam-stat-register-ham-routine 'ignore)
1489                 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1490                 (defalias 'spam-stat-register-spam-routine 'ignore)
1491                 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1492                 (defalias 'spam-stat-buffer-is-spam 'ignore)
1493                 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1494                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1495                 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1496                 (defalias 'spam-stat-split-fancy 'ignore)
1497                 (defalias 'spam-check-stat 'ignore))))
1498
1499 \f
1500
1501 ;;;; Blacklists and whitelists.
1502
1503 (defvar spam-whitelist-cache nil)
1504 (defvar spam-blacklist-cache nil)
1505
1506 (defun spam-kill-whole-line ()
1507   (beginning-of-line)
1508   (let ((kill-whole-line t))
1509     (kill-line)))
1510
1511 ;;; address can be a list, too
1512 (defun spam-enter-whitelist (address &optional remove)
1513   "Enter ADDRESS (list or single) into the whitelist.
1514 With a non-nil REMOVE, remove them."
1515   (interactive "sAddress: ")
1516   (spam-enter-list address spam-whitelist remove)
1517   (setq spam-whitelist-cache nil))
1518
1519 ;;; address can be a list, too
1520 (defun spam-enter-blacklist (address &optional remove)
1521   "Enter ADDRESS (list or single) into the blacklist.
1522 With a non-nil REMOVE, remove them."
1523   (interactive "sAddress: ")
1524   (spam-enter-list address spam-blacklist remove)
1525   (setq spam-blacklist-cache nil))
1526
1527 (defun spam-enter-list (addresses file &optional remove)
1528   "Enter ADDRESSES into the given FILE.
1529 Either the whitelist or the blacklist files can be used.  With
1530 REMOVE not nil, remove the ADDRESSES."
1531   (if (stringp addresses)
1532       (spam-enter-list (list addresses) file remove)
1533     ;; else, we have a list of addresses here
1534     (unless (file-exists-p (file-name-directory file))
1535       (make-directory (file-name-directory file) t))
1536     (save-excursion
1537       (set-buffer
1538        (find-file-noselect file))
1539       (dolist (a addresses)
1540         (when (stringp a)
1541           (goto-char (point-min))
1542           (if (re-search-forward (regexp-quote a) nil t)
1543               ;; found the address
1544               (when remove
1545                 (spam-kill-whole-line))
1546             ;; else, the address was not found
1547             (unless remove
1548               (goto-char (point-max))
1549               (unless (bobp)
1550                 (insert "\n"))
1551               (insert a "\n")))))
1552       (save-buffer))))
1553
1554 ;;; returns t if the sender is in the whitelist, nil or
1555 ;;; spam-split-group otherwise
1556 (defun spam-check-whitelist ()
1557   ;; FIXME!  Should it detect when file timestamps change?
1558   (let ((spam-split-group (if spam-split-symbolic-return
1559                               'spam
1560                             spam-split-group)))
1561     (unless spam-whitelist-cache
1562       (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1563     (if (spam-from-listed-p spam-whitelist-cache)
1564         t
1565       (if spam-use-whitelist-exclusive
1566           spam-split-group
1567         nil))))
1568
1569 (defun spam-check-blacklist ()
1570   ;; FIXME!  Should it detect when file timestamps change?
1571   (let ((spam-split-group (if spam-split-symbolic-return
1572                               'spam
1573                             spam-split-group)))
1574     (unless spam-blacklist-cache
1575       (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1576     (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1577
1578 (defun spam-parse-list (file)
1579   (when (file-readable-p file)
1580     (let (contents address)
1581       (with-temp-buffer
1582         (insert-file-contents file)
1583         (while (not (eobp))
1584           (setq address (buffer-substring (point) (point-at-eol)))
1585           (forward-line 1)
1586           ;; insert the e-mail address if detected, otherwise the raw data
1587           (unless (zerop (length address))
1588             (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1589               (push (or pure-address address) contents)))))
1590       (nreverse contents))))
1591
1592 (defun spam-from-listed-p (cache)
1593   (let ((from (nnmail-fetch-field "from"))
1594         found)
1595     (while cache
1596       (let ((address (pop cache)))
1597         (unless (zerop (length address)) ; 0 for a nil address too
1598           (setq address (regexp-quote address))
1599           ;; fix regexp-quote's treatment of user-intended regexes
1600           (while (string-match "\\\\\\*" address)
1601             (setq address (replace-match ".*" t t address))))
1602         (when (and address (string-match address from))
1603           (setq found t
1604                 cache nil))))
1605     found))
1606
1607 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1608   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1609         (declassification (if blacklist 'ham 'spam))
1610         (enter-function
1611          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1612         (remove-function
1613          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1614         from addresses unregister-list)
1615     (dolist (article articles)
1616       (let ((from (spam-fetch-field-from-fast article))
1617             (id (spam-fetch-field-message-id-fast article))
1618             sender-ignored)
1619         (when (stringp from)
1620           (dolist (ignore-regex spam-blacklist-ignored-regexes)
1621             (when (and (not sender-ignored)
1622                        (stringp ignore-regex)
1623                        (string-match ignore-regex from))
1624               (setq sender-ignored t)))
1625           ;; remember the messages we need to unregister, unless remove is set
1626           (when (and
1627                  (null unregister)
1628                  (spam-log-unregistration-needed-p
1629                   id 'process declassification de-symbol))
1630             (push from unregister-list))
1631           (unless sender-ignored
1632             (push from addresses)))))
1633
1634     (if unregister
1635         (funcall enter-function addresses t) ; unregister all these addresses
1636       ;; else, register normally and unregister what we need to
1637       (funcall remove-function unregister-list t)
1638       (dolist (article unregister-list)
1639         (spam-log-undo-registration
1640          (spam-fetch-field-message-id-fast article)
1641          'process
1642          declassification
1643          de-symbol))
1644       (funcall enter-function addresses nil))))
1645
1646 (defun spam-blacklist-unregister-routine (articles)
1647   (spam-blacklist-register-routine articles t))
1648
1649 (defun spam-blacklist-register-routine (articles &optional unregister)
1650   (spam-filelist-register-routine articles t unregister))
1651
1652 (defun spam-whitelist-unregister-routine (articles)
1653   (spam-whitelist-register-routine articles t))
1654
1655 (defun spam-whitelist-register-routine (articles &optional unregister)
1656   (spam-filelist-register-routine articles nil unregister))
1657
1658 \f
1659 ;;;; Spam-report glue
1660 (defun spam-report-gmane-register-routine (articles)
1661   (when articles
1662     (apply 'spam-report-gmane articles)))
1663
1664 \f
1665 ;;;; Bogofilter
1666 (defun spam-check-bogofilter-headers (&optional score)
1667   (let ((header (nnmail-fetch-field spam-bogofilter-header))
1668         (spam-split-group (if spam-split-symbolic-return
1669                               'spam
1670                             spam-split-group)))
1671     (when header                        ; return nil when no header
1672       (if score                         ; scoring mode
1673           (if (string-match "spamicity=\\([0-9.]+\\)" header)
1674               (match-string 1 header)
1675             "0")
1676         ;; spam detection mode
1677         (when (string-match spam-bogofilter-bogosity-positive-spam-header
1678                             header)
1679           spam-split-group)))))
1680
1681 ;; return something sensible if the score can't be determined
1682 (defun spam-bogofilter-score ()
1683   "Get the Bogofilter spamicity score"
1684   (interactive)
1685   (save-window-excursion
1686     (gnus-summary-show-article t)
1687     (set-buffer gnus-article-buffer)
1688     (let ((score (or (spam-check-bogofilter-headers t)
1689                      (spam-check-bogofilter t))))
1690       (message "Spamicity score %s" score)
1691       (or score "0"))
1692     (gnus-summary-show-article)))
1693
1694 (defun spam-check-bogofilter (&optional score)
1695   "Check the Bogofilter backend for the classification of this message"
1696   (let ((article-buffer-name (buffer-name))
1697         (db spam-bogofilter-database-directory)
1698         return)
1699     (with-temp-buffer
1700       (let ((temp-buffer-name (buffer-name)))
1701         (save-excursion
1702           (set-buffer article-buffer-name)
1703           (apply 'call-process-region
1704                  (point-min) (point-max)
1705                  spam-bogofilter-path
1706                  nil temp-buffer-name nil
1707                  (if db `("-d" ,db "-v") `("-v"))))
1708         (setq return (spam-check-bogofilter-headers score))))
1709     return))
1710
1711 (defun spam-bogofilter-register-with-bogofilter (articles
1712                                                  spam
1713                                                  &optional unregister)
1714   "Register an article, given as a string, as spam or non-spam."
1715   (dolist (article articles)
1716     (let ((article-string (spam-get-article-as-string article))
1717           (db spam-bogofilter-database-directory)
1718           (switch (if unregister
1719                       (if spam
1720                           spam-bogofilter-spam-strong-switch
1721                         spam-bogofilter-ham-strong-switch)
1722                     (if spam
1723                         spam-bogofilter-spam-switch
1724                       spam-bogofilter-ham-switch))))
1725       (when (stringp article-string)
1726         (with-temp-buffer
1727           (insert article-string)
1728
1729           (apply 'call-process-region
1730                  (point-min) (point-max)
1731                  spam-bogofilter-path
1732                  nil nil nil switch
1733                  (if db `("-d" ,db "-v") `("-v"))))))))
1734
1735 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1736   (spam-bogofilter-register-with-bogofilter articles t unregister))
1737
1738 (defun spam-bogofilter-unregister-spam-routine (articles)
1739   (spam-bogofilter-register-spam-routine articles t))
1740
1741 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1742   (spam-bogofilter-register-with-bogofilter articles nil unregister))
1743
1744 (defun spam-bogofilter-unregister-ham-routine (articles)
1745   (spam-bogofilter-register-ham-routine articles t))
1746
1747
1748 \f
1749 ;;;; spamoracle
1750 (defun spam-check-spamoracle ()
1751   "Run spamoracle on an article to determine whether it's spam."
1752   (let ((article-buffer-name (buffer-name))
1753         (spam-split-group (if spam-split-symbolic-return
1754                               'spam
1755                             spam-split-group)))
1756     (with-temp-buffer
1757       (let ((temp-buffer-name (buffer-name)))
1758         (save-excursion
1759           (set-buffer article-buffer-name)
1760           (let ((status
1761                  (apply 'call-process-region
1762                         (point-min) (point-max)
1763                         spam-spamoracle-binary
1764                         nil temp-buffer-name nil
1765                         (if spam-spamoracle-database
1766                             `("-f" ,spam-spamoracle-database "mark")
1767                           '("mark")))))
1768             (if (eq 0 status)
1769                 (progn
1770                   (set-buffer temp-buffer-name)
1771                   (goto-char (point-min))
1772                   (when (re-search-forward "^X-Spam: yes;" nil t)
1773                     spam-split-group))
1774               (error "Error running spamoracle" status))))))))
1775
1776 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1777   "Run spamoracle in training mode."
1778   (with-temp-buffer
1779     (let ((temp-buffer-name (buffer-name)))
1780       (save-excursion
1781         (goto-char (point-min))
1782         (dolist (article articles)
1783           (insert (spam-get-article-as-string article)))
1784         (let* ((arg (if (spam-xor unregister article-is-spam-p)
1785                         "-spam"
1786                       "-good"))
1787                (status
1788                 (apply 'call-process-region
1789                        (point-min) (point-max)
1790                        spam-spamoracle-binary
1791                        nil temp-buffer-name nil
1792                        (if spam-spamoracle-database
1793                            `("-f" ,spam-spamoracle-database
1794                              "add" ,arg)
1795                          `("add" ,arg)))))
1796           (when (not (eq 0 status))
1797             (error "Error running spamoracle" status)))))))
1798
1799 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1800   (spam-spamoracle-learn articles nil unregister))
1801
1802 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1803   (spam-spamoracle-learn-ham articles t))
1804
1805 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1806   (spam-spamoracle-learn articles t unregister))
1807
1808 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1809   (spam-spamoracle-learn-spam articles t))
1810
1811 \f
1812 ;;;; Hooks
1813
1814 ;;;###autoload
1815 (defun spam-initialize ()
1816   "Install the spam.el hooks and do other initialization"
1817   (interactive)
1818   (setq spam-install-hooks t)
1819   ;; TODO: How do we redo this every time spam-face is customized?
1820   (push '((eq mark gnus-spam-mark) . spam-face)
1821         gnus-summary-highlight)
1822   ;; Add hooks for loading and saving the spam stats
1823   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1824   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1825   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1826   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1827   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1828   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1829   (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1830
1831 (defun spam-unload-hook ()
1832   "Uninstall the spam.el hooks"
1833   (interactive)
1834   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1835   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1836   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1837   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1838   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1839   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1840   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1841
1842 (when spam-install-hooks
1843   (spam-initialize))
1844
1845 (provide 'spam)
1846
1847 ;;; spam.el ends here.