1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002-2011
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/>.
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
40 ;;{{{ compilation directives and autoloads/requires
42 ;; For Emacs <22.2 and XEmacs.
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
50 (require 'gnus-uu) ; because of key prefix issues
51 ;;; for the definitions of group content classification and spam processors
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))
61 (autoload 'query-dig "dig")
63 ;; autoload spam-report
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")
77 (autoload 'dns-query "dns")
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'.")
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."
99 :tag "Move spam out of all groups and ham out of spam groups.")
102 :tag "Move spam out of all groups and ham out of all groups.")
105 :tag "Never move spam or ham out of any groups."))
108 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
109 "Directory for spam whitelists and blacklists."
113 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
114 "Whether new messages in a spam group should get the spam-mark."
116 ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
119 (defcustom spam-log-to-registry nil
120 "Whether spam/ham processing should be logged in the registry."
124 (defcustom spam-split-symbolic-return nil
125 "Whether `spam-split' should work with symbols or group names."
129 (defcustom spam-split-symbolic-return-positive nil
130 "Whether `spam-split' should ALWAYS work with symbols or group names.
131 Do not set this if you use `spam-split' in a fancy split method."
135 (defcustom spam-mark-only-unseen-as-spam t
136 "Whether only unseen articles should be marked as spam in spam groups.
137 When nil, all unread articles in a spam group are marked as
138 spam. Set this if you want to leave an article unread in a spam group
139 without losing it to the automatic spam-marking process."
143 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
144 "Whether ham should be marked unread before it's moved.
145 The article is moved out of a spam group according to `ham-process-destination'.
146 This variable is an official entry in the international Longest Variable Name
151 (defcustom spam-disable-spam-split-during-ham-respool nil
152 "Whether `spam-split' should be ignored while resplitting ham.
153 This is useful to prevent ham from ending up in the same spam
154 group after the resplit. Don't set this to t if you have `spam-split' as the
155 last rule in your split configuration."
159 (defcustom spam-autodetect-recheck-messages nil
160 "Should spam.el recheck all meessages when autodetecting?
161 Normally this is nil, so only unseen messages will be checked."
165 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
166 "The location of the whitelist.
167 The file format is one regular expression per line.
168 The regular expression is matched against the address."
172 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
173 "The location of the blacklist.
174 The file format is one regular expression per line.
175 The regular expression is matched against the address."
179 (defcustom spam-use-dig t
180 "Whether `query-dig' should be used instead of `dns-query'."
184 (defcustom spam-use-gmane-xref nil
185 "Whether the Gmane spam xref should be used by `spam-split'."
189 (defcustom spam-use-blacklist nil
190 "Whether the blacklist should be used by `spam-split'."
194 (defcustom spam-blacklist-ignored-regexes nil
195 "Regular expressions that the blacklist should ignore."
196 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
199 (defcustom spam-use-whitelist nil
200 "Whether the whitelist should be used by `spam-split'."
204 (defcustom spam-use-whitelist-exclusive nil
205 "Whether whitelist-exclusive should be used by `spam-split'.
206 Exclusive whitelisting means that all messages from senders not in the whitelist
207 are considered spam."
211 (defcustom spam-use-blackholes nil
212 "Whether blackholes should be used by `spam-split'."
216 (defcustom spam-use-hashcash nil
217 "Whether hashcash payments should be detected by `spam-split'."
221 (defcustom spam-use-regex-headers nil
222 "Whether a header regular expression match should be used by `spam-split'.
223 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
227 (defcustom spam-use-regex-body nil
228 "Whether a body regular expression match should be used by `spam-split'.
229 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
233 (defcustom spam-use-bogofilter-headers nil
234 "Whether bogofilter headers should be used by `spam-split'.
235 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
239 (defcustom spam-use-bogofilter nil
240 "Whether bogofilter should be invoked by `spam-split'.
241 Enable this if you want Gnus to invoke Bogofilter on new messages."
245 (defcustom spam-use-bsfilter-headers nil
246 "Whether bsfilter headers should be used by `spam-split'.
247 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
251 (defcustom spam-use-bsfilter nil
252 "Whether bsfilter should be invoked by `spam-split'.
253 Enable this if you want Gnus to invoke Bsfilter on new messages."
257 (defcustom spam-use-BBDB nil
258 "Whether BBDB should be used by `spam-split'."
262 (defcustom spam-use-BBDB-exclusive nil
263 "Whether BBDB-exclusive should be used by `spam-split'.
264 Exclusive BBDB means that all messages from senders not in the BBDB are
269 (defcustom spam-use-ifile nil
270 "Whether ifile should be used by `spam-split'."
274 (defcustom spam-use-stat nil
275 "Whether `spam-stat' should be used by `spam-split'."
279 (defcustom spam-use-spamoracle nil
280 "Whether spamoracle should be used by `spam-split'."
284 (defcustom spam-use-spamassassin nil
285 "Whether spamassassin should be invoked by `spam-split'.
286 Enable this if you want Gnus to invoke SpamAssassin on new messages."
290 (defcustom spam-use-spamassassin-headers nil
291 "Whether spamassassin headers should be checked by `spam-split'.
292 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
297 (defcustom spam-use-crm114 nil
298 "Whether the CRM114 Mailfilter should be used by `spam-split'."
302 (defcustom spam-install-hooks (or
307 spam-use-whitelist-exclusive
310 spam-use-regex-headers
313 spam-use-bogofilter-headers
314 spam-use-spamassassin
315 spam-use-spamassassin-headers
317 spam-use-bsfilter-headers
319 spam-use-BBDB-exclusive
324 "Whether the spam hooks should be installed.
325 Default to t if one of the spam-use-* variables is set."
329 (defcustom spam-split-group "spam"
330 "Group name where incoming spam should be put by `spam-split'."
334 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
335 ;;; not regular expressions
336 (defcustom spam-junk-mailgroups (cons
338 '("mail.junk" "poste.pourriel"))
339 "Mailgroups with spam contents.
340 All unmarked article in such group receive the spam mark on group entry."
341 :type '(repeat (string :tag "Group"))
345 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
346 "The group where spam xrefs can be found on Gmane.
347 Only meaningful if you enable `spam-use-gmane-xref'."
351 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
352 "dev.null.dk" "relays.visi.com")
353 "List of blackhole servers.
354 Only meaningful if you enable `spam-use-blackholes'."
355 :type '(repeat (string :tag "Server"))
358 (defcustom spam-blackhole-good-server-regex nil
359 "String matching IP addresses that should not be checked in the blackholes.
360 Only meaningful if you enable `spam-use-blackholes'."
361 :type '(radio (const nil) regexp)
365 '((((class color) (type tty) (background dark))
366 (:foreground "gray80" :background "gray50"))
367 (((class color) (type tty) (background light))
368 (:foreground "gray50" :background "gray80"))
369 (((class color) (background dark))
370 (:foreground "ivory2"))
371 (((class color) (background light))
372 (:foreground "ivory4"))
373 (t :inverse-video t))
374 "Face for spam-marked articles."
376 ;; backward-compatibility alias
377 (put 'spam-face 'face-alias 'spam)
378 (put 'spam-face 'obsolete-face "22.1")
380 (defcustom spam-face 'spam
381 "Face for spam-marked articles."
385 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
386 "Regular expression for positive header spam matches.
387 Only meaningful if you enable `spam-use-regex-headers'."
388 :type '(repeat (regexp :tag "Regular expression to match spam header"))
391 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
392 "Regular expression for positive header ham matches.
393 Only meaningful if you enable `spam-use-regex-headers'."
394 :type '(repeat (regexp :tag "Regular expression to match ham header"))
397 (defcustom spam-regex-body-spam '()
398 "Regular expression for positive body spam matches.
399 Only meaningful if you enable `spam-use-regex-body'."
400 :type '(repeat (regexp :tag "Regular expression to match spam body"))
403 (defcustom spam-regex-body-ham '()
404 "Regular expression for positive body ham matches.
405 Only meaningful if you enable `spam-use-regex-body'."
406 :type '(repeat (regexp :tag "Regular expression to match ham body"))
409 (defcustom spam-summary-score-preferred-header nil
410 "Preferred header to use for `spam-summary-score'."
411 :type '(choice :tag "Header name"
412 (symbol :tag "SpamAssassin etc" X-Spam-Status)
413 (symbol :tag "Bogofilter" X-Bogosity)
414 (const :tag "No preference, take best guess." nil))
417 (defgroup spam-ifile nil
418 "Spam ifile configuration."
421 (make-obsolete-variable 'spam-ifile-path 'spam-ifile-program
422 "Gnus 5.10.9 (Emacs 22.1)")
423 (defcustom spam-ifile-program (executable-find "ifile")
424 "Name of the ifile program."
425 :type '(choice (file :tag "Location of ifile")
426 (const :tag "ifile is not installed"))
429 (make-obsolete-variable 'spam-ifile-database-path 'spam-ifile-database
430 "Gnus 5.10.9 (Emacs 22.1)")
431 (defcustom spam-ifile-database nil
432 "File name of the ifile database."
433 :type '(choice (file :tag "Location of the ifile database")
434 (const :tag "Use the default"))
437 (defcustom spam-ifile-spam-category "spam"
438 "Name of the spam ifile category."
442 (defcustom spam-ifile-ham-category nil
443 "Name of the ham ifile category.
444 If nil, the current group name will be used."
445 :type '(choice (string :tag "Use a fixed category")
446 (const :tag "Use the current group name"))
449 (defcustom spam-ifile-all-categories nil
450 "Whether the ifile check will return all categories, or just spam.
451 Set this to t if you want to use the `spam-split' invocation of ifile as
452 your main source of newsgroup names."
456 (defgroup spam-bogofilter nil
457 "Spam bogofilter configuration."
460 (make-obsolete-variable 'spam-bogofilter-path 'spam-bogofilter-program
461 "Gnus 5.10.9 (Emacs 22.1)")
462 (defcustom spam-bogofilter-program (executable-find "bogofilter")
463 "Name of the Bogofilter program."
464 :type '(choice (file :tag "Location of bogofilter")
465 (const :tag "Bogofilter is not installed"))
466 :group 'spam-bogofilter)
468 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
470 (defcustom spam-bogofilter-header "X-Bogosity"
471 "The header that Bogofilter inserts in messages."
473 :group 'spam-bogofilter)
475 (defcustom spam-bogofilter-spam-switch "-s"
476 "The switch that Bogofilter uses to register spam messages."
478 :group 'spam-bogofilter)
480 (defcustom spam-bogofilter-ham-switch "-n"
481 "The switch that Bogofilter uses to register ham messages."
483 :group 'spam-bogofilter)
485 (defcustom spam-bogofilter-spam-strong-switch "-S"
486 "The switch that Bogofilter uses to unregister ham messages."
488 :group 'spam-bogofilter)
490 (defcustom spam-bogofilter-ham-strong-switch "-N"
491 "The switch that Bogofilter uses to unregister spam messages."
493 :group 'spam-bogofilter)
495 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
496 "The regex on `spam-bogofilter-header' for positive spam identification."
498 :group 'spam-bogofilter)
500 (defcustom spam-bogofilter-database-directory nil
501 "Location of the Bogofilter database.
502 When nil, use the default location."
503 :type '(choice (directory
504 :tag "Location of the Bogofilter database directory")
505 (const :tag "Use the default"))
506 :group 'spam-bogofilter)
508 (defgroup spam-bsfilter nil
509 "Spam bsfilter configuration."
512 (make-obsolete-variable 'spam-bsfilter-path 'spam-bsfilter-program
513 "Gnus 5.10.9 (Emacs 22.1)")
514 (defcustom spam-bsfilter-program (executable-find "bsfilter")
515 "Name of the Bsfilter program."
516 :type '(choice (file :tag "Location of bsfilter")
517 (const :tag "Bsfilter is not installed"))
518 :group 'spam-bsfilter)
520 (defcustom spam-bsfilter-header "X-Spam-Flag"
521 "The header inserted by Bsfilter to flag spam."
523 :group 'spam-bsfilter)
525 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
526 "The header that Bsfilter inserts in messages."
528 :group 'spam-bsfilter)
530 (defcustom spam-bsfilter-spam-switch "--add-spam"
531 "The switch that Bsfilter uses to register spam messages."
533 :group 'spam-bsfilter)
535 (defcustom spam-bsfilter-ham-switch "--add-clean"
536 "The switch that Bsfilter uses to register ham messages."
538 :group 'spam-bsfilter)
540 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
541 "The switch that Bsfilter uses to unregister ham messages."
543 :group 'spam-bsfilter)
545 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
546 "The switch that Bsfilter uses to unregister spam messages."
548 :group 'spam-bsfilter)
550 (defcustom spam-bsfilter-database-directory nil
551 "Directory path of the Bsfilter databases."
552 :type '(choice (directory
553 :tag "Location of the Bsfilter database directory")
554 (const :tag "Use the default"))
555 :group 'spam-bsfilter)
557 (defgroup spam-spamoracle nil
558 "Spam spamoracle configuration."
561 (defcustom spam-spamoracle-database nil
562 "Location of spamoracle database file.
563 When nil, use the default spamoracle database."
564 :type '(choice (directory :tag "Location of spamoracle database file.")
565 (const :tag "Use the default"))
566 :group 'spam-spamoracle)
568 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
569 "Location of the spamoracle binary."
570 :type '(choice (directory :tag "Location of the spamoracle binary")
571 (const :tag "Use the default"))
572 :group 'spam-spamoracle)
574 (defgroup spam-spamassassin nil
575 "Spam SpamAssassin configuration."
578 (make-obsolete-variable 'spam-spamassassin-path
579 'spam-spamassassin-program "Gnus 5.10.9 (Emacs 22.1)")
580 (defcustom spam-assassin-program (executable-find "spamassassin")
581 "Name of the spamassassin program.
582 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
583 spamd man pages for more information on these programs."
584 :type '(choice (file :tag "Location of spamc")
585 (const :tag "spamassassin is not installed"))
586 :group 'spam-spamassassin)
588 (defcustom spam-spamassassin-arguments ()
589 "Arguments to pass to the spamassassin executable.
590 This must be a list. For example, `(\"-C\" \"configfile\")'."
591 :type '(restricted-sexp :match-alternatives (listp))
592 :group 'spam-spamassassin)
594 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
595 "The header inserted by SpamAssassin to flag spam."
597 :group 'spam-spamassassin)
599 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
600 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
603 :group 'spam-spamassassin)
605 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
606 "The header inserted by SpamAssassin, giving extended scoring information"
608 :group 'spam-spamassassin)
610 (make-obsolete-variable 'spam-sa-learn-path 'spam-sa-learn-program
611 "Gnus 5.10.9 (Emacs 22.1)")
612 (defcustom spam-sa-learn-program (executable-find "sa-learn")
613 "Name of the sa-learn program."
614 :type '(choice (file :tag "Location of spamassassin")
615 (const :tag "spamassassin is not installed"))
616 :group 'spam-spamassassin)
618 (defcustom spam-sa-learn-rebuild t
619 "Whether sa-learn should rebuild the database every time it is called
620 Enable this if you want sa-learn to rebuild the database automatically. Doing
621 this will slightly increase the running time of the spam registration process.
622 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
623 order for SpamAssassin to recognize the new registered spam."
625 :group 'spam-spamassassin)
627 (defcustom spam-sa-learn-spam-switch "--spam"
628 "The switch that sa-learn uses to register spam messages."
630 :group 'spam-spamassassin)
632 (defcustom spam-sa-learn-ham-switch "--ham"
633 "The switch that sa-learn uses to register ham messages."
635 :group 'spam-spamassassin)
637 (defcustom spam-sa-learn-unregister-switch "--forget"
638 "The switch that sa-learn uses to unregister messages messages."
640 :group 'spam-spamassassin)
642 (defgroup spam-crm114 nil
643 "Spam CRM114 Mailfilter configuration."
646 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
647 "File path of the CRM114 Mailfilter executable program."
648 :type '(choice (file :tag "Location of CRM114 Mailfilter")
649 (const :tag "CRM114 Mailfilter is not installed"))
652 (defcustom spam-crm114-header "X-CRM114-Status"
653 "The header that CRM114 Mailfilter inserts in messages."
657 (defcustom spam-crm114-spam-switch "--learnspam"
658 "The switch that CRM114 Mailfilter uses to register spam messages."
662 (defcustom spam-crm114-ham-switch "--learnnonspam"
663 "The switch that CRM114 Mailfilter uses to register ham messages."
667 (defcustom spam-crm114-spam-strong-switch "--unlearn"
668 "The switch that CRM114 Mailfilter uses to unregister ham messages."
672 (defcustom spam-crm114-ham-strong-switch "--unlearn"
673 "The switch that CRM114 Mailfilter uses to unregister spam messages."
677 (defcustom spam-crm114-positive-spam-header "^SPAM"
678 "The regex on `spam-crm114-header' for positive spam identification."
682 (defcustom spam-crm114-database-directory nil
683 "Directory path of the CRM114 Mailfilter databases."
684 :type '(choice (directory
685 :tag "Location of the CRM114 Mailfilter database directory")
686 (const :tag "Use the default"))
689 ;;; Key bindings for spam control.
691 (gnus-define-keys gnus-summary-mode-map
692 "St" spam-generic-score
693 "Sx" gnus-summary-mark-as-spam
694 "Mst" spam-generic-score
695 "Msx" gnus-summary-mark-as-spam
696 "\M-d" gnus-summary-mark-as-spam
697 "$" gnus-summary-mark-as-spam)
699 (defvar spam-cache-lookups t
700 "Whether spam.el will try to cache lookups using `spam-caches'.")
702 (defvar spam-caches (make-hash-table
705 "Cache of spam detection entries.")
707 (defvar spam-old-articles nil
708 "List of old ham and spam articles, generated when a group is entered.")
710 (defvar spam-split-disabled nil
711 "If non-nil, `spam-split' is disabled, and always returns nil.")
713 (defvar spam-split-last-successful-check nil
715 `spam-split' will set this to nil or a spam-use-XYZ check if it
718 ;; internal variables for backends
719 ;; TODO: find a way to create these on the fly in spam-install-backend-super
720 (defvar spam-use-copy nil)
721 (defvar spam-use-move nil)
722 (defvar spam-use-gmane nil)
723 (defvar spam-use-resend nil)
727 ;;{{{ convenience functions
729 (defun spam-clear-cache (symbol)
730 "Clear the `spam-caches' entry for a check."
731 (remhash symbol spam-caches))
733 (defun spam-xor (a b)
735 (and (or a b) (not (and a b))))
737 (defun spam-set-difference (list1 list2)
738 "Return a set difference of LIST1 and LIST2.
739 When either list is nil, the other is returned."
740 (if (and list1 list2)
741 ;; we have two non-nil lists
743 (dolist (item (append list1 list2))
744 (when (and (memq item list1) (memq item list2))
745 (setq list1 (delq item list1))
746 (setq list2 (delq item list2))))
747 (append list1 list2))
748 ;; if either of the lists was nil, return the other one
749 (if list1 list1 list2)))
751 (defun spam-group-ham-mark-p (group mark &optional spam)
752 "Checks if MARK is considered a ham mark in GROUP."
753 (when (stringp group)
754 (let* ((marks (spam-group-ham-marks group spam))
755 (marks (if (symbolp mark)
757 (mapcar 'symbol-value marks))))
760 (defun spam-group-spam-mark-p (group mark)
761 "Checks if MARK is considered a spam mark in GROUP."
762 (spam-group-ham-mark-p group mark t))
764 (defun spam-group-ham-marks (group &optional spam)
765 "In GROUP, get all the ham marks."
766 (when (stringp group)
767 (let* ((marks (if spam
768 (gnus-parameter-spam-marks group)
769 (gnus-parameter-ham-marks group)))
771 (marks (if (listp (car marks)) (car marks) marks)))
774 (defun spam-group-spam-marks (group)
775 "In GROUP, get all the spam marks."
776 (spam-group-ham-marks group t))
778 (defun spam-group-spam-contents-p (group)
779 "Is GROUP a spam group?"
780 (if (and (stringp group) (< 0 (length group)))
781 (or (member group spam-junk-mailgroups)
782 (memq 'gnus-group-spam-classification-spam
783 (gnus-parameter-spam-contents group)))
786 (defun spam-group-ham-contents-p (group)
787 "Is GROUP a ham group?"
789 (memq 'gnus-group-spam-classification-ham
790 (gnus-parameter-spam-contents group))
793 (defun spam-classifications ()
794 "Return list of valid classifications"
797 (defun spam-classification-valid-p (classification)
798 "Is CLASSIFICATION a valid spam/ham classification?"
799 (memq classification (spam-classifications)))
801 (defun spam-backend-properties ()
802 "Return list of valid classifications."
803 '(statistical mover check hrf srf huf suf))
805 (defun spam-backend-property-valid-p (property)
806 "Is PROPERTY a valid backend property?"
807 (memq property (spam-backend-properties)))
809 (defun spam-backend-function-type-valid-p (type)
810 (or (eq type 'registration)
811 (eq type 'unregistration)))
813 (defun spam-process-type-valid-p (process-type)
814 (or (eq process-type 'incoming)
815 (eq process-type 'process)))
817 (defun spam-list-articles (articles classification)
818 (let ((mark-check (if (eq classification 'spam)
819 'spam-group-spam-mark-p
820 'spam-group-ham-mark-p))
821 alist mark-cache-yes mark-cache-no)
822 (dolist (article articles)
823 (let ((mark (gnus-summary-article-mark article)))
824 (unless (or (memq mark mark-cache-yes)
825 (memq mark mark-cache-no))
826 (if (funcall mark-check
829 (push mark mark-cache-yes)
830 (push mark mark-cache-no)))
831 (when (memq mark mark-cache-yes)
832 (push article alist))))
837 ;;{{{ backend installation functions and procedures
839 (defun spam-install-backend-super (backend &rest properties)
840 "Install BACKEND for spam.el.
841 Accepts incoming CHECK, ham registration function HRF, spam
842 registration function SRF, ham unregistration function HUF, spam
843 unregistration function SUF, and an indication whether the
844 backend is STATISTICAL."
845 (setq spam-backends (add-to-list 'spam-backends backend))
847 (let ((property (pop properties))
848 (value (pop properties)))
849 (if (spam-backend-property-valid-p property)
850 (put backend property value)
853 "spam-install-backend-super got an invalid property %s"
856 (defun spam-backend-list (&optional type)
857 "Return a list of all the backend symbols, constrained by TYPE.
858 When TYPE is 'non-mover, only non-mover backends are returned.
859 When TYPE is 'mover, only mover backends are returned."
861 (dolist (backend spam-backends)
863 (null type) ;either no type was requested
864 ;; or the type is 'mover and the backend is a mover
867 (spam-backend-mover-p backend))
868 ;; or the type is 'non-mover and the backend is not a mover
871 (not (spam-backend-mover-p backend))))
872 (push backend list)))
875 (defun spam-backend-check (backend)
876 "Get the check function for BACKEND.
877 Each individual check may return nil, t, or a mailgroup name.
878 The value nil means that the check does not yield a decision, and
879 so, that further checks are needed. The value t means that the
880 message is definitely not spam, and that further spam checks
881 should be inhibited. Otherwise, a mailgroup name or the symbol
882 'spam (depending on `spam-split-symbolic-return') is returned where
883 the mail should go, and further checks are also inhibited. The
884 usual mailgroup name is the value of `spam-split-group', meaning
885 that the message is definitely a spam."
886 (get backend 'check))
888 (defun spam-backend-valid-p (backend)
890 (member backend (spam-backend-list)))
892 (defun spam-backend-info (backend)
893 "Return information about BACKEND."
894 (if (spam-backend-valid-p backend)
896 (setq info (format "Backend %s has the following properties:\n"
898 (dolist (property (spam-backend-properties))
899 (setq info (format "%s%s=%s\n"
902 (get backend property))))
904 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
907 (defun spam-backend-function (backend classification type)
908 "Get the BACKEND function for CLASSIFICATION and TYPE.
909 TYPE is 'registration or 'unregistration.
910 CLASSIFICATION is 'ham or 'spam."
912 (spam-classification-valid-p classification)
913 (spam-backend-function-type-valid-p type))
916 (format "spam-backend-%s-%s-function"
919 (funcall retrieval backend))
922 "%s was passed invalid backend %s, classification %s, or type %s"
923 "spam-backend-function"
928 (defun spam-backend-article-list-property (classification
929 &optional unregister)
930 "Property name of article list with CLASSIFICATION and UNREGISTER."
931 (let* ((r (if unregister "unregister" "register"))
932 (prop (format "%s-%s" classification r)))
935 (defun spam-backend-get-article-todo-list (backend
937 &optional unregister)
938 "Get the articles to be processed for BACKEND and CLASSIFICATION.
939 With UNREGISTER, get articles to be unregistered.
940 This is a temporary storage function - nothing here persists."
943 (intern (spam-backend-article-list-property classification unregister))))
945 (defun spam-backend-put-article-todo-list (backend classification list
946 &optional unregister)
947 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
948 With UNREGISTER, set articles to be unregistered.
949 This is a temporary storage function - nothing here persists."
952 (intern (spam-backend-article-list-property classification unregister))
955 (defun spam-backend-ham-registration-function (backend)
956 "Get the ham registration function for BACKEND."
959 (defun spam-backend-spam-registration-function (backend)
960 "Get the spam registration function for BACKEND."
963 (defun spam-backend-ham-unregistration-function (backend)
964 "Get the ham unregistration function for BACKEND."
967 (defun spam-backend-spam-unregistration-function (backend)
968 "Get the spam unregistration function for BACKEND."
971 (defun spam-backend-statistical-p (backend)
972 "Is BACKEND statistical?"
973 (get backend 'statistical))
975 (defun spam-backend-mover-p (backend)
976 "Is BACKEND a mover?"
977 (get backend 'mover))
979 (defun spam-install-backend-alias (backend alias)
980 "Add ALIAS to an existing BACKEND.
981 The previous backend settings for ALIAS are erased."
983 ;; install alias with no properties at first
984 (spam-install-backend-super alias)
986 (dolist (property (spam-backend-properties))
987 (put alias property (get backend property))))
989 (defun spam-install-checkonly-backend (backend check)
990 "Install a BACKEND than can only CHECK for spam."
991 (spam-install-backend-super backend 'check check))
993 (defun spam-install-mover-backend (backend hrf srf huf suf)
994 "Install a BACKEND than can move articles at summary exit.
995 Accepts ham registration function HRF, spam registration function
996 SRF, ham unregistration function HUF, spam unregistration
997 function SUF. The backend has no incoming check and can't be
999 (spam-install-backend-super
1001 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
1003 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
1004 "Install a BACKEND than has no check.
1005 Accepts ham registration function HRF, spam registration function
1006 SRF, ham unregistration function HUF, spam unregistration
1007 function SUF. The backend has no incoming check and can't be
1008 statistical (it could be, but in practice that doesn't happen)."
1009 (spam-install-backend-super
1011 'hrf hrf 'srf srf 'huf huf 'suf suf))
1013 (defun spam-install-backend (backend check hrf srf huf suf)
1015 Accepts incoming CHECK, ham registration function HRF, spam
1016 registration function SRF, ham unregistration function HUF, spam
1017 unregistration function SUF. The backend won't be
1018 statistical (use `spam-install-statistical-backend' for that)."
1019 (spam-install-backend-super
1021 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
1023 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
1025 Accepts incoming CHECK, ham registration function HRF, spam
1026 registration function SRF, ham unregistration function HUF, spam
1027 unregistration function SUF. The backend will be
1028 statistical (use `spam-install-backend' for non-statistical
1030 (spam-install-backend-super
1032 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1034 (defun spam-install-statistical-checkonly-backend (backend check)
1035 "Install a statistical BACKEND than can only CHECK for spam."
1036 (spam-install-backend-super
1038 'check check 'statistical t))
1042 ;;{{{ backend installations
1043 (spam-install-checkonly-backend 'spam-use-blackholes
1044 'spam-check-blackholes)
1046 (spam-install-checkonly-backend 'spam-use-hashcash
1047 'spam-check-hashcash)
1049 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1050 'spam-check-spamassassin-headers)
1052 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1053 'spam-check-bogofilter-headers)
1055 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1056 'spam-check-bsfilter-headers)
1058 (spam-install-checkonly-backend 'spam-use-gmane-xref
1059 'spam-check-gmane-xref)
1061 (spam-install-checkonly-backend 'spam-use-regex-headers
1062 'spam-check-regex-headers)
1064 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1065 'spam-check-regex-body)
1067 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy)
1068 (spam-install-mover-backend 'spam-use-move
1069 'spam-move-ham-routine
1070 'spam-move-spam-routine
1074 (spam-install-nocheck-backend 'spam-use-copy
1075 'spam-copy-ham-routine
1076 'spam-copy-spam-routine
1080 (spam-install-nocheck-backend 'spam-use-gmane
1081 'spam-report-gmane-unregister-routine
1082 'spam-report-gmane-register-routine
1083 'spam-report-gmane-register-routine
1084 'spam-report-gmane-unregister-routine)
1086 (spam-install-nocheck-backend 'spam-use-resend
1087 'spam-report-resend-register-ham-routine
1088 'spam-report-resend-register-routine
1092 (spam-install-backend 'spam-use-BBDB
1094 'spam-BBDB-register-routine
1096 'spam-BBDB-unregister-routine
1099 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1101 (spam-install-backend 'spam-use-blacklist
1102 'spam-check-blacklist
1104 'spam-blacklist-register-routine
1106 'spam-blacklist-unregister-routine)
1108 (spam-install-backend 'spam-use-whitelist
1109 'spam-check-whitelist
1110 'spam-whitelist-register-routine
1112 'spam-whitelist-unregister-routine
1115 (spam-install-statistical-backend 'spam-use-ifile
1117 'spam-ifile-register-ham-routine
1118 'spam-ifile-register-spam-routine
1119 'spam-ifile-unregister-ham-routine
1120 'spam-ifile-unregister-spam-routine)
1122 (spam-install-statistical-backend 'spam-use-spamoracle
1123 'spam-check-spamoracle
1124 'spam-spamoracle-learn-ham
1125 'spam-spamoracle-learn-spam
1126 'spam-spamoracle-unlearn-ham
1127 'spam-spamoracle-unlearn-spam)
1129 (spam-install-statistical-backend 'spam-use-stat
1131 'spam-stat-register-ham-routine
1132 'spam-stat-register-spam-routine
1133 'spam-stat-unregister-ham-routine
1134 'spam-stat-unregister-spam-routine)
1136 (spam-install-statistical-backend 'spam-use-spamassassin
1137 'spam-check-spamassassin
1138 'spam-spamassassin-register-ham-routine
1139 'spam-spamassassin-register-spam-routine
1140 'spam-spamassassin-unregister-ham-routine
1141 'spam-spamassassin-unregister-spam-routine)
1143 (spam-install-statistical-backend 'spam-use-bogofilter
1144 'spam-check-bogofilter
1145 'spam-bogofilter-register-ham-routine
1146 'spam-bogofilter-register-spam-routine
1147 'spam-bogofilter-unregister-ham-routine
1148 'spam-bogofilter-unregister-spam-routine)
1150 (spam-install-statistical-backend 'spam-use-bsfilter
1151 'spam-check-bsfilter
1152 'spam-bsfilter-register-ham-routine
1153 'spam-bsfilter-register-spam-routine
1154 'spam-bsfilter-unregister-ham-routine
1155 'spam-bsfilter-unregister-spam-routine)
1157 (spam-install-statistical-backend 'spam-use-crm114
1159 'spam-crm114-register-ham-routine
1160 'spam-crm114-register-spam-routine
1161 'spam-crm114-unregister-ham-routine
1162 'spam-crm114-unregister-spam-routine)
1165 ;;{{{ scoring and summary formatting
1166 (defun spam-necessary-extra-headers ()
1167 "Return the extra headers spam.el thinks are necessary."
1169 (when (or spam-use-spamassassin
1170 spam-use-spamassassin-headers
1171 spam-use-regex-headers)
1172 (push 'X-Spam-Status list))
1173 (when (or spam-use-bogofilter
1174 spam-use-regex-headers)
1175 (push 'X-Bogosity list))
1176 (when (or spam-use-crm114
1177 spam-use-regex-headers)
1178 (push 'X-CRM114-Status list))
1181 (defun spam-user-format-function-S (headers)
1184 (spam-summary-score headers spam-summary-score-preferred-header))))
1186 (defun spam-article-sort-by-spam-status (h1 h2)
1187 "Sort articles by score."
1189 (dolist (header (spam-necessary-extra-headers))
1190 (let ((s1 (spam-summary-score h1 header))
1191 (s2 (spam-summary-score h2 header)))
1193 (setq result (< s1 s2))
1197 (defvar spam-spamassassin-score-regexp
1198 ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
1199 "Regexp matching SpamAssassin score header.
1200 The first group must match the number.")
1202 (defun spam-extra-header-to-number (header headers)
1203 "Transform an extra HEADER to a number, using list of HEADERS.
1204 Note this has to be fast."
1205 (let ((header-content (gnus-extra-header header headers)))
1208 ((eq header 'X-Spam-Status)
1209 (string-to-number (gnus-replace-in-string
1211 spam-spamassassin-score-regexp
1213 ;; for CRM checking, it's probably faster to just do the string match
1214 ((string-match "( pR: \\([0-9.-]+\\)" header-content)
1215 (- (string-to-number (match-string 1 header-content))))
1216 ((eq header 'X-Bogosity)
1217 (string-to-number (gnus-replace-in-string
1218 (gnus-replace-in-string
1225 (defun spam-summary-score (headers &optional specific-header)
1226 "Score an article for the summary buffer, as fast as possible.
1227 With SPECIFIC-HEADER, returns only that header's score.
1228 Will not return a nil score."
1232 (list specific-header)
1233 (spam-necessary-extra-headers)))
1235 (spam-extra-header-to-number header headers))
1240 (defun spam-generic-score (&optional recheck)
1241 "Invoke whatever scoring method we can."
1244 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1245 (spam-spamassassin-score recheck))
1246 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1247 (spam-bsfilter-score recheck))
1249 (spam-crm114-score))
1250 (t (spam-bogofilter-score recheck))))
1253 ;;{{{ set up widening, processor checks
1255 ;;; set up IMAP widening if it's necessary
1256 (defun spam-setup-widening ()
1257 (when (spam-widening-needed-p)
1258 (setq nnimap-split-download-body-default t)))
1260 (defun spam-widening-needed-p (&optional force-symbols)
1262 (dolist (backend (spam-backend-list))
1263 (when (and (spam-backend-statistical-p backend)
1264 (or (symbol-value backend)
1265 (memq backend force-symbols)))
1266 (setq found backend)))
1269 (defvar spam-list-of-processors
1270 ;; note the nil processors are not defined in gnus.el
1271 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1272 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1273 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1274 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1275 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1276 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1277 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1278 (gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane) ;; Buggy?
1279 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1280 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1281 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1282 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1283 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1284 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1285 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1286 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1287 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1288 "The OBSOLETE `spam-list-of-processors' list.
1289 This list contains pairs associating the obsolete ham/spam exit
1290 processor variables with a classification and a spam-use-*
1291 variable. When the processor variable is nil, just the
1292 classification and spam-use-* check variable are used. This is
1293 superseded by the new spam backend code, so it's only consulted
1294 for backwards compatibility.")
1295 (make-obsolete-variable 'spam-list-of-processors nil "22.1")
1297 (defun spam-group-processor-p (group backend &optional classification)
1298 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1299 Also accepts the obsolete processors, which can be found in
1300 gnus.el and in spam-list-of-processors. In the case of mover
1301 backends, checks the setting of `spam-summary-exit-behavior' in
1302 addition to the set values for the group."
1303 (if (and (stringp group)
1305 (let ((old-style (assq backend spam-list-of-processors))
1306 (parameters (nth 0 (gnus-parameter-spam-process group)))
1308 (if old-style ; old-style processor
1309 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1310 ;; now search for the parameter
1311 (dolist (parameter parameters)
1312 (when (and (null found)
1314 (eq classification (nth 0 parameter))
1315 (eq backend (nth 1 parameter)))
1318 ;; now, if the parameter was not found, do the
1319 ;; spam-summary-exit-behavior-logic for mover backends
1321 (when (spam-backend-mover-p backend)
1325 ((eq spam-summary-exit-behavior 'move-all) t)
1326 ((eq spam-summary-exit-behavior 'move-none) nil)
1327 ((eq spam-summary-exit-behavior 'default)
1328 (or (eq classification 'spam) ;move spam out of all groups
1329 ;; move ham out of spam groups
1330 (and (eq classification 'ham)
1331 (spam-group-spam-contents-p group))))
1332 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1333 spam-summary-exit-behavior))))))
1340 ;;{{{ Summary entry and exit processing.
1342 (defun spam-mark-junk-as-spam-routine ()
1343 ;; check the global list of group names spam-junk-mailgroups and the
1345 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1346 (gnus-message 6 "Marking %s articles as spam"
1347 (if spam-mark-only-unseen-as-spam
1350 (let ((articles (if spam-mark-only-unseen-as-spam
1351 gnus-newsgroup-unseen
1352 gnus-newsgroup-unreads)))
1353 (if spam-mark-new-messages-in-spam-group-as-spam
1354 (dolist (article articles)
1355 (gnus-summary-mark-article article gnus-spam-mark))
1356 (gnus-message 9 "Did not mark new messages as spam.")))))
1358 (defun spam-summary-prepare ()
1359 (setq spam-old-articles
1360 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1361 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1362 (spam-mark-junk-as-spam-routine))
1364 ;; The spam processors are invoked for any group, spam or ham or neither
1365 (defun spam-summary-prepare-exit ()
1366 (unless gnus-group-is-exiting-without-update-p
1367 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1369 ;; before we begin, remove any article limits
1371 ; (gnus-summary-pop-limit t))
1373 ;; first of all, unregister any articles that are no longer ham or spam
1374 ;; we have to iterate over the processors, or else we'll be too slow
1375 (dolist (classification (spam-classifications))
1376 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1377 (new-articles (spam-list-articles
1378 gnus-newsgroup-articles
1380 (changed-articles (spam-set-difference new-articles old-articles)))
1381 ;; now that we have the changed articles, we go through the processors
1382 (dolist (backend (spam-backend-list))
1383 (let (unregister-list)
1384 (dolist (article changed-articles)
1385 (let ((id (spam-fetch-field-message-id-fast article)))
1386 (when (spam-log-unregistration-needed-p
1387 id 'process classification backend)
1388 (push article unregister-list))))
1389 ;; call spam-register-routine with specific articles to unregister,
1390 ;; when there are articles to unregister and the check is enabled
1391 (when (and unregister-list (symbol-value backend))
1392 (spam-backend-put-article-todo-list backend
1397 ;; do the non-moving backends first, then the moving ones
1398 (dolist (backend-type '(non-mover mover))
1399 (dolist (classification (spam-classifications))
1400 (dolist (backend (spam-backend-list backend-type))
1401 (when (spam-group-processor-p
1405 (spam-backend-put-article-todo-list backend
1408 gnus-newsgroup-articles
1409 classification))))))
1411 (spam-resolve-registrations-routine) ; do the registrations now
1413 ;; we mark all the leftover spam articles as expired at the end
1414 (dolist (article (spam-list-articles
1415 gnus-newsgroup-articles
1417 (gnus-summary-mark-article article gnus-expirable-mark)))
1419 (setq spam-old-articles nil))
1423 ;;{{{ spam-use-move and spam-use-copy backend support functions
1425 (defun spam-copy-or-move-routine (copy groups articles classification)
1427 (when (and (car-safe groups) (listp (car-safe groups)))
1428 (setq groups (pop groups)))
1430 (unless (listp groups)
1431 (setq groups (list groups)))
1433 ;; remove the current process mark
1434 (gnus-summary-kill-process-mark)
1436 (let ((backend-supports-deletions
1437 (gnus-check-backend-function
1438 'request-move-article gnus-newsgroup-name))
1439 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1440 article mark deletep respool valid-move-destinations)
1442 (when (member 'respool groups)
1443 (setq respool t) ; boolean for later
1444 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1446 ;; exclude invalid move destinations
1447 (dolist (group groups)
1451 (eq classification 'spam)
1452 (spam-group-spam-contents-p gnus-newsgroup-name)
1453 (spam-group-spam-contents-p group)
1456 "Sorry, can't move spam from spam group %s to spam group %s"
1460 (eq classification 'ham)
1461 (spam-group-ham-contents-p gnus-newsgroup-name)
1462 (spam-group-ham-contents-p group)
1465 "Sorry, can't move ham from ham group %s to ham group %s"
1468 (push group valid-move-destinations)))
1470 (setq groups (nreverse valid-move-destinations))
1472 ;; now do the actual move
1473 (dolist (group groups)
1475 (when (and articles (stringp group))
1477 ;; first, mark the article with the process mark and, if needed,
1478 ;; the unread or expired mark (for ham and spam respectively)
1479 (dolist (article articles)
1480 (when (and (eq classification 'ham)
1481 spam-mark-ham-unread-before-move-from-spam-group)
1482 (gnus-message 9 "Marking ham article %d unread before move"
1484 (gnus-summary-mark-article article gnus-unread-mark))
1485 (when (and (eq classification 'spam)
1487 (gnus-message 9 "Marking spam article %d expirable before move"
1489 (gnus-summary-mark-article article gnus-expirable-mark))
1490 (gnus-summary-set-process-mark article)
1492 (if respool ; respooling is with a "fake" group
1493 (let ((spam-split-disabled
1494 (or spam-split-disabled
1495 (and (eq classification 'ham)
1496 spam-disable-spam-split-during-ham-respool))))
1497 (gnus-message 9 "Respooling article %d with method %s"
1498 article respool-method)
1499 (gnus-summary-respool-article nil respool-method))
1500 ;; else, we are not respooling
1501 (if (or (not backend-supports-deletions)
1502 (> (length groups) 1))
1503 (progn ; if copying, copy and set deletep
1504 (gnus-message 9 "Copying article %d to group %s"
1506 (gnus-summary-copy-article nil group)
1508 (gnus-message 9 "Moving article %d to group %s"
1510 (gnus-summary-move-article nil group)))))) ; else move articles
1512 ;; now delete the articles, unless a) copy is t, and there was a copy done
1513 ;; b) a move was done to a single group
1514 ;; c) backend-supports-deletions is nil
1516 (when (and deletep backend-supports-deletions)
1517 (dolist (article articles)
1518 (gnus-summary-set-process-mark article)
1519 (gnus-message 9 "Deleting article %d" article))
1521 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1522 (gnus-summary-delete-article nil)))))
1523 (gnus-summary-yank-process-mark)
1526 (defun spam-copy-spam-routine (articles)
1527 (spam-copy-or-move-routine
1529 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1533 (defun spam-move-spam-routine (articles)
1534 (spam-copy-or-move-routine
1536 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1540 (defun spam-copy-ham-routine (articles)
1541 (spam-copy-or-move-routine
1543 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1547 (defun spam-move-ham-routine (articles)
1548 (spam-copy-or-move-routine
1550 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1556 ;;{{{ article and field retrieval code
1557 (defun spam-get-article-as-string (article)
1558 (when (numberp article)
1560 (gnus-request-article-this-buffer
1562 gnus-newsgroup-name)
1566 ;; (defun spam-get-article-as-filename (article)
1567 ;; (let ((article-filename))
1568 ;; (when (numberp article)
1569 ;; (nnml-possibly-change-directory
1570 ;; (gnus-group-real-name gnus-newsgroup-name))
1571 ;; (setq article-filename (expand-file-name
1572 ;; (int-to-string article) nnml-current-directory)))
1573 ;; (if (file-exists-p article-filename)
1577 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1578 "Fetch a FIELD for ARTICLE with the internal `gnus-data-list' function.
1579 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1580 When FIELD is 'number, ARTICLE can be any number (since we want
1582 (when (numberp article)
1583 (let* ((data-header (or prepared-data-header
1584 (spam-fetch-article-header article))))
1585 (if (arrayp data-header)
1587 ((equal field 'number)
1588 (mail-header-number data-header))
1589 ((equal field 'from)
1590 (mail-header-from data-header))
1591 ((equal field 'message-id)
1592 (mail-header-message-id data-header))
1593 ((equal field 'subject)
1594 (mail-header-subject data-header))
1595 ((equal field 'references)
1596 (mail-header-references data-header))
1597 ((equal field 'date)
1598 (mail-header-date data-header))
1599 ((equal field 'xref)
1600 (mail-header-xref data-header))
1601 ((equal field 'extra)
1602 (mail-header-extra data-header))
1606 "spam-fetch-field-fast: unknown field %s requested"
1609 (gnus-message 6 "Article %d has a nil data header" article)))))
1611 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1612 (spam-fetch-field-fast article 'from prepared-data-header))
1614 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1615 (spam-fetch-field-fast article 'subject prepared-data-header))
1617 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1618 (spam-fetch-field-fast article 'message-id prepared-data-header))
1620 (defun spam-generate-fake-headers (article)
1621 (let ((dh (spam-fetch-article-header article)))
1625 ;; 80-character limit makes for strange constructs
1626 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1627 "Date: %s\nReferences: %s\nXref: %s\n")
1628 (spam-fetch-field-fast article 'from dh)
1629 (spam-fetch-field-fast article 'subject dh)
1630 (spam-fetch-field-fast article 'message-id dh)
1631 (spam-fetch-field-fast article 'date dh)
1632 (spam-fetch-field-fast article 'references dh)
1633 (spam-fetch-field-fast article 'xref dh))
1634 (when (spam-fetch-field-fast article 'extra dh)
1635 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1638 "spam-generate-fake-headers: article %d didn't have a valid header"
1641 (defun spam-fetch-article-header (article)
1642 (with-current-buffer gnus-summary-buffer
1643 (gnus-read-header article)
1644 (nth 3 (assq article gnus-newsgroup-data))))
1647 ;;{{{ Spam determination.
1649 (defun spam-split (&rest specific-checks)
1650 "Split this message into the `spam' group if it is spam.
1651 This function can be used as an entry in the variable `nnmail-split-fancy',
1652 for example like this: (: spam-split). It can take checks as
1653 parameters. A string as a parameter will set the
1654 `spam-split-group' to that string.
1656 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1658 (setq spam-split-last-successful-check nil)
1659 (unless spam-split-disabled
1660 (let ((spam-split-group-choice spam-split-group))
1661 (dolist (check specific-checks)
1662 (when (stringp check)
1663 (setq spam-split-group-choice check)
1664 (setq specific-checks (delq check specific-checks))))
1666 (let ((spam-split-group spam-split-group-choice)
1667 (widening-needed-check (spam-widening-needed-p specific-checks)))
1670 (when widening-needed-check
1672 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1673 widening-needed-check))
1674 (let ((backends (spam-backend-list))
1676 (while (and backends (not decision))
1677 (let* ((backend (pop backends))
1678 (check-function (spam-backend-check backend))
1679 (spam-split-group (if spam-split-symbolic-return
1683 ;; either, given specific checks, this is one of them
1684 (memq backend specific-checks)
1685 ;; or, given no specific checks, spam-use-CHECK is set
1686 (and (null specific-checks) (symbol-value backend)))
1687 (gnus-message 6 "spam-split: calling the %s function"
1689 (setq decision (funcall check-function))
1690 ;; if we got a decision at all, save the current check
1692 (setq spam-split-last-successful-check backend))
1694 (when (eq decision 'spam)
1695 (unless spam-split-symbolic-return
1698 (format "spam-split got %s but %s is nil"
1700 spam-split-symbolic-return)))))))
1702 (if spam-split-symbolic-return-positive 'ham nil)
1705 (defun spam-find-spam ()
1706 "Detect spam in the current newsgroup using `spam-split'."
1709 (let* ((group gnus-newsgroup-name)
1710 (autodetect (gnus-parameter-spam-autodetect group))
1711 (methods (gnus-parameter-spam-autodetect-methods group))
1712 (first-method (nth 0 methods))
1713 (articles (if spam-autodetect-recheck-messages
1714 gnus-newsgroup-articles
1715 gnus-newsgroup-unseen))
1716 article-cannot-be-faked)
1719 (dolist (backend methods)
1720 (when (spam-backend-statistical-p backend)
1721 (setq article-cannot-be-faked t)
1724 (when (memq 'default methods)
1725 (setq article-cannot-be-faked t))
1727 (when (and autodetect
1728 (not (equal first-method 'none)))
1731 (let ((id (spam-fetch-field-message-id-fast article))
1732 (subject (spam-fetch-field-subject-fast article))
1733 (sender (spam-fetch-field-from-fast article))
1737 (gnus-message 6 "Article %d has no message ID!" article))
1739 (when (and id spam-log-to-registry)
1740 (setq registry-lookup (spam-log-registration-type id 'incoming))
1741 (when registry-lookup
1744 "spam-find-spam: message %s was already registered incoming"
1747 (let* ((spam-split-symbolic-return t)
1748 (spam-split-symbolic-return-positive t)
1749 (fake-headers (spam-generate-fake-headers article))
1753 (if article-cannot-be-faked
1754 (gnus-request-article-this-buffer
1757 ;; else, we fake the article
1758 (when fake-headers (insert fake-headers)))
1759 (if (or (null first-method)
1760 (equal first-method 'default))
1762 (apply 'spam-split methods))))))
1763 (if (equal split-return 'spam)
1764 (gnus-summary-mark-article article gnus-spam-mark))
1766 (when (and id split-return spam-log-to-registry)
1767 (when (zerop (gnus-registry-group-count id))
1768 (gnus-registry-add-group
1769 id group subject sender))
1771 (unless registry-lookup
1772 (spam-log-processing-to-registry
1776 spam-split-last-successful-check
1782 ;;{{{ registration/unregistration functions
1784 (defun spam-resolve-registrations-routine ()
1785 "Go through the backends and register or unregister articles as needed."
1786 (dolist (backend-type '(non-mover mover))
1787 (dolist (classification (spam-classifications))
1788 (dolist (backend (spam-backend-list backend-type))
1789 (let ((rlist (spam-backend-get-article-todo-list
1790 backend classification))
1791 (ulist (spam-backend-get-article-todo-list
1792 backend classification t))
1795 ;; clear the old lists right away
1796 (spam-backend-put-article-todo-list backend
1800 (spam-backend-put-article-todo-list backend
1805 ;; eliminate duplicates
1806 (dolist (article (copy-sequence ulist))
1807 (when (memq article rlist)
1809 (setq rlist (delq article rlist))
1810 (setq ulist (delq article ulist))))
1812 (unless (zerop delcount)
1815 "%d messages did not have to unregister and then register"
1818 ;; unregister articles
1819 (unless (zerop (length ulist))
1820 (let ((num (spam-unregister-routine classification backend ulist)))
1824 "%d %s messages were unregistered by backend %s."
1829 ;; register articles
1830 (unless (zerop (length rlist))
1831 (let ((num (spam-register-routine classification backend rlist)))
1835 "%d %s messages were registered by backend %s."
1840 (defun spam-unregister-routine (classification
1843 (spam-register-routine classification backend specific-articles t))
1845 (defun spam-register-routine (classification
1848 &optional unregister)
1849 (when (and (spam-classification-valid-p classification)
1850 (spam-backend-valid-p backend))
1851 (let* ((register-function
1852 (spam-backend-function backend classification 'registration))
1853 (unregister-function
1854 (spam-backend-function backend classification 'unregistration))
1855 (run-function (if unregister
1858 (log-function (if unregister
1859 'spam-log-undo-registration
1860 'spam-log-processing-to-registry))
1864 ;; make list of articles, using specific-articles if given
1865 (setq articles (or specific-articles
1867 gnus-newsgroup-articles
1870 (when (> (length articles) 0)
1871 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1872 (if unregister "Unregistering" "Registering")
1874 (if specific-articles "specific" "")
1877 (funcall run-function articles)
1878 ;; now log all the registrations (or undo them, depending on
1880 (dolist (article articles)
1881 (funcall log-function
1882 (spam-fetch-field-message-id-fast article)
1886 gnus-newsgroup-name))))
1887 ;; return the number of articles processed
1888 (length articles))))
1890 ;;; log a ham- or spam-processor invocation to the registry
1891 (defun spam-log-processing-to-registry (id type classification backend group)
1892 (when spam-log-to-registry
1893 (if (and (stringp id)
1895 (spam-process-type-valid-p type)
1896 (spam-classification-valid-p classification)
1897 (spam-backend-valid-p backend))
1898 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1899 (cell (list classification backend group)))
1900 (push cell cell-list)
1901 (gnus-registry-store-extra-entry
1909 "%s call with bad ID, type, classification, spam-backend, or group"
1910 "spam-log-processing-to-registry")))))
1912 ;;; check if a ham- or spam-processor registration has been done
1913 (defun spam-log-registered-p (id type)
1914 (when spam-log-to-registry
1915 (if (and (stringp id)
1916 (spam-process-type-valid-p type))
1917 (cdr-safe (gnus-registry-fetch-extra id type))
1921 (format "%s called with bad ID, type, classification, or spam-backend"
1922 "spam-log-registered-p"))
1925 ;;; check what a ham- or spam-processor registration says
1926 ;;; returns nil if conflicting registrations are found
1927 (defun spam-log-registration-type (id type)
1930 (dolist (reg (spam-log-registered-p id type))
1931 (let ((classification (nth 0 reg)))
1932 (when (spam-classification-valid-p classification)
1934 (not (eq classification decision)))
1935 (setq count (+ 1 count)))
1936 (setq decision classification))))
1942 ;;; check if a ham- or spam-processor registration needs to be undone
1943 (defun spam-log-unregistration-needed-p (id type classification backend)
1944 (when spam-log-to-registry
1945 (if (and (stringp id)
1946 (spam-process-type-valid-p type)
1947 (spam-classification-valid-p classification)
1948 (spam-backend-valid-p backend))
1949 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1951 (dolist (cell cell-list)
1953 (when (and (eq classification (nth 0 cell))
1954 (eq backend (nth 1 cell)))
1960 (format "%s called with bad ID, type, classification, or spam-backend"
1961 "spam-log-unregistration-needed-p"))
1965 ;;; undo a ham- or spam-processor registration (the group is not used)
1966 (defun spam-log-undo-registration (id type classification backend
1968 (when (and spam-log-to-registry
1969 (spam-log-unregistration-needed-p id type classification backend))
1970 (if (and (stringp id)
1971 (spam-process-type-valid-p type)
1972 (spam-classification-valid-p classification)
1973 (spam-backend-valid-p backend))
1974 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1975 new-cell-list found)
1976 (dolist (cell cell-list)
1977 (unless (and (eq classification (nth 0 cell))
1978 (eq backend (nth 1 cell)))
1979 (push cell new-cell-list)))
1980 (gnus-registry-store-extra-entry
1985 (gnus-error 7 (format
1986 "%s call with bad ID, type, spam-backend, or group"
1987 "spam-log-undo-registration"))
1992 ;;{{{ backend functions
1995 (defun spam-check-gmane-xref ()
1997 (message-fetch-field "Xref")
1998 (message-fetch-field "Newsgroups"))))
1999 (when header ; return nil when no header
2000 (when (string-match spam-gmane-xref-spam-group
2002 spam-split-group))))
2008 (defun spam-check-regex-body ()
2009 (let ((spam-regex-headers-ham spam-regex-body-ham)
2010 (spam-regex-headers-spam spam-regex-body-spam))
2011 (spam-check-regex-headers t)))
2017 (defun spam-check-regex-headers (&optional body)
2018 (let ((type (if body "body" "header"))
2020 (dolist (h-regex spam-regex-headers-ham)
2022 (goto-char (point-min))
2023 (when (re-search-forward h-regex nil t)
2024 (message "Ham regex %s search positive." type)
2026 (dolist (s-regex spam-regex-headers-spam)
2028 (goto-char (point-min))
2029 (when (re-search-forward s-regex nil t)
2030 (message "Spam regex %s search positive." type)
2032 (setq ret spam-split-group))))
2039 (defun spam-reverse-ip-string (ip)
2041 (mapconcat 'identity
2042 (nreverse (split-string ip "\\."))
2045 (defun spam-check-blackholes ()
2046 "Check the Received headers for blackholed relays."
2047 (let ((headers (message-fetch-field "received"))
2052 (goto-char (point-min))
2053 (gnus-message 6 "Checking headers for relay addresses")
2054 (while (re-search-forward
2055 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
2056 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
2057 (push (spam-reverse-ip-string (match-string 1))
2059 (dolist (server spam-blackhole-servers)
2061 (unless (and spam-blackhole-good-server-regex
2062 ;; match against the reversed (again) IP string
2064 spam-blackhole-good-server-regex
2065 (spam-reverse-ip-string ip)))
2067 (let ((query-string (concat ip "." server)))
2069 (let ((query-result (query-dig query-string)))
2071 (gnus-message 6 "(DIG): positive blackhole check '%s'"
2073 (push (list ip server query-result)
2075 ;; else, if not using dig.el
2076 (when (dns-query query-string)
2077 (gnus-message 6 "positive blackhole check")
2078 (push (list ip server (dns-query query-string 'TXT))
2086 (defun spam-check-hashcash ()
2087 "Check the headers for hashcash payments."
2088 (ignore-errors (mail-check-payment))) ;mail-check-payment returns a boolean
2094 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2095 ;;; <sacha@giotto.sj.ru>
2097 ;; all this is done inside a condition-case to trap errors
2100 (autoload 'bbdb-buffer "bbdb")
2101 (autoload 'bbdb-create-internal "bbdb")
2102 (autoload 'bbdb-search-simple "bbdb"))
2104 ;; Autoloaded in message, which we require.
2105 (declare-function gnus-extract-address-components "gnus-util" (from))
2108 (when (condition-case nil
2111 (require 'bbdb-com))
2113 ;; `bbdb-records' should not be bound as an autoload function
2114 ;; before loading bbdb because of `bbdb-hashtable-size'.
2115 (defalias 'bbdb-records 'ignore)
2116 (defalias 'spam-BBDB-register-routine 'ignore)
2117 (defalias 'spam-enter-ham-BBDB 'ignore)
2120 ;; when the BBDB changes, we want to clear out our cache
2121 (defun spam-clear-cache-BBDB (&rest immaterial)
2122 (spam-clear-cache 'spam-use-BBDB))
2124 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2126 (defun spam-enter-ham-BBDB (addresses &optional remove)
2127 "Enter an address into the BBDB; implies ham (non-spam) sender"
2128 (dolist (from addresses)
2129 (when (stringp from)
2130 (let* ((parsed-address (gnus-extract-address-components from))
2131 (name (or (nth 0 parsed-address) "Ham Sender"))
2132 (remove-function (if remove
2133 'bbdb-delete-record-internal
2135 (net-address (nth 1 parsed-address))
2136 (record (and net-address
2137 (bbdb-search-simple nil net-address))))
2139 (gnus-message 6 "%s address %s %s BBDB"
2140 (if remove "Deleting" "Adding")
2142 (if remove "from" "to"))
2144 (funcall remove-function record)
2145 (bbdb-create-internal name nil net-address nil nil
2146 "ham sender added by spam.el")))))))
2148 (defun spam-BBDB-register-routine (articles &optional unregister)
2150 (dolist (article articles)
2151 (when (stringp (spam-fetch-field-from-fast article))
2152 (push (spam-fetch-field-from-fast article) addresses)))
2153 ;; now do the register/unregister action
2154 (spam-enter-ham-BBDB addresses unregister)))
2156 (defun spam-BBDB-unregister-routine (articles)
2157 (spam-BBDB-register-routine articles t))
2159 (defun spam-check-BBDB ()
2160 "Mail from people in the BBDB is classified as ham or non-spam"
2161 (let ((who (message-fetch-field "from"))
2162 bbdb-cache bbdb-hashtable)
2163 (when spam-cache-lookups
2164 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
2166 (setq bbdb-cache (make-vector 17 0)) ; a good starting hash value
2167 ;; this is based on the expanded (bbdb-hashtable) macro
2168 ;; without the debugging support
2169 (with-current-buffer (bbdb-buffer)
2171 (save-window-excursion
2172 (bbdb-records nil t)
2175 (intern (downcase (symbol-name symbol)) bbdb-cache))
2177 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
2179 (setq who (nth 1 (gnus-extract-address-components who)))
2181 (if spam-cache-lookups
2182 (intern-soft (downcase who) bbdb-cache)
2183 (bbdb-search-simple nil who))
2185 (if spam-use-BBDB-exclusive
2193 ;;; check the ifile backend; return nil if the mail was NOT classified
2197 (defun spam-get-ifile-database-parameter ()
2198 "Return the command-line parameter for ifile's database.
2199 See `spam-ifile-database'."
2200 (if spam-ifile-database
2201 (format "--db-file=%s" spam-ifile-database)
2204 (defun spam-check-ifile ()
2205 "Check the ifile backend for the classification of this message."
2206 (let ((article-buffer-name (buffer-name))
2209 (let ((temp-buffer-name (buffer-name))
2210 (db-param (spam-get-ifile-database-parameter)))
2211 (with-current-buffer article-buffer-name
2212 (apply 'call-process-region
2213 (point-min) (point-max) spam-ifile-program
2214 nil temp-buffer-name nil "-c"
2215 (if db-param `(,db-param "-q") `("-q"))))
2216 ;; check the return now (we're back in the temp buffer)
2217 (goto-char (point-min))
2219 (setq category (buffer-substring (point) (point-at-eol))))
2220 (when (not (zerop (length category))) ; we need a category here
2221 (if spam-ifile-all-categories
2222 (setq return category)
2223 ;; else, if spam-ifile-all-categories is not set...
2224 (when (string-equal spam-ifile-spam-category category)
2225 (setq return spam-split-group)))))) ; note return is nil otherwise
2228 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2229 "Register an article, given as a string, with a category.
2230 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2231 (let ((category (or category gnus-newsgroup-name))
2232 (add-or-delete-option (if unregister "-d" "-i"))
2233 (db (spam-get-ifile-database-parameter))
2236 (dolist (article articles)
2237 (let ((article-string (spam-get-article-as-string article)))
2238 (when (stringp article-string)
2239 (insert article-string))))
2240 (apply 'call-process-region
2241 (point-min) (point-max) spam-ifile-program
2243 add-or-delete-option category
2244 (if db `(,db "-h") `("-h"))))))
2246 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2247 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2249 (defun spam-ifile-unregister-spam-routine (articles)
2250 (spam-ifile-register-spam-routine articles t))
2252 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2253 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2255 (defun spam-ifile-unregister-ham-routine (articles)
2256 (spam-ifile-register-ham-routine articles t))
2263 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2264 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2265 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2266 (autoload 'spam-stat-buffer-is-spam "spam-stat")
2267 (autoload 'spam-stat-load "spam-stat")
2268 (autoload 'spam-stat-save "spam-stat")
2269 (autoload 'spam-stat-split-fancy "spam-stat"))
2272 (when (condition-case nil
2273 (let ((spam-stat-install-hooks nil))
2274 (require 'spam-stat))
2276 (defalias 'spam-stat-register-ham-routine 'ignore)
2277 (defalias 'spam-stat-register-spam-routine 'ignore)
2280 (defun spam-check-stat ()
2281 "Check the spam-stat backend for the classification of this message"
2282 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2283 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2285 (spam-stat-split-fancy)))
2287 (defun spam-stat-register-spam-routine (articles &optional unregister)
2288 (dolist (article articles)
2289 (let ((article-string (spam-get-article-as-string article)))
2291 (insert article-string)
2293 (spam-stat-buffer-change-to-non-spam)
2294 (spam-stat-buffer-is-spam))))))
2296 (defun spam-stat-unregister-spam-routine (articles)
2297 (spam-stat-register-spam-routine articles t))
2299 (defun spam-stat-register-ham-routine (articles &optional unregister)
2300 (dolist (article articles)
2301 (let ((article-string (spam-get-article-as-string article)))
2303 (insert article-string)
2305 (spam-stat-buffer-change-to-spam)
2306 (spam-stat-buffer-is-non-spam))))))
2308 (defun spam-stat-unregister-ham-routine (articles)
2309 (spam-stat-register-ham-routine articles t))
2311 (defun spam-maybe-spam-stat-load ()
2312 (when spam-use-stat (spam-stat-load)))
2314 (defun spam-maybe-spam-stat-save ()
2315 (when spam-use-stat (spam-stat-save)))))
2319 ;;{{{ Blacklists and whitelists.
2321 (defvar spam-whitelist-cache nil)
2322 (defvar spam-blacklist-cache nil)
2324 (defun spam-kill-whole-line ()
2326 (let ((kill-whole-line t))
2329 ;;; address can be a list, too
2330 (defun spam-enter-whitelist (address &optional remove)
2331 "Enter ADDRESS (list or single) into the whitelist.
2332 With a non-nil REMOVE, remove them."
2333 (interactive "sAddress: ")
2334 (spam-enter-list address spam-whitelist remove)
2335 (setq spam-whitelist-cache nil)
2336 (spam-clear-cache 'spam-use-whitelist))
2338 ;;; address can be a list, too
2339 (defun spam-enter-blacklist (address &optional remove)
2340 "Enter ADDRESS (list or single) into the blacklist.
2341 With a non-nil REMOVE, remove them."
2342 (interactive "sAddress: ")
2343 (spam-enter-list address spam-blacklist remove)
2344 (setq spam-blacklist-cache nil)
2345 (spam-clear-cache 'spam-use-whitelist))
2347 (defun spam-enter-list (addresses file &optional remove)
2348 "Enter ADDRESSES into the given FILE.
2349 Either the whitelist or the blacklist files can be used.
2350 With a non-nil REMOVE, remove the ADDRESSES."
2351 (if (stringp addresses)
2352 (spam-enter-list (list addresses) file remove)
2353 ;; else, we have a list of addresses here
2354 (unless (file-exists-p (file-name-directory file))
2355 (make-directory (file-name-directory file) t))
2356 (with-current-buffer
2357 (find-file-noselect file)
2358 (dolist (a addresses)
2360 (goto-char (point-min))
2361 (if (re-search-forward (regexp-quote a) nil t)
2362 ;; found the address
2364 (spam-kill-whole-line))
2365 ;; else, the address was not found
2367 (goto-char (point-max))
2373 (defun spam-filelist-build-cache (type)
2374 (let ((cache (if (eq type 'spam-use-blacklist)
2375 spam-blacklist-cache
2376 spam-whitelist-cache))
2378 (unless (gethash type spam-caches)
2380 (let ((address (pop cache)))
2381 (unless (zerop (length address)) ; 0 for a nil address too
2382 (setq address (regexp-quote address))
2383 ;; fix regexp-quote's treatment of user-intended regexes
2384 (while (string-match "\\\\\\*" address)
2385 (setq address (replace-match ".*" t t address))))
2386 (push address parsed-cache)))
2387 (puthash type parsed-cache spam-caches))))
2389 (defun spam-filelist-check-cache (type from)
2390 (when (stringp from)
2391 (spam-filelist-build-cache type)
2393 (dolist (address (gethash type spam-caches))
2394 (when (and address (string-match address from))
2399 ;;; returns t if the sender is in the whitelist, nil or
2400 ;;; spam-split-group otherwise
2401 (defun spam-check-whitelist ()
2402 ;; FIXME! Should it detect when file timestamps change?
2403 (unless spam-whitelist-cache
2404 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2405 (if (spam-from-listed-p 'spam-use-whitelist)
2407 (if spam-use-whitelist-exclusive
2411 (defun spam-check-blacklist ()
2412 ;; FIXME! Should it detect when file timestamps change?
2413 (unless spam-blacklist-cache
2414 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2415 (and (spam-from-listed-p 'spam-use-blacklist)
2418 (defun spam-parse-list (file)
2419 (when (file-readable-p file)
2420 (let (contents address)
2422 (insert-file-contents file)
2424 (setq address (buffer-substring (point) (point-at-eol)))
2426 ;; insert the e-mail address if detected, otherwise the raw data
2427 (unless (zerop (length address))
2429 (nth 1 (gnus-extract-address-components address))))
2430 (push (or pure-address address) contents)))))
2431 (nreverse contents))))
2433 (defun spam-from-listed-p (type)
2434 (let ((from (message-fetch-field "from"))
2436 (spam-filelist-check-cache type from)))
2438 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2439 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2440 (declassification (if blacklist 'ham 'spam))
2442 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2444 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2445 from addresses unregister-list article-unregister-list)
2446 (dolist (article articles)
2447 (let ((from (spam-fetch-field-from-fast article))
2448 (id (spam-fetch-field-message-id-fast article))
2450 (when (stringp from)
2451 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2452 (when (and (not sender-ignored)
2453 (stringp ignore-regex)
2454 (string-match ignore-regex from))
2455 (setq sender-ignored t)))
2456 ;; remember the messages we need to unregister, unless remove is set
2459 (spam-log-unregistration-needed-p
2460 id 'process declassification de-symbol))
2461 (push article article-unregister-list)
2462 (push from unregister-list))
2463 (unless sender-ignored
2464 (push from addresses)))))
2467 (funcall enter-function addresses t) ; unregister all these addresses
2468 ;; else, register normally and unregister what we need to
2469 (funcall remove-function unregister-list t)
2470 (dolist (article article-unregister-list)
2471 (spam-log-undo-registration
2472 (spam-fetch-field-message-id-fast article)
2476 (funcall enter-function addresses nil))))
2478 (defun spam-blacklist-unregister-routine (articles)
2479 (spam-blacklist-register-routine articles t))
2481 (defun spam-blacklist-register-routine (articles &optional unregister)
2482 (spam-filelist-register-routine articles t unregister))
2484 (defun spam-whitelist-unregister-routine (articles)
2485 (spam-whitelist-register-routine articles t))
2487 (defun spam-whitelist-register-routine (articles &optional unregister)
2488 (spam-filelist-register-routine articles nil unregister))
2492 ;;{{{ Spam-report glue (gmane and resend reporting)
2493 (defun spam-report-gmane-register-routine (articles)
2495 (apply 'spam-report-gmane-spam articles)))
2497 (defun spam-report-gmane-unregister-routine (articles)
2499 (apply 'spam-report-gmane-ham articles)))
2501 (defun spam-report-resend-register-ham-routine (articles)
2502 (spam-report-resend-register-routine articles t))
2504 (defun spam-report-resend-register-routine (articles &optional ham)
2505 (let* ((resend-to-gp
2507 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2508 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2509 (spam-report-resend-to (or (car-safe resend-to-gp)
2510 spam-report-resend-to)))
2511 (spam-report-resend articles ham)))
2516 (defun spam-check-bogofilter-headers (&optional score)
2517 (let ((header (message-fetch-field spam-bogofilter-header)))
2518 (when header ; return nil when no header
2519 (if score ; scoring mode
2520 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2521 (match-string 1 header)
2523 ;; spam detection mode
2524 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2526 spam-split-group)))))
2528 ;; return something sensible if the score can't be determined
2529 (defun spam-bogofilter-score (&optional recheck)
2530 "Get the Bogofilter spamicity score."
2532 (save-window-excursion
2533 (gnus-summary-show-article t)
2534 (set-buffer gnus-article-buffer)
2535 (let ((score (or (unless recheck
2536 (spam-check-bogofilter-headers t))
2537 (spam-check-bogofilter t))))
2538 (gnus-summary-show-article)
2539 (message "Spamicity score %s" score)
2542 (defun spam-verify-bogofilter ()
2543 "Verify the Bogofilter version is sufficient."
2544 (when (eq spam-bogofilter-valid 'unknown)
2545 (setq spam-bogofilter-valid
2546 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2547 (shell-command-to-string
2548 (format "%s -V" spam-bogofilter-program))))))
2549 spam-bogofilter-valid)
2551 (defun spam-check-bogofilter (&optional score)
2552 "Check the Bogofilter backend for the classification of this message."
2553 (if (spam-verify-bogofilter)
2554 (let ((article-buffer-name (buffer-name))
2555 (db spam-bogofilter-database-directory)
2558 (let ((temp-buffer-name (buffer-name)))
2559 (with-current-buffer article-buffer-name
2560 (apply 'call-process-region
2561 (point-min) (point-max)
2562 spam-bogofilter-program
2563 nil temp-buffer-name nil
2564 (if db `("-d" ,db "-v") `("-v"))))
2565 (setq return (spam-check-bogofilter-headers score))))
2567 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2569 (defun spam-bogofilter-register-with-bogofilter (articles
2571 &optional unregister)
2572 "Register an article, given as a string, as spam or non-spam."
2573 (if (spam-verify-bogofilter)
2574 (dolist (article articles)
2575 (let ((article-string (spam-get-article-as-string article))
2576 (db spam-bogofilter-database-directory)
2577 (switch (if unregister
2579 spam-bogofilter-spam-strong-switch
2580 spam-bogofilter-ham-strong-switch)
2582 spam-bogofilter-spam-switch
2583 spam-bogofilter-ham-switch))))
2584 (when (stringp article-string)
2586 (insert article-string)
2588 (apply 'call-process-region
2589 (point-min) (point-max)
2590 spam-bogofilter-program
2592 (if db `("-d" ,db "-v") `("-v")))))))
2593 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2595 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2596 (spam-bogofilter-register-with-bogofilter articles t unregister))
2598 (defun spam-bogofilter-unregister-spam-routine (articles)
2599 (spam-bogofilter-register-spam-routine articles t))
2601 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2602 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2604 (defun spam-bogofilter-unregister-ham-routine (articles)
2605 (spam-bogofilter-register-ham-routine articles t))
2611 (defun spam-check-spamoracle ()
2612 "Run spamoracle on an article to determine whether it's spam."
2613 (let ((article-buffer-name (buffer-name)))
2615 (let ((temp-buffer-name (buffer-name)))
2616 (with-current-buffer article-buffer-name
2618 (apply 'call-process-region
2619 (point-min) (point-max)
2620 spam-spamoracle-binary
2621 nil temp-buffer-name nil
2622 (if spam-spamoracle-database
2623 `("-f" ,spam-spamoracle-database "mark")
2627 (set-buffer temp-buffer-name)
2628 (goto-char (point-min))
2629 (when (re-search-forward "^X-Spam: yes;" nil t)
2631 (error "Error running spamoracle: %s" status))))))))
2633 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2634 "Run spamoracle in training mode."
2636 (let ((temp-buffer-name (buffer-name)))
2638 (goto-char (point-min))
2639 (dolist (article articles)
2640 (insert (spam-get-article-as-string article)))
2641 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2645 (apply 'call-process-region
2646 (point-min) (point-max)
2647 spam-spamoracle-binary
2648 nil temp-buffer-name nil
2649 (if spam-spamoracle-database
2650 `("-f" ,spam-spamoracle-database
2653 (unless (eq 0 status)
2654 (error "Error running spamoracle: %s" status)))))))
2656 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2657 (spam-spamoracle-learn articles nil unregister))
2659 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2660 (spam-spamoracle-learn-ham articles t))
2662 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2663 (spam-spamoracle-learn articles t unregister))
2665 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2666 (spam-spamoracle-learn-spam articles t))
2671 ;;; based mostly on the bogofilter code
2672 (defun spam-check-spamassassin-headers (&optional score)
2673 "Check the SpamAssassin headers for the classification of this message."
2674 (if score ; scoring mode
2675 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2677 (if (string-match spam-spamassassin-score-regexp header)
2678 (match-string 1 header)
2680 ;; spam detection mode
2681 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2682 (when header ; return nil when no header
2683 (when (string-match spam-spamassassin-positive-spam-flag-header
2685 spam-split-group)))))
2687 (defun spam-check-spamassassin (&optional score)
2688 "Check the SpamAssassin backend for the classification of this message."
2689 (let ((article-buffer-name (buffer-name)))
2691 (let ((temp-buffer-name (buffer-name)))
2692 (with-current-buffer article-buffer-name
2693 (apply 'call-process-region
2694 (point-min) (point-max) spam-assassin-program
2695 nil temp-buffer-name nil spam-spamassassin-arguments))
2696 ;; check the return now (we're back in the temp buffer)
2697 (goto-char (point-min))
2698 (spam-check-spamassassin-headers score)))))
2700 ;; return something sensible if the score can't be determined
2701 (defun spam-spamassassin-score (&optional recheck)
2702 "Get the SpamAssassin score"
2704 (save-window-excursion
2705 (gnus-summary-show-article t)
2706 (set-buffer gnus-article-buffer)
2707 (let ((score (or (unless recheck
2708 (spam-check-spamassassin-headers t))
2709 (spam-check-spamassassin t))))
2710 (gnus-summary-show-article)
2711 (message "SpamAssassin score %s" score)
2714 (defun spam-spamassassin-register-with-sa-learn (articles spam
2715 &optional unregister)
2716 "Register articles with spamassassin's sa-learn as spam or non-spam."
2718 (let ((action (if unregister spam-sa-learn-unregister-switch
2719 (if spam spam-sa-learn-spam-switch
2720 spam-sa-learn-ham-switch)))
2721 (summary-buffer-name (buffer-name)))
2723 ;; group the articles into mbox format
2724 (dolist (article articles)
2725 (let (article-string)
2726 (with-current-buffer summary-buffer-name
2727 (setq article-string (spam-get-article-as-string article)))
2728 (when (stringp article-string)
2730 (insert (concat "From nobody " (current-time-string) "\n"))
2731 (insert article-string)
2733 ;; call sa-learn on all messages at the same time
2734 (apply 'call-process-region
2735 (point-min) (point-max)
2736 spam-sa-learn-program
2737 nil nil nil "--mbox"
2738 (if spam-sa-learn-rebuild
2740 `("--no-rebuild" ,action)))))))
2742 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2743 (spam-spamassassin-register-with-sa-learn articles t unregister))
2745 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2746 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2748 (defun spam-spamassassin-unregister-spam-routine (articles)
2749 (spam-spamassassin-register-with-sa-learn articles t t))
2751 (defun spam-spamassassin-unregister-ham-routine (articles)
2752 (spam-spamassassin-register-with-sa-learn articles nil t))
2757 ;;; based mostly on the bogofilter code
2758 (defun spam-check-bsfilter-headers (&optional score)
2760 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2762 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2763 (when header ; return nil when no header
2764 (when (string-match "YES" header)
2765 spam-split-group)))))
2767 ;; return something sensible if the score can't be determined
2768 (defun spam-bsfilter-score (&optional recheck)
2769 "Get the Bsfilter spamicity score."
2771 (save-window-excursion
2772 (gnus-summary-show-article t)
2773 (set-buffer gnus-article-buffer)
2774 (let ((score (or (unless recheck
2775 (spam-check-bsfilter-headers t))
2776 (spam-check-bsfilter t))))
2777 (gnus-summary-show-article)
2778 (message "Spamicity score %s" score)
2781 (defun spam-check-bsfilter (&optional score)
2782 "Check the Bsfilter backend for the classification of this message."
2783 (let ((article-buffer-name (buffer-name))
2784 (dir spam-bsfilter-database-directory)
2787 (let ((temp-buffer-name (buffer-name)))
2788 (with-current-buffer article-buffer-name
2789 (apply 'call-process-region
2790 (point-min) (point-max)
2791 spam-bsfilter-program
2792 nil temp-buffer-name nil
2795 "--insert-probability"
2797 (list "--homedir" dir))))
2798 (setq return (spam-check-bsfilter-headers score))))
2801 (defun spam-bsfilter-register-with-bsfilter (articles
2803 &optional unregister)
2804 "Register an article, given as a string, as spam or non-spam."
2805 (dolist (article articles)
2806 (let ((article-string (spam-get-article-as-string article))
2807 (switch (if unregister
2809 spam-bsfilter-spam-strong-switch
2810 spam-bsfilter-ham-strong-switch)
2812 spam-bsfilter-spam-switch
2813 spam-bsfilter-ham-switch))))
2814 (when (stringp article-string)
2816 (insert article-string)
2817 (apply 'call-process-region
2818 (point-min) (point-max)
2819 spam-bsfilter-program
2822 (when spam-bsfilter-database-directory
2824 spam-bsfilter-database-directory))))))))
2826 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2827 (spam-bsfilter-register-with-bsfilter articles t unregister))
2829 (defun spam-bsfilter-unregister-spam-routine (articles)
2830 (spam-bsfilter-register-spam-routine articles t))
2832 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2833 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2835 (defun spam-bsfilter-unregister-ham-routine (articles)
2836 (spam-bsfilter-register-ham-routine articles t))
2840 ;;{{{ CRM114 Mailfilter
2841 (defun spam-check-crm114-headers (&optional score)
2842 (let ((header (message-fetch-field spam-crm114-header)))
2843 (when header ; return nil when no header
2844 (if score ; scoring mode
2845 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2846 (match-string 1 header)
2848 ;; spam detection mode
2849 (when (string-match spam-crm114-positive-spam-header
2851 spam-split-group)))))
2853 ;; return something sensible if the score can't be determined
2854 (defun spam-crm114-score ()
2855 "Get the CRM114 Mailfilter pR."
2857 (save-window-excursion
2858 (gnus-summary-show-article t)
2859 (set-buffer gnus-article-buffer)
2860 (let ((score (or (spam-check-crm114-headers t)
2861 (spam-check-crm114 t))))
2862 (gnus-summary-show-article)
2863 (message "pR: %s" score)
2866 (defun spam-check-crm114 (&optional score)
2867 "Check the CRM114 Mailfilter backend for the classification of this message."
2868 (let ((article-buffer-name (buffer-name))
2869 (db spam-crm114-database-directory)
2872 (let ((temp-buffer-name (buffer-name)))
2873 (with-current-buffer article-buffer-name
2874 (apply 'call-process-region
2875 (point-min) (point-max)
2877 nil temp-buffer-name nil
2878 (when db (list (concat "--fileprefix=" db)))))
2879 (setq return (spam-check-crm114-headers score))))
2882 (defun spam-crm114-register-with-crm114 (articles
2884 &optional unregister)
2885 "Register an article, given as a string, as spam or non-spam."
2886 (dolist (article articles)
2887 (let ((article-string (spam-get-article-as-string article))
2888 (db spam-crm114-database-directory)
2889 (switch (if unregister
2891 spam-crm114-spam-strong-switch
2892 spam-crm114-ham-strong-switch)
2894 spam-crm114-spam-switch
2895 spam-crm114-ham-switch))))
2896 (when (stringp article-string)
2898 (insert article-string)
2900 (apply 'call-process-region
2901 (point-min) (point-max)
2904 (when db (list switch (concat "--fileprefix=" db)))))))))
2906 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2907 (spam-crm114-register-with-crm114 articles t unregister))
2909 (defun spam-crm114-unregister-spam-routine (articles)
2910 (spam-crm114-register-spam-routine articles t))
2912 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2913 (spam-crm114-register-with-crm114 articles nil unregister))
2915 (defun spam-crm114-unregister-ham-routine (articles)
2916 (spam-crm114-register-ham-routine articles t))
2925 (defun spam-initialize (&rest symbols)
2926 "Install the spam.el hooks and do other initialization.
2927 When SYMBOLS is given, set those variables to t. This is so you
2928 can call `spam-initialize' before you set spam-use-* variables on
2929 explicitly, and matters only if you need the extra headers
2930 installed through `spam-necessary-extra-headers'."
2933 (dolist (var symbols)
2936 (dolist (header (spam-necessary-extra-headers))
2937 (add-to-list 'nnmail-extra-headers header)
2938 (add-to-list 'gnus-extra-headers header))
2940 (setq spam-install-hooks t)
2941 ;; TODO: How do we redo this every time the `spam' face is customized?
2942 (push '((eq mark gnus-spam-mark) . spam)
2943 gnus-summary-highlight)
2944 ;; Add hooks for loading and saving the spam stats
2945 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2946 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2947 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2948 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2949 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2950 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2951 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2953 (defun spam-unload-hook ()
2954 "Uninstall the spam.el hooks."
2956 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2957 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2958 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2959 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2960 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2961 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2962 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2964 (add-hook 'spam-unload-hook 'spam-unload-hook)
2966 (when spam-install-hooks
2972 ;;; spam.el ends here