0de7d23026097f84065bce9561ffaa29f51f550d
[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     (gnus-group-ham-exit-processor-ifile         ham spam-use-ifile)
734     (gnus-group-ham-exit-processor-bogofilter    ham spam-use-bogofilter)
735     (gnus-group-ham-exit-processor-bsfilter      ham spam-use-bsfilter)
736     (gnus-group-ham-exit-processor-stat          ham spam-use-stat)
737     (gnus-group-ham-exit-processor-whitelist     ham spam-use-whitelist)
738     (gnus-group-ham-exit-processor-BBDB          ham spam-use-BBDB)
739     (gnus-group-ham-exit-processor-copy          ham spam-use-ham-copy)
740     (gnus-group-ham-exit-processor-spamassassin  ham spam-use-spamassassin)
741     (gnus-group-ham-exit-processor-spamoracle    ham spam-use-spamoracle)
742     (nil ham spam-use-crm114))
743   "The `spam-list-of-processors' list.
744 This list contains pairs associating the obsolete ham/spam exit
745 processor variables with a classification and a spam-use-*
746 variable.  When the processor variable is nil, just the
747 classification and spam-use-* check variable are used.")
748
749 (defun spam-group-processor-p (group check &optional classification)
750   "Checks if GROUP has a CHECK with CLASSIFICATION registered.
751 Also accepts the obsolete processors, which can be found in
752 gnus.el and in spam-list-of-processors."
753   (if (and (stringp group)
754            (symbolp check))
755       (let ((old-style (assq check spam-list-of-processors))
756             (parameters (nth 0 (gnus-parameter-spam-process group)))
757             found)
758         (if old-style  ; old-style processor
759             (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
760           ;; now search for the parameter
761           (dolist (parameter parameters)
762             (when (and (null found)
763                        (listp parameter)
764                        (eq classification (nth 0 parameter))
765                        (eq check (nth 1 parameter)))
766               (setq found t)))
767           found))
768     nil))
769
770 (defun spam-report-articles-gmane (n)
771   "Report the current message as spam via Gmane.
772 Respects the process/prefix convention."
773   (interactive "P")
774   (dolist (article (gnus-summary-work-articles n))
775     (gnus-summary-remove-process-mark article)
776     (spam-report-gmane article)))
777
778 (defun spam-report-articles-resend (n)
779   "Report the current message as spam by resending it.
780 Respects the process/prefix convention.  Also see
781 `spam-report-resend-to'."
782   (interactive "P")
783   (let* ((gp
784           (gnus-parameter-spam-resend-to gnus-newsgroup-name))
785          (spam-report-resend-to (or (car-safe gp)
786                                     spam-report-resend-to))
787          (articles (gnus-summary-work-articles n)))
788     (spam-report-resend articles)
789     (dolist (article articles)
790       (gnus-summary-remove-process-mark article))))
791
792 (defun spam-necessary-extra-headers ()
793   "Return the extra headers spam.el thinks are necessary."
794   (let (list)
795     (when (or spam-use-spamassassin
796               spam-use-spamassassin-headers
797               spam-use-regex-headers)
798       (push 'X-Spam-Status list))
799     list))
800
801 (defun spam-user-format-function-S (headers)
802   (when headers
803     (spam-summary-score headers)))
804
805 (defun spam-article-sort-by-spam-status (h1 h2)
806   "Sort articles by score."
807   (let (result)
808     (dolist (header (spam-necessary-extra-headers))
809       (let ((s1 (spam-summary-score h1 header))
810             (s2 (spam-summary-score h2 header)))
811       (unless (= s1 s2)
812         (setq result (< s1 s2))
813         (return))))
814     result))
815
816 (defun spam-extra-header-to-number (header headers)
817   "Transform an extra header to a number."
818   (if (gnus-extra-header header headers)
819       (cond
820        ((eq header 'X-Spam-Status)
821         (string-to-number (gnus-replace-in-string
822                            (gnus-extra-header header headers)
823                            ".*hits=" "")))
824        ;; for CRM checking, it's probably faster to just do the string match
825        ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
826         (match-string 1 header))
827        (t nil))
828     nil))
829
830 (defun spam-summary-score (headers &optional specific-header)
831   "Score an article for the summary buffer, as fast as possible.
832 With SPECIFIC-HEADER, returns only that header's score.
833 Will not return a nil score."
834   (let (score)
835     (dolist (header 
836              (if specific-header
837                  (list specific-header)
838                (spam-necessary-extra-headers)))
839       (setq score 
840             (spam-extra-header-to-number header headers))
841       (when score 
842         (return)))
843     (or score 0)))
844
845 (defun spam-generic-score (&optional recheck)
846   "Invoke whatever scoring method we can."
847   (interactive "P")
848   (cond
849    ((or spam-use-spamassassin spam-use-spamassassin-headers)
850     (spam-spamassassin-score recheck))
851    ((or spam-use-bsfilter spam-use-bsfilter-headers)
852     (spam-bsfilter-score recheck))
853    (spam-use-crm114
854     (spam-crm114-score))
855    (t (spam-bogofilter-score recheck))))
856
857 ;;; Summary entry and exit processing.
858
859 (defun spam-summary-prepare ()
860   (setq spam-old-ham-articles
861         (spam-list-articles gnus-newsgroup-articles 'ham))
862   (setq spam-old-spam-articles
863         (spam-list-articles gnus-newsgroup-articles 'spam))
864   (spam-mark-junk-as-spam-routine))
865
866 ;; The spam processors are invoked for any group, spam or ham or neither
867 (defun spam-summary-prepare-exit ()
868   (unless gnus-group-is-exiting-without-update-p
869     (gnus-message 6 "Exiting summary buffer and applying spam rules")
870
871     ;; first of all, unregister any articles that are no longer ham or spam
872     ;; we have to iterate over the processors, or else we'll be too slow
873     (dolist (classification '(spam ham))
874       (let* ((old-articles (if (eq classification 'spam)
875                                spam-old-spam-articles
876                              spam-old-ham-articles))
877              (new-articles (spam-list-articles
878                             gnus-newsgroup-articles
879                             classification))
880              (changed-articles (spam-set-difference new-articles old-articles)))
881         ;; now that we have the changed articles, we go through the processors
882         (dolist (processor-param spam-list-of-processors)
883           (let ((processor (nth 0 processor-param))
884                 (processor-classification (nth 1 processor-param))
885                 (check (nth 2 processor-param))
886                 unregister-list)
887             (dolist (article changed-articles)
888               (let ((id (spam-fetch-field-message-id-fast article)))
889                 (when (spam-log-unregistration-needed-p
890                        id 'process classification check)
891                   (push article unregister-list))))
892             ;; call spam-register-routine with specific articles to unregister,
893             ;; when there are articles to unregister and the check is enabled
894             (when (and unregister-list (symbol-value check))
895               (spam-register-routine
896                classification check t unregister-list))))))
897
898     ;; find all the spam processors applicable to this group
899     (dolist (processor-param spam-list-of-processors)
900       (let ((processor (nth 0 processor-param))
901             (classification (nth 1 processor-param))
902             (check (nth 2 processor-param)))
903         (when (and (eq 'spam classification)
904                    (spam-group-processor-p gnus-newsgroup-name check classification))
905           (spam-register-routine classification check))))
906
907     (unless (and spam-move-spam-nonspam-groups-only
908                  (spam-group-spam-contents-p gnus-newsgroup-name))
909       (let* ((group (gnus-parameter-spam-process-destination
910                      gnus-newsgroup-name))
911              (num (spam-mark-spam-as-expired-and-move-routine group)))
912         (when (> num 0)
913           (gnus-message 6
914                         "%d spam messages are marked as expired%s."
915                         num
916                         (if group 
917                             (format " and moved it to %s" group)
918                           "")))))
919
920     ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
921     ;; expire spam, in case the above did not expire them
922     (let ((num (spam-mark-spam-as-expired-and-move-routine nil)))
923       (when (> num 0)
924         (gnus-message 6
925                       "%d spam messages were marked as expired."
926                       num)))
927
928     (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
929               (and (spam-group-spam-contents-p gnus-newsgroup-name)
930                    spam-process-ham-in-spam-groups)
931               spam-process-ham-in-nonham-groups)
932       ;; find all the ham processors applicable to this group
933       (dolist (processor-param spam-list-of-processors)
934         (let ((processor (nth 0 processor-param))
935               (classification (nth 1 processor-param))
936               (check (nth 2 processor-param)))
937           (when (and (eq 'ham classification)
938                      (spam-group-processor-p gnus-newsgroup-name check classification))
939             (spam-register-routine classification check)))))
940
941     (when (spam-group-processor-p gnus-newsgroup-name 'ham 'spam-use-ham-copy)
942       (let ((num
943              (spam-ham-copy-routine
944               (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
945         (when (> num 0)
946           (gnus-message 6 "%d ham messages were copied" num))))
947
948     ;; now move all ham articles out of spam groups
949     (when (spam-group-spam-contents-p gnus-newsgroup-name)
950       (let ((num
951              (spam-ham-move-routine
952               (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
953         (when (> num 0)
954           (gnus-message 6 "%d ham messages were moved from spam group" num)))))
955
956   (setq spam-old-ham-articles nil)
957   (setq spam-old-spam-articles nil))
958
959 (defun spam-set-difference (list1 list2)
960   "Return a set difference of LIST1 and LIST2.  
961 When either list is nil, the other is returned."
962   (if (and list1 list2)
963       ;; we have two non-nil lists
964       (progn
965         (dolist (item (append list1 list2))
966           (when (and (memq item list1) (memq item list2))
967             (setq list1 (delq item list1))
968             (setq list2 (delq item list2))))
969         (append list1 list2))
970     ;; if either of the lists was nil, return the other one
971     (if list1 list1 list2)))
972
973 (defun spam-mark-junk-as-spam-routine ()
974   ;; check the global list of group names spam-junk-mailgroups and the
975   ;; group parameters
976   (when (spam-group-spam-contents-p gnus-newsgroup-name)
977     (gnus-message 6 "Marking %s articles as spam"
978                   (if spam-mark-only-unseen-as-spam
979                       "unseen"
980                     "unread"))
981     (let ((articles (if spam-mark-only-unseen-as-spam
982                         gnus-newsgroup-unseen
983                       gnus-newsgroup-unreads)))
984       (if spam-mark-new-messages-in-spam-group-as-spam
985           (dolist (article articles)
986             (gnus-summary-mark-article article gnus-spam-mark))
987         (gnus-message 9 "Did not mark new messages as spam.")))))
988
989 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
990   (if (and (car-safe groups) (listp (car-safe groups)))
991       (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
992     (gnus-summary-kill-process-mark)
993     (let ((articles gnus-newsgroup-articles)
994           (backend-supports-deletions
995            (gnus-check-backend-function
996             'request-move-article gnus-newsgroup-name))
997           article tomove deletep)
998       (dolist (article articles)
999         (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
1000           (gnus-summary-mark-article article gnus-expirable-mark)
1001           (push article tomove)))
1002
1003       ;; now do the actual copies
1004       (dolist (group groups)
1005         (when (and tomove
1006                    (stringp group))
1007           (dolist (article tomove)
1008             (gnus-summary-set-process-mark article))
1009           (when tomove
1010             (if (or (not backend-supports-deletions)
1011                     (> (length groups) 1))
1012                 (progn
1013                   (gnus-summary-copy-article nil group)
1014                   (setq deletep t))
1015               (gnus-summary-move-article nil group)))))
1016
1017       ;; now delete the articles, if there was a copy done, and the
1018       ;; backend allows it
1019       (when (and deletep backend-supports-deletions)
1020         (dolist (article tomove)
1021           (gnus-summary-set-process-mark article))
1022         (when tomove
1023           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1024             (gnus-summary-delete-article nil))))
1025
1026       (gnus-summary-yank-process-mark)
1027       (length tomove))))
1028
1029 (defun spam-ham-copy-or-move-routine (copy groups)
1030   (gnus-summary-kill-process-mark)
1031   (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
1032         (backend-supports-deletions
1033          (gnus-check-backend-function
1034           'request-move-article gnus-newsgroup-name))
1035         (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1036         article mark deletep respool)
1037
1038     (when (member 'respool groups)
1039       (setq respool t)                  ; boolean for later
1040       (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1041
1042     ;; now do the actual move
1043     (dolist (group groups)
1044       (when (and todo (stringp group))
1045         (dolist (article todo)
1046           (when spam-mark-ham-unread-before-move-from-spam-group
1047             (gnus-summary-mark-article article gnus-unread-mark))
1048           (gnus-summary-set-process-mark article))
1049
1050         (if respool                        ; respooling is with a "fake" group
1051             (let ((spam-split-disabled
1052                    (or spam-split-disabled
1053                        spam-disable-spam-split-during-ham-respool)))
1054               (gnus-summary-respool-article nil respool-method))
1055           (if (or (not backend-supports-deletions) ; else, we are not respooling
1056                   (> (length groups) 1))
1057               (progn                ; if copying, copy and set deletep
1058                 (gnus-summary-copy-article nil group)
1059                 (setq deletep t))
1060             (gnus-summary-move-article nil group))))) ; else move articles
1061
1062     ;; now delete the articles, unless a) copy is t, and there was a copy done
1063     ;;                                 b) a move was done to a single group
1064     ;;                                 c) backend-supports-deletions is nil
1065     (unless copy
1066       (when (and deletep backend-supports-deletions)
1067         (dolist (article todo)
1068           (gnus-summary-set-process-mark article))
1069         (when todo
1070           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1071             (gnus-summary-delete-article nil)))))
1072
1073     (gnus-summary-yank-process-mark)
1074     (length todo)))
1075
1076 (defun spam-ham-copy-routine (&rest groups)
1077   (if (and (car-safe groups) (listp (car-safe groups)))
1078       (apply 'spam-ham-copy-routine (car groups))
1079     (spam-ham-copy-or-move-routine t groups)))
1080
1081 (defun spam-ham-move-routine (&rest groups)
1082   (if (and (car-safe groups) (listp (car-safe groups)))
1083       (apply 'spam-ham-move-routine (car groups))
1084     (spam-ham-copy-or-move-routine nil groups)))
1085
1086 (defun spam-get-article-as-string (article)
1087   (when (numberp article)
1088     (with-temp-buffer
1089       (gnus-request-article-this-buffer
1090        article
1091        gnus-newsgroup-name)
1092       (buffer-string))))
1093
1094 ;; disabled for now
1095 ;; (defun spam-get-article-as-filename (article)
1096 ;;   (let ((article-filename))
1097 ;;     (when (numberp article)
1098 ;;       (nnml-possibly-change-directory
1099 ;;        (gnus-group-real-name gnus-newsgroup-name))
1100 ;;       (setq article-filename (expand-file-name
1101 ;;                              (int-to-string article) nnml-current-directory)))
1102 ;;     (if (file-exists-p article-filename)
1103 ;;      article-filename
1104 ;;       nil)))
1105
1106 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1107   "Fetch a field quickly, using the internal gnus-data-list function"
1108   (when (numberp article)
1109     (let* ((data-header (or prepared-data-header
1110                             (spam-fetch-article-header article))))
1111       (if (arrayp data-header)
1112         (cond
1113          ((equal field 'from)
1114           (mail-header-from data-header))
1115          ((equal field 'message-id)
1116           (mail-header-message-id data-header))
1117          ((equal field 'subject)
1118           (mail-header-subject data-header))
1119          ((equal field 'references)
1120           (mail-header-references data-header))
1121          ((equal field 'date)
1122           (mail-header-date data-header))
1123          ((equal field 'xref)
1124           (mail-header-xref data-header))
1125          ((equal field 'extra)
1126           (mail-header-extra data-header))
1127          (t
1128           nil))
1129         (gnus-message 6 "Article %d has a nil data header" article)))))
1130
1131 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1132   (spam-fetch-field-fast article 'from prepared-data-header))
1133
1134 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1135   (spam-fetch-field-fast article 'subject prepared-data-header))
1136
1137 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1138   (spam-fetch-field-fast article 'message-id prepared-data-header))
1139
1140 (defun spam-generate-fake-headers (article)
1141   (let ((dh (spam-fetch-article-header article)))
1142     (if dh
1143         (concat
1144          (format 
1145           (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1146                   "Date: %s\nReferences: %s\nXref: %s\n")
1147           (spam-fetch-field-fast article 'from dh)
1148           (spam-fetch-field-fast article 'subject dh)
1149           (spam-fetch-field-fast article 'message-id dh)
1150           (spam-fetch-field-fast article 'date dh)
1151           (spam-fetch-field-fast article 'references dh)
1152           (spam-fetch-field-fast article 'xref dh))
1153          (when (spam-fetch-field-fast article 'extra dh)
1154            (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1155       (gnus-message
1156        5
1157        "spam-generate-fake-headers: article %d didn't have a valid header"
1158        article))))
1159
1160 (defun spam-fetch-article-header (article)
1161   (save-excursion
1162     (set-buffer gnus-summary-buffer)
1163     (gnus-read-header article)
1164     (nth 3 (assq article gnus-newsgroup-data))))
1165
1166 \f
1167 ;;;; Spam determination.
1168
1169 (defvar spam-list-of-checks
1170   '((spam-use-blacklist                 .       spam-check-blacklist)
1171     (spam-use-regex-headers             .       spam-check-regex-headers)
1172     (spam-use-gmane-xref                .       spam-check-gmane-xref)
1173     (spam-use-regex-body                .       spam-check-regex-body)
1174     (spam-use-whitelist                 .       spam-check-whitelist)
1175     (spam-use-BBDB                      .       spam-check-BBDB)
1176     (spam-use-BBDB-exclusive            .       spam-check-BBDB)
1177     (spam-use-ifile                     .       spam-check-ifile)
1178     (spam-use-spamoracle                .       spam-check-spamoracle)
1179     (spam-use-stat                      .       spam-check-stat)
1180     (spam-use-blackholes                .       spam-check-blackholes)
1181     (spam-use-hashcash                  .       spam-check-hashcash)
1182     (spam-use-spamassassin-headers      .       spam-check-spamassassin-headers)
1183     (spam-use-spamassassin              .       spam-check-spamassassin)
1184     (spam-use-bogofilter-headers        .       spam-check-bogofilter-headers)
1185     (spam-use-bogofilter                .       spam-check-bogofilter)
1186     (spam-use-bsfilter-headers          .       spam-check-bsfilter-headers)
1187     (spam-use-bsfilter                  .       spam-check-bsfilter)
1188     (spam-use-crm114                    .       spam-check-crm114))
1189   "The spam-list-of-checks list contains pairs associating a
1190 parameter variable with a spam checking function.  If the
1191 parameter variable is true, then the checking function is called,
1192 and its value decides what happens.  Each individual check may
1193 return nil, t, or a mailgroup name.  The value nil means that the
1194 check does not yield a decision, and so, that further checks are
1195 needed.  The value t means that the message is definitely not
1196 spam, and that further spam checks should be inhibited.
1197 Otherwise, a mailgroup name or the symbol 'spam (depending on
1198 spam-split-symbolic-return) is returned where the mail should go,
1199 and further checks are also inhibited.  The usual mailgroup name
1200 is the value of `spam-split-group', meaning that the message is
1201 definitely a spam.")
1202
1203 (defvar spam-list-of-statistical-checks
1204   '(spam-use-ifile
1205     spam-use-regex-body
1206     spam-use-stat
1207     spam-use-bogofilter
1208     spam-use-bsfilter
1209     spam-use-blackholes
1210     spam-use-spamassassin
1211     spam-use-spamoracle
1212     spam-use-crm114)
1213   "The spam-list-of-statistical-checks list contains all the mail
1214 splitters that need to have the full message body available.
1215 Note that you should fetch extra headers if you don't like this,
1216 e.g. fetch the 'Received' header for spam-use-blackholes.")
1217
1218 (defun spam-split (&rest specific-checks)
1219   "Split this message into the `spam' group if it is spam.
1220 This function can be used as an entry in the variable `nnmail-split-fancy',
1221 for example like this: (: spam-split).  It can take checks as
1222 parameters.  A string as a parameter will set the
1223 spam-split-group to that string.
1224
1225 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1226   (interactive)
1227   (setq spam-split-last-successful-check nil)
1228   (unless spam-split-disabled
1229     (let ((spam-split-group-choice spam-split-group))
1230       (dolist (check specific-checks)
1231         (when (stringp check)
1232           (setq spam-split-group-choice check)
1233           (setq specific-checks (delq check specific-checks))))
1234
1235       (let ((spam-split-group spam-split-group-choice))
1236         (save-excursion
1237           (save-restriction
1238             (dolist (check spam-list-of-statistical-checks)
1239               (when (and (symbolp check)
1240                          (or (symbol-value check)
1241                              (memq check specific-checks)))
1242                 (widen)
1243                 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1244                               (symbol-name check))
1245                 (return)))
1246             ;;   (progn (widen) (debug (buffer-string)))
1247             (let ((list-of-checks spam-list-of-checks)
1248                   decision)
1249               (while (and list-of-checks (not decision))
1250                 (let ((pair (pop list-of-checks)))
1251                   (when (or
1252                          ;; either, given specific checks, this is one of them
1253                          (and specific-checks (memq (car pair) specific-checks))
1254                          ;; or, given no specific checks, spam-use-CHECK is set
1255                          (and (null specific-checks) (symbol-value (car pair))))
1256                     (gnus-message 6 "spam-split: calling the %s function"
1257                                   (symbol-name (cdr pair)))
1258                     (setq decision (funcall (cdr pair)))
1259                     ;; if we got a decision at all, save the current check
1260                     (when decision
1261                       (setq spam-split-last-successful-check (car pair)))
1262
1263                     (when (eq decision 'spam)
1264                       (unless spam-split-symbolic-return
1265                         (gnus-error
1266                          5
1267                          (format "spam-split got %s but %s is nil"
1268                                  (symbol-name decision)
1269                                  (symbol-name spam-split-symbolic-return))))))))
1270               (if (eq decision t)
1271                   (if spam-split-symbolic-return-positive 'ham nil)
1272                 decision))))))))
1273
1274 (defun spam-find-spam ()
1275   "This function will detect spam in the current newsgroup using spam-split."
1276   (interactive)
1277
1278   (let* ((group gnus-newsgroup-name)
1279          (autodetect (gnus-parameter-spam-autodetect group))
1280          (methods (gnus-parameter-spam-autodetect-methods group))
1281          (first-method (nth 0 methods))
1282          (articles (if spam-autodetect-recheck-messages
1283                        gnus-newsgroup-articles
1284                      gnus-newsgroup-unseen))
1285          article-cannot-be-faked)
1286
1287     (dolist (check spam-list-of-statistical-checks)
1288       (when (and (symbolp check)
1289                  (memq check methods))
1290         (setq article-cannot-be-faked t)
1291         (return)))
1292
1293     (when (memq 'default methods)
1294       (setq article-cannot-be-faked t))
1295
1296     (when (and autodetect
1297                (not (equal first-method 'none)))
1298     (mapcar
1299      (lambda (article)
1300        (let ((id (spam-fetch-field-message-id-fast article))
1301              (subject (spam-fetch-field-subject-fast article))
1302              (sender (spam-fetch-field-from-fast article))
1303              registry-lookup)
1304          
1305          (unless id
1306            (gnus-message 6 "Article %d has no message ID!" article))
1307          
1308          (when (and id spam-log-to-registry)
1309            (setq registry-lookup (spam-log-registration-type id 'incoming))
1310            (when registry-lookup
1311              (gnus-message
1312               9
1313               "spam-find-spam: message %s was already registered incoming"
1314               id)))
1315
1316          (let* ((spam-split-symbolic-return t)
1317                 (spam-split-symbolic-return-positive t)
1318                 (fake-headers (spam-generate-fake-headers article))
1319                 (split-return
1320                  (or registry-lookup
1321                      (with-temp-buffer
1322                        (if article-cannot-be-faked
1323                            (gnus-request-article-this-buffer
1324                             article
1325                             group)
1326                          ;; else, we fake the article
1327                          (when fake-headers (insert fake-headers)))
1328                        (if (or (null first-method)
1329                                (equal first-method 'default))
1330                            (spam-split)
1331                          (apply 'spam-split methods))))))
1332            (if (equal split-return 'spam)
1333                (gnus-summary-mark-article article gnus-spam-mark))
1334            
1335            (when (and id split-return spam-log-to-registry)
1336              (when (zerop (gnus-registry-group-count id))
1337                (gnus-registry-add-group
1338                 id group subject sender))
1339                
1340              (unless registry-lookup
1341                (spam-log-processing-to-registry
1342                 id
1343                 'incoming
1344                 split-return
1345                 spam-split-last-successful-check
1346                 group))))))
1347     articles))))
1348
1349 (defvar spam-registration-functions
1350   ;; first the ham register, second the spam register function
1351   ;; third the ham unregister, fourth the spam unregister function
1352   '((spam-use-blacklist  nil
1353                          spam-blacklist-register-routine
1354                          nil
1355                          spam-blacklist-unregister-routine)
1356     (spam-use-whitelist  spam-whitelist-register-routine
1357                          nil
1358                          spam-whitelist-unregister-routine
1359                          nil)
1360     (spam-use-ham-copy   nil
1361                          nil
1362                          nil
1363                          nil)
1364     (spam-use-BBDB       spam-BBDB-register-routine
1365                          nil
1366                          spam-BBDB-unregister-routine
1367                          nil)
1368     (spam-use-ifile      spam-ifile-register-ham-routine
1369                          spam-ifile-register-spam-routine
1370                          spam-ifile-unregister-ham-routine
1371                          spam-ifile-unregister-spam-routine)
1372     (spam-use-spamoracle spam-spamoracle-learn-ham
1373                          spam-spamoracle-learn-spam
1374                          spam-spamoracle-unlearn-ham
1375                          spam-spamoracle-unlearn-spam)
1376     (spam-use-stat       spam-stat-register-ham-routine
1377                          spam-stat-register-spam-routine
1378                          spam-stat-unregister-ham-routine
1379                          spam-stat-unregister-spam-routine)
1380     ;; note that spam-use-gmane and spam-use-resend are not legitimate checks
1381     (spam-use-gmane      nil
1382                          spam-report-gmane-register-routine
1383                          ;; does Gmane support unregistration?
1384                          nil
1385                          nil)
1386     (spam-use-resend     nil
1387                          spam-report-resend-register-routine
1388                          nil
1389                          nil)
1390     (spam-use-spamassassin spam-spamassassin-register-ham-routine
1391                            spam-spamassassin-register-spam-routine
1392                            spam-spamassassin-unregister-ham-routine
1393                            spam-spamassassin-unregister-spam-routine)
1394     (spam-use-bogofilter spam-bogofilter-register-ham-routine
1395                          spam-bogofilter-register-spam-routine
1396                          spam-bogofilter-unregister-ham-routine
1397                          spam-bogofilter-unregister-spam-routine)
1398     (spam-use-bsfilter   spam-bsfilter-register-ham-routine
1399                          spam-bsfilter-register-spam-routine
1400                          spam-bsfilter-unregister-ham-routine
1401                          spam-bsfilter-unregister-spam-routine)
1402     (spam-use-crm114 spam-crm114-register-ham-routine
1403                      spam-crm114-register-spam-routine
1404                      ;; does CRM114 Mailfilter support unregistration?
1405                      nil
1406                      nil))
1407   "The spam-registration-functions list contains pairs
1408 associating a parameter variable with the ham and spam
1409 registration functions, and the ham and spam unregistration
1410 functions")
1411
1412 (defun spam-classification-valid-p (classification)
1413   (or  (eq classification 'spam)
1414        (eq classification 'ham)))
1415
1416 (defun spam-process-type-valid-p (process-type)
1417   (or  (eq process-type 'incoming)
1418        (eq process-type 'process)))
1419
1420 (defun spam-registration-check-valid-p (check)
1421   (assoc check spam-registration-functions))
1422
1423 (defun spam-unregistration-check-valid-p (check)
1424   (assoc check spam-registration-functions))
1425
1426 (defun spam-registration-function (classification check)
1427   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1428     (if (eq classification 'spam)
1429         (nth 1 flist)
1430       (nth 0 flist))))
1431
1432 (defun spam-unregistration-function (classification check)
1433   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1434     (if (eq classification 'spam)
1435         (nth 3 flist)
1436       (nth 2 flist))))
1437
1438 (defun spam-list-articles (articles classification)
1439   (let ((mark-check (if (eq classification 'spam)
1440                         'spam-group-spam-mark-p
1441                       'spam-group-ham-mark-p))
1442         alist mark-cache-yes mark-cache-no)
1443     (dolist (article articles)
1444       (let ((mark (gnus-summary-article-mark article)))
1445         (unless (or (memq mark mark-cache-yes)
1446                     (memq mark mark-cache-no))
1447           (if (funcall mark-check
1448                        gnus-newsgroup-name
1449                        mark)
1450               (push mark mark-cache-yes)
1451             (push mark mark-cache-no)))
1452         (when (memq mark mark-cache-yes)
1453           (push article alist))))
1454     alist))
1455
1456 (defun spam-register-routine (classification
1457                               check
1458                               &optional unregister
1459                               specific-articles)
1460   (when (and (spam-classification-valid-p classification)
1461              (spam-registration-check-valid-p check))
1462     (let* ((register-function
1463             (spam-registration-function classification check))
1464            (unregister-function
1465             (spam-unregistration-function classification check))
1466            (run-function (if unregister
1467                              unregister-function
1468                            register-function))
1469            (log-function (if unregister
1470                              'spam-log-undo-registration
1471                            'spam-log-processing-to-registry))
1472            article articles)
1473
1474       (when run-function
1475         ;; make list of articles, using specific-articles if given
1476         (setq articles (or specific-articles
1477                            (spam-list-articles
1478                             gnus-newsgroup-articles
1479                             classification)))
1480         ;; process them
1481         (when (> (length articles) 0)
1482           (gnus-message 5 "%s %d %s articles as %s using backend %s"
1483                         (if unregister "Unregistering" "Registering")
1484                         (length articles)
1485                         (if specific-articles "specific" "")
1486                         (symbol-name classification)
1487                         (symbol-name check))
1488           (funcall run-function articles)
1489           ;; now log all the registrations (or undo them, depending on unregister)
1490           (dolist (article articles)
1491             (funcall log-function
1492                      (spam-fetch-field-message-id-fast article)
1493                      'process
1494                      classification
1495                      check
1496                      gnus-newsgroup-name)))))))
1497
1498 ;;; log a ham- or spam-processor invocation to the registry
1499 (defun spam-log-processing-to-registry (id type classification check group)
1500   (when spam-log-to-registry
1501     (if (and (stringp id)
1502              (stringp group)
1503              (spam-process-type-valid-p type)
1504              (spam-classification-valid-p classification)
1505              (spam-registration-check-valid-p check))
1506         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1507               (cell (list classification check group)))
1508           (push cell cell-list)
1509           (gnus-registry-store-extra-entry
1510            id
1511            type
1512            cell-list))
1513
1514       (gnus-message 
1515        5 
1516        (format "%s call with bad ID, type, classification, spam-check, or group"
1517                "spam-log-processing-to-registry")))))
1518
1519 ;;; check if a ham- or spam-processor registration has been done
1520 (defun spam-log-registered-p (id type)
1521   (when spam-log-to-registry
1522     (if (and (stringp id)
1523              (spam-process-type-valid-p type))
1524         (cdr-safe (gnus-registry-fetch-extra id type))
1525       (progn
1526         (gnus-message
1527          5 
1528          (format "%s called with bad ID, type, classification, or spam-check"
1529                  "spam-log-registered-p"))
1530         nil))))
1531
1532 ;;; check what a ham- or spam-processor registration says
1533 ;;; returns nil if conflicting registrations are found
1534 (defun spam-log-registration-type (id type)
1535   (let ((count 0)
1536         decision)
1537     (dolist (reg (spam-log-registered-p id type))
1538       (let ((classification (nth 0 reg)))
1539         (when (spam-classification-valid-p classification)
1540           (when (and decision
1541                      (not (eq classification decision)))
1542             (setq count (+ 1 count)))
1543           (setq decision classification))))
1544     (if (< 0 count)
1545         nil
1546       decision)))
1547
1548
1549 ;;; check if a ham- or spam-processor registration needs to be undone
1550 (defun spam-log-unregistration-needed-p (id type classification check)
1551   (when spam-log-to-registry
1552     (if (and (stringp id)
1553              (spam-process-type-valid-p type)
1554              (spam-classification-valid-p classification)
1555              (spam-registration-check-valid-p check))
1556         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1557               found)
1558           (dolist (cell cell-list)
1559             (unless found
1560               (when (and (eq classification (nth 0 cell))
1561                          (eq check (nth 1 cell)))
1562                 (setq found t))))
1563           found)
1564       (progn
1565         (gnus-message
1566          5 
1567          (format "%s called with bad ID, type, classification, or spam-check"
1568                  "spam-log-unregistration-needed-p"))
1569         nil))))
1570
1571
1572 ;;; undo a ham- or spam-processor registration (the group is not used)
1573 (defun spam-log-undo-registration (id type classification check &optional group)
1574   (when (and spam-log-to-registry
1575              (spam-log-unregistration-needed-p id type classification check))
1576     (if (and (stringp id)
1577              (spam-process-type-valid-p type)
1578              (spam-classification-valid-p classification)
1579              (spam-registration-check-valid-p check))
1580         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1581               new-cell-list found)
1582           (dolist (cell cell-list)
1583             (unless (and (eq classification (nth 0 cell))
1584                          (eq check (nth 1 cell)))
1585               (push cell new-cell-list)))
1586           (gnus-registry-store-extra-entry
1587            id
1588            type
1589            new-cell-list))
1590       (progn
1591         (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1592                                 "spam-log-undo-registration"))
1593         nil))))
1594
1595 ;;; set up IMAP widening if it's necessary
1596 (defun spam-setup-widening ()
1597   (dolist (check spam-list-of-statistical-checks)
1598     (when (symbol-value check)
1599       (setq nnimap-split-download-body-default t))))
1600
1601 \f
1602 ;;;; Gmane xrefs
1603 (defun spam-check-gmane-xref ()
1604   (let ((header (or
1605                  (message-fetch-field "Xref")
1606                  (message-fetch-field "Newsgroups")))
1607         (spam-split-group (if spam-split-symbolic-return
1608                               'spam
1609                             spam-split-group)))
1610     (when header                        ; return nil when no header
1611       (when (string-match spam-gmane-xref-spam-group
1612                           header)
1613           spam-split-group))))
1614
1615 \f
1616 ;;;; Regex body
1617
1618 (defun spam-check-regex-body ()
1619   (let ((spam-regex-headers-ham spam-regex-body-ham)
1620         (spam-regex-headers-spam spam-regex-body-spam))
1621     (spam-check-regex-headers t)))
1622
1623 \f
1624 ;;;; Regex headers
1625
1626 (defun spam-check-regex-headers (&optional body)
1627   (let ((type (if body "body" "header"))
1628         (spam-split-group (if spam-split-symbolic-return
1629                               'spam
1630                             spam-split-group))
1631         ret found)
1632     (dolist (h-regex spam-regex-headers-ham)
1633       (unless found
1634         (goto-char (point-min))
1635         (when (re-search-forward h-regex nil t)
1636           (message "Ham regex %s search positive." type)
1637           (setq found t))))
1638     (dolist (s-regex spam-regex-headers-spam)
1639       (unless found
1640         (goto-char (point-min))
1641         (when (re-search-forward s-regex nil t)
1642           (message "Spam regex %s search positive." type)
1643           (setq found t)
1644           (setq ret spam-split-group))))
1645     ret))
1646
1647 \f
1648 ;;;; Blackholes.
1649
1650 (defun spam-reverse-ip-string (ip)
1651   (when (stringp ip)
1652     (mapconcat 'identity
1653                (nreverse (split-string ip "\\."))
1654                ".")))
1655
1656 (defun spam-check-blackholes ()
1657   "Check the Received headers for blackholed relays."
1658   (let ((headers (message-fetch-field "received"))
1659         (spam-split-group (if spam-split-symbolic-return
1660                               'spam
1661                             spam-split-group))
1662         ips matches)
1663     (when headers
1664       (with-temp-buffer
1665         (insert headers)
1666         (goto-char (point-min))
1667         (gnus-message 6 "Checking headers for relay addresses")
1668         (while (re-search-forward
1669                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1670           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1671           (push (spam-reverse-ip-string (match-string 1))
1672                 ips)))
1673       (dolist (server spam-blackhole-servers)
1674         (dolist (ip ips)
1675           (unless (and spam-blackhole-good-server-regex
1676                        ;; match the good-server-regex against the reversed (again) IP string
1677                        (string-match
1678                         spam-blackhole-good-server-regex
1679                         (spam-reverse-ip-string ip)))
1680             (unless matches
1681               (let ((query-string (concat ip "." server)))
1682                 (if spam-use-dig
1683                     (let ((query-result (query-dig query-string)))
1684                       (when query-result
1685                         (gnus-message 6 "(DIG): positive blackhole check '%s'"
1686                                       query-result)
1687                         (push (list ip server query-result)
1688                               matches)))
1689                   ;; else, if not using dig.el
1690                   (when (query-dns query-string)
1691                     (gnus-message 6 "positive blackhole check")
1692                     (push (list ip server (query-dns query-string 'TXT))
1693                           matches)))))))))
1694     (when matches
1695       spam-split-group)))
1696 \f
1697 ;;;; Hashcash.
1698
1699 (condition-case nil
1700     (progn
1701       (require 'hashcash)
1702
1703       (defun spam-check-hashcash ()
1704         "Check the headers for hashcash payments."
1705         (mail-check-payment)))   ;mail-check-payment returns a boolean
1706
1707   (file-error (progn
1708                 (defalias 'mail-check-payment 'ignore)
1709                 (defalias 'spam-check-hashcash 'ignore))))
1710 \f
1711 ;;;; BBDB
1712
1713 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1714 ;;; <sacha@giotto.sj.ru>
1715
1716 ;; all this is done inside a condition-case to trap errors
1717
1718 (condition-case nil
1719     (progn
1720       (require 'bbdb)
1721       (require 'bbdb-com)
1722
1723       ;; when the BBDB changes, we want to clear out our cache
1724       (defun spam-clear-cache-BBDB (&rest immaterial)
1725         (spam-clear-cache 'spam-use-BBDB))
1726
1727       (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1728
1729       (defun spam-enter-ham-BBDB (addresses &optional remove)
1730         "Enter an address into the BBDB; implies ham (non-spam) sender"
1731         (dolist (from addresses)
1732           (when (stringp from)
1733             (let* ((parsed-address (gnus-extract-address-components from))
1734                    (name (or (nth 0 parsed-address) "Ham Sender"))
1735                    (remove-function (if remove
1736                                         'bbdb-delete-record-internal
1737                                       'ignore))
1738                    (net-address (nth 1 parsed-address))
1739                    (record (and net-address
1740                                 (bbdb-search-simple nil net-address))))
1741               (when net-address
1742                 (gnus-message 6 "%s address %s %s BBDB"
1743                               (if remove "Deleting" "Adding")
1744                               from
1745                               (if remove "from" "to"))
1746                 (if record
1747                     (funcall remove-function record)
1748                   (bbdb-create-internal name nil net-address nil nil
1749                                         "ham sender added by spam.el")))))))
1750
1751       (defun spam-BBDB-register-routine (articles &optional unregister)
1752         (let (addresses)
1753           (dolist (article articles)
1754             (when (stringp (spam-fetch-field-from-fast article))
1755               (push (spam-fetch-field-from-fast article) addresses)))
1756           ;; now do the register/unregister action
1757           (spam-enter-ham-BBDB addresses unregister)))
1758
1759       (defun spam-BBDB-unregister-routine (articles)
1760         (spam-BBDB-register-routine articles t))
1761
1762       (defun spam-check-BBDB ()
1763         "Mail from people in the BBDB is classified as ham or non-spam"
1764         (let ((who (message-fetch-field "from"))
1765               (spam-split-group (if spam-split-symbolic-return
1766                                     'spam
1767                                   spam-split-group))
1768               bbdb-cache bbdb-hashtable)
1769           (when spam-cache-lookups
1770             (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1771             (unless bbdb-cache
1772               (setq bbdb-cache
1773                     ;; this is the expanded (bbdb-hashtable) macro
1774                     ;; without the debugging support
1775                     (with-current-buffer (bbdb-buffer)
1776                       (save-excursion
1777                         (save-window-excursion
1778                           (bbdb-records nil t)
1779                           bbdb-hashtable))))
1780               (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1781           (when who
1782             (setq who (nth 1 (gnus-extract-address-components who)))
1783             (if
1784                 (if spam-cache-lookups
1785                     (symbol-value
1786                      (intern-soft who bbdb-cache))
1787                   (bbdb-search-simple nil who))
1788                 t
1789               (if spam-use-BBDB-exclusive
1790                   spam-split-group
1791                 nil))))))
1792
1793   (file-error (progn
1794                 (defalias 'bbdb-search-simple 'ignore)
1795                 (defalias 'bbdb-records 'ignore)
1796                 (defalias 'bbdb-buffer 'ignore)
1797                 (defalias 'spam-check-BBDB 'ignore)
1798                 (defalias 'spam-BBDB-register-routine 'ignore)
1799                 (defalias 'spam-enter-ham-BBDB 'ignore)
1800                 (defalias 'bbdb-create-internal 'ignore)
1801                 (defalias 'bbdb-delete-record-internal 'ignore)
1802                 (defalias 'bbdb-records 'ignore))))
1803
1804 \f
1805 ;;;; ifile
1806
1807 ;;; check the ifile backend; return nil if the mail was NOT classified
1808 ;;; as spam
1809
1810 (defun spam-get-ifile-database-parameter ()
1811   "Get the command-line parameter for ifile's database from
1812   spam-ifile-database-path."
1813   (if spam-ifile-database-path
1814       (format "--db-file=%s" spam-ifile-database-path)
1815     nil))
1816
1817 (defun spam-check-ifile ()
1818   "Check the ifile backend for the classification of this message."
1819   (let ((article-buffer-name (buffer-name))
1820         (spam-split-group (if spam-split-symbolic-return
1821                               'spam
1822                             spam-split-group))
1823         category return)
1824     (with-temp-buffer
1825       (let ((temp-buffer-name (buffer-name))
1826             (db-param (spam-get-ifile-database-parameter)))
1827         (save-excursion
1828           (set-buffer article-buffer-name)
1829           (apply 'call-process-region
1830                  (point-min) (point-max) spam-ifile-path
1831                  nil temp-buffer-name nil "-c"
1832                  (if db-param `(,db-param "-q") `("-q"))))
1833         ;; check the return now (we're back in the temp buffer)
1834         (goto-char (point-min))
1835         (if (not (eobp))
1836             (setq category (buffer-substring (point) (point-at-eol))))
1837         (when (not (zerop (length category))) ; we need a category here
1838           (if spam-ifile-all-categories
1839               (setq return category)
1840             ;; else, if spam-ifile-all-categories is not set...
1841             (when (string-equal spam-ifile-spam-category category)
1842               (setq return spam-split-group)))))) ; note return is nil otherwise
1843     return))
1844
1845 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1846   "Register an article, given as a string, with a category.
1847 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1848   (let ((category (or category gnus-newsgroup-name))
1849         (add-or-delete-option (if unregister "-d" "-i"))
1850         (db (spam-get-ifile-database-parameter))
1851         parameters)
1852     (with-temp-buffer
1853       (dolist (article articles)
1854         (let ((article-string (spam-get-article-as-string article)))
1855           (when (stringp article-string)
1856             (insert article-string))))
1857       (apply 'call-process-region
1858              (point-min) (point-max) spam-ifile-path
1859              nil nil nil
1860              add-or-delete-option category
1861              (if db `(,db "-h") `("-h"))))))
1862
1863 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1864   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1865
1866 (defun spam-ifile-unregister-spam-routine (articles)
1867   (spam-ifile-register-spam-routine articles t))
1868
1869 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1870   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1871
1872 (defun spam-ifile-unregister-ham-routine (articles)
1873   (spam-ifile-register-ham-routine articles t))
1874
1875 \f
1876 ;;;; spam-stat
1877
1878 (condition-case nil
1879     (progn
1880       (let ((spam-stat-install-hooks nil))
1881         (require 'spam-stat))
1882
1883       (defun spam-check-stat ()
1884         "Check the spam-stat backend for the classification of this message"
1885         (let ((spam-split-group (if spam-split-symbolic-return
1886                                     'spam
1887                                   spam-split-group))
1888               (spam-stat-split-fancy-spam-group spam-split-group) ; override
1889               (spam-stat-buffer (buffer-name)) ; stat the current buffer
1890               category return)
1891           (spam-stat-split-fancy)))
1892
1893       (defun spam-stat-register-spam-routine (articles &optional unregister)
1894         (dolist (article articles)
1895           (let ((article-string (spam-get-article-as-string article)))
1896             (with-temp-buffer
1897               (insert article-string)
1898               (if unregister
1899                   (spam-stat-buffer-change-to-non-spam)
1900               (spam-stat-buffer-is-spam))))))
1901
1902       (defun spam-stat-unregister-spam-routine (articles)
1903         (spam-stat-register-spam-routine articles t))
1904
1905       (defun spam-stat-register-ham-routine (articles &optional unregister)
1906         (dolist (article articles)
1907           (let ((article-string (spam-get-article-as-string article)))
1908             (with-temp-buffer
1909               (insert article-string)
1910               (if unregister
1911                   (spam-stat-buffer-change-to-spam)
1912               (spam-stat-buffer-is-non-spam))))))
1913
1914       (defun spam-stat-unregister-ham-routine (articles)
1915         (spam-stat-register-ham-routine articles t))
1916
1917       (defun spam-maybe-spam-stat-load ()
1918         (when spam-use-stat (spam-stat-load)))
1919
1920       (defun spam-maybe-spam-stat-save ()
1921         (when spam-use-stat (spam-stat-save))))
1922
1923   (file-error (progn
1924                 (defalias 'spam-stat-load 'ignore)
1925                 (defalias 'spam-stat-save 'ignore)
1926                 (defalias 'spam-maybe-spam-stat-load 'ignore)
1927                 (defalias 'spam-maybe-spam-stat-save 'ignore)
1928                 (defalias 'spam-stat-register-ham-routine 'ignore)
1929                 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1930                 (defalias 'spam-stat-register-spam-routine 'ignore)
1931                 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1932                 (defalias 'spam-stat-buffer-is-spam 'ignore)
1933                 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1934                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1935                 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1936                 (defalias 'spam-stat-split-fancy 'ignore)
1937                 (defalias 'spam-check-stat 'ignore))))
1938
1939 \f
1940
1941 ;;;; Blacklists and whitelists.
1942
1943 (defvar spam-whitelist-cache nil)
1944 (defvar spam-blacklist-cache nil)
1945
1946 (defun spam-kill-whole-line ()
1947   (beginning-of-line)
1948   (let ((kill-whole-line t))
1949     (kill-line)))
1950
1951 ;;; address can be a list, too
1952 (defun spam-enter-whitelist (address &optional remove)
1953   "Enter ADDRESS (list or single) into the whitelist.
1954 With a non-nil REMOVE, remove them."
1955   (interactive "sAddress: ")
1956   (spam-enter-list address spam-whitelist remove)
1957   (setq spam-whitelist-cache nil)
1958   (spam-clear-cache 'spam-use-whitelist))
1959
1960 ;;; address can be a list, too
1961 (defun spam-enter-blacklist (address &optional remove)
1962   "Enter ADDRESS (list or single) into the blacklist.
1963 With a non-nil REMOVE, remove them."
1964   (interactive "sAddress: ")
1965   (spam-enter-list address spam-blacklist remove)
1966   (setq spam-blacklist-cache nil)
1967   (spam-clear-cache 'spam-use-whitelist))
1968
1969 (defun spam-enter-list (addresses file &optional remove)
1970   "Enter ADDRESSES into the given FILE.
1971 Either the whitelist or the blacklist files can be used.  With
1972 REMOVE not nil, remove the ADDRESSES."
1973   (if (stringp addresses)
1974       (spam-enter-list (list addresses) file remove)
1975     ;; else, we have a list of addresses here
1976     (unless (file-exists-p (file-name-directory file))
1977       (make-directory (file-name-directory file) t))
1978     (save-excursion
1979       (set-buffer
1980        (find-file-noselect file))
1981       (dolist (a addresses)
1982         (when (stringp a)
1983           (goto-char (point-min))
1984           (if (re-search-forward (regexp-quote a) nil t)
1985               ;; found the address
1986               (when remove
1987                 (spam-kill-whole-line))
1988             ;; else, the address was not found
1989             (unless remove
1990               (goto-char (point-max))
1991               (unless (bobp)
1992                 (insert "\n"))
1993               (insert a "\n")))))
1994       (save-buffer))))
1995
1996 (defun spam-filelist-build-cache (type)
1997   (let ((cache (if (eq type 'spam-use-blacklist)
1998                    spam-blacklist-cache
1999                  spam-whitelist-cache))
2000         parsed-cache)
2001     (unless (gethash type spam-caches)
2002       (while cache
2003         (let ((address (pop cache)))
2004           (unless (zerop (length address)) ; 0 for a nil address too
2005             (setq address (regexp-quote address))
2006             ;; fix regexp-quote's treatment of user-intended regexes
2007             (while (string-match "\\\\\\*" address)
2008               (setq address (replace-match ".*" t t address))))
2009           (push address parsed-cache)))
2010       (puthash type parsed-cache spam-caches))))
2011
2012 (defun spam-filelist-check-cache (type from)
2013   (when (stringp from)
2014     (spam-filelist-build-cache type)
2015     (let (found)
2016       (dolist (address (gethash type spam-caches))
2017         (when (and address (string-match address from))
2018           (setq found t)
2019           (return)))
2020       found)))
2021
2022 ;;; returns t if the sender is in the whitelist, nil or
2023 ;;; spam-split-group otherwise
2024 (defun spam-check-whitelist ()
2025   ;; FIXME!  Should it detect when file timestamps change?
2026   (let ((spam-split-group (if spam-split-symbolic-return
2027                               'spam
2028                             spam-split-group)))
2029     (unless spam-whitelist-cache
2030       (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2031     (if (spam-from-listed-p 'spam-use-whitelist)
2032         t
2033       (if spam-use-whitelist-exclusive
2034           spam-split-group
2035         nil))))
2036
2037 (defun spam-check-blacklist ()
2038   ;; FIXME!  Should it detect when file timestamps change?
2039   (let ((spam-split-group (if spam-split-symbolic-return
2040                               'spam
2041                             spam-split-group)))
2042     (unless spam-blacklist-cache
2043       (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2044     (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
2045
2046 (defun spam-parse-list (file)
2047   (when (file-readable-p file)
2048     (let (contents address)
2049       (with-temp-buffer
2050         (insert-file-contents file)
2051         (while (not (eobp))
2052           (setq address (buffer-substring (point) (point-at-eol)))
2053           (forward-line 1)
2054           ;; insert the e-mail address if detected, otherwise the raw data
2055           (unless (zerop (length address))
2056             (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2057               (push (or pure-address address) contents)))))
2058       (nreverse contents))))
2059
2060 (defun spam-from-listed-p (type)
2061   (let ((from (message-fetch-field "from"))
2062         found)
2063     (spam-filelist-check-cache type from)))
2064
2065 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2066   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2067         (declassification (if blacklist 'ham 'spam))
2068         (enter-function
2069          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2070         (remove-function
2071          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2072         from addresses unregister-list article-unregister-list)
2073     (dolist (article articles)
2074       (let ((from (spam-fetch-field-from-fast article))
2075             (id (spam-fetch-field-message-id-fast article))
2076             sender-ignored)
2077         (when (stringp from)
2078           (dolist (ignore-regex spam-blacklist-ignored-regexes)
2079             (when (and (not sender-ignored)
2080                        (stringp ignore-regex)
2081                        (string-match ignore-regex from))
2082               (setq sender-ignored t)))
2083           ;; remember the messages we need to unregister, unless remove is set
2084           (when (and
2085                  (null unregister)
2086                  (spam-log-unregistration-needed-p
2087                   id 'process declassification de-symbol))
2088             (push article article-unregister-list)
2089             (push from unregister-list))
2090           (unless sender-ignored
2091             (push from addresses)))))
2092
2093     (if unregister
2094         (funcall enter-function addresses t) ; unregister all these addresses
2095       ;; else, register normally and unregister what we need to
2096       (funcall remove-function unregister-list t)
2097       (dolist (article article-unregister-list)
2098         (spam-log-undo-registration
2099          (spam-fetch-field-message-id-fast article)
2100          'process
2101          declassification
2102          de-symbol))
2103       (funcall enter-function addresses nil))))
2104
2105 (defun spam-blacklist-unregister-routine (articles)
2106   (spam-blacklist-register-routine articles t))
2107
2108 (defun spam-blacklist-register-routine (articles &optional unregister)
2109   (spam-filelist-register-routine articles t unregister))
2110
2111 (defun spam-whitelist-unregister-routine (articles)
2112   (spam-whitelist-register-routine articles t))
2113
2114 (defun spam-whitelist-register-routine (articles &optional unregister)
2115   (spam-filelist-register-routine articles nil unregister))
2116
2117 \f
2118 ;;;; Spam-report glue
2119 (defun spam-report-gmane-register-routine (articles)
2120   (when articles
2121     (apply 'spam-report-gmane articles)))
2122
2123 (defun spam-report-resend-register-routine (articles)
2124   (let* ((resend-to-gp (gnus-parameter-spam-resend-to gnus-newsgroup-name))
2125          (spam-report-resend-to (or (car-safe resend-to-gp)
2126                                     spam-report-resend-to)))
2127     (spam-report-resend articles)))
2128
2129 \f
2130 ;;;; Bogofilter
2131 (defun spam-check-bogofilter-headers (&optional score)
2132   (let ((header (message-fetch-field spam-bogofilter-header))
2133         (spam-split-group (if spam-split-symbolic-return
2134                               'spam
2135                             spam-split-group)))
2136     (when header                        ; return nil when no header
2137       (if score                         ; scoring mode
2138           (if (string-match "spamicity=\\([0-9.]+\\)" header)
2139               (match-string 1 header)
2140             "0")
2141         ;; spam detection mode
2142         (when (string-match spam-bogofilter-bogosity-positive-spam-header
2143                             header)
2144           spam-split-group)))))
2145
2146 ;; return something sensible if the score can't be determined
2147 (defun spam-bogofilter-score (&optional recheck)
2148   "Get the Bogofilter spamicity score"
2149   (interactive "P")
2150   (save-window-excursion
2151     (gnus-summary-show-article t)
2152     (set-buffer gnus-article-buffer)
2153     (let ((score (or (unless recheck
2154                        (spam-check-bogofilter-headers t))
2155                      (spam-check-bogofilter t))))
2156       (gnus-summary-show-article)
2157       (message "Spamicity score %s" score)
2158       (or score "0"))))
2159
2160 (defun spam-check-bogofilter (&optional score)
2161   "Check the Bogofilter backend for the classification of this message"
2162   (let ((article-buffer-name (buffer-name))
2163         (db spam-bogofilter-database-directory)
2164         return)
2165     (with-temp-buffer
2166       (let ((temp-buffer-name (buffer-name)))
2167         (save-excursion
2168           (set-buffer article-buffer-name)
2169           (apply 'call-process-region
2170                  (point-min) (point-max)
2171                  spam-bogofilter-path
2172                  nil temp-buffer-name nil
2173                  (if db `("-d" ,db "-v") `("-v"))))
2174         (setq return (spam-check-bogofilter-headers score))))
2175     return))
2176
2177 (defun spam-bogofilter-register-with-bogofilter (articles
2178                                                  spam
2179                                                  &optional unregister)
2180   "Register an article, given as a string, as spam or non-spam."
2181   (dolist (article articles)
2182     (let ((article-string (spam-get-article-as-string article))
2183           (db spam-bogofilter-database-directory)
2184           (switch (if unregister
2185                       (if spam
2186                           spam-bogofilter-spam-strong-switch
2187                         spam-bogofilter-ham-strong-switch)
2188                     (if spam
2189                         spam-bogofilter-spam-switch
2190                       spam-bogofilter-ham-switch))))
2191       (when (stringp article-string)
2192         (with-temp-buffer
2193           (insert article-string)
2194
2195           (apply 'call-process-region
2196                  (point-min) (point-max)
2197                  spam-bogofilter-path
2198                  nil nil nil switch
2199                  (if db `("-d" ,db "-v") `("-v"))))))))
2200
2201 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2202   (spam-bogofilter-register-with-bogofilter articles t unregister))
2203
2204 (defun spam-bogofilter-unregister-spam-routine (articles)
2205   (spam-bogofilter-register-spam-routine articles t))
2206
2207 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2208   (spam-bogofilter-register-with-bogofilter articles nil unregister))
2209
2210 (defun spam-bogofilter-unregister-ham-routine (articles)
2211   (spam-bogofilter-register-ham-routine articles t))
2212
2213
2214 \f
2215 ;;;; spamoracle
2216 (defun spam-check-spamoracle ()
2217   "Run spamoracle on an article to determine whether it's spam."
2218   (let ((article-buffer-name (buffer-name))
2219         (spam-split-group (if spam-split-symbolic-return
2220                               'spam
2221                             spam-split-group)))
2222     (with-temp-buffer
2223       (let ((temp-buffer-name (buffer-name)))
2224         (save-excursion
2225           (set-buffer article-buffer-name)
2226           (let ((status
2227                  (apply 'call-process-region
2228                         (point-min) (point-max)
2229                         spam-spamoracle-binary
2230                         nil temp-buffer-name nil
2231                         (if spam-spamoracle-database
2232                             `("-f" ,spam-spamoracle-database "mark")
2233                           '("mark")))))
2234             (if (eq 0 status)
2235                 (progn
2236                   (set-buffer temp-buffer-name)
2237                   (goto-char (point-min))
2238                   (when (re-search-forward "^X-Spam: yes;" nil t)
2239                     spam-split-group))
2240               (error "Error running spamoracle: %s" status))))))))
2241
2242 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2243   "Run spamoracle in training mode."
2244   (with-temp-buffer
2245     (let ((temp-buffer-name (buffer-name)))
2246       (save-excursion
2247         (goto-char (point-min))
2248         (dolist (article articles)
2249           (insert (spam-get-article-as-string article)))
2250         (let* ((arg (if (spam-xor unregister article-is-spam-p)
2251                         "-spam"
2252                       "-good"))
2253                (status
2254                 (apply 'call-process-region
2255                        (point-min) (point-max)
2256                        spam-spamoracle-binary
2257                        nil temp-buffer-name nil
2258                        (if spam-spamoracle-database
2259                            `("-f" ,spam-spamoracle-database
2260                              "add" ,arg)
2261                          `("add" ,arg)))))
2262           (unless (eq 0 status)
2263             (error "Error running spamoracle: %s" status)))))))
2264
2265 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2266   (spam-spamoracle-learn articles nil unregister))
2267
2268 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2269   (spam-spamoracle-learn-ham articles t))
2270
2271 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2272   (spam-spamoracle-learn articles t unregister))
2273
2274 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2275   (spam-spamoracle-learn-spam articles t))
2276
2277 \f
2278 ;;;; SpamAssassin
2279 ;;; based mostly on the bogofilter code
2280 (defun spam-check-spamassassin-headers (&optional score)
2281   "Check the SpamAssassin headers for the classification of this message."
2282   (if score                             ; scoring mode
2283       (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2284         (when header
2285           (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2286               (match-string 1 header)
2287             "0")))
2288     ;; spam detection mode
2289     (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2290           (spam-split-group (if spam-split-symbolic-return
2291                                  'spam
2292                                spam-split-group)))
2293           (when header                  ; return nil when no header
2294             (when (string-match spam-spamassassin-positive-spam-flag-header
2295                                 header)
2296               spam-split-group)))))
2297
2298 (defun spam-check-spamassassin (&optional score)
2299   "Check the SpamAssassin backend for the classification of this message."
2300   (let ((article-buffer-name (buffer-name)))
2301     (with-temp-buffer
2302       (let ((temp-buffer-name (buffer-name)))
2303         (save-excursion
2304           (set-buffer article-buffer-name)
2305           (apply 'call-process-region
2306                  (point-min) (point-max) spam-spamassassin-path
2307                  nil temp-buffer-name nil spam-spamassassin-arguments))
2308         ;; check the return now (we're back in the temp buffer)
2309         (goto-char (point-min))
2310         (spam-check-spamassassin-headers score)))))
2311
2312 ;; return something sensible if the score can't be determined
2313 (defun spam-spamassassin-score (&optional recheck)
2314   "Get the SpamAssassin score"
2315   (interactive "P")
2316   (save-window-excursion
2317     (gnus-summary-show-article t)
2318     (set-buffer gnus-article-buffer)
2319     (let ((score (or (unless recheck
2320                        (spam-check-spamassassin-headers t))
2321                      (spam-check-spamassassin t))))
2322       (gnus-summary-show-article)
2323       (message "SpamAssassin score %s" score)
2324       (or score "0"))))
2325
2326 (defun spam-spamassassin-register-with-sa-learn (articles spam
2327                                                  &optional unregister)
2328   "Register articles with spamassassin's sa-learn as spam or non-spam."
2329   (if articles
2330       (let ((action (if unregister spam-sa-learn-unregister-switch
2331                       (if spam spam-sa-learn-spam-switch
2332                         spam-sa-learn-ham-switch)))
2333             (summary-buffer-name (buffer-name)))
2334         (with-temp-buffer
2335           ;; group the articles into mbox format
2336           (dolist (article articles)
2337             (let (article-string)
2338               (save-excursion
2339                 (set-buffer summary-buffer-name)
2340                 (setq article-string (spam-get-article-as-string article)))
2341               (when (stringp article-string)
2342                 (insert "From \n") ; mbox separator (sa-learn only checks the
2343                                    ; first five chars, so we can get away with
2344                                    ; a bogus line))
2345                 (insert article-string)
2346                 (insert "\n"))))
2347           ;; call sa-learn on all messages at the same time
2348           (apply 'call-process-region
2349                  (point-min) (point-max)
2350                  spam-sa-learn-path
2351                  nil nil nil "--mbox"
2352                  (if spam-sa-learn-rebuild
2353                      (list action)
2354                    `("--no-rebuild" ,action)))))))
2355
2356 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2357   (spam-spamassassin-register-with-sa-learn articles t unregister))
2358
2359 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2360   (spam-spamassassin-register-with-sa-learn articles nil unregister))
2361
2362 (defun spam-spamassassin-unregister-spam-routine (articles)
2363   (spam-spamassassin-register-with-sa-learn articles t t))
2364
2365 (defun spam-spamassassin-unregister-ham-routine (articles)
2366   (spam-spamassassin-register-with-sa-learn articles nil t))
2367
2368 \f
2369 ;;;; Bsfilter
2370 ;;; based mostly on the bogofilter code
2371 (defun spam-check-bsfilter-headers (&optional score)
2372   (if score
2373       (or (nnmail-fetch-field spam-bsfilter-probability-header)
2374           "0")
2375     (let ((header (nnmail-fetch-field spam-bsfilter-header))
2376           (spam-split-group (if spam-split-symbolic-return
2377                                 'spam
2378                               spam-split-group)))
2379       (when header ; return nil when no header
2380         (when (string-match "YES" header)
2381           spam-split-group)))))
2382
2383 ;; return something sensible if the score can't be determined
2384 (defun spam-bsfilter-score (&optional recheck)
2385   "Get the Bsfilter spamicity score"
2386   (interactive "P")
2387   (save-window-excursion
2388     (gnus-summary-show-article t)
2389     (set-buffer gnus-article-buffer)
2390     (let ((score (or (unless recheck
2391                        (spam-check-bsfilter-headers t))
2392                      (spam-check-bsfilter t))))
2393       (gnus-summary-show-article)
2394       (message "Spamicity score %s" score)
2395       (or score "0"))))
2396
2397 (defun spam-check-bsfilter (&optional score)
2398   "Check the Bsfilter backend for the classification of this message"
2399   (let ((article-buffer-name (buffer-name))
2400         (dir spam-bsfilter-database-directory)
2401         return)
2402     (with-temp-buffer
2403       (let ((temp-buffer-name (buffer-name)))
2404         (save-excursion
2405           (set-buffer article-buffer-name)
2406           (apply 'call-process-region
2407                  (point-min) (point-max)
2408                  spam-bsfilter-path
2409                  nil temp-buffer-name nil
2410                  "--pipe"
2411                  "--insert-flag"
2412                  "--insert-probability"
2413                  (when dir
2414                    (list "--homedir" dir))))
2415         (setq return (spam-check-bsfilter-headers score))))
2416     return))
2417
2418 (defun spam-bsfilter-register-with-bsfilter (articles
2419                                              spam
2420                                              &optional unregister)
2421   "Register an article, given as a string, as spam or non-spam."
2422   (dolist (article articles)
2423     (let ((article-string (spam-get-article-as-string article))
2424           (switch (if unregister
2425                       (if spam
2426                           spam-bsfilter-spam-strong-switch
2427                         spam-bsfilter-ham-strong-switch)
2428                     (if spam
2429                         spam-bsfilter-spam-switch
2430                       spam-bsfilter-ham-switch))))
2431       (when (stringp article-string)
2432         (with-temp-buffer
2433           (insert article-string)
2434           (apply 'call-process-region
2435                  (point-min) (point-max)
2436                  spam-bsfilter-path
2437                  nil nil nil switch
2438                  "--update"
2439                  (when spam-bsfilter-database-directory
2440                    (list "--homedir"
2441                          spam-bsfilter-database-directory))))))))
2442
2443 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2444   (spam-bsfilter-register-with-bsfilter articles t unregister))
2445
2446 (defun spam-bsfilter-unregister-spam-routine (articles)
2447   (spam-bsfilter-register-spam-routine articles t))
2448
2449 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2450   (spam-bsfilter-register-with-bsfilter articles nil unregister))
2451
2452 (defun spam-bsfilter-unregister-ham-routine (articles)
2453   (spam-bsfilter-register-ham-routine articles t))
2454
2455 \f
2456 ;;;; CRM114 Mailfilter
2457 (defun spam-check-crm114-headers (&optional score)
2458   (let ((header (message-fetch-field spam-crm114-header))
2459         (spam-split-group (if spam-split-symbolic-return
2460                               'spam
2461                             spam-split-group)))
2462     (when header                        ; return nil when no header
2463       (if score                         ; scoring mode
2464           (if (string-match "( pR: \\([0-9.-]+\\)" header)
2465               (match-string 1 header)
2466             "0")
2467         ;; spam detection mode
2468         (when (string-match spam-crm114-positive-spam-header
2469                             header)
2470           spam-split-group)))))
2471
2472 ;; return something sensible if the score can't be determined
2473 (defun spam-crm114-score ()
2474   "Get the CRM114 Mailfilter pR"
2475   (interactive)
2476   (save-window-excursion
2477     (gnus-summary-show-article t)
2478     (set-buffer gnus-article-buffer)
2479     (let ((score (or (spam-check-crm114-headers t)
2480                      (spam-check-crm114 t))))
2481       (gnus-summary-show-article)
2482       (message "pR: %s" score)
2483       (or score "0"))))
2484
2485 (defun spam-check-crm114 (&optional score)
2486   "Check the CRM114 Mailfilter backend for the classification of this message"
2487   (let ((article-buffer-name (buffer-name))
2488         (db spam-crm114-database-directory)
2489         return)
2490     (with-temp-buffer
2491       (let ((temp-buffer-name (buffer-name)))
2492         (save-excursion
2493           (set-buffer article-buffer-name)
2494           (apply 'call-process-region
2495                  (point-min) (point-max)
2496                  spam-crm114-program
2497                  nil temp-buffer-name nil
2498                  (when db (list (concat "--fileprefix=" db)))))
2499         (setq return (spam-check-crm114-headers score))))
2500     return))
2501
2502 (defun spam-crm114-register-with-crm114 (articles
2503                                          spam
2504                                          &optional unregister)
2505   "Register an article, given as a string, as spam or non-spam."
2506   (dolist (article articles)
2507     (let ((article-string (spam-get-article-as-string article))
2508           (db spam-crm114-database-directory)
2509           (switch (if unregister
2510                       (if spam
2511                           spam-crm114-spam-strong-switch
2512                         spam-crm114-ham-strong-switch)
2513                     (if spam
2514                         spam-crm114-spam-switch
2515                       spam-crm114-ham-switch))))
2516       (when (stringp article-string)
2517         (with-temp-buffer
2518           (insert article-string)
2519
2520           (apply 'call-process-region
2521                  (point-min) (point-max)
2522                  spam-crm114-program
2523                  nil nil nil
2524                  (when db (list switch (concat "--fileprefix=" db)))))))))
2525
2526 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2527   (spam-crm114-register-with-crm114 articles t unregister))
2528
2529 (defun spam-crm114-unregister-spam-routine (articles)
2530   (spam-crm114-register-spam-routine articles t))
2531
2532 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2533   (spam-crm114-register-with-crm114 articles nil unregister))
2534
2535 (defun spam-crm114-unregister-ham-routine (articles)
2536   (spam-crm114-register-ham-routine articles t))
2537
2538 \f
2539 ;;;; Hooks
2540
2541 ;;;###autoload
2542 (defun spam-initialize (&rest symbols)
2543   "Install the spam.el hooks and do other initialization.
2544 When SYMBOLS is given, set those variables to t.  This is so you
2545 can call spam-initialize before you set spam-use-* variables on
2546 explicitly, and matters only if you need the extra headers
2547 installed through spam-necessary-extra-headers."
2548   (interactive)
2549
2550   (dolist (var symbols)
2551     (set var t))
2552
2553   (dolist (header (spam-necessary-extra-headers))
2554     (add-to-list 'nnmail-extra-headers header)
2555     (add-to-list 'gnus-extra-headers header))
2556
2557   (setq spam-install-hooks t)
2558   ;; TODO: How do we redo this every time spam-face is customized?
2559   (push '((eq mark gnus-spam-mark) . spam-face)
2560         gnus-summary-highlight)
2561   ;; Add hooks for loading and saving the spam stats
2562   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2563   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2564   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2565   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2566   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2567   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2568   (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2569
2570 (defun spam-unload-hook ()
2571   "Uninstall the spam.el hooks"
2572   (interactive)
2573   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2574   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2575   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2576   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2577   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2578   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2579   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2580
2581 (when spam-install-hooks
2582   (spam-initialize))
2583
2584 (provide 'spam)
2585
2586 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
2587 ;;; spam.el ends here