1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004, 2005 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.")
87 (defcustom spam-summary-exit-behavior 'default
88 "Exit behavior at the time of summary exit.
89 Note that setting the spam-use-move or spam-use-copy backends on
90 a group through group/topic parameters overrides this mechanism."
91 :type '(choice (const 'default :tag
92 "Move spam out of all groups. Move ham out of spam groups.")
94 "Move spam out of all groups. Move ham out of all groups.")
95 (const 'move-none :tag
96 "Never move spam or ham out of any groups."))
99 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
100 "Directory for spam whitelists and blacklists."
104 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
105 "Whether new messages in a spam group should get the spam-mark."
109 (defcustom spam-log-to-registry nil
110 "Whether spam/ham processing should be logged in the registry."
114 (defcustom spam-split-symbolic-return nil
115 "Whether `spam-split' should work with symbols or group names."
119 (defcustom spam-split-symbolic-return-positive nil
120 "Whether `spam-split' should ALWAYS work with symbols or group names.
121 Do not set this if you use `spam-split' in a fancy split
126 (defcustom spam-mark-only-unseen-as-spam t
127 "Whether only unseen articles should be marked as spam in spam groups.
128 When nil, all unread articles in a spam group are marked as
129 spam. Set this if you want to leave an article unread in a spam group
130 without losing it to the automatic spam-marking process."
134 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
135 "Whether ham should be marked unread before it's moved.
136 The article is moved out of a spam group according to ham-process-destination.
137 This variable is an official entry in the international Longest Variable Name
142 (defcustom spam-disable-spam-split-during-ham-respool nil
143 "Whether `spam-split' should be ignored while resplitting ham.
144 This is useful to prevent ham from ending up in the same spam
145 group after the resplit. Don't set this to t if you have `spam-split' as the
146 last rule in your split configuration."
150 (defcustom spam-autodetect-recheck-messages nil
151 "Should spam.el recheck all meessages when autodetecting?
152 Normally this is nil, so only unseen messages will be checked."
156 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
157 "The location of the whitelist.
158 The file format is one regular expression per line.
159 The regular expression is matched against the address."
163 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
164 "The location of the blacklist.
165 The file format is one regular expression per line.
166 The regular expression is matched against the address."
170 (defcustom spam-use-dig t
171 "Whether `query-dig' should be used instead of `query-dns'."
175 (defcustom spam-use-gmane-xref nil
176 "Whether the Gmane spam xref should be used by `spam-split'."
180 (defcustom spam-use-blacklist nil
181 "Whether the blacklist should be used by `spam-split'."
185 (defcustom spam-blacklist-ignored-regexes nil
186 "Regular expressions that the blacklist should ignore."
187 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
190 (defcustom spam-use-whitelist nil
191 "Whether the whitelist should be used by `spam-split'."
195 (defcustom spam-use-whitelist-exclusive nil
196 "Whether whitelist-exclusive should be used by `spam-split'.
197 Exclusive whitelisting means that all messages from senders not in the whitelist
198 are considered spam."
202 (defcustom spam-use-blackholes nil
203 "Whether blackholes should be used by `spam-split'."
207 (defcustom spam-use-hashcash nil
208 "Whether hashcash payments should be detected by `spam-split'."
212 (defcustom spam-use-regex-headers nil
213 "Whether a header regular expression match should be used by `spam-split'.
214 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
218 (defcustom spam-use-regex-body nil
219 "Whether a body regular expression match should be used by `spam-split'.
220 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
224 (defcustom spam-use-bogofilter-headers nil
225 "Whether bogofilter headers should be used by `spam-split'.
226 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
230 (defcustom spam-use-bogofilter nil
231 "Whether bogofilter should be invoked by `spam-split'.
232 Enable this if you want Gnus to invoke Bogofilter on new messages."
236 (defcustom spam-use-bsfilter-headers nil
237 "Whether bsfilter headers should be used by `spam-split'.
238 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
242 (defcustom spam-use-bsfilter nil
243 "Whether bsfilter should be invoked by `spam-split'.
244 Enable this if you want Gnus to invoke Bsfilter on new messages."
248 (defcustom spam-use-BBDB nil
249 "Whether BBDB should be used by `spam-split'."
253 (defcustom spam-use-BBDB-exclusive nil
254 "Whether BBDB-exclusive should be used by `spam-split'.
255 Exclusive BBDB means that all messages from senders not in the BBDB are
260 (defcustom spam-use-ifile nil
261 "Whether ifile should be used by `spam-split'."
265 (defcustom spam-use-stat nil
266 "Whether `spam-stat' should be used by `spam-split'."
270 (defcustom spam-use-spamoracle nil
271 "Whether spamoracle should be used by `spam-split'."
275 (defcustom spam-use-spamassassin nil
276 "Whether spamassassin should be invoked by `spam-split'.
277 Enable this if you want Gnus to invoke SpamAssassin on new messages."
281 (defcustom spam-use-spamassassin-headers nil
282 "Whether spamassassin headers should be checked by `spam-split'.
283 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
288 (defcustom spam-use-crm114 nil
289 "Whether the CRM114 Mailfilter should be used by `spam-split'."
293 (defcustom spam-install-hooks (or
298 spam-use-whitelist-exclusive
301 spam-use-regex-headers
304 spam-use-bogofilter-headers
305 spam-use-spamassassin
306 spam-use-spamassassin-headers
308 spam-use-bsfilter-headers
310 spam-use-BBDB-exclusive
315 "Whether the spam hooks should be installed.
316 Default to t if one of the spam-use-* variables is set."
320 (defcustom spam-split-group "spam"
321 "Group name where incoming spam should be put by `spam-split'."
325 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
326 ;;; not regular expressions
327 (defcustom spam-junk-mailgroups (cons
329 '("mail.junk" "poste.pourriel"))
330 "Mailgroups with spam contents.
331 All unmarked article in such group receive the spam mark on group entry."
332 :type '(repeat (string :tag "Group"))
336 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
337 "The group where spam xrefs can be found on Gmane.
338 Only meaningful if you enable `spam-use-gmane-xref'."
342 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
343 "dev.null.dk" "relays.visi.com")
344 "List of blackhole servers.
345 Only meaningful if you enable `spam-use-blackholes'."
346 :type '(repeat (string :tag "Server"))
349 (defcustom spam-blackhole-good-server-regex nil
350 "String matching IP addresses that should not be checked in the blackholes.
351 Only meaningful if you enable `spam-use-blackholes'."
352 :type '(radio (const nil) regexp)
356 '((((class color) (type tty) (background dark))
357 (:foreground "gray80" :background "gray50"))
358 (((class color) (type tty) (background light))
359 (:foreground "gray50" :background "gray80"))
360 (((class color) (background dark))
361 (:foreground "ivory2"))
362 (((class color) (background light))
363 (:foreground "ivory4"))
364 (t :inverse-video t))
365 "Face for spam-marked articles."
368 (defcustom spam-face 'spam-face
369 "Face for spam-marked articles."
373 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
374 "Regular expression for positive header spam matches.
375 Only meaningful if you enable `spam-use-regex-headers'."
376 :type '(repeat (regexp :tag "Regular expression to match spam header"))
379 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
380 "Regular expression for positive header ham matches.
381 Only meaningful if you enable `spam-use-regex-headers'."
382 :type '(repeat (regexp :tag "Regular expression to match ham header"))
385 (defcustom spam-regex-body-spam '()
386 "Regular expression for positive body spam matches.
387 Only meaningful if you enable `spam-use-regex-body'."
388 :type '(repeat (regexp :tag "Regular expression to match spam body"))
391 (defcustom spam-regex-body-ham '()
392 "Regular expression for positive body ham matches.
393 Only meaningful if you enable `spam-use-regex-body'."
394 :type '(repeat (regexp :tag "Regular expression to match ham body"))
397 (defcustom spam-summary-score-preferred-header nil
398 "Preferred header to use for spam-summary-score."
399 :type '(choice :tag "Header name"
400 (symbol :tag "SpamAssassin etc" X-Spam-Status)
401 (symbol :tag "Bogofilter" X-Bogosity)
402 (const :tag "No preference, take best guess." nil))
405 (defgroup spam-ifile nil
406 "Spam ifile configuration."
409 (defcustom spam-ifile-path (executable-find "ifile")
410 "File path of the ifile executable program."
411 :type '(choice (file :tag "Location of ifile")
412 (const :tag "ifile is not installed"))
415 (defcustom spam-ifile-database-path nil
416 "File path of the ifile database."
417 :type '(choice (file :tag "Location of the ifile database")
418 (const :tag "Use the default"))
421 (defcustom spam-ifile-spam-category "spam"
422 "Name of the spam ifile category."
426 (defcustom spam-ifile-ham-category nil
427 "Name of the ham ifile category.
428 If nil, the current group name will be used."
429 :type '(choice (string :tag "Use a fixed category")
430 (const :tag "Use the current group name"))
433 (defcustom spam-ifile-all-categories nil
434 "Whether the ifile check will return all categories, or just spam.
435 Set this to t if you want to use the `spam-split' invocation of ifile as
436 your main source of newsgroup names."
440 (defgroup spam-bogofilter nil
441 "Spam bogofilter configuration."
444 (defcustom spam-bogofilter-path (executable-find "bogofilter")
445 "File path of the Bogofilter executable program."
446 :type '(choice (file :tag "Location of bogofilter")
447 (const :tag "Bogofilter is not installed"))
448 :group 'spam-bogofilter)
450 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
452 (defcustom spam-bogofilter-header "X-Bogosity"
453 "The header that Bogofilter inserts in messages."
455 :group 'spam-bogofilter)
457 (defcustom spam-bogofilter-spam-switch "-s"
458 "The switch that Bogofilter uses to register spam messages."
460 :group 'spam-bogofilter)
462 (defcustom spam-bogofilter-ham-switch "-n"
463 "The switch that Bogofilter uses to register ham messages."
465 :group 'spam-bogofilter)
467 (defcustom spam-bogofilter-spam-strong-switch "-S"
468 "The switch that Bogofilter uses to unregister ham messages."
470 :group 'spam-bogofilter)
472 (defcustom spam-bogofilter-ham-strong-switch "-N"
473 "The switch that Bogofilter uses to unregister spam messages."
475 :group 'spam-bogofilter)
477 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
478 "The regex on `spam-bogofilter-header' for positive spam identification."
480 :group 'spam-bogofilter)
482 (defcustom spam-bogofilter-database-directory nil
483 "Directory path of the Bogofilter databases."
484 :type '(choice (directory
485 :tag "Location of the Bogofilter database directory")
486 (const :tag "Use the default"))
487 :group 'spam-bogofilter)
489 (defgroup spam-bsfilter nil
490 "Spam bsfilter configuration."
493 (defcustom spam-bsfilter-path (executable-find "bsfilter")
494 "File path of the Bsfilter executable program."
495 :type '(choice (file :tag "Location of bsfilter")
496 (const :tag "Bsfilter is not installed"))
497 :group 'spam-bsfilter)
499 (defcustom spam-bsfilter-header "X-Spam-Flag"
500 "The header inserted by Bsfilter to flag spam."
502 :group 'spam-bsfilter)
504 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
505 "The header that Bsfilter inserts in messages."
507 :group 'spam-bsfilter)
509 (defcustom spam-bsfilter-spam-switch "--add-spam"
510 "The switch that Bsfilter uses to register spam messages."
512 :group 'spam-bsfilter)
514 (defcustom spam-bsfilter-ham-switch "--add-ham"
515 "The switch that Bsfilter uses to register ham messages."
517 :group 'spam-bsfilter)
519 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
520 "The switch that Bsfilter uses to unregister ham messages."
522 :group 'spam-bsfilter)
524 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
525 "The switch that Bsfilter uses to unregister spam messages."
527 :group 'spam-bsfilter)
529 (defcustom spam-bsfilter-database-directory nil
530 "Directory path of the Bsfilter databases."
531 :type '(choice (directory
532 :tag "Location of the Bsfilter database directory")
533 (const :tag "Use the default"))
534 :group 'spam-bsfilter)
536 (defgroup spam-spamoracle nil
537 "Spam spamoracle configuration."
540 (defcustom spam-spamoracle-database nil
541 "Location of spamoracle database file.
542 When nil, use the default spamoracle database."
543 :type '(choice (directory :tag "Location of spamoracle database file.")
544 (const :tag "Use the default"))
545 :group 'spam-spamoracle)
547 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
548 "Location of the spamoracle binary."
549 :type '(choice (directory :tag "Location of the spamoracle binary")
550 (const :tag "Use the default"))
551 :group 'spam-spamoracle)
553 (defgroup spam-spamassassin nil
554 "Spam SpamAssassin configuration."
557 (defcustom spam-spamassassin-path (executable-find "spamassassin")
558 "File path of the spamassassin executable program.
559 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
560 spamd man pages for more information on these programs."
561 :type '(choice (file :tag "Location of spamc")
562 (const :tag "spamassassin is not installed"))
563 :group 'spam-spamassassin)
565 (defcustom spam-spamassassin-arguments ()
566 "Arguments to pass to the spamassassin executable.
567 This must be a list. For example, `(\"-C\" \"configfile\")'."
568 :type '(restricted-sexp :match-alternatives (listp))
569 :group 'spam-spamassassin)
571 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
572 "The header inserted by SpamAssassin to flag spam."
574 :group 'spam-spamassassin)
576 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
577 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
580 :group 'spam-spamassassin)
582 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
583 "The header inserted by SpamAssassin, giving extended scoring information"
585 :group 'spam-spamassassin)
587 (defcustom spam-sa-learn-path (executable-find "sa-learn")
588 "File path of the sa-learn executable program."
589 :type '(choice (file :tag "Location of spamassassin")
590 (const :tag "spamassassin is not installed"))
591 :group 'spam-spamassassin)
593 (defcustom spam-sa-learn-rebuild t
594 "Whether sa-learn should rebuild the database every time it is called
595 Enable this if you want sa-learn to rebuild the database automatically. Doing
596 this will slightly increase the running time of the spam registration process.
597 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
598 order for SpamAssassin to recognize the new registered spam."
600 :group 'spam-spamassassin)
602 (defcustom spam-sa-learn-spam-switch "--spam"
603 "The switch that sa-learn uses to register spam messages"
605 :group 'spam-spamassassin)
607 (defcustom spam-sa-learn-ham-switch "--ham"
608 "The switch that sa-learn uses to register ham messages"
610 :group 'spam-spamassassin)
612 (defcustom spam-sa-learn-unregister-switch "--forget"
613 "The switch that sa-learn uses to unregister messages messages"
615 :group 'spam-spamassassin)
617 (defgroup spam-crm114 nil
618 "Spam CRM114 Mailfilter configuration."
621 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
622 "File path of the CRM114 Mailfilter executable program."
623 :type '(choice (file :tag "Location of CRM114 Mailfilter")
624 (const :tag "CRM114 Mailfilter is not installed"))
627 (defcustom spam-crm114-header "X-CRM114-Status"
628 "The header that CRM114 Mailfilter inserts in messages."
632 (defcustom spam-crm114-spam-switch "--learnspam"
633 "The switch that CRM114 Mailfilter uses to register spam messages."
637 (defcustom spam-crm114-ham-switch "--learnnonspam"
638 "The switch that CRM114 Mailfilter uses to register ham messages."
642 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
643 "The switch that CRM114 Mailfilter uses to unregister ham messages."
647 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
648 "The switch that CRM114 Mailfilter uses to unregister spam messages."
652 (defcustom spam-crm114-positive-spam-header "^SPAM"
653 "The regex on `spam-crm114-header' for positive spam identification."
657 (defcustom spam-crm114-database-directory nil
658 "Directory path of the CRM114 Mailfilter databases."
659 :type '(choice (directory
660 :tag "Location of the CRM114 Mailfilter database directory")
661 (const :tag "Use the default"))
664 ;;; Key bindings for spam control.
666 (gnus-define-keys gnus-summary-mode-map
667 "St" spam-generic-score
668 "Sx" gnus-summary-mark-as-spam
669 "Mst" spam-generic-score
670 "Msx" gnus-summary-mark-as-spam
671 "\M-d" gnus-summary-mark-as-spam)
673 (defvar spam-cache-lookups t
674 "Whether spam.el will try to cache lookups using `spam-caches'.")
676 (defvar spam-caches (make-hash-table
679 "Cache of spam detection entries.")
681 (defvar spam-old-articles nil
682 "List of old ham and spam articles, generated when a group is entered.")
684 (defvar spam-split-disabled nil
685 "If non-nil, `spam-split' is disabled, and always returns nil.")
687 (defvar spam-split-last-successful-check nil
689 `spam-split' will set this to nil or a spam-use-XYZ check if it
692 ;; internal variables for backends
693 ;; TODO: find a way to create these on the fly in spam-install-backend-super
694 (defvar spam-use-copy nil)
695 (defvar spam-use-move nil)
696 (defvar spam-use-gmane nil)
697 (defvar spam-use-resend nil)
701 ;;{{{ convenience functions
703 (defun spam-clear-cache (symbol)
704 "Clear the spam-caches entry for a check."
705 (remhash symbol spam-caches))
707 (defun spam-xor (a b)
709 (and (or a b) (not (and a b))))
711 (defun spam-set-difference (list1 list2)
712 "Return a set difference of LIST1 and LIST2.
713 When either list is nil, the other is returned."
714 (if (and list1 list2)
715 ;; we have two non-nil lists
717 (dolist (item (append list1 list2))
718 (when (and (memq item list1) (memq item list2))
719 (setq list1 (delq item list1))
720 (setq list2 (delq item list2))))
721 (append list1 list2))
722 ;; if either of the lists was nil, return the other one
723 (if list1 list1 list2)))
725 (defun spam-group-ham-mark-p (group mark &optional spam)
726 "Checks if MARK is considered a ham mark in GROUP."
727 (when (stringp group)
728 (let* ((marks (spam-group-ham-marks group spam))
729 (marks (if (symbolp mark)
731 (mapcar 'symbol-value marks))))
734 (defun spam-group-spam-mark-p (group mark)
735 "Checks if MARK is considered a spam mark in GROUP."
736 (spam-group-ham-mark-p group mark t))
738 (defun spam-group-ham-marks (group &optional spam)
739 "In GROUP, get all the ham marks."
740 (when (stringp group)
741 (let* ((marks (if spam
742 (gnus-parameter-spam-marks group)
743 (gnus-parameter-ham-marks group)))
745 (marks (if (listp (car marks)) (car marks) marks)))
748 (defun spam-group-spam-marks (group)
749 "In GROUP, get all the spam marks."
750 (spam-group-ham-marks group t))
752 (defun spam-group-spam-contents-p (group)
753 "Is GROUP a spam group?"
754 (if (and (stringp group) (< 0 (length group)))
755 (or (member group spam-junk-mailgroups)
756 (memq 'gnus-group-spam-classification-spam
757 (gnus-parameter-spam-contents group)))
760 (defun spam-group-ham-contents-p (group)
761 "Is GROUP a ham group?"
763 (memq 'gnus-group-spam-classification-ham
764 (gnus-parameter-spam-contents group))
767 (defun spam-classifications ()
768 "Return list of valid classifications"
771 (defun spam-classification-valid-p (classification)
772 "Is CLASSIFICATION a valid spam/ham classification?"
773 (memq classification (spam-classifications)))
775 (defun spam-backend-properties ()
776 "Return list of valid classifications."
777 '(statistical mover check hrf srf huf suf))
779 (defun spam-backend-property-valid-p (property)
780 "Is PROPERTY a valid backend property?"
781 (memq property (spam-backend-properties)))
783 (defun spam-backend-function-type-valid-p (type)
784 (or (eq type 'registration)
785 (eq type 'unregistration)))
787 (defun spam-process-type-valid-p (process-type)
788 (or (eq process-type 'incoming)
789 (eq process-type 'process)))
791 (defun spam-list-articles (articles classification)
792 (let ((mark-check (if (eq classification 'spam)
793 'spam-group-spam-mark-p
794 'spam-group-ham-mark-p))
795 alist mark-cache-yes mark-cache-no)
796 (dolist (article articles)
797 (let ((mark (gnus-summary-article-mark article)))
798 (unless (or (memq mark mark-cache-yes)
799 (memq mark mark-cache-no))
800 (if (funcall mark-check
803 (push mark mark-cache-yes)
804 (push mark mark-cache-no)))
805 (when (memq mark mark-cache-yes)
806 (push article alist))))
811 ;;{{{ backend installation functions and procedures
813 (defun spam-install-backend-super (backend &rest properties)
814 "Install BACKEND for spam.el.
815 Accepts incoming CHECK, ham registration function HRF, spam
816 registration function SRF, ham unregistration function HUF, spam
817 unregistration function SUF, and an indication whether the
818 backend is STATISTICAL."
820 (setq spam-backends (add-to-list 'spam-backends backend))
822 (let ((property (pop properties))
823 (value (pop properties)))
824 (if (spam-backend-property-valid-p property)
825 (put backend property value)
828 "spam-install-backend-super got an invalid property %s"
831 (defun spam-backend-list (&optional type)
832 "Return a list of all the backend symbols, constrained by TYPE.
833 When TYPE is 'non-mover, only non-mover backends are returned.
834 When TYPE is 'mover, only mover backends are returned."
836 (dolist (backend spam-backends)
838 (null type) ;either no type was requested
839 ;; or the type is 'mover and the backend is a mover
842 (spam-backend-mover-p backend))
843 ;; or the type is 'non-mover and the backend is not a mover
846 (not (spam-backend-mover-p backend))))
847 (push backend list)))
850 (defun spam-backend-check (backend)
851 "Get the check function for BACKEND.
852 Each individual check may return nil, t, or a mailgroup name.
853 The value nil means that the check does not yield a decision, and
854 so, that further checks are needed. The value t means that the
855 message is definitely not spam, and that further spam checks
856 should be inhibited. Otherwise, a mailgroup name or the symbol
857 'spam (depending on spam-split-symbolic-return) is returned where
858 the mail should go, and further checks are also inhibited. The
859 usual mailgroup name is the value of `spam-split-group', meaning
860 that the message is definitely a spam."
861 (get backend 'check))
863 (defun spam-backend-valid-p (backend)
865 (member backend (spam-backend-list)))
867 (defun spam-backend-info (backend)
868 "Return information about BACKEND."
869 (if (spam-backend-valid-p backend)
871 (setq info (format "Backend %s has the following properties:\n"
873 (dolist (property (spam-backend-properties))
874 (setq info (format "%s%s=%s\n"
877 (get backend property))))
879 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
882 (defun spam-backend-function (backend classification type)
883 "Get the BACKEND function for CLASSIFICATION and TYPE.
884 TYPE is 'registration or 'unregistration.
885 CLASSIFICATION is 'ham or 'spam."
887 (spam-classification-valid-p classification)
888 (spam-backend-function-type-valid-p type))
891 (format "spam-backend-%s-%s-function"
894 (funcall retrieval backend))
897 "%s was passed invalid backend %s, classification %s, or type %s"
898 "spam-backend-function"
903 (defun spam-backend-article-list-property (classification
904 &optional unregister)
905 "Property name of article list with CLASSIFICATION and UNREGISTER."
906 (let* ((r (if unregister "unregister" "register"))
907 (prop (format "%s-%s" classification r)))
910 (defun spam-backend-get-article-todo-list (backend
912 &optional unregister)
913 "Get the articles to be processed for BACKEND and CLASSIFICATION.
914 With UNREGISTER, get articles to be unregistered.
915 This is a temporary storage function - nothing here persists."
918 (intern (spam-backend-article-list-property classification unregister))))
920 (defun spam-backend-put-article-todo-list (backend classification list &optional unregister)
921 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
922 With UNREGISTER, set articles to be unregistered.
923 This is a temporary storage function - nothing here persists."
926 (intern (spam-backend-article-list-property classification unregister))
929 (defun spam-backend-ham-registration-function (backend)
930 "Get the ham registration function for BACKEND."
933 (defun spam-backend-spam-registration-function (backend)
934 "Get the spam registration function for BACKEND."
937 (defun spam-backend-ham-unregistration-function (backend)
938 "Get the ham unregistration function for BACKEND."
941 (defun spam-backend-spam-unregistration-function (backend)
942 "Get the spam unregistration function for BACKEND."
945 (defun spam-backend-statistical-p (backend)
946 "Is BACKEND statistical?"
947 (get backend 'statistical))
949 (defun spam-backend-mover-p (backend)
950 "Is BACKEND a mover?"
951 (get backend 'mover))
953 (defun spam-install-backend-alias (backend alias)
954 "Add ALIAS to an existing BACKEND.
955 The previous backend settings for ALIAS are erased."
957 ;; install alias with no properties at first
958 (spam-install-backend-super alias)
960 (dolist (property (spam-backend-properties))
961 (put alias property (get backend property))))
963 (defun spam-install-checkonly-backend (backend check)
964 "Install a BACKEND than can only CHECK for spam."
965 (spam-install-backend-super backend 'check check))
967 (defun spam-install-mover-backend (backend hrf srf huf suf)
968 "Install a BACKEND than can move articles at summary exit.
969 Accepts ham registration function HRF, spam registration function
970 SRF, ham unregistration function HUF, spam unregistration
971 function SUF. The backend has no incoming check and can't be
973 (spam-install-backend-super
975 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
977 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
978 "Install a BACKEND than has no check.
979 Accepts ham registration function HRF, spam registration function
980 SRF, ham unregistration function HUF, spam unregistration
981 function SUF. The backend has no incoming check and can't be
982 statistical (it could be, but in practice that doesn't happen)."
983 (spam-install-backend-super
985 'hrf hrf 'srf srf 'huf huf 'suf suf))
987 (defun spam-install-backend (backend check hrf srf huf suf)
989 Accepts incoming CHECK, ham registration function HRF, spam
990 registration function SRF, ham unregistration function HUF, spam
991 unregistration function SUF. The backend won't be
992 statistical (use spam-install-statistical-backend for that)."
993 (spam-install-backend-super
995 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
997 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
999 Accepts incoming CHECK, ham registration function HRF, spam
1000 registration function SRF, ham unregistration function HUF, spam
1001 unregistration function SUF. The backend will be
1002 statistical (use spam-install-backend for non-statistical
1004 (spam-install-backend-super
1006 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1008 (defun spam-install-statistical-checkonly-backend (backend check)
1009 "Install a statistical BACKEND than can only CHECK for spam."
1010 (spam-install-backend-super
1012 'check check 'statistical t))
1016 ;;{{{ backend installations
1017 (spam-install-checkonly-backend 'spam-use-blackholes
1018 'spam-check-blackholes)
1020 (spam-install-checkonly-backend 'spam-use-hashcash
1021 'spam-check-hashcash)
1023 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1024 'spam-check-spamassassin-headers)
1026 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1027 'spam-check-bogofilter-headers)
1029 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1030 'spam-check-bsfilter-headers)
1032 (spam-install-checkonly-backend 'spam-use-gmane-xref
1033 'spam-check-gmane-xref)
1035 (spam-install-checkonly-backend 'spam-use-regex-headers
1036 'spam-check-regex-headers)
1038 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1039 'spam-check-regex-body)
1041 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy) instead
1042 (spam-install-mover-backend 'spam-use-move
1043 'spam-move-ham-routine
1044 'spam-move-spam-routine
1048 (spam-install-nocheck-backend 'spam-use-copy
1049 'spam-copy-ham-routine
1050 'spam-copy-spam-routine
1054 (spam-install-nocheck-backend 'spam-use-gmane
1056 'spam-report-gmane-register-routine
1057 ;; does Gmane support unregistration?
1061 (spam-install-nocheck-backend 'spam-use-resend
1062 'spam-report-resend-register-ham-routine
1063 'spam-report-resend-register-routine
1067 (spam-install-backend 'spam-use-BBDB
1069 'spam-BBDB-register-routine
1071 'spam-BBDB-unregister-routine
1074 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1076 (spam-install-backend 'spam-use-blacklist
1077 'spam-check-blacklist
1079 'spam-blacklist-register-routine
1081 'spam-blacklist-unregister-routine)
1083 (spam-install-backend 'spam-use-whitelist
1084 'spam-check-whitelist
1085 'spam-whitelist-register-routine
1087 'spam-whitelist-unregister-routine
1090 (spam-install-statistical-backend 'spam-use-ifile
1092 'spam-ifile-register-ham-routine
1093 'spam-ifile-register-spam-routine
1094 'spam-ifile-unregister-ham-routine
1095 'spam-ifile-unregister-spam-routine)
1097 (spam-install-statistical-backend 'spam-use-spamoracle
1098 'spam-check-spamoracle
1099 'spam-spamoracle-learn-ham
1100 'spam-spamoracle-learn-spam
1101 'spam-spamoracle-unlearn-ham
1102 'spam-spamoracle-unlearn-spam)
1104 (spam-install-statistical-backend 'spam-use-stat
1106 'spam-stat-register-ham-routine
1107 'spam-stat-register-spam-routine
1108 'spam-stat-unregister-ham-routine
1109 'spam-stat-unregister-spam-routine)
1111 (spam-install-statistical-backend 'spam-use-spamassassin
1112 'spam-check-spamassassin
1113 'spam-spamassassin-register-ham-routine
1114 'spam-spamassassin-register-spam-routine
1115 'spam-spamassassin-unregister-ham-routine
1116 'spam-spamassassin-unregister-spam-routine)
1118 (spam-install-statistical-backend 'spam-use-bogofilter
1119 'spam-check-bogofilter
1120 'spam-bogofilter-register-ham-routine
1121 'spam-bogofilter-register-spam-routine
1122 'spam-bogofilter-unregister-ham-routine
1123 'spam-bogofilter-unregister-spam-routine)
1125 (spam-install-statistical-backend 'spam-use-bsfilter
1126 'spam-check-bsfilter
1127 'spam-bsfilter-register-ham-routine
1128 'spam-bsfilter-register-spam-routine
1129 'spam-bsfilter-unregister-ham-routine
1130 'spam-bsfilter-unregister-spam-routine)
1132 (spam-install-statistical-backend 'spam-use-crm114
1134 'spam-crm114-register-ham-routine
1135 'spam-crm114-register-spam-routine
1136 ;; does CRM114 Mailfilter support unregistration?
1142 ;;{{{ scoring and summary formatting
1143 (defun spam-necessary-extra-headers ()
1144 "Return the extra headers spam.el thinks are necessary."
1146 (when (or spam-use-spamassassin
1147 spam-use-spamassassin-headers
1148 spam-use-regex-headers)
1149 (push 'X-Spam-Status list))
1150 (when spam-use-bogofilter
1151 (push 'X-Bogosity list))
1154 (defun spam-user-format-function-S (headers)
1157 (spam-summary-score headers spam-summary-score-preferred-header))))
1159 (defun spam-article-sort-by-spam-status (h1 h2)
1160 "Sort articles by score."
1162 (dolist (header (spam-necessary-extra-headers))
1163 (let ((s1 (spam-summary-score h1 header))
1164 (s2 (spam-summary-score h2 header)))
1166 (setq result (< s1 s2))
1170 (defun spam-extra-header-to-number (header headers)
1171 "Transform an extra HEADER to a number, using list of HEADERS.
1172 Note this has to be fast."
1173 (if (gnus-extra-header header headers)
1175 ((eq header 'X-Spam-Status)
1176 (string-to-number (gnus-replace-in-string
1177 (gnus-extra-header header headers)
1179 ;; for CRM checking, it's probably faster to just do the string match
1180 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
1181 (match-string 1 header))
1182 ((eq header 'X-Bogosity)
1183 (string-to-number (gnus-replace-in-string
1184 (gnus-replace-in-string
1185 (gnus-extra-header header headers)
1191 (defun spam-summary-score (headers &optional specific-header)
1192 "Score an article for the summary buffer, as fast as possible.
1193 With SPECIFIC-HEADER, returns only that header's score.
1194 Will not return a nil score."
1198 (list specific-header)
1199 (spam-necessary-extra-headers)))
1201 (spam-extra-header-to-number header headers))
1206 (defun spam-generic-score (&optional recheck)
1207 "Invoke whatever scoring method we can."
1210 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1211 (spam-spamassassin-score recheck))
1212 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1213 (spam-bsfilter-score recheck))
1215 (spam-crm114-score))
1216 (t (spam-bogofilter-score recheck))))
1219 ;;{{{ set up widening, processor checks
1221 ;;; set up IMAP widening if it's necessary
1222 (defun spam-setup-widening ()
1223 (when (spam-widening-needed-p)
1224 (setq nnimap-split-download-body-default t)))
1226 (defun spam-widening-needed-p (&optional force-symbols)
1228 (dolist (backend (spam-backend-list))
1229 (when (and (spam-backend-statistical-p backend)
1230 (or (symbol-value backend)
1231 (memq backend force-symbols)))
1232 (setq found backend)))
1235 (defvar spam-list-of-processors
1236 ;; note the nil processors are not defined in gnus.el
1237 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1238 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1239 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1240 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1241 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1242 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1243 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1244 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1245 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1246 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1247 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1248 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1249 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1250 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1251 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1252 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1253 "The OBSOLETE `spam-list-of-processors' list.
1254 This list contains pairs associating the obsolete ham/spam exit
1255 processor variables with a classification and a spam-use-*
1256 variable. When the processor variable is nil, just the
1257 classification and spam-use-* check variable are used. This is
1258 superceded by the new spam backend code, so it's only consulted
1259 for backwards compatibility.")
1261 (defun spam-group-processor-p (group backend &optional classification)
1262 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1263 Also accepts the obsolete processors, which can be found in
1264 gnus.el and in spam-list-of-processors. In the case of mover
1265 backends, checks the setting of spam-summary-exit-behavior in
1266 addition to the set values for the group."
1267 (if (and (stringp group)
1269 (let ((old-style (assq backend spam-list-of-processors))
1270 (parameters (nth 0 (gnus-parameter-spam-process group)))
1272 (if old-style ; old-style processor
1273 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1274 ;; now search for the parameter
1275 (dolist (parameter parameters)
1276 (when (and (null found)
1278 (eq classification (nth 0 parameter))
1279 (eq backend (nth 1 parameter)))
1282 ;; now, if the parameter was not found, do the
1283 ;; spam-summary-exit-behavior-logic for mover backends
1285 (when (spam-backend-mover-p backend)
1289 ((eq spam-summary-exit-behavior 'move-all) t)
1290 ((eq spam-summary-exit-behavior 'move-none) nil)
1291 ((eq spam-summary-exit-behavior 'default)
1292 (or (eq classification 'spam) ;move spam out of all groups
1293 ;; move ham out of spam groups
1294 (and (eq classification 'ham)
1295 (spam-group-spam-contents-p group))))
1296 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1297 spam-summary-exit-behavior))))))
1304 ;;{{{ Summary entry and exit processing.
1306 (defun spam-mark-junk-as-spam-routine ()
1307 ;; check the global list of group names spam-junk-mailgroups and the
1309 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1310 (gnus-message 6 "Marking %s articles as spam"
1311 (if spam-mark-only-unseen-as-spam
1314 (let ((articles (if spam-mark-only-unseen-as-spam
1315 gnus-newsgroup-unseen
1316 gnus-newsgroup-unreads)))
1317 (if spam-mark-new-messages-in-spam-group-as-spam
1318 (dolist (article articles)
1319 (gnus-summary-mark-article article gnus-spam-mark))
1320 (gnus-message 9 "Did not mark new messages as spam.")))))
1322 (defun spam-summary-prepare ()
1323 (setq spam-old-articles
1324 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1325 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1326 (spam-mark-junk-as-spam-routine))
1328 ;; The spam processors are invoked for any group, spam or ham or neither
1329 (defun spam-summary-prepare-exit ()
1330 (unless gnus-group-is-exiting-without-update-p
1331 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1333 ;; before we begin, remove any article limits
1335 ; (gnus-summary-pop-limit t))
1337 ;; first of all, unregister any articles that are no longer ham or spam
1338 ;; we have to iterate over the processors, or else we'll be too slow
1339 (dolist (classification (spam-classifications))
1340 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1341 (new-articles (spam-list-articles
1342 gnus-newsgroup-articles
1344 (changed-articles (spam-set-difference new-articles old-articles)))
1345 ;; now that we have the changed articles, we go through the processors
1346 (dolist (backend (spam-backend-list))
1347 (let (unregister-list)
1348 (dolist (article changed-articles)
1349 (let ((id (spam-fetch-field-message-id-fast article)))
1350 (when (spam-log-unregistration-needed-p
1351 id 'process classification backend)
1352 (push article unregister-list))))
1353 ;; call spam-register-routine with specific articles to unregister,
1354 ;; when there are articles to unregister and the check is enabled
1355 (when (and unregister-list (symbol-value backend))
1356 (spam-backend-put-article-todo-list backend
1361 ;; do the non-moving backends first, then the moving ones
1362 (dolist (backend-type '(non-mover mover))
1363 (dolist (classification (spam-classifications))
1364 (dolist (backend (spam-backend-list backend-type))
1365 (when (spam-group-processor-p
1369 (spam-backend-put-article-todo-list backend
1372 gnus-newsgroup-articles
1373 classification))))))
1375 (spam-resolve-registrations-routine) ; do the registrations now
1377 ;; we mark all the leftover spam articles as expired at the end
1378 (dolist (article (spam-list-articles
1379 gnus-newsgroup-articles
1381 (gnus-summary-mark-article article gnus-expirable-mark)))
1383 (setq spam-old-articles nil))
1387 ;;{{{ spam-use-move and spam-use-copy backend support functions
1389 (defun spam-copy-or-move-routine (copy groups articles classification)
1391 (when (and (car-safe groups) (listp (car-safe groups)))
1392 (setq groups (pop groups)))
1394 (unless (listp groups)
1395 (setq groups (list groups)))
1397 ;; remove the current process mark
1398 (gnus-summary-kill-process-mark)
1400 (let ((backend-supports-deletions
1401 (gnus-check-backend-function
1402 'request-move-article gnus-newsgroup-name))
1403 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1404 article mark deletep respool)
1406 (when (member 'respool groups)
1407 (setq respool t) ; boolean for later
1408 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1410 ;; now do the actual move
1411 (dolist (group groups)
1412 (when (and articles (stringp group))
1414 ;; first, mark the article with the process mark and, if needed,
1415 ;; the unread or expired mark (for ham and spam respectively)
1416 (dolist (article articles)
1417 (when (and (eq classification 'ham)
1418 spam-mark-ham-unread-before-move-from-spam-group)
1419 (gnus-message 9 "Marking ham article %d unread before move"
1421 (gnus-summary-mark-article article gnus-unread-mark))
1422 (when (and (eq classification 'spam)
1424 (gnus-message 9 "Marking spam article %d expirable before move"
1426 (gnus-summary-mark-article article gnus-expirable-mark))
1427 (gnus-summary-set-process-mark article)
1429 (if respool ; respooling is with a "fake" group
1430 (let ((spam-split-disabled
1431 (or spam-split-disabled
1432 (and (eq classification 'ham)
1433 spam-disable-spam-split-during-ham-respool))))
1434 (gnus-message 9 "Respooling article %d with method %s"
1435 article respool-method)
1436 (gnus-summary-respool-article nil respool-method))
1437 (if (or (not backend-supports-deletions) ; else, we are not respooling
1438 (> (length groups) 1))
1439 (progn ; if copying, copy and set deletep
1440 (gnus-message 9 "Copying article %d to group %s"
1442 (gnus-summary-copy-article nil group)
1444 (gnus-message 9 "Moving article %d to group %s"
1446 (gnus-summary-move-article nil group))))) ; else move articles
1448 ;; now delete the articles, unless a) copy is t, and there was a copy done
1449 ;; b) a move was done to a single group
1450 ;; c) backend-supports-deletions is nil
1452 (when (and deletep backend-supports-deletions)
1453 (dolist (article articles)
1454 (gnus-summary-set-process-mark article)
1455 (gnus-message 9 "Deleting article %d" article))
1457 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1458 (gnus-summary-delete-article nil)))))
1460 (gnus-summary-yank-process-mark)
1461 (length articles))))
1463 (defun spam-copy-spam-routine (articles)
1464 (spam-copy-or-move-routine
1466 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1470 (defun spam-move-spam-routine (articles)
1471 (spam-copy-or-move-routine
1473 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1477 (defun spam-copy-ham-routine (articles)
1478 (spam-copy-or-move-routine
1480 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1484 (defun spam-move-ham-routine (articles)
1485 (spam-copy-or-move-routine
1487 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1493 ;;{{{ article and field retrieval code
1494 (defun spam-get-article-as-string (article)
1495 (when (numberp article)
1497 (gnus-request-article-this-buffer
1499 gnus-newsgroup-name)
1503 ;; (defun spam-get-article-as-filename (article)
1504 ;; (let ((article-filename))
1505 ;; (when (numberp article)
1506 ;; (nnml-possibly-change-directory
1507 ;; (gnus-group-real-name gnus-newsgroup-name))
1508 ;; (setq article-filename (expand-file-name
1509 ;; (int-to-string article) nnml-current-directory)))
1510 ;; (if (file-exists-p article-filename)
1514 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1515 "Fetch a FIELD for ARTICLE quickly, using the internal gnus-data-list function.
1516 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1517 When FIELD is 'number, ARTICLE can be any number (since we want
1519 (when (numberp article)
1520 (let* ((data-header (or prepared-data-header
1521 (spam-fetch-article-header article))))
1522 (if (arrayp data-header)
1524 ((equal field 'number)
1525 (mail-header-number data-header))
1526 ((equal field 'from)
1527 (mail-header-from data-header))
1528 ((equal field 'message-id)
1529 (mail-header-message-id data-header))
1530 ((equal field 'subject)
1531 (mail-header-subject data-header))
1532 ((equal field 'references)
1533 (mail-header-references data-header))
1534 ((equal field 'date)
1535 (mail-header-date data-header))
1536 ((equal field 'xref)
1537 (mail-header-xref data-header))
1538 ((equal field 'extra)
1539 (mail-header-extra data-header))
1543 "spam-fetch-field-fast: unknown field %s requested"
1546 (gnus-message 6 "Article %d has a nil data header" article)))))
1548 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1549 (spam-fetch-field-fast article 'from prepared-data-header))
1551 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1552 (spam-fetch-field-fast article 'subject prepared-data-header))
1554 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1555 (spam-fetch-field-fast article 'message-id prepared-data-header))
1557 (defun spam-generate-fake-headers (article)
1558 (let ((dh (spam-fetch-article-header article)))
1562 ;; 80-character limit makes for strange constructs
1563 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1564 "Date: %s\nReferences: %s\nXref: %s\n")
1565 (spam-fetch-field-fast article 'from dh)
1566 (spam-fetch-field-fast article 'subject dh)
1567 (spam-fetch-field-fast article 'message-id dh)
1568 (spam-fetch-field-fast article 'date dh)
1569 (spam-fetch-field-fast article 'references dh)
1570 (spam-fetch-field-fast article 'xref dh))
1571 (when (spam-fetch-field-fast article 'extra dh)
1572 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1575 "spam-generate-fake-headers: article %d didn't have a valid header"
1578 (defun spam-fetch-article-header (article)
1580 (set-buffer gnus-summary-buffer)
1581 (gnus-read-header article)
1582 (nth 3 (assq article gnus-newsgroup-data))))
1585 ;;{{{ Spam determination.
1587 (defun spam-split (&rest specific-checks)
1588 "Split this message into the `spam' group if it is spam.
1589 This function can be used as an entry in the variable `nnmail-split-fancy',
1590 for example like this: (: spam-split). It can take checks as
1591 parameters. A string as a parameter will set the
1592 spam-split-group to that string.
1594 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1596 (setq spam-split-last-successful-check nil)
1597 (unless spam-split-disabled
1598 (let ((spam-split-group-choice spam-split-group))
1599 (dolist (check specific-checks)
1600 (when (stringp check)
1601 (setq spam-split-group-choice check)
1602 (setq specific-checks (delq check specific-checks))))
1604 (let ((spam-split-group spam-split-group-choice)
1605 (widening-needed-check (spam-widening-needed-p specific-checks)))
1608 (when widening-needed-check
1610 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1611 widening-needed-check))
1612 (let ((backends (spam-backend-list))
1614 (while (and backends (not decision))
1615 (let* ((backend (pop backends))
1616 (check-function (spam-backend-check backend))
1617 (spam-split-group (if spam-split-symbolic-return
1621 ;; either, given specific checks, this is one of them
1622 (memq backend specific-checks)
1623 ;; or, given no specific checks, spam-use-CHECK is set
1624 (and (null specific-checks) (symbol-value backend)))
1625 (gnus-message 6 "spam-split: calling the %s function"
1627 (setq decision (funcall check-function))
1628 ;; if we got a decision at all, save the current check
1630 (setq spam-split-last-successful-check backend))
1632 (when (eq decision 'spam)
1633 (unless spam-split-symbolic-return
1636 (format "spam-split got %s but %s is nil"
1638 spam-split-symbolic-return)))))))
1640 (if spam-split-symbolic-return-positive 'ham nil)
1643 (defun spam-find-spam ()
1644 "This function will detect spam in the current newsgroup using spam-split."
1647 (let* ((group gnus-newsgroup-name)
1648 (autodetect (gnus-parameter-spam-autodetect group))
1649 (methods (gnus-parameter-spam-autodetect-methods group))
1650 (first-method (nth 0 methods))
1651 (articles (if spam-autodetect-recheck-messages
1652 gnus-newsgroup-articles
1653 gnus-newsgroup-unseen))
1654 article-cannot-be-faked)
1657 (dolist (backend methods)
1658 (when (spam-backend-statistical-p backend)
1659 (setq article-cannot-be-faked t)
1662 (when (memq 'default methods)
1663 (setq article-cannot-be-faked t))
1665 (when (and autodetect
1666 (not (equal first-method 'none)))
1669 (let ((id (spam-fetch-field-message-id-fast article))
1670 (subject (spam-fetch-field-subject-fast article))
1671 (sender (spam-fetch-field-from-fast article))
1675 (gnus-message 6 "Article %d has no message ID!" article))
1677 (when (and id spam-log-to-registry)
1678 (setq registry-lookup (spam-log-registration-type id 'incoming))
1679 (when registry-lookup
1682 "spam-find-spam: message %s was already registered incoming"
1685 (let* ((spam-split-symbolic-return t)
1686 (spam-split-symbolic-return-positive t)
1687 (fake-headers (spam-generate-fake-headers article))
1691 (if article-cannot-be-faked
1692 (gnus-request-article-this-buffer
1695 ;; else, we fake the article
1696 (when fake-headers (insert fake-headers)))
1697 (if (or (null first-method)
1698 (equal first-method 'default))
1700 (apply 'spam-split methods))))))
1701 (if (equal split-return 'spam)
1702 (gnus-summary-mark-article article gnus-spam-mark))
1704 (when (and id split-return spam-log-to-registry)
1705 (when (zerop (gnus-registry-group-count id))
1706 (gnus-registry-add-group
1707 id group subject sender))
1709 (unless registry-lookup
1710 (spam-log-processing-to-registry
1714 spam-split-last-successful-check
1720 ;;{{{ registration/unregistration functions
1722 (defun spam-resolve-registrations-routine ()
1723 "Go through the backends and register or unregister articles as needed."
1724 (dolist (backend-type '(non-mover mover))
1725 (dolist (classification (spam-classifications))
1726 (dolist (backend (spam-backend-list backend-type))
1727 (let ((rlist (spam-backend-get-article-todo-list
1728 backend classification))
1729 (ulist (spam-backend-get-article-todo-list
1730 backend classification t))
1733 ;; clear the old lists right away
1734 (spam-backend-put-article-todo-list backend
1738 (spam-backend-put-article-todo-list backend
1743 ;; eliminate duplicates
1744 (dolist (article (copy-sequence ulist))
1745 (when (memq article rlist)
1747 (setq rlist (delq article rlist))
1748 (setq ulist (delq article ulist))))
1750 (unless (zerop delcount)
1753 "%d messages were saved the trouble of unregistering and then registering"
1756 ;; unregister articles
1757 (unless (zerop (length ulist))
1758 (let ((num (spam-unregister-routine classification backend ulist)))
1762 "%d %s messages were unregistered by backend %s."
1767 ;; register articles
1768 (unless (zerop (length rlist))
1769 (let ((num (spam-register-routine classification backend rlist)))
1773 "%d %s messages were registered by backend %s."
1778 (defun spam-unregister-routine (classification
1781 (spam-register-routine classification backend specific-articles t))
1783 (defun spam-register-routine (classification
1786 &optional unregister)
1787 (when (and (spam-classification-valid-p classification)
1788 (spam-backend-valid-p backend))
1789 (let* ((register-function
1790 (spam-backend-function backend classification 'registration))
1791 (unregister-function
1792 (spam-backend-function backend classification 'unregistration))
1793 (run-function (if unregister
1796 (log-function (if unregister
1797 'spam-log-undo-registration
1798 'spam-log-processing-to-registry))
1802 ;; make list of articles, using specific-articles if given
1803 (setq articles (or specific-articles
1805 gnus-newsgroup-articles
1808 (when (> (length articles) 0)
1809 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1810 (if unregister "Unregistering" "Registering")
1812 (if specific-articles "specific" "")
1815 (funcall run-function articles)
1816 ;; now log all the registrations (or undo them, depending on
1818 (dolist (article articles)
1819 (funcall log-function
1820 (spam-fetch-field-message-id-fast article)
1824 gnus-newsgroup-name))))
1825 ;; return the number of articles processed
1826 (length articles))))
1828 ;;; log a ham- or spam-processor invocation to the registry
1829 (defun spam-log-processing-to-registry (id type classification backend group)
1830 (when spam-log-to-registry
1831 (if (and (stringp id)
1833 (spam-process-type-valid-p type)
1834 (spam-classification-valid-p classification)
1835 (spam-backend-valid-p backend))
1836 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1837 (cell (list classification backend group)))
1838 (push cell cell-list)
1839 (gnus-registry-store-extra-entry
1846 (format "%s call with bad ID, type, classification, spam-backend, or group"
1847 "spam-log-processing-to-registry")))))
1849 ;;; check if a ham- or spam-processor registration has been done
1850 (defun spam-log-registered-p (id type)
1851 (when spam-log-to-registry
1852 (if (and (stringp id)
1853 (spam-process-type-valid-p type))
1854 (cdr-safe (gnus-registry-fetch-extra id type))
1858 (format "%s called with bad ID, type, classification, or spam-backend"
1859 "spam-log-registered-p"))
1862 ;;; check what a ham- or spam-processor registration says
1863 ;;; returns nil if conflicting registrations are found
1864 (defun spam-log-registration-type (id type)
1867 (dolist (reg (spam-log-registered-p id type))
1868 (let ((classification (nth 0 reg)))
1869 (when (spam-classification-valid-p classification)
1871 (not (eq classification decision)))
1872 (setq count (+ 1 count)))
1873 (setq decision classification))))
1879 ;;; check if a ham- or spam-processor registration needs to be undone
1880 (defun spam-log-unregistration-needed-p (id type classification backend)
1881 (when spam-log-to-registry
1882 (if (and (stringp id)
1883 (spam-process-type-valid-p type)
1884 (spam-classification-valid-p classification)
1885 (spam-backend-valid-p backend))
1886 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1888 (dolist (cell cell-list)
1890 (when (and (eq classification (nth 0 cell))
1891 (eq backend (nth 1 cell)))
1897 (format "%s called with bad ID, type, classification, or spam-backend"
1898 "spam-log-unregistration-needed-p"))
1902 ;;; undo a ham- or spam-processor registration (the group is not used)
1903 (defun spam-log-undo-registration (id type classification backend &optional group)
1904 (when (and spam-log-to-registry
1905 (spam-log-unregistration-needed-p id type classification backend))
1906 (if (and (stringp id)
1907 (spam-process-type-valid-p type)
1908 (spam-classification-valid-p classification)
1909 (spam-backend-valid-p backend))
1910 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1911 new-cell-list found)
1912 (dolist (cell cell-list)
1913 (unless (and (eq classification (nth 0 cell))
1914 (eq backend (nth 1 cell)))
1915 (push cell new-cell-list)))
1916 (gnus-registry-store-extra-entry
1921 (gnus-error 7 (format "%s call with bad ID, type, spam-backend, or group"
1922 "spam-log-undo-registration"))
1927 ;;{{{ backend functions
1930 (defun spam-check-gmane-xref ()
1932 (message-fetch-field "Xref")
1933 (message-fetch-field "Newsgroups"))))
1934 (when header ; return nil when no header
1935 (when (string-match spam-gmane-xref-spam-group
1937 spam-split-group))))
1943 (defun spam-check-regex-body ()
1944 (let ((spam-regex-headers-ham spam-regex-body-ham)
1945 (spam-regex-headers-spam spam-regex-body-spam))
1946 (spam-check-regex-headers t)))
1952 (defun spam-check-regex-headers (&optional body)
1953 (let ((type (if body "body" "header"))
1955 (dolist (h-regex spam-regex-headers-ham)
1957 (goto-char (point-min))
1958 (when (re-search-forward h-regex nil t)
1959 (message "Ham regex %s search positive." type)
1961 (dolist (s-regex spam-regex-headers-spam)
1963 (goto-char (point-min))
1964 (when (re-search-forward s-regex nil t)
1965 (message "Spam regex %s search positive." type)
1967 (setq ret spam-split-group))))
1974 (defun spam-reverse-ip-string (ip)
1976 (mapconcat 'identity
1977 (nreverse (split-string ip "\\."))
1980 (defun spam-check-blackholes ()
1981 "Check the Received headers for blackholed relays."
1982 (let ((headers (message-fetch-field "received"))
1987 (goto-char (point-min))
1988 (gnus-message 6 "Checking headers for relay addresses")
1989 (while (re-search-forward
1990 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1991 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1992 (push (spam-reverse-ip-string (match-string 1))
1994 (dolist (server spam-blackhole-servers)
1996 (unless (and spam-blackhole-good-server-regex
1997 ;; match the good-server-regex against the reversed (again) IP string
1999 spam-blackhole-good-server-regex
2000 (spam-reverse-ip-string ip)))
2002 (let ((query-string (concat ip "." server)))
2004 (let ((query-result (query-dig query-string)))
2006 (gnus-message 6 "(DIG): positive blackhole check '%s'"
2008 (push (list ip server query-result)
2010 ;; else, if not using dig.el
2011 (when (query-dns query-string)
2012 (gnus-message 6 "positive blackhole check")
2013 (push (list ip server (query-dns query-string 'TXT))
2022 (autoload 'mail-check-payment "hashcash"))
2028 (defun spam-check-hashcash ()
2029 "Check the headers for hashcash payments."
2030 (mail-check-payment))) ;mail-check-payment returns a boolean
2037 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2038 ;;; <sacha@giotto.sj.ru>
2040 ;; all this is done inside a condition-case to trap errors
2043 (autoload 'bbdb-buffer "bbdb")
2044 (autoload 'bbdb-create-internal "bbdb")
2045 (autoload 'bbdb-search-simple "bbdb"))
2048 (when (condition-case nil
2051 (require 'bbdb-com))
2053 ;; `bbdb-records' should not be bound as an autoload function
2054 ;; before loading bbdb because of `bbdb-hashtable-size'.
2055 (defalias 'bbdb-records 'ignore)
2056 (defalias 'spam-BBDB-register-routine 'ignore)
2057 (defalias 'spam-enter-ham-BBDB 'ignore)
2060 ;; when the BBDB changes, we want to clear out our cache
2061 (defun spam-clear-cache-BBDB (&rest immaterial)
2062 (spam-clear-cache 'spam-use-BBDB))
2064 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2066 (defun spam-enter-ham-BBDB (addresses &optional remove)
2067 "Enter an address into the BBDB; implies ham (non-spam) sender"
2068 (dolist (from addresses)
2069 (when (stringp from)
2070 (let* ((parsed-address (gnus-extract-address-components from))
2071 (name (or (nth 0 parsed-address) "Ham Sender"))
2072 (remove-function (if remove
2073 'bbdb-delete-record-internal
2075 (net-address (nth 1 parsed-address))
2076 (record (and net-address
2077 (bbdb-search-simple nil net-address))))
2079 (gnus-message 6 "%s address %s %s BBDB"
2080 (if remove "Deleting" "Adding")
2082 (if remove "from" "to"))
2084 (funcall remove-function record)
2085 (bbdb-create-internal name nil net-address nil nil
2086 "ham sender added by spam.el")))))))
2088 (defun spam-BBDB-register-routine (articles &optional unregister)
2090 (dolist (article articles)
2091 (when (stringp (spam-fetch-field-from-fast article))
2092 (push (spam-fetch-field-from-fast article) addresses)))
2093 ;; now do the register/unregister action
2094 (spam-enter-ham-BBDB addresses unregister)))
2096 (defun spam-BBDB-unregister-routine (articles)
2097 (spam-BBDB-register-routine articles t))
2099 (defun spam-check-BBDB ()
2100 "Mail from people in the BBDB is classified as ham or non-spam"
2101 (let ((who (message-fetch-field "from"))
2102 bbdb-cache bbdb-hashtable)
2103 (when spam-cache-lookups
2104 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
2106 (setq bbdb-cache (make-vector 17 0)) ; a good starting hash value
2107 ;; this is based on the expanded (bbdb-hashtable) macro
2108 ;; without the debugging support
2109 (with-current-buffer (bbdb-buffer)
2111 (save-window-excursion
2112 (bbdb-records nil t)
2115 (intern (downcase (symbol-name symbol)) bbdb-cache))
2117 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
2119 (setq who (nth 1 (gnus-extract-address-components who)))
2121 (if spam-cache-lookups
2122 (intern-soft (downcase who) bbdb-cache)
2123 (bbdb-search-simple nil who))
2125 (if spam-use-BBDB-exclusive
2133 ;;; check the ifile backend; return nil if the mail was NOT classified
2136 (defun spam-get-ifile-database-parameter ()
2137 "Get the command-line parameter for ifile's database from
2138 spam-ifile-database-path."
2139 (if spam-ifile-database-path
2140 (format "--db-file=%s" spam-ifile-database-path)
2143 (defun spam-check-ifile ()
2144 "Check the ifile backend for the classification of this message."
2145 (let ((article-buffer-name (buffer-name))
2148 (let ((temp-buffer-name (buffer-name))
2149 (db-param (spam-get-ifile-database-parameter)))
2151 (set-buffer article-buffer-name)
2152 (apply 'call-process-region
2153 (point-min) (point-max) spam-ifile-path
2154 nil temp-buffer-name nil "-c"
2155 (if db-param `(,db-param "-q") `("-q"))))
2156 ;; check the return now (we're back in the temp buffer)
2157 (goto-char (point-min))
2159 (setq category (buffer-substring (point) (point-at-eol))))
2160 (when (not (zerop (length category))) ; we need a category here
2161 (if spam-ifile-all-categories
2162 (setq return category)
2163 ;; else, if spam-ifile-all-categories is not set...
2164 (when (string-equal spam-ifile-spam-category category)
2165 (setq return spam-split-group)))))) ; note return is nil otherwise
2168 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2169 "Register an article, given as a string, with a category.
2170 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2171 (let ((category (or category gnus-newsgroup-name))
2172 (add-or-delete-option (if unregister "-d" "-i"))
2173 (db (spam-get-ifile-database-parameter))
2176 (dolist (article articles)
2177 (let ((article-string (spam-get-article-as-string article)))
2178 (when (stringp article-string)
2179 (insert article-string))))
2180 (apply 'call-process-region
2181 (point-min) (point-max) spam-ifile-path
2183 add-or-delete-option category
2184 (if db `(,db "-h") `("-h"))))))
2186 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2187 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2189 (defun spam-ifile-unregister-spam-routine (articles)
2190 (spam-ifile-register-spam-routine articles t))
2192 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2193 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2195 (defun spam-ifile-unregister-ham-routine (articles)
2196 (spam-ifile-register-ham-routine articles t))
2203 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2204 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2205 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2206 (autoload 'spam-stat-buffer-is-spam "spam-stat")
2207 (autoload 'spam-stat-load "spam-stat")
2208 (autoload 'spam-stat-save "spam-stat")
2209 (autoload 'spam-stat-split-fancy "spam-stat"))
2212 (when (condition-case nil
2213 (let ((spam-stat-install-hooks nil))
2214 (require 'spam-stat))
2216 (defalias 'spam-stat-register-ham-routine 'ignore)
2217 (defalias 'spam-stat-register-spam-routine 'ignore)
2220 (defun spam-check-stat ()
2221 "Check the spam-stat backend for the classification of this message"
2222 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2223 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2225 (spam-stat-split-fancy)))
2227 (defun spam-stat-register-spam-routine (articles &optional unregister)
2228 (dolist (article articles)
2229 (let ((article-string (spam-get-article-as-string article)))
2231 (insert article-string)
2233 (spam-stat-buffer-change-to-non-spam)
2234 (spam-stat-buffer-is-spam))))))
2236 (defun spam-stat-unregister-spam-routine (articles)
2237 (spam-stat-register-spam-routine articles t))
2239 (defun spam-stat-register-ham-routine (articles &optional unregister)
2240 (dolist (article articles)
2241 (let ((article-string (spam-get-article-as-string article)))
2243 (insert article-string)
2245 (spam-stat-buffer-change-to-spam)
2246 (spam-stat-buffer-is-non-spam))))))
2248 (defun spam-stat-unregister-ham-routine (articles)
2249 (spam-stat-register-ham-routine articles t))
2251 (defun spam-maybe-spam-stat-load ()
2252 (when spam-use-stat (spam-stat-load)))
2254 (defun spam-maybe-spam-stat-save ()
2255 (when spam-use-stat (spam-stat-save)))))
2259 ;;{{{ Blacklists and whitelists.
2261 (defvar spam-whitelist-cache nil)
2262 (defvar spam-blacklist-cache nil)
2264 (defun spam-kill-whole-line ()
2266 (let ((kill-whole-line t))
2269 ;;; address can be a list, too
2270 (defun spam-enter-whitelist (address &optional remove)
2271 "Enter ADDRESS (list or single) into the whitelist.
2272 With a non-nil REMOVE, remove them."
2273 (interactive "sAddress: ")
2274 (spam-enter-list address spam-whitelist remove)
2275 (setq spam-whitelist-cache nil)
2276 (spam-clear-cache 'spam-use-whitelist))
2278 ;;; address can be a list, too
2279 (defun spam-enter-blacklist (address &optional remove)
2280 "Enter ADDRESS (list or single) into the blacklist.
2281 With a non-nil REMOVE, remove them."
2282 (interactive "sAddress: ")
2283 (spam-enter-list address spam-blacklist remove)
2284 (setq spam-blacklist-cache nil)
2285 (spam-clear-cache 'spam-use-whitelist))
2287 (defun spam-enter-list (addresses file &optional remove)
2288 "Enter ADDRESSES into the given FILE.
2289 Either the whitelist or the blacklist files can be used. With
2290 REMOVE not nil, remove the ADDRESSES."
2291 (if (stringp addresses)
2292 (spam-enter-list (list addresses) file remove)
2293 ;; else, we have a list of addresses here
2294 (unless (file-exists-p (file-name-directory file))
2295 (make-directory (file-name-directory file) t))
2298 (find-file-noselect file))
2299 (dolist (a addresses)
2301 (goto-char (point-min))
2302 (if (re-search-forward (regexp-quote a) nil t)
2303 ;; found the address
2305 (spam-kill-whole-line))
2306 ;; else, the address was not found
2308 (goto-char (point-max))
2314 (defun spam-filelist-build-cache (type)
2315 (let ((cache (if (eq type 'spam-use-blacklist)
2316 spam-blacklist-cache
2317 spam-whitelist-cache))
2319 (unless (gethash type spam-caches)
2321 (let ((address (pop cache)))
2322 (unless (zerop (length address)) ; 0 for a nil address too
2323 (setq address (regexp-quote address))
2324 ;; fix regexp-quote's treatment of user-intended regexes
2325 (while (string-match "\\\\\\*" address)
2326 (setq address (replace-match ".*" t t address))))
2327 (push address parsed-cache)))
2328 (puthash type parsed-cache spam-caches))))
2330 (defun spam-filelist-check-cache (type from)
2331 (when (stringp from)
2332 (spam-filelist-build-cache type)
2334 (dolist (address (gethash type spam-caches))
2335 (when (and address (string-match address from))
2340 ;;; returns t if the sender is in the whitelist, nil or
2341 ;;; spam-split-group otherwise
2342 (defun spam-check-whitelist ()
2343 ;; FIXME! Should it detect when file timestamps change?
2344 (unless spam-whitelist-cache
2345 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2346 (if (spam-from-listed-p 'spam-use-whitelist)
2348 (if spam-use-whitelist-exclusive
2352 (defun spam-check-blacklist ()
2353 ;; FIXME! Should it detect when file timestamps change?
2354 (unless spam-blacklist-cache
2355 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2356 (and (spam-from-listed-p 'spam-use-blacklist)
2359 (defun spam-parse-list (file)
2360 (when (file-readable-p file)
2361 (let (contents address)
2363 (insert-file-contents file)
2365 (setq address (buffer-substring (point) (point-at-eol)))
2367 ;; insert the e-mail address if detected, otherwise the raw data
2368 (unless (zerop (length address))
2369 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2370 (push (or pure-address address) contents)))))
2371 (nreverse contents))))
2373 (defun spam-from-listed-p (type)
2374 (let ((from (message-fetch-field "from"))
2376 (spam-filelist-check-cache type from)))
2378 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2379 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2380 (declassification (if blacklist 'ham 'spam))
2382 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2384 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2385 from addresses unregister-list article-unregister-list)
2386 (dolist (article articles)
2387 (let ((from (spam-fetch-field-from-fast article))
2388 (id (spam-fetch-field-message-id-fast article))
2390 (when (stringp from)
2391 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2392 (when (and (not sender-ignored)
2393 (stringp ignore-regex)
2394 (string-match ignore-regex from))
2395 (setq sender-ignored t)))
2396 ;; remember the messages we need to unregister, unless remove is set
2399 (spam-log-unregistration-needed-p
2400 id 'process declassification de-symbol))
2401 (push article article-unregister-list)
2402 (push from unregister-list))
2403 (unless sender-ignored
2404 (push from addresses)))))
2407 (funcall enter-function addresses t) ; unregister all these addresses
2408 ;; else, register normally and unregister what we need to
2409 (funcall remove-function unregister-list t)
2410 (dolist (article article-unregister-list)
2411 (spam-log-undo-registration
2412 (spam-fetch-field-message-id-fast article)
2416 (funcall enter-function addresses nil))))
2418 (defun spam-blacklist-unregister-routine (articles)
2419 (spam-blacklist-register-routine articles t))
2421 (defun spam-blacklist-register-routine (articles &optional unregister)
2422 (spam-filelist-register-routine articles t unregister))
2424 (defun spam-whitelist-unregister-routine (articles)
2425 (spam-whitelist-register-routine articles t))
2427 (defun spam-whitelist-register-routine (articles &optional unregister)
2428 (spam-filelist-register-routine articles nil unregister))
2432 ;;{{{ Spam-report glue (gmane and resend reporting)
2433 (defun spam-report-gmane-register-routine (articles)
2435 (apply 'spam-report-gmane articles)))
2437 (defun spam-report-resend-register-ham-routine (articles)
2438 (spam-report-resend-register-routine articles t))
2440 (defun spam-report-resend-register-routine (articles &optional ham)
2441 (let* ((resend-to-gp
2443 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2444 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2445 (spam-report-resend-to (or (car-safe resend-to-gp)
2446 spam-report-resend-to)))
2447 (spam-report-resend articles ham)))
2452 (defun spam-check-bogofilter-headers (&optional score)
2453 (let ((header (message-fetch-field spam-bogofilter-header)))
2454 (when header ; return nil when no header
2455 (if score ; scoring mode
2456 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2457 (match-string 1 header)
2459 ;; spam detection mode
2460 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2462 spam-split-group)))))
2464 ;; return something sensible if the score can't be determined
2465 (defun spam-bogofilter-score (&optional recheck)
2466 "Get the Bogofilter spamicity score"
2468 (save-window-excursion
2469 (gnus-summary-show-article t)
2470 (set-buffer gnus-article-buffer)
2471 (let ((score (or (unless recheck
2472 (spam-check-bogofilter-headers t))
2473 (spam-check-bogofilter t))))
2474 (gnus-summary-show-article)
2475 (message "Spamicity score %s" score)
2478 (defun spam-verify-bogofilter ()
2479 "Verify the Bogofilter version is sufficient."
2480 (when (eq spam-bogofilter-valid 'unknown)
2481 (setq spam-bogofilter-valid
2482 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2483 (shell-command-to-string
2484 (format "%s -V" spam-bogofilter-path))))))
2485 spam-bogofilter-valid)
2487 (defun spam-check-bogofilter (&optional score)
2488 "Check the Bogofilter backend for the classification of this message."
2489 (if (spam-verify-bogofilter)
2490 (let ((article-buffer-name (buffer-name))
2491 (db spam-bogofilter-database-directory)
2494 (let ((temp-buffer-name (buffer-name)))
2496 (set-buffer article-buffer-name)
2497 (apply 'call-process-region
2498 (point-min) (point-max)
2499 spam-bogofilter-path
2500 nil temp-buffer-name nil
2501 (if db `("-d" ,db "-v") `("-v"))))
2502 (setq return (spam-check-bogofilter-headers score))))
2504 (gnus-error "`spam.el' doesnt support obsolete bogofilter versions")))
2506 (defun spam-bogofilter-register-with-bogofilter (articles
2508 &optional unregister)
2509 "Register an article, given as a string, as spam or non-spam."
2510 (if (spam-verify-bogofilter)
2511 (dolist (article articles)
2512 (let ((article-string (spam-get-article-as-string article))
2513 (db spam-bogofilter-database-directory)
2514 (switch (if unregister
2516 spam-bogofilter-spam-strong-switch
2517 spam-bogofilter-ham-strong-switch)
2519 spam-bogofilter-spam-switch
2520 spam-bogofilter-ham-switch))))
2521 (when (stringp article-string)
2523 (insert article-string)
2525 (apply 'call-process-region
2526 (point-min) (point-max)
2527 spam-bogofilter-path
2529 (if db `("-d" ,db "-v") `("-v")))))))
2530 (gnus-error "`spam.el' doesnt support obsolete bogofilter versions")))
2532 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2533 (spam-bogofilter-register-with-bogofilter articles t unregister))
2535 (defun spam-bogofilter-unregister-spam-routine (articles)
2536 (spam-bogofilter-register-spam-routine articles t))
2538 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2539 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2541 (defun spam-bogofilter-unregister-ham-routine (articles)
2542 (spam-bogofilter-register-ham-routine articles t))
2548 (defun spam-check-spamoracle ()
2549 "Run spamoracle on an article to determine whether it's spam."
2550 (let ((article-buffer-name (buffer-name)))
2552 (let ((temp-buffer-name (buffer-name)))
2554 (set-buffer article-buffer-name)
2556 (apply 'call-process-region
2557 (point-min) (point-max)
2558 spam-spamoracle-binary
2559 nil temp-buffer-name nil
2560 (if spam-spamoracle-database
2561 `("-f" ,spam-spamoracle-database "mark")
2565 (set-buffer temp-buffer-name)
2566 (goto-char (point-min))
2567 (when (re-search-forward "^X-Spam: yes;" nil t)
2569 (error "Error running spamoracle: %s" status))))))))
2571 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2572 "Run spamoracle in training mode."
2574 (let ((temp-buffer-name (buffer-name)))
2576 (goto-char (point-min))
2577 (dolist (article articles)
2578 (insert (spam-get-article-as-string article)))
2579 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2583 (apply 'call-process-region
2584 (point-min) (point-max)
2585 spam-spamoracle-binary
2586 nil temp-buffer-name nil
2587 (if spam-spamoracle-database
2588 `("-f" ,spam-spamoracle-database
2591 (unless (eq 0 status)
2592 (error "Error running spamoracle: %s" status)))))))
2594 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2595 (spam-spamoracle-learn articles nil unregister))
2597 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2598 (spam-spamoracle-learn-ham articles t))
2600 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2601 (spam-spamoracle-learn articles t unregister))
2603 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2604 (spam-spamoracle-learn-spam articles t))
2609 ;;; based mostly on the bogofilter code
2610 (defun spam-check-spamassassin-headers (&optional score)
2611 "Check the SpamAssassin headers for the classification of this message."
2612 (if score ; scoring mode
2613 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2615 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2616 (match-string 1 header)
2618 ;; spam detection mode
2619 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2620 (when header ; return nil when no header
2621 (when (string-match spam-spamassassin-positive-spam-flag-header
2623 spam-split-group)))))
2625 (defun spam-check-spamassassin (&optional score)
2626 "Check the SpamAssassin backend for the classification of this message."
2627 (let ((article-buffer-name (buffer-name)))
2629 (let ((temp-buffer-name (buffer-name)))
2631 (set-buffer article-buffer-name)
2632 (apply 'call-process-region
2633 (point-min) (point-max) spam-spamassassin-path
2634 nil temp-buffer-name nil spam-spamassassin-arguments))
2635 ;; check the return now (we're back in the temp buffer)
2636 (goto-char (point-min))
2637 (spam-check-spamassassin-headers score)))))
2639 ;; return something sensible if the score can't be determined
2640 (defun spam-spamassassin-score (&optional recheck)
2641 "Get the SpamAssassin score"
2643 (save-window-excursion
2644 (gnus-summary-show-article t)
2645 (set-buffer gnus-article-buffer)
2646 (let ((score (or (unless recheck
2647 (spam-check-spamassassin-headers t))
2648 (spam-check-spamassassin t))))
2649 (gnus-summary-show-article)
2650 (message "SpamAssassin score %s" score)
2653 (defun spam-spamassassin-register-with-sa-learn (articles spam
2654 &optional unregister)
2655 "Register articles with spamassassin's sa-learn as spam or non-spam."
2657 (let ((action (if unregister spam-sa-learn-unregister-switch
2658 (if spam spam-sa-learn-spam-switch
2659 spam-sa-learn-ham-switch)))
2660 (summary-buffer-name (buffer-name)))
2662 ;; group the articles into mbox format
2663 (dolist (article articles)
2664 (let (article-string)
2666 (set-buffer summary-buffer-name)
2667 (setq article-string (spam-get-article-as-string article)))
2668 (when (stringp article-string)
2669 (insert "From \n") ; mbox separator (sa-learn only checks the
2670 ; first five chars, so we can get away with
2672 (insert article-string)
2674 ;; call sa-learn on all messages at the same time
2675 (apply 'call-process-region
2676 (point-min) (point-max)
2678 nil nil nil "--mbox"
2679 (if spam-sa-learn-rebuild
2681 `("--no-rebuild" ,action)))))))
2683 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2684 (spam-spamassassin-register-with-sa-learn articles t unregister))
2686 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2687 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2689 (defun spam-spamassassin-unregister-spam-routine (articles)
2690 (spam-spamassassin-register-with-sa-learn articles t t))
2692 (defun spam-spamassassin-unregister-ham-routine (articles)
2693 (spam-spamassassin-register-with-sa-learn articles nil t))
2698 ;;; based mostly on the bogofilter code
2699 (defun spam-check-bsfilter-headers (&optional score)
2701 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2703 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2704 (when header ; return nil when no header
2705 (when (string-match "YES" header)
2706 spam-split-group)))))
2708 ;; return something sensible if the score can't be determined
2709 (defun spam-bsfilter-score (&optional recheck)
2710 "Get the Bsfilter spamicity score"
2712 (save-window-excursion
2713 (gnus-summary-show-article t)
2714 (set-buffer gnus-article-buffer)
2715 (let ((score (or (unless recheck
2716 (spam-check-bsfilter-headers t))
2717 (spam-check-bsfilter t))))
2718 (gnus-summary-show-article)
2719 (message "Spamicity score %s" score)
2722 (defun spam-check-bsfilter (&optional score)
2723 "Check the Bsfilter backend for the classification of this message"
2724 (let ((article-buffer-name (buffer-name))
2725 (dir spam-bsfilter-database-directory)
2728 (let ((temp-buffer-name (buffer-name)))
2730 (set-buffer article-buffer-name)
2731 (apply 'call-process-region
2732 (point-min) (point-max)
2734 nil temp-buffer-name nil
2737 "--insert-probability"
2739 (list "--homedir" dir))))
2740 (setq return (spam-check-bsfilter-headers score))))
2743 (defun spam-bsfilter-register-with-bsfilter (articles
2745 &optional unregister)
2746 "Register an article, given as a string, as spam or non-spam."
2747 (dolist (article articles)
2748 (let ((article-string (spam-get-article-as-string article))
2749 (switch (if unregister
2751 spam-bsfilter-spam-strong-switch
2752 spam-bsfilter-ham-strong-switch)
2754 spam-bsfilter-spam-switch
2755 spam-bsfilter-ham-switch))))
2756 (when (stringp article-string)
2758 (insert article-string)
2759 (apply 'call-process-region
2760 (point-min) (point-max)
2764 (when spam-bsfilter-database-directory
2766 spam-bsfilter-database-directory))))))))
2768 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2769 (spam-bsfilter-register-with-bsfilter articles t unregister))
2771 (defun spam-bsfilter-unregister-spam-routine (articles)
2772 (spam-bsfilter-register-spam-routine articles t))
2774 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2775 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2777 (defun spam-bsfilter-unregister-ham-routine (articles)
2778 (spam-bsfilter-register-ham-routine articles t))
2782 ;;{{{ CRM114 Mailfilter
2783 (defun spam-check-crm114-headers (&optional score)
2784 (let ((header (message-fetch-field spam-crm114-header)))
2785 (when header ; return nil when no header
2786 (if score ; scoring mode
2787 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2788 (match-string 1 header)
2790 ;; spam detection mode
2791 (when (string-match spam-crm114-positive-spam-header
2793 spam-split-group)))))
2795 ;; return something sensible if the score can't be determined
2796 (defun spam-crm114-score ()
2797 "Get the CRM114 Mailfilter pR"
2799 (save-window-excursion
2800 (gnus-summary-show-article t)
2801 (set-buffer gnus-article-buffer)
2802 (let ((score (or (spam-check-crm114-headers t)
2803 (spam-check-crm114 t))))
2804 (gnus-summary-show-article)
2805 (message "pR: %s" score)
2808 (defun spam-check-crm114 (&optional score)
2809 "Check the CRM114 Mailfilter backend for the classification of this message"
2810 (let ((article-buffer-name (buffer-name))
2811 (db spam-crm114-database-directory)
2814 (let ((temp-buffer-name (buffer-name)))
2816 (set-buffer article-buffer-name)
2817 (apply 'call-process-region
2818 (point-min) (point-max)
2820 nil temp-buffer-name nil
2821 (when db (list (concat "--fileprefix=" db)))))
2822 (setq return (spam-check-crm114-headers score))))
2825 (defun spam-crm114-register-with-crm114 (articles
2827 &optional unregister)
2828 "Register an article, given as a string, as spam or non-spam."
2829 (dolist (article articles)
2830 (let ((article-string (spam-get-article-as-string article))
2831 (db spam-crm114-database-directory)
2832 (switch (if unregister
2834 spam-crm114-spam-strong-switch
2835 spam-crm114-ham-strong-switch)
2837 spam-crm114-spam-switch
2838 spam-crm114-ham-switch))))
2839 (when (stringp article-string)
2841 (insert article-string)
2843 (apply 'call-process-region
2844 (point-min) (point-max)
2847 (when db (list switch (concat "--fileprefix=" db)))))))))
2849 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2850 (spam-crm114-register-with-crm114 articles t unregister))
2852 (defun spam-crm114-unregister-spam-routine (articles)
2853 (spam-crm114-register-spam-routine articles t))
2855 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2856 (spam-crm114-register-with-crm114 articles nil unregister))
2858 (defun spam-crm114-unregister-ham-routine (articles)
2859 (spam-crm114-register-ham-routine articles t))
2868 (defun spam-initialize (&rest symbols)
2869 "Install the spam.el hooks and do other initialization.
2870 When SYMBOLS is given, set those variables to t. This is so you
2871 can call spam-initialize before you set spam-use-* variables on
2872 explicitly, and matters only if you need the extra headers
2873 installed through spam-necessary-extra-headers."
2876 (dolist (var symbols)
2879 (dolist (header (spam-necessary-extra-headers))
2880 (add-to-list 'nnmail-extra-headers header)
2881 (add-to-list 'gnus-extra-headers header))
2883 (setq spam-install-hooks t)
2884 ;; TODO: How do we redo this every time spam-face is customized?
2885 (push '((eq mark gnus-spam-mark) . spam-face)
2886 gnus-summary-highlight)
2887 ;; Add hooks for loading and saving the spam stats
2888 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2889 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2890 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2891 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2892 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2893 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2894 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2896 (defun spam-unload-hook ()
2897 "Uninstall the spam.el hooks"
2899 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2900 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2901 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2902 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2903 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2904 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2905 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2907 (add-hook 'spam-unload-hook 'spam-unload-hook)
2909 (when spam-install-hooks
2915 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
2916 ;;; spam.el ends here