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