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.")
85 (defcustom spam-summary-exit-behavior 'default
86 "Exit behavior at the time of summary exit.
87 Note that setting the spam-use-move or spam-use-copy backends on
88 a group through group/topic parameters overrides this mechanism."
89 :type '(choice (const 'default :tag
90 "Move spam out of all groups. Move ham out of spam groups.")
92 "Move spam out of all groups. Move ham out of all groups.")
93 (const 'move-none :tag
94 "Never move spam or ham out of any groups."))
97 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
98 "Directory for spam whitelists and blacklists."
102 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
103 "Whether new messages in a spam group should get the spam-mark."
107 (defcustom spam-log-to-registry nil
108 "Whether spam/ham processing should be logged in the registry."
112 (defcustom spam-split-symbolic-return nil
113 "Whether `spam-split' should work with symbols or group names."
117 (defcustom spam-split-symbolic-return-positive nil
118 "Whether `spam-split' should ALWAYS work with symbols or group names.
119 Do not set this if you use `spam-split' in a fancy split
124 (defcustom spam-mark-only-unseen-as-spam t
125 "Whether only unseen articles should be marked as spam in spam groups.
126 When nil, all unread articles in a spam group are marked as
127 spam. Set this if you want to leave an article unread in a spam group
128 without losing it to the automatic spam-marking process."
132 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
133 "Whether ham should be marked unread before it's moved.
134 The article is moved out of a spam group according to ham-process-destination.
135 This variable is an official entry in the international Longest Variable Name
140 (defcustom spam-disable-spam-split-during-ham-respool nil
141 "Whether `spam-split' should be ignored while resplitting ham.
142 This is useful to prevent ham from ending up in the same spam
143 group after the resplit. Don't set this to t if you have `spam-split' as the
144 last rule in your split configuration."
148 (defcustom spam-autodetect-recheck-messages nil
149 "Should spam.el recheck all meessages when autodetecting?
150 Normally this is nil, so only unseen messages will be checked."
154 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
155 "The location of the whitelist.
156 The file format is one regular expression per line.
157 The regular expression is matched against the address."
161 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
162 "The location of the blacklist.
163 The file format is one regular expression per line.
164 The regular expression is matched against the address."
168 (defcustom spam-use-dig t
169 "Whether `query-dig' should be used instead of `query-dns'."
173 (defcustom spam-use-gmane-xref nil
174 "Whether the Gmane spam xref should be used by `spam-split'."
178 (defcustom spam-use-blacklist nil
179 "Whether the blacklist should be used by `spam-split'."
183 (defcustom spam-blacklist-ignored-regexes nil
184 "Regular expressions that the blacklist should ignore."
185 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
188 (defcustom spam-use-whitelist nil
189 "Whether the whitelist should be used by `spam-split'."
193 (defcustom spam-use-whitelist-exclusive nil
194 "Whether whitelist-exclusive should be used by `spam-split'.
195 Exclusive whitelisting means that all messages from senders not in the whitelist
196 are considered spam."
200 (defcustom spam-use-blackholes nil
201 "Whether blackholes should be used by `spam-split'."
205 (defcustom spam-use-hashcash nil
206 "Whether hashcash payments should be detected by `spam-split'."
210 (defcustom spam-use-regex-headers nil
211 "Whether a header regular expression match should be used by `spam-split'.
212 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
216 (defcustom spam-use-regex-body nil
217 "Whether a body regular expression match should be used by `spam-split'.
218 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
222 (defcustom spam-use-bogofilter-headers nil
223 "Whether bogofilter headers should be used by `spam-split'.
224 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
228 (defcustom spam-use-bogofilter nil
229 "Whether bogofilter should be invoked by `spam-split'.
230 Enable this if you want Gnus to invoke Bogofilter on new messages."
234 (defcustom spam-use-bsfilter-headers nil
235 "Whether bsfilter headers should be used by `spam-split'.
236 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
240 (defcustom spam-use-bsfilter nil
241 "Whether bsfilter should be invoked by `spam-split'.
242 Enable this if you want Gnus to invoke Bsfilter on new messages."
246 (defcustom spam-use-BBDB nil
247 "Whether BBDB should be used by `spam-split'."
251 (defcustom spam-use-BBDB-exclusive nil
252 "Whether BBDB-exclusive should be used by `spam-split'.
253 Exclusive BBDB means that all messages from senders not in the BBDB are
258 (defcustom spam-use-ifile nil
259 "Whether ifile should be used by `spam-split'."
263 (defcustom spam-use-stat nil
264 "Whether `spam-stat' should be used by `spam-split'."
268 (defcustom spam-use-spamoracle nil
269 "Whether spamoracle should be used by `spam-split'."
273 (defcustom spam-use-spamassassin nil
274 "Whether spamassassin should be invoked by `spam-split'.
275 Enable this if you want Gnus to invoke SpamAssassin on new messages."
279 (defcustom spam-use-spamassassin-headers nil
280 "Whether spamassassin headers should be checked by `spam-split'.
281 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
286 (defcustom spam-use-crm114 nil
287 "Whether the CRM114 Mailfilter should be used by `spam-split'."
291 (defcustom spam-install-hooks (or
296 spam-use-whitelist-exclusive
299 spam-use-regex-headers
302 spam-use-bogofilter-headers
303 spam-use-spamassassin
304 spam-use-spamassassin-headers
306 spam-use-bsfilter-headers
308 spam-use-BBDB-exclusive
313 "Whether the spam hooks should be installed.
314 Default to t if one of the spam-use-* variables is set."
318 (defcustom spam-split-group "spam"
319 "Group name where incoming spam should be put by `spam-split'."
323 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
324 ;;; not regular expressions
325 (defcustom spam-junk-mailgroups (cons
327 '("mail.junk" "poste.pourriel"))
328 "Mailgroups with spam contents.
329 All unmarked article in such group receive the spam mark on group entry."
330 :type '(repeat (string :tag "Group"))
334 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
335 "The group where spam xrefs can be found on Gmane.
336 Only meaningful if you enable `spam-use-gmane-xref'."
340 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
341 "dev.null.dk" "relays.visi.com")
342 "List of blackhole servers.
343 Only meaningful if you enable `spam-use-blackholes'."
344 :type '(repeat (string :tag "Server"))
347 (defcustom spam-blackhole-good-server-regex nil
348 "String matching IP addresses that should not be checked in the blackholes.
349 Only meaningful if you enable `spam-use-blackholes'."
350 :type '(radio (const nil) regexp)
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 (defcustom spam-summary-score-preferred-header nil
383 "Preferred header to use for spam-summary-score."
384 :type '(choice :tag "Header name"
385 (symbol :tag "SpamAssassin etc" X-Spam-Status)
386 (symbol :tag "Bogofilter" X-Bogosity)
387 (const :tag "No preference, take best guess." nil))
390 (defgroup spam-ifile nil
391 "Spam ifile configuration."
394 (defcustom spam-ifile-path (exec-installed-p "ifile")
395 "File path of the ifile executable program."
396 :type '(choice (file :tag "Location of ifile")
397 (const :tag "ifile is not installed"))
400 (defcustom spam-ifile-database-path nil
401 "File path of the ifile database."
402 :type '(choice (file :tag "Location of the ifile database")
403 (const :tag "Use the default"))
406 (defcustom spam-ifile-spam-category "spam"
407 "Name of the spam ifile category."
411 (defcustom spam-ifile-ham-category nil
412 "Name of the ham ifile category.
413 If nil, the current group name will be used."
414 :type '(choice (string :tag "Use a fixed category")
415 (const :tag "Use the current group name"))
418 (defcustom spam-ifile-all-categories nil
419 "Whether the ifile check will return all categories, or just spam.
420 Set this to t if you want to use the `spam-split' invocation of ifile as
421 your main source of newsgroup names."
425 (defgroup spam-bogofilter nil
426 "Spam bogofilter configuration."
429 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
430 "File path of the Bogofilter executable program."
431 :type '(choice (file :tag "Location of bogofilter")
432 (const :tag "Bogofilter is not installed"))
433 :group 'spam-bogofilter)
435 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
437 (defcustom spam-bogofilter-header "X-Bogosity"
438 "The header that Bogofilter inserts in messages."
440 :group 'spam-bogofilter)
442 (defcustom spam-bogofilter-spam-switch "-s"
443 "The switch that Bogofilter uses to register spam messages."
445 :group 'spam-bogofilter)
447 (defcustom spam-bogofilter-ham-switch "-n"
448 "The switch that Bogofilter uses to register ham messages."
450 :group 'spam-bogofilter)
452 (defcustom spam-bogofilter-spam-strong-switch "-S"
453 "The switch that Bogofilter uses to unregister ham messages."
455 :group 'spam-bogofilter)
457 (defcustom spam-bogofilter-ham-strong-switch "-N"
458 "The switch that Bogofilter uses to unregister spam messages."
460 :group 'spam-bogofilter)
462 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
463 "The regex on `spam-bogofilter-header' for positive spam identification."
465 :group 'spam-bogofilter)
467 (defcustom spam-bogofilter-database-directory nil
468 "Directory path of the Bogofilter databases."
469 :type '(choice (directory
470 :tag "Location of the Bogofilter database directory")
471 (const :tag "Use the default"))
472 :group 'spam-bogofilter)
474 (defgroup spam-bsfilter nil
475 "Spam bsfilter configuration."
478 (defcustom spam-bsfilter-path (executable-find "bsfilter")
479 "File path of the Bsfilter executable program."
480 :type '(choice (file :tag "Location of bsfilter")
481 (const :tag "Bsfilter is not installed"))
482 :group 'spam-bsfilter)
484 (defcustom spam-bsfilter-header "X-Spam-Flag"
485 "The header inserted by Bsfilter to flag spam."
487 :group 'spam-bsfilter)
489 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
490 "The header that Bsfilter inserts in messages."
492 :group 'spam-bsfilter)
494 (defcustom spam-bsfilter-spam-switch "--add-spam"
495 "The switch that Bsfilter uses to register spam messages."
497 :group 'spam-bsfilter)
499 (defcustom spam-bsfilter-ham-switch "--add-ham"
500 "The switch that Bsfilter uses to register ham messages."
502 :group 'spam-bsfilter)
504 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
505 "The switch that Bsfilter uses to unregister ham messages."
507 :group 'spam-bsfilter)
509 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
510 "The switch that Bsfilter uses to unregister spam messages."
512 :group 'spam-bsfilter)
514 (defcustom spam-bsfilter-database-directory nil
515 "Directory path of the Bsfilter databases."
516 :type '(choice (directory
517 :tag "Location of the Bsfilter database directory")
518 (const :tag "Use the default"))
519 :group 'spam-bsfilter)
521 (defgroup spam-spamoracle nil
522 "Spam spamoracle configuration."
525 (defcustom spam-spamoracle-database nil
526 "Location of spamoracle database file.
527 When nil, use the default spamoracle database."
528 :type '(choice (directory :tag "Location of spamoracle database file.")
529 (const :tag "Use the default"))
530 :group 'spam-spamoracle)
532 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
533 "Location of the spamoracle binary."
534 :type '(choice (directory :tag "Location of the spamoracle binary")
535 (const :tag "Use the default"))
536 :group 'spam-spamoracle)
538 (defgroup spam-spamassassin nil
539 "Spam SpamAssassin configuration."
542 (defcustom spam-spamassassin-path (executable-find "spamassassin")
543 "File path of the spamassassin executable program.
544 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
545 spamd man pages for more information on these programs."
546 :type '(choice (file :tag "Location of spamc")
547 (const :tag "spamassassin is not installed"))
548 :group 'spam-spamassassin)
550 (defcustom spam-spamassassin-arguments ()
551 "Arguments to pass to the spamassassin executable.
552 This must be a list. For example, `(\"-C\" \"configfile\")'."
553 :type '(restricted-sexp :match-alternatives (listp))
554 :group 'spam-spamassassin)
556 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
557 "The header inserted by SpamAssassin to flag spam."
559 :group 'spam-spamassassin)
561 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
562 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
565 :group 'spam-spamassassin)
567 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
568 "The header inserted by SpamAssassin, giving extended scoring information"
570 :group 'spam-spamassassin)
572 (defcustom spam-sa-learn-path (executable-find "sa-learn")
573 "File path of the sa-learn executable program."
574 :type '(choice (file :tag "Location of spamassassin")
575 (const :tag "spamassassin is not installed"))
576 :group 'spam-spamassassin)
578 (defcustom spam-sa-learn-rebuild t
579 "Whether sa-learn should rebuild the database every time it is called
580 Enable this if you want sa-learn to rebuild the database automatically. Doing
581 this will slightly increase the running time of the spam registration process.
582 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
583 order for SpamAssassin to recognize the new registered spam."
585 :group 'spam-spamassassin)
587 (defcustom spam-sa-learn-spam-switch "--spam"
588 "The switch that sa-learn uses to register spam messages"
590 :group 'spam-spamassassin)
592 (defcustom spam-sa-learn-ham-switch "--ham"
593 "The switch that sa-learn uses to register ham messages"
595 :group 'spam-spamassassin)
597 (defcustom spam-sa-learn-unregister-switch "--forget"
598 "The switch that sa-learn uses to unregister messages messages"
600 :group 'spam-spamassassin)
602 (defgroup spam-crm114 nil
603 "Spam CRM114 Mailfilter configuration."
606 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
607 "File path of the CRM114 Mailfilter executable program."
608 :type '(choice (file :tag "Location of CRM114 Mailfilter")
609 (const :tag "CRM114 Mailfilter is not installed"))
612 (defcustom spam-crm114-header "X-CRM114-Status"
613 "The header that CRM114 Mailfilter inserts in messages."
617 (defcustom spam-crm114-spam-switch "--learnspam"
618 "The switch that CRM114 Mailfilter uses to register spam messages."
622 (defcustom spam-crm114-ham-switch "--learnnonspam"
623 "The switch that CRM114 Mailfilter uses to register ham messages."
627 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
628 "The switch that CRM114 Mailfilter uses to unregister ham messages."
632 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
633 "The switch that CRM114 Mailfilter uses to unregister spam messages."
637 (defcustom spam-crm114-positive-spam-header "^SPAM"
638 "The regex on `spam-crm114-header' for positive spam identification."
642 (defcustom spam-crm114-database-directory nil
643 "Directory path of the CRM114 Mailfilter databases."
644 :type '(choice (directory
645 :tag "Location of the CRM114 Mailfilter database directory")
646 (const :tag "Use the default"))
649 ;;; Key bindings for spam control.
651 (gnus-define-keys gnus-summary-mode-map
652 "St" spam-generic-score
653 "Sx" gnus-summary-mark-as-spam
654 "Mst" spam-generic-score
655 "Msx" gnus-summary-mark-as-spam
656 "\M-d" gnus-summary-mark-as-spam)
658 (defvar spam-cache-lookups t
659 "Whether spam.el will try to cache lookups using `spam-caches'.")
661 (defvar spam-caches (make-hash-table
664 "Cache of spam detection entries.")
666 (defvar spam-old-articles nil
667 "List of old ham and spam articles, generated when a group is entered.")
669 (defvar spam-split-disabled nil
670 "If non-nil, `spam-split' is disabled, and always returns nil.")
672 (defvar spam-split-last-successful-check nil
674 `spam-split' will set this to nil or a spam-use-XYZ check if it
677 ;; internal variables for backends
678 ;; TODO: find a way to create these on the fly in spam-install-backend-super
679 (defvar spam-use-copy nil)
680 (defvar spam-use-move nil)
681 (defvar spam-use-gmane nil)
682 (defvar spam-use-resend nil)
686 ;;{{{ convenience functions
688 (defun spam-clear-cache (symbol)
689 "Clear the spam-caches entry for a check."
690 (remhash symbol spam-caches))
692 (defun spam-xor (a b)
694 (and (or a b) (not (and a b))))
696 (defun spam-set-difference (list1 list2)
697 "Return a set difference of LIST1 and LIST2.
698 When either list is nil, the other is returned."
699 (if (and list1 list2)
700 ;; we have two non-nil lists
702 (dolist (item (append list1 list2))
703 (when (and (memq item list1) (memq item list2))
704 (setq list1 (delq item list1))
705 (setq list2 (delq item list2))))
706 (append list1 list2))
707 ;; if either of the lists was nil, return the other one
708 (if list1 list1 list2)))
710 (defun spam-group-ham-mark-p (group mark &optional spam)
711 "Checks if MARK is considered a ham mark in GROUP."
712 (when (stringp group)
713 (let* ((marks (spam-group-ham-marks group spam))
714 (marks (if (symbolp mark)
716 (mapcar 'symbol-value marks))))
719 (defun spam-group-spam-mark-p (group mark)
720 "Checks if MARK is considered a spam mark in GROUP."
721 (spam-group-ham-mark-p group mark t))
723 (defun spam-group-ham-marks (group &optional spam)
724 "In GROUP, get all the ham marks."
725 (when (stringp group)
726 (let* ((marks (if spam
727 (gnus-parameter-spam-marks group)
728 (gnus-parameter-ham-marks group)))
730 (marks (if (listp (car marks)) (car marks) marks)))
733 (defun spam-group-spam-marks (group)
734 "In GROUP, get all the spam marks."
735 (spam-group-ham-marks group t))
737 (defun spam-group-spam-contents-p (group)
738 "Is GROUP a spam group?"
739 (if (and (stringp group) (< 0 (length group)))
740 (or (member group spam-junk-mailgroups)
741 (memq 'gnus-group-spam-classification-spam
742 (gnus-parameter-spam-contents group)))
745 (defun spam-group-ham-contents-p (group)
746 "Is GROUP a ham group?"
748 (memq 'gnus-group-spam-classification-ham
749 (gnus-parameter-spam-contents group))
752 (defun spam-classifications ()
753 "Return list of valid classifications"
756 (defun spam-classification-valid-p (classification)
757 "Is CLASSIFICATION a valid spam/ham classification?"
758 (memq classification (spam-classifications)))
760 (defun spam-backend-properties ()
761 "Return list of valid classifications."
762 '(statistical mover check hrf srf huf suf))
764 (defun spam-backend-property-valid-p (property)
765 "Is PROPERTY a valid backend property?"
766 (memq property (spam-backend-properties)))
768 (defun spam-backend-function-type-valid-p (type)
769 (or (eq type 'registration)
770 (eq type 'unregistration)))
772 (defun spam-process-type-valid-p (process-type)
773 (or (eq process-type 'incoming)
774 (eq process-type 'process)))
776 (defun spam-list-articles (articles classification)
777 (let ((mark-check (if (eq classification 'spam)
778 'spam-group-spam-mark-p
779 'spam-group-ham-mark-p))
780 alist mark-cache-yes mark-cache-no)
781 (dolist (article articles)
782 (let ((mark (gnus-summary-article-mark article)))
783 (unless (or (memq mark mark-cache-yes)
784 (memq mark mark-cache-no))
785 (if (funcall mark-check
788 (push mark mark-cache-yes)
789 (push mark mark-cache-no)))
790 (when (memq mark mark-cache-yes)
791 (push article alist))))
796 ;;{{{ backend installation functions and procedures
798 (defun spam-install-backend-super (backend &rest properties)
799 "Install BACKEND for spam.el.
800 Accepts incoming CHECK, ham registration function HRF, spam
801 registration function SRF, ham unregistration function HUF, spam
802 unregistration function SUF, and an indication whether the
803 backend is STATISTICAL."
805 (setq spam-backends (add-to-list 'spam-backends backend))
807 (let ((property (pop properties))
808 (value (pop properties)))
809 (if (spam-backend-property-valid-p property)
810 (put backend property value)
813 "spam-install-backend-super got an invalid property %s"
816 (defun spam-backend-list (&optional type)
817 "Return a list of all the backend symbols, constrained by TYPE.
818 When TYPE is 'non-mover, only non-mover backends are returned.
819 When TYPE is 'mover, only mover backends are returned."
821 (dolist (backend spam-backends)
823 (null type) ;either no type was requested
824 ;; or the type is 'mover and the backend is a mover
827 (spam-backend-mover-p backend))
828 ;; or the type is 'non-mover and the backend is not a mover
831 (not (spam-backend-mover-p backend))))
832 (push backend list)))
835 (defun spam-backend-check (backend)
836 "Get the check function for BACKEND.
837 Each individual check may return nil, t, or a mailgroup name.
838 The value nil means that the check does not yield a decision, and
839 so, that further checks are needed. The value t means that the
840 message is definitely not spam, and that further spam checks
841 should be inhibited. Otherwise, a mailgroup name or the symbol
842 'spam (depending on spam-split-symbolic-return) is returned where
843 the mail should go, and further checks are also inhibited. The
844 usual mailgroup name is the value of `spam-split-group', meaning
845 that the message is definitely a spam."
846 (get backend 'check))
848 (defun spam-backend-valid-p (backend)
850 (member backend (spam-backend-list)))
852 (defun spam-backend-info (backend)
853 "Return information about BACKEND."
854 (if (spam-backend-valid-p backend)
856 (setq info (format "Backend %s has the following properties:\n"
858 (dolist (property (spam-backend-properties))
859 (setq info (format "%s%s=%s\n"
862 (get backend property))))
864 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
867 (defun spam-backend-function (backend classification type)
868 "Get the BACKEND function for CLASSIFICATION and TYPE.
869 TYPE is 'registration or 'unregistration.
870 CLASSIFICATION is 'ham or 'spam."
872 (spam-classification-valid-p classification)
873 (spam-backend-function-type-valid-p type))
876 (format "spam-backend-%s-%s-function"
879 (funcall retrieval backend))
882 "%s was passed invalid backend %s, classification %s, or type %s"
883 "spam-backend-function"
888 (defun spam-backend-article-list-property (classification
889 &optional unregister)
890 "Property name of article list with CLASSIFICATION and UNREGISTER."
891 (let* ((r (if unregister "unregister" "register"))
892 (prop (format "%s-%s" classification r)))
895 (defun spam-backend-get-article-todo-list (backend
897 &optional unregister)
898 "Get the articles to be processed for BACKEND and CLASSIFICATION.
899 With UNREGISTER, get articles to be unregistered.
900 This is a temporary storage function - nothing here persists."
903 (intern (spam-backend-article-list-property classification unregister))))
905 (defun spam-backend-put-article-todo-list (backend classification list &optional unregister)
906 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
907 With UNREGISTER, set articles to be unregistered.
908 This is a temporary storage function - nothing here persists."
911 (intern (spam-backend-article-list-property classification unregister))
914 (defun spam-backend-ham-registration-function (backend)
915 "Get the ham registration function for BACKEND."
918 (defun spam-backend-spam-registration-function (backend)
919 "Get the spam registration function for BACKEND."
922 (defun spam-backend-ham-unregistration-function (backend)
923 "Get the ham unregistration function for BACKEND."
926 (defun spam-backend-spam-unregistration-function (backend)
927 "Get the spam unregistration function for BACKEND."
930 (defun spam-backend-statistical-p (backend)
931 "Is BACKEND statistical?"
932 (get backend 'statistical))
934 (defun spam-backend-mover-p (backend)
935 "Is BACKEND a mover?"
936 (get backend 'mover))
938 (defun spam-install-backend-alias (backend alias)
939 "Add ALIAS to an existing BACKEND.
940 The previous backend settings for ALIAS are erased."
942 ;; install alias with no properties at first
943 (spam-install-backend-super alias)
945 (dolist (property (spam-backend-properties))
946 (put alias property (get backend property))))
948 (defun spam-install-checkonly-backend (backend check)
949 "Install a BACKEND than can only CHECK for spam."
950 (spam-install-backend-super backend 'check check))
952 (defun spam-install-mover-backend (backend hrf srf huf suf)
953 "Install a BACKEND than can move articles at summary exit.
954 Accepts ham registration function HRF, spam registration function
955 SRF, ham unregistration function HUF, spam unregistration
956 function SUF. The backend has no incoming check and can't be
958 (spam-install-backend-super
960 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
962 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
963 "Install a BACKEND than has no check.
964 Accepts ham registration function HRF, spam registration function
965 SRF, ham unregistration function HUF, spam unregistration
966 function SUF. The backend has no incoming check and can't be
967 statistical (it could be, but in practice that doesn't happen)."
968 (spam-install-backend-super
970 'hrf hrf 'srf srf 'huf huf 'suf suf))
972 (defun spam-install-backend (backend check hrf srf huf suf)
974 Accepts incoming CHECK, ham registration function HRF, spam
975 registration function SRF, ham unregistration function HUF, spam
976 unregistration function SUF. The backend won't be
977 statistical (use spam-install-statistical-backend for that)."
978 (spam-install-backend-super
980 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
982 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
984 Accepts incoming CHECK, ham registration function HRF, spam
985 registration function SRF, ham unregistration function HUF, spam
986 unregistration function SUF. The backend will be
987 statistical (use spam-install-backend for non-statistical
989 (spam-install-backend-super
991 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
993 (defun spam-install-statistical-checkonly-backend (backend check)
994 "Install a statistical BACKEND than can only CHECK for spam."
995 (spam-install-backend-super
997 'check check 'statistical t))
1001 ;;{{{ backend installations
1002 (spam-install-checkonly-backend 'spam-use-blackholes
1003 'spam-check-blackholes)
1005 (spam-install-checkonly-backend 'spam-use-hashcash
1006 'spam-check-hashcash)
1008 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1009 'spam-check-spamassassin-headers)
1011 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1012 'spam-check-bogofilter-headers)
1014 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1015 'spam-check-bsfilter-headers)
1017 (spam-install-checkonly-backend 'spam-use-gmane-xref
1018 'spam-check-gmane-xref)
1020 (spam-install-checkonly-backend 'spam-use-regex-headers
1021 'spam-check-regex-headers)
1023 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1024 'spam-check-regex-body)
1026 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy) instead
1027 (spam-install-mover-backend 'spam-use-move
1028 'spam-move-ham-routine
1029 'spam-move-spam-routine
1033 (spam-install-nocheck-backend 'spam-use-copy
1034 'spam-copy-ham-routine
1035 'spam-copy-spam-routine
1039 (spam-install-nocheck-backend 'spam-use-gmane
1041 'spam-report-gmane-register-routine
1042 ;; does Gmane support unregistration?
1046 (spam-install-nocheck-backend 'spam-use-resend
1047 'spam-report-resend-register-ham-routine
1048 'spam-report-resend-register-routine
1052 (spam-install-backend 'spam-use-BBDB
1054 'spam-BBDB-register-routine
1056 'spam-BBDB-unregister-routine
1059 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1061 (spam-install-backend 'spam-use-blacklist
1062 'spam-check-blacklist
1064 'spam-blacklist-register-routine
1066 'spam-blacklist-unregister-routine)
1068 (spam-install-backend 'spam-use-whitelist
1069 'spam-check-whitelist
1070 'spam-whitelist-register-routine
1072 'spam-whitelist-unregister-routine
1075 (spam-install-statistical-backend 'spam-use-ifile
1077 'spam-ifile-register-ham-routine
1078 'spam-ifile-register-spam-routine
1079 'spam-ifile-unregister-ham-routine
1080 'spam-ifile-unregister-spam-routine)
1082 (spam-install-statistical-backend 'spam-use-spamoracle
1083 'spam-check-spamoracle
1084 'spam-spamoracle-learn-ham
1085 'spam-spamoracle-learn-spam
1086 'spam-spamoracle-unlearn-ham
1087 'spam-spamoracle-unlearn-spam)
1089 (spam-install-statistical-backend 'spam-use-stat
1091 'spam-stat-register-ham-routine
1092 'spam-stat-register-spam-routine
1093 'spam-stat-unregister-ham-routine
1094 'spam-stat-unregister-spam-routine)
1096 (spam-install-statistical-backend 'spam-use-spamassassin
1097 'spam-check-spamassassin
1098 'spam-spamassassin-register-ham-routine
1099 'spam-spamassassin-register-spam-routine
1100 'spam-spamassassin-unregister-ham-routine
1101 'spam-spamassassin-unregister-spam-routine)
1103 (spam-install-statistical-backend 'spam-use-bogofilter
1104 'spam-check-bogofilter
1105 'spam-bogofilter-register-ham-routine
1106 'spam-bogofilter-register-spam-routine
1107 'spam-bogofilter-unregister-ham-routine
1108 'spam-bogofilter-unregister-spam-routine)
1110 (spam-install-statistical-backend 'spam-use-bsfilter
1111 'spam-check-bsfilter
1112 'spam-bsfilter-register-ham-routine
1113 'spam-bsfilter-register-spam-routine
1114 'spam-bsfilter-unregister-ham-routine
1115 'spam-bsfilter-unregister-spam-routine)
1117 (spam-install-statistical-backend 'spam-use-crm114
1119 'spam-crm114-register-ham-routine
1120 'spam-crm114-register-spam-routine
1121 ;; does CRM114 Mailfilter support unregistration?
1127 ;;{{{ scoring and summary formatting
1128 (defun spam-necessary-extra-headers ()
1129 "Return the extra headers spam.el thinks are necessary."
1131 (when (or spam-use-spamassassin
1132 spam-use-spamassassin-headers
1133 spam-use-regex-headers)
1134 (push 'X-Spam-Status list))
1135 (when spam-use-bogofilter
1136 (push 'X-Bogosity list))
1139 (defun spam-user-format-function-S (headers)
1142 (spam-summary-score headers spam-summary-score-preferred-header))))
1144 (defun spam-article-sort-by-spam-status (h1 h2)
1145 "Sort articles by score."
1147 (dolist (header (spam-necessary-extra-headers))
1148 (let ((s1 (spam-summary-score h1 header))
1149 (s2 (spam-summary-score h2 header)))
1151 (setq result (< s1 s2))
1155 (defun spam-extra-header-to-number (header headers)
1156 "Transform an extra HEADER to a number, using list of HEADERS.
1157 Note this has to be fast."
1158 (if (gnus-extra-header header headers)
1160 ((eq header 'X-Spam-Status)
1161 (string-to-number (gnus-replace-in-string
1162 (gnus-extra-header header headers)
1164 ;; for CRM checking, it's probably faster to just do the string match
1165 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
1166 (match-string 1 header))
1167 ((eq header 'X-Bogosity)
1168 (string-to-number (gnus-replace-in-string
1169 (gnus-replace-in-string
1170 (gnus-extra-header header headers)
1176 (defun spam-summary-score (headers &optional specific-header)
1177 "Score an article for the summary buffer, as fast as possible.
1178 With SPECIFIC-HEADER, returns only that header's score.
1179 Will not return a nil score."
1183 (list specific-header)
1184 (spam-necessary-extra-headers)))
1186 (spam-extra-header-to-number header headers))
1191 (defun spam-generic-score (&optional recheck)
1192 "Invoke whatever scoring method we can."
1195 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1196 (spam-spamassassin-score recheck))
1197 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1198 (spam-bsfilter-score recheck))
1200 (spam-crm114-score))
1201 (t (spam-bogofilter-score recheck))))
1204 ;;{{{ set up widening, processor checks
1206 ;;; set up IMAP widening if it's necessary
1207 (defun spam-setup-widening ()
1208 (when (spam-widening-needed-p)
1209 (setq nnimap-split-download-body-default t)))
1211 (defun spam-widening-needed-p (&optional force-symbols)
1213 (dolist (backend (spam-backend-list))
1214 (when (and (spam-backend-statistical-p backend)
1215 (or (symbol-value backend)
1216 (memq backend force-symbols)))
1217 (setq found backend)))
1220 (defvar spam-list-of-processors
1221 ;; note the nil processors are not defined in gnus.el
1222 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1223 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1224 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1225 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1226 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1227 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1228 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1229 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1230 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1231 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1232 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1233 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1234 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1235 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1236 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1237 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1238 "The OBSOLETE `spam-list-of-processors' list.
1239 This list contains pairs associating the obsolete ham/spam exit
1240 processor variables with a classification and a spam-use-*
1241 variable. When the processor variable is nil, just the
1242 classification and spam-use-* check variable are used. This is
1243 superceded by the new spam backend code, so it's only consulted
1244 for backwards compatibility.")
1246 (defun spam-group-processor-p (group backend &optional classification)
1247 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1248 Also accepts the obsolete processors, which can be found in
1249 gnus.el and in spam-list-of-processors. In the case of mover
1250 backends, checks the setting of spam-summary-exit-behavior in
1251 addition to the set values for the group."
1252 (if (and (stringp group)
1254 (let ((old-style (assq backend spam-list-of-processors))
1255 (parameters (nth 0 (gnus-parameter-spam-process group)))
1257 (if old-style ; old-style processor
1258 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1259 ;; now search for the parameter
1260 (dolist (parameter parameters)
1261 (when (and (null found)
1263 (eq classification (nth 0 parameter))
1264 (eq backend (nth 1 parameter)))
1267 ;; now, if the parameter was not found, do the
1268 ;; spam-summary-exit-behavior-logic for mover backends
1270 (when (spam-backend-mover-p backend)
1274 ((eq spam-summary-exit-behavior 'move-all) t)
1275 ((eq spam-summary-exit-behavior 'move-none) nil)
1276 ((eq spam-summary-exit-behavior 'default)
1277 (or (eq classification 'spam) ;move spam out of all groups
1278 ;; move ham out of spam groups
1279 (and (eq classification 'ham)
1280 (spam-group-spam-contents-p group))))
1281 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1282 spam-summary-exit-behavior))))))
1289 ;;{{{ Summary entry and exit processing.
1291 (defun spam-mark-junk-as-spam-routine ()
1292 ;; check the global list of group names spam-junk-mailgroups and the
1294 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1295 (gnus-message 6 "Marking %s articles as spam"
1296 (if spam-mark-only-unseen-as-spam
1299 (let ((articles (if spam-mark-only-unseen-as-spam
1300 gnus-newsgroup-unseen
1301 gnus-newsgroup-unreads)))
1302 (if spam-mark-new-messages-in-spam-group-as-spam
1303 (dolist (article articles)
1304 (gnus-summary-mark-article article gnus-spam-mark))
1305 (gnus-message 9 "Did not mark new messages as spam.")))))
1307 (defun spam-summary-prepare ()
1308 (setq spam-old-articles
1309 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1310 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1311 (spam-mark-junk-as-spam-routine))
1313 ;; The spam processors are invoked for any group, spam or ham or neither
1314 (defun spam-summary-prepare-exit ()
1315 (unless gnus-group-is-exiting-without-update-p
1316 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1318 ;; before we begin, remove any article limits
1320 ; (gnus-summary-pop-limit t))
1322 ;; first of all, unregister any articles that are no longer ham or spam
1323 ;; we have to iterate over the processors, or else we'll be too slow
1324 (dolist (classification (spam-classifications))
1325 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1326 (new-articles (spam-list-articles
1327 gnus-newsgroup-articles
1329 (changed-articles (spam-set-difference new-articles old-articles)))
1330 ;; now that we have the changed articles, we go through the processors
1331 (dolist (backend (spam-backend-list))
1332 (let (unregister-list)
1333 (dolist (article changed-articles)
1334 (let ((id (spam-fetch-field-message-id-fast article)))
1335 (when (spam-log-unregistration-needed-p
1336 id 'process classification backend)
1337 (push article unregister-list))))
1338 ;; call spam-register-routine with specific articles to unregister,
1339 ;; when there are articles to unregister and the check is enabled
1340 (when (and unregister-list (symbol-value backend))
1341 (spam-backend-put-article-todo-list backend
1346 ;; do the non-moving backends first, then the moving ones
1347 (dolist (backend-type '(non-mover mover))
1348 (dolist (classification (spam-classifications))
1349 (dolist (backend (spam-backend-list backend-type))
1350 (when (spam-group-processor-p
1354 (spam-backend-put-article-todo-list backend
1357 gnus-newsgroup-articles
1358 classification))))))
1360 (spam-resolve-registrations-routine) ; do the registrations now
1362 ;; we mark all the leftover spam articles as expired at the end
1363 (dolist (article (spam-list-articles
1364 gnus-newsgroup-articles
1366 (gnus-summary-mark-article article gnus-expirable-mark)))
1368 (setq spam-old-articles nil))
1372 ;;{{{ spam-use-move and spam-use-copy backend support functions
1374 (defun spam-copy-or-move-routine (copy groups articles classification)
1376 (when (and (car-safe groups) (listp (car-safe groups)))
1377 (setq groups (pop groups)))
1379 (unless (listp groups)
1380 (setq groups (list groups)))
1382 ;; remove the current process mark
1383 (gnus-summary-kill-process-mark)
1385 (let ((backend-supports-deletions
1386 (gnus-check-backend-function
1387 'request-move-article gnus-newsgroup-name))
1388 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1389 article mark deletep respool)
1391 (when (member 'respool groups)
1392 (setq respool t) ; boolean for later
1393 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1395 ;; now do the actual move
1396 (dolist (group groups)
1397 (when (and articles (stringp group))
1399 ;; first, mark the article with the process mark and, if needed,
1400 ;; the unread or expired mark (for ham and spam respectively)
1401 (dolist (article articles)
1402 (when (and (eq classification 'ham)
1403 spam-mark-ham-unread-before-move-from-spam-group)
1404 (gnus-message 9 "Marking ham article %d unread before move"
1406 (gnus-summary-mark-article article gnus-unread-mark))
1407 (when (and (eq classification 'spam)
1409 (gnus-message 9 "Marking spam article %d expirable before move"
1411 (gnus-summary-mark-article article gnus-expirable-mark))
1412 (gnus-summary-set-process-mark article)
1414 (if respool ; respooling is with a "fake" group
1415 (let ((spam-split-disabled
1416 (or spam-split-disabled
1417 (and (eq classification 'ham)
1418 spam-disable-spam-split-during-ham-respool))))
1419 (gnus-message 9 "Respooling article %d with method %s"
1420 article respool-method)
1421 (gnus-summary-respool-article nil respool-method))
1422 (if (or (not backend-supports-deletions) ; else, we are not respooling
1423 (> (length groups) 1))
1424 (progn ; if copying, copy and set deletep
1425 (gnus-message 9 "Copying article %d to group %s"
1427 (gnus-summary-copy-article nil group)
1429 (gnus-message 9 "Moving article %d to group %s"
1431 (gnus-summary-move-article nil group))))) ; else move articles
1433 ;; now delete the articles, unless a) copy is t, and there was a copy done
1434 ;; b) a move was done to a single group
1435 ;; c) backend-supports-deletions is nil
1437 (when (and deletep backend-supports-deletions)
1438 (dolist (article articles)
1439 (gnus-summary-set-process-mark article)
1440 (gnus-message 9 "Deleting article %d" article))
1442 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1443 (gnus-summary-delete-article nil)))))
1445 (gnus-summary-yank-process-mark)
1446 (length articles))))
1448 (defun spam-copy-spam-routine (articles)
1449 (spam-copy-or-move-routine
1451 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1455 (defun spam-move-spam-routine (articles)
1456 (spam-copy-or-move-routine
1458 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1462 (defun spam-copy-ham-routine (articles)
1463 (spam-copy-or-move-routine
1465 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1469 (defun spam-move-ham-routine (articles)
1470 (spam-copy-or-move-routine
1472 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1478 ;;{{{ article and field retrieval code
1479 (defun spam-get-article-as-string (article)
1480 (when (numberp article)
1482 (gnus-request-article-this-buffer
1484 gnus-newsgroup-name)
1488 ;; (defun spam-get-article-as-filename (article)
1489 ;; (let ((article-filename))
1490 ;; (when (numberp article)
1491 ;; (nnml-possibly-change-directory
1492 ;; (gnus-group-real-name gnus-newsgroup-name))
1493 ;; (setq article-filename (expand-file-name
1494 ;; (int-to-string article) nnml-current-directory)))
1495 ;; (if (file-exists-p article-filename)
1499 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1500 "Fetch a FIELD for ARTICLE quickly, using the internal gnus-data-list function.
1501 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1502 When FIELD is 'number, ARTICLE can be any number (since we want
1504 (when (numberp article)
1505 (let* ((data-header (or prepared-data-header
1506 (spam-fetch-article-header article))))
1507 (if (arrayp data-header)
1509 ((equal field 'number)
1510 (mail-header-number data-header))
1511 ((equal field 'from)
1512 (mail-header-from data-header))
1513 ((equal field 'message-id)
1514 (mail-header-message-id data-header))
1515 ((equal field 'subject)
1516 (mail-header-subject data-header))
1517 ((equal field 'references)
1518 (mail-header-references data-header))
1519 ((equal field 'date)
1520 (mail-header-date data-header))
1521 ((equal field 'xref)
1522 (mail-header-xref data-header))
1523 ((equal field 'extra)
1524 (mail-header-extra data-header))
1528 "spam-fetch-field-fast: unknown field %s requested"
1531 (gnus-message 6 "Article %d has a nil data header" article)))))
1533 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1534 (spam-fetch-field-fast article 'from prepared-data-header))
1536 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1537 (spam-fetch-field-fast article 'subject prepared-data-header))
1539 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1540 (spam-fetch-field-fast article 'message-id prepared-data-header))
1542 (defun spam-generate-fake-headers (article)
1543 (let ((dh (spam-fetch-article-header article)))
1547 ;; 80-character limit makes for strange constructs
1548 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1549 "Date: %s\nReferences: %s\nXref: %s\n")
1550 (spam-fetch-field-fast article 'from dh)
1551 (spam-fetch-field-fast article 'subject dh)
1552 (spam-fetch-field-fast article 'message-id dh)
1553 (spam-fetch-field-fast article 'date dh)
1554 (spam-fetch-field-fast article 'references dh)
1555 (spam-fetch-field-fast article 'xref dh))
1556 (when (spam-fetch-field-fast article 'extra dh)
1557 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1560 "spam-generate-fake-headers: article %d didn't have a valid header"
1563 (defun spam-fetch-article-header (article)
1565 (set-buffer gnus-summary-buffer)
1566 (gnus-read-header article)
1567 (nth 3 (assq article gnus-newsgroup-data))))
1570 ;;{{{ Spam determination.
1572 (defun spam-split (&rest specific-checks)
1573 "Split this message into the `spam' group if it is spam.
1574 This function can be used as an entry in the variable `nnmail-split-fancy',
1575 for example like this: (: spam-split). It can take checks as
1576 parameters. A string as a parameter will set the
1577 spam-split-group to that string.
1579 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1581 (setq spam-split-last-successful-check nil)
1582 (unless spam-split-disabled
1583 (let ((spam-split-group-choice spam-split-group))
1584 (dolist (check specific-checks)
1585 (when (stringp check)
1586 (setq spam-split-group-choice check)
1587 (setq specific-checks (delq check specific-checks))))
1589 (let ((spam-split-group spam-split-group-choice)
1590 (widening-needed-check (spam-widening-needed-p specific-checks)))
1593 (when widening-needed-check
1595 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1596 widening-needed-check))
1597 (let ((backends (spam-backend-list))
1599 (while (and backends (not decision))
1600 (let* ((backend (pop backends))
1601 (check-function (spam-backend-check backend))
1602 (spam-split-group (if spam-split-symbolic-return
1606 ;; either, given specific checks, this is one of them
1607 (memq backend specific-checks)
1608 ;; or, given no specific checks, spam-use-CHECK is set
1609 (and (null specific-checks) (symbol-value backend)))
1610 (gnus-message 6 "spam-split: calling the %s function"
1612 (setq decision (funcall check-function))
1613 ;; if we got a decision at all, save the current check
1615 (setq spam-split-last-successful-check backend))
1617 (when (eq decision 'spam)
1618 (unless spam-split-symbolic-return
1621 (format "spam-split got %s but %s is nil"
1623 spam-split-symbolic-return)))))))
1625 (if spam-split-symbolic-return-positive 'ham nil)
1628 (defun spam-find-spam ()
1629 "This function will detect spam in the current newsgroup using spam-split."
1632 (let* ((group gnus-newsgroup-name)
1633 (autodetect (gnus-parameter-spam-autodetect group))
1634 (methods (gnus-parameter-spam-autodetect-methods group))
1635 (first-method (nth 0 methods))
1636 (articles (if spam-autodetect-recheck-messages
1637 gnus-newsgroup-articles
1638 gnus-newsgroup-unseen))
1639 article-cannot-be-faked)
1642 (dolist (backend methods)
1643 (when (spam-backend-statistical-p backend)
1644 (setq article-cannot-be-faked t)
1647 (when (memq 'default methods)
1648 (setq article-cannot-be-faked t))
1650 (when (and autodetect
1651 (not (equal first-method 'none)))
1654 (let ((id (spam-fetch-field-message-id-fast article))
1655 (subject (spam-fetch-field-subject-fast article))
1656 (sender (spam-fetch-field-from-fast article))
1660 (gnus-message 6 "Article %d has no message ID!" article))
1662 (when (and id spam-log-to-registry)
1663 (setq registry-lookup (spam-log-registration-type id 'incoming))
1664 (when registry-lookup
1667 "spam-find-spam: message %s was already registered incoming"
1670 (let* ((spam-split-symbolic-return t)
1671 (spam-split-symbolic-return-positive t)
1672 (fake-headers (spam-generate-fake-headers article))
1676 (if article-cannot-be-faked
1677 (gnus-request-article-this-buffer
1680 ;; else, we fake the article
1681 (when fake-headers (insert fake-headers)))
1682 (if (or (null first-method)
1683 (equal first-method 'default))
1685 (apply 'spam-split methods))))))
1686 (if (equal split-return 'spam)
1687 (gnus-summary-mark-article article gnus-spam-mark))
1689 (when (and id split-return spam-log-to-registry)
1690 (when (zerop (gnus-registry-group-count id))
1691 (gnus-registry-add-group
1692 id group subject sender))
1694 (unless registry-lookup
1695 (spam-log-processing-to-registry
1699 spam-split-last-successful-check
1705 ;;{{{ registration/unregistration functions
1707 (defun spam-resolve-registrations-routine ()
1708 "Go through the backends and register or unregister articles as needed."
1709 (dolist (backend-type '(non-mover mover))
1710 (dolist (classification (spam-classifications))
1711 (dolist (backend (spam-backend-list backend-type))
1712 (let ((rlist (spam-backend-get-article-todo-list
1713 backend classification))
1714 (ulist (spam-backend-get-article-todo-list
1715 backend classification t))
1718 ;; clear the old lists right away
1719 (spam-backend-put-article-todo-list backend
1723 (spam-backend-put-article-todo-list backend
1728 ;; eliminate duplicates
1729 (dolist (article (copy-sequence ulist))
1730 (when (memq article rlist)
1732 (setq rlist (delq article rlist))
1733 (setq ulist (delq article ulist))))
1735 (unless (zerop delcount)
1738 "%d messages were saved the trouble of unregistering and then registering"
1741 ;; unregister articles
1742 (unless (zerop (length ulist))
1743 (let ((num (spam-unregister-routine classification backend ulist)))
1747 "%d %s messages were unregistered by backend %s."
1752 ;; register articles
1753 (unless (zerop (length rlist))
1754 (let ((num (spam-register-routine classification backend rlist)))
1758 "%d %s messages were registered by backend %s."
1763 (defun spam-unregister-routine (classification
1766 (spam-register-routine classification backend specific-articles t))
1768 (defun spam-register-routine (classification
1771 &optional unregister)
1772 (when (and (spam-classification-valid-p classification)
1773 (spam-backend-valid-p backend))
1774 (let* ((register-function
1775 (spam-backend-function backend classification 'registration))
1776 (unregister-function
1777 (spam-backend-function backend classification 'unregistration))
1778 (run-function (if unregister
1781 (log-function (if unregister
1782 'spam-log-undo-registration
1783 'spam-log-processing-to-registry))
1787 ;; make list of articles, using specific-articles if given
1788 (setq articles (or specific-articles
1790 gnus-newsgroup-articles
1793 (when (> (length articles) 0)
1794 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1795 (if unregister "Unregistering" "Registering")
1797 (if specific-articles "specific" "")
1800 (funcall run-function articles)
1801 ;; now log all the registrations (or undo them, depending on
1803 (dolist (article articles)
1804 (funcall log-function
1805 (spam-fetch-field-message-id-fast article)
1809 gnus-newsgroup-name))))
1810 ;; return the number of articles processed
1811 (length articles))))
1813 ;;; log a ham- or spam-processor invocation to the registry
1814 (defun spam-log-processing-to-registry (id type classification backend group)
1815 (when spam-log-to-registry
1816 (if (and (stringp id)
1818 (spam-process-type-valid-p type)
1819 (spam-classification-valid-p classification)
1820 (spam-backend-valid-p backend))
1821 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1822 (cell (list classification backend group)))
1823 (push cell cell-list)
1824 (gnus-registry-store-extra-entry
1831 (format "%s call with bad ID, type, classification, spam-backend, or group"
1832 "spam-log-processing-to-registry")))))
1834 ;;; check if a ham- or spam-processor registration has been done
1835 (defun spam-log-registered-p (id type)
1836 (when spam-log-to-registry
1837 (if (and (stringp id)
1838 (spam-process-type-valid-p type))
1839 (cdr-safe (gnus-registry-fetch-extra id type))
1843 (format "%s called with bad ID, type, classification, or spam-backend"
1844 "spam-log-registered-p"))
1847 ;;; check what a ham- or spam-processor registration says
1848 ;;; returns nil if conflicting registrations are found
1849 (defun spam-log-registration-type (id type)
1852 (dolist (reg (spam-log-registered-p id type))
1853 (let ((classification (nth 0 reg)))
1854 (when (spam-classification-valid-p classification)
1856 (not (eq classification decision)))
1857 (setq count (+ 1 count)))
1858 (setq decision classification))))
1864 ;;; check if a ham- or spam-processor registration needs to be undone
1865 (defun spam-log-unregistration-needed-p (id type classification backend)
1866 (when spam-log-to-registry
1867 (if (and (stringp id)
1868 (spam-process-type-valid-p type)
1869 (spam-classification-valid-p classification)
1870 (spam-backend-valid-p backend))
1871 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1873 (dolist (cell cell-list)
1875 (when (and (eq classification (nth 0 cell))
1876 (eq backend (nth 1 cell)))
1882 (format "%s called with bad ID, type, classification, or spam-backend"
1883 "spam-log-unregistration-needed-p"))
1887 ;;; undo a ham- or spam-processor registration (the group is not used)
1888 (defun spam-log-undo-registration (id type classification backend &optional group)
1889 (when (and spam-log-to-registry
1890 (spam-log-unregistration-needed-p id type classification backend))
1891 (if (and (stringp id)
1892 (spam-process-type-valid-p type)
1893 (spam-classification-valid-p classification)
1894 (spam-backend-valid-p backend))
1895 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1896 new-cell-list found)
1897 (dolist (cell cell-list)
1898 (unless (and (eq classification (nth 0 cell))
1899 (eq backend (nth 1 cell)))
1900 (push cell new-cell-list)))
1901 (gnus-registry-store-extra-entry
1906 (gnus-error 7 (format "%s call with bad ID, type, spam-backend, or group"
1907 "spam-log-undo-registration"))
1912 ;;{{{ backend functions
1915 (defun spam-check-gmane-xref ()
1917 (message-fetch-field "Xref")
1918 (message-fetch-field "Newsgroups"))))
1919 (when header ; return nil when no header
1920 (when (string-match spam-gmane-xref-spam-group
1922 spam-split-group))))
1928 (defun spam-check-regex-body ()
1929 (let ((spam-regex-headers-ham spam-regex-body-ham)
1930 (spam-regex-headers-spam spam-regex-body-spam))
1931 (spam-check-regex-headers t)))
1937 (defun spam-check-regex-headers (&optional body)
1938 (let ((type (if body "body" "header"))
1940 (dolist (h-regex spam-regex-headers-ham)
1942 (goto-char (point-min))
1943 (when (re-search-forward h-regex nil t)
1944 (message "Ham regex %s search positive." type)
1946 (dolist (s-regex spam-regex-headers-spam)
1948 (goto-char (point-min))
1949 (when (re-search-forward s-regex nil t)
1950 (message "Spam regex %s search positive." type)
1952 (setq ret spam-split-group))))
1959 (defun spam-reverse-ip-string (ip)
1961 (mapconcat 'identity
1962 (nreverse (split-string ip "\\."))
1965 (defun spam-check-blackholes ()
1966 "Check the Received headers for blackholed relays."
1967 (let ((headers (message-fetch-field "received"))
1972 (goto-char (point-min))
1973 (gnus-message 6 "Checking headers for relay addresses")
1974 (while (re-search-forward
1975 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1976 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1977 (push (spam-reverse-ip-string (match-string 1))
1979 (dolist (server spam-blackhole-servers)
1981 (unless (and spam-blackhole-good-server-regex
1982 ;; match the good-server-regex against the reversed (again) IP string
1984 spam-blackhole-good-server-regex
1985 (spam-reverse-ip-string ip)))
1987 (let ((query-string (concat ip "." server)))
1989 (let ((query-result (query-dig query-string)))
1991 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1993 (push (list ip server query-result)
1995 ;; else, if not using dig.el
1996 (when (query-dns query-string)
1997 (gnus-message 6 "positive blackhole check")
1998 (push (list ip server (query-dns query-string 'TXT))
2007 (autoload 'mail-check-payment "hashcash"))
2013 (defun spam-check-hashcash ()
2014 "Check the headers for hashcash payments."
2015 (mail-check-payment))) ;mail-check-payment returns a boolean
2022 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2023 ;;; <sacha@giotto.sj.ru>
2025 ;; all this is done inside a condition-case to trap errors
2028 (autoload 'bbdb-buffer "bbdb")
2029 (autoload 'bbdb-create-internal "bbdb")
2030 (autoload 'bbdb-search-simple "bbdb"))
2033 (when (condition-case nil
2036 (require 'bbdb-com))
2038 ;; `bbdb-records' should not be bound as an autoload function
2039 ;; before loading bbdb because of `bbdb-hashtable-size'.
2040 (defalias 'bbdb-records 'ignore)
2041 (defalias 'spam-BBDB-register-routine 'ignore)
2042 (defalias 'spam-enter-ham-BBDB 'ignore)
2045 ;; when the BBDB changes, we want to clear out our cache
2046 (defun spam-clear-cache-BBDB (&rest immaterial)
2047 (spam-clear-cache 'spam-use-BBDB))
2049 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2051 (defun spam-enter-ham-BBDB (addresses &optional remove)
2052 "Enter an address into the BBDB; implies ham (non-spam) sender"
2053 (dolist (from addresses)
2054 (when (stringp from)
2055 (let* ((parsed-address (gnus-extract-address-components from))
2056 (name (or (nth 0 parsed-address) "Ham Sender"))
2057 (remove-function (if remove
2058 'bbdb-delete-record-internal
2060 (net-address (nth 1 parsed-address))
2061 (record (and net-address
2062 (bbdb-search-simple nil net-address))))
2064 (gnus-message 6 "%s address %s %s BBDB"
2065 (if remove "Deleting" "Adding")
2067 (if remove "from" "to"))
2069 (funcall remove-function record)
2070 (bbdb-create-internal name nil net-address nil nil
2071 "ham sender added by spam.el")))))))
2073 (defun spam-BBDB-register-routine (articles &optional unregister)
2075 (dolist (article articles)
2076 (when (stringp (spam-fetch-field-from-fast article))
2077 (push (spam-fetch-field-from-fast article) addresses)))
2078 ;; now do the register/unregister action
2079 (spam-enter-ham-BBDB addresses unregister)))
2081 (defun spam-BBDB-unregister-routine (articles)
2082 (spam-BBDB-register-routine articles t))
2084 (defun spam-check-BBDB ()
2085 "Mail from people in the BBDB is classified as ham or non-spam"
2086 (let ((who (message-fetch-field "from"))
2087 bbdb-cache bbdb-hashtable)
2088 (when spam-cache-lookups
2089 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
2091 (setq bbdb-cache (make-vector 17 0)) ; a good starting hash value
2092 ;; this is based on the expanded (bbdb-hashtable) macro
2093 ;; without the debugging support
2094 (with-current-buffer (bbdb-buffer)
2096 (save-window-excursion
2097 (bbdb-records nil t)
2100 (intern (downcase (symbol-name symbol)) bbdb-cache))
2102 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
2104 (setq who (nth 1 (gnus-extract-address-components who)))
2106 (if spam-cache-lookups
2107 (intern-soft (downcase who) bbdb-cache)
2108 (bbdb-search-simple nil who))
2110 (if spam-use-BBDB-exclusive
2118 ;;; check the ifile backend; return nil if the mail was NOT classified
2121 (defun spam-get-ifile-database-parameter ()
2122 "Get the command-line parameter for ifile's database from
2123 spam-ifile-database-path."
2124 (if spam-ifile-database-path
2125 (format "--db-file=%s" spam-ifile-database-path)
2128 (defun spam-check-ifile ()
2129 "Check the ifile backend for the classification of this message."
2130 (let ((article-buffer-name (buffer-name))
2133 (let ((temp-buffer-name (buffer-name))
2134 (db-param (spam-get-ifile-database-parameter)))
2136 (set-buffer article-buffer-name)
2137 (apply 'call-process-region
2138 (point-min) (point-max) spam-ifile-path
2139 nil temp-buffer-name nil "-c"
2140 (if db-param `(,db-param "-q") `("-q"))))
2141 ;; check the return now (we're back in the temp buffer)
2142 (goto-char (point-min))
2144 (setq category (buffer-substring (point) (point-at-eol))))
2145 (when (not (zerop (length category))) ; we need a category here
2146 (if spam-ifile-all-categories
2147 (setq return category)
2148 ;; else, if spam-ifile-all-categories is not set...
2149 (when (string-equal spam-ifile-spam-category category)
2150 (setq return spam-split-group)))))) ; note return is nil otherwise
2153 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2154 "Register an article, given as a string, with a category.
2155 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2156 (let ((category (or category gnus-newsgroup-name))
2157 (add-or-delete-option (if unregister "-d" "-i"))
2158 (db (spam-get-ifile-database-parameter))
2161 (dolist (article articles)
2162 (let ((article-string (spam-get-article-as-string article)))
2163 (when (stringp article-string)
2164 (insert article-string))))
2165 (apply 'call-process-region
2166 (point-min) (point-max) spam-ifile-path
2168 add-or-delete-option category
2169 (if db `(,db "-h") `("-h"))))))
2171 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2172 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2174 (defun spam-ifile-unregister-spam-routine (articles)
2175 (spam-ifile-register-spam-routine articles t))
2177 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2178 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2180 (defun spam-ifile-unregister-ham-routine (articles)
2181 (spam-ifile-register-ham-routine articles t))
2188 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2189 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2190 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2191 (autoload 'spam-stat-buffer-is-spam "spam-stat")
2192 (autoload 'spam-stat-load "spam-stat")
2193 (autoload 'spam-stat-save "spam-stat")
2194 (autoload 'spam-stat-split-fancy "spam-stat"))
2197 (when (condition-case nil
2198 (let ((spam-stat-install-hooks nil))
2199 (require 'spam-stat))
2201 (defalias 'spam-stat-register-ham-routine 'ignore)
2202 (defalias 'spam-stat-register-spam-routine 'ignore)
2205 (defun spam-check-stat ()
2206 "Check the spam-stat backend for the classification of this message"
2207 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2208 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2210 (spam-stat-split-fancy)))
2212 (defun spam-stat-register-spam-routine (articles &optional unregister)
2213 (dolist (article articles)
2214 (let ((article-string (spam-get-article-as-string article)))
2216 (insert article-string)
2218 (spam-stat-buffer-change-to-non-spam)
2219 (spam-stat-buffer-is-spam))))))
2221 (defun spam-stat-unregister-spam-routine (articles)
2222 (spam-stat-register-spam-routine articles t))
2224 (defun spam-stat-register-ham-routine (articles &optional unregister)
2225 (dolist (article articles)
2226 (let ((article-string (spam-get-article-as-string article)))
2228 (insert article-string)
2230 (spam-stat-buffer-change-to-spam)
2231 (spam-stat-buffer-is-non-spam))))))
2233 (defun spam-stat-unregister-ham-routine (articles)
2234 (spam-stat-register-ham-routine articles t))
2236 (defun spam-maybe-spam-stat-load ()
2237 (when spam-use-stat (spam-stat-load)))
2239 (defun spam-maybe-spam-stat-save ()
2240 (when spam-use-stat (spam-stat-save)))))
2244 ;;{{{ Blacklists and whitelists.
2246 (defvar spam-whitelist-cache nil)
2247 (defvar spam-blacklist-cache nil)
2249 (defun spam-kill-whole-line ()
2251 (let ((kill-whole-line t))
2254 ;;; address can be a list, too
2255 (defun spam-enter-whitelist (address &optional remove)
2256 "Enter ADDRESS (list or single) into the whitelist.
2257 With a non-nil REMOVE, remove them."
2258 (interactive "sAddress: ")
2259 (spam-enter-list address spam-whitelist remove)
2260 (setq spam-whitelist-cache nil)
2261 (spam-clear-cache 'spam-use-whitelist))
2263 ;;; address can be a list, too
2264 (defun spam-enter-blacklist (address &optional remove)
2265 "Enter ADDRESS (list or single) into the blacklist.
2266 With a non-nil REMOVE, remove them."
2267 (interactive "sAddress: ")
2268 (spam-enter-list address spam-blacklist remove)
2269 (setq spam-blacklist-cache nil)
2270 (spam-clear-cache 'spam-use-whitelist))
2272 (defun spam-enter-list (addresses file &optional remove)
2273 "Enter ADDRESSES into the given FILE.
2274 Either the whitelist or the blacklist files can be used. With
2275 REMOVE not nil, remove the ADDRESSES."
2276 (if (stringp addresses)
2277 (spam-enter-list (list addresses) file remove)
2278 ;; else, we have a list of addresses here
2279 (unless (file-exists-p (file-name-directory file))
2280 (make-directory (file-name-directory file) t))
2283 (find-file-noselect file))
2284 (dolist (a addresses)
2286 (goto-char (point-min))
2287 (if (re-search-forward (regexp-quote a) nil t)
2288 ;; found the address
2290 (spam-kill-whole-line))
2291 ;; else, the address was not found
2293 (goto-char (point-max))
2299 (defun spam-filelist-build-cache (type)
2300 (let ((cache (if (eq type 'spam-use-blacklist)
2301 spam-blacklist-cache
2302 spam-whitelist-cache))
2304 (unless (gethash type spam-caches)
2306 (let ((address (pop cache)))
2307 (unless (zerop (length address)) ; 0 for a nil address too
2308 (setq address (regexp-quote address))
2309 ;; fix regexp-quote's treatment of user-intended regexes
2310 (while (string-match "\\\\\\*" address)
2311 (setq address (replace-match ".*" t t address))))
2312 (push address parsed-cache)))
2313 (puthash type parsed-cache spam-caches))))
2315 (defun spam-filelist-check-cache (type from)
2316 (when (stringp from)
2317 (spam-filelist-build-cache type)
2319 (dolist (address (gethash type spam-caches))
2320 (when (and address (string-match address from))
2325 ;;; returns t if the sender is in the whitelist, nil or
2326 ;;; spam-split-group otherwise
2327 (defun spam-check-whitelist ()
2328 ;; FIXME! Should it detect when file timestamps change?
2329 (unless spam-whitelist-cache
2330 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2331 (if (spam-from-listed-p 'spam-use-whitelist)
2333 (if spam-use-whitelist-exclusive
2337 (defun spam-check-blacklist ()
2338 ;; FIXME! Should it detect when file timestamps change?
2339 (unless spam-blacklist-cache
2340 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2341 (and (spam-from-listed-p 'spam-use-blacklist)
2344 (defun spam-parse-list (file)
2345 (when (file-readable-p file)
2346 (let (contents address)
2348 (insert-file-contents file)
2350 (setq address (buffer-substring (point) (point-at-eol)))
2352 ;; insert the e-mail address if detected, otherwise the raw data
2353 (unless (zerop (length address))
2354 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2355 (push (or pure-address address) contents)))))
2356 (nreverse contents))))
2358 (defun spam-from-listed-p (type)
2359 (let ((from (message-fetch-field "from"))
2361 (spam-filelist-check-cache type from)))
2363 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2364 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2365 (declassification (if blacklist 'ham 'spam))
2367 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2369 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2370 from addresses unregister-list article-unregister-list)
2371 (dolist (article articles)
2372 (let ((from (spam-fetch-field-from-fast article))
2373 (id (spam-fetch-field-message-id-fast article))
2375 (when (stringp from)
2376 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2377 (when (and (not sender-ignored)
2378 (stringp ignore-regex)
2379 (string-match ignore-regex from))
2380 (setq sender-ignored t)))
2381 ;; remember the messages we need to unregister, unless remove is set
2384 (spam-log-unregistration-needed-p
2385 id 'process declassification de-symbol))
2386 (push article article-unregister-list)
2387 (push from unregister-list))
2388 (unless sender-ignored
2389 (push from addresses)))))
2392 (funcall enter-function addresses t) ; unregister all these addresses
2393 ;; else, register normally and unregister what we need to
2394 (funcall remove-function unregister-list t)
2395 (dolist (article article-unregister-list)
2396 (spam-log-undo-registration
2397 (spam-fetch-field-message-id-fast article)
2401 (funcall enter-function addresses nil))))
2403 (defun spam-blacklist-unregister-routine (articles)
2404 (spam-blacklist-register-routine articles t))
2406 (defun spam-blacklist-register-routine (articles &optional unregister)
2407 (spam-filelist-register-routine articles t unregister))
2409 (defun spam-whitelist-unregister-routine (articles)
2410 (spam-whitelist-register-routine articles t))
2412 (defun spam-whitelist-register-routine (articles &optional unregister)
2413 (spam-filelist-register-routine articles nil unregister))
2417 ;;{{{ Spam-report glue (gmane and resend reporting)
2418 (defun spam-report-gmane-register-routine (articles)
2420 (apply 'spam-report-gmane articles)))
2422 (defun spam-report-resend-register-ham-routine (articles)
2423 (spam-report-resend-register-routine articles t))
2425 (defun spam-report-resend-register-routine (articles &optional ham)
2426 (let* ((resend-to-gp
2428 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2429 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2430 (spam-report-resend-to (or (car-safe resend-to-gp)
2431 spam-report-resend-to)))
2432 (spam-report-resend articles ham)))
2437 (defun spam-check-bogofilter-headers (&optional score)
2438 (let ((header (message-fetch-field spam-bogofilter-header)))
2439 (when header ; return nil when no header
2440 (if score ; scoring mode
2441 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2442 (match-string 1 header)
2444 ;; spam detection mode
2445 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2447 spam-split-group)))))
2449 ;; return something sensible if the score can't be determined
2450 (defun spam-bogofilter-score (&optional recheck)
2451 "Get the Bogofilter spamicity score"
2453 (save-window-excursion
2454 (gnus-summary-show-article t)
2455 (set-buffer gnus-article-buffer)
2456 (let ((score (or (unless recheck
2457 (spam-check-bogofilter-headers t))
2458 (spam-check-bogofilter t))))
2459 (gnus-summary-show-article)
2460 (message "Spamicity score %s" score)
2463 (defun spam-verify-bogofilter ()
2464 "Verify the Bogofilter version is sufficient."
2465 (when (eq spam-bogofilter-valid 'unknown)
2466 (setq spam-bogofilter-valid
2467 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2468 (shell-command-to-string
2469 (format "%s -V" spam-bogofilter-path))))))
2470 spam-bogofilter-valid)
2472 (defun spam-check-bogofilter (&optional score)
2473 "Check the Bogofilter backend for the classification of this message."
2474 (if (spam-verify-bogofilter)
2475 (let ((article-buffer-name (buffer-name))
2476 (db spam-bogofilter-database-directory)
2479 (let ((temp-buffer-name (buffer-name)))
2481 (set-buffer article-buffer-name)
2482 (apply 'call-process-region
2483 (point-min) (point-max)
2484 spam-bogofilter-path
2485 nil temp-buffer-name nil
2486 (if db `("-d" ,db "-v") `("-v"))))
2487 (setq return (spam-check-bogofilter-headers score))))
2489 (gnus-error "`spam.el' doesnt support obsolete bogofilter versions")))
2491 (defun spam-bogofilter-register-with-bogofilter (articles
2493 &optional unregister)
2494 "Register an article, given as a string, as spam or non-spam."
2495 (if (spam-verify-bogofilter)
2496 (dolist (article articles)
2497 (let ((article-string (spam-get-article-as-string article))
2498 (db spam-bogofilter-database-directory)
2499 (switch (if unregister
2501 spam-bogofilter-spam-strong-switch
2502 spam-bogofilter-ham-strong-switch)
2504 spam-bogofilter-spam-switch
2505 spam-bogofilter-ham-switch))))
2506 (when (stringp article-string)
2508 (insert article-string)
2510 (apply 'call-process-region
2511 (point-min) (point-max)
2512 spam-bogofilter-path
2514 (if db `("-d" ,db "-v") `("-v")))))))
2515 (gnus-error "`spam.el' doesnt support obsolete bogofilter versions")))
2517 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2518 (spam-bogofilter-register-with-bogofilter articles t unregister))
2520 (defun spam-bogofilter-unregister-spam-routine (articles)
2521 (spam-bogofilter-register-spam-routine articles t))
2523 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2524 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2526 (defun spam-bogofilter-unregister-ham-routine (articles)
2527 (spam-bogofilter-register-ham-routine articles t))
2533 (defun spam-check-spamoracle ()
2534 "Run spamoracle on an article to determine whether it's spam."
2535 (let ((article-buffer-name (buffer-name)))
2537 (let ((temp-buffer-name (buffer-name)))
2539 (set-buffer article-buffer-name)
2541 (apply 'call-process-region
2542 (point-min) (point-max)
2543 spam-spamoracle-binary
2544 nil temp-buffer-name nil
2545 (if spam-spamoracle-database
2546 `("-f" ,spam-spamoracle-database "mark")
2550 (set-buffer temp-buffer-name)
2551 (goto-char (point-min))
2552 (when (re-search-forward "^X-Spam: yes;" nil t)
2554 (error "Error running spamoracle: %s" status))))))))
2556 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2557 "Run spamoracle in training mode."
2559 (let ((temp-buffer-name (buffer-name)))
2561 (goto-char (point-min))
2562 (dolist (article articles)
2563 (insert (spam-get-article-as-string article)))
2564 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2568 (apply 'call-process-region
2569 (point-min) (point-max)
2570 spam-spamoracle-binary
2571 nil temp-buffer-name nil
2572 (if spam-spamoracle-database
2573 `("-f" ,spam-spamoracle-database
2576 (unless (eq 0 status)
2577 (error "Error running spamoracle: %s" status)))))))
2579 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2580 (spam-spamoracle-learn articles nil unregister))
2582 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2583 (spam-spamoracle-learn-ham articles t))
2585 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2586 (spam-spamoracle-learn articles t unregister))
2588 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2589 (spam-spamoracle-learn-spam articles t))
2594 ;;; based mostly on the bogofilter code
2595 (defun spam-check-spamassassin-headers (&optional score)
2596 "Check the SpamAssassin headers for the classification of this message."
2597 (if score ; scoring mode
2598 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2600 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2601 (match-string 1 header)
2603 ;; spam detection mode
2604 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2605 (when header ; return nil when no header
2606 (when (string-match spam-spamassassin-positive-spam-flag-header
2608 spam-split-group)))))
2610 (defun spam-check-spamassassin (&optional score)
2611 "Check the SpamAssassin backend for the classification of this message."
2612 (let ((article-buffer-name (buffer-name)))
2614 (let ((temp-buffer-name (buffer-name)))
2616 (set-buffer article-buffer-name)
2617 (apply 'call-process-region
2618 (point-min) (point-max) spam-spamassassin-path
2619 nil temp-buffer-name nil spam-spamassassin-arguments))
2620 ;; check the return now (we're back in the temp buffer)
2621 (goto-char (point-min))
2622 (spam-check-spamassassin-headers score)))))
2624 ;; return something sensible if the score can't be determined
2625 (defun spam-spamassassin-score (&optional recheck)
2626 "Get the SpamAssassin score"
2628 (save-window-excursion
2629 (gnus-summary-show-article t)
2630 (set-buffer gnus-article-buffer)
2631 (let ((score (or (unless recheck
2632 (spam-check-spamassassin-headers t))
2633 (spam-check-spamassassin t))))
2634 (gnus-summary-show-article)
2635 (message "SpamAssassin score %s" score)
2638 (defun spam-spamassassin-register-with-sa-learn (articles spam
2639 &optional unregister)
2640 "Register articles with spamassassin's sa-learn as spam or non-spam."
2642 (let ((action (if unregister spam-sa-learn-unregister-switch
2643 (if spam spam-sa-learn-spam-switch
2644 spam-sa-learn-ham-switch)))
2645 (summary-buffer-name (buffer-name)))
2647 ;; group the articles into mbox format
2648 (dolist (article articles)
2649 (let (article-string)
2651 (set-buffer summary-buffer-name)
2652 (setq article-string (spam-get-article-as-string article)))
2653 (when (stringp article-string)
2654 (insert "From \n") ; mbox separator (sa-learn only checks the
2655 ; first five chars, so we can get away with
2657 (insert article-string)
2659 ;; call sa-learn on all messages at the same time
2660 (apply 'call-process-region
2661 (point-min) (point-max)
2663 nil nil nil "--mbox"
2664 (if spam-sa-learn-rebuild
2666 `("--no-rebuild" ,action)))))))
2668 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2669 (spam-spamassassin-register-with-sa-learn articles t unregister))
2671 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2672 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2674 (defun spam-spamassassin-unregister-spam-routine (articles)
2675 (spam-spamassassin-register-with-sa-learn articles t t))
2677 (defun spam-spamassassin-unregister-ham-routine (articles)
2678 (spam-spamassassin-register-with-sa-learn articles nil t))
2683 ;;; based mostly on the bogofilter code
2684 (defun spam-check-bsfilter-headers (&optional score)
2686 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2688 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2689 (when header ; return nil when no header
2690 (when (string-match "YES" header)
2691 spam-split-group)))))
2693 ;; return something sensible if the score can't be determined
2694 (defun spam-bsfilter-score (&optional recheck)
2695 "Get the Bsfilter spamicity score"
2697 (save-window-excursion
2698 (gnus-summary-show-article t)
2699 (set-buffer gnus-article-buffer)
2700 (let ((score (or (unless recheck
2701 (spam-check-bsfilter-headers t))
2702 (spam-check-bsfilter t))))
2703 (gnus-summary-show-article)
2704 (message "Spamicity score %s" score)
2707 (defun spam-check-bsfilter (&optional score)
2708 "Check the Bsfilter backend for the classification of this message"
2709 (let ((article-buffer-name (buffer-name))
2710 (dir spam-bsfilter-database-directory)
2713 (let ((temp-buffer-name (buffer-name)))
2715 (set-buffer article-buffer-name)
2716 (apply 'call-process-region
2717 (point-min) (point-max)
2719 nil temp-buffer-name nil
2722 "--insert-probability"
2724 (list "--homedir" dir))))
2725 (setq return (spam-check-bsfilter-headers score))))
2728 (defun spam-bsfilter-register-with-bsfilter (articles
2730 &optional unregister)
2731 "Register an article, given as a string, as spam or non-spam."
2732 (dolist (article articles)
2733 (let ((article-string (spam-get-article-as-string article))
2734 (switch (if unregister
2736 spam-bsfilter-spam-strong-switch
2737 spam-bsfilter-ham-strong-switch)
2739 spam-bsfilter-spam-switch
2740 spam-bsfilter-ham-switch))))
2741 (when (stringp article-string)
2743 (insert article-string)
2744 (apply 'call-process-region
2745 (point-min) (point-max)
2749 (when spam-bsfilter-database-directory
2751 spam-bsfilter-database-directory))))))))
2753 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2754 (spam-bsfilter-register-with-bsfilter articles t unregister))
2756 (defun spam-bsfilter-unregister-spam-routine (articles)
2757 (spam-bsfilter-register-spam-routine articles t))
2759 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2760 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2762 (defun spam-bsfilter-unregister-ham-routine (articles)
2763 (spam-bsfilter-register-ham-routine articles t))
2767 ;;{{{ CRM114 Mailfilter
2768 (defun spam-check-crm114-headers (&optional score)
2769 (let ((header (message-fetch-field spam-crm114-header)))
2770 (when header ; return nil when no header
2771 (if score ; scoring mode
2772 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2773 (match-string 1 header)
2775 ;; spam detection mode
2776 (when (string-match spam-crm114-positive-spam-header
2778 spam-split-group)))))
2780 ;; return something sensible if the score can't be determined
2781 (defun spam-crm114-score ()
2782 "Get the CRM114 Mailfilter pR"
2784 (save-window-excursion
2785 (gnus-summary-show-article t)
2786 (set-buffer gnus-article-buffer)
2787 (let ((score (or (spam-check-crm114-headers t)
2788 (spam-check-crm114 t))))
2789 (gnus-summary-show-article)
2790 (message "pR: %s" score)
2793 (defun spam-check-crm114 (&optional score)
2794 "Check the CRM114 Mailfilter backend for the classification of this message"
2795 (let ((article-buffer-name (buffer-name))
2796 (db spam-crm114-database-directory)
2799 (let ((temp-buffer-name (buffer-name)))
2801 (set-buffer article-buffer-name)
2802 (apply 'call-process-region
2803 (point-min) (point-max)
2805 nil temp-buffer-name nil
2806 (when db (list (concat "--fileprefix=" db)))))
2807 (setq return (spam-check-crm114-headers score))))
2810 (defun spam-crm114-register-with-crm114 (articles
2812 &optional unregister)
2813 "Register an article, given as a string, as spam or non-spam."
2814 (dolist (article articles)
2815 (let ((article-string (spam-get-article-as-string article))
2816 (db spam-crm114-database-directory)
2817 (switch (if unregister
2819 spam-crm114-spam-strong-switch
2820 spam-crm114-ham-strong-switch)
2822 spam-crm114-spam-switch
2823 spam-crm114-ham-switch))))
2824 (when (stringp article-string)
2826 (insert article-string)
2828 (apply 'call-process-region
2829 (point-min) (point-max)
2832 (when db (list switch (concat "--fileprefix=" db)))))))))
2834 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2835 (spam-crm114-register-with-crm114 articles t unregister))
2837 (defun spam-crm114-unregister-spam-routine (articles)
2838 (spam-crm114-register-spam-routine articles t))
2840 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2841 (spam-crm114-register-with-crm114 articles nil unregister))
2843 (defun spam-crm114-unregister-ham-routine (articles)
2844 (spam-crm114-register-ham-routine articles t))
2853 (defun spam-initialize (&rest symbols)
2854 "Install the spam.el hooks and do other initialization.
2855 When SYMBOLS is given, set those variables to t. This is so you
2856 can call spam-initialize before you set spam-use-* variables on
2857 explicitly, and matters only if you need the extra headers
2858 installed through spam-necessary-extra-headers."
2861 (dolist (var symbols)
2864 (dolist (header (spam-necessary-extra-headers))
2865 (add-to-list 'nnmail-extra-headers header)
2866 (add-to-list 'gnus-extra-headers header))
2868 (setq spam-install-hooks t)
2869 ;; TODO: How do we redo this every time spam-face is customized?
2870 (push '((eq mark gnus-spam-mark) . spam-face)
2871 gnus-summary-highlight)
2872 ;; Add hooks for loading and saving the spam stats
2873 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2874 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2875 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2876 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2877 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2878 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2879 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2881 (defun spam-unload-hook ()
2882 "Uninstall the spam.el hooks"
2884 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2885 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2886 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2887 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2888 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2889 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2890 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2892 (add-hook 'spam-unload-hook 'spam-unload-hook)
2894 (when spam-install-hooks
2900 ;;; spam.el ends here