* gnus-registry.el: Silence XEmacs byte compiler.
[gnus] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2
3 ;; Copyright (C) 2002-2013 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-get-id-key "gnus-registry")
72 (autoload 'gnus-registry-set-id-key "gnus-registry")
73 (autoload 'gnus-registry-handle-action "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 messages 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       (cond
1585        ((not (arrayp data-header))
1586         (gnus-message 6 "Article %d has a nil data header" article))
1587        ((equal field 'number)
1588         (mail-header-number data-header))
1589        ((equal field 'from)
1590         (mail-header-from data-header))
1591        ((equal field 'message-id)
1592         (mail-header-message-id data-header))
1593        ((equal field 'subject)
1594         (mail-header-subject data-header))
1595        ((equal field 'references)
1596         (mail-header-references data-header))
1597        ((equal field 'date)
1598         (mail-header-date data-header))
1599        ((equal field 'xref)
1600         (mail-header-xref data-header))
1601        ((equal field 'extra)
1602         (mail-header-extra data-header))
1603        (t
1604         (gnus-error
1605          5
1606          "spam-fetch-field-fast: unknown field %s requested"
1607          field)
1608         nil)))))
1609
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-handle-action id nil group subject sender))
1768
1769                (unless registry-lookup
1770                  (spam-log-processing-to-registry
1771                   id
1772                   'incoming
1773                   split-return
1774                   spam-split-last-successful-check
1775                   group))))))
1776        articles))))
1777
1778 ;;}}}
1779
1780 ;;{{{ registration/unregistration functions
1781
1782 (defun spam-resolve-registrations-routine ()
1783   "Go through the backends and register or unregister articles as needed."
1784   (dolist (backend-type '(non-mover mover))
1785     (dolist (classification (spam-classifications))
1786       (dolist (backend (spam-backend-list backend-type))
1787         (let ((rlist (spam-backend-get-article-todo-list
1788                       backend classification))
1789               (ulist (spam-backend-get-article-todo-list
1790                       backend classification t))
1791               (delcount 0))
1792
1793           ;; clear the old lists right away
1794           (spam-backend-put-article-todo-list backend
1795                                               classification
1796                                               nil
1797                                               nil)
1798           (spam-backend-put-article-todo-list backend
1799                                               classification
1800                                               nil
1801                                               t)
1802
1803           ;; eliminate duplicates
1804           (dolist (article (copy-sequence ulist))
1805             (when (memq article rlist)
1806               (incf delcount)
1807               (setq rlist (delq article rlist))
1808               (setq ulist (delq article ulist))))
1809
1810           (unless (zerop delcount)
1811             (gnus-message
1812              9
1813              "%d messages did not have to unregister and then register"
1814              delcount))
1815
1816           ;; unregister articles
1817           (unless (zerop (length ulist))
1818             (let ((num (spam-unregister-routine classification backend ulist)))
1819               (when (> num 0)
1820                 (gnus-message
1821                  6
1822                  "%d %s messages were unregistered by backend %s."
1823                  num
1824                  classification
1825                  backend))))
1826
1827             ;; register articles
1828             (unless (zerop (length rlist))
1829               (let ((num (spam-register-routine classification backend rlist)))
1830                 (when (> num 0)
1831                   (gnus-message
1832                    6
1833                    "%d %s messages were registered by backend %s."
1834                    num
1835                    classification
1836                    backend)))))))))
1837
1838 (defun spam-unregister-routine (classification
1839                                 backend
1840                                 specific-articles)
1841   (spam-register-routine classification backend specific-articles t))
1842
1843 (defun spam-register-routine (classification
1844                               backend
1845                               specific-articles
1846                               &optional unregister)
1847   (when (and (spam-classification-valid-p classification)
1848              (spam-backend-valid-p backend))
1849     (let* ((register-function
1850             (spam-backend-function backend classification 'registration))
1851            (unregister-function
1852             (spam-backend-function backend classification 'unregistration))
1853            (run-function (if unregister
1854                              unregister-function
1855                            register-function))
1856            (log-function (if unregister
1857                              'spam-log-undo-registration
1858                            'spam-log-processing-to-registry))
1859            article articles)
1860
1861       (when run-function
1862         ;; make list of articles, using specific-articles if given
1863         (setq articles (or specific-articles
1864                            (spam-list-articles
1865                             gnus-newsgroup-articles
1866                             classification)))
1867         ;; process them
1868         (when (> (length articles) 0)
1869           (gnus-message 5 "%s %d %s articles as %s using backend %s"
1870                         (if unregister "Unregistering" "Registering")
1871                         (length articles)
1872                         (if specific-articles "specific" "")
1873                         classification
1874                         backend)
1875           (funcall run-function articles)
1876           ;; now log all the registrations (or undo them, depending on
1877           ;; unregister)
1878           (dolist (article articles)
1879             (funcall log-function
1880                      (spam-fetch-field-message-id-fast article)
1881                      'process
1882                      classification
1883                      backend
1884                      gnus-newsgroup-name))))
1885       ;; return the number of articles processed
1886       (length articles))))
1887
1888 ;;; log a ham- or spam-processor invocation to the registry
1889 (defun spam-log-processing-to-registry (id type classification backend group)
1890   (when spam-log-to-registry
1891     (if (and (stringp id)
1892              (stringp group)
1893              (spam-process-type-valid-p type)
1894              (spam-classification-valid-p classification)
1895              (spam-backend-valid-p backend))
1896         (let ((cell-list (gnus-registry-get-id-key id type))
1897               (cell (list classification backend group)))
1898           (push cell cell-list)
1899           (gnus-registry-set-id-key id type cell-list))
1900
1901       (gnus-error
1902        7
1903        (format
1904         "%s call with bad ID, type, classification, spam-backend, or group"
1905         "spam-log-processing-to-registry")))))
1906
1907 ;;; check if a ham- or spam-processor registration has been done
1908 (defun spam-log-registered-p (id type)
1909   (when spam-log-to-registry
1910     (if (and (stringp id)
1911              (spam-process-type-valid-p type))
1912         (gnus-registry-get-id-key id type)
1913       (progn
1914         (gnus-error
1915          7
1916          (format "%s called with bad ID, type, classification, or spam-backend"
1917                  "spam-log-registered-p"))
1918         nil))))
1919
1920 ;;; check what a ham- or spam-processor registration says
1921 ;;; returns nil if conflicting registrations are found
1922 (defun spam-log-registration-type (id type)
1923   (let ((count 0)
1924         decision)
1925     (dolist (reg (spam-log-registered-p id type))
1926       (let ((classification (nth 0 reg)))
1927         (when (spam-classification-valid-p classification)
1928           (when (and decision
1929                      (not (eq classification decision)))
1930             (setq count (+ 1 count)))
1931           (setq decision classification))))
1932     (if (< 0 count)
1933         nil
1934       decision)))
1935
1936
1937 ;;; check if a ham- or spam-processor registration needs to be undone
1938 (defun spam-log-unregistration-needed-p (id type classification backend)
1939   (when spam-log-to-registry
1940     (if (and (stringp id)
1941              (spam-process-type-valid-p type)
1942              (spam-classification-valid-p classification)
1943              (spam-backend-valid-p backend))
1944         (let ((cell-list (gnus-registry-get-id-key id type))
1945               found)
1946           (dolist (cell cell-list)
1947             (unless found
1948               (when (and (eq classification (nth 0 cell))
1949                          (eq backend (nth 1 cell)))
1950                 (setq found t))))
1951           found)
1952       (progn
1953         (gnus-error
1954          7
1955          (format "%s called with bad ID, type, classification, or spam-backend"
1956                  "spam-log-unregistration-needed-p"))
1957         nil))))
1958
1959
1960 ;;; undo a ham- or spam-processor registration (the group is not used)
1961 (defun spam-log-undo-registration (id type classification backend
1962                                       &optional group)
1963   (when (and spam-log-to-registry
1964              (spam-log-unregistration-needed-p id type classification backend))
1965     (if (and (stringp id)
1966              (spam-process-type-valid-p type)
1967              (spam-classification-valid-p classification)
1968              (spam-backend-valid-p backend))
1969         (let ((cell-list (gnus-registry-get-id-key id type))
1970               new-cell-list found)
1971           (dolist (cell cell-list)
1972             (unless (and (eq classification (nth 0 cell))
1973                          (eq backend (nth 1 cell)))
1974               (push cell new-cell-list)))
1975           (gnus-registry-set-id-key id type new-cell-list))
1976       (progn
1977         (gnus-error 7 (format
1978                        "%s call with bad ID, type, spam-backend, or group"
1979                        "spam-log-undo-registration"))
1980         nil))))
1981
1982 ;;}}}
1983
1984 ;;{{{ backend functions
1985
1986 ;;{{{ Gmane xrefs
1987 (defun spam-check-gmane-xref ()
1988   (let ((header (or
1989                  (message-fetch-field "Xref")
1990                  (message-fetch-field "Newsgroups"))))
1991     (when header                        ; return nil when no header
1992       (when (string-match spam-gmane-xref-spam-group
1993                           header)
1994           spam-split-group))))
1995
1996 ;;}}}
1997
1998 ;;{{{ Regex body
1999
2000 (defun spam-check-regex-body ()
2001   (let ((spam-regex-headers-ham spam-regex-body-ham)
2002         (spam-regex-headers-spam spam-regex-body-spam))
2003     (spam-check-regex-headers t)))
2004
2005 ;;}}}
2006
2007 ;;{{{ Regex headers
2008
2009 (defun spam-check-regex-headers (&optional body)
2010   (let ((type (if body "body" "header"))
2011         ret found)
2012     (dolist (h-regex spam-regex-headers-ham)
2013       (unless found
2014         (goto-char (point-min))
2015         (when (re-search-forward h-regex nil t)
2016           (message "Ham regex %s search positive." type)
2017           (setq found t))))
2018     (dolist (s-regex spam-regex-headers-spam)
2019       (unless found
2020         (goto-char (point-min))
2021         (when (re-search-forward s-regex nil t)
2022           (message "Spam regex %s search positive." type)
2023           (setq found t)
2024           (setq ret spam-split-group))))
2025     ret))
2026
2027 ;;}}}
2028
2029 ;;{{{ Blackholes.
2030
2031 (defun spam-reverse-ip-string (ip)
2032   (when (stringp ip)
2033     (mapconcat 'identity
2034                (nreverse (split-string ip "\\."))
2035                ".")))
2036
2037 (defun spam-check-blackholes ()
2038   "Check the Received headers for blackholed relays."
2039   (let ((headers (message-fetch-field "received"))
2040         ips matches)
2041     (when headers
2042       (with-temp-buffer
2043         (insert headers)
2044         (goto-char (point-min))
2045         (gnus-message 6 "Checking headers for relay addresses")
2046         (while (re-search-forward
2047                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
2048           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
2049           (push (spam-reverse-ip-string (match-string 1))
2050                 ips)))
2051       (dolist (server spam-blackhole-servers)
2052         (dolist (ip ips)
2053           (unless (and spam-blackhole-good-server-regex
2054                        ;; match against the reversed (again) IP string
2055                        (string-match
2056                         spam-blackhole-good-server-regex
2057                         (spam-reverse-ip-string ip)))
2058             (unless matches
2059               (let ((query-string (concat ip "." server)))
2060                 (if spam-use-dig
2061                     (let ((query-result (query-dig query-string)))
2062                       (when query-result
2063                         (gnus-message 6 "(DIG): positive blackhole check '%s'"
2064                                       query-result)
2065                         (push (list ip server query-result)
2066                               matches)))
2067                   ;; else, if not using dig.el
2068                   (when (dns-query query-string)
2069                     (gnus-message 6 "positive blackhole check")
2070                     (push (list ip server (dns-query query-string 'TXT))
2071                           matches)))))))))
2072     (when matches
2073       spam-split-group)))
2074 ;;}}}
2075
2076 ;;{{{ Hashcash.
2077
2078 (defun spam-check-hashcash ()
2079   "Check the headers for hashcash payments."
2080   (ignore-errors (mail-check-payment)))  ;mail-check-payment returns a boolean
2081
2082 ;;}}}
2083
2084 ;;{{{ BBDB
2085
2086 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2087 ;;; <sacha@giotto.sj.ru>
2088
2089 ;; all this is done inside a condition-case to trap errors
2090
2091 ;; Autoloaded in message, which we require.
2092 (declare-function gnus-extract-address-components "gnus-util" (from))
2093
2094 (eval-and-compile
2095   (condition-case nil
2096       (progn
2097         (require 'bbdb)
2098         (require 'bbdb-com))
2099     (file-error
2100      ;; `bbdb-records' should not be bound as an autoload function
2101      ;; before loading bbdb because of `bbdb-hashtable-size'.
2102      (defalias 'bbdb-buffer 'ignore)
2103      (defalias 'bbdb-create-internal 'ignore)
2104      (defalias 'bbdb-records 'ignore)
2105      (defalias 'spam-BBDB-register-routine 'ignore)
2106      (defalias 'spam-enter-ham-BBDB 'ignore)
2107      (defalias 'spam-exists-in-BBDB-p 'ignore)
2108      (defalias 'bbdb-gethash 'ignore)
2109      nil)))
2110
2111 (eval-and-compile
2112   (when (featurep 'bbdb-com)
2113     ;; when the BBDB changes, we want to clear out our cache
2114     (defun spam-clear-cache-BBDB (&rest immaterial)
2115       (spam-clear-cache 'spam-use-BBDB))
2116
2117     (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2118
2119     (defun spam-enter-ham-BBDB (addresses &optional remove)
2120       "Enter an address into the BBDB; implies ham (non-spam) sender"
2121       (dolist (from addresses)
2122         (when (stringp from)
2123           (let* ((parsed-address (gnus-extract-address-components from))
2124                  (name (or (nth 0 parsed-address) "Ham Sender"))
2125                  (remove-function (if remove
2126                                       'bbdb-delete-record-internal
2127                                     'ignore))
2128                  (net-address (nth 1 parsed-address))
2129                  (record (and net-address
2130                               (spam-exists-in-BBDB-p net-address))))
2131             (when net-address
2132               (gnus-message 6 "%s address %s %s BBDB"
2133                             (if remove "Deleting" "Adding")
2134                             from
2135                             (if remove "from" "to"))
2136               (if record
2137                   (funcall remove-function record)
2138                 (bbdb-create-internal name nil net-address nil nil
2139                                       "ham sender added by spam.el")))))))
2140
2141     (defun spam-BBDB-register-routine (articles &optional unregister)
2142       (let (addresses)
2143         (dolist (article articles)
2144           (when (stringp (spam-fetch-field-from-fast article))
2145             (push (spam-fetch-field-from-fast article) addresses)))
2146         ;; now do the register/unregister action
2147         (spam-enter-ham-BBDB addresses unregister)))
2148
2149     (defun spam-BBDB-unregister-routine (articles)
2150       (spam-BBDB-register-routine articles t))
2151
2152     (defsubst spam-exists-in-BBDB-p (net)
2153       (when (and (stringp net) (not (zerop (length net))))
2154         (bbdb-records)
2155         (bbdb-gethash (downcase net))))
2156
2157     (defun spam-check-BBDB ()
2158       "Mail from people in the BBDB is classified as ham or non-spam"
2159       (let ((net (message-fetch-field "from")))
2160         (when net
2161           (setq net (nth 1 (gnus-extract-address-components net)))
2162           (if (spam-exists-in-BBDB-p net)
2163               t
2164             (if spam-use-BBDB-exclusive
2165                 spam-split-group
2166               nil)))))))
2167
2168 ;;}}}
2169
2170 ;;{{{ ifile
2171
2172 ;;; check the ifile backend; return nil if the mail was NOT classified
2173 ;;; as spam
2174
2175
2176 (defun spam-get-ifile-database-parameter ()
2177   "Return the command-line parameter for ifile's database.
2178 See `spam-ifile-database'."
2179   (if spam-ifile-database
2180       (format "--db-file=%s" spam-ifile-database)
2181     nil))
2182
2183 (defun spam-check-ifile ()
2184   "Check the ifile backend for the classification of this message."
2185   (let ((article-buffer-name (buffer-name))
2186         category return)
2187     (with-temp-buffer
2188       (let ((temp-buffer-name (buffer-name))
2189             (db-param (spam-get-ifile-database-parameter)))
2190         (with-current-buffer article-buffer-name
2191           (apply 'call-process-region
2192                  (point-min) (point-max) spam-ifile-program
2193                  nil temp-buffer-name nil "-c"
2194                  (if db-param `(,db-param "-q") `("-q"))))
2195         ;; check the return now (we're back in the temp buffer)
2196         (goto-char (point-min))
2197         (if (not (eobp))
2198             (setq category (buffer-substring (point) (point-at-eol))))
2199         (when (not (zerop (length category))) ; we need a category here
2200           (if spam-ifile-all-categories
2201               (setq return category)
2202             ;; else, if spam-ifile-all-categories is not set...
2203             (when (string-equal spam-ifile-spam-category category)
2204               (setq return spam-split-group)))))) ; note return is nil otherwise
2205     return))
2206
2207 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2208   "Register an article, given as a string, with a category.
2209 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2210   (let ((category (or category gnus-newsgroup-name))
2211         (add-or-delete-option (if unregister "-d" "-i"))
2212         (db (spam-get-ifile-database-parameter))
2213         parameters)
2214     (with-temp-buffer
2215       (dolist (article articles)
2216         (let ((article-string (spam-get-article-as-string article)))
2217           (when (stringp article-string)
2218             (insert article-string))))
2219       (apply 'call-process-region
2220              (point-min) (point-max) spam-ifile-program
2221              nil nil nil
2222              add-or-delete-option category
2223              (if db `(,db "-h") `("-h"))))))
2224
2225 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2226   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2227
2228 (defun spam-ifile-unregister-spam-routine (articles)
2229   (spam-ifile-register-spam-routine articles t))
2230
2231 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2232   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2233
2234 (defun spam-ifile-unregister-ham-routine (articles)
2235   (spam-ifile-register-ham-routine articles t))
2236
2237 ;;}}}
2238
2239 ;;{{{ spam-stat
2240
2241 (eval-when-compile
2242   (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2243   (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2244   (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2245   (autoload 'spam-stat-buffer-is-spam "spam-stat")
2246   (autoload 'spam-stat-load "spam-stat")
2247   (autoload 'spam-stat-save "spam-stat")
2248   (autoload 'spam-stat-split-fancy "spam-stat"))
2249
2250 (require 'spam-stat)
2251
2252 (defun spam-check-stat ()
2253   "Check the spam-stat backend for the classification of this message"
2254   (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2255         (spam-stat-buffer (buffer-name)) ; stat the current buffer
2256         category return)
2257     (spam-stat-split-fancy)))
2258
2259 (defun spam-stat-register-spam-routine (articles &optional unregister)
2260   (dolist (article articles)
2261     (let ((article-string (spam-get-article-as-string article)))
2262       (with-temp-buffer
2263         (insert article-string)
2264         (if unregister
2265             (spam-stat-buffer-change-to-non-spam)
2266           (spam-stat-buffer-is-spam))))))
2267
2268 (defun spam-stat-unregister-spam-routine (articles)
2269   (spam-stat-register-spam-routine articles t))
2270
2271 (defun spam-stat-register-ham-routine (articles &optional unregister)
2272   (dolist (article articles)
2273     (let ((article-string (spam-get-article-as-string article)))
2274       (with-temp-buffer
2275         (insert article-string)
2276         (if unregister
2277             (spam-stat-buffer-change-to-spam)
2278           (spam-stat-buffer-is-non-spam))))))
2279
2280 (defun spam-stat-unregister-ham-routine (articles)
2281   (spam-stat-register-ham-routine articles t))
2282
2283 (defun spam-maybe-spam-stat-load ()
2284   (when spam-use-stat (spam-stat-load)))
2285
2286 (defun spam-maybe-spam-stat-save ()
2287   (when spam-use-stat (spam-stat-save)))
2288
2289 ;;}}}
2290
2291 ;;{{{ Blacklists and whitelists.
2292
2293 (defvar spam-whitelist-cache nil)
2294 (defvar spam-blacklist-cache nil)
2295
2296 (defun spam-kill-whole-line ()
2297   (beginning-of-line)
2298   (let ((kill-whole-line t))
2299     (kill-line)))
2300
2301 ;;; address can be a list, too
2302 (defun spam-enter-whitelist (address &optional remove)
2303   "Enter ADDRESS (list or single) into the whitelist.
2304 With a non-nil REMOVE, remove them."
2305   (interactive "sAddress: ")
2306   (spam-enter-list address spam-whitelist remove)
2307   (setq spam-whitelist-cache nil)
2308   (spam-clear-cache 'spam-use-whitelist))
2309
2310 ;;; address can be a list, too
2311 (defun spam-enter-blacklist (address &optional remove)
2312   "Enter ADDRESS (list or single) into the blacklist.
2313 With a non-nil REMOVE, remove them."
2314   (interactive "sAddress: ")
2315   (spam-enter-list address spam-blacklist remove)
2316   (setq spam-blacklist-cache nil)
2317   (spam-clear-cache 'spam-use-whitelist))
2318
2319 (defun spam-enter-list (addresses file &optional remove)
2320   "Enter ADDRESSES into the given FILE.
2321 Either the whitelist or the blacklist files can be used.
2322 With a non-nil REMOVE, remove the ADDRESSES."
2323   (if (stringp addresses)
2324       (spam-enter-list (list addresses) file remove)
2325     ;; else, we have a list of addresses here
2326     (unless (file-exists-p (file-name-directory file))
2327       (make-directory (file-name-directory file) t))
2328     (with-current-buffer
2329        (find-file-noselect file)
2330       (dolist (a addresses)
2331         (when (stringp a)
2332           (goto-char (point-min))
2333           (if (re-search-forward (regexp-quote a) nil t)
2334               ;; found the address
2335               (when remove
2336                 (spam-kill-whole-line))
2337             ;; else, the address was not found
2338             (unless remove
2339               (goto-char (point-max))
2340               (unless (bobp)
2341                 (insert "\n"))
2342               (insert a "\n")))))
2343       (save-buffer))))
2344
2345 (defun spam-filelist-build-cache (type)
2346   (let ((cache (if (eq type 'spam-use-blacklist)
2347                    spam-blacklist-cache
2348                  spam-whitelist-cache))
2349         parsed-cache)
2350     (unless (gethash type spam-caches)
2351       (while cache
2352         (let ((address (pop cache)))
2353           (unless (zerop (length address)) ; 0 for a nil address too
2354             (setq address (regexp-quote address))
2355             ;; fix regexp-quote's treatment of user-intended regexes
2356             (while (string-match "\\\\\\*" address)
2357               (setq address (replace-match ".*" t t address))))
2358           (push address parsed-cache)))
2359       (puthash type parsed-cache spam-caches))))
2360
2361 (defun spam-filelist-check-cache (type from)
2362   (when (stringp from)
2363     (spam-filelist-build-cache type)
2364     (let (found)
2365       (dolist (address (gethash type spam-caches))
2366         (when (and address (string-match address from))
2367           (setq found t)
2368           (return)))
2369       found)))
2370
2371 ;;; returns t if the sender is in the whitelist, nil or
2372 ;;; spam-split-group otherwise
2373 (defun spam-check-whitelist ()
2374   ;; FIXME!  Should it detect when file timestamps change?
2375   (unless spam-whitelist-cache
2376     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2377   (if (spam-from-listed-p 'spam-use-whitelist)
2378       t
2379     (if spam-use-whitelist-exclusive
2380         spam-split-group
2381       nil)))
2382
2383 (defun spam-check-blacklist ()
2384   ;; FIXME!  Should it detect when file timestamps change?
2385   (unless spam-blacklist-cache
2386     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2387   (and (spam-from-listed-p 'spam-use-blacklist)
2388        spam-split-group))
2389
2390 (defun spam-parse-list (file)
2391   (when (file-readable-p file)
2392     (let (contents address)
2393       (with-temp-buffer
2394         (insert-file-contents file)
2395         (while (not (eobp))
2396           (setq address (buffer-substring (point) (point-at-eol)))
2397           (forward-line 1)
2398           ;; insert the e-mail address if detected, otherwise the raw data
2399           (unless (zerop (length address))
2400             (let ((pure-address
2401                    (nth 1 (gnus-extract-address-components address))))
2402               (push (or pure-address address) contents)))))
2403       (nreverse contents))))
2404
2405 (defun spam-from-listed-p (type)
2406   (let ((from (message-fetch-field "from"))
2407         found)
2408     (spam-filelist-check-cache type from)))
2409
2410 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2411   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2412         (declassification (if blacklist 'ham 'spam))
2413         (enter-function
2414          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2415         (remove-function
2416          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2417         from addresses unregister-list article-unregister-list)
2418     (dolist (article articles)
2419       (let ((from (spam-fetch-field-from-fast article))
2420             (id (spam-fetch-field-message-id-fast article))
2421             sender-ignored)
2422         (when (stringp from)
2423           (dolist (ignore-regex spam-blacklist-ignored-regexes)
2424             (when (and (not sender-ignored)
2425                        (stringp ignore-regex)
2426                        (string-match ignore-regex from))
2427               (setq sender-ignored t)))
2428           ;; remember the messages we need to unregister, unless remove is set
2429           (when (and
2430                  (null unregister)
2431                  (spam-log-unregistration-needed-p
2432                   id 'process declassification de-symbol))
2433             (push article article-unregister-list)
2434             (push from unregister-list))
2435           (unless sender-ignored
2436             (push from addresses)))))
2437
2438     (if unregister
2439         (funcall enter-function addresses t) ; unregister all these addresses
2440       ;; else, register normally and unregister what we need to
2441       (funcall remove-function unregister-list t)
2442       (dolist (article article-unregister-list)
2443         (spam-log-undo-registration
2444          (spam-fetch-field-message-id-fast article)
2445          'process
2446          declassification
2447          de-symbol))
2448       (funcall enter-function addresses nil))))
2449
2450 (defun spam-blacklist-unregister-routine (articles)
2451   (spam-blacklist-register-routine articles t))
2452
2453 (defun spam-blacklist-register-routine (articles &optional unregister)
2454   (spam-filelist-register-routine articles t unregister))
2455
2456 (defun spam-whitelist-unregister-routine (articles)
2457   (spam-whitelist-register-routine articles t))
2458
2459 (defun spam-whitelist-register-routine (articles &optional unregister)
2460   (spam-filelist-register-routine articles nil unregister))
2461
2462 ;;}}}
2463
2464 ;;{{{ Spam-report glue (gmane and resend reporting)
2465 (defun spam-report-gmane-register-routine (articles)
2466   (when articles
2467     (apply 'spam-report-gmane-spam articles)))
2468
2469 (defun spam-report-gmane-unregister-routine (articles)
2470   (when articles
2471     (apply 'spam-report-gmane-ham articles)))
2472
2473 (defun spam-report-resend-register-ham-routine (articles)
2474   (spam-report-resend-register-routine articles t))
2475
2476 (defun spam-report-resend-register-routine (articles &optional ham)
2477   (let* ((resend-to-gp
2478           (if ham
2479               (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2480             (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2481          (spam-report-resend-to (or (car-safe resend-to-gp)
2482                                     spam-report-resend-to)))
2483     (spam-report-resend articles ham)))
2484
2485 ;;}}}
2486
2487 ;;{{{ Bogofilter
2488 (defun spam-check-bogofilter-headers (&optional score)
2489   (let ((header (message-fetch-field spam-bogofilter-header)))
2490     (when header                        ; return nil when no header
2491       (if score                         ; scoring mode
2492           (if (string-match "spamicity=\\([0-9.]+\\)" header)
2493               (match-string 1 header)
2494             "0")
2495         ;; spam detection mode
2496         (when (string-match spam-bogofilter-bogosity-positive-spam-header
2497                             header)
2498           spam-split-group)))))
2499
2500 ;; return something sensible if the score can't be determined
2501 (defun spam-bogofilter-score (&optional recheck)
2502   "Get the Bogofilter spamicity score."
2503   (interactive "P")
2504   (save-window-excursion
2505     (gnus-summary-show-article t)
2506     (set-buffer gnus-article-buffer)
2507     (let ((score (or (unless recheck
2508                        (spam-check-bogofilter-headers t))
2509                      (spam-check-bogofilter t))))
2510       (gnus-summary-show-article)
2511       (message "Spamicity score %s" score)
2512       (or score "0"))))
2513
2514 (defun spam-verify-bogofilter ()
2515   "Verify the Bogofilter version is sufficient."
2516   (when (eq spam-bogofilter-valid 'unknown)
2517     (setq spam-bogofilter-valid
2518           (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2519                              (shell-command-to-string
2520                               (format "%s -V" spam-bogofilter-program))))))
2521   spam-bogofilter-valid)
2522
2523 (defun spam-check-bogofilter (&optional score)
2524   "Check the Bogofilter backend for the classification of this message."
2525   (if (spam-verify-bogofilter)
2526       (let ((article-buffer-name (buffer-name))
2527             (db spam-bogofilter-database-directory)
2528             return)
2529         (with-temp-buffer
2530           (let ((temp-buffer-name (buffer-name)))
2531             (with-current-buffer article-buffer-name
2532               (apply 'call-process-region
2533                      (point-min) (point-max)
2534                      spam-bogofilter-program
2535                      nil temp-buffer-name nil
2536                      (if db `("-d" ,db "-v") `("-v"))))
2537             (setq return (spam-check-bogofilter-headers score))))
2538         return)
2539     (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2540
2541 (defun spam-bogofilter-register-with-bogofilter (articles
2542                                                  spam
2543                                                  &optional unregister)
2544   "Register an article, given as a string, as spam or non-spam."
2545   (if (spam-verify-bogofilter)
2546       (dolist (article articles)
2547         (let ((article-string (spam-get-article-as-string article))
2548               (db spam-bogofilter-database-directory)
2549               (switch (if unregister
2550                           (if spam
2551                               spam-bogofilter-spam-strong-switch
2552                             spam-bogofilter-ham-strong-switch)
2553                         (if spam
2554                             spam-bogofilter-spam-switch
2555                           spam-bogofilter-ham-switch))))
2556           (when (stringp article-string)
2557             (with-temp-buffer
2558               (insert article-string)
2559
2560               (apply 'call-process-region
2561                      (point-min) (point-max)
2562                      spam-bogofilter-program
2563                      nil nil nil switch
2564                      (if db `("-d" ,db "-v") `("-v")))))))
2565     (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2566
2567 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2568   (spam-bogofilter-register-with-bogofilter articles t unregister))
2569
2570 (defun spam-bogofilter-unregister-spam-routine (articles)
2571   (spam-bogofilter-register-spam-routine articles t))
2572
2573 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2574   (spam-bogofilter-register-with-bogofilter articles nil unregister))
2575
2576 (defun spam-bogofilter-unregister-ham-routine (articles)
2577   (spam-bogofilter-register-ham-routine articles t))
2578
2579
2580 ;;}}}
2581
2582 ;;{{{ spamoracle
2583 (defun spam-check-spamoracle ()
2584   "Run spamoracle on an article to determine whether it's spam."
2585   (let ((article-buffer-name (buffer-name)))
2586     (with-temp-buffer
2587       (let ((temp-buffer-name (buffer-name)))
2588         (with-current-buffer article-buffer-name
2589           (let ((status
2590                  (apply 'call-process-region
2591                         (point-min) (point-max)
2592                         spam-spamoracle-binary
2593                         nil temp-buffer-name nil
2594                         (if spam-spamoracle-database
2595                             `("-f" ,spam-spamoracle-database "mark")
2596                           '("mark")))))
2597             (if (eq 0 status)
2598                 (progn
2599                   (set-buffer temp-buffer-name)
2600                   (goto-char (point-min))
2601                   (when (re-search-forward "^X-Spam: yes;" nil t)
2602                     spam-split-group))
2603               (error "Error running spamoracle: %s" status))))))))
2604
2605 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2606   "Run spamoracle in training mode."
2607   (with-temp-buffer
2608     (let ((temp-buffer-name (buffer-name)))
2609       (save-excursion
2610         (goto-char (point-min))
2611         (dolist (article articles)
2612           (insert (spam-get-article-as-string article)))
2613         (let* ((arg (if (spam-xor unregister article-is-spam-p)
2614                         "-spam"
2615                       "-good"))
2616                (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
2623                              "add" ,arg)
2624                          `("add" ,arg)))))
2625           (unless (eq 0 status)
2626             (error "Error running spamoracle: %s" status)))))))
2627
2628 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2629   (spam-spamoracle-learn articles nil unregister))
2630
2631 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2632   (spam-spamoracle-learn-ham articles t))
2633
2634 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2635   (spam-spamoracle-learn articles t unregister))
2636
2637 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2638   (spam-spamoracle-learn-spam articles t))
2639
2640 ;;}}}
2641
2642 ;;{{{ SpamAssassin
2643 ;;; based mostly on the bogofilter code
2644 (defun spam-check-spamassassin-headers (&optional score)
2645   "Check the SpamAssassin headers for the classification of this message."
2646   (if score                             ; scoring mode
2647       (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2648         (when header
2649           (if (string-match spam-spamassassin-score-regexp header)
2650               (match-string 1 header)
2651             "0")))
2652     ;; spam detection mode
2653     (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2654           (when header                  ; return nil when no header
2655             (when (string-match spam-spamassassin-positive-spam-flag-header
2656                                 header)
2657               spam-split-group)))))
2658
2659 (defun spam-check-spamassassin (&optional score)
2660   "Check the SpamAssassin backend for the classification of this message."
2661   (let ((article-buffer-name (buffer-name)))
2662     (with-temp-buffer
2663       (let ((temp-buffer-name (buffer-name)))
2664         (with-current-buffer article-buffer-name
2665           (apply 'call-process-region
2666                  (point-min) (point-max) spam-assassin-program
2667                  nil temp-buffer-name nil spam-spamassassin-arguments))
2668         ;; check the return now (we're back in the temp buffer)
2669         (goto-char (point-min))
2670         (spam-check-spamassassin-headers score)))))
2671
2672 ;; return something sensible if the score can't be determined
2673 (defun spam-spamassassin-score (&optional recheck)
2674   "Get the SpamAssassin score"
2675   (interactive "P")
2676   (save-window-excursion
2677     (gnus-summary-show-article t)
2678     (set-buffer gnus-article-buffer)
2679     (let ((score (or (unless recheck
2680                        (spam-check-spamassassin-headers t))
2681                      (spam-check-spamassassin t))))
2682       (gnus-summary-show-article)
2683       (message "SpamAssassin score %s" score)
2684       (or score "0"))))
2685
2686 (defun spam-spamassassin-register-with-sa-learn (articles spam
2687                                                  &optional unregister)
2688   "Register articles with spamassassin's sa-learn as spam or non-spam."
2689   (if articles
2690       (let ((action (if unregister spam-sa-learn-unregister-switch
2691                       (if spam spam-sa-learn-spam-switch
2692                         spam-sa-learn-ham-switch)))
2693             (summary-buffer-name (buffer-name)))
2694         (with-temp-buffer
2695           ;; group the articles into mbox format
2696           (dolist (article articles)
2697             (let (article-string)
2698               (with-current-buffer summary-buffer-name
2699                 (setq article-string (spam-get-article-as-string article)))
2700               (when (stringp article-string)
2701                 ;; mbox separator
2702                 (insert (concat "From nobody " (current-time-string) "\n"))
2703                 (insert article-string)
2704                 (insert "\n"))))
2705           ;; call sa-learn on all messages at the same time
2706           (apply 'call-process-region
2707                  (point-min) (point-max)
2708                  spam-sa-learn-program
2709                  nil nil nil "--mbox"
2710                  (if spam-sa-learn-rebuild
2711                      (list action)
2712                    `("--no-rebuild" ,action)))))))
2713
2714 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2715   (spam-spamassassin-register-with-sa-learn articles t unregister))
2716
2717 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2718   (spam-spamassassin-register-with-sa-learn articles nil unregister))
2719
2720 (defun spam-spamassassin-unregister-spam-routine (articles)
2721   (spam-spamassassin-register-with-sa-learn articles t t))
2722
2723 (defun spam-spamassassin-unregister-ham-routine (articles)
2724   (spam-spamassassin-register-with-sa-learn articles nil t))
2725
2726 ;;}}}
2727
2728 ;;{{{ Bsfilter
2729 ;;; based mostly on the bogofilter code
2730 (defun spam-check-bsfilter-headers (&optional score)
2731   (if score
2732       (or (nnmail-fetch-field spam-bsfilter-probability-header)
2733           "0")
2734     (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2735       (when header ; return nil when no header
2736         (when (string-match "YES" header)
2737           spam-split-group)))))
2738
2739 ;; return something sensible if the score can't be determined
2740 (defun spam-bsfilter-score (&optional recheck)
2741   "Get the Bsfilter spamicity score."
2742   (interactive "P")
2743   (save-window-excursion
2744     (gnus-summary-show-article t)
2745     (set-buffer gnus-article-buffer)
2746     (let ((score (or (unless recheck
2747                        (spam-check-bsfilter-headers t))
2748                      (spam-check-bsfilter t))))
2749       (gnus-summary-show-article)
2750       (message "Spamicity score %s" score)
2751       (or score "0"))))
2752
2753 (defun spam-check-bsfilter (&optional score)
2754   "Check the Bsfilter backend for the classification of this message."
2755   (let ((article-buffer-name (buffer-name))
2756         (dir spam-bsfilter-database-directory)
2757         return)
2758     (with-temp-buffer
2759       (let ((temp-buffer-name (buffer-name)))
2760         (with-current-buffer article-buffer-name
2761           (apply 'call-process-region
2762                  (point-min) (point-max)
2763                  spam-bsfilter-program
2764                  nil temp-buffer-name nil
2765                  "--pipe"
2766                  "--insert-flag"
2767                  "--insert-probability"
2768                  (when dir
2769                    (list "--homedir" dir))))
2770         (setq return (spam-check-bsfilter-headers score))))
2771     return))
2772
2773 (defun spam-bsfilter-register-with-bsfilter (articles
2774                                              spam
2775                                              &optional unregister)
2776   "Register an article, given as a string, as spam or non-spam."
2777   (dolist (article articles)
2778     (let ((article-string (spam-get-article-as-string article))
2779           (switch (if unregister
2780                       (if spam
2781                           spam-bsfilter-spam-strong-switch
2782                         spam-bsfilter-ham-strong-switch)
2783                     (if spam
2784                         spam-bsfilter-spam-switch
2785                       spam-bsfilter-ham-switch))))
2786       (when (stringp article-string)
2787         (with-temp-buffer
2788           (insert article-string)
2789           (apply 'call-process-region
2790                  (point-min) (point-max)
2791                  spam-bsfilter-program
2792                  nil nil nil switch
2793                  "--update"
2794                  (when spam-bsfilter-database-directory
2795                    (list "--homedir"
2796                          spam-bsfilter-database-directory))))))))
2797
2798 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2799   (spam-bsfilter-register-with-bsfilter articles t unregister))
2800
2801 (defun spam-bsfilter-unregister-spam-routine (articles)
2802   (spam-bsfilter-register-spam-routine articles t))
2803
2804 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2805   (spam-bsfilter-register-with-bsfilter articles nil unregister))
2806
2807 (defun spam-bsfilter-unregister-ham-routine (articles)
2808   (spam-bsfilter-register-ham-routine articles t))
2809
2810 ;;}}}
2811
2812 ;;{{{ CRM114 Mailfilter
2813 (defun spam-check-crm114-headers (&optional score)
2814   (let ((header (message-fetch-field spam-crm114-header)))
2815     (when header                        ; return nil when no header
2816       (if score                         ; scoring mode
2817           (if (string-match "( pR: \\([0-9.-]+\\)" header)
2818               (match-string 1 header)
2819             "0")
2820         ;; spam detection mode
2821         (when (string-match spam-crm114-positive-spam-header
2822                             header)
2823           spam-split-group)))))
2824
2825 ;; return something sensible if the score can't be determined
2826 (defun spam-crm114-score ()
2827   "Get the CRM114 Mailfilter pR."
2828   (interactive)
2829   (save-window-excursion
2830     (gnus-summary-show-article t)
2831     (set-buffer gnus-article-buffer)
2832     (let ((score (or (spam-check-crm114-headers t)
2833                      (spam-check-crm114 t))))
2834       (gnus-summary-show-article)
2835       (message "pR: %s" score)
2836       (or score "0"))))
2837
2838 (defun spam-check-crm114 (&optional score)
2839   "Check the CRM114 Mailfilter backend for the classification of this message."
2840   (let ((article-buffer-name (buffer-name))
2841         (db spam-crm114-database-directory)
2842         return)
2843     (with-temp-buffer
2844       (let ((temp-buffer-name (buffer-name)))
2845         (with-current-buffer article-buffer-name
2846           (apply 'call-process-region
2847                  (point-min) (point-max)
2848                  spam-crm114-program
2849                  nil temp-buffer-name nil
2850                  (when db (list (concat "--fileprefix=" db)))))
2851         (setq return (spam-check-crm114-headers score))))
2852     return))
2853
2854 (defun spam-crm114-register-with-crm114 (articles
2855                                          spam
2856                                          &optional unregister)
2857   "Register an article, given as a string, as spam or non-spam."
2858   (dolist (article articles)
2859     (let ((article-string (spam-get-article-as-string article))
2860           (db spam-crm114-database-directory)
2861           (switch (if unregister
2862                       (if spam
2863                           spam-crm114-spam-strong-switch
2864                         spam-crm114-ham-strong-switch)
2865                     (if spam
2866                         spam-crm114-spam-switch
2867                       spam-crm114-ham-switch))))
2868       (when (stringp article-string)
2869         (with-temp-buffer
2870           (insert article-string)
2871
2872           (apply 'call-process-region
2873                  (point-min) (point-max)
2874                  spam-crm114-program
2875                  nil nil nil
2876                  (when db (list switch (concat "--fileprefix=" db)))))))))
2877
2878 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2879   (spam-crm114-register-with-crm114 articles t unregister))
2880
2881 (defun spam-crm114-unregister-spam-routine (articles)
2882   (spam-crm114-register-spam-routine articles t))
2883
2884 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2885   (spam-crm114-register-with-crm114 articles nil unregister))
2886
2887 (defun spam-crm114-unregister-ham-routine (articles)
2888   (spam-crm114-register-ham-routine articles t))
2889
2890 ;;}}}
2891
2892 ;;}}}
2893
2894 ;;{{{ Hooks
2895
2896 ;;;###autoload
2897 (defun spam-initialize (&rest symbols)
2898   "Install the spam.el hooks and do other initialization.
2899 When SYMBOLS is given, set those variables to t.  This is so you
2900 can call `spam-initialize' before you set spam-use-* variables on
2901 explicitly, and matters only if you need the extra headers
2902 installed through `spam-necessary-extra-headers'."
2903   (interactive)
2904
2905   (dolist (var symbols)
2906     (set var t))
2907
2908   (dolist (header (spam-necessary-extra-headers))
2909     (add-to-list 'nnmail-extra-headers header)
2910     (add-to-list 'gnus-extra-headers header))
2911
2912   (setq spam-install-hooks t)
2913   ;; TODO: How do we redo this every time the `spam' face is customized?
2914   (push '((eq mark gnus-spam-mark) . spam)
2915         gnus-summary-highlight)
2916   ;; Add hooks for loading and saving the spam stats
2917   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2918   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2919   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2920   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2921   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2922   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2923   (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2924
2925 (defun spam-unload-hook ()
2926   "Uninstall the spam.el hooks."
2927   (interactive)
2928   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2929   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2930   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2931   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2932   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2933   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2934   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2935
2936 (add-hook 'spam-unload-hook 'spam-unload-hook)
2937
2938 (when spam-install-hooks
2939   (spam-initialize))
2940 ;;}}}
2941
2942 (provide 'spam)
2943
2944 ;;; spam.el ends here