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