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