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