Fix comment typo.
[gnus] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Maintainer: Ted Zlatanov <tzz@lifelogs.com>
8 ;; Keywords: network, spam, mail, bogofilter, BBDB, dspam, dig, whitelist, blacklist, gmane, hashcash, spamassassin, bsfilter, ifile, stat, crm114, spamoracle
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; This module addresses a few aspects of spam control under Gnus.  Page
28 ;;; breaks are used for grouping declarations and documentation relating to
29 ;;; each particular aspect.
30
31 ;;; The integration with Gnus is not yet complete.  See various `FIXME'
32 ;;; comments, below, for supplementary explanations or discussions.
33
34 ;;; Several TODO items are marked as such
35
36 ;; TODO: cross-server splitting, remote processing, training through files
37
38 ;;; Code:
39
40 ;;{{{ compilation directives and autoloads/requires
41
42 ;; For Emacs < 22.2.
43 (eval-and-compile
44   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
45
46 (eval-when-compile (require 'cl))
47
48 (require 'message)              ;for the message-fetch-field functions
49 (require 'gnus-sum)
50 (require 'gnus-uu)                      ; because of key prefix issues
51 ;;; for the definitions of group content classification and spam processors
52 (require 'gnus)
53
54 (eval-when-compile (require 'spam-report))
55 (eval-when-compile (require 'hashcash))
56
57 ;; for nnimap-split-download-body-default
58 (eval-when-compile (require 'nnimap))
59
60 ;; autoload query-dig
61 (autoload 'query-dig "dig")
62
63 ;; autoload spam-report
64 (eval-and-compile
65   (autoload 'spam-report-gmane "spam-report")
66   (autoload 'spam-report-gmane-spam "spam-report")
67   (autoload 'spam-report-gmane-ham "spam-report")
68   (autoload 'spam-report-resend "spam-report"))
69
70 ;; autoload gnus-registry
71 (autoload 'gnus-registry-group-count "gnus-registry")
72 (autoload 'gnus-registry-add-group "gnus-registry")
73 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
74 (autoload 'gnus-registry-fetch-extra "gnus-registry")
75
76 ;; autoload dns-query
77 (autoload 'dns-query "dns")
78
79 ;;}}}
80
81 ;;{{{ Main parameters.
82 (defvar spam-backends nil
83   "List of spam.el backends with all the pertinent data.
84 Populated by `spam-install-backend-super'.")
85
86 (defgroup spam nil
87   "Spam configuration."
88   :version "22.1"
89   :group 'mail
90   :group 'news)
91
92 (defcustom spam-summary-exit-behavior 'default
93   "Exit behavior at the time of summary exit.
94 Note that setting the `spam-use-move' or `spam-use-copy' backends on
95 a group through group/topic parameters overrides this mechanism."
96   :type '(choice (const 'default :tag
97                         "Move spam out of all groups.  Move ham out of spam groups.")
98                  (const 'move-all :tag
99                         "Move spam out of all groups.  Move ham out of all groups.")
100                  (const 'move-none :tag
101                         "Never move spam or ham out of any groups."))
102   :group 'spam)
103
104 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
105   "Directory for spam whitelists and blacklists."
106   :type 'directory
107   :group 'spam)
108
109 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
110   "Whether new messages in a spam group should get the spam-mark."
111   :type 'boolean
112   ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
113   :group 'spam)
114
115 (defcustom spam-log-to-registry nil
116   "Whether spam/ham processing should be logged in the registry."
117   :type 'boolean
118   :group 'spam)
119
120 (defcustom spam-split-symbolic-return nil
121   "Whether `spam-split' should work with symbols or group names."
122   :type 'boolean
123   :group 'spam)
124
125 (defcustom spam-split-symbolic-return-positive nil
126   "Whether `spam-split' should ALWAYS work with symbols or group names.
127 Do not set this if you use `spam-split' in a fancy split method."
128   :type 'boolean
129   :group 'spam)
130
131 (defcustom spam-mark-only-unseen-as-spam t
132   "Whether only unseen articles should be marked as spam in spam groups.
133 When nil, all unread articles in a spam group are marked as
134 spam.  Set this if you want to leave an article unread in a spam group
135 without losing it to the automatic spam-marking process."
136   :type 'boolean
137   :group 'spam)
138
139 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
140   "Whether ham should be marked unread before it's moved.
141 The article is moved out of a spam group according to `ham-process-destination'.
142 This variable is an official entry in the international Longest Variable Name
143 Competition."
144   :type 'boolean
145   :group 'spam)
146
147 (defcustom spam-disable-spam-split-during-ham-respool nil
148   "Whether `spam-split' should be ignored while resplitting ham.
149 This is useful to prevent ham from ending up in the same spam
150 group after the resplit.  Don't set this to t if you have `spam-split' as the
151 last rule in your split configuration."
152   :type 'boolean
153   :group 'spam)
154
155 (defcustom spam-autodetect-recheck-messages nil
156   "Should spam.el recheck all meessages when autodetecting?
157 Normally this is nil, so only unseen messages will be checked."
158   :type 'boolean
159   :group 'spam)
160
161 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
162   "The location of the whitelist.
163 The file format is one regular expression per line.
164 The regular expression is matched against the address."
165   :type 'file
166   :group 'spam)
167
168 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
169   "The location of the blacklist.
170 The file format is one regular expression per line.
171 The regular expression is matched against the address."
172   :type 'file
173   :group 'spam)
174
175 (defcustom spam-use-dig t
176   "Whether `query-dig' should be used instead of `dns-query'."
177   :type 'boolean
178   :group 'spam)
179
180 (defcustom spam-use-gmane-xref nil
181   "Whether the Gmane spam xref should be used by `spam-split'."
182   :type 'boolean
183   :group 'spam)
184
185 (defcustom spam-use-blacklist nil
186   "Whether the blacklist should be used by `spam-split'."
187   :type 'boolean
188   :group 'spam)
189
190 (defcustom spam-blacklist-ignored-regexes nil
191   "Regular expressions that the blacklist should ignore."
192   :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
193   :group 'spam)
194
195 (defcustom spam-use-whitelist nil
196   "Whether the whitelist should be used by `spam-split'."
197   :type 'boolean
198   :group 'spam)
199
200 (defcustom spam-use-whitelist-exclusive nil
201   "Whether whitelist-exclusive should be used by `spam-split'.
202 Exclusive whitelisting means that all messages from senders not in the whitelist
203 are considered spam."
204   :type 'boolean
205   :group 'spam)
206
207 (defcustom spam-use-blackholes nil
208   "Whether blackholes should be used by `spam-split'."
209   :type 'boolean
210   :group 'spam)
211
212 (defcustom spam-use-hashcash nil
213   "Whether hashcash payments should be detected by `spam-split'."
214   :type 'boolean
215   :group 'spam)
216
217 (defcustom spam-use-regex-headers nil
218   "Whether a header regular expression match should be used by `spam-split'.
219 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
220   :type 'boolean
221   :group 'spam)
222
223 (defcustom spam-use-regex-body nil
224   "Whether a body regular expression match should be used by `spam-split'.
225 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
226   :type 'boolean
227   :group 'spam)
228
229 (defcustom spam-use-bogofilter-headers nil
230   "Whether bogofilter headers should be used by `spam-split'.
231 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
232   :type 'boolean
233   :group 'spam)
234
235 (defcustom spam-use-bogofilter nil
236   "Whether bogofilter should be invoked by `spam-split'.
237 Enable this if you want Gnus to invoke Bogofilter on new messages."
238   :type 'boolean
239   :group 'spam)
240
241 (defcustom spam-use-bsfilter-headers nil
242   "Whether bsfilter headers should be used by `spam-split'.
243 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
244   :type 'boolean
245   :group 'spam)
246
247 (defcustom spam-use-bsfilter nil
248   "Whether bsfilter should be invoked by `spam-split'.
249 Enable this if you want Gnus to invoke Bsfilter on new messages."
250   :type 'boolean
251   :group 'spam)
252
253 (defcustom spam-use-BBDB nil
254   "Whether BBDB should be used by `spam-split'."
255   :type 'boolean
256   :group 'spam)
257
258 (defcustom spam-use-BBDB-exclusive nil
259   "Whether BBDB-exclusive should be used by `spam-split'.
260 Exclusive BBDB means that all messages from senders not in the BBDB are
261 considered spam."
262   :type 'boolean
263   :group 'spam)
264
265 (defcustom spam-use-ifile nil
266   "Whether ifile should be used by `spam-split'."
267   :type 'boolean
268   :group 'spam)
269
270 (defcustom spam-use-stat nil
271   "Whether `spam-stat' should be used by `spam-split'."
272   :type 'boolean
273   :group 'spam)
274
275 (defcustom spam-use-spamoracle nil
276   "Whether spamoracle should be used by `spam-split'."
277   :type 'boolean
278   :group 'spam)
279
280 (defcustom spam-use-spamassassin nil
281   "Whether spamassassin should be invoked by `spam-split'.
282 Enable this if you want Gnus to invoke SpamAssassin on new messages."
283   :type 'boolean
284   :group 'spam)
285
286 (defcustom spam-use-spamassassin-headers nil
287   "Whether spamassassin headers should be checked by `spam-split'.
288 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
289 them."
290   :type 'boolean
291   :group 'spam)
292
293 (defcustom spam-use-crm114 nil
294   "Whether the CRM114 Mailfilter should be used by `spam-split'."
295   :type 'boolean
296   :group 'spam)
297
298 (defcustom spam-install-hooks (or
299                                spam-use-dig
300                                spam-use-gmane-xref
301                                spam-use-blacklist
302                                spam-use-whitelist
303                                spam-use-whitelist-exclusive
304                                spam-use-blackholes
305                                spam-use-hashcash
306                                spam-use-regex-headers
307                                spam-use-regex-body
308                                spam-use-bogofilter
309                                spam-use-bogofilter-headers
310                                spam-use-spamassassin
311                                spam-use-spamassassin-headers
312                                spam-use-bsfilter
313                                spam-use-bsfilter-headers
314                                spam-use-BBDB
315                                spam-use-BBDB-exclusive
316                                spam-use-ifile
317                                spam-use-stat
318                                spam-use-spamoracle
319                                spam-use-crm114)
320   "Whether the spam hooks should be installed.
321 Default to t if one of the spam-use-* variables is set."
322   :group 'spam
323   :type 'boolean)
324
325 (defcustom spam-split-group "spam"
326   "Group name where incoming spam should be put by `spam-split'."
327   :type 'string
328   :group 'spam)
329
330 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
331 ;;; not regular expressions
332 (defcustom spam-junk-mailgroups (cons
333                                  spam-split-group
334                                  '("mail.junk" "poste.pourriel"))
335   "Mailgroups with spam contents.
336 All unmarked article in such group receive the spam mark on group entry."
337   :type '(repeat (string :tag "Group"))
338   :group 'spam)
339
340
341 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
342   "The group where spam xrefs can be found on Gmane.
343 Only meaningful if you enable `spam-use-gmane-xref'."
344   :type 'string
345   :group 'spam)
346
347 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
348                                     "dev.null.dk" "relays.visi.com")
349   "List of blackhole servers.
350 Only meaningful if you enable `spam-use-blackholes'."
351   :type '(repeat (string :tag "Server"))
352   :group 'spam)
353
354 (defcustom spam-blackhole-good-server-regex nil
355   "String matching IP addresses that should not be checked in the blackholes.
356 Only meaningful if you enable `spam-use-blackholes'."
357   :type '(radio (const nil) regexp)
358   :group 'spam)
359
360 (defface spam
361   '((((class color) (type tty) (background dark))
362      (:foreground "gray80" :background "gray50"))
363     (((class color) (type tty) (background light))
364      (:foreground "gray50" :background "gray80"))
365     (((class color) (background dark))
366      (:foreground "ivory2"))
367     (((class color) (background light))
368      (:foreground "ivory4"))
369     (t :inverse-video t))
370   "Face for spam-marked articles."
371   :group 'spam)
372 ;; backward-compatibility alias
373 (put 'spam-face 'face-alias 'spam)
374 (put 'spam-face 'obsolete-face "22.1")
375
376 (defcustom spam-face 'spam
377   "Face for spam-marked articles."
378   :type 'face
379   :group 'spam)
380
381 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
382   "Regular expression for positive header spam matches.
383 Only meaningful if you enable `spam-use-regex-headers'."
384   :type '(repeat (regexp :tag "Regular expression to match spam header"))
385   :group 'spam)
386
387 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
388   "Regular expression for positive header ham matches.
389 Only meaningful if you enable `spam-use-regex-headers'."
390   :type '(repeat (regexp :tag "Regular expression to match ham header"))
391   :group 'spam)
392
393 (defcustom spam-regex-body-spam '()
394   "Regular expression for positive body spam matches.
395 Only meaningful if you enable `spam-use-regex-body'."
396   :type '(repeat (regexp :tag "Regular expression to match spam body"))
397   :group 'spam)
398
399 (defcustom spam-regex-body-ham '()
400   "Regular expression for positive body ham matches.
401 Only meaningful if you enable `spam-use-regex-body'."
402   :type '(repeat (regexp :tag "Regular expression to match ham body"))
403   :group 'spam)
404
405 (defcustom spam-summary-score-preferred-header nil
406   "Preferred header to use for `spam-summary-score'."
407   :type '(choice :tag "Header name"
408           (symbol :tag "SpamAssassin etc" X-Spam-Status)
409           (symbol :tag "Bogofilter"       X-Bogosity)
410           (const  :tag "No preference, take best guess." nil))
411   :group 'spam)
412
413 (defgroup spam-ifile nil
414   "Spam ifile configuration."
415   :group 'spam)
416
417 (make-obsolete-variable 'spam-ifile-path 'spam-ifile-program
418                         "Gnus 5.10.9 (Emacs 22.1)")
419 (defcustom spam-ifile-program (executable-find "ifile")
420   "Name of the ifile program."
421   :type '(choice (file :tag "Location of ifile")
422                  (const :tag "ifile is not installed"))
423   :group 'spam-ifile)
424
425 (make-obsolete-variable 'spam-ifile-database-path 'spam-ifile-database
426                         "Gnus 5.10.9 (Emacs 22.1)")
427 (defcustom spam-ifile-database nil
428   "File name of the ifile database."
429   :type '(choice (file :tag "Location of the ifile database")
430                  (const :tag "Use the default"))
431   :group 'spam-ifile)
432
433 (defcustom spam-ifile-spam-category "spam"
434   "Name of the spam ifile category."
435   :type 'string
436   :group 'spam-ifile)
437
438 (defcustom spam-ifile-ham-category nil
439   "Name of the ham ifile category.
440 If nil, the current group name will be used."
441   :type '(choice (string :tag "Use a fixed category")
442                  (const :tag "Use the current group name"))
443   :group 'spam-ifile)
444
445 (defcustom spam-ifile-all-categories nil
446   "Whether the ifile check will return all categories, or just spam.
447 Set this to t if you want to use the `spam-split' invocation of ifile as
448 your main source of newsgroup names."
449   :type 'boolean
450   :group 'spam-ifile)
451
452 (defgroup spam-bogofilter nil
453   "Spam bogofilter configuration."
454   :group 'spam)
455
456 (make-obsolete-variable 'spam-bogofilter-path 'spam-bogofilter-program
457                         "Gnus 5.10.9 (Emacs 22.1)")
458 (defcustom spam-bogofilter-program (executable-find "bogofilter")
459   "Name of the Bogofilter program."
460   :type '(choice (file :tag "Location of bogofilter")
461                  (const :tag "Bogofilter is not installed"))
462   :group 'spam-bogofilter)
463
464 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
465
466 (defcustom spam-bogofilter-header "X-Bogosity"
467   "The header that Bogofilter inserts in messages."
468   :type 'string
469   :group 'spam-bogofilter)
470
471 (defcustom spam-bogofilter-spam-switch "-s"
472   "The switch that Bogofilter uses to register spam messages."
473   :type 'string
474   :group 'spam-bogofilter)
475
476 (defcustom spam-bogofilter-ham-switch "-n"
477   "The switch that Bogofilter uses to register ham messages."
478   :type 'string
479   :group 'spam-bogofilter)
480
481 (defcustom spam-bogofilter-spam-strong-switch "-S"
482   "The switch that Bogofilter uses to unregister ham messages."
483   :type 'string
484   :group 'spam-bogofilter)
485
486 (defcustom spam-bogofilter-ham-strong-switch "-N"
487   "The switch that Bogofilter uses to unregister spam messages."
488   :type 'string
489   :group 'spam-bogofilter)
490
491 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
492   "The regex on `spam-bogofilter-header' for positive spam identification."
493   :type 'regexp
494   :group 'spam-bogofilter)
495
496 (defcustom spam-bogofilter-database-directory nil
497   "Location of the Bogofilter database.
498 When nil, use the default location."
499   :type '(choice (directory
500                   :tag "Location of the Bogofilter database directory")
501                  (const :tag "Use the default"))
502   :group 'spam-bogofilter)
503
504 (defgroup spam-bsfilter nil
505   "Spam bsfilter configuration."
506   :group 'spam)
507
508 (make-obsolete-variable 'spam-bsfilter-path 'spam-bsfilter-program
509                         "Gnus 5.10.9 (Emacs 22.1)")
510 (defcustom spam-bsfilter-program (executable-find "bsfilter")
511   "Name of the Bsfilter program."
512   :type '(choice (file :tag "Location of bsfilter")
513                  (const :tag "Bsfilter is not installed"))
514   :group 'spam-bsfilter)
515
516 (defcustom spam-bsfilter-header "X-Spam-Flag"
517   "The header inserted by Bsfilter to flag spam."
518   :type 'string
519   :group 'spam-bsfilter)
520
521 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
522   "The header that Bsfilter inserts in messages."
523   :type 'string
524   :group 'spam-bsfilter)
525
526 (defcustom spam-bsfilter-spam-switch "--add-spam"
527   "The switch that Bsfilter uses to register spam messages."
528   :type 'string
529   :group 'spam-bsfilter)
530
531 (defcustom spam-bsfilter-ham-switch "--add-clean"
532   "The switch that Bsfilter uses to register ham messages."
533   :type 'string
534   :group 'spam-bsfilter)
535
536 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
537   "The switch that Bsfilter uses to unregister ham messages."
538   :type 'string
539   :group 'spam-bsfilter)
540
541 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
542   "The switch that Bsfilter uses to unregister spam messages."
543   :type 'string
544   :group 'spam-bsfilter)
545
546 (defcustom spam-bsfilter-database-directory nil
547   "Directory path of the Bsfilter databases."
548   :type '(choice (directory
549                   :tag "Location of the Bsfilter database directory")
550                  (const :tag "Use the default"))
551   :group 'spam-bsfilter)
552
553 (defgroup spam-spamoracle nil
554   "Spam spamoracle configuration."
555   :group 'spam)
556
557 (defcustom spam-spamoracle-database nil
558   "Location of spamoracle database file.
559 When nil, use the default spamoracle database."
560   :type '(choice (directory :tag "Location of spamoracle database file.")
561                  (const :tag "Use the default"))
562   :group 'spam-spamoracle)
563
564 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
565   "Location of the spamoracle binary."
566   :type '(choice (directory :tag "Location of the spamoracle binary")
567                  (const :tag "Use the default"))
568   :group 'spam-spamoracle)
569
570 (defgroup spam-spamassassin nil
571   "Spam SpamAssassin configuration."
572   :group 'spam)
573
574 (make-obsolete-variable 'spam-spamassassin-path
575   'spam-spamassassin-program "Gnus 5.10.9 (Emacs 22.1)")
576 (defcustom spam-assassin-program (executable-find "spamassassin")
577   "Name of the spamassassin program.
578 Hint: set this to \"spamc\" if you have spamd running.  See the spamc and
579 spamd man pages for more information on these programs."
580   :type '(choice (file :tag "Location of spamc")
581                  (const :tag "spamassassin is not installed"))
582   :group 'spam-spamassassin)
583
584 (defcustom spam-spamassassin-arguments ()
585   "Arguments to pass to the spamassassin executable.
586 This must be a list.  For example, `(\"-C\" \"configfile\")'."
587   :type '(restricted-sexp :match-alternatives (listp))
588   :group 'spam-spamassassin)
589
590 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
591   "The header inserted by SpamAssassin to flag spam."
592   :type 'string
593   :group 'spam-spamassassin)
594
595 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
596   "The regex on `spam-spamassassin-spam-flag-header' for positive spam
597 identification"
598   :type 'string
599   :group 'spam-spamassassin)
600
601 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
602   "The header inserted by SpamAssassin, giving extended scoring information"
603   :type 'string
604   :group 'spam-spamassassin)
605
606 (make-obsolete-variable 'spam-sa-learn-path 'spam-sa-learn-program
607                         "Gnus 5.10.9 (Emacs 22.1)")
608 (defcustom spam-sa-learn-program (executable-find "sa-learn")
609   "Name of the sa-learn program."
610   :type '(choice (file :tag "Location of spamassassin")
611                  (const :tag "spamassassin is not installed"))
612   :group 'spam-spamassassin)
613
614 (defcustom spam-sa-learn-rebuild t
615   "Whether sa-learn should rebuild the database every time it is called
616 Enable this if you want sa-learn to rebuild the database automatically.  Doing
617 this will slightly increase the running time of the spam registration process.
618 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
619 order for SpamAssassin to recognize the new registered spam."
620   :type 'boolean
621   :group 'spam-spamassassin)
622
623 (defcustom spam-sa-learn-spam-switch "--spam"
624   "The switch that sa-learn uses to register spam messages."
625   :type 'string
626   :group 'spam-spamassassin)
627
628 (defcustom spam-sa-learn-ham-switch "--ham"
629   "The switch that sa-learn uses to register ham messages."
630   :type 'string
631   :group 'spam-spamassassin)
632
633 (defcustom spam-sa-learn-unregister-switch "--forget"
634   "The switch that sa-learn uses to unregister messages messages."
635   :type 'string
636   :group 'spam-spamassassin)
637
638 (defgroup spam-crm114 nil
639   "Spam CRM114 Mailfilter configuration."
640   :group 'spam)
641
642 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
643   "File path of the CRM114 Mailfilter executable program."
644   :type '(choice (file :tag "Location of CRM114 Mailfilter")
645          (const :tag "CRM114 Mailfilter is not installed"))
646   :group 'spam-crm114)
647
648 (defcustom spam-crm114-header "X-CRM114-Status"
649   "The header that CRM114 Mailfilter inserts in messages."
650   :type 'string
651   :group 'spam-crm114)
652
653 (defcustom spam-crm114-spam-switch "--learnspam"
654   "The switch that CRM114 Mailfilter uses to register spam messages."
655   :type 'string
656   :group 'spam-crm114)
657
658 (defcustom spam-crm114-ham-switch "--learnnonspam"
659   "The switch that CRM114 Mailfilter uses to register ham messages."
660   :type 'string
661   :group 'spam-crm114)
662
663 (defcustom spam-crm114-spam-strong-switch "--unlearn"
664   "The switch that CRM114 Mailfilter uses to unregister ham messages."
665   :type 'string
666   :group 'spam-crm114)
667
668 (defcustom spam-crm114-ham-strong-switch "--unlearn"
669   "The switch that CRM114 Mailfilter uses to unregister spam messages."
670   :type 'string
671   :group 'spam-crm114)
672
673 (defcustom spam-crm114-positive-spam-header "^SPAM"
674   "The regex on `spam-crm114-header' for positive spam identification."
675   :type 'regexp
676   :group 'spam-crm114)
677
678 (defcustom spam-crm114-database-directory nil
679   "Directory path of the CRM114 Mailfilter databases."
680   :type '(choice (directory
681           :tag "Location of the CRM114 Mailfilter database directory")
682          (const :tag "Use the default"))
683   :group 'spam-crm114)
684
685 ;;; Key bindings for spam control.
686
687 (gnus-define-keys gnus-summary-mode-map
688   "St" spam-generic-score
689   "Sx" gnus-summary-mark-as-spam
690   "Mst" spam-generic-score
691   "Msx" gnus-summary-mark-as-spam
692   "\M-d" gnus-summary-mark-as-spam)
693
694 (defvar spam-cache-lookups t
695   "Whether spam.el will try to cache lookups using `spam-caches'.")
696
697 (defvar spam-caches (make-hash-table
698                      :size 10
699                      :test 'equal)
700   "Cache of spam detection entries.")
701
702 (defvar spam-old-articles nil
703   "List of old ham and spam articles, generated when a group is entered.")
704
705 (defvar spam-split-disabled nil
706   "If non-nil, `spam-split' is disabled, and always returns nil.")
707
708 (defvar spam-split-last-successful-check nil
709   "Internal variable.
710 `spam-split' will set this to nil or a spam-use-XYZ check if it
711 finds ham or spam.")
712
713 ;; internal variables for backends
714 ;; TODO: find a way to create these on the fly in spam-install-backend-super
715 (defvar spam-use-copy nil)
716 (defvar spam-use-move nil)
717 (defvar spam-use-gmane nil)
718 (defvar spam-use-resend nil)
719
720 ;;}}}
721
722 ;;{{{ convenience functions
723
724 (defun spam-clear-cache (symbol)
725   "Clear the `spam-caches' entry for a check."
726   (remhash symbol spam-caches))
727
728 (defun spam-xor (a b)
729   "Logical A xor B."
730   (and (or a b) (not (and a b))))
731
732 (defun spam-set-difference (list1 list2)
733   "Return a set difference of LIST1 and LIST2.
734 When either list is nil, the other is returned."
735   (if (and list1 list2)
736       ;; we have two non-nil lists
737       (progn
738         (dolist (item (append list1 list2))
739           (when (and (memq item list1) (memq item list2))
740             (setq list1 (delq item list1))
741             (setq list2 (delq item list2))))
742         (append list1 list2))
743     ;; if either of the lists was nil, return the other one
744     (if list1 list1 list2)))
745
746 (defun spam-group-ham-mark-p (group mark &optional spam)
747   "Checks if MARK is considered a ham mark in GROUP."
748   (when (stringp group)
749     (let* ((marks (spam-group-ham-marks group spam))
750            (marks (if (symbolp mark)
751                       marks
752                     (mapcar 'symbol-value marks))))
753       (memq mark marks))))
754
755 (defun spam-group-spam-mark-p (group mark)
756   "Checks if MARK is considered a spam mark in GROUP."
757   (spam-group-ham-mark-p group mark t))
758
759 (defun spam-group-ham-marks (group &optional spam)
760   "In GROUP, get all the ham marks."
761   (when (stringp group)
762     (let* ((marks (if spam
763                       (gnus-parameter-spam-marks group)
764                     (gnus-parameter-ham-marks group)))
765            (marks (car marks))
766            (marks (if (listp (car marks)) (car marks) marks)))
767       marks)))
768
769 (defun spam-group-spam-marks (group)
770   "In GROUP, get all the spam marks."
771   (spam-group-ham-marks group t))
772
773 (defun spam-group-spam-contents-p (group)
774   "Is GROUP a spam group?"
775   (if (and (stringp group) (< 0 (length group)))
776       (or (member group spam-junk-mailgroups)
777           (memq 'gnus-group-spam-classification-spam
778                 (gnus-parameter-spam-contents group)))
779     nil))
780
781 (defun spam-group-ham-contents-p (group)
782   "Is GROUP a ham group?"
783   (if (stringp group)
784       (memq 'gnus-group-spam-classification-ham
785             (gnus-parameter-spam-contents group))
786     nil))
787
788 (defun spam-classifications ()
789   "Return list of valid classifications"
790   '(spam ham))
791
792 (defun spam-classification-valid-p (classification)
793   "Is CLASSIFICATION a valid spam/ham classification?"
794   (memq classification (spam-classifications)))
795
796 (defun spam-backend-properties ()
797   "Return list of valid classifications."
798   '(statistical mover check hrf srf huf suf))
799
800 (defun spam-backend-property-valid-p (property)
801   "Is PROPERTY a valid backend property?"
802   (memq property (spam-backend-properties)))
803
804 (defun spam-backend-function-type-valid-p (type)
805   (or (eq type 'registration)
806       (eq type 'unregistration)))
807
808 (defun spam-process-type-valid-p (process-type)
809   (or (eq process-type 'incoming)
810       (eq process-type 'process)))
811
812 (defun spam-list-articles (articles classification)
813   (let ((mark-check (if (eq classification 'spam)
814                         'spam-group-spam-mark-p
815                       'spam-group-ham-mark-p))
816         alist mark-cache-yes mark-cache-no)
817     (dolist (article articles)
818       (let ((mark (gnus-summary-article-mark article)))
819         (unless (or (memq mark mark-cache-yes)
820                     (memq mark mark-cache-no))
821           (if (funcall mark-check
822                        gnus-newsgroup-name
823                        mark)
824               (push mark mark-cache-yes)
825             (push mark mark-cache-no)))
826         (when (memq mark mark-cache-yes)
827           (push article alist))))
828     alist))
829
830 ;;}}}
831
832 ;;{{{ backend installation functions and procedures
833
834 (defun spam-install-backend-super (backend &rest properties)
835   "Install BACKEND for spam.el.
836 Accepts incoming CHECK, ham registration function HRF, spam
837 registration function SRF, ham unregistration function HUF, spam
838 unregistration function SUF, and an indication whether the
839 backend is STATISTICAL."
840   (setq spam-backends (add-to-list 'spam-backends backend))
841   (while properties
842     (let ((property (pop properties))
843           (value (pop properties)))
844       (if (spam-backend-property-valid-p property)
845           (put backend property value)
846         (gnus-error
847          5
848          "spam-install-backend-super got an invalid property %s"
849          property)))))
850
851 (defun spam-backend-list (&optional type)
852   "Return a list of all the backend symbols, constrained by TYPE.
853 When TYPE is 'non-mover, only non-mover backends are returned.
854 When TYPE is 'mover, only mover backends are returned."
855   (let (list)
856     (dolist (backend spam-backends)
857       (when (or
858              (null type)                ;either no type was requested
859              ;; or the type is 'mover and the backend is a mover
860              (and
861               (eq type 'mover)
862               (spam-backend-mover-p backend))
863              ;; or the type is 'non-mover and the backend is not a mover
864              (and
865               (eq type 'non-mover)
866               (not (spam-backend-mover-p backend))))
867         (push backend list)))
868       list))
869
870 (defun spam-backend-check (backend)
871   "Get the check function for BACKEND.
872 Each individual check may return nil, t, or a mailgroup name.
873 The value nil means that the check does not yield a decision, and
874 so, that further checks are needed.  The value t means that the
875 message is definitely not spam, and that further spam checks
876 should be inhibited.  Otherwise, a mailgroup name or the symbol
877 'spam (depending on `spam-split-symbolic-return') is returned where
878 the mail should go, and further checks are also inhibited.  The
879 usual mailgroup name is the value of `spam-split-group', meaning
880 that the message is definitely a spam."
881   (get backend 'check))
882
883 (defun spam-backend-valid-p (backend)
884   "Is BACKEND valid?"
885   (member backend (spam-backend-list)))
886
887 (defun spam-backend-info (backend)
888   "Return information about BACKEND."
889   (if (spam-backend-valid-p backend)
890       (let (info)
891         (setq info (format "Backend %s has the following properties:\n"
892                            backend))
893         (dolist (property (spam-backend-properties))
894           (setq info (format "%s%s=%s\n"
895                              info
896                              property
897                              (get backend property))))
898         info)
899     (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
900                 backend)))
901
902 (defun spam-backend-function (backend classification type)
903   "Get the BACKEND function for CLASSIFICATION and TYPE.
904 TYPE is 'registration or 'unregistration.
905 CLASSIFICATION is 'ham or 'spam."
906   (if (and
907        (spam-classification-valid-p classification)
908        (spam-backend-function-type-valid-p type))
909       (let ((retrieval
910              (intern
911               (format "spam-backend-%s-%s-function"
912                       classification
913                       type))))
914         (funcall retrieval backend))
915     (gnus-error
916      5
917      "%s was passed invalid backend %s, classification %s, or type %s"
918      "spam-backend-function"
919      backend
920      classification
921      type)))
922
923 (defun spam-backend-article-list-property (classification
924                                            &optional unregister)
925   "Property name of article list with CLASSIFICATION and UNREGISTER."
926   (let* ((r (if unregister "unregister" "register"))
927          (prop (format "%s-%s" classification r)))
928     prop))
929
930 (defun spam-backend-get-article-todo-list (backend
931                                            classification
932                                            &optional unregister)
933   "Get the articles to be processed for BACKEND and CLASSIFICATION.
934 With UNREGISTER, get articles to be unregistered.
935 This is a temporary storage function - nothing here persists."
936   (get
937    backend
938    (intern (spam-backend-article-list-property classification unregister))))
939
940 (defun spam-backend-put-article-todo-list (backend classification list &optional unregister)
941   "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
942 With UNREGISTER, set articles to be unregistered.
943 This is a temporary storage function - nothing here persists."
944   (put
945    backend
946    (intern (spam-backend-article-list-property classification unregister))
947    list))
948
949 (defun spam-backend-ham-registration-function (backend)
950   "Get the ham registration function for BACKEND."
951   (get backend 'hrf))
952
953 (defun spam-backend-spam-registration-function (backend)
954   "Get the spam registration function for BACKEND."
955   (get backend 'srf))
956
957 (defun spam-backend-ham-unregistration-function (backend)
958   "Get the ham unregistration function for BACKEND."
959   (get backend 'huf))
960
961 (defun spam-backend-spam-unregistration-function (backend)
962   "Get the spam unregistration function for BACKEND."
963   (get backend 'suf))
964
965 (defun spam-backend-statistical-p (backend)
966   "Is BACKEND statistical?"
967   (get backend 'statistical))
968
969 (defun spam-backend-mover-p (backend)
970   "Is BACKEND a mover?"
971   (get backend 'mover))
972
973 (defun spam-install-backend-alias (backend alias)
974   "Add ALIAS to an existing BACKEND.
975 The previous backend settings for ALIAS are erased."
976
977   ;; install alias with no properties at first
978   (spam-install-backend-super alias)
979
980   (dolist (property (spam-backend-properties))
981     (put alias property (get backend property))))
982
983 (defun spam-install-checkonly-backend (backend check)
984   "Install a BACKEND than can only CHECK for spam."
985   (spam-install-backend-super backend 'check check))
986
987 (defun spam-install-mover-backend (backend hrf srf huf suf)
988   "Install a BACKEND than can move articles at summary exit.
989 Accepts ham registration function HRF, spam registration function
990 SRF, ham unregistration function HUF, spam unregistration
991 function SUF.  The backend has no incoming check and can't be
992 statistical."
993   (spam-install-backend-super
994    backend
995    'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
996
997 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
998   "Install a BACKEND than has no check.
999 Accepts ham registration function HRF, spam registration function
1000 SRF, ham unregistration function HUF, spam unregistration
1001 function SUF.  The backend has no incoming check and can't be
1002 statistical (it could be, but in practice that doesn't happen)."
1003   (spam-install-backend-super
1004    backend
1005    'hrf hrf 'srf srf 'huf huf 'suf suf))
1006
1007 (defun spam-install-backend (backend check hrf srf huf suf)
1008   "Install a BACKEND.
1009 Accepts incoming CHECK, ham registration function HRF, spam
1010 registration function SRF, ham unregistration function HUF, spam
1011 unregistration function SUF.  The backend won't be
1012 statistical (use `spam-install-statistical-backend' for that)."
1013   (spam-install-backend-super
1014    backend
1015    'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
1016
1017 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
1018   "Install a BACKEND.
1019 Accepts incoming CHECK, ham registration function HRF, spam
1020 registration function SRF, ham unregistration function HUF, spam
1021 unregistration function SUF.  The backend will be
1022 statistical (use `spam-install-backend' for non-statistical
1023 backends)."
1024   (spam-install-backend-super
1025    backend
1026    'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1027
1028 (defun spam-install-statistical-checkonly-backend (backend check)
1029   "Install a statistical BACKEND than can only CHECK for spam."
1030   (spam-install-backend-super
1031    backend
1032    'check check 'statistical t))
1033
1034 ;;}}}
1035
1036 ;;{{{ backend installations
1037 (spam-install-checkonly-backend 'spam-use-blackholes
1038                                 'spam-check-blackholes)
1039
1040 (spam-install-checkonly-backend 'spam-use-hashcash
1041                                 'spam-check-hashcash)
1042
1043 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1044                                 'spam-check-spamassassin-headers)
1045
1046 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1047                                 'spam-check-bogofilter-headers)
1048
1049 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1050                                 'spam-check-bsfilter-headers)
1051
1052 (spam-install-checkonly-backend 'spam-use-gmane-xref
1053                                 'spam-check-gmane-xref)
1054
1055 (spam-install-checkonly-backend 'spam-use-regex-headers
1056                                 'spam-check-regex-headers)
1057
1058 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1059                                             'spam-check-regex-body)
1060
1061 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy) instead
1062 (spam-install-mover-backend 'spam-use-move
1063                             'spam-move-ham-routine
1064                             'spam-move-spam-routine
1065                             nil
1066                             nil)
1067
1068 (spam-install-nocheck-backend 'spam-use-copy
1069                               'spam-copy-ham-routine
1070                               'spam-copy-spam-routine
1071                               nil
1072                               nil)
1073
1074 (spam-install-nocheck-backend 'spam-use-gmane
1075                               'spam-report-gmane-unregister-routine
1076                               'spam-report-gmane-register-routine
1077                               'spam-report-gmane-register-routine
1078                               'spam-report-gmane-unregister-routine)
1079
1080 (spam-install-nocheck-backend 'spam-use-resend
1081                               'spam-report-resend-register-ham-routine
1082                               'spam-report-resend-register-routine
1083                               nil
1084                               nil)
1085
1086 (spam-install-backend 'spam-use-BBDB
1087                       'spam-check-BBDB
1088                       'spam-BBDB-register-routine
1089                       nil
1090                       'spam-BBDB-unregister-routine
1091                       nil)
1092
1093 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1094
1095 (spam-install-backend 'spam-use-blacklist
1096                       'spam-check-blacklist
1097                       nil
1098                       'spam-blacklist-register-routine
1099                       nil
1100                       'spam-blacklist-unregister-routine)
1101
1102 (spam-install-backend 'spam-use-whitelist
1103                       'spam-check-whitelist
1104                       'spam-whitelist-register-routine
1105                       nil
1106                       'spam-whitelist-unregister-routine
1107                       nil)
1108
1109 (spam-install-statistical-backend 'spam-use-ifile
1110                                   'spam-check-ifile
1111                                   'spam-ifile-register-ham-routine
1112                                   'spam-ifile-register-spam-routine
1113                                   'spam-ifile-unregister-ham-routine
1114                                   'spam-ifile-unregister-spam-routine)
1115
1116 (spam-install-statistical-backend 'spam-use-spamoracle
1117                                   'spam-check-spamoracle
1118                                   'spam-spamoracle-learn-ham
1119                                   'spam-spamoracle-learn-spam
1120                                   'spam-spamoracle-unlearn-ham
1121                                   'spam-spamoracle-unlearn-spam)
1122
1123 (spam-install-statistical-backend 'spam-use-stat
1124                                   'spam-check-stat
1125                                   'spam-stat-register-ham-routine
1126                                   'spam-stat-register-spam-routine
1127                                   'spam-stat-unregister-ham-routine
1128                                   'spam-stat-unregister-spam-routine)
1129
1130 (spam-install-statistical-backend 'spam-use-spamassassin
1131                                   'spam-check-spamassassin
1132                                   'spam-spamassassin-register-ham-routine
1133                                   'spam-spamassassin-register-spam-routine
1134                                   'spam-spamassassin-unregister-ham-routine
1135                                   'spam-spamassassin-unregister-spam-routine)
1136
1137 (spam-install-statistical-backend 'spam-use-bogofilter
1138                                   'spam-check-bogofilter
1139                                   'spam-bogofilter-register-ham-routine
1140                                   'spam-bogofilter-register-spam-routine
1141                                   'spam-bogofilter-unregister-ham-routine
1142                                   'spam-bogofilter-unregister-spam-routine)
1143
1144 (spam-install-statistical-backend 'spam-use-bsfilter
1145                                   'spam-check-bsfilter
1146                                   'spam-bsfilter-register-ham-routine
1147                                   'spam-bsfilter-register-spam-routine
1148                                   'spam-bsfilter-unregister-ham-routine
1149                                   'spam-bsfilter-unregister-spam-routine)
1150
1151 (spam-install-statistical-backend 'spam-use-crm114
1152                                   'spam-check-crm114
1153                                   'spam-crm114-register-ham-routine
1154                                   'spam-crm114-register-spam-routine
1155                                   'spam-crm114-unregister-ham-routine
1156                                   'spam-crm114-unregister-spam-routine)
1157 ;;}}}
1158
1159 ;;{{{ scoring and summary formatting
1160 (defun spam-necessary-extra-headers ()
1161   "Return the extra headers spam.el thinks are necessary."
1162   (let (list)
1163     (when (or spam-use-spamassassin
1164               spam-use-spamassassin-headers
1165               spam-use-regex-headers)
1166       (push 'X-Spam-Status list))
1167     (when (or spam-use-bogofilter
1168               spam-use-regex-headers)
1169       (push 'X-Bogosity list))
1170     (when (or spam-use-crm114
1171               spam-use-regex-headers)
1172       (push 'X-CRM114-Status list))
1173     list))
1174
1175 (defun spam-user-format-function-S (headers)
1176   (when headers
1177     (format "%3.2f"
1178             (spam-summary-score headers spam-summary-score-preferred-header))))
1179
1180 (defun spam-article-sort-by-spam-status (h1 h2)
1181   "Sort articles by score."
1182   (let (result)
1183     (dolist (header (spam-necessary-extra-headers))
1184       (let ((s1 (spam-summary-score h1 header))
1185             (s2 (spam-summary-score h2 header)))
1186       (unless (= s1 s2)
1187         (setq result (< s1 s2))
1188         (return))))
1189     result))
1190
1191 (defvar spam-spamassassin-score-regexp
1192   ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
1193   "Regexp matching SpamAssassin score header.
1194 The first group must match the number.")
1195
1196 (defun spam-extra-header-to-number (header headers)
1197   "Transform an extra HEADER to a number, using list of HEADERS.
1198 Note this has to be fast."
1199   (let ((header-content (gnus-extra-header header headers)))
1200     (if header-content
1201         (cond
1202          ((eq header 'X-Spam-Status)
1203           (string-to-number (gnus-replace-in-string
1204                              header-content
1205                              spam-spamassassin-score-regexp
1206                              "\\1")))
1207          ;; for CRM checking, it's probably faster to just do the string match
1208          ((string-match "( pR: \\([0-9.-]+\\)" header-content)
1209           (- (string-to-number (match-string 1 header-content))))
1210          ((eq header 'X-Bogosity)
1211           (string-to-number (gnus-replace-in-string
1212                              (gnus-replace-in-string
1213                               header-content
1214                               ".*spamicity=" "")
1215                              ",.*" "")))
1216          (t nil))
1217       nil)))
1218
1219 (defun spam-summary-score (headers &optional specific-header)
1220   "Score an article for the summary buffer, as fast as possible.
1221 With SPECIFIC-HEADER, returns only that header's score.
1222 Will not return a nil score."
1223   (let (score)
1224     (dolist (header
1225              (if specific-header
1226                  (list specific-header)
1227                (spam-necessary-extra-headers)))
1228       (setq score
1229             (spam-extra-header-to-number header headers))
1230       (when score
1231         (return)))
1232     (or score 0)))
1233
1234 (defun spam-generic-score (&optional recheck)
1235   "Invoke whatever scoring method we can."
1236   (interactive "P")
1237   (cond
1238    ((or spam-use-spamassassin spam-use-spamassassin-headers)
1239     (spam-spamassassin-score recheck))
1240    ((or spam-use-bsfilter spam-use-bsfilter-headers)
1241     (spam-bsfilter-score recheck))
1242    (spam-use-crm114
1243     (spam-crm114-score))
1244    (t (spam-bogofilter-score recheck))))
1245 ;;}}}
1246
1247 ;;{{{ set up widening, processor checks
1248
1249 ;;; set up IMAP widening if it's necessary
1250 (defun spam-setup-widening ()
1251   (when (spam-widening-needed-p)
1252     (setq nnimap-split-download-body-default t)))
1253
1254 (defun spam-widening-needed-p (&optional force-symbols)
1255   (let (found)
1256     (dolist (backend (spam-backend-list))
1257       (when (and (spam-backend-statistical-p backend)
1258                  (or (symbol-value backend)
1259                      (memq backend force-symbols)))
1260         (setq found backend)))
1261     found))
1262
1263 (defvar spam-list-of-processors
1264   ;; note the nil processors are not defined in gnus.el
1265   '((gnus-group-spam-exit-processor-bogofilter   spam spam-use-bogofilter)
1266     (gnus-group-spam-exit-processor-bsfilter     spam spam-use-bsfilter)
1267     (gnus-group-spam-exit-processor-blacklist    spam spam-use-blacklist)
1268     (gnus-group-spam-exit-processor-ifile        spam spam-use-ifile)
1269     (gnus-group-spam-exit-processor-stat         spam spam-use-stat)
1270     (gnus-group-spam-exit-processor-spamoracle   spam spam-use-spamoracle)
1271     (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1272     (gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane) ;; Buggy?
1273     (gnus-group-ham-exit-processor-ifile         ham spam-use-ifile)
1274     (gnus-group-ham-exit-processor-bogofilter    ham spam-use-bogofilter)
1275     (gnus-group-ham-exit-processor-bsfilter      ham spam-use-bsfilter)
1276     (gnus-group-ham-exit-processor-stat          ham spam-use-stat)
1277     (gnus-group-ham-exit-processor-whitelist     ham spam-use-whitelist)
1278     (gnus-group-ham-exit-processor-BBDB          ham spam-use-BBDB)
1279     (gnus-group-ham-exit-processor-copy          ham spam-use-ham-copy)
1280     (gnus-group-ham-exit-processor-spamassassin  ham spam-use-spamassassin)
1281     (gnus-group-ham-exit-processor-spamoracle    ham spam-use-spamoracle))
1282   "The OBSOLETE `spam-list-of-processors' list.
1283 This list contains pairs associating the obsolete ham/spam exit
1284 processor variables with a classification and a spam-use-*
1285 variable.  When the processor variable is nil, just the
1286 classification and spam-use-* check variable are used.  This is
1287 superseded by the new spam backend code, so it's only consulted
1288 for backwards compatibility.")
1289
1290 (defun spam-group-processor-p (group backend &optional classification)
1291   "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1292 Also accepts the obsolete processors, which can be found in
1293 gnus.el and in spam-list-of-processors.  In the case of mover
1294 backends, checks the setting of `spam-summary-exit-behavior' in
1295 addition to the set values for the group."
1296   (if (and (stringp group)
1297            (symbolp backend))
1298       (let ((old-style (assq backend spam-list-of-processors))
1299             (parameters (nth 0 (gnus-parameter-spam-process group)))
1300             found)
1301         (if old-style  ; old-style processor
1302             (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1303           ;; now search for the parameter
1304           (dolist (parameter parameters)
1305             (when (and (null found)
1306                        (listp parameter)
1307                        (eq classification (nth 0 parameter))
1308                        (eq backend (nth 1 parameter)))
1309               (setq found t)))
1310
1311           ;; now, if the parameter was not found, do the
1312           ;; spam-summary-exit-behavior-logic for mover backends
1313           (unless found
1314             (when (spam-backend-mover-p backend)
1315               (setq
1316                found
1317                (cond
1318                 ((eq spam-summary-exit-behavior 'move-all) t)
1319                 ((eq spam-summary-exit-behavior 'move-none) nil)
1320                 ((eq spam-summary-exit-behavior 'default)
1321                  (or (eq classification 'spam) ;move spam out of all groups
1322                      ;; move ham out of spam groups
1323                      (and (eq classification 'ham)
1324                           (spam-group-spam-contents-p group))))
1325                 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1326                                spam-summary-exit-behavior))))))
1327
1328           found))
1329     nil))
1330
1331 ;;}}}
1332
1333 ;;{{{ Summary entry and exit processing.
1334
1335 (defun spam-mark-junk-as-spam-routine ()
1336   ;; check the global list of group names spam-junk-mailgroups and the
1337   ;; group parameters
1338   (when (spam-group-spam-contents-p gnus-newsgroup-name)
1339     (gnus-message 6 "Marking %s articles as spam"
1340                   (if spam-mark-only-unseen-as-spam
1341                       "unseen"
1342                     "unread"))
1343     (let ((articles (if spam-mark-only-unseen-as-spam
1344                         gnus-newsgroup-unseen
1345                       gnus-newsgroup-unreads)))
1346       (if spam-mark-new-messages-in-spam-group-as-spam
1347           (dolist (article articles)
1348             (gnus-summary-mark-article article gnus-spam-mark))
1349         (gnus-message 9 "Did not mark new messages as spam.")))))
1350
1351 (defun spam-summary-prepare ()
1352   (setq spam-old-articles
1353         (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1354               (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1355   (spam-mark-junk-as-spam-routine))
1356
1357 ;; The spam processors are invoked for any group, spam or ham or neither
1358 (defun spam-summary-prepare-exit ()
1359   (unless gnus-group-is-exiting-without-update-p
1360     (gnus-message 6 "Exiting summary buffer and applying spam rules")
1361
1362     ;; before we begin, remove any article limits
1363 ;    (ignore-errors
1364 ;      (gnus-summary-pop-limit t))
1365
1366     ;; first of all, unregister any articles that are no longer ham or spam
1367     ;; we have to iterate over the processors, or else we'll be too slow
1368     (dolist (classification (spam-classifications))
1369       (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1370              (new-articles (spam-list-articles
1371                             gnus-newsgroup-articles
1372                             classification))
1373              (changed-articles (spam-set-difference new-articles old-articles)))
1374         ;; now that we have the changed articles, we go through the processors
1375         (dolist (backend (spam-backend-list))
1376           (let (unregister-list)
1377             (dolist (article changed-articles)
1378               (let ((id (spam-fetch-field-message-id-fast article)))
1379                 (when (spam-log-unregistration-needed-p
1380                        id 'process classification backend)
1381                   (push article unregister-list))))
1382             ;; call spam-register-routine with specific articles to unregister,
1383             ;; when there are articles to unregister and the check is enabled
1384             (when (and unregister-list (symbol-value backend))
1385               (spam-backend-put-article-todo-list backend
1386                                                   classification
1387                                                   unregister-list
1388                                                   t))))))
1389
1390     ;; do the non-moving backends first, then the moving ones
1391     (dolist (backend-type '(non-mover mover))
1392       (dolist (classification (spam-classifications))
1393         (dolist (backend (spam-backend-list backend-type))
1394           (when (spam-group-processor-p
1395                  gnus-newsgroup-name
1396                  backend
1397                  classification)
1398             (spam-backend-put-article-todo-list backend
1399                                                 classification
1400                                                 (spam-list-articles
1401                                                  gnus-newsgroup-articles
1402                                                  classification))))))
1403
1404     (spam-resolve-registrations-routine) ; do the registrations now
1405
1406     ;; we mark all the leftover spam articles as expired at the end
1407     (dolist (article (spam-list-articles
1408                       gnus-newsgroup-articles
1409                       'spam))
1410       (gnus-summary-mark-article article gnus-expirable-mark)))
1411
1412   (setq spam-old-articles nil))
1413
1414 ;;}}}
1415
1416 ;;{{{ spam-use-move and spam-use-copy backend support functions
1417
1418 (defun spam-copy-or-move-routine (copy groups articles classification)
1419
1420   (when (and (car-safe groups) (listp (car-safe groups)))
1421     (setq groups (pop groups)))
1422
1423   (unless (listp groups)
1424     (setq groups (list groups)))
1425
1426     ;; remove the current process mark
1427   (gnus-summary-kill-process-mark)
1428
1429   (let ((backend-supports-deletions
1430          (gnus-check-backend-function
1431           'request-move-article gnus-newsgroup-name))
1432         (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1433         article mark deletep respool)
1434
1435     (when (member 'respool groups)
1436       (setq respool t)                  ; boolean for later
1437       (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1438
1439     ;; now do the actual move
1440     (dolist (group groups)
1441       (when (and articles (stringp group))
1442
1443         ;; first, mark the article with the process mark and, if needed,
1444         ;; the unread or expired mark (for ham and spam respectively)
1445         (dolist (article articles)
1446           (when (and (eq classification 'ham)
1447                      spam-mark-ham-unread-before-move-from-spam-group)
1448             (gnus-message 9 "Marking ham article %d unread before move"
1449                           article)
1450             (gnus-summary-mark-article article gnus-unread-mark))
1451           (when (and (eq classification 'spam)
1452                      (not copy))
1453             (gnus-message 9 "Marking spam article %d expirable before move"
1454                           article)
1455             (gnus-summary-mark-article article gnus-expirable-mark))
1456           (gnus-summary-set-process-mark article)
1457
1458           (if respool              ; respooling is with a "fake" group
1459               (let ((spam-split-disabled
1460                      (or spam-split-disabled
1461                          (and (eq classification 'ham)
1462                               spam-disable-spam-split-during-ham-respool))))
1463                 (gnus-message 9 "Respooling article %d with method %s"
1464                               article respool-method)
1465                 (gnus-summary-respool-article nil respool-method))
1466             (if (or (not backend-supports-deletions) ; else, we are not respooling
1467                     (> (length groups) 1))
1468                 (progn              ; if copying, copy and set deletep
1469                   (gnus-message 9 "Copying article %d to group %s"
1470                                 article group)
1471                   (gnus-summary-copy-article nil group)
1472                   (setq deletep t))
1473               (gnus-message 9 "Moving article %d to group %s"
1474                             article group)
1475               (gnus-summary-move-article nil group))))) ; else move articles
1476
1477       ;; now delete the articles, unless a) copy is t, and there was a copy done
1478       ;;                                 b) a move was done to a single group
1479       ;;                                 c) backend-supports-deletions is nil
1480       (unless copy
1481         (when (and deletep backend-supports-deletions)
1482           (dolist (article articles)
1483               (gnus-summary-set-process-mark article)
1484               (gnus-message 9 "Deleting article %d" article))
1485           (when articles
1486             (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1487               (gnus-summary-delete-article nil)))))
1488
1489       (gnus-summary-yank-process-mark)
1490       (length articles))))
1491
1492 (defun spam-copy-spam-routine (articles)
1493   (spam-copy-or-move-routine
1494    t
1495    (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1496    articles
1497    'spam))
1498
1499 (defun spam-move-spam-routine (articles)
1500   (spam-copy-or-move-routine
1501    nil
1502    (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1503    articles
1504    'spam))
1505
1506 (defun spam-copy-ham-routine (articles)
1507   (spam-copy-or-move-routine
1508    t
1509    (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1510    articles
1511    'ham))
1512
1513 (defun spam-move-ham-routine (articles)
1514   (spam-copy-or-move-routine
1515    nil
1516    (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1517    articles
1518    'ham))
1519
1520 ;;}}}
1521
1522 ;;{{{ article and field retrieval code
1523 (defun spam-get-article-as-string (article)
1524   (when (numberp article)
1525     (with-temp-buffer
1526       (gnus-request-article-this-buffer
1527        article
1528        gnus-newsgroup-name)
1529       (buffer-string))))
1530
1531 ;; disabled for now
1532 ;; (defun spam-get-article-as-filename (article)
1533 ;;   (let ((article-filename))
1534 ;;     (when (numberp article)
1535 ;;       (nnml-possibly-change-directory
1536 ;;        (gnus-group-real-name gnus-newsgroup-name))
1537 ;;       (setq article-filename (expand-file-name
1538 ;;                              (int-to-string article) nnml-current-directory)))
1539 ;;     (if (file-exists-p article-filename)
1540 ;;      article-filename
1541 ;;       nil)))
1542
1543 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1544   "Fetch a FIELD for ARTICLE quickly, using the internal gnus-data-list function.
1545 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1546 When FIELD is 'number, ARTICLE can be any number (since we want
1547 to find it out)."
1548   (when (numberp article)
1549     (let* ((data-header (or prepared-data-header
1550                             (spam-fetch-article-header article))))
1551       (if (arrayp data-header)
1552         (cond
1553          ((equal field 'number)
1554           (mail-header-number data-header))
1555          ((equal field 'from)
1556           (mail-header-from data-header))
1557          ((equal field 'message-id)
1558           (mail-header-message-id data-header))
1559          ((equal field 'subject)
1560           (mail-header-subject data-header))
1561          ((equal field 'references)
1562           (mail-header-references data-header))
1563          ((equal field 'date)
1564           (mail-header-date data-header))
1565          ((equal field 'xref)
1566           (mail-header-xref data-header))
1567          ((equal field 'extra)
1568           (mail-header-extra data-header))
1569          (t
1570           (gnus-error
1571            5
1572            "spam-fetch-field-fast: unknown field %s requested"
1573            field)
1574           nil))
1575         (gnus-message 6 "Article %d has a nil data header" article)))))
1576
1577 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1578   (spam-fetch-field-fast article 'from prepared-data-header))
1579
1580 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1581   (spam-fetch-field-fast article 'subject prepared-data-header))
1582
1583 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1584   (spam-fetch-field-fast article 'message-id prepared-data-header))
1585
1586 (defun spam-generate-fake-headers (article)
1587   (let ((dh (spam-fetch-article-header article)))
1588     (if dh
1589         (concat
1590          (format
1591           ;; 80-character limit makes for strange constructs
1592           (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1593                   "Date: %s\nReferences: %s\nXref: %s\n")
1594           (spam-fetch-field-fast article 'from dh)
1595           (spam-fetch-field-fast article 'subject dh)
1596           (spam-fetch-field-fast article 'message-id dh)
1597           (spam-fetch-field-fast article 'date dh)
1598           (spam-fetch-field-fast article 'references dh)
1599           (spam-fetch-field-fast article 'xref dh))
1600          (when (spam-fetch-field-fast article 'extra dh)
1601            (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1602       (gnus-message
1603        5
1604        "spam-generate-fake-headers: article %d didn't have a valid header"
1605        article))))
1606
1607 (defun spam-fetch-article-header (article)
1608   (with-current-buffer gnus-summary-buffer
1609     (gnus-read-header article)
1610     (nth 3 (assq article gnus-newsgroup-data))))
1611 ;;}}}
1612
1613 ;;{{{ Spam determination.
1614
1615 (defun spam-split (&rest specific-checks)
1616   "Split this message into the `spam' group if it is spam.
1617 This function can be used as an entry in the variable `nnmail-split-fancy',
1618 for example like this: (: spam-split).  It can take checks as
1619 parameters.  A string as a parameter will set the
1620 `spam-split-group' to that string.
1621
1622 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1623   (interactive)
1624   (setq spam-split-last-successful-check nil)
1625   (unless spam-split-disabled
1626     (let ((spam-split-group-choice spam-split-group))
1627       (dolist (check specific-checks)
1628         (when (stringp check)
1629           (setq spam-split-group-choice check)
1630           (setq specific-checks (delq check specific-checks))))
1631
1632       (let ((spam-split-group spam-split-group-choice)
1633             (widening-needed-check (spam-widening-needed-p specific-checks)))
1634         (save-excursion
1635           (save-restriction
1636             (when widening-needed-check
1637               (widen)
1638               (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1639                             widening-needed-check))
1640             (let ((backends (spam-backend-list))
1641                   decision)
1642               (while (and backends (not decision))
1643                 (let* ((backend (pop backends))
1644                        (check-function (spam-backend-check backend))
1645                        (spam-split-group (if spam-split-symbolic-return
1646                                              'spam
1647                                            spam-split-group)))
1648                   (when (or
1649                          ;; either, given specific checks, this is one of them
1650                          (memq backend specific-checks)
1651                          ;; or, given no specific checks, spam-use-CHECK is set
1652                          (and (null specific-checks) (symbol-value backend)))
1653                     (gnus-message 6 "spam-split: calling the %s function"
1654                                   check-function)
1655                     (setq decision (funcall check-function))
1656                     ;; if we got a decision at all, save the current check
1657                     (when decision
1658                       (setq spam-split-last-successful-check backend))
1659
1660                     (when (eq decision 'spam)
1661                       (unless spam-split-symbolic-return
1662                         (gnus-error
1663                          5
1664                          (format "spam-split got %s but %s is nil"
1665                                  decision
1666                                  spam-split-symbolic-return)))))))
1667               (if (eq decision t)
1668                   (if spam-split-symbolic-return-positive 'ham nil)
1669                 decision))))))))
1670
1671 (defun spam-find-spam ()
1672   "Detect spam in the current newsgroup using `spam-split'."
1673   (interactive)
1674
1675   (let* ((group gnus-newsgroup-name)
1676          (autodetect (gnus-parameter-spam-autodetect group))
1677          (methods (gnus-parameter-spam-autodetect-methods group))
1678          (first-method (nth 0 methods))
1679          (articles (if spam-autodetect-recheck-messages
1680                        gnus-newsgroup-articles
1681                      gnus-newsgroup-unseen))
1682          article-cannot-be-faked)
1683
1684
1685     (dolist (backend methods)
1686       (when (spam-backend-statistical-p backend)
1687         (setq article-cannot-be-faked t)
1688         (return)))
1689
1690     (when (memq 'default methods)
1691       (setq article-cannot-be-faked t))
1692
1693     (when (and autodetect
1694                (not (equal first-method 'none)))
1695       (mapcar
1696        (lambda (article)
1697          (let ((id (spam-fetch-field-message-id-fast article))
1698                (subject (spam-fetch-field-subject-fast article))
1699                (sender (spam-fetch-field-from-fast article))
1700                registry-lookup)
1701
1702            (unless id
1703              (gnus-message 6 "Article %d has no message ID!" article))
1704
1705            (when (and id spam-log-to-registry)
1706              (setq registry-lookup (spam-log-registration-type id 'incoming))
1707              (when registry-lookup
1708                (gnus-message
1709                 9
1710                 "spam-find-spam: message %s was already registered incoming"
1711                 id)))
1712
1713            (let* ((spam-split-symbolic-return t)
1714                   (spam-split-symbolic-return-positive t)
1715                   (fake-headers (spam-generate-fake-headers article))
1716                   (split-return
1717                    (or registry-lookup
1718                        (with-temp-buffer
1719                          (if article-cannot-be-faked
1720                              (gnus-request-article-this-buffer
1721                               article
1722                               group)
1723                            ;; else, we fake the article
1724                            (when fake-headers (insert fake-headers)))
1725                          (if (or (null first-method)
1726                                  (equal first-method 'default))
1727                              (spam-split)
1728                            (apply 'spam-split methods))))))
1729              (if (equal split-return 'spam)
1730                  (gnus-summary-mark-article article gnus-spam-mark))
1731
1732              (when (and id split-return spam-log-to-registry)
1733                (when (zerop (gnus-registry-group-count id))
1734                  (gnus-registry-add-group
1735                   id group subject sender))
1736
1737                (unless registry-lookup
1738                  (spam-log-processing-to-registry
1739                   id
1740                   'incoming
1741                   split-return
1742                   spam-split-last-successful-check
1743                   group))))))
1744        articles))))
1745
1746 ;;}}}
1747
1748 ;;{{{ registration/unregistration functions
1749
1750 (defun spam-resolve-registrations-routine ()
1751   "Go through the backends and register or unregister articles as needed."
1752   (dolist (backend-type '(non-mover mover))
1753     (dolist (classification (spam-classifications))
1754       (dolist (backend (spam-backend-list backend-type))
1755         (let ((rlist (spam-backend-get-article-todo-list
1756                       backend classification))
1757               (ulist (spam-backend-get-article-todo-list
1758                       backend classification t))
1759               (delcount 0))
1760
1761           ;; clear the old lists right away
1762           (spam-backend-put-article-todo-list backend
1763                                               classification
1764                                               nil
1765                                               nil)
1766           (spam-backend-put-article-todo-list backend
1767                                               classification
1768                                               nil
1769                                               t)
1770
1771           ;; eliminate duplicates
1772           (dolist (article (copy-sequence ulist))
1773             (when (memq article rlist)
1774               (incf delcount)
1775               (setq rlist (delq article rlist))
1776               (setq ulist (delq article ulist))))
1777
1778           (unless (zerop delcount)
1779             (gnus-message
1780              9
1781              "%d messages were saved the trouble of unregistering and then registering"
1782              delcount))
1783
1784           ;; unregister articles
1785           (unless (zerop (length ulist))
1786             (let ((num (spam-unregister-routine classification backend ulist)))
1787               (when (> num 0)
1788                 (gnus-message
1789                  6
1790                  "%d %s messages were unregistered by backend %s."
1791                  num
1792                  classification
1793                  backend))))
1794
1795             ;; register articles
1796             (unless (zerop (length rlist))
1797               (let ((num (spam-register-routine classification backend rlist)))
1798                 (when (> num 0)
1799                   (gnus-message
1800                    6
1801                    "%d %s messages were registered by backend %s."
1802                    num
1803                    classification
1804                    backend)))))))))
1805
1806 (defun spam-unregister-routine (classification
1807                                 backend
1808                                 specific-articles)
1809   (spam-register-routine classification backend specific-articles t))
1810
1811 (defun spam-register-routine (classification
1812                               backend
1813                               specific-articles
1814                               &optional unregister)
1815   (when (and (spam-classification-valid-p classification)
1816              (spam-backend-valid-p backend))
1817     (let* ((register-function
1818             (spam-backend-function backend classification 'registration))
1819            (unregister-function
1820             (spam-backend-function backend classification 'unregistration))
1821            (run-function (if unregister
1822                              unregister-function
1823                            register-function))
1824            (log-function (if unregister
1825                              'spam-log-undo-registration
1826                            'spam-log-processing-to-registry))
1827            article articles)
1828
1829       (when run-function
1830         ;; make list of articles, using specific-articles if given
1831         (setq articles (or specific-articles
1832                            (spam-list-articles
1833                             gnus-newsgroup-articles
1834                             classification)))
1835         ;; process them
1836         (when (> (length articles) 0)
1837           (gnus-message 5 "%s %d %s articles as %s using backend %s"
1838                         (if unregister "Unregistering" "Registering")
1839                         (length articles)
1840                         (if specific-articles "specific" "")
1841                         classification
1842                         backend)
1843           (funcall run-function articles)
1844           ;; now log all the registrations (or undo them, depending on
1845           ;; unregister)
1846           (dolist (article articles)
1847             (funcall log-function
1848                      (spam-fetch-field-message-id-fast article)
1849                      'process
1850                      classification
1851                      backend
1852                      gnus-newsgroup-name))))
1853       ;; return the number of articles processed
1854       (length articles))))
1855
1856 ;;; log a ham- or spam-processor invocation to the registry
1857 (defun spam-log-processing-to-registry (id type classification backend group)
1858   (when spam-log-to-registry
1859     (if (and (stringp id)
1860              (stringp group)
1861              (spam-process-type-valid-p type)
1862              (spam-classification-valid-p classification)
1863              (spam-backend-valid-p backend))
1864         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1865               (cell (list classification backend group)))
1866           (push cell cell-list)
1867           (gnus-registry-store-extra-entry
1868            id
1869            type
1870            cell-list))
1871
1872       (gnus-error
1873        7
1874        (format "%s call with bad ID, type, classification, spam-backend, or group"
1875                "spam-log-processing-to-registry")))))
1876
1877 ;;; check if a ham- or spam-processor registration has been done
1878 (defun spam-log-registered-p (id type)
1879   (when spam-log-to-registry
1880     (if (and (stringp id)
1881              (spam-process-type-valid-p type))
1882         (cdr-safe (gnus-registry-fetch-extra id type))
1883       (progn
1884         (gnus-error
1885          7
1886          (format "%s called with bad ID, type, classification, or spam-backend"
1887                  "spam-log-registered-p"))
1888         nil))))
1889
1890 ;;; check what a ham- or spam-processor registration says
1891 ;;; returns nil if conflicting registrations are found
1892 (defun spam-log-registration-type (id type)
1893   (let ((count 0)
1894         decision)
1895     (dolist (reg (spam-log-registered-p id type))
1896       (let ((classification (nth 0 reg)))
1897         (when (spam-classification-valid-p classification)
1898           (when (and decision
1899                      (not (eq classification decision)))
1900             (setq count (+ 1 count)))
1901           (setq decision classification))))
1902     (if (< 0 count)
1903         nil
1904       decision)))
1905
1906
1907 ;;; check if a ham- or spam-processor registration needs to be undone
1908 (defun spam-log-unregistration-needed-p (id type classification backend)
1909   (when spam-log-to-registry
1910     (if (and (stringp id)
1911              (spam-process-type-valid-p type)
1912              (spam-classification-valid-p classification)
1913              (spam-backend-valid-p backend))
1914         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1915               found)
1916           (dolist (cell cell-list)
1917             (unless found
1918               (when (and (eq classification (nth 0 cell))
1919                          (eq backend (nth 1 cell)))
1920                 (setq found t))))
1921           found)
1922       (progn
1923         (gnus-error
1924          7
1925          (format "%s called with bad ID, type, classification, or spam-backend"
1926                  "spam-log-unregistration-needed-p"))
1927         nil))))
1928
1929
1930 ;;; undo a ham- or spam-processor registration (the group is not used)
1931 (defun spam-log-undo-registration (id type classification backend &optional group)
1932   (when (and spam-log-to-registry
1933              (spam-log-unregistration-needed-p id type classification backend))
1934     (if (and (stringp id)
1935              (spam-process-type-valid-p type)
1936              (spam-classification-valid-p classification)
1937              (spam-backend-valid-p backend))
1938         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1939               new-cell-list found)
1940           (dolist (cell cell-list)
1941             (unless (and (eq classification (nth 0 cell))
1942                          (eq backend (nth 1 cell)))
1943               (push cell new-cell-list)))
1944           (gnus-registry-store-extra-entry
1945            id
1946            type
1947            new-cell-list))
1948       (progn
1949         (gnus-error 7 (format "%s call with bad ID, type, spam-backend, or group"
1950                               "spam-log-undo-registration"))
1951         nil))))
1952
1953 ;;}}}
1954
1955 ;;{{{ backend functions
1956
1957 ;;{{{ Gmane xrefs
1958 (defun spam-check-gmane-xref ()
1959   (let ((header (or
1960                  (message-fetch-field "Xref")
1961                  (message-fetch-field "Newsgroups"))))
1962     (when header                        ; return nil when no header
1963       (when (string-match spam-gmane-xref-spam-group
1964                           header)
1965           spam-split-group))))
1966
1967 ;;}}}
1968
1969 ;;{{{ Regex body
1970
1971 (defun spam-check-regex-body ()
1972   (let ((spam-regex-headers-ham spam-regex-body-ham)
1973         (spam-regex-headers-spam spam-regex-body-spam))
1974     (spam-check-regex-headers t)))
1975
1976 ;;}}}
1977
1978 ;;{{{ Regex headers
1979
1980 (defun spam-check-regex-headers (&optional body)
1981   (let ((type (if body "body" "header"))
1982         ret found)
1983     (dolist (h-regex spam-regex-headers-ham)
1984       (unless found
1985         (goto-char (point-min))
1986         (when (re-search-forward h-regex nil t)
1987           (message "Ham regex %s search positive." type)
1988           (setq found t))))
1989     (dolist (s-regex spam-regex-headers-spam)
1990       (unless found
1991         (goto-char (point-min))
1992         (when (re-search-forward s-regex nil t)
1993           (message "Spam regex %s search positive." type)
1994           (setq found t)
1995           (setq ret spam-split-group))))
1996     ret))
1997
1998 ;;}}}
1999
2000 ;;{{{ Blackholes.
2001
2002 (defun spam-reverse-ip-string (ip)
2003   (when (stringp ip)
2004     (mapconcat 'identity
2005                (nreverse (split-string ip "\\."))
2006                ".")))
2007
2008 (defun spam-check-blackholes ()
2009   "Check the Received headers for blackholed relays."
2010   (let ((headers (message-fetch-field "received"))
2011         ips matches)
2012     (when headers
2013       (with-temp-buffer
2014         (insert headers)
2015         (goto-char (point-min))
2016         (gnus-message 6 "Checking headers for relay addresses")
2017         (while (re-search-forward
2018                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
2019           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
2020           (push (spam-reverse-ip-string (match-string 1))
2021                 ips)))
2022       (dolist (server spam-blackhole-servers)
2023         (dolist (ip ips)
2024           (unless (and spam-blackhole-good-server-regex
2025                        ;; match the good-server-regex against the reversed (again) IP string
2026                        (string-match
2027                         spam-blackhole-good-server-regex
2028                         (spam-reverse-ip-string ip)))
2029             (unless matches
2030               (let ((query-string (concat ip "." server)))
2031                 (if spam-use-dig
2032                     (let ((query-result (query-dig query-string)))
2033                       (when query-result
2034                         (gnus-message 6 "(DIG): positive blackhole check '%s'"
2035                                       query-result)
2036                         (push (list ip server query-result)
2037                               matches)))
2038                   ;; else, if not using dig.el
2039                   (when (dns-query query-string)
2040                     (gnus-message 6 "positive blackhole check")
2041                     (push (list ip server (dns-query query-string 'TXT))
2042                           matches)))))))))
2043     (when matches
2044       spam-split-group)))
2045 ;;}}}
2046
2047 ;;{{{ Hashcash.
2048
2049 (defun spam-check-hashcash ()
2050   "Check the headers for hashcash payments."
2051   (ignore-errors (mail-check-payment)))  ;mail-check-payment returns a boolean
2052
2053 ;;}}}
2054
2055 ;;{{{ BBDB
2056
2057 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2058 ;;; <sacha@giotto.sj.ru>
2059
2060 ;; all this is done inside a condition-case to trap errors
2061
2062 (eval-when-compile
2063   (autoload 'bbdb-buffer "bbdb")
2064   (autoload 'bbdb-create-internal "bbdb")
2065   (autoload 'bbdb-search-simple "bbdb"))
2066
2067 ;; Autoloaded in message, which we require.
2068 (declare-function gnus-extract-address-components "gnus-util" (from))
2069
2070 (eval-and-compile
2071   (when (condition-case nil
2072             (progn
2073               (require 'bbdb)
2074               (require 'bbdb-com))
2075           (file-error
2076            ;; `bbdb-records' should not be bound as an autoload function
2077            ;; before loading bbdb because of `bbdb-hashtable-size'.
2078            (defalias 'bbdb-records 'ignore)
2079            (defalias 'spam-BBDB-register-routine 'ignore)
2080            (defalias 'spam-enter-ham-BBDB 'ignore)
2081            nil))
2082
2083     ;; when the BBDB changes, we want to clear out our cache
2084     (defun spam-clear-cache-BBDB (&rest immaterial)
2085       (spam-clear-cache 'spam-use-BBDB))
2086
2087     (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2088
2089     (defun spam-enter-ham-BBDB (addresses &optional remove)
2090       "Enter an address into the BBDB; implies ham (non-spam) sender"
2091       (dolist (from addresses)
2092         (when (stringp from)
2093           (let* ((parsed-address (gnus-extract-address-components from))
2094                  (name (or (nth 0 parsed-address) "Ham Sender"))
2095                  (remove-function (if remove
2096                                       'bbdb-delete-record-internal
2097                                     'ignore))
2098                  (net-address (nth 1 parsed-address))
2099                  (record (and net-address
2100                               (bbdb-search-simple nil net-address))))
2101             (when net-address
2102               (gnus-message 6 "%s address %s %s BBDB"
2103                             (if remove "Deleting" "Adding")
2104                             from
2105                             (if remove "from" "to"))
2106               (if record
2107                   (funcall remove-function record)
2108                 (bbdb-create-internal name nil net-address nil nil
2109                                       "ham sender added by spam.el")))))))
2110
2111     (defun spam-BBDB-register-routine (articles &optional unregister)
2112       (let (addresses)
2113         (dolist (article articles)
2114           (when (stringp (spam-fetch-field-from-fast article))
2115             (push (spam-fetch-field-from-fast article) addresses)))
2116         ;; now do the register/unregister action
2117         (spam-enter-ham-BBDB addresses unregister)))
2118
2119     (defun spam-BBDB-unregister-routine (articles)
2120       (spam-BBDB-register-routine articles t))
2121
2122     (defun spam-check-BBDB ()
2123       "Mail from people in the BBDB is classified as ham or non-spam"
2124       (let ((who (message-fetch-field "from"))
2125             bbdb-cache bbdb-hashtable)
2126         (when spam-cache-lookups
2127           (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
2128           (unless bbdb-cache
2129             (setq bbdb-cache (make-vector 17 0)) ; a good starting hash value
2130             ;; this is based on the expanded (bbdb-hashtable) macro
2131             ;; without the debugging support
2132             (with-current-buffer (bbdb-buffer)
2133               (save-excursion
2134                 (save-window-excursion
2135                   (bbdb-records nil t)
2136                   (mapatoms
2137                    (lambda (symbol)
2138                      (intern (downcase (symbol-name symbol)) bbdb-cache))
2139                    bbdb-hashtable))))
2140             (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
2141         (when who
2142           (setq who (nth 1 (gnus-extract-address-components who)))
2143           (if
2144               (if spam-cache-lookups
2145                   (intern-soft (downcase who) bbdb-cache)
2146                 (bbdb-search-simple nil who))
2147               t
2148             (if spam-use-BBDB-exclusive
2149                 spam-split-group
2150               nil)))))))
2151
2152 ;;}}}
2153
2154 ;;{{{ ifile
2155
2156 ;;; check the ifile backend; return nil if the mail was NOT classified
2157 ;;; as spam
2158
2159
2160 (defun spam-get-ifile-database-parameter ()
2161   "Return the command-line parameter for ifile's database.
2162 See `spam-ifile-database'."
2163   (if spam-ifile-database
2164       (format "--db-file=%s" spam-ifile-database)
2165     nil))
2166
2167 (defun spam-check-ifile ()
2168   "Check the ifile backend for the classification of this message."
2169   (let ((article-buffer-name (buffer-name))
2170         category return)
2171     (with-temp-buffer
2172       (let ((temp-buffer-name (buffer-name))
2173             (db-param (spam-get-ifile-database-parameter)))
2174         (with-current-buffer article-buffer-name
2175           (apply 'call-process-region
2176                  (point-min) (point-max) spam-ifile-program
2177                  nil temp-buffer-name nil "-c"
2178                  (if db-param `(,db-param "-q") `("-q"))))
2179         ;; check the return now (we're back in the temp buffer)
2180         (goto-char (point-min))
2181         (if (not (eobp))
2182             (setq category (buffer-substring (point) (point-at-eol))))
2183         (when (not (zerop (length category))) ; we need a category here
2184           (if spam-ifile-all-categories
2185               (setq return category)
2186             ;; else, if spam-ifile-all-categories is not set...
2187             (when (string-equal spam-ifile-spam-category category)
2188               (setq return spam-split-group)))))) ; note return is nil otherwise
2189     return))
2190
2191 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2192   "Register an article, given as a string, with a category.
2193 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2194   (let ((category (or category gnus-newsgroup-name))
2195         (add-or-delete-option (if unregister "-d" "-i"))
2196         (db (spam-get-ifile-database-parameter))
2197         parameters)
2198     (with-temp-buffer
2199       (dolist (article articles)
2200         (let ((article-string (spam-get-article-as-string article)))
2201           (when (stringp article-string)
2202             (insert article-string))))
2203       (apply 'call-process-region
2204              (point-min) (point-max) spam-ifile-program
2205              nil nil nil
2206              add-or-delete-option category
2207              (if db `(,db "-h") `("-h"))))))
2208
2209 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2210   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2211
2212 (defun spam-ifile-unregister-spam-routine (articles)
2213   (spam-ifile-register-spam-routine articles t))
2214
2215 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2216   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2217
2218 (defun spam-ifile-unregister-ham-routine (articles)
2219   (spam-ifile-register-ham-routine articles t))
2220
2221 ;;}}}
2222
2223 ;;{{{ spam-stat
2224
2225 (eval-when-compile
2226   (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2227   (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2228   (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2229   (autoload 'spam-stat-buffer-is-spam "spam-stat")
2230   (autoload 'spam-stat-load "spam-stat")
2231   (autoload 'spam-stat-save "spam-stat")
2232   (autoload 'spam-stat-split-fancy "spam-stat"))
2233
2234 (eval-and-compile
2235   (when (condition-case nil
2236             (let ((spam-stat-install-hooks nil))
2237               (require 'spam-stat))
2238           (file-error
2239            (defalias 'spam-stat-register-ham-routine 'ignore)
2240            (defalias 'spam-stat-register-spam-routine 'ignore)
2241            nil))
2242
2243     (defun spam-check-stat ()
2244       "Check the spam-stat backend for the classification of this message"
2245       (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2246             (spam-stat-buffer (buffer-name)) ; stat the current buffer
2247             category return)
2248         (spam-stat-split-fancy)))
2249
2250     (defun spam-stat-register-spam-routine (articles &optional unregister)
2251       (dolist (article articles)
2252         (let ((article-string (spam-get-article-as-string article)))
2253           (with-temp-buffer
2254             (insert article-string)
2255             (if unregister
2256                 (spam-stat-buffer-change-to-non-spam)
2257               (spam-stat-buffer-is-spam))))))
2258
2259     (defun spam-stat-unregister-spam-routine (articles)
2260       (spam-stat-register-spam-routine articles t))
2261
2262     (defun spam-stat-register-ham-routine (articles &optional unregister)
2263       (dolist (article articles)
2264         (let ((article-string (spam-get-article-as-string article)))
2265           (with-temp-buffer
2266             (insert article-string)
2267             (if unregister
2268                 (spam-stat-buffer-change-to-spam)
2269               (spam-stat-buffer-is-non-spam))))))
2270
2271     (defun spam-stat-unregister-ham-routine (articles)
2272       (spam-stat-register-ham-routine articles t))
2273
2274     (defun spam-maybe-spam-stat-load ()
2275       (when spam-use-stat (spam-stat-load)))
2276
2277     (defun spam-maybe-spam-stat-save ()
2278       (when spam-use-stat (spam-stat-save)))))
2279
2280 ;;}}}
2281
2282 ;;{{{ Blacklists and whitelists.
2283
2284 (defvar spam-whitelist-cache nil)
2285 (defvar spam-blacklist-cache nil)
2286
2287 (defun spam-kill-whole-line ()
2288   (beginning-of-line)
2289   (let ((kill-whole-line t))
2290     (kill-line)))
2291
2292 ;;; address can be a list, too
2293 (defun spam-enter-whitelist (address &optional remove)
2294   "Enter ADDRESS (list or single) into the whitelist.
2295 With a non-nil REMOVE, remove them."
2296   (interactive "sAddress: ")
2297   (spam-enter-list address spam-whitelist remove)
2298   (setq spam-whitelist-cache nil)
2299   (spam-clear-cache 'spam-use-whitelist))
2300
2301 ;;; address can be a list, too
2302 (defun spam-enter-blacklist (address &optional remove)
2303   "Enter ADDRESS (list or single) into the blacklist.
2304 With a non-nil REMOVE, remove them."
2305   (interactive "sAddress: ")
2306   (spam-enter-list address spam-blacklist remove)
2307   (setq spam-blacklist-cache nil)
2308   (spam-clear-cache 'spam-use-whitelist))
2309
2310 (defun spam-enter-list (addresses file &optional remove)
2311   "Enter ADDRESSES into the given FILE.
2312 Either the whitelist or the blacklist files can be used.
2313 With a non-nil REMOVE, remove the ADDRESSES."
2314   (if (stringp addresses)
2315       (spam-enter-list (list addresses) file remove)
2316     ;; else, we have a list of addresses here
2317     (unless (file-exists-p (file-name-directory file))
2318       (make-directory (file-name-directory file) t))
2319     (with-current-buffer
2320        (find-file-noselect file)
2321       (dolist (a addresses)
2322         (when (stringp a)
2323           (goto-char (point-min))
2324           (if (re-search-forward (regexp-quote a) nil t)
2325               ;; found the address
2326               (when remove
2327                 (spam-kill-whole-line))
2328             ;; else, the address was not found
2329             (unless remove
2330               (goto-char (point-max))
2331               (unless (bobp)
2332                 (insert "\n"))
2333               (insert a "\n")))))
2334       (save-buffer))))
2335
2336 (defun spam-filelist-build-cache (type)
2337   (let ((cache (if (eq type 'spam-use-blacklist)
2338                    spam-blacklist-cache
2339                  spam-whitelist-cache))
2340         parsed-cache)
2341     (unless (gethash type spam-caches)
2342       (while cache
2343         (let ((address (pop cache)))
2344           (unless (zerop (length address)) ; 0 for a nil address too
2345             (setq address (regexp-quote address))
2346             ;; fix regexp-quote's treatment of user-intended regexes
2347             (while (string-match "\\\\\\*" address)
2348               (setq address (replace-match ".*" t t address))))
2349           (push address parsed-cache)))
2350       (puthash type parsed-cache spam-caches))))
2351
2352 (defun spam-filelist-check-cache (type from)
2353   (when (stringp from)
2354     (spam-filelist-build-cache type)
2355     (let (found)
2356       (dolist (address (gethash type spam-caches))
2357         (when (and address (string-match address from))
2358           (setq found t)
2359           (return)))
2360       found)))
2361
2362 ;;; returns t if the sender is in the whitelist, nil or
2363 ;;; spam-split-group otherwise
2364 (defun spam-check-whitelist ()
2365   ;; FIXME!  Should it detect when file timestamps change?
2366   (unless spam-whitelist-cache
2367     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2368   (if (spam-from-listed-p 'spam-use-whitelist)
2369       t
2370     (if spam-use-whitelist-exclusive
2371         spam-split-group
2372       nil)))
2373
2374 (defun spam-check-blacklist ()
2375   ;; FIXME!  Should it detect when file timestamps change?
2376   (unless spam-blacklist-cache
2377     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2378   (and (spam-from-listed-p 'spam-use-blacklist)
2379        spam-split-group))
2380
2381 (defun spam-parse-list (file)
2382   (when (file-readable-p file)
2383     (let (contents address)
2384       (with-temp-buffer
2385         (insert-file-contents file)
2386         (while (not (eobp))
2387           (setq address (buffer-substring (point) (point-at-eol)))
2388           (forward-line 1)
2389           ;; insert the e-mail address if detected, otherwise the raw data
2390           (unless (zerop (length address))
2391             (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2392               (push (or pure-address address) contents)))))
2393       (nreverse contents))))
2394
2395 (defun spam-from-listed-p (type)
2396   (let ((from (message-fetch-field "from"))
2397         found)
2398     (spam-filelist-check-cache type from)))
2399
2400 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2401   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2402         (declassification (if blacklist 'ham 'spam))
2403         (enter-function
2404          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2405         (remove-function
2406          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2407         from addresses unregister-list article-unregister-list)
2408     (dolist (article articles)
2409       (let ((from (spam-fetch-field-from-fast article))
2410             (id (spam-fetch-field-message-id-fast article))
2411             sender-ignored)
2412         (when (stringp from)
2413           (dolist (ignore-regex spam-blacklist-ignored-regexes)
2414             (when (and (not sender-ignored)
2415                        (stringp ignore-regex)
2416                        (string-match ignore-regex from))
2417               (setq sender-ignored t)))
2418           ;; remember the messages we need to unregister, unless remove is set
2419           (when (and
2420                  (null unregister)
2421                  (spam-log-unregistration-needed-p
2422                   id 'process declassification de-symbol))
2423             (push article article-unregister-list)
2424             (push from unregister-list))
2425           (unless sender-ignored
2426             (push from addresses)))))
2427
2428     (if unregister
2429         (funcall enter-function addresses t) ; unregister all these addresses
2430       ;; else, register normally and unregister what we need to
2431       (funcall remove-function unregister-list t)
2432       (dolist (article article-unregister-list)
2433         (spam-log-undo-registration
2434          (spam-fetch-field-message-id-fast article)
2435          'process
2436          declassification
2437          de-symbol))
2438       (funcall enter-function addresses nil))))
2439
2440 (defun spam-blacklist-unregister-routine (articles)
2441   (spam-blacklist-register-routine articles t))
2442
2443 (defun spam-blacklist-register-routine (articles &optional unregister)
2444   (spam-filelist-register-routine articles t unregister))
2445
2446 (defun spam-whitelist-unregister-routine (articles)
2447   (spam-whitelist-register-routine articles t))
2448
2449 (defun spam-whitelist-register-routine (articles &optional unregister)
2450   (spam-filelist-register-routine articles nil unregister))
2451
2452 ;;}}}
2453
2454 ;;{{{ Spam-report glue (gmane and resend reporting)
2455 (defun spam-report-gmane-register-routine (articles)
2456   (when articles
2457     (apply 'spam-report-gmane-spam articles)))
2458
2459 (defun spam-report-gmane-unregister-routine (articles)
2460   (when articles
2461     (apply 'spam-report-gmane-ham articles)))
2462
2463 (defun spam-report-resend-register-ham-routine (articles)
2464   (spam-report-resend-register-routine articles t))
2465
2466 (defun spam-report-resend-register-routine (articles &optional ham)
2467   (let* ((resend-to-gp
2468           (if ham
2469               (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2470             (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2471          (spam-report-resend-to (or (car-safe resend-to-gp)
2472                                     spam-report-resend-to)))
2473     (spam-report-resend articles ham)))
2474
2475 ;;}}}
2476
2477 ;;{{{ Bogofilter
2478 (defun spam-check-bogofilter-headers (&optional score)
2479   (let ((header (message-fetch-field spam-bogofilter-header)))
2480     (when header                        ; return nil when no header
2481       (if score                         ; scoring mode
2482           (if (string-match "spamicity=\\([0-9.]+\\)" header)
2483               (match-string 1 header)
2484             "0")
2485         ;; spam detection mode
2486         (when (string-match spam-bogofilter-bogosity-positive-spam-header
2487                             header)
2488           spam-split-group)))))
2489
2490 ;; return something sensible if the score can't be determined
2491 (defun spam-bogofilter-score (&optional recheck)
2492   "Get the Bogofilter spamicity score."
2493   (interactive "P")
2494   (save-window-excursion
2495     (gnus-summary-show-article t)
2496     (set-buffer gnus-article-buffer)
2497     (let ((score (or (unless recheck
2498                        (spam-check-bogofilter-headers t))
2499                      (spam-check-bogofilter t))))
2500       (gnus-summary-show-article)
2501       (message "Spamicity score %s" score)
2502       (or score "0"))))
2503
2504 (defun spam-verify-bogofilter ()
2505   "Verify the Bogofilter version is sufficient."
2506   (when (eq spam-bogofilter-valid 'unknown)
2507     (setq spam-bogofilter-valid
2508           (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2509                              (shell-command-to-string
2510                               (format "%s -V" spam-bogofilter-program))))))
2511   spam-bogofilter-valid)
2512
2513 (defun spam-check-bogofilter (&optional score)
2514   "Check the Bogofilter backend for the classification of this message."
2515   (if (spam-verify-bogofilter)
2516       (let ((article-buffer-name (buffer-name))
2517             (db spam-bogofilter-database-directory)
2518             return)
2519         (with-temp-buffer
2520           (let ((temp-buffer-name (buffer-name)))
2521             (with-current-buffer article-buffer-name
2522               (apply 'call-process-region
2523                      (point-min) (point-max)
2524                      spam-bogofilter-program
2525                      nil temp-buffer-name nil
2526                      (if db `("-d" ,db "-v") `("-v"))))
2527             (setq return (spam-check-bogofilter-headers score))))
2528         return)
2529     (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2530
2531 (defun spam-bogofilter-register-with-bogofilter (articles
2532                                                  spam
2533                                                  &optional unregister)
2534   "Register an article, given as a string, as spam or non-spam."
2535   (if (spam-verify-bogofilter)
2536       (dolist (article articles)
2537         (let ((article-string (spam-get-article-as-string article))
2538               (db spam-bogofilter-database-directory)
2539               (switch (if unregister
2540                           (if spam
2541                               spam-bogofilter-spam-strong-switch
2542                             spam-bogofilter-ham-strong-switch)
2543                         (if spam
2544                             spam-bogofilter-spam-switch
2545                           spam-bogofilter-ham-switch))))
2546           (when (stringp article-string)
2547             (with-temp-buffer
2548               (insert article-string)
2549
2550               (apply 'call-process-region
2551                      (point-min) (point-max)
2552                      spam-bogofilter-program
2553                      nil nil nil switch
2554                      (if db `("-d" ,db "-v") `("-v")))))))
2555     (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2556
2557 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2558   (spam-bogofilter-register-with-bogofilter articles t unregister))
2559
2560 (defun spam-bogofilter-unregister-spam-routine (articles)
2561   (spam-bogofilter-register-spam-routine articles t))
2562
2563 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2564   (spam-bogofilter-register-with-bogofilter articles nil unregister))
2565
2566 (defun spam-bogofilter-unregister-ham-routine (articles)
2567   (spam-bogofilter-register-ham-routine articles t))
2568
2569
2570 ;;}}}
2571
2572 ;;{{{ spamoracle
2573 (defun spam-check-spamoracle ()
2574   "Run spamoracle on an article to determine whether it's spam."
2575   (let ((article-buffer-name (buffer-name)))
2576     (with-temp-buffer
2577       (let ((temp-buffer-name (buffer-name)))
2578         (with-current-buffer article-buffer-name
2579           (let ((status
2580                  (apply 'call-process-region
2581                         (point-min) (point-max)
2582                         spam-spamoracle-binary
2583                         nil temp-buffer-name nil
2584                         (if spam-spamoracle-database
2585                             `("-f" ,spam-spamoracle-database "mark")
2586                           '("mark")))))
2587             (if (eq 0 status)
2588                 (progn
2589                   (set-buffer temp-buffer-name)
2590                   (goto-char (point-min))
2591                   (when (re-search-forward "^X-Spam: yes;" nil t)
2592                     spam-split-group))
2593               (error "Error running spamoracle: %s" status))))))))
2594
2595 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2596   "Run spamoracle in training mode."
2597   (with-temp-buffer
2598     (let ((temp-buffer-name (buffer-name)))
2599       (save-excursion
2600         (goto-char (point-min))
2601         (dolist (article articles)
2602           (insert (spam-get-article-as-string article)))
2603         (let* ((arg (if (spam-xor unregister article-is-spam-p)
2604                         "-spam"
2605                       "-good"))
2606                (status
2607                 (apply 'call-process-region
2608                        (point-min) (point-max)
2609                        spam-spamoracle-binary
2610                        nil temp-buffer-name nil
2611                        (if spam-spamoracle-database
2612                            `("-f" ,spam-spamoracle-database
2613                              "add" ,arg)
2614                          `("add" ,arg)))))
2615           (unless (eq 0 status)
2616             (error "Error running spamoracle: %s" status)))))))
2617
2618 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2619   (spam-spamoracle-learn articles nil unregister))
2620
2621 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2622   (spam-spamoracle-learn-ham articles t))
2623
2624 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2625   (spam-spamoracle-learn articles t unregister))
2626
2627 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2628   (spam-spamoracle-learn-spam articles t))
2629
2630 ;;}}}
2631
2632 ;;{{{ SpamAssassin
2633 ;;; based mostly on the bogofilter code
2634 (defun spam-check-spamassassin-headers (&optional score)
2635   "Check the SpamAssassin headers for the classification of this message."
2636   (if score                             ; scoring mode
2637       (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2638         (when header
2639           (if (string-match spam-spamassassin-score-regexp header)
2640               (match-string 1 header)
2641             "0")))
2642     ;; spam detection mode
2643     (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2644           (when header                  ; return nil when no header
2645             (when (string-match spam-spamassassin-positive-spam-flag-header
2646                                 header)
2647               spam-split-group)))))
2648
2649 (defun spam-check-spamassassin (&optional score)
2650   "Check the SpamAssassin backend for the classification of this message."
2651   (let ((article-buffer-name (buffer-name)))
2652     (with-temp-buffer
2653       (let ((temp-buffer-name (buffer-name)))
2654         (with-current-buffer article-buffer-name
2655           (apply 'call-process-region
2656                  (point-min) (point-max) spam-assassin-program
2657                  nil temp-buffer-name nil spam-spamassassin-arguments))
2658         ;; check the return now (we're back in the temp buffer)
2659         (goto-char (point-min))
2660         (spam-check-spamassassin-headers score)))))
2661
2662 ;; return something sensible if the score can't be determined
2663 (defun spam-spamassassin-score (&optional recheck)
2664   "Get the SpamAssassin score"
2665   (interactive "P")
2666   (save-window-excursion
2667     (gnus-summary-show-article t)
2668     (set-buffer gnus-article-buffer)
2669     (let ((score (or (unless recheck
2670                        (spam-check-spamassassin-headers t))
2671                      (spam-check-spamassassin t))))
2672       (gnus-summary-show-article)
2673       (message "SpamAssassin score %s" score)
2674       (or score "0"))))
2675
2676 (defun spam-spamassassin-register-with-sa-learn (articles spam
2677                                                  &optional unregister)
2678   "Register articles with spamassassin's sa-learn as spam or non-spam."
2679   (if articles
2680       (let ((action (if unregister spam-sa-learn-unregister-switch
2681                       (if spam spam-sa-learn-spam-switch
2682                         spam-sa-learn-ham-switch)))
2683             (summary-buffer-name (buffer-name)))
2684         (with-temp-buffer
2685           ;; group the articles into mbox format
2686           (dolist (article articles)
2687             (let (article-string)
2688               (with-current-buffer summary-buffer-name
2689                 (setq article-string (spam-get-article-as-string article)))
2690               (when (stringp article-string)
2691                 (insert "From \n") ; mbox separator (sa-learn only checks the
2692                                    ; first five chars, so we can get away with
2693                                    ; a bogus line))
2694                 (insert article-string)
2695                 (insert "\n"))))
2696           ;; call sa-learn on all messages at the same time
2697           (apply 'call-process-region
2698                  (point-min) (point-max)
2699                  spam-sa-learn-program
2700                  nil nil nil "--mbox"
2701                  (if spam-sa-learn-rebuild
2702                      (list action)
2703                    `("--no-rebuild" ,action)))))))
2704
2705 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2706   (spam-spamassassin-register-with-sa-learn articles t unregister))
2707
2708 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2709   (spam-spamassassin-register-with-sa-learn articles nil unregister))
2710
2711 (defun spam-spamassassin-unregister-spam-routine (articles)
2712   (spam-spamassassin-register-with-sa-learn articles t t))
2713
2714 (defun spam-spamassassin-unregister-ham-routine (articles)
2715   (spam-spamassassin-register-with-sa-learn articles nil t))
2716
2717 ;;}}}
2718
2719 ;;{{{ Bsfilter
2720 ;;; based mostly on the bogofilter code
2721 (defun spam-check-bsfilter-headers (&optional score)
2722   (if score
2723       (or (nnmail-fetch-field spam-bsfilter-probability-header)
2724           "0")
2725     (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2726       (when header ; return nil when no header
2727         (when (string-match "YES" header)
2728           spam-split-group)))))
2729
2730 ;; return something sensible if the score can't be determined
2731 (defun spam-bsfilter-score (&optional recheck)
2732   "Get the Bsfilter spamicity score."
2733   (interactive "P")
2734   (save-window-excursion
2735     (gnus-summary-show-article t)
2736     (set-buffer gnus-article-buffer)
2737     (let ((score (or (unless recheck
2738                        (spam-check-bsfilter-headers t))
2739                      (spam-check-bsfilter t))))
2740       (gnus-summary-show-article)
2741       (message "Spamicity score %s" score)
2742       (or score "0"))))
2743
2744 (defun spam-check-bsfilter (&optional score)
2745   "Check the Bsfilter backend for the classification of this message."
2746   (let ((article-buffer-name (buffer-name))
2747         (dir spam-bsfilter-database-directory)
2748         return)
2749     (with-temp-buffer
2750       (let ((temp-buffer-name (buffer-name)))
2751         (with-current-buffer article-buffer-name
2752           (apply 'call-process-region
2753                  (point-min) (point-max)
2754                  spam-bsfilter-program
2755                  nil temp-buffer-name nil
2756                  "--pipe"
2757                  "--insert-flag"
2758                  "--insert-probability"
2759                  (when dir
2760                    (list "--homedir" dir))))
2761         (setq return (spam-check-bsfilter-headers score))))
2762     return))
2763
2764 (defun spam-bsfilter-register-with-bsfilter (articles
2765                                              spam
2766                                              &optional unregister)
2767   "Register an article, given as a string, as spam or non-spam."
2768   (dolist (article articles)
2769     (let ((article-string (spam-get-article-as-string article))
2770           (switch (if unregister
2771                       (if spam
2772                           spam-bsfilter-spam-strong-switch
2773                         spam-bsfilter-ham-strong-switch)
2774                     (if spam
2775                         spam-bsfilter-spam-switch
2776                       spam-bsfilter-ham-switch))))
2777       (when (stringp article-string)
2778         (with-temp-buffer
2779           (insert article-string)
2780           (apply 'call-process-region
2781                  (point-min) (point-max)
2782                  spam-bsfilter-program
2783                  nil nil nil switch
2784                  "--update"
2785                  (when spam-bsfilter-database-directory
2786                    (list "--homedir"
2787                          spam-bsfilter-database-directory))))))))
2788
2789 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2790   (spam-bsfilter-register-with-bsfilter articles t unregister))
2791
2792 (defun spam-bsfilter-unregister-spam-routine (articles)
2793   (spam-bsfilter-register-spam-routine articles t))
2794
2795 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2796   (spam-bsfilter-register-with-bsfilter articles nil unregister))
2797
2798 (defun spam-bsfilter-unregister-ham-routine (articles)
2799   (spam-bsfilter-register-ham-routine articles t))
2800
2801 ;;}}}
2802
2803 ;;{{{ CRM114 Mailfilter
2804 (defun spam-check-crm114-headers (&optional score)
2805   (let ((header (message-fetch-field spam-crm114-header)))
2806     (when header                        ; return nil when no header
2807       (if score                         ; scoring mode
2808           (if (string-match "( pR: \\([0-9.-]+\\)" header)
2809               (match-string 1 header)
2810             "0")
2811         ;; spam detection mode
2812         (when (string-match spam-crm114-positive-spam-header
2813                             header)
2814           spam-split-group)))))
2815
2816 ;; return something sensible if the score can't be determined
2817 (defun spam-crm114-score ()
2818   "Get the CRM114 Mailfilter pR."
2819   (interactive)
2820   (save-window-excursion
2821     (gnus-summary-show-article t)
2822     (set-buffer gnus-article-buffer)
2823     (let ((score (or (spam-check-crm114-headers t)
2824                      (spam-check-crm114 t))))
2825       (gnus-summary-show-article)
2826       (message "pR: %s" score)
2827       (or score "0"))))
2828
2829 (defun spam-check-crm114 (&optional score)
2830   "Check the CRM114 Mailfilter backend for the classification of this message."
2831   (let ((article-buffer-name (buffer-name))
2832         (db spam-crm114-database-directory)
2833         return)
2834     (with-temp-buffer
2835       (let ((temp-buffer-name (buffer-name)))
2836         (with-current-buffer article-buffer-name
2837           (apply 'call-process-region
2838                  (point-min) (point-max)
2839                  spam-crm114-program
2840                  nil temp-buffer-name nil
2841                  (when db (list (concat "--fileprefix=" db)))))
2842         (setq return (spam-check-crm114-headers score))))
2843     return))
2844
2845 (defun spam-crm114-register-with-crm114 (articles
2846                                          spam
2847                                          &optional unregister)
2848   "Register an article, given as a string, as spam or non-spam."
2849   (dolist (article articles)
2850     (let ((article-string (spam-get-article-as-string article))
2851           (db spam-crm114-database-directory)
2852           (switch (if unregister
2853                       (if spam
2854                           spam-crm114-spam-strong-switch
2855                         spam-crm114-ham-strong-switch)
2856                     (if spam
2857                         spam-crm114-spam-switch
2858                       spam-crm114-ham-switch))))
2859       (when (stringp article-string)
2860         (with-temp-buffer
2861           (insert article-string)
2862
2863           (apply 'call-process-region
2864                  (point-min) (point-max)
2865                  spam-crm114-program
2866                  nil nil nil
2867                  (when db (list switch (concat "--fileprefix=" db)))))))))
2868
2869 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2870   (spam-crm114-register-with-crm114 articles t unregister))
2871
2872 (defun spam-crm114-unregister-spam-routine (articles)
2873   (spam-crm114-register-spam-routine articles t))
2874
2875 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2876   (spam-crm114-register-with-crm114 articles nil unregister))
2877
2878 (defun spam-crm114-unregister-ham-routine (articles)
2879   (spam-crm114-register-ham-routine articles t))
2880
2881 ;;}}}
2882
2883 ;;}}}
2884
2885 ;;{{{ Hooks
2886
2887 ;;;###autoload
2888 (defun spam-initialize (&rest symbols)
2889   "Install the spam.el hooks and do other initialization.
2890 When SYMBOLS is given, set those variables to t.  This is so you
2891 can call `spam-initialize' before you set spam-use-* variables on
2892 explicitly, and matters only if you need the extra headers
2893 installed through `spam-necessary-extra-headers'."
2894   (interactive)
2895
2896   (dolist (var symbols)
2897     (set var t))
2898
2899   (dolist (header (spam-necessary-extra-headers))
2900     (add-to-list 'nnmail-extra-headers header)
2901     (add-to-list 'gnus-extra-headers header))
2902
2903   (setq spam-install-hooks t)
2904   ;; TODO: How do we redo this every time the `spam' face is customized?
2905   (push '((eq mark gnus-spam-mark) . spam)
2906         gnus-summary-highlight)
2907   ;; Add hooks for loading and saving the spam stats
2908   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2909   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2910   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2911   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2912   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2913   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2914   (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2915
2916 (defun spam-unload-hook ()
2917   "Uninstall the spam.el hooks."
2918   (interactive)
2919   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2920   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2921   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2922   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2923   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2924   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2925   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2926
2927 (add-hook 'spam-unload-hook 'spam-unload-hook)
2928
2929 (when spam-install-hooks
2930   (spam-initialize))
2931 ;;}}}
2932
2933 (provide 'spam)
2934
2935 ;;; spam.el ends here