cf7a85d5c55bd394fc4f0b427b64379761d3ccb4
[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 t
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
942     (when (and autodetect
943                (not (equal first-method 'none)))
944     (mapcar
945      (lambda (article)
946        (let ((id (spam-fetch-field-message-id-fast article))
947              (subject (spam-fetch-field-subject-fast article))
948              (sender (spam-fetch-field-from-fast article))
949              registry-lookup)
950          
951          (unless id
952            (gnus-error 5 "Article %d has no message ID!" article))
953          
954          (when (and id spam-log-to-registry)
955            (setq registry-lookup (spam-log-registration-type id 'incoming))
956            (when registry-lookup
957              (gnus-message 
958               9 
959               "spam-find-spam: message %s was already registered incoming"
960               id)))
961
962          (let* ((spam-split-symbolic-return t)
963                 (spam-split-symbolic-return-positive t)
964                 (split-return 
965                  (or registry-lookup
966                      (with-temp-buffer
967                        (gnus-request-article-this-buffer
968                         article
969                         group)
970                        (if (or (null first-method)
971                                (equal first-method 'default))
972                            (spam-split)
973                          (apply 'spam-split methods))))))
974            (if (equal split-return 'spam)
975                (gnus-summary-mark-article article gnus-spam-mark))
976            
977            (when (and id split-return spam-log-to-registry)
978              (when (zerop (gnus-registry-group-count id))
979                (gnus-registry-add-group
980                 id group subject sender))
981                
982              (unless registry-lookup
983                (spam-log-processing-to-registry
984                 id
985                 'incoming
986                 split-return
987                 spam-split-last-successful-check
988                 group))))))
989     articles))))
990
991 (defvar spam-registration-functions
992   ;; first the ham register, second the spam register function
993   ;; third the ham unregister, fourth the spam unregister function
994   '((spam-use-blacklist  nil
995                          spam-blacklist-register-routine
996                          nil
997                          spam-blacklist-unregister-routine)
998     (spam-use-whitelist  spam-whitelist-register-routine
999                          nil
1000                          spam-whitelist-unregister-routine
1001                          nil)
1002     (spam-use-BBDB       spam-BBDB-register-routine
1003                          nil
1004                          spam-BBDB-unregister-routine
1005                          nil)
1006     (spam-use-ifile      spam-ifile-register-ham-routine
1007                          spam-ifile-register-spam-routine
1008                          spam-ifile-unregister-ham-routine
1009                          spam-ifile-unregister-spam-routine)
1010     (spam-use-spamoracle spam-spamoracle-learn-ham
1011                          spam-spamoracle-learn-spam
1012                          spam-spamoracle-unlearn-ham
1013                          spam-spamoracle-unlearn-spam)
1014     (spam-use-stat       spam-stat-register-ham-routine
1015                          spam-stat-register-spam-routine
1016                          spam-stat-unregister-ham-routine
1017                          spam-stat-unregister-spam-routine)
1018     ;; note that spam-use-gmane is not a legitimate check
1019     (spam-use-gmane      nil
1020                          spam-report-gmane-register-routine
1021                          ;; does Gmane support unregistration?
1022                          nil
1023                          nil)
1024     (spam-use-bogofilter spam-bogofilter-register-ham-routine
1025                          spam-bogofilter-register-spam-routine
1026                          spam-bogofilter-unregister-ham-routine
1027                          spam-bogofilter-unregister-spam-routine))
1028   "The spam-registration-functions list contains pairs
1029 associating a parameter variable with the ham and spam
1030 registration functions, and the ham and spam unregistration
1031 functions")
1032
1033 (defun spam-classification-valid-p (classification)
1034   (or  (eq classification 'spam)
1035        (eq classification 'ham)))
1036
1037 (defun spam-process-type-valid-p (process-type)
1038   (or  (eq process-type 'incoming)
1039        (eq process-type 'process)))
1040
1041 (defun spam-registration-check-valid-p (check)
1042   (assoc check spam-registration-functions))
1043
1044 (defun spam-unregistration-check-valid-p (check)
1045   (assoc check spam-registration-functions))
1046
1047 (defun spam-registration-function (classification check)
1048   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1049     (if (eq classification 'spam)
1050         (nth 1 flist)
1051       (nth 0 flist))))
1052
1053 (defun spam-unregistration-function (classification check)
1054   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1055     (if (eq classification 'spam)
1056         (nth 3 flist)
1057       (nth 2 flist))))
1058
1059 (defun spam-list-articles (articles classification)
1060   (let ((mark-check (if (eq classification 'spam)
1061                         'spam-group-spam-mark-p
1062                       'spam-group-ham-mark-p))
1063         list mark-cache-yes mark-cache-no)
1064     (dolist (article articles)
1065       (let ((mark (gnus-summary-article-mark article)))
1066         (unless (memq mark mark-cache-no)
1067           (if (memq mark mark-cache-yes)
1068               (push article list)
1069             ;; else, we have to actually check the mark
1070             (if (funcall mark-check
1071                          gnus-newsgroup-name
1072                          mark)
1073                 (progn
1074                   (push article list)
1075                   (push mark mark-cache-yes))
1076               (push mark mark-cache-no))))))
1077     list))
1078
1079 (defun spam-register-routine (classification
1080                               check
1081                               &optional unregister
1082                               specific-articles)
1083   (when (and (spam-classification-valid-p classification)
1084              (spam-registration-check-valid-p check))
1085     (let* ((register-function
1086             (spam-registration-function classification check))
1087            (unregister-function
1088             (spam-unregistration-function classification check))
1089            (run-function (if unregister
1090                              unregister-function
1091                            register-function))
1092            (log-function (if unregister
1093                              'spam-log-undo-registration
1094                            'spam-log-processing-to-registry))
1095            article articles)
1096
1097       (when run-function
1098         ;; make list of articles, using specific-articles if given
1099         (setq articles (or specific-articles
1100                            (spam-list-articles
1101                             gnus-newsgroup-articles
1102                             classification)))
1103         ;; process them
1104         (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1105                       (if unregister "Unregistering" "Registering")
1106                       (length articles)
1107                       (if specific-articles "specific" "")
1108                       (symbol-name classification)
1109                       (symbol-name check))
1110         (funcall run-function articles)
1111         ;; now log all the registrations (or undo them, depending on unregister)
1112         (dolist (article articles)
1113           (funcall log-function
1114                    (spam-fetch-field-message-id-fast article)
1115                    'process
1116                    classification
1117                    check
1118                    gnus-newsgroup-name))))))
1119
1120 ;;; log a ham- or spam-processor invocation to the registry
1121 (defun spam-log-processing-to-registry (id type classification check group)
1122   (when spam-log-to-registry
1123     (if (and (stringp id)
1124              (stringp group)
1125              (spam-process-type-valid-p type)
1126              (spam-classification-valid-p classification)
1127              (spam-registration-check-valid-p check))
1128         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1129               (cell (list classification check group)))
1130           (push cell cell-list)
1131           (gnus-registry-store-extra-entry
1132            id
1133            type
1134            cell-list))
1135
1136       (gnus-error 5 (format "%s called with bad ID, type, classification, check, or group"
1137                             "spam-log-processing-to-registry")))))
1138
1139 ;;; check if a ham- or spam-processor registration has been done
1140 (defun spam-log-registered-p (id type)
1141   (when spam-log-to-registry
1142     (if (and (stringp id)
1143              (spam-process-type-valid-p type))
1144         (cdr-safe (gnus-registry-fetch-extra id type))
1145       (progn
1146         (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1147                               "spam-log-registered-p"))
1148         nil))))
1149
1150 ;;; check what a ham- or spam-processor registration says
1151 ;;; returns nil if conflicting registrations are found
1152 (defun spam-log-registration-type (id type)
1153   (let ((count 0)
1154         decision)
1155     (dolist (reg (spam-log-registered-p id type))
1156       (let ((classification (nth 0 reg)))
1157         (when (spam-classification-valid-p classification)
1158           (when (and decision
1159                      (not (eq classification decision)))
1160             (setq count (+ 1 count)))
1161           (setq decision classification))))
1162     (if (< 0 count)
1163         nil
1164       decision)))
1165
1166 ;;; check if a ham- or spam-processor registration needs to be undone
1167 (defun spam-log-unregistration-needed-p (id type classification check)
1168   (when spam-log-to-registry
1169     (if (and (stringp id)
1170              (spam-process-type-valid-p type)
1171              (spam-classification-valid-p classification)
1172              (spam-registration-check-valid-p check))
1173         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1174               found)
1175           (dolist (cell cell-list)
1176             (unless found
1177               (when (and (eq classification (nth 0 cell))
1178                          (eq check (nth 1 cell)))
1179                 (setq found t))))
1180           found)
1181       (progn
1182         (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1183                               "spam-log-unregistration-needed-p"))
1184         nil))))
1185
1186
1187 ;;; undo a ham- or spam-processor registration (the group is not used)
1188 (defun spam-log-undo-registration (id type classification check &optional group)
1189   (when (and spam-log-to-registry
1190              (spam-log-unregistration-needed-p id type classification check))
1191     (if (and (stringp id)
1192              (spam-process-type-valid-p type)
1193              (spam-classification-valid-p classification)
1194              (spam-registration-check-valid-p check))
1195         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1196               new-cell-list found)
1197           (dolist (cell cell-list)
1198             (unless (and (eq classification (nth 0 cell))
1199                          (eq check (nth 1 cell)))
1200               (push cell new-cell-list)))
1201           (gnus-registry-store-extra-entry
1202            id
1203            type
1204            new-cell-list))
1205       (progn
1206         (gnus-error 5 (format "%s called with bad ID, type, check, or group"
1207                               "spam-log-undo-registration"))
1208         nil))))
1209
1210 ;;; set up IMAP widening if it's necessary
1211 (defun spam-setup-widening ()
1212   (dolist (check spam-list-of-statistical-checks)
1213     (when (symbol-value check)
1214       (setq nnimap-split-download-body-default t))))
1215
1216 \f
1217 ;;;; Regex body
1218
1219 (defun spam-check-regex-body ()
1220   (let ((spam-regex-headers-ham spam-regex-body-ham)
1221         (spam-regex-headers-spam spam-regex-body-spam))
1222     (spam-check-regex-headers t)))
1223
1224 \f
1225 ;;;; Regex headers
1226
1227 (defun spam-check-regex-headers (&optional body)
1228   (let ((type (if body "body" "header"))
1229         (spam-split-group (if spam-split-symbolic-return
1230                               'spam
1231                             spam-split-group))
1232         ret found)
1233     (dolist (h-regex spam-regex-headers-ham)
1234       (unless found
1235         (goto-char (point-min))
1236         (when (re-search-forward h-regex nil t)
1237           (message "Ham regex %s search positive." type)
1238           (setq found t))))
1239     (dolist (s-regex spam-regex-headers-spam)
1240       (unless found
1241         (goto-char (point-min))
1242         (when (re-search-forward s-regex nil t)
1243           (message "Spam regex %s search positive." type)
1244           (setq found t)
1245           (setq ret spam-split-group))))
1246     ret))
1247
1248 \f
1249 ;;;; Blackholes.
1250
1251 (defun spam-reverse-ip-string (ip)
1252   (when (stringp ip)
1253     (mapconcat 'identity
1254                (nreverse (split-string ip "\\."))
1255                ".")))
1256
1257 (defun spam-check-blackholes ()
1258   "Check the Received headers for blackholed relays."
1259   (let ((headers (nnmail-fetch-field "received"))
1260         (spam-split-group (if spam-split-symbolic-return
1261                               'spam
1262                             spam-split-group))
1263         ips matches)
1264     (when headers
1265       (with-temp-buffer
1266         (insert headers)
1267         (goto-char (point-min))
1268         (gnus-message 5 "Checking headers for relay addresses")
1269         (while (re-search-forward
1270                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1271           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1272           (push (spam-reverse-ip-string (match-string 1))
1273                 ips)))
1274       (dolist (server spam-blackhole-servers)
1275         (dolist (ip ips)
1276           (unless (and spam-blackhole-good-server-regex
1277                        ;; match the good-server-regex against the reversed (again) IP string
1278                        (string-match
1279                         spam-blackhole-good-server-regex
1280                         (spam-reverse-ip-string ip)))
1281             (unless matches
1282               (let ((query-string (concat ip "." server)))
1283                 (if spam-use-dig
1284                     (let ((query-result (query-dig query-string)))
1285                       (when query-result
1286                         (gnus-message 5 "(DIG): positive blackhole check '%s'"
1287                                       query-result)
1288                         (push (list ip server query-result)
1289                               matches)))
1290                   ;; else, if not using dig.el
1291                   (when (query-dns query-string)
1292                     (gnus-message 5 "positive blackhole check")
1293                     (push (list ip server (query-dns query-string 'TXT))
1294                           matches)))))))))
1295     (when matches
1296       spam-split-group)))
1297 \f
1298 ;;;; Hashcash.
1299
1300 (condition-case nil
1301     (progn
1302       (require 'hashcash)
1303
1304       (defun spam-check-hashcash ()
1305         "Check the headers for hashcash payments."
1306         (mail-check-payment)))   ;mail-check-payment returns a boolean
1307
1308   (file-error (progn
1309                 (defalias 'mail-check-payment 'ignore)
1310                 (defalias 'spam-check-hashcash 'ignore))))
1311 \f
1312 ;;;; BBDB
1313
1314 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1315 ;;; <sacha@giotto.sj.ru>
1316
1317 ;; all this is done inside a condition-case to trap errors
1318
1319 (condition-case nil
1320     (progn
1321       (require 'bbdb)
1322       (require 'bbdb-com)
1323
1324       ;; when the BBDB changes, we want to clear out our cache
1325       (defun spam-clear-cache-BBDB (&rest immaterial)
1326         (spam-clear-cache 'spam-use-BBDB))
1327
1328       (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1329
1330       (defun spam-enter-ham-BBDB (addresses &optional remove)
1331         "Enter an address into the BBDB; implies ham (non-spam) sender"
1332         (dolist (from addresses)
1333           (when (stringp from)
1334             (let* ((parsed-address (gnus-extract-address-components from))
1335                    (name (or (nth 0 parsed-address) "Ham Sender"))
1336                    (remove-function (if remove
1337                                         'bbdb-delete-record-internal
1338                                       'ignore))
1339                    (net-address (nth 1 parsed-address))
1340                    (record (and net-address
1341                                 (bbdb-search-simple nil net-address))))
1342               (when net-address
1343                 (gnus-message 5 "%s address %s %s BBDB"
1344                               (if remove "Deleting" "Adding")
1345                               from
1346                               (if remove "from" "to"))
1347                 (if record
1348                     (funcall remove-function record)
1349                   (bbdb-create-internal name nil net-address nil nil
1350                                         "ham sender added by spam.el")))))))
1351
1352       (defun spam-BBDB-register-routine (articles &optional unregister)
1353         (let (addresses)
1354           (dolist (article articles)
1355             (when (stringp (spam-fetch-field-from-fast article))
1356               (push (spam-fetch-field-from-fast article) addresses)))
1357           ;; now do the register/unregister action
1358           (spam-enter-ham-BBDB addresses unregister)))
1359
1360       (defun spam-BBDB-unregister-routine (articles)
1361         (spam-BBDB-register-routine articles t))
1362
1363       (defun spam-check-BBDB ()
1364         "Mail from people in the BBDB is classified as ham or non-spam"
1365         (let ((who (nnmail-fetch-field "from"))
1366               (spam-split-group (if spam-split-symbolic-return
1367                                     'spam
1368                                   spam-split-group))
1369               bbdb-cache bbdb-hashtable)
1370           
1371           (when spam-cache-lookups
1372             (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1373             (unless bbdb-cache
1374               (setq bbdb-cache
1375                     ;; this is the expanded (bbdb-hashtable) macro
1376                     ;; without the debugging support
1377                     (with-current-buffer (bbdb-buffer)
1378                       (save-excursion
1379                         (save-window-excursion
1380                           (bbdb-records nil t)
1381                           bbdb-hashtable))))
1382               (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1383           (when who
1384             (setq who (nth 1 (gnus-extract-address-components who)))
1385             (if
1386                 (if spam-cache-lookups
1387                     (symbol-value 
1388                      (intern-soft who bbdb-cache))
1389                   (bbdb-search-simple nil who))
1390                 t
1391               (if spam-use-BBDB-exclusive
1392                   spam-split-group
1393                 nil))))))
1394
1395   (file-error (progn
1396                 (defalias 'bbdb-search-simple 'ignore)
1397                 (defalias 'bbdb-records 'ignore)
1398                 (defalias 'bbdb-buffer 'ignore)
1399                 (defalias 'spam-check-BBDB 'ignore)
1400                 (defalias 'spam-BBDB-register-routine 'ignore)
1401                 (defalias 'spam-enter-ham-BBDB 'ignore)
1402                 (defalias 'bbdb-create-internal 'ignore)
1403                 (defalias 'bbdb-delete-record-internal 'ignore)
1404                 (defalias 'bbdb-records 'ignore))))
1405
1406 \f
1407 ;;;; ifile
1408
1409 ;;; check the ifile backend; return nil if the mail was NOT classified
1410 ;;; as spam
1411
1412 (defun spam-get-ifile-database-parameter ()
1413   "Get the command-line parameter for ifile's database from
1414   spam-ifile-database-path."
1415   (if spam-ifile-database-path
1416       (format "--db-file=%s" spam-ifile-database-path)
1417     nil))
1418
1419 (defun spam-check-ifile ()
1420   "Check the ifile backend for the classification of this message."
1421   (let ((article-buffer-name (buffer-name))
1422         (spam-split-group (if spam-split-symbolic-return
1423                               'spam
1424                             spam-split-group))
1425         category return)
1426     (with-temp-buffer
1427       (let ((temp-buffer-name (buffer-name))
1428             (db-param (spam-get-ifile-database-parameter)))
1429         (save-excursion
1430           (set-buffer article-buffer-name)
1431           (apply 'call-process-region
1432                  (point-min) (point-max) spam-ifile-path
1433                  nil temp-buffer-name nil "-c"
1434                  (if db-param `(,db-param "-q") `("-q"))))
1435         ;; check the return now (we're back in the temp buffer)
1436         (goto-char (point-min))
1437         (if (not (eobp))
1438             (setq category (buffer-substring (point) (point-at-eol))))
1439         (when (not (zerop (length category))) ; we need a category here
1440           (if spam-ifile-all-categories
1441               (setq return category)
1442             ;; else, if spam-ifile-all-categories is not set...
1443             (when (string-equal spam-ifile-spam-category category)
1444               (setq return spam-split-group)))))) ; note return is nil otherwise
1445     return))
1446
1447 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1448   "Register an article, given as a string, with a category.
1449 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1450   (let ((category (or category gnus-newsgroup-name))
1451         (add-or-delete-option (if unregister "-d" "-i"))
1452         (db (spam-get-ifile-database-parameter))
1453         parameters)
1454     (with-temp-buffer
1455       (dolist (article articles)
1456         (let ((article-string (spam-get-article-as-string article)))
1457           (when (stringp article-string)
1458             (insert article-string))))
1459       (apply 'call-process-region
1460              (point-min) (point-max) spam-ifile-path
1461              nil nil nil
1462              add-or-delete-option category
1463              (if db `(,db "-h") `("-h"))))))
1464
1465 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1466   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1467
1468 (defun spam-ifile-unregister-spam-routine (articles)
1469   (spam-ifile-register-spam-routine articles t))
1470
1471 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1472   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1473
1474 (defun spam-ifile-unregister-ham-routine (articles)
1475   (spam-ifile-register-ham-routine articles t))
1476
1477 \f
1478 ;;;; spam-stat
1479
1480 (condition-case nil
1481     (progn
1482       (let ((spam-stat-install-hooks nil))
1483         (require 'spam-stat))
1484
1485       (defun spam-check-stat ()
1486         "Check the spam-stat backend for the classification of this message"
1487         (let ((spam-split-group (if spam-split-symbolic-return
1488                                     'spam
1489                                   spam-split-group))
1490               (spam-stat-split-fancy-spam-group spam-split-group) ; override
1491               (spam-stat-buffer (buffer-name)) ; stat the current buffer
1492               category return)
1493           (spam-stat-split-fancy)))
1494
1495       (defun spam-stat-register-spam-routine (articles &optional unregister)
1496         (dolist (article articles)
1497           (let ((article-string (spam-get-article-as-string article)))
1498             (with-temp-buffer
1499               (insert article-string)
1500               (if unregister
1501                   (spam-stat-buffer-change-to-non-spam)
1502               (spam-stat-buffer-is-spam))))))
1503
1504       (defun spam-stat-unregister-spam-routine (articles)
1505         (spam-stat-register-spam-routine articles t))
1506
1507       (defun spam-stat-register-ham-routine (articles &optional unregister)
1508         (dolist (article articles)
1509           (let ((article-string (spam-get-article-as-string article)))
1510             (with-temp-buffer
1511               (insert article-string)
1512               (if unregister
1513                   (spam-stat-buffer-change-to-spam)
1514               (spam-stat-buffer-is-non-spam))))))
1515
1516       (defun spam-stat-unregister-ham-routine (articles)
1517         (spam-stat-register-ham-routine articles t))
1518
1519       (defun spam-maybe-spam-stat-load ()
1520         (when spam-use-stat (spam-stat-load)))
1521
1522       (defun spam-maybe-spam-stat-save ()
1523         (when spam-use-stat (spam-stat-save))))
1524
1525   (file-error (progn
1526                 (defalias 'spam-stat-load 'ignore)
1527                 (defalias 'spam-stat-save 'ignore)
1528                 (defalias 'spam-maybe-spam-stat-load 'ignore)
1529                 (defalias 'spam-maybe-spam-stat-save 'ignore)
1530                 (defalias 'spam-stat-register-ham-routine 'ignore)
1531                 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1532                 (defalias 'spam-stat-register-spam-routine 'ignore)
1533                 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1534                 (defalias 'spam-stat-buffer-is-spam 'ignore)
1535                 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1536                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1537                 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1538                 (defalias 'spam-stat-split-fancy 'ignore)
1539                 (defalias 'spam-check-stat 'ignore))))
1540
1541 \f
1542
1543 ;;;; Blacklists and whitelists.
1544
1545 (defvar spam-whitelist-cache nil)
1546 (defvar spam-blacklist-cache nil)
1547
1548 (defun spam-kill-whole-line ()
1549   (beginning-of-line)
1550   (let ((kill-whole-line t))
1551     (kill-line)))
1552
1553 ;;; address can be a list, too
1554 (defun spam-enter-whitelist (address &optional remove)
1555   "Enter ADDRESS (list or single) into the whitelist.
1556 With a non-nil REMOVE, remove them."
1557   (interactive "sAddress: ")
1558   (spam-enter-list address spam-whitelist remove)
1559   (setq spam-whitelist-cache nil)
1560   (spam-clear-cache 'spam-use-whitelist))
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   (spam-clear-cache 'spam-use-whitelist))
1570
1571 (defun spam-enter-list (addresses file &optional remove)
1572   "Enter ADDRESSES into the given FILE.
1573 Either the whitelist or the blacklist files can be used.  With
1574 REMOVE not nil, remove the ADDRESSES."
1575   (if (stringp addresses)
1576       (spam-enter-list (list addresses) file remove)
1577     ;; else, we have a list of addresses here
1578     (unless (file-exists-p (file-name-directory file))
1579       (make-directory (file-name-directory file) t))
1580     (save-excursion
1581       (set-buffer
1582        (find-file-noselect file))
1583       (dolist (a addresses)
1584         (when (stringp a)
1585           (goto-char (point-min))
1586           (if (re-search-forward (regexp-quote a) nil t)
1587               ;; found the address
1588               (when remove
1589                 (spam-kill-whole-line))
1590             ;; else, the address was not found
1591             (unless remove
1592               (goto-char (point-max))
1593               (unless (bobp)
1594                 (insert "\n"))
1595               (insert a "\n")))))
1596       (save-buffer))))
1597
1598 (defun spam-filelist-build-cache (type)
1599   (let ((cache (if (eq type 'spam-use-blacklist)
1600                    spam-blacklist-cache
1601                  spam-whitelist-cache))
1602         parsed-cache)
1603     (unless (gethash type spam-caches)
1604       (while cache
1605         (let ((address (pop cache)))
1606           (unless (zerop (length address)) ; 0 for a nil address too
1607             (setq address (regexp-quote address))
1608             ;; fix regexp-quote's treatment of user-intended regexes
1609             (while (string-match "\\\\\\*" address)
1610               (setq address (replace-match ".*" t t address))))
1611           (push address parsed-cache)))
1612       (puthash type parsed-cache spam-caches))))
1613
1614 (defun spam-filelist-check-cache (type from)
1615   (when (stringp from)
1616     (spam-filelist-build-cache type)
1617     (let (found)
1618       (dolist (address (gethash type spam-caches))
1619         (when (and address (string-match address from))
1620           (setq found t)
1621           (return)))
1622       found)))
1623
1624 ;;; returns t if the sender is in the whitelist, nil or
1625 ;;; spam-split-group otherwise
1626 (defun spam-check-whitelist ()
1627   ;; FIXME!  Should it detect when file timestamps change?
1628   (let ((spam-split-group (if spam-split-symbolic-return
1629                               'spam
1630                             spam-split-group)))
1631     (unless spam-whitelist-cache
1632       (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1633     (if (spam-from-listed-p 'spam-use-whitelist)
1634         t
1635       (if spam-use-whitelist-exclusive
1636           spam-split-group
1637         nil))))
1638
1639 (defun spam-check-blacklist ()
1640   ;; FIXME!  Should it detect when file timestamps change?
1641   (let ((spam-split-group (if spam-split-symbolic-return
1642                               'spam
1643                             spam-split-group)))
1644     (unless spam-blacklist-cache
1645       (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1646     (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1647
1648 (defun spam-parse-list (file)
1649   (when (file-readable-p file)
1650     (let (contents address)
1651       (with-temp-buffer
1652         (insert-file-contents file)
1653         (while (not (eobp))
1654           (setq address (buffer-substring (point) (point-at-eol)))
1655           (forward-line 1)
1656           ;; insert the e-mail address if detected, otherwise the raw data
1657           (unless (zerop (length address))
1658             (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1659               (push (or pure-address address) contents)))))
1660       (nreverse contents))))
1661
1662 (defun spam-from-listed-p (type)
1663   (let ((from (nnmail-fetch-field "from"))
1664         found)
1665     (spam-filelist-check-cache type from)))
1666
1667 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1668   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1669         (declassification (if blacklist 'ham 'spam))
1670         (enter-function
1671          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1672         (remove-function
1673          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1674         from addresses unregister-list)
1675     (dolist (article articles)
1676       (let ((from (spam-fetch-field-from-fast article))
1677             (id (spam-fetch-field-message-id-fast article))
1678             sender-ignored)
1679         (when (stringp from)
1680           (dolist (ignore-regex spam-blacklist-ignored-regexes)
1681             (when (and (not sender-ignored)
1682                        (stringp ignore-regex)
1683                        (string-match ignore-regex from))
1684               (setq sender-ignored t)))
1685           ;; remember the messages we need to unregister, unless remove is set
1686           (when (and
1687                  (null unregister)
1688                  (spam-log-unregistration-needed-p
1689                   id 'process declassification de-symbol))
1690             (push from unregister-list))
1691           (unless sender-ignored
1692             (push from addresses)))))
1693
1694     (if unregister
1695         (funcall enter-function addresses t) ; unregister all these addresses
1696       ;; else, register normally and unregister what we need to
1697       (funcall remove-function unregister-list t)
1698       (dolist (article unregister-list)
1699         (spam-log-undo-registration
1700          (spam-fetch-field-message-id-fast article)
1701          'process
1702          declassification
1703          de-symbol))
1704       (funcall enter-function addresses nil))))
1705
1706 (defun spam-blacklist-unregister-routine (articles)
1707   (spam-blacklist-register-routine articles t))
1708
1709 (defun spam-blacklist-register-routine (articles &optional unregister)
1710   (spam-filelist-register-routine articles t unregister))
1711
1712 (defun spam-whitelist-unregister-routine (articles)
1713   (spam-whitelist-register-routine articles t))
1714
1715 (defun spam-whitelist-register-routine (articles &optional unregister)
1716   (spam-filelist-register-routine articles nil unregister))
1717
1718 \f
1719 ;;;; Spam-report glue
1720 (defun spam-report-gmane-register-routine (articles)
1721   (when articles
1722     (apply 'spam-report-gmane articles)))
1723
1724 \f
1725 ;;;; Bogofilter
1726 (defun spam-check-bogofilter-headers (&optional score)
1727   (let ((header (nnmail-fetch-field spam-bogofilter-header))
1728         (spam-split-group (if spam-split-symbolic-return
1729                               'spam
1730                             spam-split-group)))
1731     (when header                        ; return nil when no header
1732       (if score                         ; scoring mode
1733           (if (string-match "spamicity=\\([0-9.]+\\)" header)
1734               (match-string 1 header)
1735             "0")
1736         ;; spam detection mode
1737         (when (string-match spam-bogofilter-bogosity-positive-spam-header
1738                             header)
1739           spam-split-group)))))
1740
1741 ;; return something sensible if the score can't be determined
1742 (defun spam-bogofilter-score ()
1743   "Get the Bogofilter spamicity score"
1744   (interactive)
1745   (save-window-excursion
1746     (gnus-summary-show-article t)
1747     (set-buffer gnus-article-buffer)
1748     (let ((score (or (spam-check-bogofilter-headers t)
1749                      (spam-check-bogofilter t))))
1750       (message "Spamicity score %s" score)
1751       (or score "0"))
1752     (gnus-summary-show-article)))
1753
1754 (defun spam-check-bogofilter (&optional score)
1755   "Check the Bogofilter backend for the classification of this message"
1756   (let ((article-buffer-name (buffer-name))
1757         (db spam-bogofilter-database-directory)
1758         return)
1759     (with-temp-buffer
1760       (let ((temp-buffer-name (buffer-name)))
1761         (save-excursion
1762           (set-buffer article-buffer-name)
1763           (apply 'call-process-region
1764                  (point-min) (point-max)
1765                  spam-bogofilter-path
1766                  nil temp-buffer-name nil
1767                  (if db `("-d" ,db "-v") `("-v"))))
1768         (setq return (spam-check-bogofilter-headers score))))
1769     return))
1770
1771 (defun spam-bogofilter-register-with-bogofilter (articles
1772                                                  spam
1773                                                  &optional unregister)
1774   "Register an article, given as a string, as spam or non-spam."
1775   (dolist (article articles)
1776     (let ((article-string (spam-get-article-as-string article))
1777           (db spam-bogofilter-database-directory)
1778           (switch (if unregister
1779                       (if spam
1780                           spam-bogofilter-spam-strong-switch
1781                         spam-bogofilter-ham-strong-switch)
1782                     (if spam
1783                         spam-bogofilter-spam-switch
1784                       spam-bogofilter-ham-switch))))
1785       (when (stringp article-string)
1786         (with-temp-buffer
1787           (insert article-string)
1788
1789           (apply 'call-process-region
1790                  (point-min) (point-max)
1791                  spam-bogofilter-path
1792                  nil nil nil switch
1793                  (if db `("-d" ,db "-v") `("-v"))))))))
1794
1795 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1796   (spam-bogofilter-register-with-bogofilter articles t unregister))
1797
1798 (defun spam-bogofilter-unregister-spam-routine (articles)
1799   (spam-bogofilter-register-spam-routine articles t))
1800
1801 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1802   (spam-bogofilter-register-with-bogofilter articles nil unregister))
1803
1804 (defun spam-bogofilter-unregister-ham-routine (articles)
1805   (spam-bogofilter-register-ham-routine articles t))
1806
1807
1808 \f
1809 ;;;; spamoracle
1810 (defun spam-check-spamoracle ()
1811   "Run spamoracle on an article to determine whether it's spam."
1812   (let ((article-buffer-name (buffer-name))
1813         (spam-split-group (if spam-split-symbolic-return
1814                               'spam
1815                             spam-split-group)))
1816     (with-temp-buffer
1817       (let ((temp-buffer-name (buffer-name)))
1818         (save-excursion
1819           (set-buffer article-buffer-name)
1820           (let ((status
1821                  (apply 'call-process-region
1822                         (point-min) (point-max)
1823                         spam-spamoracle-binary
1824                         nil temp-buffer-name nil
1825                         (if spam-spamoracle-database
1826                             `("-f" ,spam-spamoracle-database "mark")
1827                           '("mark")))))
1828             (if (eq 0 status)
1829                 (progn
1830                   (set-buffer temp-buffer-name)
1831                   (goto-char (point-min))
1832                   (when (re-search-forward "^X-Spam: yes;" nil t)
1833                     spam-split-group))
1834               (error "Error running spamoracle" status))))))))
1835
1836 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1837   "Run spamoracle in training mode."
1838   (with-temp-buffer
1839     (let ((temp-buffer-name (buffer-name)))
1840       (save-excursion
1841         (goto-char (point-min))
1842         (dolist (article articles)
1843           (insert (spam-get-article-as-string article)))
1844         (let* ((arg (if (spam-xor unregister article-is-spam-p)
1845                         "-spam"
1846                       "-good"))
1847                (status
1848                 (apply 'call-process-region
1849                        (point-min) (point-max)
1850                        spam-spamoracle-binary
1851                        nil temp-buffer-name nil
1852                        (if spam-spamoracle-database
1853                            `("-f" ,spam-spamoracle-database
1854                              "add" ,arg)
1855                          `("add" ,arg)))))
1856           (when (not (eq 0 status))
1857             (error "Error running spamoracle" status)))))))
1858
1859 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1860   (spam-spamoracle-learn articles nil unregister))
1861
1862 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1863   (spam-spamoracle-learn-ham articles t))
1864
1865 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1866   (spam-spamoracle-learn articles t unregister))
1867
1868 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1869   (spam-spamoracle-learn-spam articles t))
1870
1871 \f
1872 ;;;; Hooks
1873
1874 ;;;###autoload
1875 (defun spam-initialize ()
1876   "Install the spam.el hooks and do other initialization"
1877   (interactive)
1878   (setq spam-install-hooks t)
1879   ;; TODO: How do we redo this every time spam-face is customized?
1880   (push '((eq mark gnus-spam-mark) . spam-face)
1881         gnus-summary-highlight)
1882   ;; Add hooks for loading and saving the spam stats
1883   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1884   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1885   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1886   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1887   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1888   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1889   (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1890
1891 (defun spam-unload-hook ()
1892   "Uninstall the spam.el hooks"
1893   (interactive)
1894   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1895   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1896   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1897   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1898   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1899   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1900   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1901
1902 (when spam-install-hooks
1903   (spam-initialize))
1904
1905 (provide 'spam)
1906
1907 ;;; spam.el ends here.