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