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