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