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