c315f2f0347fad237689fd2354f5a414234d008a
[gnus] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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.
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 (const 'default :tag
97                         "Move spam out of all groups.  Move ham out of spam groups.")
98                  (const 'move-all :tag
99                         "Move spam out of all groups.  Move ham out of all groups.")
100                  (const 'move-none :tag
101                         "Never move spam or ham out of any groups."))
102   :group 'spam)
103
104 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
105   "Directory for spam whitelists and blacklists."
106   :type 'directory
107   :group 'spam)
108
109 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
110   "Whether new messages in a spam group should get the spam-mark."
111   :type 'boolean
112   ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
113   :group 'spam)
114
115 (defcustom spam-log-to-registry nil
116   "Whether spam/ham processing should be logged in the registry."
117   :type 'boolean
118   :group 'spam)
119
120 (defcustom spam-split-symbolic-return nil
121   "Whether `spam-split' should work with symbols or group names."
122   :type 'boolean
123   :group 'spam)
124
125 (defcustom spam-split-symbolic-return-positive nil
126   "Whether `spam-split' should ALWAYS work with symbols or group names.
127 Do not set this if you use `spam-split' in a fancy split method."
128   :type 'boolean
129   :group 'spam)
130
131 (defcustom spam-mark-only-unseen-as-spam t
132   "Whether only unseen articles should be marked as spam in spam groups.
133 When nil, all unread articles in a spam group are marked as
134 spam.  Set this if you want to leave an article unread in a spam group
135 without losing it to the automatic spam-marking process."
136   :type 'boolean
137   :group 'spam)
138
139 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
140   "Whether ham should be marked unread before it's moved.
141 The article is moved out of a spam group according to `ham-process-destination'.
142 This variable is an official entry in the international Longest Variable Name
143 Competition."
144   :type 'boolean
145   :group 'spam)
146
147 (defcustom spam-disable-spam-split-during-ham-respool nil
148   "Whether `spam-split' should be ignored while resplitting ham.
149 This is useful to prevent ham from ending up in the same spam
150 group after the resplit.  Don't set this to t if you have `spam-split' as the
151 last rule in your split configuration."
152   :type 'boolean
153   :group 'spam)
154
155 (defcustom spam-autodetect-recheck-messages nil
156   "Should spam.el recheck all meessages when autodetecting?
157 Normally this is nil, so only unseen messages will be checked."
158   :type 'boolean
159   :group 'spam)
160
161 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
162   "The location of the whitelist.
163 The file format is one regular expression per line.
164 The regular expression is matched against the address."
165   :type 'file
166   :group 'spam)
167
168 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
169   "The location of the blacklist.
170 The file format is one regular expression per line.
171 The regular expression is matched against the address."
172   :type 'file
173   :group 'spam)
174
175 (defcustom spam-use-dig t
176   "Whether `query-dig' should be used instead of `dns-query'."
177   :type 'boolean
178   :group 'spam)
179
180 (defcustom spam-use-gmane-xref nil
181   "Whether the Gmane spam xref should be used by `spam-split'."
182   :type 'boolean
183   :group 'spam)
184
185 (defcustom spam-use-blacklist nil
186   "Whether the blacklist should be used by `spam-split'."
187   :type 'boolean
188   :group 'spam)
189
190 (defcustom spam-blacklist-ignored-regexes nil
191   "Regular expressions that the blacklist should ignore."
192   :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
193   :group 'spam)
194
195 (defcustom spam-use-whitelist nil
196   "Whether the whitelist should be used by `spam-split'."
197   :type 'boolean
198   :group 'spam)
199
200 (defcustom spam-use-whitelist-exclusive nil
201   "Whether whitelist-exclusive should be used by `spam-split'.
202 Exclusive whitelisting means that all messages from senders not in the whitelist
203 are considered spam."
204   :type 'boolean
205   :group 'spam)
206
207 (defcustom spam-use-blackholes nil
208   "Whether blackholes should be used by `spam-split'."
209   :type 'boolean
210   :group 'spam)
211
212 (defcustom spam-use-hashcash nil
213   "Whether hashcash payments should be detected by `spam-split'."
214   :type 'boolean
215   :group 'spam)
216
217 (defcustom spam-use-regex-headers nil
218   "Whether a header regular expression match should be used by `spam-split'.
219 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
220   :type 'boolean
221   :group 'spam)
222
223 (defcustom spam-use-regex-body nil
224   "Whether a body regular expression match should be used by `spam-split'.
225 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
226   :type 'boolean
227   :group 'spam)
228
229 (defcustom spam-use-bogofilter-headers nil
230   "Whether bogofilter headers should be used by `spam-split'.
231 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
232   :type 'boolean
233   :group 'spam)
234
235 (defcustom spam-use-bogofilter nil
236   "Whether bogofilter should be invoked by `spam-split'.
237 Enable this if you want Gnus to invoke Bogofilter on new messages."
238   :type 'boolean
239   :group 'spam)
240
241 (defcustom spam-use-bsfilter-headers nil
242   "Whether bsfilter headers should be used by `spam-split'.
243 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
244   :type 'boolean
245   :group 'spam)
246
247 (defcustom spam-use-bsfilter nil
248   "Whether bsfilter should be invoked by `spam-split'.
249 Enable this if you want Gnus to invoke Bsfilter on new messages."
250   :type 'boolean
251   :group 'spam)
252
253 (defcustom spam-use-BBDB nil
254   "Whether BBDB should be used by `spam-split'."
255   :type 'boolean
256   :group 'spam)
257
258 (defcustom spam-use-BBDB-exclusive nil
259   "Whether BBDB-exclusive should be used by `spam-split'.
260 Exclusive BBDB means that all messages from senders not in the BBDB are
261 considered spam."
262   :type 'boolean
263   :group 'spam)
264
265 (defcustom spam-use-ifile nil
266   "Whether ifile should be used by `spam-split'."
267   :type 'boolean
268   :group 'spam)
269
270 (defcustom spam-use-stat nil
271   "Whether `spam-stat' should be used by `spam-split'."
272   :type 'boolean
273   :group 'spam)
274
275 (defcustom spam-use-spamoracle nil
276   "Whether spamoracle should be used by `spam-split'."
277   :type 'boolean
278   :group 'spam)
279
280 (defcustom spam-use-spamassassin nil
281   "Whether spamassassin should be invoked by `spam-split'.
282 Enable this if you want Gnus to invoke SpamAssassin on new messages."
283   :type 'boolean
284   :group 'spam)
285
286 (defcustom spam-use-spamassassin-headers nil
287   "Whether spamassassin headers should be checked by `spam-split'.
288 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
289 them."
290   :type 'boolean
291   :group 'spam)
292
293 (defcustom spam-use-crm114 nil
294   "Whether the CRM114 Mailfilter should be used by `spam-split'."
295   :type 'boolean
296   :group 'spam)
297
298 (defcustom spam-install-hooks (or
299                                spam-use-dig
300                                spam-use-gmane-xref
301                                spam-use-blacklist
302                                spam-use-whitelist
303                                spam-use-whitelist-exclusive
304                                spam-use-blackholes
305                                spam-use-hashcash
306                                spam-use-regex-headers
307                                spam-use-regex-body
308                                spam-use-bogofilter
309                                spam-use-bogofilter-headers
310                                spam-use-spamassassin
311                                spam-use-spamassassin-headers
312                                spam-use-bsfilter
313                                spam-use-bsfilter-headers
314                                spam-use-BBDB
315                                spam-use-BBDB-exclusive
316                                spam-use-ifile
317                                spam-use-stat
318                                spam-use-spamoracle
319                                spam-use-crm114)
320   "Whether the spam hooks should be installed.
321 Default to t if one of the spam-use-* variables is set."
322   :group 'spam
323   :type 'boolean)
324
325 (defcustom spam-split-group "spam"
326   "Group name where incoming spam should be put by `spam-split'."
327   :type 'string
328   :group 'spam)
329
330 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
331 ;;; not regular expressions
332 (defcustom spam-junk-mailgroups (cons
333                                  spam-split-group
334                                  '("mail.junk" "poste.pourriel"))
335   "Mailgroups with spam contents.
336 All unmarked article in such group receive the spam mark on group entry."
337   :type '(repeat (string :tag "Group"))
338   :group 'spam)
339
340
341 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
342   "The group where spam xrefs can be found on Gmane.
343 Only meaningful if you enable `spam-use-gmane-xref'."
344   :type 'string
345   :group 'spam)
346
347 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
348                                     "dev.null.dk" "relays.visi.com")
349   "List of blackhole servers.
350 Only meaningful if you enable `spam-use-blackholes'."
351   :type '(repeat (string :tag "Server"))
352   :group 'spam)
353
354 (defcustom spam-blackhole-good-server-regex nil
355   "String matching IP addresses that should not be checked in the blackholes.
356 Only meaningful if you enable `spam-use-blackholes'."
357   :type '(radio (const nil) regexp)
358   :group 'spam)
359
360 (defface spam
361   '((((class color) (type tty) (background dark))
362      (:foreground "gray80" :background "gray50"))
363     (((class color) (type tty) (background light))
364      (:foreground "gray50" :background "gray80"))
365     (((class color) (background dark))
366      (:foreground "ivory2"))
367     (((class color) (background light))
368      (:foreground "ivory4"))
369     (t :inverse-video t))
370   "Face for spam-marked articles."
371   :group 'spam)
372 ;; backward-compatibility alias
373 (put 'spam-face 'face-alias 'spam)
374
375 (defcustom spam-face 'spam
376   "Face for spam-marked articles."
377   :type 'face
378   :group 'spam)
379
380 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
381   "Regular expression for positive header spam matches.
382 Only meaningful if you enable `spam-use-regex-headers'."
383   :type '(repeat (regexp :tag "Regular expression to match spam header"))
384   :group 'spam)
385
386 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
387   "Regular expression for positive header ham matches.
388 Only meaningful if you enable `spam-use-regex-headers'."
389   :type '(repeat (regexp :tag "Regular expression to match ham header"))
390   :group 'spam)
391
392 (defcustom spam-regex-body-spam '()
393   "Regular expression for positive body spam matches.
394 Only meaningful if you enable `spam-use-regex-body'."
395   :type '(repeat (regexp :tag "Regular expression to match spam body"))
396   :group 'spam)
397
398 (defcustom spam-regex-body-ham '()
399   "Regular expression for positive body ham matches.
400 Only meaningful if you enable `spam-use-regex-body'."
401   :type '(repeat (regexp :tag "Regular expression to match ham body"))
402   :group 'spam)
403
404 (defcustom spam-summary-score-preferred-header nil
405   "Preferred header to use for `spam-summary-score'."
406   :type '(choice :tag "Header name"
407           (symbol :tag "SpamAssassin etc" X-Spam-Status)
408           (symbol :tag "Bogofilter"       X-Bogosity)
409           (const  :tag "No preference, take best guess." nil))
410   :group 'spam)
411
412 (defgroup spam-ifile nil
413   "Spam ifile configuration."
414   :group 'spam)
415
416 (make-obsolete-variable 'spam-ifile-path 'spam-ifile-program)
417 ;; "22.1" ;; Gnus 5.10.9
418 (defcustom spam-ifile-program (executable-find "ifile")
419   "Name of the ifile program."
420   :type '(choice (file :tag "Location of ifile")
421                  (const :tag "ifile is not installed"))
422   :group 'spam-ifile)
423
424 (make-obsolete-variable 'spam-ifile-database-path 'spam-ifile-database)
425 ;; "22.1" ;; Gnus 5.10.9
426 (defcustom spam-ifile-database nil
427   "File name of the ifile database."
428   :type '(choice (file :tag "Location of the ifile database")
429                  (const :tag "Use the default"))
430   :group 'spam-ifile)
431
432 (defcustom spam-ifile-spam-category "spam"
433   "Name of the spam ifile category."
434   :type 'string
435   :group 'spam-ifile)
436
437 (defcustom spam-ifile-ham-category nil
438   "Name of the ham ifile category.
439 If nil, the current group name will be used."
440   :type '(choice (string :tag "Use a fixed category")
441                  (const :tag "Use the current group name"))
442   :group 'spam-ifile)
443
444 (defcustom spam-ifile-all-categories nil
445   "Whether the ifile check will return all categories, or just spam.
446 Set this to t if you want to use the `spam-split' invocation of ifile as
447 your main source of newsgroup names."
448   :type 'boolean
449   :group 'spam-ifile)
450
451 (defgroup spam-bogofilter nil
452   "Spam bogofilter configuration."
453   :group 'spam)
454
455 (make-obsolete-variable 'spam-bogofilter-path 'spam-bogofilter-program)
456 ;; "22.1" ;; Gnus 5.10.9
457 (defcustom spam-bogofilter-program (executable-find "bogofilter")
458   "Name of the Bogofilter program."
459   :type '(choice (file :tag "Location of bogofilter")
460                  (const :tag "Bogofilter is not installed"))
461   :group 'spam-bogofilter)
462
463 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
464
465 (defcustom spam-bogofilter-header "X-Bogosity"
466   "The header that Bogofilter inserts in messages."
467   :type 'string
468   :group 'spam-bogofilter)
469
470 (defcustom spam-bogofilter-spam-switch "-s"
471   "The switch that Bogofilter uses to register spam messages."
472   :type 'string
473   :group 'spam-bogofilter)
474
475 (defcustom spam-bogofilter-ham-switch "-n"
476   "The switch that Bogofilter uses to register ham messages."
477   :type 'string
478   :group 'spam-bogofilter)
479
480 (defcustom spam-bogofilter-spam-strong-switch "-S"
481   "The switch that Bogofilter uses to unregister ham messages."
482   :type 'string
483   :group 'spam-bogofilter)
484
485 (defcustom spam-bogofilter-ham-strong-switch "-N"
486   "The switch that Bogofilter uses to unregister spam messages."
487   :type 'string
488   :group 'spam-bogofilter)
489
490 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
491   "The regex on `spam-bogofilter-header' for positive spam identification."
492   :type 'regexp
493   :group 'spam-bogofilter)
494
495 (defcustom spam-bogofilter-database-directory nil
496   "Location of the Bogofilter database.
497 When nil, use the default location."
498   :type '(choice (directory
499                   :tag "Location of the Bogofilter database directory")
500                  (const :tag "Use the default"))
501   :group 'spam-bogofilter)
502
503 (defgroup spam-bsfilter nil
504   "Spam bsfilter configuration."
505   :group 'spam)
506
507 (make-obsolete-variable 'spam-bsfilter-path 'spam-bsfilter-program)
508 ;; "22.1" ;; Gnus 5.10.9
509 (defcustom spam-bsfilter-program (executable-find "bsfilter")
510   "Name of the Bsfilter program."
511   :type '(choice (file :tag "Location of bsfilter")
512                  (const :tag "Bsfilter is not installed"))
513   :group 'spam-bsfilter)
514
515 (defcustom spam-bsfilter-header "X-Spam-Flag"
516   "The header inserted by Bsfilter to flag spam."
517   :type 'string
518   :group 'spam-bsfilter)
519
520 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
521   "The header that Bsfilter inserts in messages."
522   :type 'string
523   :group 'spam-bsfilter)
524
525 (defcustom spam-bsfilter-spam-switch "--add-spam"
526   "The switch that Bsfilter uses to register spam messages."
527   :type 'string
528   :group 'spam-bsfilter)
529
530 (defcustom spam-bsfilter-ham-switch "--add-clean"
531   "The switch that Bsfilter uses to register ham messages."
532   :type 'string
533   :group 'spam-bsfilter)
534
535 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
536   "The switch that Bsfilter uses to unregister ham messages."
537   :type 'string
538   :group 'spam-bsfilter)
539
540 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
541   "The switch that Bsfilter uses to unregister spam messages."
542   :type 'string
543   :group 'spam-bsfilter)
544
545 (defcustom spam-bsfilter-database-directory nil
546   "Directory path of the Bsfilter databases."
547   :type '(choice (directory
548                   :tag "Location of the Bsfilter database directory")
549                  (const :tag "Use the default"))
550   :group 'spam-bsfilter)
551
552 (defgroup spam-spamoracle nil
553   "Spam spamoracle configuration."
554   :group 'spam)
555
556 (defcustom spam-spamoracle-database nil
557   "Location of spamoracle database file.
558 When nil, use the default spamoracle database."
559   :type '(choice (directory :tag "Location of spamoracle database file.")
560                  (const :tag "Use the default"))
561   :group 'spam-spamoracle)
562
563 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
564   "Location of the spamoracle binary."
565   :type '(choice (directory :tag "Location of the spamoracle binary")
566                  (const :tag "Use the default"))
567   :group 'spam-spamoracle)
568
569 (defgroup spam-spamassassin nil
570   "Spam SpamAssassin configuration."
571   :group 'spam)
572
573 (make-obsolete-variable 'spam-spamassassin-path
574   'spam-spamassassin-program) ;; "22.1" ;; Gnus 5.10.9
575 (defcustom spam-assassin-program (executable-find "spamassassin")
576   "Name of the spamassassin program.
577 Hint: set this to \"spamc\" if you have spamd running.  See the spamc and
578 spamd man pages for more information on these programs."
579   :type '(choice (file :tag "Location of spamc")
580                  (const :tag "spamassassin is not installed"))
581   :group 'spam-spamassassin)
582
583 (defcustom spam-spamassassin-arguments ()
584   "Arguments to pass to the spamassassin executable.
585 This must be a list.  For example, `(\"-C\" \"configfile\")'."
586   :type '(restricted-sexp :match-alternatives (listp))
587   :group 'spam-spamassassin)
588
589 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
590   "The header inserted by SpamAssassin to flag spam."
591   :type 'string
592   :group 'spam-spamassassin)
593
594 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
595   "The regex on `spam-spamassassin-spam-flag-header' for positive spam
596 identification"
597   :type 'string
598   :group 'spam-spamassassin)
599
600 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
601   "The header inserted by SpamAssassin, giving extended scoring information"
602   :type 'string
603   :group 'spam-spamassassin)
604
605 (make-obsolete-variable 'spam-sa-learn-path 'spam-sa-learn-program)
606 ;; "22.1" ;; Gnus 5.10.9
607 (defcustom spam-sa-learn-program (executable-find "sa-learn")
608   "Name of the sa-learn program."
609   :type '(choice (file :tag "Location of spamassassin")
610                  (const :tag "spamassassin is not installed"))
611   :group 'spam-spamassassin)
612
613 (defcustom spam-sa-learn-rebuild t
614   "Whether sa-learn should rebuild the database every time it is called
615 Enable this if you want sa-learn to rebuild the database automatically.  Doing
616 this will slightly increase the running time of the spam registration process.
617 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
618 order for SpamAssassin to recognize the new registered spam."
619   :type 'boolean
620   :group 'spam-spamassassin)
621
622 (defcustom spam-sa-learn-spam-switch "--spam"
623   "The switch that sa-learn uses to register spam messages."
624   :type 'string
625   :group 'spam-spamassassin)
626
627 (defcustom spam-sa-learn-ham-switch "--ham"
628   "The switch that sa-learn uses to register ham messages."
629   :type 'string
630   :group 'spam-spamassassin)
631
632 (defcustom spam-sa-learn-unregister-switch "--forget"
633   "The switch that sa-learn uses to unregister messages messages."
634   :type 'string
635   :group 'spam-spamassassin)
636
637 (defgroup spam-crm114 nil
638   "Spam CRM114 Mailfilter configuration."
639   :group 'spam)
640
641 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
642   "File path of the CRM114 Mailfilter executable program."
643   :type '(choice (file :tag "Location of CRM114 Mailfilter")
644          (const :tag "CRM114 Mailfilter is not installed"))
645   :group 'spam-crm114)
646
647 (defcustom spam-crm114-header "X-CRM114-Status"
648   "The header that CRM114 Mailfilter inserts in messages."
649   :type 'string
650   :group 'spam-crm114)
651
652 (defcustom spam-crm114-spam-switch "--learnspam"
653   "The switch that CRM114 Mailfilter uses to register spam messages."
654   :type 'string
655   :group 'spam-crm114)
656
657 (defcustom spam-crm114-ham-switch "--learnnonspam"
658   "The switch that CRM114 Mailfilter uses to register ham messages."
659   :type 'string
660   :group 'spam-crm114)
661
662 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
663   "The switch that CRM114 Mailfilter uses to unregister ham messages."
664   :type 'string
665   :group 'spam-crm114)
666
667 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
668   "The switch that CRM114 Mailfilter uses to unregister spam messages."
669   :type 'string
670   :group 'spam-crm114)
671
672 (defcustom spam-crm114-positive-spam-header "^SPAM"
673   "The regex on `spam-crm114-header' for positive spam identification."
674   :type 'regexp
675   :group 'spam-crm114)
676
677 (defcustom spam-crm114-database-directory nil
678   "Directory path of the CRM114 Mailfilter databases."
679   :type '(choice (directory
680           :tag "Location of the CRM114 Mailfilter database directory")
681          (const :tag "Use the default"))
682   :group 'spam-crm114)
683
684 ;;; Key bindings for spam control.
685
686 (gnus-define-keys gnus-summary-mode-map
687   "St" spam-generic-score
688   "Sx" gnus-summary-mark-as-spam
689   "Mst" spam-generic-score
690   "Msx" gnus-summary-mark-as-spam
691   "\M-d" gnus-summary-mark-as-spam)
692
693 (defvar spam-cache-lookups t
694   "Whether spam.el will try to cache lookups using `spam-caches'.")
695
696 (defvar spam-caches (make-hash-table
697                      :size 10
698                      :test 'equal)
699   "Cache of spam detection entries.")
700
701 (defvar spam-old-articles nil
702   "List of old ham and spam articles, generated when a group is entered.")
703
704 (defvar spam-split-disabled nil
705   "If non-nil, `spam-split' is disabled, and always returns nil.")
706
707 (defvar spam-split-last-successful-check nil
708   "Internal variable.
709 `spam-split' will set this to nil or a spam-use-XYZ check if it
710 finds ham or spam.")
711
712 ;; internal variables for backends
713 ;; TODO: find a way to create these on the fly in spam-install-backend-super
714 (defvar spam-use-copy nil)
715 (defvar spam-use-move nil)
716 (defvar spam-use-gmane nil)
717 (defvar spam-use-resend nil)
718
719 ;;}}}
720
721 ;;{{{ convenience functions
722
723 (defun spam-clear-cache (symbol)
724   "Clear the `spam-caches' entry for a check."
725   (remhash symbol spam-caches))
726
727 (defun spam-xor (a b)
728   "Logical A xor B."
729   (and (or a b) (not (and a b))))
730
731 (defun spam-set-difference (list1 list2)
732   "Return a set difference of LIST1 and LIST2.
733 When either list is nil, the other is returned."
734   (if (and list1 list2)
735       ;; we have two non-nil lists
736       (progn
737         (dolist (item (append list1 list2))
738           (when (and (memq item list1) (memq item list2))
739             (setq list1 (delq item list1))
740             (setq list2 (delq item list2))))
741         (append list1 list2))
742     ;; if either of the lists was nil, return the other one
743     (if list1 list1 list2)))
744
745 (defun spam-group-ham-mark-p (group mark &optional spam)
746   "Checks if MARK is considered a ham mark in GROUP."
747   (when (stringp group)
748     (let* ((marks (spam-group-ham-marks group spam))
749            (marks (if (symbolp mark)
750                       marks
751                     (mapcar 'symbol-value marks))))
752       (memq mark marks))))
753
754 (defun spam-group-spam-mark-p (group mark)
755   "Checks if MARK is considered a spam mark in GROUP."
756   (spam-group-ham-mark-p group mark t))
757
758 (defun spam-group-ham-marks (group &optional spam)
759   "In GROUP, get all the ham marks."
760   (when (stringp group)
761     (let* ((marks (if spam
762                       (gnus-parameter-spam-marks group)
763                     (gnus-parameter-ham-marks group)))
764            (marks (car marks))
765            (marks (if (listp (car marks)) (car marks) marks)))
766       marks)))
767
768 (defun spam-group-spam-marks (group)
769   "In GROUP, get all the spam marks."
770   (spam-group-ham-marks group t))
771
772 (defun spam-group-spam-contents-p (group)
773   "Is GROUP a spam group?"
774   (if (and (stringp group) (< 0 (length group)))
775       (or (member group spam-junk-mailgroups)
776           (memq 'gnus-group-spam-classification-spam
777                 (gnus-parameter-spam-contents group)))
778     nil))
779
780 (defun spam-group-ham-contents-p (group)
781   "Is GROUP a ham group?"
782   (if (stringp group)
783       (memq 'gnus-group-spam-classification-ham
784             (gnus-parameter-spam-contents group))
785     nil))
786
787 (defun spam-classifications ()
788   "Return list of valid classifications"
789   '(spam ham))
790
791 (defun spam-classification-valid-p (classification)
792   "Is CLASSIFICATION a valid spam/ham classification?"
793   (memq classification (spam-classifications)))
794
795 (defun spam-backend-properties ()
796   "Return list of valid classifications."
797   '(statistical mover check hrf srf huf suf))
798
799 (defun spam-backend-property-valid-p (property)
800   "Is PROPERTY a valid backend property?"
801   (memq property (spam-backend-properties)))
802
803 (defun spam-backend-function-type-valid-p (type)
804   (or (eq type 'registration)
805       (eq type 'unregistration)))
806
807 (defun spam-process-type-valid-p (process-type)
808   (or (eq process-type 'incoming)
809       (eq process-type 'process)))
810
811 (defun spam-list-articles (articles classification)
812   (let ((mark-check (if (eq classification 'spam)
813                         'spam-group-spam-mark-p
814                       'spam-group-ham-mark-p))
815         alist mark-cache-yes mark-cache-no)
816     (dolist (article articles)
817       (let ((mark (gnus-summary-article-mark article)))
818         (unless (or (memq mark mark-cache-yes)
819                     (memq mark mark-cache-no))
820           (if (funcall mark-check
821                        gnus-newsgroup-name
822                        mark)
823               (push mark mark-cache-yes)
824             (push mark mark-cache-no)))
825         (when (memq mark mark-cache-yes)
826           (push article alist))))
827     alist))
828
829 ;;}}}
830
831 ;;{{{ backend installation functions and procedures
832
833 (defun spam-install-backend-super (backend &rest properties)
834   "Install BACKEND for spam.el.
835 Accepts incoming CHECK, ham registration function HRF, spam
836 registration function SRF, ham unregistration function HUF, spam
837 unregistration function SUF, and an indication whether the
838 backend is STATISTICAL."
839   (setq spam-backends (add-to-list 'spam-backends backend))
840   (while properties
841     (let ((property (pop properties))
842           (value (pop properties)))
843       (if (spam-backend-property-valid-p property)
844           (put backend property value)
845         (gnus-error
846          5
847          "spam-install-backend-super got an invalid property %s"
848          property)))))
849
850 (defun spam-backend-list (&optional type)
851   "Return a list of all the backend symbols, constrained by TYPE.
852 When TYPE is 'non-mover, only non-mover backends are returned.
853 When TYPE is 'mover, only mover backends are returned."
854   (let (list)
855     (dolist (backend spam-backends)
856       (when (or
857              (null type)                ;either no type was requested
858              ;; or the type is 'mover and the backend is a mover
859              (and
860               (eq type 'mover)
861               (spam-backend-mover-p backend))
862              ;; or the type is 'non-mover and the backend is not a mover
863              (and
864               (eq type 'non-mover)
865               (not (spam-backend-mover-p backend))))
866         (push backend list)))
867       list))
868
869 (defun spam-backend-check (backend)
870   "Get the check function for BACKEND.
871 Each individual check may return nil, t, or a mailgroup name.
872 The value nil means that the check does not yield a decision, and
873 so, that further checks are needed.  The value t means that the
874 message is definitely not spam, and that further spam checks
875 should be inhibited.  Otherwise, a mailgroup name or the symbol
876 'spam (depending on `spam-split-symbolic-return') is returned where
877 the mail should go, and further checks are also inhibited.  The
878 usual mailgroup name is the value of `spam-split-group', meaning
879 that the message is definitely a spam."
880   (get backend 'check))
881
882 (defun spam-backend-valid-p (backend)
883   "Is BACKEND valid?"
884   (member backend (spam-backend-list)))
885
886 (defun spam-backend-info (backend)
887   "Return information about BACKEND."
888   (if (spam-backend-valid-p backend)
889       (let (info)
890         (setq info (format "Backend %s has the following properties:\n"
891                            backend))
892         (dolist (property (spam-backend-properties))
893           (setq info (format "%s%s=%s\n"
894                              info
895                              property
896                              (get backend property))))
897         info)
898     (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
899                 backend)))
900
901 (defun spam-backend-function (backend classification type)
902   "Get the BACKEND function for CLASSIFICATION and TYPE.
903 TYPE is 'registration or 'unregistration.
904 CLASSIFICATION is 'ham or 'spam."
905   (if (and
906        (spam-classification-valid-p classification)
907        (spam-backend-function-type-valid-p type))
908       (let ((retrieval
909              (intern
910               (format "spam-backend-%s-%s-function"
911                       classification
912                       type))))
913         (funcall retrieval backend))
914     (gnus-error
915      5
916      "%s was passed invalid backend %s, classification %s, or type %s"
917      "spam-backend-function"
918      backend
919      classification
920      type)))
921
922 (defun spam-backend-article-list-property (classification
923                                            &optional unregister)
924   "Property name of article list with CLASSIFICATION and UNREGISTER."
925   (let* ((r (if unregister "unregister" "register"))
926          (prop (format "%s-%s" classification r)))
927     prop))
928
929 (defun spam-backend-get-article-todo-list (backend
930                                            classification
931                                            &optional unregister)
932   "Get the articles to be processed for BACKEND and CLASSIFICATION.
933 With UNREGISTER, get articles to be unregistered.
934 This is a temporary storage function - nothing here persists."
935   (get
936    backend
937    (intern (spam-backend-article-list-property classification unregister))))
938
939 (defun spam-backend-put-article-todo-list (backend classification list &optional unregister)
940   "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
941 With UNREGISTER, set articles to be unregistered.
942 This is a temporary storage function - nothing here persists."
943   (put
944    backend
945    (intern (spam-backend-article-list-property classification unregister))
946    list))
947
948 (defun spam-backend-ham-registration-function (backend)
949   "Get the ham registration function for BACKEND."
950   (get backend 'hrf))
951
952 (defun spam-backend-spam-registration-function (backend)
953   "Get the spam registration function for BACKEND."
954   (get backend 'srf))
955
956 (defun spam-backend-ham-unregistration-function (backend)
957   "Get the ham unregistration function for BACKEND."
958   (get backend 'huf))
959
960 (defun spam-backend-spam-unregistration-function (backend)
961   "Get the spam unregistration function for BACKEND."
962   (get backend 'suf))
963
964 (defun spam-backend-statistical-p (backend)
965   "Is BACKEND statistical?"
966   (get backend 'statistical))
967
968 (defun spam-backend-mover-p (backend)
969   "Is BACKEND a mover?"
970   (get backend 'mover))
971
972 (defun spam-install-backend-alias (backend alias)
973   "Add ALIAS to an existing BACKEND.
974 The previous backend settings for ALIAS are erased."
975
976   ;; install alias with no properties at first
977   (spam-install-backend-super alias)
978
979   (dolist (property (spam-backend-properties))
980     (put alias property (get backend property))))
981
982 (defun spam-install-checkonly-backend (backend check)
983   "Install a BACKEND than can only CHECK for spam."
984   (spam-install-backend-super backend 'check check))
985
986 (defun spam-install-mover-backend (backend hrf srf huf suf)
987   "Install a BACKEND than can move articles at summary exit.
988 Accepts ham registration function HRF, spam registration function
989 SRF, ham unregistration function HUF, spam unregistration
990 function SUF.  The backend has no incoming check and can't be
991 statistical."
992   (spam-install-backend-super
993    backend
994    'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
995
996 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
997   "Install a BACKEND than has no check.
998 Accepts ham registration function HRF, spam registration function
999 SRF, ham unregistration function HUF, spam unregistration
1000 function SUF.  The backend has no incoming check and can't be
1001 statistical (it could be, but in practice that doesn't happen)."
1002   (spam-install-backend-super
1003    backend
1004    'hrf hrf 'srf srf 'huf huf 'suf suf))
1005
1006 (defun spam-install-backend (backend check hrf srf huf suf)
1007   "Install a BACKEND.
1008 Accepts incoming CHECK, ham registration function HRF, spam
1009 registration function SRF, ham unregistration function HUF, spam
1010 unregistration function SUF.  The backend won't be
1011 statistical (use `spam-install-statistical-backend' for that)."
1012   (spam-install-backend-super
1013    backend
1014    'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
1015
1016 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
1017   "Install a BACKEND.
1018 Accepts incoming CHECK, ham registration function HRF, spam
1019 registration function SRF, ham unregistration function HUF, spam
1020 unregistration function SUF.  The backend will be
1021 statistical (use `spam-install-backend' for non-statistical
1022 backends)."
1023   (spam-install-backend-super
1024    backend
1025    'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1026
1027 (defun spam-install-statistical-checkonly-backend (backend check)
1028   "Install a statistical BACKEND than can only CHECK for spam."
1029   (spam-install-backend-super
1030    backend
1031    'check check 'statistical t))
1032
1033 ;;}}}
1034
1035 ;;{{{ backend installations
1036 (spam-install-checkonly-backend 'spam-use-blackholes
1037                                 'spam-check-blackholes)
1038
1039 (spam-install-checkonly-backend 'spam-use-hashcash
1040                                 'spam-check-hashcash)
1041
1042 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1043                                 'spam-check-spamassassin-headers)
1044
1045 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1046                                 'spam-check-bogofilter-headers)
1047
1048 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1049                                 'spam-check-bsfilter-headers)
1050
1051 (spam-install-checkonly-backend 'spam-use-gmane-xref
1052                                 'spam-check-gmane-xref)
1053
1054 (spam-install-checkonly-backend 'spam-use-regex-headers
1055                                 'spam-check-regex-headers)
1056
1057 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1058                                             'spam-check-regex-body)
1059
1060 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy) instead
1061 (spam-install-mover-backend 'spam-use-move
1062                             'spam-move-ham-routine
1063                             'spam-move-spam-routine
1064                             nil
1065                             nil)
1066
1067 (spam-install-nocheck-backend 'spam-use-copy
1068                               'spam-copy-ham-routine
1069                               'spam-copy-spam-routine
1070                               nil
1071                               nil)
1072
1073 (spam-install-nocheck-backend 'spam-use-gmane
1074                               'spam-report-gmane-unregister-routine
1075                               'spam-report-gmane-register-routine
1076                               'spam-report-gmane-register-routine
1077                               'spam-report-gmane-unregister-routine)
1078
1079 (spam-install-nocheck-backend 'spam-use-resend
1080                               'spam-report-resend-register-ham-routine
1081                               'spam-report-resend-register-routine
1082                               nil
1083                               nil)
1084
1085 (spam-install-backend 'spam-use-BBDB
1086                       'spam-check-BBDB
1087                       'spam-BBDB-register-routine
1088                       nil
1089                       'spam-BBDB-unregister-routine
1090                       nil)
1091
1092 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1093
1094 (spam-install-backend 'spam-use-blacklist
1095                       'spam-check-blacklist
1096                       nil
1097                       'spam-blacklist-register-routine
1098                       nil
1099                       'spam-blacklist-unregister-routine)
1100
1101 (spam-install-backend 'spam-use-whitelist
1102                       'spam-check-whitelist
1103                       'spam-whitelist-register-routine
1104                       nil
1105                       'spam-whitelist-unregister-routine
1106                       nil)
1107
1108 (spam-install-statistical-backend 'spam-use-ifile
1109                                   'spam-check-ifile
1110                                   'spam-ifile-register-ham-routine
1111                                   'spam-ifile-register-spam-routine
1112                                   'spam-ifile-unregister-ham-routine
1113                                   'spam-ifile-unregister-spam-routine)
1114
1115 (spam-install-statistical-backend 'spam-use-spamoracle
1116                                   'spam-check-spamoracle
1117                                   'spam-spamoracle-learn-ham
1118                                   'spam-spamoracle-learn-spam
1119                                   'spam-spamoracle-unlearn-ham
1120                                   'spam-spamoracle-unlearn-spam)
1121
1122 (spam-install-statistical-backend 'spam-use-stat
1123                                   'spam-check-stat
1124                                   'spam-stat-register-ham-routine
1125                                   'spam-stat-register-spam-routine
1126                                   'spam-stat-unregister-ham-routine
1127                                   'spam-stat-unregister-spam-routine)
1128
1129 (spam-install-statistical-backend 'spam-use-spamassassin
1130                                   'spam-check-spamassassin
1131                                   'spam-spamassassin-register-ham-routine
1132                                   'spam-spamassassin-register-spam-routine
1133                                   'spam-spamassassin-unregister-ham-routine
1134                                   'spam-spamassassin-unregister-spam-routine)
1135
1136 (spam-install-statistical-backend 'spam-use-bogofilter
1137                                   'spam-check-bogofilter
1138                                   'spam-bogofilter-register-ham-routine
1139                                   'spam-bogofilter-register-spam-routine
1140                                   'spam-bogofilter-unregister-ham-routine
1141                                   'spam-bogofilter-unregister-spam-routine)
1142
1143 (spam-install-statistical-backend 'spam-use-bsfilter
1144                                   'spam-check-bsfilter
1145                                   'spam-bsfilter-register-ham-routine
1146                                   'spam-bsfilter-register-spam-routine
1147                                   'spam-bsfilter-unregister-ham-routine
1148                                   'spam-bsfilter-unregister-spam-routine)
1149
1150 (spam-install-statistical-backend 'spam-use-crm114
1151                                   'spam-check-crm114
1152                                   'spam-crm114-register-ham-routine
1153                                   'spam-crm114-register-spam-routine
1154                                   ;; does CRM114 Mailfilter support unregistration?
1155                                   nil
1156                                   nil)
1157
1158 ;;}}}
1159
1160 ;;{{{ scoring and summary formatting
1161 (defun spam-necessary-extra-headers ()
1162   "Return the extra headers spam.el thinks are necessary."
1163   (let (list)
1164     (when (or spam-use-spamassassin
1165               spam-use-spamassassin-headers
1166               spam-use-regex-headers)
1167       (push 'X-Spam-Status list))
1168     (when (or spam-use-bogofilter
1169               spam-use-regex-headers)
1170       (push 'X-Bogosity list))
1171     (when (or spam-use-crm114
1172               spam-use-regex-headers)
1173       (push 'X-CRM114-Status list))
1174     list))
1175
1176 (defun spam-user-format-function-S (headers)
1177   (when headers
1178     (format "%3.2f"
1179             (spam-summary-score headers spam-summary-score-preferred-header))))
1180
1181 (defun spam-article-sort-by-spam-status (h1 h2)
1182   "Sort articles by score."
1183   (let (result)
1184     (dolist (header (spam-necessary-extra-headers))
1185       (let ((s1 (spam-summary-score h1 header))
1186             (s2 (spam-summary-score h2 header)))
1187       (unless (= s1 s2)
1188         (setq result (< s1 s2))
1189         (return))))
1190     result))
1191
1192 (defvar spam-spamassassin-score-regexp
1193   ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
1194   "Regexp matching SpamAssassin score header.
1195 The first group must match the number.")
1196
1197 (defun spam-extra-header-to-number (header headers)
1198   "Transform an extra HEADER to a number, using list of HEADERS.
1199 Note this has to be fast."
1200   (let ((header-content (gnus-extra-header header headers)))
1201     (if header-content
1202         (cond
1203          ((eq header 'X-Spam-Status)
1204           (string-to-number (gnus-replace-in-string
1205                              header-content
1206                              spam-spamassassin-score-regexp
1207                              "\\1")))
1208          ;; for CRM checking, it's probably faster to just do the string match
1209          ((string-match "( pR: \\([0-9.-]+\\)" header-content)
1210           (- (string-to-number (match-string 1 header-content))))
1211          ((eq header 'X-Bogosity)
1212           (string-to-number (gnus-replace-in-string
1213                              (gnus-replace-in-string
1214                               header-content
1215                               ".*spamicity=" "")
1216                              ",.*" "")))
1217          (t nil))
1218       nil)))
1219
1220 (defun spam-summary-score (headers &optional specific-header)
1221   "Score an artic