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