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)
354 '((((class color) (type tty) (background dark))
355 (:foreground "gray80" :background "gray50"))
356 (((class color) (type tty) (background light))
357 (:foreground "gray50" :background "gray80"))
358 (((class color) (background dark))
359 (:foreground "ivory2"))
360 (((class color) (background light))
361 (:foreground "ivory4"))
362 (t :inverse-video t))
363 "Face for spam-marked articles.")
365 (defcustom spam-face 'spam-face
366 "Face for spam-marked articles."
370 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
371 "Regular expression for positive header spam matches.
372 Only meaningful if you enable `spam-use-regex-headers'."
373 :type '(repeat (regexp :tag "Regular expression to match spam header"))
376 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
377 "Regular expression for positive header ham matches.
378 Only meaningful if you enable `spam-use-regex-headers'."
379 :type '(repeat (regexp :tag "Regular expression to match ham header"))
382 (defcustom spam-regex-body-spam '()
383 "Regular expression for positive body spam matches.
384 Only meaningful if you enable `spam-use-regex-body'."
385 :type '(repeat (regexp :tag "Regular expression to match spam body"))
388 (defcustom spam-regex-body-ham '()
389 "Regular expression for positive body ham matches.
390 Only meaningful if you enable `spam-use-regex-body'."
391 :type '(repeat (regexp :tag "Regular expression to match ham body"))
394 (defcustom spam-summary-score-preferred-header nil
395 "Preferred header to use for spam-summary-score."
396 :type '(choice :tag "Header name"
397 (symbol :tag "SpamAssassin etc" X-Spam-Status)
398 (symbol :tag "Bogofilter" X-Bogosity)
399 (const :tag "No preference, take best guess." nil))
402 (defgroup spam-ifile nil
403 "Spam ifile configuration."
406 (defcustom spam-ifile-path (exec-installed-p "ifile")
407 "File path of the ifile executable program."
408 :type '(choice (file :tag "Location of ifile")
409 (const :tag "ifile is not installed"))
412 (defcustom spam-ifile-database-path nil
413 "File path of the ifile database."
414 :type '(choice (file :tag "Location of the ifile database")
415 (const :tag "Use the default"))
418 (defcustom spam-ifile-spam-category "spam"
419 "Name of the spam ifile category."
423 (defcustom spam-ifile-ham-category nil
424 "Name of the ham ifile category.
425 If nil, the current group name will be used."
426 :type '(choice (string :tag "Use a fixed category")
427 (const :tag "Use the current group name"))
430 (defcustom spam-ifile-all-categories nil
431 "Whether the ifile check will return all categories, or just spam.
432 Set this to t if you want to use the `spam-split' invocation of ifile as
433 your main source of newsgroup names."
437 (defgroup spam-bogofilter nil
438 "Spam bogofilter configuration."
441 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
442 "File path of the Bogofilter executable program."
443 :type '(choice (file :tag "Location of bogofilter")
444 (const :tag "Bogofilter is not installed"))
445 :group 'spam-bogofilter)
447 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
449 (defcustom spam-bogofilter-header "X-Bogosity"
450 "The header that Bogofilter inserts in messages."
452 :group 'spam-bogofilter)
454 (defcustom spam-bogofilter-spam-switch "-s"
455 "The switch that Bogofilter uses to register spam messages."
457 :group 'spam-bogofilter)
459 (defcustom spam-bogofilter-ham-switch "-n"
460 "The switch that Bogofilter uses to register ham messages."
462 :group 'spam-bogofilter)
464 (defcustom spam-bogofilter-spam-strong-switch "-S"
465 "The switch that Bogofilter uses to unregister ham messages."
467 :group 'spam-bogofilter)
469 (defcustom spam-bogofilter-ham-strong-switch "-N"
470 "The switch that Bogofilter uses to unregister spam messages."
472 :group 'spam-bogofilter)
474 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
475 "The regex on `spam-bogofilter-header' for positive spam identification."
477 :group 'spam-bogofilter)
479 (defcustom spam-bogofilter-database-directory nil
480 "Directory path of the Bogofilter databases."
481 :type '(choice (directory
482 :tag "Location of the Bogofilter database directory")
483 (const :tag "Use the default"))
484 :group 'spam-bogofilter)
486 (defgroup spam-bsfilter nil
487 "Spam bsfilter configuration."
490 (defcustom spam-bsfilter-path (executable-find "bsfilter")
491 "File path of the Bsfilter executable program."
492 :type '(choice (file :tag "Location of bsfilter")
493 (const :tag "Bsfilter is not installed"))
494 :group 'spam-bsfilter)
496 (defcustom spam-bsfilter-header "X-Spam-Flag"
497 "The header inserted by Bsfilter to flag spam."
499 :group 'spam-bsfilter)
501 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
502 "The header that Bsfilter inserts in messages."
504 :group 'spam-bsfilter)
506 (defcustom spam-bsfilter-spam-switch "--add-spam"
507 "The switch that Bsfilter uses to register spam messages."
509 :group 'spam-bsfilter)
511 (defcustom spam-bsfilter-ham-switch "--add-ham"
512 "The switch that Bsfilter uses to register ham messages."
514 :group 'spam-bsfilter)
516 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
517 "The switch that Bsfilter uses to unregister ham messages."
519 :group 'spam-bsfilter)
521 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
522 "The switch that Bsfilter uses to unregister spam messages."
524 :group 'spam-bsfilter)
526 (defcustom spam-bsfilter-database-directory nil
527 "Directory path of the Bsfilter databases."
528 :type '(choice (directory
529 :tag "Location of the Bsfilter database directory")
530 (const :tag "Use the default"))
531 :group 'spam-bsfilter)
533 (defgroup spam-spamoracle nil
534 "Spam spamoracle configuration."
537 (defcustom spam-spamoracle-database nil
538 "Location of spamoracle database file.
539 When nil, use the default spamoracle database."
540 :type '(choice (directory :tag "Location of spamoracle database file.")
541 (const :tag "Use the default"))
542 :group 'spam-spamoracle)
544 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
545 "Location of the spamoracle binary."
546 :type '(choice (directory :tag "Location of the spamoracle binary")
547 (const :tag "Use the default"))
548 :group 'spam-spamoracle)
550 (defgroup spam-spamassassin nil
551 "Spam SpamAssassin configuration."
554 (defcustom spam-spamassassin-path (executable-find "spamassassin")
555 "File path of the spamassassin executable program.
556 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
557 spamd man pages for more information on these programs."
558 :type '(choice (file :tag "Location of spamc")
559 (const :tag "spamassassin is not installed"))
560 :group 'spam-spamassassin)
562 (defcustom spam-spamassassin-arguments ()
563 "Arguments to pass to the spamassassin executable.
564 This must be a list. For example, `(\"-C\" \"configfile\")'."
565 :type '(restricted-sexp :match-alternatives (listp))
566 :group 'spam-spamassassin)
568 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
569 "The header inserted by SpamAssassin to flag spam."
571 :group 'spam-spamassassin)
573 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
574 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
577 :group 'spam-spamassassin)
579 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
580 "The header inserted by SpamAssassin, giving extended scoring information"
582 :group 'spam-spamassassin)
584 (defcustom spam-sa-learn-path (executable-find "sa-learn")
585 "File path of the sa-learn executable program."
586 :type '(choice (file :tag "Location of spamassassin")
587 (const :tag "spamassassin is not installed"))
588 :group 'spam-spamassassin)
590 (defcustom spam-sa-learn-rebuild t
591 "Whether sa-learn should rebuild the database every time it is called
592 Enable this if you want sa-learn to rebuild the database automatically. Doing
593 this will slightly increase the running time of the spam registration process.
594 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
595 order for SpamAssassin to recognize the new registered spam."
597 :group 'spam-spamassassin)
599 (defcustom spam-sa-learn-spam-switch "--spam"
600 "The switch that sa-learn uses to register spam messages"
602 :group 'spam-spamassassin)
604 (defcustom spam-sa-learn-ham-switch "--ham"
605 "The switch that sa-learn uses to register ham messages"
607 :group 'spam-spamassassin)
609 (defcustom spam-sa-learn-unregister-switch "--forget"
610 "The switch that sa-learn uses to unregister messages messages"
612 :group 'spam-spamassassin)
614 (defgroup spam-crm114 nil
615 "Spam CRM114 Mailfilter configuration."
618 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
619 "File path of the CRM114 Mailfilter executable program."
620 :type '(choice (file :tag "Location of CRM114 Mailfilter")
621 (const :tag "CRM114 Mailfilter is not installed"))
624 (defcustom spam-crm114-header "X-CRM114-Status"
625 "The header that CRM114 Mailfilter inserts in messages."
629 (defcustom spam-crm114-spam-switch "--learnspam"
630 "The switch that CRM114 Mailfilter uses to register spam messages."
634 (defcustom spam-crm114-ham-switch "--learnnonspam"
635 "The switch that CRM114 Mailfilter uses to register ham messages."
639 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
640 "The switch that CRM114 Mailfilter uses to unregister ham messages."
644 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
645 "The switch that CRM114 Mailfilter uses to unregister spam messages."
649 (defcustom spam-crm114-positive-spam-header "^SPAM"
650 "The regex on `spam-crm114-header' for positive spam identification."
654 (defcustom spam-crm114-database-directory nil
655 "Directory path of the CRM114 Mailfilter databases."
656 :type '(choice (directory
657 :tag "Location of the CRM114 Mailfilter database directory")
658 (const :tag "Use the default"))
661 ;;; Key bindings for spam control.
663 (gnus-define-keys gnus-summary-mode-map
664 "St" spam-generic-score
665 "Sx" gnus-summary-mark-as-spam
666 "Mst" spam-generic-score
667 "Msx" gnus-summary-mark-as-spam
668 "\M-d" gnus-summary-mark-as-spam)
670 (defvar spam-cache-lookups t
671 "Whether spam.el will try to cache lookups using `spam-caches'.")
673 (defvar spam-caches (make-hash-table
676 "Cache of spam detection entries.")
678 (defvar spam-old-articles nil
679 "List of old ham and spam articles, generated when a group is entered.")
681 (defvar spam-split-disabled nil
682 "If non-nil, `spam-split' is disabled, and always returns nil.")
684 (defvar spam-split-last-successful-check nil
686 `spam-split' will set this to nil or a spam-use-XYZ check if it
689 ;; internal variables for backends
690 ;; TODO: find a way to create these on the fly in spam-install-backend-super
691 (defvar spam-use-copy nil)
692 (defvar spam-use-move nil)
693 (defvar spam-use-gmane nil)
694 (defvar spam-use-resend nil)
698 ;;{{{ convenience functions
700 (defun spam-clear-cache (symbol)
701 "Clear the spam-caches entry for a check."
702 (remhash symbol spam-caches))
704 (defun spam-xor (a b)
706 (and (or a b) (not (and a b))))
708 (defun spam-set-difference (list1 list2)
709 "Return a set difference of LIST1 and LIST2.
710 When either list is nil, the other is returned."
711 (if (and list1 list2)
712 ;; we have two non-nil lists
714 (dolist (item (append list1 list2))
715 (when (and (memq item list1) (memq item list2))
716 (setq list1 (delq item list1))
717 (setq list2 (delq item list2))))
718 (append list1 list2))
719 ;; if either of the lists was nil, return the other one
720 (if list1 list1 list2)))
722 (defun spam-group-ham-mark-p (group mark &optional spam)
723 "Checks if MARK is considered a ham mark in GROUP."
724 (when (stringp group)
725 (let* ((marks (spam-group-ham-marks group spam))
726 (marks (if (symbolp mark)
728 (mapcar 'symbol-value marks))))
731 (defun spam-group-spam-mark-p (group mark)
732 "Checks if MARK is considered a spam mark in GROUP."
733 (spam-group-ham-mark-p group mark t))
735 (defun spam-group-ham-marks (group &optional spam)
736 "In GROUP, get all the ham marks."
737 (when (stringp group)
738 (let* ((marks (if spam
739 (gnus-parameter-spam-marks group)
740 (gnus-parameter-ham-marks group)))
742 (marks (if (listp (car marks)) (car marks) marks)))
745 (defun spam-group-spam-marks (group)
746 "In GROUP, get all the spam marks."
747 (spam-group-ham-marks group t))
749 (defun spam-group-spam-contents-p (group)
750 "Is GROUP a spam group?"
751 (if (and (stringp group) (< 0 (length group)))
752 (or (member group spam-junk-mailgroups)
753 (memq 'gnus-group-spam-classification-spam
754 (gnus-parameter-spam-contents group)))
757 (defun spam-group-ham-contents-p (group)
758 "Is GROUP a ham group?"
760 (memq 'gnus-group-spam-classification-ham
761 (gnus-parameter-spam-contents group))
764 (defun spam-classifications ()
765 "Return list of valid classifications"
768 (defun spam-classification-valid-p (classification)
769 "Is CLASSIFICATION a valid spam/ham classification?"
770 (memq classification (spam-classifications)))
772 (defun spam-backend-properties ()
773 "Return list of valid classifications."
774 '(statistical mover check hrf srf huf suf))
776 (defun spam-backend-property-valid-p (property)
777 "Is PROPERTY a valid backend property?"
778 (memq property (spam-backend-properties)))
780 (defun spam-backend-function-type-valid-p (type)
781 (or (eq type 'registration)
782 (eq type 'unregistration)))
784 (defun spam-process-type-valid-p (process-type)
785 (or (eq process-type 'incoming)
786 (eq process-type 'process)))
788 (defun spam-list-articles (articles classification)
789 (let ((mark-check (if (eq classification 'spam)
790 'spam-group-spam-mark-p
791 'spam-group-ham-mark-p))
792 alist mark-cache-yes mark-cache-no)
793 (dolist (article articles)
794 (let ((mark (gnus-summary-article-mark article)))
795 (unless (or (memq mark mark-cache-yes)
796 (memq mark mark-cache-no))
797 (if (funcall mark-check
800 (push mark mark-cache-yes)
801 (push mark mark-cache-no)))
802 (when (memq mark mark-cache-yes)
803 (push article alist))))
808 ;;{{{ backend installation functions and procedures
810 (defun spam-install-backend-super (backend &rest properties)
811 "Install BACKEND for spam.el.
812 Accepts incoming CHECK, ham registration function HRF, spam
813 registration function SRF, ham unregistration function HUF, spam
814 unregistration function SUF, and an indication whether the
815 backend is STATISTICAL."
817 (setq spam-backends (add-to-list 'spam-backends backend))
819 (let ((property (pop properties))
820 (value (pop properties)))
821 (if (spam-backend-property-valid-p property)
822 (put backend property value)
825 "spam-install-backend-super got an invalid property %s"
828 (defun spam-backend-list (&optional type)
829 "Return a list of all the backend symbols, constrained by TYPE.
830 When TYPE is 'non-mover, only non-mover backends are returned.
831 When TYPE is 'mover, only mover backends are returned."
833 (dolist (backend spam-backends)
835 (null type) ;either no type was requested
836 ;; or the type is 'mover and the backend is a mover
839 (spam-backend-mover-p backend))
840 ;; or the type is 'non-mover and the backend is not a mover
843 (not (spam-backend-mover-p backend))))
844 (push backend list)))
847 (defun spam-backend-check (backend)
848 "Get the check function for BACKEND.
849 Each individual check may return nil, t, or a mailgroup name.
850 The value nil means that the check does not yield a decision, and
851 so, that further checks are needed. The value t means that the
852 message is definitely not spam, and that further spam checks
853 should be inhibited. Otherwise, a mailgroup name or the symbol
854 'spam (depending on spam-split-symbolic-return) is returned where
855 the mail should go, and further checks are also inhibited. The
856 usual mailgroup name is the value of `spam-split-group', meaning
857 that the message is definitely a spam."
858 (get backend 'check))
860 (defun spam-backend-valid-p (backend)
862 (member backend (spam-backend-list)))
864 (defun spam-backend-info (backend)
865 "Return information about BACKEND."
866 (if (spam-backend-valid-p backend)
868 (setq info (format "Backend %s has the following properties:\n"
870 (dolist (property (spam-backend-properties))
871 (setq info (format "%s%s=%s\n"
874 (get backend property))))
876 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
879 (defun spam-backend-function (backend classification type)
880 "Get the BACKEND function for CLASSIFICATION and TYPE.
881 TYPE is 'registration or 'unregistration.
882 CLASSIFICATION is 'ham or 'spam."
884 (spam-classification-valid-p classification)
885 (spam-backend-function-type-valid-p type))
888 (format "spam-backend-%s-%s-function"
891 (funcall retrieval backend))
894 "%s was passed invalid backend %s, classification %s, or type %s"
895 "spam-backend-function"
900 (defun spam-backend-article-list-property (classification
901 &optional unregister)
902 "Property name of article list with CLASSIFICATION and UNREGISTER."
903 (let* ((r (if unregister "unregister" "register"))
904 (prop (format "%s-%s" classification r)))
907 (defun spam-backend-get-article-todo-list (backend
909 &optional unregister)
910 "Get the articles to be processed for BACKEND and CLASSIFICATION.
911 With UNREGISTER, get articles to be unregistered.
912 This is a temporary storage function - nothing here persists."
915 (intern (spam-backend-article-list-property classification unregister))))
917 (defun spam-backend-put-article-todo-list (backend classification list &optional unregister)
918 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
919 With UNREGISTER, set articles to be unregistered.
920 This is a temporary storage function - nothing here persists."
923 (intern (spam-backend-article-list-property classification unregister))
926 (defun spam-backend-ham-registration-function (backend)
927 "Get the ham registration function for BACKEND."
930 (defun spam-backend-spam-registration-function (backend)
931 "Get the spam registration function for BACKEND."
934 (defun spam-backend-ham-unregistration-function (backend)
935 "Get the ham unregistration function for BACKEND."
938 (defun spam-backend-spam-unregistration-function (backend)
939 "Get the spam unregistration function for BACKEND."
942 (defun spam-backend-statistical-p (backend)
943 "Is BACKEND statistical?"
944 (get backend 'statistical))
946 (defun spam-backend-mover-p (backend)
947 "Is BACKEND a mover?"
948 (get backend 'mover))
950 (defun spam-install-backend-alias (backend alias)
951 "Add ALIAS to an existing BACKEND.
952 The previous backend settings for ALIAS are erased."
954 ;; install alias with no properties at first
955 (spam-install-backend-super alias)
957 (dolist (property (spam-backend-properties))
958 (put alias property (get backend property))))
960 (defun spam-install-checkonly-backend (backend check)
961 "Install a BACKEND than can only CHECK for spam."
962 (spam-install-backend-super backend 'check check))
964 (defun spam-install-mover-backend (backend hrf srf huf suf)
965 "Install a BACKEND than can move articles at summary exit.
966 Accepts ham registration function HRF, spam registration function
967 SRF, ham unregistration function HUF, spam unregistration
968 function SUF. The backend has no incoming check and can't be
970 (spam-install-backend-super
972 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
974 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
975 "Install a BACKEND than has no check.
976 Accepts ham registration function HRF, spam registration function
977 SRF, ham unregistration function HUF, spam unregistration
978 function SUF. The backend has no incoming check and can't be
979 statistical (it could be, but in practice that doesn't happen)."
980 (spam-install-backend-super
982 'hrf hrf 'srf srf 'huf huf 'suf suf))
984 (defun spam-install-backend (backend check hrf srf huf suf)
986 Accepts incoming CHECK, ham registration function HRF, spam
987 registration function SRF, ham unregistration function HUF, spam
988 unregistration function SUF. The backend won't be
989 statistical (use spam-install-statistical-backend for that)."
990 (spam-install-backend-super
992 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
994 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
996 Accepts incoming CHECK, ham registration function HRF, spam
997 registration function SRF, ham unregistration function HUF, spam
998 unregistration function SUF. The backend will be
999 statistical (use spam-install-backend for non-statistical
1001 (spam-install-backend-super
1003 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1005 (defun spam-install-statistical-checkonly-backend (backend check)
1006 "Install a statistical BACKEND than can only CHECK for spam."
1007 (spam-install-backend-super
1009 'check check 'statistical t))
1013 ;;{{{ backend installations
1014 (spam-install-checkonly-backend 'spam-use-blackholes
1015 'spam-check-blackholes)
1017 (spam-install-checkonly-backend 'spam-use-hashcash
1018 'spam-check-hashcash)
1020 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1021 'spam-check-spamassassin-headers)
1023 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1024 'spam-check-bogofilter-headers)
1026 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1027 'spam-check-bsfilter-headers)
1029 (spam-install-checkonly-backend 'spam-use-gmane-xref
1030 'spam-check-gmane-xref)
1032 (spam-install-checkonly-backend 'spam-use-regex-headers
1033 'spam-check-regex-headers)
1035 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1036 'spam-check-regex-body)
1038 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy) instead
1039 (spam-install-mover-backend 'spam-use-move
1040 'spam-move-ham-routine
1041 'spam-move-spam-routine
1045 (spam-install-nocheck-backend 'spam-use-copy
1046 'spam-copy-ham-routine
1047 'spam-copy-spam-routine
1051 (spam-install-nocheck-backend 'spam-use-gmane
1053 'spam-report-gmane-register-routine
1054 ;; does Gmane support unregistration?
1058 (spam-install-nocheck-backend 'spam-use-resend
1059 'spam-report-resend-register-ham-routine
1060 'spam-report-resend-register-routine
1064 (spam-install-backend 'spam-use-BBDB
1066 'spam-BBDB-register-routine
1068 'spam-BBDB-unregister-routine
1071 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1073 (spam-install-backend 'spam-use-blacklist
1074 'spam-check-blacklist
1076 'spam-blacklist-register-routine
1078 'spam-blacklist-unregister-routine)
1080 (spam-install-backend 'spam-use-whitelist
1081 'spam-check-whitelist
1082 'spam-whitelist-register-routine
1084 'spam-whitelist-unregister-routine
1087 (spam-install-statistical-backend 'spam-use-ifile
1089 'spam-ifile-register-ham-routine
1090 'spam-ifile-register-spam-routine
1091 'spam-ifile-unregister-ham-routine
1092 'spam-ifile-unregister-spam-routine)
1094 (spam-install-statistical-backend 'spam-use-spamoracle
1095 'spam-check-spamoracle
1096 'spam-spamoracle-learn-ham
1097 'spam-spamoracle-learn-spam
1098 'spam-spamoracle-unlearn-ham
1099 'spam-spamoracle-unlearn-spam)
1101 (spam-install-statistical-backend 'spam-use-stat
1103 'spam-stat-register-ham-routine
1104 'spam-stat-register-spam-routine
1105 'spam-stat-unregister-ham-routine
1106 'spam-stat-unregister-spam-routine)
1108 (spam-install-statistical-backend 'spam-use-spamassassin
1109 'spam-check-spamassassin
1110 'spam-spamassassin-register-ham-routine
1111 'spam-spamassassin-register-spam-routine
1112 'spam-spamassassin-unregister-ham-routine
1113 'spam-spamassassin-unregister-spam-routine)
1115 (spam-install-statistical-backend 'spam-use-bogofilter
1116 'spam-check-bogofilter
1117 'spam-bogofilter-register-ham-routine
1118 'spam-bogofilter-register-spam-routine
1119 'spam-bogofilter-unregister-ham-routine
1120 'spam-bogofilter-unregister-spam-routine)
1122 (spam-install-statistical-backend 'spam-use-bsfilter
1123 'spam-check-bsfilter
1124 'spam-bsfilter-register-ham-routine
1125 'spam-bsfilter-register-spam-routine
1126 'spam-bsfilter-unregister-ham-routine
1127 'spam-bsfilter-unregister-spam-routine)
1129 (spam-install-statistical-backend 'spam-use-crm114
1131 'spam-crm114-register-ham-routine
1132 'spam-crm114-register-spam-routine
1133 ;; does CRM114 Mailfilter support unregistration?
1139 ;;{{{ scoring and summary formatting
1140 (defun spam-necessary-extra-headers ()
1141 "Return the extra headers spam.el thinks are necessary."
1143 (when (or spam-use-spamassassin
1144 spam-use-spamassassin-headers
1145 spam-use-regex-headers)
1146 (push 'X-Spam-Status list))
1147 (when spam-use-bogofilter
1148 (push 'X-Bogosity list))
1151 (defun spam-user-format-function-S (headers)
1154 (spam-summary-score headers spam-summary-score-preferred-header))))
1156 (defun spam-article-sort-by-spam-status (h1 h2)
1157 "Sort articles by score."
1159 (dolist (header (spam-necessary-extra-headers))
1160 (let ((s1 (spam-summary-score h1 header))
1161 (s2 (spam-summary-score h2 header)))
1163 (setq result (< s1 s2))
1167 (defun spam-extra-header-to-number (header headers)
1168 "Transform an extra HEADER to a number, using list of HEADERS.
1169 Note this has to be fast."
1170 (if (gnus-extra-header header headers)
1172 ((eq header 'X-Spam-Status)
1173 (string-to-number (gnus-replace-in-string
1174 (gnus-extra-header header headers)
1176 ;; for CRM checking, it's probably faster to just do the string match
1177 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
1178 (match-string 1 header))
1179 ((eq header 'X-Bogosity)
1180 (string-to-number (gnus-replace-in-string
1181 (gnus-replace-in-string
1182 (gnus-extra-header header headers)
1188 (defun spam-summary-score (headers &optional specific-header)
1189 "Score an article for the summary buffer, as fast as possible.
1190 With SPECIFIC-HEADER, returns only that header's score.
1191 Will not return a nil score."
1195 (list specific-header)
1196 (spam-necessary-extra-headers)))
1198 (spam-extra-header-to-number header headers))
1203 (defun spam-generic-score (&optional recheck)
1204 "Invoke whatever scoring method we can."
1207 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1208 (spam-spamassassin-score recheck))
1209 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1210 (spam-bsfilter-score recheck))
1212 (spam-crm114-score))
1213 (t (spam-bogofilter-score recheck))))
1216 ;;{{{ set up widening, processor checks
1218 ;;; set up IMAP widening if it's necessary
1219 (defun spam-setup-widening ()
1220 (when (spam-widening-needed-p)
1221 (setq nnimap-split-download-body-default t)))
1223 (defun spam-widening-needed-p (&optional force-symbols)
1225 (dolist (backend (spam-backend-list))
1226 (when (and (spam-backend-statistical-p backend)
1227 (or (symbol-value backend)
1228 (memq backend force-symbols)))
1229 (setq found backend)))
1232 (defvar spam-list-of-processors
1233 ;; note the nil processors are not defined in gnus.el
1234 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1235 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1236 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1237 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1238 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1239 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1240 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1241 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1242 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1243 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1244 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1245 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1246 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1247 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1248 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1249 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1250 "The OBSOLETE `spam-list-of-processors' list.
1251 This list contains pairs associating the obsolete ham/spam exit
1252 processor variables with a classification and a spam-use-*
1253 variable. When the processor variable is nil, just the
1254 classification and spam-use-* check variable are used. This is
1255 superceded by the new spam backend code, so it's only consulted
1256 for backwards compatibility.")
1258 (defun spam-group-processor-p (group backend &optional classification)
1259 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1260 Also accepts the obsolete processors, which can be found in
1261 gnus.el and in spam-list-of-processors. In the case of mover
1262 backends, checks the setting of spam-summary-exit-behavior in
1263 addition to the set values for the group."
1264 (if (and (stringp group)
1266 (let ((old-style (assq backend spam-list-of-processors))
1267 (parameters (nth 0 (gnus-parameter-spam-process group)))
1269 (if old-style ; old-style processor
1270 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1271 ;; now search for the parameter
1272 (dolist (parameter parameters)
1273 (when (and (null found)
1275 (eq classification (nth 0 parameter))
1276 (eq backend (nth 1 parameter)))
1279 ;; now, if the parameter was not found, do the
1280 ;; spam-summary-exit-behavior-logic for mover backends
1282 (when (spam-backend-mover-p backend)
1286 ((eq spam-summary-exit-behavior 'move-all) t)
1287 ((eq spam-summary-exit-behavior 'move-none) nil)
1288 ((eq spam-summary-exit-behavior 'default)
1289 (or (eq classification 'spam) ;move spam out of all groups
1290 ;; move ham out of spam groups
1291 (and (eq classification 'ham)
1292 (spam-group-spam-contents-p group))))
1293 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1294 spam-summary-exit-behavior))))))
1301 ;;{{{ Summary entry and exit processing.
1303 (defun spam-mark-junk-as-spam-routine ()
1304 ;; check the global list of group names spam-junk-mailgroups and the
1306 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1307 (gnus-message 6 "Marking %s articles as spam"
1308 (if spam-mark-only-unseen-as-spam
1311 (let ((articles (if spam-mark-only-unseen-as-spam
1312 gnus-newsgroup-unseen
1313 gnus-newsgroup-unreads)))
1314 (if spam-mark-new-messages-in-spam-group-as-spam
1315 (dolist (article articles)
1316 (gnus-summary-mark-article article gnus-spam-mark))
1317 (gnus-message 9 "Did not mark new messages as spam.")))))
1319 (defun spam-summary-prepare ()
1320 (setq spam-old-articles
1321 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1322 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1323 (spam-mark-junk-as-spam-routine))
1325 ;; The spam processors are invoked for any group, spam or ham or neither
1326 (defun spam-summary-prepare-exit ()
1327 (unless gnus-group-is-exiting-without-update-p
1328 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1330 ;; before we begin, remove any article limits
1332 ; (gnus-summary-pop-limit t))
1334 ;; first of all, unregister any articles that are no longer ham or spam
1335 ;; we have to iterate over the processors, or else we'll be too slow
1336 (dolist (classification (spam-classifications))
1337 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1338 (new-articles (spam-list-articles
1339 gnus-newsgroup-articles
1341 (changed-articles (spam-set-difference new-articles old-articles)))
1342 ;; now that we have the changed articles, we go through the processors
1343 (dolist (backend (spam-backend-list))
1344 (let (unregister-list)
1345 (dolist (article changed-articles)
1346 (let ((id (spam-fetch-field-message-id-fast article)))
1347 (when (spam-log-unregistration-needed-p
1348 id 'process classification backend)
1349 (push article unregister-list))))
1350 ;; call spam-register-routine with specific articles to unregister,
1351 ;; when there are articles to unregister and the check is enabled
1352 (when (and unregister-list (symbol-value backend))
1353 (spam-backend-put-article-todo-list backend
1358 ;; do the non-moving backends first, then the moving ones
1359 (dolist (backend-type '(non-mover mover))
1360 (dolist (classification (spam-classifications))
1361 (dolist (backend (spam-backend-list backend-type))
1362 (when (spam-group-processor-p
1366 (spam-backend-put-article-todo-list backend
1369 gnus-newsgroup-articles
1370 classification))))))
1372 (spam-resolve-registrations-routine) ; do the registrations now
1374 ;; we mark all the leftover spam articles as expired at the end
1375 (dolist (article (spam-list-articles
1376 gnus-newsgroup-articles
1378 (gnus-summary-mark-article article gnus-expirable-mark)))
1380 (setq spam-old-articles nil))
1384 ;;{{{ spam-use-move and spam-use-copy backend support functions
1386 (defun spam-copy-or-move-routine (copy groups articles classification)
1388 (when (and (car-safe groups) (listp (car-safe groups)))
1389 (setq groups (pop groups)))
1391 (unless (listp groups)
1392 (setq groups (list groups)))
1394 ;; remove the current process mark
1395 (gnus-summary-kill-process-mark)
1397 (let ((backend-supports-deletions
1398 (gnus-check-backend-function
1399 'request-move-article gnus-newsgroup-name))
1400 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1401 article mark deletep respool)
1403 (when (member 'respool groups)
1404 (setq respool t) ; boolean for later
1405 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1407 ;; now do the actual move
1408 (dolist (group groups)
1409 (when (and articles (stringp group))
1411 ;; first, mark the article with the process mark and, if needed,
1412 ;; the unread or expired mark (for ham and spam respectively)
1413 (dolist (article articles)
1414 (when (and (eq classification 'ham)
1415 spam-mark-ham-unread-before-move-from-spam-group)
1416 (gnus-message 9 "Marking ham article %d unread before move"
1418 (gnus-summary-mark-article article gnus-unread-mark))
1419 (when (and (eq classification 'spam)
1421 (gnus-message 9 "Marking spam article %d expirable before move"
1423 (gnus-summary-mark-article article gnus-expirable-mark))
1424 (gnus-summary-set-process-mark article)
1426 (if respool ; respooling is with a "fake" group
1427 (let ((spam-split-disabled
1428 (or spam-split-disabled
1429 (and (eq classification 'ham)
1430 spam-disable-spam-split-during-ham-respool))))
1431 (gnus-message 9 "Respooling article %d with method %s"
1432 article respool-method)
1433 (gnus-summary-respool-article nil respool-method))
1434 (if (or (not backend-supports-deletions) ; else, we are not respooling
1435 (> (length groups) 1))
1436 (progn ; if copying, copy and set deletep
1437 (gnus-message 9 "Copying article %d to group %s"
1439 (gnus-summary-copy-article nil group)
1441 (gnus-message 9 "Moving article %d to group %s"
1443 (gnus-summary-move-article nil group))))) ; else move articles
1445 ;; now delete the articles, unless a) copy is t, and there was a copy done
1446 ;; b) a move was done to a single group
1447 ;; c) backend-supports-deletions is nil
1449 (when (and deletep backend-supports-deletions)
1450 (dolist (article articles)
1451 (gnus-summary-set-process-mark article)
1452 (gnus-message 9 "Deleting article %d" article))
1454 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1455 (gnus-summary-delete-article nil)))))
1457 (gnus-summary-yank-process-mark)
1458 (length articles))))
1460 (defun spam-copy-spam-routine (articles)
1461 (spam-copy-or-move-routine
1463 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1467 (defun spam-move-spam-routine (articles)
1468 (spam-copy-or-move-routine
1470 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1474 (defun spam-copy-ham-routine (articles)
1475 (spam-copy-or-move-routine
1477 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1481 (defun spam-move-ham-routine (articles)
1482 (spam-copy-or-move-routine
1484 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1490 ;;{{{ article and field retrieval code
1491 (defun spam-get-article-as-string (article)
1492 (when (numberp article)
1494 (gnus-request-article-this-buffer
1496 gnus-newsgroup-name)
1500 ;; (defun spam-get-article-as-filename (article)
1501 ;; (let ((article-filename))
1502 ;; (when (numberp article)
1503 ;; (nnml-possibly-change-directory
1504 ;; (gnus-group-real-name gnus-newsgroup-name))
1505 ;; (setq article-filename (expand-file-name
1506 ;; (int-to-string article) nnml-current-directory)))
1507 ;; (if (file-exists-p article-filename)
1511 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1512 "Fetch a FIELD for ARTICLE quickly, using the internal gnus-data-list function.
1513 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1514 When FIELD is 'number, ARTICLE can be any number (since we want
1516 (when (numberp article)
1517 (let* ((data-header (or prepared-data-header
1518 (spam-fetch-article-header article))))
1519 (if (arrayp data-header)
1521 ((equal field 'number)
1522 (mail-header-number data-header))
1523 ((equal field 'from)
1524 (mail-header-from data-header))
1525 ((equal field 'message-id)
1526 (mail-header-message-id data-header))
1527 ((equal field 'subject)
1528 (mail-header-subject data-header))
1529 ((equal field 'references)
1530 (mail-header-references data-header))
1531 ((equal field 'date)
1532 (mail-header-date data-header))
1533 ((equal field 'xref)
1534 (mail-header-xref data-header))
1535 ((equal field 'extra)
1536 (mail-header-extra data-header))
1540 "spam-fetch-field-fast: unknown field %s requested"
1543 (gnus-message 6 "Article %d has a nil data header" article)))))
1545 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1546 (spam-fetch-field-fast article 'from prepared-data-header))
1548 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1549 (spam-fetch-field-fast article 'subject prepared-data-header))
1551 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1552 (spam-fetch-field-fast article 'message-id prepared-data-header))
1554 (defun spam-generate-fake-headers (article)
1555 (let ((dh (spam-fetch-article-header article)))
1559 ;; 80-character limit makes for strange constructs
1560 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1561 "Date: %s\nReferences: %s\nXref: %s\n")
1562 (spam-fetch-field-fast article 'from dh)
1563 (spam-fetch-field-fast article 'subject dh)
1564 (spam-fetch-field-fast article 'message-id dh)
1565 (spam-fetch-field-fast article 'date dh)
1566 (spam-fetch-field-fast article 'references dh)
1567 (spam-fetch-field-fast article 'xref dh))
1568 (when (spam-fetch-field-fast article 'extra dh)
1569 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1572 "spam-generate-fake-headers: article %d didn't have a valid header"
1575 (defun spam-fetch-article-header (article)
1577 (set-buffer gnus-summary-buffer)
1578 (gnus-read-header article)
1579 (nth 3 (assq article gnus-newsgroup-data))))
1582 ;;{{{ Spam determination.
1584 (defun spam-split (&rest specific-checks)
1585 "Split this message into the `spam' group if it is spam.
1586 This function can be used as an entry in the variable `nnmail-split-fancy',
1587 for example like this: (: spam-split). It can take checks as
1588 parameters. A string as a parameter will set the
1589 spam-split-group to that string.
1591 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1593 (setq spam-split-last-successful-check nil)
1594 (unless spam-split-disabled
1595 (let ((spam-split-group-choice spam-split-group))
1596 (dolist (check specific-checks)
1597 (when (stringp check)
1598 (setq spam-split-group-choice check)
1599 (setq specific-checks (delq check specific-checks))))
1601 (let ((spam-split-group spam-split-group-choice)
1602 (widening-needed-check (spam-widening-needed-p specific-checks)))
1605 (when widening-needed-check
1607 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1608 widening-needed-check))
1609 (let ((backends (spam-backend-list))
1611 (while (and backends (not decision))
1612 (let* ((backend (pop backends))
1613 (check-function (spam-backend-check backend))
1614 (spam-split-group (if spam-split-symbolic-return
1618 ;; either, given specific checks, this is one of them
1619 (memq backend specific-checks)
1620 ;; or, given no specific checks, spam-use-CHECK is set
1621 (and (null specific-checks) (symbol-value backend)))
1622 (gnus-message 6 "spam-split: calling the %s function"
1624 (setq decision (funcall check-function))
1625 ;; if we got a decision at all, save the current check
1627 (setq spam-split-last-successful-check backend))
1629 (when (eq decision 'spam)
1630 (unless spam-split-symbolic-return
1633 (format "spam-split got %s but %s is nil"
1635 spam-split-symbolic-return)))))))
1637 (if spam-split-symbolic-return-positive 'ham nil)
1640 (defun spam-find-spam ()
1641 "This function will detect spam in the current newsgroup using spam-split."
1644 (let* ((group gnus-newsgroup-name)
1645 (autodetect (gnus-parameter-spam-autodetect group))
1646 (methods (gnus-parameter-spam-autodetect-methods group))
1647 (first-method (nth 0 methods))
1648 (articles (if spam-autodetect-recheck-messages
1649 gnus-newsgroup-articles
1650 gnus-newsgroup-unseen))
1651 article-cannot-be-faked)
1654 (dolist (backend methods)
1655 (when (spam-backend-statistical-p backend)
1656 (setq article-cannot-be-faked t)
1659 (when (memq 'default methods)
1660 (setq article-cannot-be-faked t))
1662 (when (and autodetect
1663 (not (equal first-method 'none)))
1666 (let ((id (spam-fetch-field-message-id-fast article))
1667 (subject (spam-fetch-field-subject-fast article))
1668 (sender (spam-fetch-field-from-fast article))
1672 (gnus-message 6 "Article %d has no message ID!" article))
1674 (when (and id spam-log-to-registry)
1675 (setq registry-lookup (spam-log-registration-type id 'incoming))
1676 (when registry-lookup
1679 "spam-find-spam: message %s was already registered incoming"
1682 (let* ((spam-split-symbolic-return t)
1683 (spam-split-symbolic-return-positive t)
1684 (fake-headers (spam-generate-fake-headers article))
1688 (if article-cannot-be-faked
1689 (gnus-request-article-this-buffer
1692 ;; else, we fake the article
1693 (when fake-headers (insert fake-headers)))
1694 (if (or (null first-method)
1695 (equal first-method 'default))
1697 (apply 'spam-split methods))))))
1698 (if (equal split-return 'spam)
1699 (gnus-summary-mark-article article gnus-spam-mark))
1701 (when (and id split-return spam-log-to-registry)
1702 (when (zerop (gnus-registry-group-count id))
1703 (gnus-registry-add-group
1704 id group subject sender))
1706 (unless registry-lookup
1707 (spam-log-processing-to-registry
1711 spam-split-last-successful-check
1717 ;;{{{ registration/unregistration functions
1719 (defun spam-resolve-registrations-routine ()
1720 "Go through the backends and register or unregister articles as needed."
1721 (dolist (backend-type '(non-mover mover))
1722 (dolist (classification (spam-classifications))
1723 (dolist (backend (spam-backend-list backend-type))
1724 (let ((rlist (spam-backend-get-article-todo-list
1725 backend classification))
1726 (ulist (spam-backend-get-article-todo-list
1727 backend classification t))
1730 ;; clear the old lists right away
1731 (spam-backend-put-article-todo-list backend
1735 (spam-backend-put-article-todo-list backend
1740 ;; eliminate duplicates
1741 (dolist (article (copy-sequence ulist))
1742 (when (memq article rlist)
1744 (setq rlist (delq article rlist))
1745 (setq ulist (delq article ulist))))
1747 (unless (zerop delcount)
1750 "%d messages were saved the trouble of unregistering and then registering"
1753 ;; unregister articles
1754 (unless (zerop (length ulist))
1755 (let ((num (spam-unregister-routine classification backend ulist)))
1759 "%d %s messages were unregistered by backend %s."
1764 ;; register articles
1765 (unless (zerop (length rlist))
1766 (let ((num (spam-register-routine classification backend rlist)))
1770 "%d %s messages were registered by backend %s."
1775 (defun spam-unregister-routine (classification
1778 (spam-register-routine classification backend specific-articles t))
1780 (defun spam-register-routine (classification
1783 &optional unregister)
1784 (when (and (spam-classification-valid-p classification)
1785 (spam-backend-valid-p backend))
1786 (let* ((register-function
1787 (spam-backend-function backend classification 'registration))
1788 (unregister-function
1789 (spam-backend-function backend classification 'unregistration))
1790 (run-function (if unregister
1793 (log-function (if unregister
1794 'spam-log-undo-registration
1795 'spam-log-processing-to-registry))
1799 ;; make list of articles, using specific-articles if given
1800 (setq articles (or specific-articles
1802 gnus-newsgroup-articles
1805 (when (> (length articles) 0)
1806 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1807 (if unregister "Unregistering" "Registering")
1809 (if specific-articles "specific" "")
1812 (funcall run-function articles)
1813 ;; now log all the registrations (or undo them, depending on
1815 (dolist (article articles)
1816 (funcall log-function
1817 (spam-fetch-field-message-id-fast article)
1821 gnus-newsgroup-name))))
1822 ;; return the number of articles processed
1823 (length articles))))
1825 ;;; log a ham- or spam-processor invocation to the registry
1826 (defun spam-log-processing-to-registry (id type classification backend group)
1827 (when spam-log-to-registry
1828 (if (and (stringp id)
1830 (spam-process-type-valid-p type)
1831 (spam-classification-valid-p classification)
1832 (spam-backend-valid-p backend))
1833 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1834 (cell (list classification backend group)))
1835 (push cell cell-list)
1836 (gnus-registry-store-extra-entry
1843 (format "%s call with bad ID, type, classification, spam-backend, or group"
1844 "spam-log-processing-to-registry")))))
1846 ;;; check if a ham- or spam-processor registration has been done
1847 (defun spam-log-registered-p (id type)
1848 (when spam-log-to-registry
1849 (if (and (stringp id)
1850 (spam-process-type-valid-p type))
1851 (cdr-safe (gnus-registry-fetch-extra id type))
1855 (format "%s called with bad ID, type, classification, or spam-backend"
1856 "spam-log-registered-p"))
1859 ;;; check what a ham- or spam-processor registration says
1860 ;;; returns nil if conflicting registrations are found
1861 (defun spam-log-registration-type (id type)
1864 (dolist (reg (spam-log-registered-p id type))
1865 (let ((classification (nth 0 reg)))
1866 (when (spam-classification-valid-p classification)
1868 (not (eq classification decision)))
1869 (setq count (+ 1 count)))
1870 (setq decision classification))))
1876 ;;; check if a ham- or spam-processor registration needs to be undone
1877 (defun spam-log-unregistration-needed-p (id type classification backend)
1878 (when spam-log-to-registry
1879 (if (and (stringp id)
1880 (spam-process-type-valid-p type)
1881 (spam-classification-valid-p classification)
1882 (spam-backend-valid-p backend))
1883 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1885 (dolist (cell cell-list)
1887 (when (and (eq classification (nth 0 cell))
1888 (eq backend (nth 1 cell)))
1894 (format "%s called with bad ID, type, classification, or spam-backend"
1895 "spam-log-unregistration-needed-p"))
1899 ;;; undo a ham- or spam-processor registration (the group is not used)
1900 (defun spam-log-undo-registration (id type classification backend &optional group)
1901 (when (and spam-log-to-registry
1902 (spam-log-unregistration-needed-p id type classification backend))
1903 (if (and (stringp id)
1904 (spam-process-type-valid-p type)
1905 (spam-classification-valid-p classification)
1906 (spam-backend-valid-p backend))
1907 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1908 new-cell-list found)
1909 (dolist (cell cell-list)
1910 (unless (and (eq classification (nth 0 cell))
1911 (eq backend (nth 1 cell)))
1912 (push cell new-cell-list)))
1913 (gnus-registry-store-extra-entry
1918 (gnus-error 7 (format "%s call with bad ID, type, spam-backend, or group"
1919 "spam-log-undo-registration"))
1924 ;;{{{ backend functions
1927 (defun spam-check-gmane-xref ()
1929 (message-fetch-field "Xref")
1930 (message-fetch-field "Newsgroups"))))
1931 (when header ; return nil when no header
1932 (when (string-match spam-gmane-xref-spam-group
1934 spam-split-group))))
1940 (defun spam-check-regex-body ()
1941 (let ((spam-regex-headers-ham spam-regex-body-ham)
1942 (spam-regex-headers-spam spam-regex-body-spam))
1943 (spam-check-regex-headers t)))
1949 (defun spam-check-regex-headers (&optional body)
1950 (let ((type (if body "body" "header"))
1952 (dolist (h-regex spam-regex-headers-ham)
1954 (goto-char (point-min))
1955 (when (re-search-forward h-regex nil t)
1956 (message "Ham regex %s search positive." type)
1958 (dolist (s-regex spam-regex-headers-spam)
1960 (goto-char (point-min))
1961 (when (re-search-forward s-regex nil t)
1962 (message "Spam regex %s search positive." type)
1964 (setq ret spam-split-group))))
1971 (defun spam-reverse-ip-string (ip)
1973 (mapconcat 'identity
1974 (nreverse (split-string ip "\\."))
1977 (defun spam-check-blackholes ()
1978 "Check the Received headers for blackholed relays."
1979 (let ((headers (message-fetch-field "received"))
1984 (goto-char (point-min))
1985 (gnus-message 6 "Checking headers for relay addresses")
1986 (while (re-search-forward
1987 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1988 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1989 (push (spam-reverse-ip-string (match-string 1))
1991 (dolist (server spam-blackhole-servers)
1993 (unless (and spam-blackhole-good-server-regex
1994 ;; match the good-server-regex against the reversed (again) IP string
1996 spam-blackhole-good-server-regex
1997 (spam-reverse-ip-string ip)))
1999 (let ((query-string (concat ip "." server)))
2001 (let ((query-result (query-dig query-string)))
2003 (gnus-message 6 "(DIG): positive blackhole check '%s'"
2005 (push (list ip server query-result)
2007 ;; else, if not using dig.el
2008 (when (query-dns query-string)
2009 (gnus-message 6 "positive blackhole check")
2010 (push (list ip server (query-dns query-string 'TXT))
2019 (autoload 'mail-check-payment "hashcash"))
2025 (defun spam-check-hashcash ()
2026 "Check the headers for hashcash payments."
2027 (mail-check-payment))) ;mail-check-payment returns a boolean
2034 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2035 ;;; <sacha@giotto.sj.ru>
2037 ;; all this is done inside a condition-case to trap errors
2040 (autoload 'bbdb-buffer "bbdb")
2041 (autoload 'bbdb-create-internal "bbdb")
2042 (autoload 'bbdb-search-simple "bbdb"))
2045 (when (condition-case nil
2048 (require 'bbdb-com))
2050 ;; `bbdb-records' should not be bound as an autoload function
2051 ;; before loading bbdb because of `bbdb-hashtable-size'.
2052 (defalias 'bbdb-records 'ignore)
2053 (defalias 'spam-BBDB-register-routine 'ignore)
2054 (defalias 'spam-enter-ham-BBDB 'ignore)
2057 ;; when the BBDB changes, we want to clear out our cache
2058 (defun spam-clear-cache-BBDB (&rest immaterial)
2059 (spam-clear-cache 'spam-use-BBDB))
2061 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2063 (defun spam-enter-ham-BBDB (addresses &optional remove)
2064 "Enter an address into the BBDB; implies ham (non-spam) sender"
2065 (dolist (from addresses)
2066 (when (stringp from)
2067 (let* ((parsed-address (gnus-extract-address-components from))
2068 (name (or (nth 0 parsed-address) "Ham Sender"))
2069 (remove-function (if remove
2070 'bbdb-delete-record-internal
2072 (net-address (nth 1 parsed-address))
2073 (record (and net-address
2074 (bbdb-search-simple nil net-address))))
2076 (gnus-message 6 "%s address %s %s BBDB"
2077 (if remove "Deleting" "Adding")
2079 (if remove "from" "to"))
2081 (funcall remove-function record)
2082 (bbdb-create-internal name nil net-address nil nil
2083 "ham sender added by spam.el")))))))
2085 (defun spam-BBDB-register-routine (articles &optional unregister)
2087 (dolist (article articles)
2088 (when (stringp (spam-fetch-field-from-fast article))
2089 (push (spam-fetch-field-from-fast article) addresses)))
2090 ;; now do the register/unregister action
2091 (spam-enter-ham-BBDB addresses unregister)))
2093 (defun spam-BBDB-unregister-routine (articles)
2094 (spam-BBDB-register-routine articles t))
2096 (defun spam-check-BBDB ()
2097 "Mail from people in the BBDB is classified as ham or non-spam"
2098 (let ((who (message-fetch-field "from"))
2099 bbdb-cache bbdb-hashtable)
2100 (when spam-cache-lookups
2101 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
2103 (setq bbdb-cache (make-vector 17 0)) ; a good starting hash value
2104 ;; this is based on the expanded (bbdb-hashtable) macro
2105 ;; without the debugging support
2106 (with-current-buffer (bbdb-buffer)
2108 (save-window-excursion
2109 (bbdb-records nil t)
2112 (intern (downcase (symbol-name symbol)) bbdb-cache))
2114 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
2116 (setq who (nth 1 (gnus-extract-address-components who)))
2118 (if spam-cache-lookups
2119 (intern-soft (downcase who) bbdb-cache)
2120 (bbdb-search-simple nil who))
2122 (if spam-use-BBDB-exclusive
2130 ;;; check the ifile backend; return nil if the mail was NOT classified
2133 (defun spam-get-ifile-database-parameter ()
2134 "Get the command-line parameter for ifile's database from
2135 spam-ifile-database-path."
2136 (if spam-ifile-database-path
2137 (format "--db-file=%s" spam-ifile-database-path)
2140 (defun spam-check-ifile ()
2141 "Check the ifile backend for the classification of this message."
2142 (let ((article-buffer-name (buffer-name))
2145 (let ((temp-buffer-name (buffer-name))
2146 (db-param (spam-get-ifile-database-parameter)))
2148 (set-buffer article-buffer-name)
2149 (apply 'call-process-region
2150 (point-min) (point-max) spam-ifile-path
2151 nil temp-buffer-name nil "-c"
2152 (if db-param `(,db-param "-q") `("-q"))))
2153 ;; check the return now (we're back in the temp buffer)
2154 (goto-char (point-min))
2156 (setq category (buffer-substring (point) (point-at-eol))))
2157 (when (not (zerop (length category))) ; we need a category here
2158 (if spam-ifile-all-categories
2159 (setq return category)
2160 ;; else, if spam-ifile-all-categories is not set...
2161 (when (string-equal spam-ifile-spam-category category)
2162 (setq return spam-split-group)))))) ; note return is nil otherwise
2165 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2166 "Register an article, given as a string, with a category.
2167 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2168 (let ((category (or category gnus-newsgroup-name))
2169 (add-or-delete-option (if unregister "-d" "-i"))
2170 (db (spam-get-ifile-database-parameter))
2173 (dolist (article articles)
2174 (let ((article-string (spam-get-article-as-string article)))
2175 (when (stringp article-string)
2176 (insert article-string))))
2177 (apply 'call-process-region
2178 (point-min) (point-max) spam-ifile-path
2180 add-or-delete-option category
2181 (if db `(,db "-h") `("-h"))))))
2183 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2184 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2186 (defun spam-ifile-unregister-spam-routine (articles)
2187 (spam-ifile-register-spam-routine articles t))
2189 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2190 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2192 (defun spam-ifile-unregister-ham-routine (articles)
2193 (spam-ifile-register-ham-routine articles t))
2200 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2201 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2202 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2203 (autoload 'spam-stat-buffer-is-spam "spam-stat")
2204 (autoload 'spam-stat-load "spam-stat")
2205 (autoload 'spam-stat-save "spam-stat")
2206 (autoload 'spam-stat-split-fancy "spam-stat"))
2209 (when (condition-case nil
2210 (let ((spam-stat-install-hooks nil))
2211 (require 'spam-stat))
2213 (defalias 'spam-stat-register-ham-routine 'ignore)
2214 (defalias 'spam-stat-register-spam-routine 'ignore)
2217 (defun spam-check-stat ()
2218 "Check the spam-stat backend for the classification of this message"
2219 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2220 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2222 (spam-stat-split-fancy)))
2224 (defun spam-stat-register-spam-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-non-spam)
2231 (spam-stat-buffer-is-spam))))))
2233 (defun spam-stat-unregister-spam-routine (articles)
2234 (spam-stat-register-spam-routine articles t))
2236 (defun spam-stat-register-ham-routine (articles &optional unregister)
2237 (dolist (article articles)
2238 (let ((article-string (spam-get-article-as-string article)))
2240 (insert article-string)
2242 (spam-stat-buffer-change-to-spam)
2243 (spam-stat-buffer-is-non-spam))))))
2245 (defun spam-stat-unregister-ham-routine (articles)
2246 (spam-stat-register-ham-routine articles t))
2248 (defun spam-maybe-spam-stat-load ()
2249 (when spam-use-stat (spam-stat-load)))
2251 (defun spam-maybe-spam-stat-save ()
2252 (when spam-use-stat (spam-stat-save)))))
2256 ;;{{{ Blacklists and whitelists.
2258 (defvar spam-whitelist-cache nil)
2259 (defvar spam-blacklist-cache nil)
2261 (defun spam-kill-whole-line ()
2263 (let ((kill-whole-line t))
2266 ;;; address can be a list, too
2267 (defun spam-enter-whitelist (address &optional remove)
2268 "Enter ADDRESS (list or single) into the whitelist.
2269 With a non-nil REMOVE, remove them."
2270 (interactive "sAddress: ")
2271 (spam-enter-list address spam-whitelist remove)
2272 (setq spam-whitelist-cache nil)
2273 (spam-clear-cache 'spam-use-whitelist))
2275 ;;; address can be a list, too
2276 (defun spam-enter-blacklist (address &optional remove)
2277 "Enter ADDRESS (list or single) into the blacklist.
2278 With a non-nil REMOVE, remove them."
2279 (interactive "sAddress: ")
2280 (spam-enter-list address spam-blacklist remove)
2281 (setq spam-blacklist-cache nil)
2282 (spam-clear-cache 'spam-use-whitelist))
2284 (defun spam-enter-list (addresses file &optional remove)
2285 "Enter ADDRESSES into the given FILE.
2286 Either the whitelist or the blacklist files can be used. With
2287 REMOVE not nil, remove the ADDRESSES."
2288 (if (stringp addresses)
2289 (spam-enter-list (list addresses) file remove)
2290 ;; else, we have a list of addresses here
2291 (unless (file-exists-p (file-name-directory file))
2292 (make-directory (file-name-directory file) t))
2295 (find-file-noselect file))
2296 (dolist (a addresses)
2298 (goto-char (point-min))
2299 (if (re-search-forward (regexp-quote a) nil t)
2300 ;; found the address
2302 (spam-kill-whole-line))
2303 ;; else, the address was not found
2305 (goto-char (point-max))
2311 (defun spam-filelist-build-cache (type)
2312 (let ((cache (if (eq type 'spam-use-blacklist)
2313 spam-blacklist-cache
2314 spam-whitelist-cache))
2316 (unless (gethash type spam-caches)
2318 (let ((address (pop cache)))
2319 (unless (zerop (length address)) ; 0 for a nil address too
2320 (setq address (regexp-quote address))
2321 ;; fix regexp-quote's treatment of user-intended regexes
2322 (while (string-match "\\\\\\*" address)
2323 (setq address (replace-match ".*" t t address))))
2324 (push address parsed-cache)))
2325 (puthash type parsed-cache spam-caches))))
2327 (defun spam-filelist-check-cache (type from)
2328 (when (stringp from)
2329 (spam-filelist-build-cache type)
2331 (dolist (address (gethash type spam-caches))
2332 (when (and address (string-match address from))
2337 ;;; returns t if the sender is in the whitelist, nil or
2338 ;;; spam-split-group otherwise
2339 (defun spam-check-whitelist ()
2340 ;; FIXME! Should it detect when file timestamps change?
2341 (unless spam-whitelist-cache
2342 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2343 (if (spam-from-listed-p 'spam-use-whitelist)
2345 (if spam-use-whitelist-exclusive
2349 (defun spam-check-blacklist ()
2350 ;; FIXME! Should it detect when file timestamps change?
2351 (unless spam-blacklist-cache
2352 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2353 (and (spam-from-listed-p 'spam-use-blacklist)
2356 (defun spam-parse-list (file)
2357 (when (file-readable-p file)
2358 (let (contents address)
2360 (insert-file-contents file)
2362 (setq address (buffer-substring (point) (point-at-eol)))
2364 ;; insert the e-mail address if detected, otherwise the raw data
2365 (unless (zerop (length address))
2366 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2367 (push (or pure-address address) contents)))))
2368 (nreverse contents))))
2370 (defun spam-from-listed-p (type)
2371 (let ((from (message-fetch-field "from"))
2373 (spam-filelist-check-cache type from)))
2375 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2376 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2377 (declassification (if blacklist 'ham 'spam))
2379 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2381 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2382 from addresses unregister-list article-unregister-list)
2383 (dolist (article articles)
2384 (let ((from (spam-fetch-field-from-fast article))
2385 (id (spam-fetch-field-message-id-fast article))
2387 (when (stringp from)
2388 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2389 (when (and (not sender-ignored)
2390 (stringp ignore-regex)
2391 (string-match ignore-regex from))
2392 (setq sender-ignored t)))
2393 ;; remember the messages we need to unregister, unless remove is set
2396 (spam-log-unregistration-needed-p
2397 id 'process declassification de-symbol))
2398 (push article article-unregister-list)
2399 (push from unregister-list))
2400 (unless sender-ignored
2401 (push from addresses)))))
2404 (funcall enter-function addresses t) ; unregister all these addresses
2405 ;; else, register normally and unregister what we need to
2406 (funcall remove-function unregister-list t)
2407 (dolist (article article-unregister-list)
2408 (spam-log-undo-registration
2409 (spam-fetch-field-message-id-fast article)
2413 (funcall enter-function addresses nil))))
2415 (defun spam-blacklist-unregister-routine (articles)
2416 (spam-blacklist-register-routine articles t))
2418 (defun spam-blacklist-register-routine (articles &optional unregister)
2419 (spam-filelist-register-routine articles t unregister))
2421 (defun spam-whitelist-unregister-routine (articles)
2422 (spam-whitelist-register-routine articles t))
2424 (defun spam-whitelist-register-routine (articles &optional unregister)
2425 (spam-filelist-register-routine articles nil unregister))
2429 ;;{{{ Spam-report glue (gmane and resend reporting)
2430 (defun spam-report-gmane-register-routine (articles)
2432 (apply 'spam-report-gmane articles)))
2434 (defun spam-report-resend-register-ham-routine (articles)
2435 (spam-report-resend-register-routine articles t))
2437 (defun spam-report-resend-register-routine (articles &optional ham)
2438 (let* ((resend-to-gp
2440 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2441 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2442 (spam-report-resend-to (or (car-safe resend-to-gp)
2443 spam-report-resend-to)))
2444 (spam-report-resend articles ham)))
2449 (defun spam-check-bogofilter-headers (&optional score)
2450 (let ((header (message-fetch-field spam-bogofilter-header)))
2451 (when header ; return nil when no header
2452 (if score ; scoring mode
2453 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2454 (match-string 1 header)
2456 ;; spam detection mode
2457 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2459 spam-split-group)))))
2461 ;; return something sensible if the score can't be determined
2462 (defun spam-bogofilter-score (&optional recheck)
2463 "Get the Bogofilter spamicity score"
2465 (save-window-excursion
2466 (gnus-summary-show-article t)
2467 (set-buffer gnus-article-buffer)
2468 (let ((score (or (unless recheck
2469 (spam-check-bogofilter-headers t))
2470 (spam-check-bogofilter t))))
2471 (gnus-summary-show-article)
2472 (message "Spamicity score %s" score)
2475 (defun spam-verify-bogofilter ()
2476 "Verify the Bogofilter version is sufficient."
2477 (when (eq spam-bogofilter-valid 'unknown)
2478 (setq spam-bogofilter-valid
2479 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2480 (shell-command-to-string
2481 (format "%s -V" spam-bogofilter-path))))))
2482 spam-bogofilter-valid)
2484 (defun spam-check-bogofilter (&optional score)
2485 "Check the Bogofilter backend for the classification of this message."
2486 (if (spam-verify-bogofilter)
2487 (let ((article-buffer-name (buffer-name))
2488 (db spam-bogofilter-database-directory)
2491 (let ((temp-buffer-name (buffer-name)))
2493 (set-buffer article-buffer-name)
2494 (apply 'call-process-region
2495 (point-min) (point-max)
2496 spam-bogofilter-path
2497 nil temp-buffer-name nil
2498 (if db `("-d" ,db "-v") `("-v"))))
2499 (setq return (spam-check-bogofilter-headers score))))
2501 (gnus-error "`spam.el' doesnt support obsolete bogofilter versions")))
2503 (defun spam-bogofilter-register-with-bogofilter (articles
2505 &optional unregister)
2506 "Register an article, given as a string, as spam or non-spam."
2507 (if (spam-verify-bogofilter)
2508 (dolist (article articles)
2509 (let ((article-string (spam-get-article-as-string article))
2510 (db spam-bogofilter-database-directory)
2511 (switch (if unregister
2513 spam-bogofilter-spam-strong-switch
2514 spam-bogofilter-ham-strong-switch)
2516 spam-bogofilter-spam-switch
2517 spam-bogofilter-ham-switch))))
2518 (when (stringp article-string)
2520 (insert article-string)
2522 (apply 'call-process-region
2523 (point-min) (point-max)
2524 spam-bogofilter-path
2526 (if db `("-d" ,db "-v") `("-v")))))))
2527 (gnus-error "`spam.el' doesnt support obsolete bogofilter versions")))
2529 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2530 (spam-bogofilter-register-with-bogofilter articles t unregister))
2532 (defun spam-bogofilter-unregister-spam-routine (articles)
2533 (spam-bogofilter-register-spam-routine articles t))
2535 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2536 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2538 (defun spam-bogofilter-unregister-ham-routine (articles)
2539 (spam-bogofilter-register-ham-routine articles t))
2545 (defun spam-check-spamoracle ()
2546 "Run spamoracle on an article to determine whether it's spam."
2547 (let ((article-buffer-name (buffer-name)))
2549 (let ((temp-buffer-name (buffer-name)))
2551 (set-buffer article-buffer-name)
2553 (apply 'call-process-region
2554 (point-min) (point-max)
2555 spam-spamoracle-binary
2556 nil temp-buffer-name nil
2557 (if spam-spamoracle-database
2558 `("-f" ,spam-spamoracle-database "mark")
2562 (set-buffer temp-buffer-name)
2563 (goto-char (point-min))
2564 (when (re-search-forward "^X-Spam: yes;" nil t)
2566 (error "Error running spamoracle: %s" status))))))))
2568 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2569 "Run spamoracle in training mode."
2571 (let ((temp-buffer-name (buffer-name)))
2573 (goto-char (point-min))
2574 (dolist (article articles)
2575 (insert (spam-get-article-as-string article)))
2576 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2580 (apply 'call-process-region
2581 (point-min) (point-max)
2582 spam-spamoracle-binary
2583 nil temp-buffer-name nil
2584 (if spam-spamoracle-database
2585 `("-f" ,spam-spamoracle-database
2588 (unless (eq 0 status)
2589 (error "Error running spamoracle: %s" status)))))))
2591 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2592 (spam-spamoracle-learn articles nil unregister))
2594 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2595 (spam-spamoracle-learn-ham articles t))
2597 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2598 (spam-spamoracle-learn articles t unregister))
2600 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2601 (spam-spamoracle-learn-spam articles t))
2606 ;;; based mostly on the bogofilter code
2607 (defun spam-check-spamassassin-headers (&optional score)
2608 "Check the SpamAssassin headers for the classification of this message."
2609 (if score ; scoring mode
2610 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2612 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2613 (match-string 1 header)
2615 ;; spam detection mode
2616 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2617 (when header ; return nil when no header
2618 (when (string-match spam-spamassassin-positive-spam-flag-header
2620 spam-split-group)))))
2622 (defun spam-check-spamassassin (&optional score)
2623 "Check the SpamAssassin backend for the classification of this message."
2624 (let ((article-buffer-name (buffer-name)))
2626 (let ((temp-buffer-name (buffer-name)))
2628 (set-buffer article-buffer-name)
2629 (apply 'call-process-region
2630 (point-min) (point-max) spam-spamassassin-path
2631 nil temp-buffer-name nil spam-spamassassin-arguments))
2632 ;; check the return now (we're back in the temp buffer)
2633 (goto-char (point-min))
2634 (spam-check-spamassassin-headers score)))))
2636 ;; return something sensible if the score can't be determined
2637 (defun spam-spamassassin-score (&optional recheck)
2638 "Get the SpamAssassin score"
2640 (save-window-excursion
2641 (gnus-summary-show-article t)
2642 (set-buffer gnus-article-buffer)
2643 (let ((score (or (unless recheck
2644 (spam-check-spamassassin-headers t))
2645 (spam-check-spamassassin t))))
2646 (gnus-summary-show-article)
2647 (message "SpamAssassin score %s" score)
2650 (defun spam-spamassassin-register-with-sa-learn (articles spam
2651 &optional unregister)
2652 "Register articles with spamassassin's sa-learn as spam or non-spam."
2654 (let ((action (if unregister spam-sa-learn-unregister-switch
2655 (if spam spam-sa-learn-spam-switch
2656 spam-sa-learn-ham-switch)))
2657 (summary-buffer-name (buffer-name)))
2659 ;; group the articles into mbox format
2660 (dolist (article articles)
2661 (let (article-string)
2663 (set-buffer summary-buffer-name)
2664 (setq article-string (spam-get-article-as-string article)))
2665 (when (stringp article-string)
2666 (insert "From \n") ; mbox separator (sa-learn only checks the
2667 ; first five chars, so we can get away with
2669 (insert article-string)
2671 ;; call sa-learn on all messages at the same time
2672 (apply 'call-process-region
2673 (point-min) (point-max)
2675 nil nil nil "--mbox"
2676 (if spam-sa-learn-rebuild
2678 `("--no-rebuild" ,action)))))))
2680 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2681 (spam-spamassassin-register-with-sa-learn articles t unregister))
2683 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2684 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2686 (defun spam-spamassassin-unregister-spam-routine (articles)
2687 (spam-spamassassin-register-with-sa-learn articles t t))
2689 (defun spam-spamassassin-unregister-ham-routine (articles)
2690 (spam-spamassassin-register-with-sa-learn articles nil t))
2695 ;;; based mostly on the bogofilter code
2696 (defun spam-check-bsfilter-headers (&optional score)
2698 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2700 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2701 (when header ; return nil when no header
2702 (when (string-match "YES" header)
2703 spam-split-group)))))
2705 ;; return something sensible if the score can't be determined
2706 (defun spam-bsfilter-score (&optional recheck)
2707 "Get the Bsfilter spamicity score"
2709 (save-window-excursion
2710 (gnus-summary-show-article t)
2711 (set-buffer gnus-article-buffer)
2712 (let ((score (or (unless recheck
2713 (spam-check-bsfilter-headers t))
2714 (spam-check-bsfilter t))))
2715 (gnus-summary-show-article)
2716 (message "Spamicity score %s" score)
2719 (defun spam-check-bsfilter (&optional score)
2720 "Check the Bsfilter backend for the classification of this message"
2721 (let ((article-buffer-name (buffer-name))
2722 (dir spam-bsfilter-database-directory)
2725 (let ((temp-buffer-name (buffer-name)))
2727 (set-buffer article-buffer-name)
2728 (apply 'call-process-region
2729 (point-min) (point-max)
2731 nil temp-buffer-name nil
2734 "--insert-probability"
2736 (list "--homedir" dir))))
2737 (setq return (spam-check-bsfilter-headers score))))
2740 (defun spam-bsfilter-register-with-bsfilter (articles
2742 &optional unregister)
2743 "Register an article, given as a string, as spam or non-spam."
2744 (dolist (article articles)
2745 (let ((article-string (spam-get-article-as-string article))
2746 (switch (if unregister
2748 spam-bsfilter-spam-strong-switch
2749 spam-bsfilter-ham-strong-switch)
2751 spam-bsfilter-spam-switch
2752 spam-bsfilter-ham-switch))))
2753 (when (stringp article-string)
2755 (insert article-string)
2756 (apply 'call-process-region
2757 (point-min) (point-max)
2761 (when spam-bsfilter-database-directory
2763 spam-bsfilter-database-directory))))))))
2765 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2766 (spam-bsfilter-register-with-bsfilter articles t unregister))
2768 (defun spam-bsfilter-unregister-spam-routine (articles)
2769 (spam-bsfilter-register-spam-routine articles t))
2771 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2772 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2774 (defun spam-bsfilter-unregister-ham-routine (articles)
2775 (spam-bsfilter-register-ham-routine articles t))
2779 ;;{{{ CRM114 Mailfilter
2780 (defun spam-check-crm114-headers (&optional score)
2781 (let ((header (message-fetch-field spam-crm114-header)))
2782 (when header ; return nil when no header
2783 (if score ; scoring mode
2784 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2785 (match-string 1 header)
2787 ;; spam detection mode
2788 (when (string-match spam-crm114-positive-spam-header
2790 spam-split-group)))))
2792 ;; return something sensible if the score can't be determined
2793 (defun spam-crm114-score ()
2794 "Get the CRM114 Mailfilter pR"
2796 (save-window-excursion
2797 (gnus-summary-show-article t)
2798 (set-buffer gnus-article-buffer)
2799 (let ((score (or (spam-check-crm114-headers t)
2800 (spam-check-crm114 t))))
2801 (gnus-summary-show-article)
2802 (message "pR: %s" score)
2805 (defun spam-check-crm114 (&optional score)
2806 "Check the CRM114 Mailfilter backend for the classification of this message"
2807 (let ((article-buffer-name (buffer-name))
2808 (db spam-crm114-database-directory)
2811 (let ((temp-buffer-name (buffer-name)))
2813 (set-buffer article-buffer-name)
2814 (apply 'call-process-region
2815 (point-min) (point-max)
2817 nil temp-buffer-name nil
2818 (when db (list (concat "--fileprefix=" db)))))
2819 (setq return (spam-check-crm114-headers score))))
2822 (defun spam-crm114-register-with-crm114 (articles
2824 &optional unregister)
2825 "Register an article, given as a string, as spam or non-spam."
2826 (dolist (article articles)
2827 (let ((article-string (spam-get-article-as-string article))
2828 (db spam-crm114-database-directory)
2829 (switch (if unregister
2831 spam-crm114-spam-strong-switch
2832 spam-crm114-ham-strong-switch)
2834 spam-crm114-spam-switch
2835 spam-crm114-ham-switch))))
2836 (when (stringp article-string)
2838 (insert article-string)
2840 (apply 'call-process-region
2841 (point-min) (point-max)
2844 (when db (list switch (concat "--fileprefix=" db)))))))))
2846 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2847 (spam-crm114-register-with-crm114 articles t unregister))
2849 (defun spam-crm114-unregister-spam-routine (articles)
2850 (spam-crm114-register-spam-routine articles t))
2852 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2853 (spam-crm114-register-with-crm114 articles nil unregister))
2855 (defun spam-crm114-unregister-ham-routine (articles)
2856 (spam-crm114-register-ham-routine articles t))
2865 (defun spam-initialize (&rest symbols)
2866 "Install the spam.el hooks and do other initialization.
2867 When SYMBOLS is given, set those variables to t. This is so you
2868 can call spam-initialize before you set spam-use-* variables on
2869 explicitly, and matters only if you need the extra headers
2870 installed through spam-necessary-extra-headers."
2873 (dolist (var symbols)
2876 (dolist (header (spam-necessary-extra-headers))
2877 (add-to-list 'nnmail-extra-headers header)
2878 (add-to-list 'gnus-extra-headers header))
2880 (setq spam-install-hooks t)
2881 ;; TODO: How do we redo this every time spam-face is customized?
2882 (push '((eq mark gnus-spam-mark) . spam-face)
2883 gnus-summary-highlight)
2884 ;; Add hooks for loading and saving the spam stats
2885 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2886 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2887 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2888 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2889 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2890 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2891 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2893 (defun spam-unload-hook ()
2894 "Uninstall the spam.el hooks"
2896 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2897 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2898 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2899 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2900 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2901 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2902 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2904 (add-hook 'spam-unload-hook 'spam-unload-hook)
2906 (when spam-install-hooks
2912 ;;; spam.el ends here