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