(spam-split): do not require spam-use-CHECK to be
[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 (defun spam-report-articles-gmane (n)
574   "Report the current message as spam.
575 Respects the process/prefix convention."
576   (interactive "P")
577   (dolist (article (gnus-summary-work-articles n))
578     (gnus-summary-remove-process-mark article)
579     (spam-report-gmane article)))
580
581 ;;; Summary entry and exit processing.
582
583 (defun spam-summary-prepare ()
584   (setq spam-old-ham-articles
585         (spam-list-articles gnus-newsgroup-articles 'ham))
586   (setq spam-old-spam-articles
587         (spam-list-articles gnus-newsgroup-articles 'spam))
588   (spam-mark-junk-as-spam-routine))
589
590 ;; The spam processors are invoked for any group, spam or ham or neither
591 (defun spam-summary-prepare-exit ()
592   (unless gnus-group-is-exiting-without-update-p
593     (gnus-message 6 "Exiting summary buffer and applying spam rules")
594
595     ;; first of all, unregister any articles that are no longer ham or spam
596     ;; we have to iterate over the processors, or else we'll be too slow
597     (dolist (classification '(spam ham))
598       (let* ((old-articles (if (eq classification 'spam)
599                                spam-old-spam-articles
600                              spam-old-ham-articles))
601              (new-articles (spam-list-articles
602                             gnus-newsgroup-articles
603                             classification))
604              (changed-articles (gnus-set-difference old-articles new-articles)))
605         ;; now that we have the changed articles, we go through the processors
606         (dolist (processor-param spam-list-of-processors)
607           (let ((processor (nth 0 processor-param))
608                 (processor-classification (nth 1 processor-param))
609                 (check (nth 2 processor-param))
610                 unregister-list)
611             (dolist (article changed-articles)
612               (let ((id (spam-fetch-field-message-id-fast article)))
613                 (when (spam-log-unregistration-needed-p
614                        id 'process classification check)
615                   (push article unregister-list))))
616             ;; call spam-register-routine with specific articles to unregister,
617             ;; when there are articles to unregister and the check is enabled
618             (when (and unregister-list (symbol-value check))
619               (spam-register-routine classification check t unregister-list))))))
620
621     ;; find all the spam processors applicable to this group
622     (dolist (processor-param spam-list-of-processors)
623       (let ((processor (nth 0 processor-param))
624             (classification (nth 1 processor-param))
625             (check (nth 2 processor-param)))
626         (when (and (eq 'spam classification)
627                    (spam-group-processor-p gnus-newsgroup-name processor))
628           (spam-register-routine classification check))))
629
630     (if spam-move-spam-nonspam-groups-only
631         (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
632           (spam-mark-spam-as-expired-and-move-routine
633            (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
634       (gnus-message 5 "Marking spam as expired and moving it to %s"
635                     gnus-newsgroup-name)
636       (spam-mark-spam-as-expired-and-move-routine
637        (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
638
639     ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
640     ;; expire spam, in case the above did not expire them
641     (gnus-message 5 "Marking spam as expired without moving it")
642     (spam-mark-spam-as-expired-and-move-routine nil)
643
644     (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
645               (and (spam-group-spam-contents-p gnus-newsgroup-name)
646                    spam-process-ham-in-spam-groups)
647               spam-process-ham-in-nonham-groups)
648       ;; find all the ham processors applicable to this group
649       (dolist (processor-param spam-list-of-processors)
650         (let ((processor (nth 0 processor-param))
651               (classification (nth 1 processor-param))
652               (check (nth 2 processor-param)))
653           (when (and (eq 'ham classification)
654                      (spam-group-processor-p gnus-newsgroup-name processor))
655             (spam-register-routine classification check)))))
656
657     (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
658       (gnus-message 5 "Copying ham")
659       (spam-ham-copy-routine
660        (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
661
662     ;; now move all ham articles out of spam groups
663     (when (spam-group-spam-contents-p gnus-newsgroup-name)
664       (gnus-message 5 "Moving ham messages from spam group")
665       (spam-ham-move-routine
666        (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
667
668   (setq spam-old-ham-articles nil)
669   (setq spam-old-spam-articles nil))
670
671 (defun spam-mark-junk-as-spam-routine ()
672   ;; check the global list of group names spam-junk-mailgroups and the
673   ;; group parameters
674   (when (spam-group-spam-contents-p gnus-newsgroup-name)
675     (gnus-message 5 "Marking %s articles as spam"
676                   (if spam-mark-only-unseen-as-spam
677                       "unseen"
678                     "unread"))
679     (let ((articles (if spam-mark-only-unseen-as-spam
680                         gnus-newsgroup-unseen
681                       gnus-newsgroup-unreads)))
682       (dolist (article articles)
683         (gnus-summary-mark-article article gnus-spam-mark)))))
684
685 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
686   (if (and (car-safe groups) (listp (car-safe groups)))
687       (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
688     (gnus-summary-kill-process-mark)
689     (let ((articles gnus-newsgroup-articles)
690           (backend-supports-deletions
691            (gnus-check-backend-function
692             'request-move-article gnus-newsgroup-name))
693           article tomove deletep)
694       (dolist (article articles)
695         (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
696           (gnus-summary-mark-article article gnus-expirable-mark)
697           (push article tomove)))
698
699       ;; now do the actual copies
700       (dolist (group groups)
701         (when (and tomove
702                    (stringp group))
703           (dolist (article tomove)
704             (gnus-summary-set-process-mark article))
705           (when tomove
706             (if (or (not backend-supports-deletions)
707                     (> (length groups) 1))
708                 (progn
709                   (gnus-summary-copy-article nil group)
710                   (setq deletep t))
711               (gnus-summary-move-article nil group)))))
712
713       ;; now delete the articles, if there was a copy done, and the
714       ;; backend allows it
715       (when (and deletep backend-supports-deletions)
716         (dolist (article tomove)
717           (gnus-summary-set-process-mark article))
718         (when tomove
719           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
720             (gnus-summary-delete-article nil))))
721
722       (gnus-summary-yank-process-mark))))
723
724 (defun spam-ham-copy-or-move-routine (copy groups)
725   (gnus-summary-kill-process-mark)
726   (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
727         (backend-supports-deletions
728          (gnus-check-backend-function
729           'request-move-article gnus-newsgroup-name))
730         (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
731         article mark todo deletep respool)
732
733     (when (member 'respool groups)
734       (setq respool t)                  ; boolean for later
735       (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
736
737     ;; now do the actual move
738     (dolist (group groups)
739       (when (and todo (stringp group))
740         (dolist (article todo)
741           (when spam-mark-ham-unread-before-move-from-spam-group
742             (gnus-summary-mark-article article gnus-unread-mark))
743           (gnus-summary-set-process-mark article))
744
745         (if respool                        ; respooling is with a "fake" group
746             (let ((spam-split-disabled
747                    (or spam-split-disabled
748                        spam-disable-spam-split-during-ham-respool)))
749               (gnus-summary-respool-article nil respool-method))
750           (if (or (not backend-supports-deletions) ; else, we are not respooling
751                   (> (length groups) 1))
752               (progn                ; if copying, copy and set deletep
753                 (gnus-summary-copy-article nil group)
754                 (setq deletep t))
755             (gnus-summary-move-article nil group))))) ; else move articles
756
757     ;; now delete the articles, unless a) copy is t, and there was a copy done
758     ;;                                 b) a move was done to a single group
759     ;;                                 c) backend-supports-deletions is nil
760     (unless copy
761       (when (and deletep backend-supports-deletions)
762         (dolist (article todo)
763           (gnus-summary-set-process-mark article))
764         (when todo
765           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
766             (gnus-summary-delete-article nil))))))
767
768   (gnus-summary-yank-process-mark))
769
770 (defun spam-ham-copy-routine (&rest groups)
771   (if (and (car-safe groups) (listp (car-safe groups)))
772       (apply 'spam-ham-copy-routine (car groups))
773     (spam-ham-copy-or-move-routine t groups)))
774
775 (defun spam-ham-move-routine (&rest groups)
776   (if (and (car-safe groups) (listp (car-safe groups)))
777       (apply 'spam-ham-move-routine (car groups))
778     (spam-ham-copy-or-move-routine nil groups)))
779
780 (defun spam-get-article-as-string (article)
781   (let ((article-buffer (spam-get-article-as-buffer article))
782         article-string)
783     (when article-buffer
784       (save-window-excursion
785         (set-buffer article-buffer)
786         (setq article-string (buffer-string))))
787     article-string))
788
789 (defun spam-get-article-as-buffer (article)
790   (let ((article-buffer))
791     (when (numberp article)
792       (save-window-excursion
793         (gnus-summary-goto-subject article)
794         (gnus-summary-show-article t)
795         (setq article-buffer (get-buffer gnus-article-buffer))))
796     article-buffer))
797
798 ;; disabled for now
799 ;; (defun spam-get-article-as-filename (article)
800 ;;   (let ((article-filename))
801 ;;     (when (numberp article)
802 ;;       (nnml-possibly-change-directory
803 ;;        (gnus-group-real-name gnus-newsgroup-name))
804 ;;       (setq article-filename (expand-file-name
805 ;;                              (int-to-string article) nnml-current-directory)))
806 ;;     (if (file-exists-p article-filename)
807 ;;      article-filename
808 ;;       nil)))
809
810 (defun spam-fetch-field-from-fast (article)
811   "Fetch the `from' field quickly, using the internal gnus-data-list function"
812   (if (and (numberp article)
813            (assoc article (gnus-data-list nil)))
814       (mail-header-from
815        (gnus-data-header (assoc article (gnus-data-list nil))))
816     nil))
817
818 (defun spam-fetch-field-subject-fast (article)
819   "Fetch the `subject' field quickly, using the internal
820   gnus-data-list function"
821   (if (and (numberp article)
822            (assoc article (gnus-data-list nil)))
823       (mail-header-subject
824        (gnus-data-header (assoc article (gnus-data-list nil))))
825     nil))
826
827 (defun spam-fetch-field-message-id-fast (article)
828   "Fetch the `Message-ID' field quickly, using the internal
829   gnus-data-list function"
830   (if (and (numberp article)
831            (assoc article (gnus-data-list nil)))
832       (mail-header-message-id
833        (gnus-data-header (assoc article (gnus-data-list nil))))
834     nil))
835
836 \f
837 ;;;; Spam determination.
838
839 (defvar spam-list-of-checks
840   '((spam-use-blacklist          . spam-check-blacklist)
841     (spam-use-regex-headers      . spam-check-regex-headers)
842     (spam-use-regex-body         . spam-check-regex-body)
843     (spam-use-whitelist          . spam-check-whitelist)
844     (spam-use-BBDB               . spam-check-BBDB)
845     (spam-use-ifile              . spam-check-ifile)
846     (spam-use-spamoracle         . spam-check-spamoracle)
847     (spam-use-stat               . spam-check-stat)
848     (spam-use-blackholes         . spam-check-blackholes)
849     (spam-use-hashcash           . spam-check-hashcash)
850     (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
851     (spam-use-bogofilter         . spam-check-bogofilter))
852   "The spam-list-of-checks list contains pairs associating a
853 parameter variable with a spam checking function.  If the
854 parameter variable is true, then the checking function is called,
855 and its value decides what happens.  Each individual check may
856 return nil, t, or a mailgroup name.  The value nil means that the
857 check does not yield a decision, and so, that further checks are
858 needed.  The value t means that the message is definitely not
859 spam, and that further spam checks should be inhibited.
860 Otherwise, a mailgroup name or the symbol 'spam (depending on
861 spam-split-symbolic-return) is returned where the mail should go,
862 and further checks are also inhibited.  The usual mailgroup name
863 is the value of `spam-split-group', meaning that the message is
864 definitely a spam.")
865
866 (defvar spam-list-of-statistical-checks
867   '(spam-use-ifile
868     spam-use-regex-body
869     spam-use-stat
870     spam-use-bogofilter
871     spam-use-spamoracle)
872   "The spam-list-of-statistical-checks list contains all the mail
873 splitters that need to have the full message body available.")
874
875 ;;;TODO: modify to invoke self with each check if invoked without specifics
876 (defun spam-split (&rest specific-checks)
877   "Split this message into the `spam' group if it is spam.
878 This function can be used as an entry in the variable `nnmail-split-fancy',
879 for example like this: (: spam-split).  It can take checks as
880 parameters.  A string as a parameter will set the
881 spam-split-group to that string.
882
883 See the Info node `(gnus)Fancy Mail Splitting' for more details."
884   (interactive)
885   (setq spam-split-last-successful-check nil)
886   (unless spam-split-disabled
887     (let ((spam-split-group-choice spam-split-group))
888       (dolist (check specific-checks)
889         (when (stringp check)
890           (setq spam-split-group-choice check)
891           (setq specific-checks (delq check specific-checks))))
892
893       (let ((spam-split-group spam-split-group-choice))
894         (save-excursion
895           (save-restriction
896             (dolist (check spam-list-of-statistical-checks)
897               (when (and (symbolp check) (symbol-value check))
898                 (widen)
899                 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
900                               (symbol-name check))
901                 (return)))
902             ;;   (progn (widen) (debug (buffer-string)))
903             (let ((list-of-checks spam-list-of-checks)
904                   decision)
905               (while (and list-of-checks (not decision))
906                 (let ((pair (pop list-of-checks)))
907                   (when (or
908                          ;; either, given specific checks, this is one of them
909                          (and specific-checks (memq (car pair) specific-checks))
910                          ;; or, given no specific checks, spam-use-CHECK is set
911                          (and (null specific-checks) (symbol-value (car pair))))
912                     (gnus-message 5 "spam-split: calling the %s function"
913                                   (symbol-name (cdr pair)))
914                     (setq decision (funcall (cdr pair)))
915                     ;; if we got a decision at all, save the current check
916                     (when decision
917                       (setq spam-split-last-successful-check (car pair)))
918
919                     (when (eq decision 'spam)
920                       (unless spam-split-symbolic-return
921                         (gnus-error
922                          5
923                          (format "spam-split got %s but %s is nil"
924                                  (symbol-name decision)
925                                  (symbol-name spam-split-symbolic-return))))))))
926               (if (eq decision t)
927                   (if spam-split-symbolic-return-positive 'ham nil)
928                 decision))))))))
929
930 (defun spam-find-spam ()
931   "This function will detect spam in the current newsgroup using spam-split."
932   (interactive)
933
934   (let* ((group gnus-newsgroup-name)
935          (autodetect (gnus-parameter-spam-autodetect group))
936          (methods (gnus-parameter-spam-autodetect-methods group))
937          (first-method (nth 0 methods))
938          (articles (if spam-autodetect-recheck-messages
939                        gnus-newsgroup-articles
940                      gnus-newsgroup-unseen))
941          (spam-cache-lookups (< 2 (length articles))))
942
943     (when (and autodetect
944                (not (equal first-method 'none)))
945     (mapcar
946      (lambda (article)
947        (let ((id (spam-fetch-field-message-id-fast article))
948              (subject (spam-fetch-field-subject-fast article))
949              (sender (spam-fetch-field-from-fast article))
950              registry-lookup)
951          
952          (unless id
953            (gnus-error 5 "Article %d has no message ID!" article))
954          
955          (when (and id spam-log-to-registry)
956            (setq registry-lookup (spam-log-registration-type id 'incoming))
957            (when registry-lookup
958              (gnus-message 
959               9 
960               "spam-find-spam: message %s was already registered incoming"
961               id)))
962
963          (let* ((spam-split-symbolic-return t)
964                 (spam-split-symbolic-return-positive t)
965                 (split-return 
966                  (or registry-lookup
967                      (with-temp-buffer
968                        (gnus-request-article-this-buffer
969                         article
970                         group)
971                        (if (or (null first-method)
972                                (equal first-method 'default))
973                            (spam-split)
974                          (apply 'spam-split methods))))))
975            (if (equal split-return 'spam)
976                (gnus-summary-mark-article article gnus-spam-mark))
977            
978            (when (and id split-return spam-log-to-registry)
979              (when (zerop (gnus-registry-group-count id))
980                (gnus-registry-add-group
981                 id group subject sender))
982                
983              (unless registry-lookup
984                (spam-log-processing-to-registry
985                 id
986                 'incoming
987                 split-return
988                 spam-split-last-successful-check
989                 group))))))
990     articles))))
991
992 (defvar spam-registration-functions
993   ;; first the ham register, second the spam register function
994   ;; third the ham unregister, fourth the spam unregister function
995   '((spam-use-blacklist  nil
996                          spam-blacklist-register-routine
997                          nil
998                          spam-blacklist-unregister-routine)
999     (spam-use-whitelist  spam-whitelist-register-routine
1000                          nil
1001                          spam-whitelist-unregister-routine
1002                          nil)
1003     (spam-use-BBDB       spam-BBDB-register-routine
1004                          nil
1005                          spam-BBDB-unregister-routine
1006                          nil)
1007     (spam-use-ifile      spam-ifile-register-ham-routine
1008                          spam-ifile-register-spam-routine
1009                          spam-ifile-unregister-ham-routine
1010                          spam-ifile-unregister-spam-routine)
1011     (spam-use-spamoracle spam-spamoracle-learn-ham
1012                          spam-spamoracle-learn-spam
1013                          spam-spamoracle-unlearn-ham
1014                          spam-spamoracle-unlearn-spam)
1015     (spam-use-stat       spam-stat-register-ham-routine
1016                          spam-stat-register-spam-routine
1017                          spam-stat-unregister-ham-routine
1018                          spam-stat-unregister-spam-routine)
1019     ;; note that spam-use-gmane is not a legitimate check
1020     (spam-use-gmane      nil
1021                          spam-report-gmane-register-routine
1022                          ;; does Gmane support unregistration?
1023                          nil
1024                          nil)
1025     (spam-use-bogofilter spam-bogofilter-register-ham-routine
1026                          spam-bogofilter-register-spam-routine
1027                          spam-bogofilter-unregister-ham-routine
1028                          spam-bogofilter-unregister-spam-routine))
1029   "The spam-registration-functions list contains pairs
1030 associating a parameter variable with the ham and spam
1031 registration functions, and the ham and spam unregistration
1032 functions")
1033
1034 (defun spam-classification-valid-p (classification)
1035   (or  (eq classification 'spam)
1036        (eq classification 'ham)))
1037
1038 (defun spam-process-type-valid-p (process-type)
1039   (or  (eq process-type 'incoming)
1040        (eq process-type 'process)))
1041
1042 (defun spam-registration-check-valid-p (check)
1043   (assoc check spam-registration-functions))
1044
1045 (defun spam-unregistration-check-valid-p (check)
1046   (assoc check spam-registration-functions))
1047
1048 (defun spam-registration-function (classification check)
1049   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1050     (if (eq classification 'spam)
1051         (nth 1 flist)
1052       (nth 0 flist))))
1053
1054 (defun spam-unregistration-function (classification check)
1055   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1056     (if (eq classification 'spam)
1057         (nth 3 flist)
1058       (nth 2 flist))))
1059
1060 (defun spam-list-articles (articles classification)
1061   (let ((mark-check (if (eq classification 'spam)
1062                         'spam-group-spam-mark-p
1063                       'spam-group-ham-mark-p))
1064         list mark-cache-yes mark-cache-no)
1065     (dolist (article articles)
1066       (let ((mark (gnus-summary-article-mark article)))
1067         (unless (memq mark mark-cache-no)
1068           (if (memq mark mark-cache-yes)
1069               (push article list)
1070             ;; else, we have to actually check the mark
1071             (if (funcall mark-check
1072                          gnus-newsgroup-name
1073                          mark)
1074                 (progn
1075                   (push article list)
1076                   (push mark mark-cache-yes))
1077               (push mark mark-cache-no))))))
1078     list))
1079
1080 (defun spam-register-routine (classification
1081                               check
1082                               &optional unregister
1083                               specific-articles)
1084   (when (and (spam-classification-valid-p classification)
1085              (spam-registration-check-valid-p check))
1086     (let* ((register-function
1087             (spam-registration-function classification check))
1088            (unregister-function
1089             (spam-unregistration-function classification check))
1090            (run-function (if unregister
1091                              unregister-function
1092                            register-function))
1093            (log-function (if unregister
1094                              'spam-log-undo-registration
1095                            'spam-log-processing-to-registry))
1096            article articles)
1097
1098       (when run-function
1099         ;; make list of articles, using specific-articles if given
1100         (setq articles (or specific-articles
1101                            (spam-list-articles
1102                             gnus-newsgroup-articles
1103                             classification)))
1104         ;; process them
1105         (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1106                       (if unregister "Unregistering" "Registering")
1107                       (length articles)
1108                       (if specific-articles "specific" "")
1109                       (symbol-name classification)
1110                       (symbol-name check))
1111         (funcall run-function articles)
1112         ;; now log all the registrations (or undo them, depending on unregister)
1113         (dolist (article articles)
1114           (funcall log-function
1115                    (spam-fetch-field-message-id-fast article)
1116                    'process
1117                    classification
1118                    check
1119                    gnus-newsgroup-name))))))
1120
1121 ;;; log a ham- or spam-processor invocation to the registry
1122 (defun spam-log-processing-to-registry (id type classification check group)
1123   (when spam-log-to-registry
1124     (if (and (stringp id)
1125              (stringp group)
1126              (spam-process-type-valid-p type)
1127              (spam-classification-valid-p classification)
1128              (spam-registration-check-valid-p check))
1129         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1130               (cell (list classification check group)))
1131           (push cell cell-list)
1132           (gnus-registry-store-extra-entry
1133            id
1134            type
1135            cell-list))
1136
1137       (gnus-error 5 (format "%s called with bad ID, type, classification, check, or group"
1138                             "spam-log-processing-to-registry")))))
1139
1140 ;;; check if a ham- or spam-processor registration has been done
1141 (defun spam-log-registered-p (id type)
1142   (when spam-log-to-registry
1143     (if (and (stringp id)
1144              (spam-process-type-valid-p type))
1145         (cdr-safe (gnus-registry-fetch-extra id type))
1146       (progn
1147         (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1148                               "spam-log-registered-p"))
1149         nil))))
1150
1151 ;;; check what a ham- or spam-processor registration says
1152 ;;; returns nil if conflicting registrations are found
1153 (defun spam-log-registration-type (id type)
1154   (let ((count 0)
1155         decision)
1156     (dolist (reg (spam-log-registered-p id type))
1157       (let ((classification (nth 0 reg)))
1158         (when (spam-classification-valid-p classification)
1159           (when (and decision
1160                      (not (eq classification decision)))
1161             (setq count (+ 1 count)))
1162           (setq decision classification))))
1163     (if (< 0 count)
1164         nil
1165       decision)))
1166
1167 ;;; check if a ham- or spam-processor registration needs to be undone
1168 (defun spam-log-unregistration-needed-p (id type classification check)
1169   (when spam-log-to-registry
1170     (if (and (stringp id)
1171              (spam-process-type-valid-p type)
1172              (spam-classification-valid-p classification)
1173              (spam-registration-check-valid-p check))
1174         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1175               found)
1176           (dolist (cell cell-list)
1177             (unless found
1178               (when (and (eq classification (nth 0 cell))
1179                          (eq check (nth 1 cell)))
1180                 (setq found t))))
1181           found)
1182       (progn
1183         (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1184                               "spam-log-unregistration-needed-p"))
1185         nil))))
1186
1187
1188 ;;; undo a ham- or spam-processor registration (the group is not used)
1189 (defun spam-log-undo-registration (id type classification check &optional group)
1190   (when (and spam-log-to-registry
1191              (spam-log-unregistration-needed-p id type classification check))
1192     (if (and (stringp id)
1193              (spam-process-type-valid-p type)
1194              (spam-classification-valid-p classification)
1195              (spam-registration-check-valid-p check))
1196         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1197               new-cell-list found)
1198           (dolist (cell cell-list)
1199             (unless (and (eq classification (nth 0 cell))
1200                          (eq check (nth 1 cell)))
1201               (push cell new-cell-list)))
1202           (gnus-registry-store-extra-entry
1203            id
1204            type
1205            new-cell-list))
1206       (progn
1207         (gnus-error 5 (format "%s called with bad ID, type, check, or group"
1208                               "spam-log-undo-registration"))
1209         nil))))
1210
1211 ;;; set up IMAP widening if it's necessary
1212 (defun spam-setup-widening ()
1213   (dolist (check spam-list-of-statistical-checks)
1214     (when (symbol-value check)
1215       (setq nnimap-split-download-body-default t))))
1216
1217 \f
1218 ;;;; Regex body
1219
1220 (defun spam-check-regex-body ()
1221   (let ((spam-regex-headers-ham spam-regex-body-ham)
1222         (spam-regex-headers-spam spam-regex-body-spam))
1223     (spam-check-regex-headers t)))
1224
1225 \f
1226 ;;;; Regex headers
1227
1228 (defun spam-check-regex-headers (&optional body)
1229   (let ((type (if body "body" "header"))
1230         (spam-split-group (if spam-split-symbolic-return
1231                               'spam
1232                             spam-split-group))
1233         ret found)
1234     (dolist (h-regex spam-regex-headers-ham)
1235       (unless found
1236         (goto-char (point-min))
1237         (when (re-search-forward h-regex nil t)
1238           (message "Ham regex %s search positive." type)
1239           (setq found t))))
1240     (dolist (s-regex spam-regex-headers-spam)
1241       (unless found
1242         (goto-char (point-min))
1243         (when (re-search-forward s-regex nil t)
1244           (message "Spam regex %s search positive." type)
1245           (setq found t)
1246           (setq ret spam-split-group))))
1247     ret))
1248
1249 \f
1250 ;;;; Blackholes.
1251
1252 (defun spam-reverse-ip-string (ip)
1253   (when (stringp ip)
1254     (mapconcat 'identity
1255                (nreverse (split-string ip "\\."))
1256                ".")))
1257
1258 (defun spam-check-blackholes ()
1259   "Check the Received headers for blackholed relays."
1260   (let ((headers (nnmail-fetch-field "received"))
1261         (spam-split-group (if spam-split-symbolic-return
1262                               'spam
1263                             spam-split-group))
1264         ips matches)
1265     (when headers
1266       (with-temp-buffer
1267         (insert headers)
1268         (goto-char (point-min))
1269         (gnus-message 5 "Checking headers for relay addresses")
1270         (while (re-search-forward
1271                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1272           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1273           (push (spam-reverse-ip-string (match-string 1))
1274                 ips)))
1275       (dolist (server spam-blackhole-servers)
1276         (dolist (ip ips)
1277           (unless (and spam-blackhole-good-server-regex
1278                        ;; match the good-server-regex against the reversed (again) IP string
1279                        (string-match
1280                         spam-blackhole-good-server-regex
1281                         (spam-reverse-ip-string ip)))
1282             (unless matches
1283               (let ((query-string (concat ip "." server)))
1284                 (if spam-use-dig
1285                     (let ((query-result (query-dig query-string)))
1286                       (when query-result
1287                         (gnus-message 5 "(DIG): positive blackhole check '%s'"
1288                                       query-result)
1289                         (push (list ip server query-result)
1290                               matches)))
1291                   ;; else, if not using dig.el
1292                   (when (query-dns query-string)
1293                     (gnus-message 5 "positive blackhole check")
1294                     (push (list ip server (query-dns query-string 'TXT))
1295                           matches)))))))))
1296     (when matches
1297       spam-split-group)))
1298 \f
1299 ;;;; Hashcash.
1300
1301 (condition-case nil
1302     (progn
1303       (require 'hashcash)
1304
1305       (defun spam-check-hashcash ()
1306         "Check the headers for hashcash payments."
1307         (mail-check-payment)))   ;mail-check-payment returns a boolean
1308
1309   (file-error (progn
1310                 (defalias 'mail-check-payment 'ignore)
1311                 (defalias 'spam-check-hashcash 'ignore))))
1312 \f
1313 ;;;; BBDB
1314
1315 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1316 ;;; <sacha@giotto.sj.ru>
1317
1318 ;; all this is done inside a condition-case to trap errors
1319
1320 (condition-case nil
1321     (progn
1322       (require 'bbdb)
1323       (require 'bbdb-com)
1324
1325       ;; when the BBDB changes, we want to clear out our cache
1326       (defun spam-clear-cache-BBDB (&rest immaterial)
1327         (spam-clear-cache 'spam-use-BBDB))
1328
1329       (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1330
1331       (defun spam-enter-ham-BBDB (addresses &optional remove)
1332         "Enter an address into the BBDB; implies ham (non-spam) sender"
1333         (dolist (from addresses)
1334           (when (stringp from)
1335             (let* ((parsed-address (gnus-extract-address-components from))
1336                    (name (or (nth 0 parsed-address) "Ham Sender"))
1337                    (remove-function (if remove
1338                                         'bbdb-delete-record-internal
1339                                       'ignore))
1340                    (net-address (nth 1 parsed-address))
1341                    (record (and net-address
1342                                 (bbdb-search-simple nil net-address))))
1343               (when net-address
1344                 (gnus-message 5 "%s address %s %s BBDB"
1345                               (if remove "Deleting" "Adding")
1346                               from
1347                               (if remove "from" "to"))
1348                 (if record
1349                     (funcall remove-function record)
1350                   (bbdb-create-internal name nil net-address nil nil
1351                                         "ham sender added by spam.el")))))))
1352
1353       (defun spam-BBDB-register-routine (articles &optional unregister)
1354         (let (addresses)
1355           (dolist (article articles)
1356             (when (stringp (spam-fetch-field-from-fast article))
1357               (push (spam-fetch-field-from-fast article) addresses)))
1358           ;; now do the register/unregister action
1359           (spam-enter-ham-BBDB addresses unregister)))
1360
1361       (defun spam-BBDB-unregister-routine (articles)
1362         (spam-BBDB-register-routine articles t))
1363
1364       (defun spam-check-BBDB ()
1365         "Mail from people in the BBDB is classified as ham or non-spam"
1366         (let ((who (nnmail-fetch-field "from"))
1367               (spam-split-group (if spam-split-symbolic-return
1368                                     'spam
1369                                   spam-split-group))
1370               bbdb-cache bbdb-hashtable)
1371           
1372           (when spam-cache-lookups
1373             (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1374             (unless bbdb-cache
1375               (setq bbdb-cache
1376                     ;; this is the expanded (bbdb-hashtable) macro
1377                     ;; without the debugging support
1378                     (with-current-buffer (bbdb-buffer)
1379                       (save-excursion
1380                         (save-window-excursion
1381                           (bbdb-records nil t)
1382                           bbdb-hashtable))))
1383               (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1384           (when who
1385             (setq who (nth 1 (gnus-extract-address-components who)))
1386             (if
1387                 (if spam-cache-lookups
1388                     (symbol-value 
1389                      (intern-soft who bbdb-cache))
1390                   (bbdb-search-simple nil who))
1391                 t
1392               (if spam-use-BBDB-exclusive
1393                   spam-split-group
1394                 nil))))))
1395
1396   (file-error (progn
1397                 (defalias 'bbdb-search-simple 'ignore)
1398                 (defalias 'bbdb-records 'ignore)
1399                 (defalias 'bbdb-buffer 'ignore)
1400                 (defalias 'spam-check-BBDB 'ignore)
1401                 (defalias 'spam-BBDB-register-routine 'ignore)
1402                 (defalias 'spam-enter-ham-BBDB 'ignore)
1403                 (defalias 'bbdb-create-internal 'ignore)
1404                 (defalias 'bbdb-delete-record-internal 'ignore)
1405                 (defalias 'bbdb-records 'ignore))))
1406
1407 \f
1408 ;;;; ifile
1409
1410 ;;; check the ifile backend; return nil if the mail was NOT classified
1411 ;;; as spam
1412
1413 (defun spam-get-ifile-database-parameter ()
1414   "Get the command-line parameter for ifile's database from
1415   spam-ifile-database-path."
1416   (if spam-ifile-database-path
1417       (format "--db-file=%s" spam-ifile-database-path)
1418     nil))
1419
1420 (defun spam-check-ifile ()
1421   "Check the ifile backend for the classification of this message."
1422   (let ((article-buffer-name (buffer-name))
1423         (spam-split-group (if spam-split-symbolic-return
1424                               'spam
1425                             spam-split-group))
1426         category return)
1427     (with-temp-buffer
1428       (let ((temp-buffer-name (buffer-name))
1429             (db-param (spam-get-ifile-database-parameter)))
1430         (save-excursion
1431           (set-buffer article-buffer-name)
1432           (apply 'call-process-region
1433                  (point-min) (point-max) spam-ifile-path
1434                  nil temp-buffer-name nil "-c"
1435                  (if db-param `(,db-param "-q") `("-q"))))
1436         ;; check the return now (we're back in the temp buffer)
1437         (goto-char (point-min))
1438         (if (not (eobp))
1439             (setq category (buffer-substring (point) (point-at-eol))))
1440         (when (not (zerop (length category))) ; we need a category here
1441           (if spam-ifile-all-categories
1442               (setq return category)
1443             ;; else, if spam-ifile-all-categories is not set...
1444             (when (string-equal spam-ifile-spam-category category)
1445               (setq return spam-split-group)))))) ; note return is nil otherwise
1446     return))
1447
1448 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1449   "Register an article, given as a string, with a category.
1450 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1451   (let ((category (or category gnus-newsgroup-name))
1452         (add-or-delete-option (if unregister "-d" "-i"))
1453         (db (spam-get-ifile-database-parameter))
1454         parameters)
1455     (with-temp-buffer
1456       (dolist (article articles)
1457         (let ((article-string (spam-get-article-as-string article)))
1458           (when (stringp article-string)
1459             (insert article-string))))
1460       (apply 'call-process-region
1461              (point-min) (point-max) spam-ifile-path
1462              nil nil nil
1463              add-or-delete-option category
1464              (if db `(,db "-h") `("-h"))))))
1465
1466 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1467   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1468
1469 (defun spam-ifile-unregister-spam-routine (articles)
1470   (spam-ifile-register-spam-routine articles t))
1471
1472 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1473   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1474
1475 (defun spam-ifile-unregister-ham-routine (articles)
1476   (spam-ifile-register-ham-routine articles t))
1477
1478 \f
1479 ;;;; spam-stat
1480
1481 (condition-case nil
1482     (progn
1483       (let ((spam-stat-install-hooks nil))
1484         (require 'spam-stat))
1485
1486       (defun spam-check-stat ()
1487         "Check the spam-stat backend for the classification of this message"
1488         (let ((spam-split-group (if spam-split-symbolic-return
1489                                     'spam
1490                                   spam-split-group))
1491               (spam-stat-split-fancy-spam-group spam-split-group) ; override
1492               (spam-stat-buffer (buffer-name)) ; stat the current buffer
1493               category return)
1494           (spam-stat-split-fancy)))
1495
1496       (defun spam-stat-register-spam-routine (articles &optional unregister)
1497         (dolist (article articles)
1498           (let ((article-string (spam-get-article-as-string article)))
1499             (with-temp-buffer
1500               (insert article-string)
1501               (if unregister
1502                   (spam-stat-buffer-change-to-non-spam)
1503               (spam-stat-buffer-is-spam))))))
1504
1505       (defun spam-stat-unregister-spam-routine (articles)
1506         (spam-stat-register-spam-routine articles t))
1507
1508       (defun spam-stat-register-ham-routine (articles &optional unregister)
1509         (dolist (article articles)
1510           (let ((article-string (spam-get-article-as-string article)))
1511             (with-temp-buffer
1512               (insert article-string)
1513               (if unregister
1514                   (spam-stat-buffer-change-to-spam)
1515               (spam-stat-buffer-is-non-spam))))))
1516
1517       (defun spam-stat-unregister-ham-routine (articles)
1518         (spam-stat-register-ham-routine articles t))
1519
1520       (defun spam-maybe-spam-stat-load ()
1521         (when spam-use-stat (spam-stat-load)))
1522
1523       (defun spam-maybe-spam-stat-save ()
1524         (when spam-use-stat (spam-stat-save))))
1525
1526   (file-error (progn
1527                 (defalias 'spam-stat-load 'ignore)
1528                 (defalias 'spam-stat-save 'ignore)
1529                 (defalias 'spam-maybe-spam-stat-load 'ignore)
1530                 (defalias 'spam-maybe-spam-stat-save 'ignore)
1531                 (defalias 'spam-stat-register-ham-routine 'ignore)
1532                 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1533                 (defalias 'spam-stat-register-spam-routine 'ignore)
1534                 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1535                 (defalias 'spam-stat-buffer-is-spam 'ignore)
1536                 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1537                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1538                 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1539                 (defalias 'spam-stat-split-fancy 'ignore)
1540                 (defalias 'spam-check-stat 'ignore))))
1541
1542 \f
1543
1544 ;;;; Blacklists and whitelists.
1545
1546 (defvar spam-whitelist-cache nil)
1547 (defvar spam-blacklist-cache nil)
1548
1549 (defun spam-kill-whole-line ()
1550   (beginning-of-line)
1551   (let ((kill-whole-line t))
1552     (kill-line)))
1553
1554 ;;; address can be a list, too
1555 (defun spam-enter-whitelist (address &optional remove)
1556   "Enter ADDRESS (list or single) into the whitelist.
1557 With a non-nil REMOVE, remove them."
1558   (interactive "sAddress: ")
1559   (spam-enter-list address spam-whitelist remove)
1560   (setq spam-whitelist-cache nil))
1561
1562 ;;; address can be a list, too
1563 (defun spam-enter-blacklist (address &optional remove)
1564   "Enter ADDRESS (list or single) into the blacklist.
1565 With a non-nil REMOVE, remove them."
1566   (interactive "sAddress: ")
1567   (spam-enter-list address spam-blacklist remove)
1568   (setq spam-blacklist-cache nil))
1569
1570 (defun spam-enter-list (addresses file &optional remove)
1571   "Enter ADDRESSES into the given FILE.
1572 Either the whitelist or the blacklist files can be used.  With
1573 REMOVE not nil, remove the ADDRESSES."
1574   (if (stringp addresses)
1575       (spam-enter-list (list addresses) file remove)
1576     ;; else, we have a list of addresses here
1577     (unless (file-exists-p (file-name-directory file))
1578       (make-directory (file-name-directory file) t))
1579     (save-excursion
1580       (set-buffer
1581        (find-file-noselect file))
1582       (dolist (a addresses)
1583         (when (stringp a)
1584           (goto-char (point-min))
1585           (if (re-search-forward (regexp-quote a) nil t)
1586               ;; found the address
1587               (when remove
1588                 (spam-kill-whole-line))
1589             ;; else, the address was not found
1590             (unless remove
1591               (goto-char (point-max))
1592               (unless (bobp)
1593                 (insert "\n"))
1594               (insert a "\n")))))
1595       (save-buffer))))
1596
1597 ;;; returns t if the sender is in the whitelist, nil or
1598 ;;; spam-split-group otherwise
1599 (defun spam-check-whitelist ()
1600   ;; FIXME!  Should it detect when file timestamps change?
1601   (let ((spam-split-group (if spam-split-symbolic-return
1602                               'spam
1603                             spam-split-group)))
1604     (unless spam-whitelist-cache
1605       (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1606     (if (spam-from-listed-p spam-whitelist-cache)
1607         t
1608       (if spam-use-whitelist-exclusive
1609           spam-split-group
1610         nil))))
1611
1612 (defun spam-check-blacklist ()
1613   ;; FIXME!  Should it detect when file timestamps change?
1614   (let ((spam-split-group (if spam-split-symbolic-return
1615                               'spam
1616                             spam-split-group)))
1617     (unless spam-blacklist-cache
1618       (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1619     (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1620
1621 (defun spam-parse-list (file)
1622   (when (file-readable-p file)
1623     (let (contents address)
1624       (with-temp-buffer
1625         (insert-file-contents file)
1626         (while (not (eobp))
1627           (setq address (buffer-substring (point) (point-at-eol)))
1628           (forward-line 1)
1629           ;; insert the e-mail address if detected, otherwise the raw data
1630           (unless (zerop (length address))
1631             (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1632               (push (or pure-address address) contents)))))
1633       (nreverse contents))))
1634
1635 (defun spam-from-listed-p (cache)
1636   (let ((from (nnmail-fetch-field "from"))
1637         found)
1638     (while cache
1639       (let ((address (pop cache)))
1640         (unless (zerop (length address)) ; 0 for a nil address too
1641           (setq address (regexp-quote address))
1642           ;; fix regexp-quote's treatment of user-intended regexes
1643           (while (string-match "\\\\\\*" address)
1644             (setq address (replace-match ".*" t t address))))
1645         (when (and address (string-match address from))
1646           (setq found t
1647                 cache nil))))
1648     found))
1649
1650 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1651   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1652         (declassification (if blacklist 'ham 'spam))
1653         (enter-function
1654          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1655         (remove-function
1656          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1657         from addresses unregister-list)
1658     (dolist (article articles)
1659       (let ((from (spam-fetch-field-from-fast article))
1660             (id (spam-fetch-field-message-id-fast article))
1661             sender-ignored)
1662         (when (stringp from)
1663           (dolist (ignore-regex spam-blacklist-ignored-regexes)
1664             (when (and (not sender-ignored)
1665                        (stringp ignore-regex)
1666                        (string-match ignore-regex from))
1667               (setq sender-ignored t)))
1668           ;; remember the messages we need to unregister, unless remove is set
1669           (when (and
1670                  (null unregister)
1671                  (spam-log-unregistration-needed-p
1672                   id 'process declassification de-symbol))
1673             (push from unregister-list))
1674           (unless sender-ignored
1675             (push from addresses)))))
1676
1677     (if unregister
1678         (funcall enter-function addresses t) ; unregister all these addresses
1679       ;; else, register normally and unregister what we need to
1680       (funcall remove-function unregister-list t)
1681       (dolist (article unregister-list)
1682         (spam-log-undo-registration
1683          (spam-fetch-field-message-id-fast article)
1684          'process
1685          declassification
1686          de-symbol))
1687       (funcall enter-function addresses nil))))
1688
1689 (defun spam-blacklist-unregister-routine (articles)
1690   (spam-blacklist-register-routine articles t))
1691
1692 (defun spam-blacklist-register-routine (articles &optional unregister)
1693   (spam-filelist-register-routine articles t unregister))
1694
1695 (defun spam-whitelist-unregister-routine (articles)
1696   (spam-whitelist-register-routine articles t))
1697
1698 (defun spam-whitelist-register-routine (articles &optional unregister)
1699   (spam-filelist-register-routine articles nil unregister))
1700
1701 \f
1702 ;;;; Spam-report glue
1703 (defun spam-report-gmane-register-routine (articles)
1704   (when articles
1705     (apply 'spam-report-gmane articles)))
1706
1707 \f
1708 ;;;; Bogofilter
1709 (defun spam-check-bogofilter-headers (&optional score)
1710   (let ((header (nnmail-fetch-field spam-bogofilter-header))
1711         (spam-split-group (if spam-split-symbolic-return
1712                               'spam
1713                             spam-split-group)))
1714     (when header                        ; return nil when no header
1715       (if score                         ; scoring mode
1716           (if (string-match "spamicity=\\([0-9.]+\\)" header)
1717               (match-string 1 header)
1718             "0")
1719         ;; spam detection mode
1720         (when (string-match spam-bogofilter-bogosity-positive-spam-header
1721                             header)
1722           spam-split-group)))))
1723
1724 ;; return something sensible if the score can't be determined
1725 (defun spam-bogofilter-score ()
1726   "Get the Bogofilter spamicity score"
1727   (interactive)
1728   (save-window-excursion
1729     (gnus-summary-show-article t)
1730     (set-buffer gnus-article-buffer)
1731     (let ((score (or (spam-check-bogofilter-headers t)
1732                      (spam-check-bogofilter t))))
1733       (message "Spamicity score %s" score)
1734       (or score "0"))
1735     (gnus-summary-show-article)))
1736
1737 (defun spam-check-bogofilter (&optional score)
1738   "Check the Bogofilter backend for the classification of this message"
1739   (let ((article-buffer-name (buffer-name))
1740         (db spam-bogofilter-database-directory)
1741         return)
1742     (with-temp-buffer
1743       (let ((temp-buffer-name (buffer-name)))
1744         (save-excursion
1745           (set-buffer article-buffer-name)
1746           (apply 'call-process-region
1747                  (point-min) (point-max)
1748                  spam-bogofilter-path
1749                  nil temp-buffer-name nil
1750                  (if db `("-d" ,db "-v") `("-v"))))
1751         (setq return (spam-check-bogofilter-headers score))))
1752     return))
1753
1754 (defun spam-bogofilter-register-with-bogofilter (articles
1755                                                  spam
1756                                                  &optional unregister)
1757   "Register an article, given as a string, as spam or non-spam."
1758   (dolist (article articles)
1759     (let ((article-string (spam-get-article-as-string article))
1760           (db spam-bogofilter-database-directory)
1761           (switch (if unregister
1762                       (if spam
1763                           spam-bogofilter-spam-strong-switch
1764                         spam-bogofilter-ham-strong-switch)
1765                     (if spam
1766                         spam-bogofilter-spam-switch
1767                       spam-bogofilter-ham-switch))))
1768       (when (stringp article-string)
1769         (with-temp-buffer
1770           (insert article-string)
1771
1772           (apply 'call-process-region
1773                  (point-min) (point-max)
1774                  spam-bogofilter-path
1775                  nil nil nil switch
1776                  (if db `("-d" ,db "-v") `("-v"))))))))
1777
1778 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1779   (spam-bogofilter-register-with-bogofilter articles t unregister))
1780
1781 (defun spam-bogofilter-unregister-spam-routine (articles)
1782   (spam-bogofilter-register-spam-routine articles t))
1783
1784 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1785   (spam-bogofilter-register-with-bogofilter articles nil unregister))
1786
1787 (defun spam-bogofilter-unregister-ham-routine (articles)
1788   (spam-bogofilter-register-ham-routine articles t))
1789
1790
1791 \f
1792 ;;;; spamoracle
1793 (defun spam-check-spamoracle ()
1794   "Run spamoracle on an article to determine whether it's spam."
1795   (let ((article-buffer-name (buffer-name))
1796         (spam-split-group (if spam-split-symbolic-return
1797                               'spam
1798                             spam-split-group)))
1799     (with-temp-buffer
1800       (let ((temp-buffer-name (buffer-name)))
1801         (save-excursion
1802           (set-buffer article-buffer-name)
1803           (let ((status
1804                  (apply 'call-process-region
1805                         (point-min) (point-max)
1806                         spam-spamoracle-binary
1807                         nil temp-buffer-name nil
1808                         (if spam-spamoracle-database
1809                             `("-f" ,spam-spamoracle-database "mark")
1810                           '("mark")))))
1811             (if (eq 0 status)
1812                 (progn
1813                   (set-buffer temp-buffer-name)
1814                   (goto-char (point-min))
1815                   (when (re-search-forward "^X-Spam: yes;" nil t)
1816                     spam-split-group))
1817               (error "Error running spamoracle" status))))))))
1818
1819 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1820   "Run spamoracle in training mode."
1821   (with-temp-buffer
1822     (let ((temp-buffer-name (buffer-name)))
1823       (save-excursion
1824         (goto-char (point-min))
1825         (dolist (article articles)
1826           (insert (spam-get-article-as-string article)))
1827         (let* ((arg (if (spam-xor unregister article-is-spam-p)
1828                         "-spam"
1829                       "-good"))
1830                (status
1831                 (apply 'call-process-region
1832                        (point-min) (point-max)
1833                        spam-spamoracle-binary
1834                        nil temp-buffer-name nil
1835                        (if spam-spamoracle-database
1836                            `("-f" ,spam-spamoracle-database
1837                              "add" ,arg)
1838                          `("add" ,arg)))))
1839           (when (not (eq 0 status))
1840             (error "Error running spamoracle" status)))))))
1841
1842 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1843   (spam-spamoracle-learn articles nil unregister))
1844
1845 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1846   (spam-spamoracle-learn-ham articles t))
1847
1848 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1849   (spam-spamoracle-learn articles t unregister))
1850
1851 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1852   (spam-spamoracle-learn-spam articles t))
1853
1854 \f
1855 ;;;; Hooks
1856
1857 ;;;###autoload
1858 (defun spam-initialize ()
1859   "Install the spam.el hooks and do other initialization"
1860   (interactive)
1861   (setq spam-install-hooks t)
1862   ;; TODO: How do we redo this every time spam-face is customized?
1863   (push '((eq mark gnus-spam-mark) . spam-face)
1864         gnus-summary-highlight)
1865   ;; Add hooks for loading and saving the spam stats
1866   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1867   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1868   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1869   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1870   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1871   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1872   (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1873
1874 (defun spam-unload-hook ()
1875   "Uninstall the spam.el hooks"
1876   (interactive)
1877   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1878   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1879   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1880   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1881   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1882   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1883   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1884
1885 (when spam-install-hooks
1886   (spam-initialize))
1887
1888 (provide 'spam)
1889
1890 ;;; spam.el ends here.