1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;;; This module addresses a few aspects of spam control under Gnus. Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
30 ;;; The integration with Gnus is not yet complete. See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
33 ;;; Several TODO items are marked as such
35 ;; TODO: cross-server splitting, remote processing, training through files
39 ;;{{{ compilation directives and autoloads/requires
41 (eval-when-compile (require 'cl))
42 (eval-when-compile (require 'spam-report))
46 (require 'gnus-uu) ; because of key prefix issues
47 ;;; for the definitions of group content classification and spam processors
49 (require 'message) ;for the message-fetch-field functions
51 ;; for nnimap-split-download-body-default
52 (eval-when-compile (require 'nnimap))
56 (autoload 'query-dig "dig"))
58 ;; autoload spam-report
60 (autoload 'spam-report-gmane "spam-report")
61 (autoload 'spam-report-resend "spam-report"))
63 ;; autoload gnus-registry
65 (autoload 'gnus-registry-group-count "gnus-registry")
66 (autoload 'gnus-registry-add-group "gnus-registry")
67 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
68 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
72 (autoload 'query-dns "dns"))
76 ;;{{{ Main parameters.
77 (defvar spam-backends nil
78 "List of spam.el backends with all the pertinent data.
79 Populated by spam-install-backend-super.")
82 "Spam configuration.")
84 (defcustom spam-summary-exit-behavior 'default
85 "Exit behavior at the time of summary exit.
86 Note that setting the spam-use-move or spam-use-copy backends on
87 a group through group/topic parameters overrides this mechanism."
88 :type '(choice (const 'default :tag
89 "Move spam out of all groups. Move ham out of spam groups.")
91 "Move spam out of all groups. Move ham out of all groups.")
92 (const 'move-none :tag
93 "Never move spam or ham out of any groups."))
96 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
97 "Directory for spam whitelists and blacklists."
101 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
102 "Whether new messages in a spam group should get the spam-mark."
106 (defcustom spam-log-to-registry nil
107 "Whether spam/ham processing should be logged in the registry."
111 (defcustom spam-split-symbolic-return nil
112 "Whether `spam-split' should work with symbols or group names."
116 (defcustom spam-split-symbolic-return-positive nil
117 "Whether `spam-split' should ALWAYS work with symbols or group names.
118 Do not set this if you use `spam-split' in a fancy split
123 (defcustom spam-mark-only-unseen-as-spam t
124 "Whether only unseen articles should be marked as spam in spam groups.
125 When nil, all unread articles in a spam group are marked as
126 spam. Set this if you want to leave an article unread in a spam group
127 without losing it to the automatic spam-marking process."
131 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
132 "Whether ham should be marked unread before it's moved.
133 The article is moved out of a spam group according to ham-process-destination.
134 This variable is an official entry in the international Longest Variable Name
139 (defcustom spam-disable-spam-split-during-ham-respool nil
140 "Whether `spam-split' should be ignored while resplitting ham.
141 This is useful to prevent ham from ending up in the same spam
142 group after the resplit. Don't set this to t if you have `spam-split' as the
143 last rule in your split configuration."
147 (defcustom spam-autodetect-recheck-messages nil
148 "Should spam.el recheck all meessages when autodetecting?
149 Normally this is nil, so only unseen messages will be checked."
153 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
154 "The location of the whitelist.
155 The file format is one regular expression per line.
156 The regular expression is matched against the address."
160 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
161 "The location of the blacklist.
162 The file format is one regular expression per line.
163 The regular expression is matched against the address."
167 (defcustom spam-use-dig t
168 "Whether `query-dig' should be used instead of `query-dns'."
172 (defcustom spam-use-gmane-xref nil
173 "Whether the Gmane spam xref should be used by `spam-split'."
177 (defcustom spam-use-blacklist nil
178 "Whether the blacklist should be used by `spam-split'."
182 (defcustom spam-blacklist-ignored-regexes nil
183 "Regular expressions that the blacklist should ignore."
184 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
187 (defcustom spam-use-whitelist nil
188 "Whether the whitelist should be used by `spam-split'."
192 (defcustom spam-use-whitelist-exclusive nil
193 "Whether whitelist-exclusive should be used by `spam-split'.
194 Exclusive whitelisting means that all messages from senders not in the whitelist
195 are considered spam."
199 (defcustom spam-use-blackholes nil
200 "Whether blackholes should be used by `spam-split'."
204 (defcustom spam-use-hashcash nil
205 "Whether hashcash payments should be detected by `spam-split'."
209 (defcustom spam-use-regex-headers nil
210 "Whether a header regular expression match should be used by `spam-split'.
211 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
215 (defcustom spam-use-regex-body nil
216 "Whether a body regular expression match should be used by `spam-split'.
217 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
221 (defcustom spam-use-bogofilter-headers nil
222 "Whether bogofilter headers should be used by `spam-split'.
223 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
227 (defcustom spam-use-bogofilter nil
228 "Whether bogofilter should be invoked by `spam-split'.
229 Enable this if you want Gnus to invoke Bogofilter on new messages."
233 (defcustom spam-use-bsfilter-headers nil
234 "Whether bsfilter headers should be used by `spam-split'.
235 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
239 (defcustom spam-use-bsfilter nil
240 "Whether bsfilter should be invoked by `spam-split'.
241 Enable this if you want Gnus to invoke Bsfilter on new messages."
245 (defcustom spam-use-BBDB nil
246 "Whether BBDB should be used by `spam-split'."
250 (defcustom spam-use-BBDB-exclusive nil
251 "Whether BBDB-exclusive should be used by `spam-split'.
252 Exclusive BBDB means that all messages from senders not in the BBDB are
257 (defcustom spam-use-ifile nil
258 "Whether ifile should be used by `spam-split'."
262 (defcustom spam-use-stat nil
263 "Whether `spam-stat' should be used by `spam-split'."
267 (defcustom spam-use-spamoracle nil
268 "Whether spamoracle should be used by `spam-split'."
272 (defcustom spam-use-spamassassin nil
273 "Whether spamassassin should be invoked by `spam-split'.
274 Enable this if you want Gnus to invoke SpamAssassin on new messages."
278 (defcustom spam-use-spamassassin-headers nil
279 "Whether spamassassin headers should be checked by `spam-split'.
280 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
285 (defcustom spam-use-crm114 nil
286 "Whether the CRM114 Mailfilter should be used by `spam-split'."
290 (defcustom spam-install-hooks (or
295 spam-use-whitelist-exclusive
298 spam-use-regex-headers
301 spam-use-bogofilter-headers
302 spam-use-spamassassin
303 spam-use-spamassassin-headers
305 spam-use-bsfilter-headers
307 spam-use-BBDB-exclusive
312 "Whether the spam hooks should be installed.
313 Default to t if one of the spam-use-* variables is set."
317 (defcustom spam-split-group "spam"
318 "Group name where incoming spam should be put by `spam-split'."
322 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
323 ;;; not regular expressions
324 (defcustom spam-junk-mailgroups (cons
326 '("mail.junk" "poste.pourriel"))
327 "Mailgroups with spam contents.
328 All unmarked article in such group receive the spam mark on group entry."
329 :type '(repeat (string :tag "Group"))
333 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
334 "The group where spam xrefs can be found on Gmane.
335 Only meaningful if you enable `spam-use-gmane-xref'."
339 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
340 "dev.null.dk" "relays.visi.com")
341 "List of blackhole servers.
342 Only meaningful if you enable `spam-use-blackholes'."
343 :type '(repeat (string :tag "Server"))
346 (defcustom spam-blackhole-good-server-regex nil
347 "String matching IP addresses that should not be checked in the blackholes.
348 Only meaningful if you enable `spam-use-blackholes'."
349 :type '(radio (const nil)
350 (regexp :format "%t: %v\n" :size 0))
353 (defcustom spam-face 'gnus-splash-face
354 "Face for spam-marked articles."
358 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
359 "Regular expression for positive header spam matches.
360 Only meaningful if you enable `spam-use-regex-headers'."
361 :type '(repeat (regexp :tag "Regular expression to match spam header"))
364 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
365 "Regular expression for positive header ham matches.
366 Only meaningful if you enable `spam-use-regex-headers'."
367 :type '(repeat (regexp :tag "Regular expression to match ham header"))
370 (defcustom spam-regex-body-spam '()
371 "Regular expression for positive body spam matches.
372 Only meaningful if you enable `spam-use-regex-body'."
373 :type '(repeat (regexp :tag "Regular expression to match spam body"))
376 (defcustom spam-regex-body-ham '()
377 "Regular expression for positive body ham matches.
378 Only meaningful if you enable `spam-use-regex-body'."
379 :type '(repeat (regexp :tag "Regular expression to match ham body"))
382 (defgroup spam-ifile nil
383 "Spam ifile configuration."
386 (defcustom spam-ifile-path (exec-installed-p "ifile")
387 "File path of the ifile executable program."
388 :type '(choice (file :tag "Location of ifile")
389 (const :tag "ifile is not installed"))
392 (defcustom spam-ifile-database-path nil
393 "File path of the ifile database."
394 :type '(choice (file :tag "Location of the ifile database")
395 (const :tag "Use the default"))
398 (defcustom spam-ifile-spam-category "spam"
399 "Name of the spam ifile category."
403 (defcustom spam-ifile-ham-category nil
404 "Name of the ham ifile category.
405 If nil, the current group name will be used."
406 :type '(choice (string :tag "Use a fixed category")
407 (const :tag "Use the current group name"))
410 (defcustom spam-ifile-all-categories nil
411 "Whether the ifile check will return all categories, or just spam.
412 Set this to t if you want to use the `spam-split' invocation of ifile as
413 your main source of newsgroup names."
417 (defgroup spam-bogofilter nil
418 "Spam bogofilter configuration."
421 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
422 "File path of the Bogofilter executable program."
423 :type '(choice (file :tag "Location of bogofilter")
424 (const :tag "Bogofilter is not installed"))
425 :group 'spam-bogofilter)
427 (defcustom spam-bogofilter-header "X-Bogosity"
428 "The header that Bogofilter inserts in messages."
430 :group 'spam-bogofilter)
432 (defcustom spam-bogofilter-spam-switch "-s"
433 "The switch that Bogofilter uses to register spam messages."
435 :group 'spam-bogofilter)
437 (defcustom spam-bogofilter-ham-switch "-n"
438 "The switch that Bogofilter uses to register ham messages."
440 :group 'spam-bogofilter)
442 (defcustom spam-bogofilter-spam-strong-switch "-S"
443 "The switch that Bogofilter uses to unregister ham messages."
445 :group 'spam-bogofilter)
447 (defcustom spam-bogofilter-ham-strong-switch "-N"
448 "The switch that Bogofilter uses to unregister spam messages."
450 :group 'spam-bogofilter)
452 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
453 "The regex on `spam-bogofilter-header' for positive spam identification."
455 :group 'spam-bogofilter)
457 (defcustom spam-bogofilter-database-directory nil
458 "Directory path of the Bogofilter databases."
459 :type '(choice (directory
460 :tag "Location of the Bogofilter database directory")
461 (const :tag "Use the default"))
462 :group 'spam-bogofilter)
464 (defgroup spam-bsfilter nil
465 "Spam bsfilter configuration."
468 (defcustom spam-bsfilter-path (executable-find "bsfilter")
469 "File path of the Bsfilter executable program."
470 :type '(choice (file :tag "Location of bsfilter")
471 (const :tag "Bsfilter is not installed"))
472 :group 'spam-bsfilter)
474 (defcustom spam-bsfilter-header "X-Spam-Flag"
475 "The header inserted by Bsfilter to flag spam."
477 :group 'spam-bsfilter)
479 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
480 "The header that Bsfilter inserts in messages."
482 :group 'spam-bsfilter)
484 (defcustom spam-bsfilter-spam-switch "--add-spam"
485 "The switch that Bsfilter uses to register spam messages."
487 :group 'spam-bsfilter)
489 (defcustom spam-bsfilter-ham-switch "--add-ham"
490 "The switch that Bsfilter uses to register ham messages."
492 :group 'spam-bsfilter)
494 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
495 "The switch that Bsfilter uses to unregister ham messages."
497 :group 'spam-bsfilter)
499 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
500 "The switch that Bsfilter uses to unregister spam messages."
502 :group 'spam-bsfilter)
504 (defcustom spam-bsfilter-database-directory nil
505 "Directory path of the Bsfilter databases."
506 :type '(choice (directory
507 :tag "Location of the Bsfilter database directory")
508 (const :tag "Use the default"))
509 :group 'spam-bsfilter)
511 (defgroup spam-spamoracle nil
512 "Spam spamoracle configuration."
515 (defcustom spam-spamoracle-database nil
516 "Location of spamoracle database file.
517 When nil, use the default spamoracle database."
518 :type '(choice (directory :tag "Location of spamoracle database file.")
519 (const :tag "Use the default"))
520 :group 'spam-spamoracle)
522 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
523 "Location of the spamoracle binary."
524 :type '(choice (directory :tag "Location of the spamoracle binary")
525 (const :tag "Use the default"))
526 :group 'spam-spamoracle)
528 (defgroup spam-spamassassin nil
529 "Spam SpamAssassin configuration."
532 (defcustom spam-spamassassin-path (executable-find "spamassassin")
533 "File path of the spamassassin executable program.
534 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
535 spamd man pages for more information on these programs."
536 :type '(choice (file :tag "Location of spamc")
537 (const :tag "spamassassin is not installed"))
538 :group 'spam-spamassassin)
540 (defcustom spam-spamassassin-arguments ()
541 "Arguments to pass to the spamassassin executable.
542 This must be a list. For example, `(\"-C\" \"configfile\")'."
543 :type '(restricted-sexp :match-alternatives (listp))
544 :group 'spam-spamassassin)
546 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
547 "The header inserted by SpamAssassin to flag spam."
549 :group 'spam-spamassassin)
551 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
552 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
555 :group 'spam-spamassassin)
557 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
558 "The header inserted by SpamAssassin, giving extended scoring information"
560 :group 'spam-spamassassin)
562 (defcustom spam-sa-learn-path (executable-find "sa-learn")
563 "File path of the sa-learn executable program."
564 :type '(choice (file :tag "Location of spamassassin")
565 (const :tag "spamassassin is not installed"))
566 :group 'spam-spamassassin)
568 (defcustom spam-sa-learn-rebuild t
569 "Whether sa-learn should rebuild the database every time it is called
570 Enable this if you want sa-learn to rebuild the database automatically. Doing
571 this will slightly increase the running time of the spam registration process.
572 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
573 order for SpamAssassin to recognize the new registered spam."
575 :group 'spam-spamassassin)
577 (defcustom spam-sa-learn-spam-switch "--spam"
578 "The switch that sa-learn uses to register spam messages"
580 :group 'spam-spamassassin)
582 (defcustom spam-sa-learn-ham-switch "--ham"
583 "The switch that sa-learn uses to register ham messages"
585 :group 'spam-spamassassin)
587 (defcustom spam-sa-learn-unregister-switch "--forget"
588 "The switch that sa-learn uses to unregister messages messages"
590 :group 'spam-spamassassin)
592 (defgroup spam-crm114 nil
593 "Spam CRM114 Mailfilter configuration."
596 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
597 "File path of the CRM114 Mailfilter executable program."
598 :type '(choice (file :tag "Location of CRM114 Mailfilter")
599 (const :tag "CRM114 Mailfilter is not installed"))
602 (defcustom spam-crm114-header "X-CRM114-Status"
603 "The header that CRM114 Mailfilter inserts in messages."
607 (defcustom spam-crm114-spam-switch "--learnspam"
608 "The switch that CRM114 Mailfilter uses to register spam messages."
612 (defcustom spam-crm114-ham-switch "--learnnonspam"
613 "The switch that CRM114 Mailfilter uses to register ham messages."
617 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
618 "The switch that CRM114 Mailfilter uses to unregister ham messages."
622 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
623 "The switch that CRM114 Mailfilter uses to unregister spam messages."
627 (defcustom spam-crm114-positive-spam-header "^SPAM"
628 "The regex on `spam-crm114-header' for positive spam identification."
632 (defcustom spam-crm114-database-directory nil
633 "Directory path of the CRM114 Mailfilter databases."
634 :type '(choice (directory
635 :tag "Location of the CRM114 Mailfilter database directory")
636 (const :tag "Use the default"))
639 ;;; Key bindings for spam control.
641 (gnus-define-keys gnus-summary-mode-map
642 "St" spam-generic-score
643 "Sx" gnus-summary-mark-as-spam
644 "Mst" spam-generic-score
645 "Msx" gnus-summary-mark-as-spam
646 "\M-d" gnus-summary-mark-as-spam)
648 (defvar spam-cache-lookups t
649 "Whether spam.el will try to cache lookups using `spam-caches'.")
651 (defvar spam-caches (make-hash-table
654 "Cache of spam detection entries.")
656 (defvar spam-old-articles nil
657 "List of old ham and spam articles, generated when a group is entered.")
659 (defvar spam-split-disabled nil
660 "If non-nil, `spam-split' is disabled, and always returns nil.")
662 (defvar spam-split-last-successful-check nil
664 `spam-split' will set this to nil or a spam-use-XYZ check if it
667 ;; internal variables for backends
668 ;; TODO: find a way to create these on the fly in spam-install-backend-super
669 (defvar spam-use-copy nil)
670 (defvar spam-use-move nil)
671 (defvar spam-use-gmane nil)
672 (defvar spam-use-resend nil)
676 ;;{{{ convenience functions
678 (defun spam-clear-cache (symbol)
679 "Clear the spam-caches entry for a check."
680 (remhash symbol spam-caches))
682 (defun spam-xor (a b)
684 (and (or a b) (not (and a b))))
686 (defun spam-set-difference (list1 list2)
687 "Return a set difference of LIST1 and LIST2.
688 When either list is nil, the other is returned."
689 (if (and list1 list2)
690 ;; we have two non-nil lists
692 (dolist (item (append list1 list2))
693 (when (and (memq item list1) (memq item list2))
694 (setq list1 (delq item list1))
695 (setq list2 (delq item list2))))
696 (append list1 list2))
697 ;; if either of the lists was nil, return the other one
698 (if list1 list1 list2)))
700 (defun spam-group-ham-mark-p (group mark &optional spam)
701 "Checks if MARK is considered a ham mark in GROUP."
702 (when (stringp group)
703 (let* ((marks (spam-group-ham-marks group spam))
704 (marks (if (symbolp mark)
706 (mapcar 'symbol-value marks))))
709 (defun spam-group-spam-mark-p (group mark)
710 "Checks if MARK is considered a spam mark in GROUP."
711 (spam-group-ham-mark-p group mark t))
713 (defun spam-group-ham-marks (group &optional spam)
714 "In GROUP, get all the ham marks."
715 (when (stringp group)
716 (let* ((marks (if spam
717 (gnus-parameter-spam-marks group)
718 (gnus-parameter-ham-marks group)))
720 (marks (if (listp (car marks)) (car marks) marks)))
723 (defun spam-group-spam-marks (group)
724 "In GROUP, get all the spam marks."
725 (spam-group-ham-marks group t))
727 (defun spam-group-spam-contents-p (group)
728 "Is GROUP a spam group?"
729 (if (and (stringp group) (< 0 (length group)))
730 (or (member group spam-junk-mailgroups)
731 (memq 'gnus-group-spam-classification-spam
732 (gnus-parameter-spam-contents group)))
735 (defun spam-group-ham-contents-p (group)
736 "Is GROUP a ham group?"
738 (memq 'gnus-group-spam-classification-ham
739 (gnus-parameter-spam-contents group))
742 (defun spam-classifications ()
743 "Return list of valid classifications"
746 (defun spam-classification-valid-p (classification)
747 "Is CLASSIFICATION a valid spam/ham classification?"
748 (memq classification (spam-classifications)))
750 (defun spam-backend-properties ()
751 "Return list of valid classifications."
752 '(statistical mover check hrf srf huf suf))
754 (defun spam-backend-property-valid-p (property)
755 "Is PROPERTY a valid backend property?"
756 (memq property (spam-backend-properties)))
758 (defun spam-backend-function-type-valid-p (type)
759 (or (eq type 'registration)
760 (eq type 'unregistration)))
762 (defun spam-process-type-valid-p (process-type)
763 (or (eq process-type 'incoming)
764 (eq process-type 'process)))
766 (defun spam-list-articles (articles classification)
767 (let ((mark-check (if (eq classification 'spam)
768 'spam-group-spam-mark-p
769 'spam-group-ham-mark-p))
770 alist mark-cache-yes mark-cache-no)
771 (dolist (article articles)
772 (let ((mark (gnus-summary-article-mark article)))
773 (unless (or (memq mark mark-cache-yes)
774 (memq mark mark-cache-no))
775 (if (funcall mark-check
778 (push mark mark-cache-yes)
779 (push mark mark-cache-no)))
780 (when (memq mark mark-cache-yes)
781 (push article alist))))
786 ;;{{{ backend installation functions and procedures
788 (defun spam-install-backend-super (backend &rest properties)
789 "Install BACKEND for spam.el.
790 Accepts incoming CHECK, ham registration function HRF, spam
791 registration function SRF, ham unregistration function HUF, spam
792 unregistration function SUF, and an indication whether the
793 backend is STATISTICAL."
795 (setq spam-backends (add-to-list 'spam-backends backend))
797 (let ((property (pop properties))
798 (value (pop properties)))
799 (if (spam-backend-property-valid-p property)
800 (put backend property value)
803 "spam-install-backend-super got an invalid property %s"
806 (defun spam-backend-list (&optional type)
807 "Return a list of all the backend symbols, constrained by TYPE.
808 When TYPE is 'non-mover, only non-mover backends are returned.
809 When TYPE is 'mover, only mover backends are returned."
811 (dolist (backend spam-backends)
813 (null type) ;either no type was requested
814 ;; or the type is 'mover and the backend is a mover
817 (spam-backend-mover-p backend))
818 ;; or the type is 'non-mover and the backend is not a mover
821 (not (spam-backend-mover-p backend))))
822 (push backend list)))
825 (defun spam-backend-check (backend)
826 "Get the check function for BACKEND.
827 Each individual check may return nil, t, or a mailgroup name.
828 The value nil means that the check does not yield a decision, and
829 so, that further checks are needed. The value t means that the
830 message is definitely not spam, and that further spam checks
831 should be inhibited. Otherwise, a mailgroup name or the symbol
832 'spam (depending on spam-split-symbolic-return) is returned where
833 the mail should go, and further checks are also inhibited. The
834 usual mailgroup name is the value of `spam-split-group', meaning
835 that the message is definitely a spam."
836 (get backend 'check))
838 (defun spam-backend-valid-p (backend)
840 (member backend (spam-backend-list)))
842 (defun spam-backend-info (backend)
843 "Return information about BACKEND."
844 (if (spam-backend-valid-p backend)
846 (setq info (format "Backend %s has the following properties:\n"
848 (dolist (property (spam-backend-properties))
849 (setq info (format "%s%s=%s\n"
852 (get backend property))))
854 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
857 (defun spam-backend-function (backend classification type)
858 "Get the BACKEND function for CLASSIFICATION and TYPE.
859 TYPE is 'registration or 'unregistration.
860 CLASSIFICATION is 'ham or 'spam."
862 (spam-classification-valid-p classification)
863 (spam-backend-function-type-valid-p type))
866 (format "spam-backend-%s-%s-function"
869 (funcall retrieval backend))
872 "%s was passed invalid backend %s, classification %s, or type %s"
873 "spam-backend-function"
878 (defun spam-backend-ham-registration-function (backend)
879 "Get the ham registration function for BACKEND."
882 (defun spam-backend-spam-registration-function (backend)
883 "Get the spam registration function for BACKEND."
886 (defun spam-backend-ham-unregistration-function (backend)
887 "Get the ham unregistration function for BACKEND."
890 (defun spam-backend-spam-unregistration-function (backend)
891 "Get the spam unregistration function for BACKEND."
894 (defun spam-backend-statistical-p (backend)
895 "Is BACKEND statistical?"
896 (get backend 'statistical))
898 (defun spam-backend-mover-p (backend)
899 "Is BACKEND a mover?"
900 (get backend 'mover))
902 (defun spam-install-backend-alias (backend alias)
903 "Add ALIAS to an existing BACKEND.
904 The previous backend settings for ALIAS are erased."
906 ;; install alias with no properties at first
907 (spam-install-backend-super alias)
909 (dolist (property (spam-backend-properties))
910 (put alias property (get backend property))))
912 (defun spam-install-checkonly-backend (backend check)
913 "Install a BACKEND than can only CHECK for spam."
914 (spam-install-backend-super backend 'check check))
916 (defun spam-install-mover-backend (backend hrf srf huf suf)
917 "Install a BACKEND than can move articles at summary exit.
918 Accepts ham registration function HRF, spam registration function
919 SRF, ham unregistration function HUF, spam unregistration
920 function SUF. The backend has no incoming check and can't be
922 (spam-install-backend-super
924 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
926 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
927 "Install a BACKEND than has no check.
928 Accepts ham registration function HRF, spam registration function
929 SRF, ham unregistration function HUF, spam unregistration
930 function SUF. The backend has no incoming check and can't be
931 statistical (it could be, but in practice that doesn't happen)."
932 (spam-install-backend-super
934 'hrf hrf 'srf srf 'huf huf 'suf suf))
936 (defun spam-install-backend (backend check hrf srf huf suf)
938 Accepts incoming CHECK, ham registration function HRF, spam
939 registration function SRF, ham unregistration function HUF, spam
940 unregistration function SUF. The backend won't be
941 statistical (use spam-install-statistical-backend for that)."
942 (spam-install-backend-super
944 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
946 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
948 Accepts incoming CHECK, ham registration function HRF, spam
949 registration function SRF, ham unregistration function HUF, spam
950 unregistration function SUF. The backend will be
951 statistical (use spam-install-backend for non-statistical
953 (spam-install-backend-super
955 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
957 (defun spam-install-statistical-checkonly-backend (backend check)
958 "Install a statistical BACKEND than can only CHECK for spam."
959 (spam-install-backend-super
961 'check check 'statistical t))
965 ;;{{{ backend installations
966 (spam-install-checkonly-backend 'spam-use-blackholes
967 'spam-check-blackholes)
969 ;; TODO: does anyone use hashcash? We should remove it if not.
970 (spam-install-checkonly-backend 'spam-use-hashcash
971 'spam-check-hashcash)
973 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
974 'spam-check-spamassassin-headers)
976 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
977 'spam-check-bogofilter-headers)
979 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
980 'spam-check-bsfilter-headers)
982 (spam-install-checkonly-backend 'spam-use-gmane-xref
983 'spam-check-gmane-xref)
985 (spam-install-checkonly-backend 'spam-use-regex-headers
986 'spam-check-regex-headers)
988 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
989 'spam-check-regex-body)
991 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy) instead
992 (spam-install-mover-backend 'spam-use-move
993 'spam-move-ham-routine
994 'spam-move-spam-routine
998 (spam-install-nocheck-backend 'spam-use-copy
999 'spam-copy-ham-routine
1000 'spam-copy-spam-routine
1004 (spam-install-nocheck-backend 'spam-use-gmane
1006 'spam-report-gmane-register-routine
1007 ;; does Gmane support unregistration?
1011 (spam-install-nocheck-backend 'spam-use-resend
1012 'spam-report-resend-register-ham-routine
1013 'spam-report-resend-register-routine
1017 (spam-install-backend 'spam-use-BBDB
1019 'spam-BBDB-register-routine
1021 'spam-BBDB-unregister-routine
1024 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1026 (spam-install-backend 'spam-use-blacklist
1027 'spam-check-blacklist
1029 'spam-blacklist-register-routine
1031 'spam-blacklist-unregister-routine)
1033 (spam-install-backend 'spam-use-whitelist
1034 'spam-check-whitelist
1035 'spam-whitelist-register-routine
1037 'spam-whitelist-unregister-routine
1040 (spam-install-statistical-backend 'spam-use-ifile
1042 'spam-ifile-register-ham-routine
1043 'spam-ifile-register-spam-routine
1044 'spam-ifile-unregister-ham-routine
1045 'spam-ifile-unregister-spam-routine)
1047 (spam-install-statistical-backend 'spam-use-spamoracle
1048 'spam-check-spamoracle
1049 'spam-spamoracle-learn-ham
1050 'spam-spamoracle-learn-spam
1051 'spam-spamoracle-unlearn-ham
1052 'spam-spamoracle-unlearn-spam)
1054 (spam-install-statistical-backend 'spam-use-stat
1056 'spam-stat-register-ham-routine
1057 'spam-stat-register-spam-routine
1058 'spam-stat-unregister-ham-routine
1059 'spam-stat-unregister-spam-routine)
1061 (spam-install-statistical-backend 'spam-use-spamassassin
1062 'spam-check-spamassassin
1063 'spam-spamassassin-register-ham-routine
1064 'spam-spamassassin-register-spam-routine
1065 'spam-spamassassin-unregister-ham-routine
1066 'spam-spamassassin-unregister-spam-routine)
1068 (spam-install-statistical-backend 'spam-use-bogofilter
1069 'spam-check-bogofilter
1070 'spam-bogofilter-register-ham-routine
1071 'spam-bogofilter-register-spam-routine
1072 'spam-bogofilter-unregister-ham-routine
1073 'spam-bogofilter-unregister-spam-routine)
1075 (spam-install-statistical-backend 'spam-use-bsfilter
1076 'spam-check-bsfilter
1077 'spam-bsfilter-register-ham-routine
1078 'spam-bsfilter-register-spam-routine
1079 'spam-bsfilter-unregister-ham-routine
1080 'spam-bsfilter-unregister-spam-routine)
1082 (spam-install-statistical-backend 'spam-use-crm114
1084 'spam-crm114-register-ham-routine
1085 'spam-crm114-register-spam-routine
1086 ;; does CRM114 Mailfilter support unregistration?
1092 ;;{{{ scoring and summary formatting
1093 (defun spam-necessary-extra-headers ()
1094 "Return the extra headers spam.el thinks are necessary."
1096 (when (or spam-use-spamassassin
1097 spam-use-spamassassin-headers
1098 spam-use-regex-headers)
1099 (push 'X-Spam-Status list))
1102 (defun spam-user-format-function-S (headers)
1104 (spam-summary-score headers)))
1106 (defun spam-article-sort-by-spam-status (h1 h2)
1107 "Sort articles by score."
1109 (dolist (header (spam-necessary-extra-headers))
1110 (let ((s1 (spam-summary-score h1 header))
1111 (s2 (spam-summary-score h2 header)))
1113 (setq result (< s1 s2))
1117 (defun spam-extra-header-to-number (header headers)
1118 "Transform an extra header to a number."
1119 (if (gnus-extra-header header headers)
1121 ((eq header 'X-Spam-Status)
1122 (string-to-number (gnus-replace-in-string
1123 (gnus-extra-header header headers)
1125 ;; for CRM checking, it's probably faster to just do the string match
1126 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
1127 (match-string 1 header))
1131 (defun spam-summary-score (headers &optional specific-header)
1132 "Score an article for the summary buffer, as fast as possible.
1133 With SPECIFIC-HEADER, returns only that header's score.
1134 Will not return a nil score."
1138 (list specific-header)
1139 (spam-necessary-extra-headers)))
1141 (spam-extra-header-to-number header headers))
1146 (defun spam-generic-score (&optional recheck)
1147 "Invoke whatever scoring method we can."
1150 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1151 (spam-spamassassin-score recheck))
1152 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1153 (spam-bsfilter-score recheck))
1155 (spam-crm114-score))
1156 (t (spam-bogofilter-score recheck))))
1159 ;;{{{ set up widening, processor checks
1161 ;;; set up IMAP widening if it's necessary
1162 (defun spam-setup-widening ()
1163 (when (spam-widening-needed-p)
1164 (setq nnimap-split-download-body-default t)))
1166 (defun spam-widening-needed-p (&optional force-symbols)
1168 (dolist (backend (spam-backend-list))
1169 (when (and (spam-backend-statistical-p backend)
1170 (or (symbol-value backend)
1171 (memq backend force-symbols)))
1172 (setq found backend)))
1175 (defvar spam-list-of-processors
1176 ;; note the nil processors are not defined in gnus.el
1177 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1178 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1179 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1180 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1181 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1182 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1183 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1184 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1185 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1186 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1187 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1188 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1189 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1190 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1191 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1192 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1193 "The OBSOLETE `spam-list-of-processors' list.
1194 This list contains pairs associating the obsolete ham/spam exit
1195 processor variables with a classification and a spam-use-*
1196 variable. When the processor variable is nil, just the
1197 classification and spam-use-* check variable are used. This is
1198 superceded by the new spam backend code, so it's only consulted
1199 for backwards compatibility.")
1201 (defun spam-group-processor-p (group backend &optional classification)
1202 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1203 Also accepts the obsolete processors, which can be found in
1204 gnus.el and in spam-list-of-processors. In the case of mover
1205 backends, checks the setting of spam-summary-exit-behavior in
1206 addition to the set values for the group."
1207 (if (and (stringp group)
1209 (let ((old-style (assq backend spam-list-of-processors))
1210 (parameters (nth 0 (gnus-parameter-spam-process group)))
1212 (if old-style ; old-style processor
1213 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1214 ;; now search for the parameter
1215 (dolist (parameter parameters)
1216 (when (and (null found)
1218 (eq classification (nth 0 parameter))
1219 (eq backend (nth 1 parameter)))
1222 ;; now, if the parameter was not found, do the
1223 ;; spam-summary-exit-behavior-logic for mover backends
1225 (when (spam-backend-mover-p backend)
1229 ((eq spam-summary-exit-behavior 'move-all) t)
1230 ((eq spam-summary-exit-behavior 'move-none) nil)
1231 ((eq spam-summary-exit-behavior 'default)
1232 (or (eq classification 'spam) ;move spam out of all groups
1233 ;; move ham out of spam groups
1234 (and (eq classification 'ham)
1235 (spam-group-spam-contents-p group))))
1236 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1237 spam-summary-exit-behavior))))))
1244 ;;{{{ Summary entry and exit processing.
1246 (defun spam-mark-junk-as-spam-routine ()
1247 ;; check the global list of group names spam-junk-mailgroups and the
1249 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1250 (gnus-message 6 "Marking %s articles as spam"
1251 (if spam-mark-only-unseen-as-spam
1254 (let ((articles (if spam-mark-only-unseen-as-spam
1255 gnus-newsgroup-unseen
1256 gnus-newsgroup-unreads)))
1257 (if spam-mark-new-messages-in-spam-group-as-spam
1258 (dolist (article articles)
1259 (gnus-summary-mark-article article gnus-spam-mark))
1260 (gnus-message 9 "Did not mark new messages as spam.")))))
1262 (defun spam-summary-prepare ()
1263 (setq spam-old-articles
1264 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1265 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1266 (spam-mark-junk-as-spam-routine))
1268 ;; The spam processors are invoked for any group, spam or ham or neither
1269 (defun spam-summary-prepare-exit ()
1270 (unless gnus-group-is-exiting-without-update-p
1271 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1273 ;; first of all, unregister any articles that are no longer ham or spam
1274 ;; we have to iterate over the processors, or else we'll be too slow
1275 (dolist (classification (spam-classifications))
1276 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1277 (new-articles (spam-list-articles
1278 gnus-newsgroup-articles
1280 (changed-articles (spam-set-difference new-articles old-articles)))
1281 ;; now that we have the changed articles, we go through the processors
1282 (dolist (backend (spam-backend-list))
1283 (let (unregister-list)
1284 (dolist (article changed-articles)
1285 (let ((id (spam-fetch-field-message-id-fast article)))
1286 (when (spam-log-unregistration-needed-p
1287 id 'process classification backend)
1288 (push article unregister-list))))
1289 ;; call spam-register-routine with specific articles to unregister,
1290 ;; when there are articles to unregister and the check is enabled
1291 (when (and unregister-list (symbol-value backend))
1292 (spam-unregister-routine
1295 unregister-list))))))
1297 ;; do the non-moving backends first, then the moving ones
1298 (dolist (backend-type '(non-mover mover))
1299 (dolist (classification '(spam ham))
1300 (dolist (backend (spam-backend-list backend-type))
1301 (when (spam-group-processor-p
1305 (let ((num (spam-register-routine classification backend)))
1309 "%d %s messages were processed by backend %s."
1314 ;; we mark all the leftover spam articles as expired at the end
1315 (dolist (article (spam-list-articles
1316 gnus-newsgroup-articles
1318 (gnus-summary-mark-article article gnus-expirable-mark)))
1320 (setq spam-old-articles nil))
1324 ;;{{{ spam-use-move and spam-use-copy backend support functions
1326 (defun spam-copy-or-move-routine (copy groups articles classification)
1328 (when (and (car-safe groups) (listp (car-safe groups)))
1329 (setq groups (pop groups)))
1331 (unless (listp groups)
1332 (setq groups (list groups)))
1334 ;; remove the current process mark
1335 (gnus-summary-kill-process-mark)
1337 (let ((backend-supports-deletions
1338 (gnus-check-backend-function
1339 'request-move-article gnus-newsgroup-name))
1340 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1341 article mark deletep respool)
1343 (when (member 'respool groups)
1344 (setq respool t) ; boolean for later
1345 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1347 ;; now do the actual move
1348 (dolist (group groups)
1349 (when (and articles (stringp group))
1351 ;; first, mark the article with the process mark and, if needed,
1352 ;; the unread or expired mark (for ham and spam respectively)
1353 (dolist (article articles)
1354 (when (and (eq classification 'ham)
1355 spam-mark-ham-unread-before-move-from-spam-group)
1356 (gnus-message 9 "Marking ham article %d unread before move"
1358 (gnus-summary-mark-article article gnus-unread-mark))
1359 (when (and (eq classification 'spam)
1361 (gnus-message 9 "Marking spam article %d expirable before move"
1363 (gnus-summary-mark-article article gnus-expirable-mark))
1364 (gnus-summary-set-process-mark article)
1366 (if respool ; respooling is with a "fake" group
1367 (let ((spam-split-disabled
1368 (or spam-split-disabled
1369 (and (eq classification 'ham)
1370 spam-disable-spam-split-during-ham-respool))))
1371 (gnus-message 9 "Respooling article %d with method %s"
1372 article respool-method)
1373 (gnus-summary-respool-article nil respool-method))
1374 (if (or (not backend-supports-deletions) ; else, we are not respooling
1375 (> (length groups) 1))
1376 (progn ; if copying, copy and set deletep
1377 (gnus-message 9 "Copying article %d to group %s"
1379 (gnus-summary-copy-article nil group)
1381 (gnus-message 9 "Moving article %d to group %s"
1383 (gnus-summary-move-article nil group))))) ; else move articles
1385 ;; now delete the articles, unless a) copy is t, and there was a copy done
1386 ;; b) a move was done to a single group
1387 ;; c) backend-supports-deletions is nil
1389 (when (and deletep backend-supports-deletions)
1390 (dolist (article articles)
1391 (gnus-summary-set-process-mark article)
1392 (gnus-message 9 "Deleting article %d" article))
1394 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1395 (gnus-summary-delete-article nil)))))
1397 (gnus-summary-yank-process-mark)
1398 (length articles))))
1400 (defun spam-copy-spam-routine (articles)
1401 (spam-copy-or-move-routine
1403 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1407 (defun spam-move-spam-routine (articles)
1408 (spam-copy-or-move-routine
1410 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1414 (defun spam-copy-ham-routine (articles)
1415 (spam-copy-or-move-routine
1417 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1421 (defun spam-move-ham-routine (articles)
1422 (spam-copy-or-move-routine
1424 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1430 ;;{{{ article and field retrieval code
1431 (defun spam-get-article-as-string (article)
1432 (when (numberp article)
1434 (gnus-request-article-this-buffer
1436 gnus-newsgroup-name)
1440 ;; (defun spam-get-article-as-filename (article)
1441 ;; (let ((article-filename))
1442 ;; (when (numberp article)
1443 ;; (nnml-possibly-change-directory
1444 ;; (gnus-group-real-name gnus-newsgroup-name))
1445 ;; (setq article-filename (expand-file-name
1446 ;; (int-to-string article) nnml-current-directory)))
1447 ;; (if (file-exists-p article-filename)
1451 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1452 "Fetch a FIELD for ARTICLE quickly, using the internal gnus-data-list function.
1453 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1454 When FIELD is 'number, ARTICLE can be any number (since we want
1456 (when (numberp article)
1457 (let* ((data-header (or prepared-data-header
1458 (spam-fetch-article-header article))))
1459 (if (arrayp data-header)
1461 ((equal field 'number)
1462 (mail-header-number data-header))
1463 ((equal field 'from)
1464 (mail-header-from data-header))
1465 ((equal field 'message-id)
1466 (mail-header-message-id data-header))
1467 ((equal field 'subject)
1468 (mail-header-subject data-header))
1469 ((equal field 'references)
1470 (mail-header-references data-header))
1471 ((equal field 'date)
1472 (mail-header-date data-header))
1473 ((equal field 'xref)
1474 (mail-header-xref data-header))
1475 ((equal field 'extra)
1476 (mail-header-extra data-header))
1480 "spam-fetch-field-fast: unknown field %s requested"
1483 (gnus-message 6 "Article %d has a nil data header" article)))))
1485 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1486 (spam-fetch-field-fast article 'from prepared-data-header))
1488 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1489 (spam-fetch-field-fast article 'subject prepared-data-header))
1491 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1492 (spam-fetch-field-fast article 'message-id prepared-data-header))
1494 (defun spam-generate-fake-headers (article)
1495 (let ((dh (spam-fetch-article-header article)))
1499 ;; 80-character limit makes for strange constructs
1500 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1501 "Date: %s\nReferences: %s\nXref: %s\n")
1502 (spam-fetch-field-fast article 'from dh)
1503 (spam-fetch-field-fast article 'subject dh)
1504 (spam-fetch-field-fast article 'message-id dh)
1505 (spam-fetch-field-fast article 'date dh)
1506 (spam-fetch-field-fast article 'references dh)
1507 (spam-fetch-field-fast article 'xref dh))
1508 (when (spam-fetch-field-fast article 'extra dh)
1509 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1512 "spam-generate-fake-headers: article %d didn't have a valid header"
1515 (defun spam-fetch-article-header (article)
1517 (set-buffer gnus-summary-buffer)
1518 (gnus-read-header article)
1519 (nth 3 (assq article gnus-newsgroup-data))))
1522 ;;{{{ Spam determination.
1524 (defun spam-split (&rest specific-checks)
1525 "Split this message into the `spam' group if it is spam.
1526 This function can be used as an entry in the variable `nnmail-split-fancy',
1527 for example like this: (: spam-split). It can take checks as
1528 parameters. A string as a parameter will set the
1529 spam-split-group to that string.
1531 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1533 (setq spam-split-last-successful-check nil)
1534 (unless spam-split-disabled
1535 (let ((spam-split-group-choice spam-split-group))
1536 (dolist (check specific-checks)
1537 (when (stringp check)
1538 (setq spam-split-group-choice check)
1539 (setq specific-checks (delq check specific-checks))))
1541 (let ((spam-split-group spam-split-group-choice)
1542 (widening-needed-check (spam-widening-needed-p specific-checks)))
1545 (when widening-needed-check
1547 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1548 widening-needed-check))
1549 (let ((backends (spam-backend-list))
1551 (while (and backends (not decision))
1552 (let* ((backend (pop backends))
1553 (check-function (spam-backend-check backend))
1554 (spam-split-group (if spam-split-symbolic-return
1558 ;; either, given specific checks, this is one of them
1559 (memq backend specific-checks)
1560 ;; or, given no specific checks, spam-use-CHECK is set
1561 (and (null specific-checks) (symbol-value backend)))
1562 (gnus-message 6 "spam-split: calling the %s function"
1564 (setq decision (funcall check-function))
1565 ;; if we got a decision at all, save the current check
1567 (setq spam-split-last-successful-check backend))
1569 (when (eq decision 'spam)
1570 (unless spam-split-symbolic-return
1573 (format "spam-split got %s but %s is nil"
1575 spam-split-symbolic-return)))))))
1577 (if spam-split-symbolic-return-positive 'ham nil)
1580 (defun spam-find-spam ()
1581 "This function will detect spam in the current newsgroup using spam-split."
1584 (let* ((group gnus-newsgroup-name)
1585 (autodetect (gnus-parameter-spam-autodetect group))
1586 (methods (gnus-parameter-spam-autodetect-methods group))
1587 (first-method (nth 0 methods))
1588 (articles (if spam-autodetect-recheck-messages
1589 gnus-newsgroup-articles
1590 gnus-newsgroup-unseen))
1591 article-cannot-be-faked)
1594 (dolist (backend methods)
1595 (when (spam-backend-statistical-p backend)
1596 (setq article-cannot-be-faked t)
1599 (when (memq 'default methods)
1600 (setq article-cannot-be-faked t))
1602 (when (and autodetect
1603 (not (equal first-method 'none)))
1606 (let ((id (spam-fetch-field-message-id-fast article))
1607 (subject (spam-fetch-field-subject-fast article))
1608 (sender (spam-fetch-field-from-fast article))
1612 (gnus-message 6 "Article %d has no message ID!" article))
1614 (when (and id spam-log-to-registry)
1615 (setq registry-lookup (spam-log-registration-type id 'incoming))
1616 (when registry-lookup
1619 "spam-find-spam: message %s was already registered incoming"
1622 (let* ((spam-split-symbolic-return t)
1623 (spam-split-symbolic-return-positive t)
1624 (fake-headers (spam-generate-fake-headers article))
1628 (if article-cannot-be-faked
1629 (gnus-request-article-this-buffer
1632 ;; else, we fake the article
1633 (when fake-headers (insert fake-headers)))
1634 (if (or (null first-method)
1635 (equal first-method 'default))
1637 (apply 'spam-split methods))))))
1638 (if (equal split-return 'spam)
1639 (gnus-summary-mark-article article gnus-spam-mark))
1641 (when (and id split-return spam-log-to-registry)
1642 (when (zerop (gnus-registry-group-count id))
1643 (gnus-registry-add-group
1644 id group subject sender))
1646 (unless registry-lookup
1647 (spam-log-processing-to-registry
1651 spam-split-last-successful-check
1657 ;;{{{ registration/unregistration functions
1659 (defun spam-unregister-routine (classification
1661 &optional specific-articles)
1662 (spam-register-routine classification backend t specific-articles))
1664 (defun spam-register-routine (classification
1666 &optional unregister
1668 (when (and (spam-classification-valid-p classification)
1669 (spam-backend-valid-p backend))
1670 (let* ((register-function
1671 (spam-backend-function backend classification 'registration))
1672 (unregister-function
1673 (spam-backend-function backend classification 'unregistration))
1674 (run-function (if unregister
1677 (log-function (if unregister
1678 'spam-log-undo-registration
1679 'spam-log-processing-to-registry))
1683 ;; make list of articles, using specific-articles if given
1684 (setq articles (or specific-articles
1686 gnus-newsgroup-articles
1689 (when (> (length articles) 0)
1690 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1691 (if unregister "Unregistering" "Registering")
1693 (if specific-articles "specific" "")
1696 (funcall run-function articles)
1697 ;; now log all the registrations (or undo them, depending on unregister)
1698 (dolist (article articles)
1699 (funcall log-function
1700 (spam-fetch-field-message-id-fast article)
1704 gnus-newsgroup-name))))
1705 (length articles)))) ;return the number of articles processed
1707 ;;; log a ham- or spam-processor invocation to the registry
1708 (defun spam-log-processing-to-registry (id type classification backend group)
1709 (when spam-log-to-registry
1710 (if (and (stringp id)
1712 (spam-process-type-valid-p type)
1713 (spam-classification-valid-p classification)
1714 (spam-backend-valid-p backend))
1715 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1716 (cell (list classification backend group)))
1717 (push cell cell-list)
1718 (gnus-registry-store-extra-entry
1725 (format "%s call with bad ID, type, classification, spam-backend, or group"
1726 "spam-log-processing-to-registry")))))
1728 ;;; check if a ham- or spam-processor registration has been done
1729 (defun spam-log-registered-p (id type)
1730 (when spam-log-to-registry
1731 (if (and (stringp id)
1732 (spam-process-type-valid-p type))
1733 (cdr-safe (gnus-registry-fetch-extra id type))
1737 (format "%s called with bad ID, type, classification, or spam-backend"
1738 "spam-log-registered-p"))
1741 ;;; check what a ham- or spam-processor registration says
1742 ;;; returns nil if conflicting registrations are found
1743 (defun spam-log-registration-type (id type)
1746 (dolist (reg (spam-log-registered-p id type))
1747 (let ((classification (nth 0 reg)))
1748 (when (spam-classification-valid-p classification)
1750 (not (eq classification decision)))
1751 (setq count (+ 1 count)))
1752 (setq decision classification))))
1758 ;;; check if a ham- or spam-processor registration needs to be undone
1759 (defun spam-log-unregistration-needed-p (id type classification backend)
1760 (when spam-log-to-registry
1761 (if (and (stringp id)
1762 (spam-process-type-valid-p type)
1763 (spam-classification-valid-p classification)
1764 (spam-backend-valid-p backend))
1765 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1767 (dolist (cell cell-list)
1769 (when (and (eq classification (nth 0 cell))
1770 (eq backend (nth 1 cell)))
1776 (format "%s called with bad ID, type, classification, or spam-backend"
1777 "spam-log-unregistration-needed-p"))
1781 ;;; undo a ham- or spam-processor registration (the group is not used)
1782 (defun spam-log-undo-registration (id type classification backend &optional group)
1783 (when (and spam-log-to-registry
1784 (spam-log-unregistration-needed-p id type classification backend))
1785 (if (and (stringp id)
1786 (spam-process-type-valid-p type)
1787 (spam-classification-valid-p classification)
1788 (spam-backend-valid-p backend))
1789 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1790 new-cell-list found)
1791 (dolist (cell cell-list)
1792 (unless (and (eq classification (nth 0 cell))
1793 (eq backend (nth 1 cell)))
1794 (push cell new-cell-list)))
1795 (gnus-registry-store-extra-entry
1800 (gnus-error 7 (format "%s call with bad ID, type, spam-backend, or group"
1801 "spam-log-undo-registration"))
1806 ;;{{{ backend functions
1809 (defun spam-check-gmane-xref ()
1811 (message-fetch-field "Xref")
1812 (message-fetch-field "Newsgroups"))))
1813 (when header ; return nil when no header
1814 (when (string-match spam-gmane-xref-spam-group
1816 spam-split-group))))
1822 (defun spam-check-regex-body ()
1823 (let ((spam-regex-headers-ham spam-regex-body-ham)
1824 (spam-regex-headers-spam spam-regex-body-spam))
1825 (spam-check-regex-headers t)))
1831 (defun spam-check-regex-headers (&optional body)
1832 (let ((type (if body "body" "header"))
1834 (dolist (h-regex spam-regex-headers-ham)
1836 (goto-char (point-min))
1837 (when (re-search-forward h-regex nil t)
1838 (message "Ham regex %s search positive." type)
1840 (dolist (s-regex spam-regex-headers-spam)
1842 (goto-char (point-min))
1843 (when (re-search-forward s-regex nil t)
1844 (message "Spam regex %s search positive." type)
1846 (setq ret spam-split-group))))
1853 (defun spam-reverse-ip-string (ip)
1855 (mapconcat 'identity
1856 (nreverse (split-string ip "\\."))
1859 (defun spam-check-blackholes ()
1860 "Check the Received headers for blackholed relays."
1861 (let ((headers (message-fetch-field "received"))
1866 (goto-char (point-min))
1867 (gnus-message 6 "Checking headers for relay addresses")
1868 (while (re-search-forward
1869 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1870 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1871 (push (spam-reverse-ip-string (match-string 1))
1873 (dolist (server spam-blackhole-servers)
1875 (unless (and spam-blackhole-good-server-regex
1876 ;; match the good-server-regex against the reversed (again) IP string
1878 spam-blackhole-good-server-regex
1879 (spam-reverse-ip-string ip)))
1881 (let ((query-string (concat ip "." server)))
1883 (let ((query-result (query-dig query-string)))
1885 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1887 (push (list ip server query-result)
1889 ;; else, if not using dig.el
1890 (when (query-dns query-string)
1891 (gnus-message 6 "positive blackhole check")
1892 (push (list ip server (query-dns query-string 'TXT))
1904 (defun spam-check-hashcash ()
1905 "Check the headers for hashcash payments."
1906 (mail-check-payment))) ;mail-check-payment returns a boolean
1909 (defalias 'mail-check-payment 'ignore)
1910 (defalias 'spam-check-hashcash 'ignore))))
1915 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1916 ;;; <sacha@giotto.sj.ru>
1918 ;; all this is done inside a condition-case to trap errors
1925 ;; when the BBDB changes, we want to clear out our cache
1926 (defun spam-clear-cache-BBDB (&rest immaterial)
1927 (spam-clear-cache 'spam-use-BBDB))
1929 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1931 (defun spam-enter-ham-BBDB (addresses &optional remove)
1932 "Enter an address into the BBDB; implies ham (non-spam) sender"
1933 (dolist (from addresses)
1934 (when (stringp from)
1935 (let* ((parsed-address (gnus-extract-address-components from))
1936 (name (or (nth 0 parsed-address) "Ham Sender"))
1937 (remove-function (if remove
1938 'bbdb-delete-record-internal
1940 (net-address (nth 1 parsed-address))
1941 (record (and net-address
1942 (bbdb-search-simple nil net-address))))
1944 (gnus-message 6 "%s address %s %s BBDB"
1945 (if remove "Deleting" "Adding")
1947 (if remove "from" "to"))
1949 (funcall remove-function record)
1950 (bbdb-create-internal name nil net-address nil nil
1951 "ham sender added by spam.el")))))))
1953 (defun spam-BBDB-register-routine (articles &optional unregister)
1955 (dolist (article articles)
1956 (when (stringp (spam-fetch-field-from-fast article))
1957 (push (spam-fetch-field-from-fast article) addresses)))
1958 ;; now do the register/unregister action
1959 (spam-enter-ham-BBDB addresses unregister)))
1961 (defun spam-BBDB-unregister-routine (articles)
1962 (spam-BBDB-register-routine articles t))
1964 (defun spam-check-BBDB ()
1965 "Mail from people in the BBDB is classified as ham or non-spam"
1966 (let ((who (message-fetch-field "from"))
1967 bbdb-cache bbdb-hashtable)
1968 (when spam-cache-lookups
1969 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1972 ;; this is the expanded (bbdb-hashtable) macro
1973 ;; without the debugging support
1974 (with-current-buffer (bbdb-buffer)
1976 (save-window-excursion
1977 (bbdb-records nil t)
1979 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1981 (setq who (nth 1 (gnus-extract-address-components who)))
1983 (if spam-cache-lookups
1985 (intern-soft who bbdb-cache))
1986 (bbdb-search-simple nil who))
1988 (if spam-use-BBDB-exclusive
1993 (defalias 'bbdb-search-simple 'ignore)
1994 (defalias 'bbdb-records 'ignore)
1995 (defalias 'bbdb-buffer 'ignore)
1996 (defalias 'spam-check-BBDB 'ignore)
1997 (defalias 'spam-BBDB-register-routine 'ignore)
1998 (defalias 'spam-enter-ham-BBDB 'ignore)
1999 (defalias 'bbdb-create-internal 'ignore)
2000 (defalias 'bbdb-delete-record-internal 'ignore)
2001 (defalias 'bbdb-records 'ignore))))
2007 ;;; check the ifile backend; return nil if the mail was NOT classified
2010 (defun spam-get-ifile-database-parameter ()
2011 "Get the command-line parameter for ifile's database from
2012 spam-ifile-database-path."
2013 (if spam-ifile-database-path
2014 (format "--db-file=%s" spam-ifile-database-path)
2017 (defun spam-check-ifile ()
2018 "Check the ifile backend for the classification of this message."
2019 (let ((article-buffer-name (buffer-name))
2022 (let ((temp-buffer-name (buffer-name))
2023 (db-param (spam-get-ifile-database-parameter)))
2025 (set-buffer article-buffer-name)
2026 (apply 'call-process-region
2027 (point-min) (point-max) spam-ifile-path
2028 nil temp-buffer-name nil "-c"
2029 (if db-param `(,db-param "-q") `("-q"))))
2030 ;; check the return now (we're back in the temp buffer)
2031 (goto-char (point-min))
2033 (setq category (buffer-substring (point) (point-at-eol))))
2034 (when (not (zerop (length category))) ; we need a category here
2035 (if spam-ifile-all-categories
2036 (setq return category)
2037 ;; else, if spam-ifile-all-categories is not set...
2038 (when (string-equal spam-ifile-spam-category category)
2039 (setq return spam-split-group)))))) ; note return is nil otherwise
2042 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2043 "Register an article, given as a string, with a category.
2044 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2045 (let ((category (or category gnus-newsgroup-name))
2046 (add-or-delete-option (if unregister "-d" "-i"))
2047 (db (spam-get-ifile-database-parameter))
2050 (dolist (article articles)
2051 (let ((article-string (spam-get-article-as-string article)))
2052 (when (stringp article-string)
2053 (insert article-string))))
2054 (apply 'call-process-region
2055 (point-min) (point-max) spam-ifile-path
2057 add-or-delete-option category
2058 (if db `(,db "-h") `("-h"))))))
2060 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2061 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2063 (defun spam-ifile-unregister-spam-routine (articles)
2064 (spam-ifile-register-spam-routine articles t))
2066 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2067 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2069 (defun spam-ifile-unregister-ham-routine (articles)
2070 (spam-ifile-register-ham-routine articles t))
2078 (let ((spam-stat-install-hooks nil))
2079 (require 'spam-stat))
2081 (defun spam-check-stat ()
2082 "Check the spam-stat backend for the classification of this message"
2083 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2084 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2086 (spam-stat-split-fancy)))
2088 (defun spam-stat-register-spam-routine (articles &optional unregister)
2089 (dolist (article articles)
2090 (let ((article-string (spam-get-article-as-string article)))
2092 (insert article-string)
2094 (spam-stat-buffer-change-to-non-spam)
2095 (spam-stat-buffer-is-spam))))))
2097 (defun spam-stat-unregister-spam-routine (articles)
2098 (spam-stat-register-spam-routine articles t))
2100 (defun spam-stat-register-ham-routine (articles &optional unregister)
2101 (dolist (article articles)
2102 (let ((article-string (spam-get-article-as-string article)))
2104 (insert article-string)
2106 (spam-stat-buffer-change-to-spam)
2107 (spam-stat-buffer-is-non-spam))))))
2109 (defun spam-stat-unregister-ham-routine (articles)
2110 (spam-stat-register-ham-routine articles t))
2112 (defun spam-maybe-spam-stat-load ()
2113 (when spam-use-stat (spam-stat-load)))
2115 (defun spam-maybe-spam-stat-save ()
2116 (when spam-use-stat (spam-stat-save))))
2119 (defalias 'spam-stat-load 'ignore)
2120 (defalias 'spam-stat-save 'ignore)
2121 (defalias 'spam-maybe-spam-stat-load 'ignore)
2122 (defalias 'spam-maybe-spam-stat-save 'ignore)
2123 (defalias 'spam-stat-register-ham-routine 'ignore)
2124 (defalias 'spam-stat-unregister-ham-routine 'ignore)
2125 (defalias 'spam-stat-register-spam-routine 'ignore)
2126 (defalias 'spam-stat-unregister-spam-routine 'ignore)
2127 (defalias 'spam-stat-buffer-is-spam 'ignore)
2128 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
2129 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
2130 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
2131 (defalias 'spam-stat-split-fancy 'ignore)
2132 (defalias 'spam-check-stat 'ignore))))
2138 ;;{{{ Blacklists and whitelists.
2140 (defvar spam-whitelist-cache nil)
2141 (defvar spam-blacklist-cache nil)
2143 (defun spam-kill-whole-line ()
2145 (let ((kill-whole-line t))
2148 ;;; address can be a list, too
2149 (defun spam-enter-whitelist (address &optional remove)
2150 "Enter ADDRESS (list or single) into the whitelist.
2151 With a non-nil REMOVE, remove them."
2152 (interactive "sAddress: ")
2153 (spam-enter-list address spam-whitelist remove)
2154 (setq spam-whitelist-cache nil)
2155 (spam-clear-cache 'spam-use-whitelist))
2157 ;;; address can be a list, too
2158 (defun spam-enter-blacklist (address &optional remove)
2159 "Enter ADDRESS (list or single) into the blacklist.
2160 With a non-nil REMOVE, remove them."
2161 (interactive "sAddress: ")
2162 (spam-enter-list address spam-blacklist remove)
2163 (setq spam-blacklist-cache nil)
2164 (spam-clear-cache 'spam-use-whitelist))
2166 (defun spam-enter-list (addresses file &optional remove)
2167 "Enter ADDRESSES into the given FILE.
2168 Either the whitelist or the blacklist files can be used. With
2169 REMOVE not nil, remove the ADDRESSES."
2170 (if (stringp addresses)
2171 (spam-enter-list (list addresses) file remove)
2172 ;; else, we have a list of addresses here
2173 (unless (file-exists-p (file-name-directory file))
2174 (make-directory (file-name-directory file) t))
2177 (find-file-noselect file))
2178 (dolist (a addresses)
2180 (goto-char (point-min))
2181 (if (re-search-forward (regexp-quote a) nil t)
2182 ;; found the address
2184 (spam-kill-whole-line))
2185 ;; else, the address was not found
2187 (goto-char (point-max))
2193 (defun spam-filelist-build-cache (type)
2194 (let ((cache (if (eq type 'spam-use-blacklist)
2195 spam-blacklist-cache
2196 spam-whitelist-cache))
2198 (unless (gethash type spam-caches)
2200 (let ((address (pop cache)))
2201 (unless (zerop (length address)) ; 0 for a nil address too
2202 (setq address (regexp-quote address))
2203 ;; fix regexp-quote's treatment of user-intended regexes
2204 (while (string-match "\\\\\\*" address)
2205 (setq address (replace-match ".*" t t address))))
2206 (push address parsed-cache)))
2207 (puthash type parsed-cache spam-caches))))
2209 (defun spam-filelist-check-cache (type from)
2210 (when (stringp from)
2211 (spam-filelist-build-cache type)
2213 (dolist (address (gethash type spam-caches))
2214 (when (and address (string-match address from))
2219 ;;; returns t if the sender is in the whitelist, nil or
2220 ;;; spam-split-group otherwise
2221 (defun spam-check-whitelist ()
2222 ;; FIXME! Should it detect when file timestamps change?
2223 (unless spam-whitelist-cache
2224 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2225 (if (spam-from-listed-p 'spam-use-whitelist)
2227 (if spam-use-whitelist-exclusive
2231 (defun spam-check-blacklist ()
2232 ;; FIXME! Should it detect when file timestamps change?
2233 (unless spam-blacklist-cache
2234 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2235 (and (spam-from-listed-p 'spam-use-blacklist)
2238 (defun spam-parse-list (file)
2239 (when (file-readable-p file)
2240 (let (contents address)
2242 (insert-file-contents file)
2244 (setq address (buffer-substring (point) (point-at-eol)))
2246 ;; insert the e-mail address if detected, otherwise the raw data
2247 (unless (zerop (length address))
2248 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2249 (push (or pure-address address) contents)))))
2250 (nreverse contents))))
2252 (defun spam-from-listed-p (type)
2253 (let ((from (message-fetch-field "from"))
2255 (spam-filelist-check-cache type from)))
2257 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2258 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2259 (declassification (if blacklist 'ham 'spam))
2261 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2263 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2264 from addresses unregister-list article-unregister-list)
2265 (dolist (article articles)
2266 (let ((from (spam-fetch-field-from-fast article))
2267 (id (spam-fetch-field-message-id-fast article))
2269 (when (stringp from)
2270 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2271 (when (and (not sender-ignored)
2272 (stringp ignore-regex)
2273 (string-match ignore-regex from))
2274 (setq sender-ignored t)))
2275 ;; remember the messages we need to unregister, unless remove is set
2278 (spam-log-unregistration-needed-p
2279 id 'process declassification de-symbol))
2280 (push article article-unregister-list)
2281 (push from unregister-list))
2282 (unless sender-ignored
2283 (push from addresses)))))
2286 (funcall enter-function addresses t) ; unregister all these addresses
2287 ;; else, register normally and unregister what we need to
2288 (funcall remove-function unregister-list t)
2289 (dolist (article article-unregister-list)
2290 (spam-log-undo-registration
2291 (spam-fetch-field-message-id-fast article)
2295 (funcall enter-function addresses nil))))
2297 (defun spam-blacklist-unregister-routine (articles)
2298 (spam-blacklist-register-routine articles t))
2300 (defun spam-blacklist-register-routine (articles &optional unregister)
2301 (spam-filelist-register-routine articles t unregister))
2303 (defun spam-whitelist-unregister-routine (articles)
2304 (spam-whitelist-register-routine articles t))
2306 (defun spam-whitelist-register-routine (articles &optional unregister)
2307 (spam-filelist-register-routine articles nil unregister))
2311 ;;{{{ Spam-report glue (gmane and resend reporting)
2312 (defun spam-report-gmane-register-routine (articles)
2314 (apply 'spam-report-gmane articles)))
2316 (defun spam-report-resend-register-ham-routine (articles)
2317 (spam-report-resend-register-routine articles t))
2319 (defun spam-report-resend-register-routine (articles &optional ham)
2320 (let* ((resend-to-gp
2322 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2323 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2324 (spam-report-resend-to (or (car-safe resend-to-gp)
2325 spam-report-resend-to)))
2326 (spam-report-resend articles ham)))
2331 (defun spam-check-bogofilter-headers (&optional score)
2332 (let ((header (message-fetch-field spam-bogofilter-header)))
2333 (when header ; return nil when no header
2334 (if score ; scoring mode
2335 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2336 (match-string 1 header)
2338 ;; spam detection mode
2339 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2341 spam-split-group)))))
2343 ;; return something sensible if the score can't be determined
2344 (defun spam-bogofilter-score (&optional recheck)
2345 "Get the Bogofilter spamicity score"
2347 (save-window-excursion
2348 (gnus-summary-show-article t)
2349 (set-buffer gnus-article-buffer)
2350 (let ((score (or (unless recheck
2351 (spam-check-bogofilter-headers t))
2352 (spam-check-bogofilter t))))
2353 (gnus-summary-show-article)
2354 (message "Spamicity score %s" score)
2357 (defun spam-check-bogofilter (&optional score)
2358 "Check the Bogofilter backend for the classification of this message"
2359 (let ((article-buffer-name (buffer-name))
2360 (db spam-bogofilter-database-directory)
2363 (let ((temp-buffer-name (buffer-name)))
2365 (set-buffer article-buffer-name)
2366 (apply 'call-process-region
2367 (point-min) (point-max)
2368 spam-bogofilter-path
2369 nil temp-buffer-name nil
2370 (if db `("-d" ,db "-v") `("-v"))))
2371 (setq return (spam-check-bogofilter-headers score))))
2374 (defun spam-bogofilter-register-with-bogofilter (articles
2376 &optional unregister)
2377 "Register an article, given as a string, as spam or non-spam."
2378 (dolist (article articles)
2379 (let ((article-string (spam-get-article-as-string article))
2380 (db spam-bogofilter-database-directory)
2381 (switch (if unregister
2383 spam-bogofilter-spam-strong-switch
2384 spam-bogofilter-ham-strong-switch)
2386 spam-bogofilter-spam-switch
2387 spam-bogofilter-ham-switch))))
2388 (when (stringp article-string)
2390 (insert article-string)
2392 (apply 'call-process-region
2393 (point-min) (point-max)
2394 spam-bogofilter-path
2396 (if db `("-d" ,db "-v") `("-v"))))))))
2398 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2399 (spam-bogofilter-register-with-bogofilter articles t unregister))
2401 (defun spam-bogofilter-unregister-spam-routine (articles)
2402 (spam-bogofilter-register-spam-routine articles t))
2404 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2405 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2407 (defun spam-bogofilter-unregister-ham-routine (articles)
2408 (spam-bogofilter-register-ham-routine articles t))
2414 (defun spam-check-spamoracle ()
2415 "Run spamoracle on an article to determine whether it's spam."
2416 (let ((article-buffer-name (buffer-name)))
2418 (let ((temp-buffer-name (buffer-name)))
2420 (set-buffer article-buffer-name)
2422 (apply 'call-process-region
2423 (point-min) (point-max)
2424 spam-spamoracle-binary
2425 nil temp-buffer-name nil
2426 (if spam-spamoracle-database
2427 `("-f" ,spam-spamoracle-database "mark")
2431 (set-buffer temp-buffer-name)
2432 (goto-char (point-min))
2433 (when (re-search-forward "^X-Spam: yes;" nil t)
2435 (error "Error running spamoracle: %s" status))))))))
2437 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2438 "Run spamoracle in training mode."
2440 (let ((temp-buffer-name (buffer-name)))
2442 (goto-char (point-min))
2443 (dolist (article articles)
2444 (insert (spam-get-article-as-string article)))
2445 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2449 (apply 'call-process-region
2450 (point-min) (point-max)
2451 spam-spamoracle-binary
2452 nil temp-buffer-name nil
2453 (if spam-spamoracle-database
2454 `("-f" ,spam-spamoracle-database
2457 (unless (eq 0 status)
2458 (error "Error running spamoracle: %s" status)))))))
2460 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2461 (spam-spamoracle-learn articles nil unregister))
2463 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2464 (spam-spamoracle-learn-ham articles t))
2466 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2467 (spam-spamoracle-learn articles t unregister))
2469 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2470 (spam-spamoracle-learn-spam articles t))
2475 ;;; based mostly on the bogofilter code
2476 (defun spam-check-spamassassin-headers (&optional score)
2477 "Check the SpamAssassin headers for the classification of this message."
2478 (if score ; scoring mode
2479 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2481 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2482 (match-string 1 header)
2484 ;; spam detection mode
2485 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2486 (when header ; return nil when no header
2487 (when (string-match spam-spamassassin-positive-spam-flag-header
2489 spam-split-group)))))
2491 (defun spam-check-spamassassin (&optional score)
2492 "Check the SpamAssassin backend for the classification of this message."
2493 (let ((article-buffer-name (buffer-name)))
2495 (let ((temp-buffer-name (buffer-name)))
2497 (set-buffer article-buffer-name)
2498 (apply 'call-process-region
2499 (point-min) (point-max) spam-spamassassin-path
2500 nil temp-buffer-name nil spam-spamassassin-arguments))
2501 ;; check the return now (we're back in the temp buffer)
2502 (goto-char (point-min))
2503 (spam-check-spamassassin-headers score)))))
2505 ;; return something sensible if the score can't be determined
2506 (defun spam-spamassassin-score (&optional recheck)
2507 "Get the SpamAssassin score"
2509 (save-window-excursion
2510 (gnus-summary-show-article t)
2511 (set-buffer gnus-article-buffer)
2512 (let ((score (or (unless recheck
2513 (spam-check-spamassassin-headers t))
2514 (spam-check-spamassassin t))))
2515 (gnus-summary-show-article)
2516 (message "SpamAssassin score %s" score)
2519 (defun spam-spamassassin-register-with-sa-learn (articles spam
2520 &optional unregister)
2521 "Register articles with spamassassin's sa-learn as spam or non-spam."
2523 (let ((action (if unregister spam-sa-learn-unregister-switch
2524 (if spam spam-sa-learn-spam-switch
2525 spam-sa-learn-ham-switch)))
2526 (summary-buffer-name (buffer-name)))
2528 ;; group the articles into mbox format
2529 (dolist (article articles)
2530 (let (article-string)
2532 (set-buffer summary-buffer-name)
2533 (setq article-string (spam-get-article-as-string article)))
2534 (when (stringp article-string)
2535 (insert "From \n") ; mbox separator (sa-learn only checks the
2536 ; first five chars, so we can get away with
2538 (insert article-string)
2540 ;; call sa-learn on all messages at the same time
2541 (apply 'call-process-region
2542 (point-min) (point-max)
2544 nil nil nil "--mbox"
2545 (if spam-sa-learn-rebuild
2547 `("--no-rebuild" ,action)))))))
2549 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2550 (spam-spamassassin-register-with-sa-learn articles t unregister))
2552 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2553 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2555 (defun spam-spamassassin-unregister-spam-routine (articles)
2556 (spam-spamassassin-register-with-sa-learn articles t t))
2558 (defun spam-spamassassin-unregister-ham-routine (articles)
2559 (spam-spamassassin-register-with-sa-learn articles nil t))
2564 ;;; based mostly on the bogofilter code
2565 (defun spam-check-bsfilter-headers (&optional score)
2567 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2569 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2570 (when header ; return nil when no header
2571 (when (string-match "YES" header)
2572 spam-split-group)))))
2574 ;; return something sensible if the score can't be determined
2575 (defun spam-bsfilter-score (&optional recheck)
2576 "Get the Bsfilter spamicity score"
2578 (save-window-excursion
2579 (gnus-summary-show-article t)
2580 (set-buffer gnus-article-buffer)
2581 (let ((score (or (unless recheck
2582 (spam-check-bsfilter-headers t))
2583 (spam-check-bsfilter t))))
2584 (gnus-summary-show-article)
2585 (message "Spamicity score %s" score)
2588 (defun spam-check-bsfilter (&optional score)
2589 "Check the Bsfilter backend for the classification of this message"
2590 (let ((article-buffer-name (buffer-name))
2591 (dir spam-bsfilter-database-directory)
2594 (let ((temp-buffer-name (buffer-name)))
2596 (set-buffer article-buffer-name)
2597 (apply 'call-process-region
2598 (point-min) (point-max)
2600 nil temp-buffer-name nil
2603 "--insert-probability"
2605 (list "--homedir" dir))))
2606 (setq return (spam-check-bsfilter-headers score))))
2609 (defun spam-bsfilter-register-with-bsfilter (articles
2611 &optional unregister)
2612 "Register an article, given as a string, as spam or non-spam."
2613 (dolist (article articles)
2614 (let ((article-string (spam-get-article-as-string article))
2615 (switch (if unregister
2617 spam-bsfilter-spam-strong-switch
2618 spam-bsfilter-ham-strong-switch)
2620 spam-bsfilter-spam-switch
2621 spam-bsfilter-ham-switch))))
2622 (when (stringp article-string)
2624 (insert article-string)
2625 (apply 'call-process-region
2626 (point-min) (point-max)
2630 (when spam-bsfilter-database-directory
2632 spam-bsfilter-database-directory))))))))
2634 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2635 (spam-bsfilter-register-with-bsfilter articles t unregister))
2637 (defun spam-bsfilter-unregister-spam-routine (articles)
2638 (spam-bsfilter-register-spam-routine articles t))
2640 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2641 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2643 (defun spam-bsfilter-unregister-ham-routine (articles)
2644 (spam-bsfilter-register-ham-routine articles t))
2648 ;;{{{ CRM114 Mailfilter
2649 (defun spam-check-crm114-headers (&optional score)
2650 (let ((header (message-fetch-field spam-crm114-header)))
2651 (when header ; return nil when no header
2652 (if score ; scoring mode
2653 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2654 (match-string 1 header)
2656 ;; spam detection mode
2657 (when (string-match spam-crm114-positive-spam-header
2659 spam-split-group)))))
2661 ;; return something sensible if the score can't be determined
2662 (defun spam-crm114-score ()
2663 "Get the CRM114 Mailfilter pR"
2665 (save-window-excursion
2666 (gnus-summary-show-article t)
2667 (set-buffer gnus-article-buffer)
2668 (let ((score (or (spam-check-crm114-headers t)
2669 (spam-check-crm114 t))))
2670 (gnus-summary-show-article)
2671 (message "pR: %s" score)
2674 (defun spam-check-crm114 (&optional score)
2675 "Check the CRM114 Mailfilter backend for the classification of this message"
2676 (let ((article-buffer-name (buffer-name))
2677 (db spam-crm114-database-directory)
2680 (let ((temp-buffer-name (buffer-name)))
2682 (set-buffer article-buffer-name)
2683 (apply 'call-process-region
2684 (point-min) (point-max)
2686 nil temp-buffer-name nil
2687 (when db (list (concat "--fileprefix=" db)))))
2688 (setq return (spam-check-crm114-headers score))))
2691 (defun spam-crm114-register-with-crm114 (articles
2693 &optional unregister)
2694 "Register an article, given as a string, as spam or non-spam."
2695 (dolist (article articles)
2696 (let ((article-string (spam-get-article-as-string article))
2697 (db spam-crm114-database-directory)
2698 (switch (if unregister
2700 spam-crm114-spam-strong-switch
2701 spam-crm114-ham-strong-switch)
2703 spam-crm114-spam-switch
2704 spam-crm114-ham-switch))))
2705 (when (stringp article-string)
2707 (insert article-string)
2709 (apply 'call-process-region
2710 (point-min) (point-max)
2713 (when db (list switch (concat "--fileprefix=" db)))))))))
2715 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2716 (spam-crm114-register-with-crm114 articles t unregister))
2718 (defun spam-crm114-unregister-spam-routine (articles)
2719 (spam-crm114-register-spam-routine articles t))
2721 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2722 (spam-crm114-register-with-crm114 articles nil unregister))
2724 (defun spam-crm114-unregister-ham-routine (articles)
2725 (spam-crm114-register-ham-routine articles t))
2734 (defun spam-initialize (&rest symbols)
2735 "Install the spam.el hooks and do other initialization.
2736 When SYMBOLS is given, set those variables to t. This is so you
2737 can call spam-initialize before you set spam-use-* variables on
2738 explicitly, and matters only if you need the extra headers
2739 installed through spam-necessary-extra-headers."
2742 (dolist (var symbols)
2745 (dolist (header (spam-necessary-extra-headers))
2746 (add-to-list 'nnmail-extra-headers header)
2747 (add-to-list 'gnus-extra-headers header))
2749 (setq spam-install-hooks t)
2750 ;; TODO: How do we redo this every time spam-face is customized?
2751 (push '((eq mark gnus-spam-mark) . spam-face)
2752 gnus-summary-highlight)
2753 ;; Add hooks for loading and saving the spam stats
2754 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2755 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2756 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2757 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2758 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2759 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2760 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2762 (defun spam-unload-hook ()
2763 "Uninstall the spam.el hooks"
2765 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2766 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2767 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2768 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2769 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2770 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2771 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2773 (when spam-install-hooks
2779 ;;; spam.el ends here