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