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