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: spam scores, detection of spam in newsgroups, cross-server splitting,
36 ;; remote processing, training through files
40 (eval-when-compile (require 'cl))
41 (eval-when-compile (require 'spam-report))
45 (require 'gnus-uu) ; because of key prefix issues
46 ;;; for the definitions of group content classification and spam processors
48 (require 'message) ;for the message-fetch-field functions
50 ;; for nnimap-split-download-body-default
51 (eval-when-compile (require 'nnimap))
55 (autoload 'query-dig "dig"))
57 ;; autoload spam-report
59 (autoload 'spam-report-gmane "spam-report")
60 (autoload 'spam-report-resend "spam-report"))
62 ;; autoload gnus-registry
64 (autoload 'gnus-registry-group-count "gnus-registry")
65 (autoload 'gnus-registry-add-group "gnus-registry")
66 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
67 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
71 (autoload 'query-dns "dns"))
76 "Spam configuration.")
78 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
79 "Directory for spam whitelists and blacklists."
83 (defcustom spam-move-spam-nonspam-groups-only t
84 "Whether spam should be moved in non-spam groups only.
85 When t, only ham and unclassified groups will have their spam moved
86 to the spam-process-destination. When nil, spam will also be moved from
91 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
92 "Whether new messages in a spam group should get the spam-mark."
96 (defcustom spam-process-ham-in-nonham-groups nil
97 "Whether ham should be processed in non-ham groups."
101 (defcustom spam-log-to-registry nil
102 "Whether spam/ham processing should be logged in the registry."
106 (defcustom spam-split-symbolic-return nil
107 "Whether `spam-split' should work with symbols or group names."
111 (defcustom spam-split-symbolic-return-positive nil
112 "Whether `spam-split' should ALWAYS work with symbols or group names.
113 Do not set this if you use `spam-split' in a fancy split
118 (defcustom spam-process-ham-in-spam-groups nil
119 "Whether ham should be processed in spam groups."
123 (defcustom spam-mark-only-unseen-as-spam t
124 "Whether only unseen articles should be marked as spam in spam groups.
125 When nil, all unread articles in a spam group are marked as
126 spam. Set this if you want to leave an article unread in a spam group
127 without losing it to the automatic spam-marking process."
131 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
132 "Whether ham should be marked unread before it's moved.
133 The article is moved out of a spam group according to ham-process-destination.
134 This variable is an official entry in the international Longest Variable Name
139 (defcustom spam-disable-spam-split-during-ham-respool nil
140 "Whether `spam-split' should be ignored while resplitting ham.
141 This is useful to prevent ham from ending up in the same spam
142 group after the resplit. Don't set this to t if you have `spam-split' as the
143 last rule in your split configuration."
147 (defcustom spam-autodetect-recheck-messages nil
148 "Should spam.el recheck all meessages when autodetecting?
149 Normally this is nil, so only unseen messages will be checked."
153 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
154 "The location of the whitelist.
155 The file format is one regular expression per line.
156 The regular expression is matched against the address."
160 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
161 "The location of the blacklist.
162 The file format is one regular expression per line.
163 The regular expression is matched against the address."
167 (defcustom spam-use-dig t
168 "Whether `query-dig' should be used instead of `query-dns'."
172 (defcustom spam-use-gmane-xref nil
173 "Whether the Gmane spam xref should be used by `spam-split'."
177 (defcustom spam-use-blacklist nil
178 "Whether the blacklist should be used by `spam-split'."
182 (defcustom spam-blacklist-ignored-regexes nil
183 "Regular expressions that the blacklist should ignore."
184 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
187 (defcustom spam-use-whitelist nil
188 "Whether the whitelist should be used by `spam-split'."
192 (defcustom spam-use-whitelist-exclusive nil
193 "Whether whitelist-exclusive should be used by `spam-split'.
194 Exclusive whitelisting means that all messages from senders not in the whitelist
195 are considered spam."
199 (defcustom spam-use-blackholes nil
200 "Whether blackholes should be used by `spam-split'."
204 (defcustom spam-use-hashcash nil
205 "Whether hashcash payments should be detected by `spam-split'."
209 (defcustom spam-use-regex-headers nil
210 "Whether a header regular expression match should be used by `spam-split'.
211 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
215 (defcustom spam-use-regex-body nil
216 "Whether a body regular expression match should be used by `spam-split'.
217 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
221 (defcustom spam-use-bogofilter-headers nil
222 "Whether bogofilter headers should be used by `spam-split'.
223 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
227 (defcustom spam-use-bogofilter nil
228 "Whether bogofilter should be invoked by `spam-split'.
229 Enable this if you want Gnus to invoke Bogofilter on new messages."
233 (defcustom spam-use-bsfilter-headers nil
234 "Whether bsfilter headers should be used by `spam-split'.
235 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
239 (defcustom spam-use-bsfilter nil
240 "Whether bsfilter should be invoked by `spam-split'.
241 Enable this if you want Gnus to invoke Bsfilter on new messages."
245 (defcustom spam-use-BBDB nil
246 "Whether BBDB should be used by `spam-split'."
250 (defcustom spam-use-BBDB-exclusive nil
251 "Whether BBDB-exclusive should be used by `spam-split'.
252 Exclusive BBDB means that all messages from senders not in the BBDB are
257 (defcustom spam-use-ifile nil
258 "Whether ifile should be used by `spam-split'."
262 (defcustom spam-use-stat nil
263 "Whether `spam-stat' should be used by `spam-split'."
267 (defcustom spam-use-spamoracle nil
268 "Whether spamoracle should be used by `spam-split'."
272 (defcustom spam-use-spamassassin nil
273 "Whether spamassassin should be invoked by `spam-split'.
274 Enable this if you want Gnus to invoke SpamAssassin on new messages."
278 (defcustom spam-use-spamassassin-headers nil
279 "Whether spamassassin headers should be checked by `spam-split'.
280 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
285 (defcustom spam-use-crm114 nil
286 "Whether the CRM114 Mailfilter should be used by `spam-split'."
290 (defcustom spam-install-hooks (or
295 spam-use-whitelist-exclusive
298 spam-use-regex-headers
301 spam-use-bogofilter-headers
302 spam-use-spamassassin
303 spam-use-spamassassin-headers
305 spam-use-bsfilter-headers
307 spam-use-BBDB-exclusive
312 "Whether the spam hooks should be installed.
313 Default to t if one of the spam-use-* variables is set."
317 (defcustom spam-split-group "spam"
318 "Group name where incoming spam should be put by `spam-split'."
322 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
323 ;;; not regular expressions
324 (defcustom spam-junk-mailgroups (cons
326 '("mail.junk" "poste.pourriel"))
327 "Mailgroups with spam contents.
328 All unmarked article in such group receive the spam mark on group entry."
329 :type '(repeat (string :tag "Group"))
333 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
334 "The group where spam xrefs can be found on Gmane.
335 Only meaningful if you enable `spam-use-gmane-xref'."
339 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
340 "dev.null.dk" "relays.visi.com")
341 "List of blackhole servers.
342 Only meaningful if you enable `spam-use-blackholes'."
343 :type '(repeat (string :tag "Server"))
346 (defcustom spam-blackhole-good-server-regex nil
347 "String matching IP addresses that should not be checked in the blackholes.
348 Only meaningful if you enable `spam-use-blackholes'."
349 :type '(radio (const nil)
350 (regexp :format "%t: %v\n" :size 0))
353 (defcustom spam-face 'gnus-splash-face
354 "Face for spam-marked articles."
358 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
359 "Regular expression for positive header spam matches.
360 Only meaningful if you enable `spam-use-regex-headers'."
361 :type '(repeat (regexp :tag "Regular expression to match spam header"))
364 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
365 "Regular expression for positive header ham matches.
366 Only meaningful if you enable `spam-use-regex-headers'."
367 :type '(repeat (regexp :tag "Regular expression to match ham header"))
370 (defcustom spam-regex-body-spam '()
371 "Regular expression for positive body spam matches.
372 Only meaningful if you enable `spam-use-regex-body'."
373 :type '(repeat (regexp :tag "Regular expression to match spam body"))
376 (defcustom spam-regex-body-ham '()
377 "Regular expression for positive body ham matches.
378 Only meaningful if you enable `spam-use-regex-body'."
379 :type '(repeat (regexp :tag "Regular expression to match ham body"))
382 (defgroup spam-ifile nil
383 "Spam ifile configuration."
386 (defcustom spam-ifile-path (exec-installed-p "ifile")
387 "File path of the ifile executable program."
388 :type '(choice (file :tag "Location of ifile")
389 (const :tag "ifile is not installed"))
392 (defcustom spam-ifile-database-path nil
393 "File path of the ifile database."
394 :type '(choice (file :tag "Location of the ifile database")
395 (const :tag "Use the default"))
398 (defcustom spam-ifile-spam-category "spam"
399 "Name of the spam ifile category."
403 (defcustom spam-ifile-ham-category nil
404 "Name of the ham ifile category.
405 If nil, the current group name will be used."
406 :type '(choice (string :tag "Use a fixed category")
407 (const :tag "Use the current group name"))
410 (defcustom spam-ifile-all-categories nil
411 "Whether the ifile check will return all categories, or just spam.
412 Set this to t if you want to use the `spam-split' invocation of ifile as
413 your main source of newsgroup names."
417 (defgroup spam-bogofilter nil
418 "Spam bogofilter configuration."
421 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
422 "File path of the Bogofilter executable program."
423 :type '(choice (file :tag "Location of bogofilter")
424 (const :tag "Bogofilter is not installed"))
425 :group 'spam-bogofilter)
427 (defcustom spam-bogofilter-header "X-Bogosity"
428 "The header that Bogofilter inserts in messages."
430 :group 'spam-bogofilter)
432 (defcustom spam-bogofilter-spam-switch "-s"
433 "The switch that Bogofilter uses to register spam messages."
435 :group 'spam-bogofilter)
437 (defcustom spam-bogofilter-ham-switch "-n"
438 "The switch that Bogofilter uses to register ham messages."
440 :group 'spam-bogofilter)
442 (defcustom spam-bogofilter-spam-strong-switch "-S"
443 "The switch that Bogofilter uses to unregister ham messages."
445 :group 'spam-bogofilter)
447 (defcustom spam-bogofilter-ham-strong-switch "-N"
448 "The switch that Bogofilter uses to unregister spam messages."
450 :group 'spam-bogofilter)
452 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
453 "The regex on `spam-bogofilter-header' for positive spam identification."
455 :group 'spam-bogofilter)
457 (defcustom spam-bogofilter-database-directory nil
458 "Directory path of the Bogofilter databases."
459 :type '(choice (directory
460 :tag "Location of the Bogofilter database directory")
461 (const :tag "Use the default"))
462 :group 'spam-bogofilter)
464 (defgroup spam-bsfilter nil
465 "Spam bsfilter configuration."
468 (defcustom spam-bsfilter-path (executable-find "bsfilter")
469 "File path of the Bsfilter executable program."
470 :type '(choice (file :tag "Location of bsfilter")
471 (const :tag "Bsfilter is not installed"))
472 :group 'spam-bsfilter)
474 (defcustom spam-bsfilter-header "X-Spam-Flag"
475 "The header inserted by Bsfilter to flag spam."
477 :group 'spam-bsfilter)
479 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
480 "The header that Bsfilter inserts in messages."
482 :group 'spam-bsfilter)
484 (defcustom spam-bsfilter-spam-switch "--add-spam"
485 "The switch that Bsfilter uses to register spam messages."
487 :group 'spam-bsfilter)
489 (defcustom spam-bsfilter-ham-switch "--add-ham"
490 "The switch that Bsfilter uses to register ham messages."
492 :group 'spam-bsfilter)
494 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
495 "The switch that Bsfilter uses to unregister ham messages."
497 :group 'spam-bsfilter)
499 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
500 "The switch that Bsfilter uses to unregister spam messages."
502 :group 'spam-bsfilter)
504 (defcustom spam-bsfilter-database-directory nil
505 "Directory path of the Bsfilter databases."
506 :type '(choice (directory
507 :tag "Location of the Bsfilter database directory")
508 (const :tag "Use the default"))
509 :group 'spam-bsfilter)
511 (defgroup spam-spamoracle nil
512 "Spam spamoracle configuration."
515 (defcustom spam-spamoracle-database nil
516 "Location of spamoracle database file.
517 When nil, use the default spamoracle database."
518 :type '(choice (directory :tag "Location of spamoracle database file.")
519 (const :tag "Use the default"))
520 :group 'spam-spamoracle)
522 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
523 "Location of the spamoracle binary."
524 :type '(choice (directory :tag "Location of the spamoracle binary")
525 (const :tag "Use the default"))
526 :group 'spam-spamoracle)
528 (defgroup spam-spamassassin nil
529 "Spam SpamAssassin configuration."
532 (defcustom spam-spamassassin-path (executable-find "spamassassin")
533 "File path of the spamassassin executable program.
534 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
535 spamd man pages for more information on these programs."
536 :type '(choice (file :tag "Location of spamc")
537 (const :tag "spamassassin is not installed"))
538 :group 'spam-spamassassin)
540 (defcustom spam-spamassassin-arguments ()
541 "Arguments to pass to the spamassassin executable.
542 This must be a list. For example, `(\"-C\" \"configfile\")'."
543 :type '(restricted-sexp :match-alternatives (listp))
544 :group 'spam-spamassassin)
546 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
547 "The header inserted by SpamAssassin to flag spam."
549 :group 'spam-spamassassin)
551 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
552 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
555 :group 'spam-spamassassin)
557 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
558 "The header inserted by SpamAssassin, giving extended scoring information"
560 :group 'spam-spamassassin)
562 (defcustom spam-sa-learn-path (executable-find "sa-learn")
563 "File path of the sa-learn executable program."
564 :type '(choice (file :tag "Location of spamassassin")
565 (const :tag "spamassassin is not installed"))
566 :group 'spam-spamassassin)
568 (defcustom spam-sa-learn-rebuild t
569 "Whether sa-learn should rebuild the database every time it is called
570 Enable this if you want sa-learn to rebuild the database automatically. Doing
571 this will slightly increase the running time of the spam registration process.
572 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
573 order for SpamAssassin to recognize the new registered spam."
575 :group 'spam-spamassassin)
577 (defcustom spam-sa-learn-spam-switch "--spam"
578 "The switch that sa-learn uses to register spam messages"
580 :group 'spam-spamassassin)
582 (defcustom spam-sa-learn-ham-switch "--ham"
583 "The switch that sa-learn uses to register ham messages"
585 :group 'spam-spamassassin)
587 (defcustom spam-sa-learn-unregister-switch "--forget"
588 "The switch that sa-learn uses to unregister messages messages"
590 :group 'spam-spamassassin)
592 (defgroup spam-crm114 nil
593 "Spam CRM114 Mailfilter configuration."
596 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
597 "File path of the CRM114 Mailfilter executable program."
598 :type '(choice (file :tag "Location of CRM114 Mailfilter")
599 (const :tag "CRM114 Mailfilter is not installed"))
602 (defcustom spam-crm114-header "X-CRM114-Status"
603 "The header that CRM114 Mailfilter inserts in messages."
607 (defcustom spam-crm114-spam-switch "--learnspam"
608 "The switch that CRM114 Mailfilter uses to register spam messages."
612 (defcustom spam-crm114-ham-switch "--learnnonspam"
613 "The switch that CRM114 Mailfilter uses to register ham messages."
617 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
618 "The switch that CRM114 Mailfilter uses to unregister ham messages."
622 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
623 "The switch that CRM114 Mailfilter uses to unregister spam messages."
627 (defcustom spam-crm114-positive-spam-header "^SPAM"
628 "The regex on `spam-crm114-header' for positive spam identification."
632 (defcustom spam-crm114-database-directory nil
633 "Directory path of the CRM114 Mailfilter databases."
634 :type '(choice (directory
635 :tag "Location of the CRM114 Mailfilter database directory")
636 (const :tag "Use the default"))
639 ;;; Key bindings for spam control.
641 (gnus-define-keys gnus-summary-mode-map
642 "St" spam-generic-score
643 "Sx" gnus-summary-mark-as-spam
644 "Mst" spam-generic-score
645 "Msx" gnus-summary-mark-as-spam
646 "\M-d" gnus-summary-mark-as-spam)
648 (defvar spam-cache-lookups t
649 "Whether spam.el will try to cache lookups using `spam-caches'.")
651 (defvar spam-caches (make-hash-table
654 "Cache of spam detection entries.")
656 (defvar spam-old-ham-articles nil
657 "List of old ham articles, generated when a group is entered.")
659 (defvar spam-old-spam-articles nil
660 "List of old spam articles, generated when a group is entered.")
662 (defvar spam-split-disabled nil
663 "If non-nil, `spam-split' is disabled, and always returns nil.")
665 (defvar spam-split-last-successful-check nil
667 `spam-split' will set this to nil or a spam-use-XYZ check if it
670 ;; convenience functions
671 (defun spam-clear-cache (symbol)
672 "Clear the spam-caches entry for a check."
673 (remhash symbol spam-caches))
675 (defun spam-xor (a b)
677 (and (or a b) (not (and a b))))
679 (defun spam-group-ham-mark-p (group mark &optional spam)
680 "Checks if MARK is considered a ham mark in GROUP."
681 (when (stringp group)
682 (let* ((marks (spam-group-ham-marks group spam))
683 (marks (if (symbolp mark)
685 (mapcar 'symbol-value marks))))
688 (defun spam-group-spam-mark-p (group mark)
689 "Checks if MARK is considered a spam mark in GROUP."
690 (spam-group-ham-mark-p group mark t))
692 (defun spam-group-ham-marks (group &optional spam)
693 "In GROUP, get all the ham marks."
694 (when (stringp group)
695 (let* ((marks (if spam
696 (gnus-parameter-spam-marks group)
697 (gnus-parameter-ham-marks group)))
699 (marks (if (listp (car marks)) (car marks) marks)))
702 (defun spam-group-spam-marks (group)
703 "In GROUP, get all the spam marks."
704 (spam-group-ham-marks group t))
706 (defun spam-group-spam-contents-p (group)
707 "Is GROUP a spam group?"
708 (if (and (stringp group) (< 0 (length group)))
709 (or (member group spam-junk-mailgroups)
710 (memq 'gnus-group-spam-classification-spam
711 (gnus-parameter-spam-contents group)))
714 (defun spam-group-ham-contents-p (group)
715 "Is GROUP a ham group?"
717 (memq 'gnus-group-spam-classification-ham
718 (gnus-parameter-spam-contents group))
721 (defvar spam-list-of-processors
722 ;; note the nil processors are not defined in gnus.el
723 '((nil spam spam-use-gmane)
724 (nil spam spam-use-resend)
725 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
726 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
727 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
728 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
729 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
730 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
731 (nil spam spam-use-crm114)
732 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
734 (nil ham spam-use-resend)
735 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
736 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
737 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
738 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
739 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
740 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
741 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
742 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
743 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle)
744 (nil ham spam-use-crm114))
745 "The `spam-list-of-processors' list.
746 This list contains pairs associating the obsolete ham/spam exit
747 processor variables with a classification and a spam-use-*
748 variable. When the processor variable is nil, just the
749 classification and spam-use-* check variable are used.")
751 (defun spam-group-processor-p (group check &optional classification)
752 "Checks if GROUP has a CHECK with CLASSIFICATION registered.
753 Also accepts the obsolete processors, which can be found in
754 gnus.el and in spam-list-of-processors."
755 (if (and (stringp group)
757 (let ((old-style (assq check spam-list-of-processors))
758 (parameters (nth 0 (gnus-parameter-spam-process group)))
760 (if old-style ; old-style processor
761 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
762 ;; now search for the parameter
763 (dolist (parameter parameters)
764 (when (and (null found)
766 (eq classification (nth 0 parameter))
767 (eq check (nth 1 parameter)))
772 (defun spam-report-articles-gmane (n)
773 "Report the current message as spam via Gmane.
774 Respects the process/prefix convention."
776 (dolist (article (gnus-summary-work-articles n))
777 (gnus-summary-remove-process-mark article)
778 (spam-report-gmane article)))
780 (defun spam-report-articles-resend (n &optional ham)
781 "Report the current message as spam by resending it.
782 Respects the process/prefix convention. Also see
783 `spam-report-resend-to'. Operates as ham when HAM is set."
787 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
788 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
789 (spam-report-resend-to (or (car-safe gp)
790 spam-report-resend-to))
791 (articles (gnus-summary-work-articles n)))
792 (spam-report-resend articles ham)
793 (dolist (article articles)
794 (gnus-summary-remove-process-mark article))))
796 (defun spam-necessary-extra-headers ()
797 "Return the extra headers spam.el thinks are necessary."
799 (when (or spam-use-spamassassin
800 spam-use-spamassassin-headers
801 spam-use-regex-headers)
802 (push 'X-Spam-Status list))
805 (defun spam-user-format-function-S (headers)
807 (spam-summary-score headers)))
809 (defun spam-article-sort-by-spam-status (h1 h2)
810 "Sort articles by score."
812 (dolist (header (spam-necessary-extra-headers))
813 (let ((s1 (spam-summary-score h1 header))
814 (s2 (spam-summary-score h2 header)))
816 (setq result (< s1 s2))
820 (defun spam-extra-header-to-number (header headers)
821 "Transform an extra header to a number."
822 (if (gnus-extra-header header headers)
824 ((eq header 'X-Spam-Status)
825 (string-to-number (gnus-replace-in-string
826 (gnus-extra-header header headers)
828 ;; for CRM checking, it's probably faster to just do the string match
829 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
830 (match-string 1 header))
834 (defun spam-summary-score (headers &optional specific-header)
835 "Score an article for the summary buffer, as fast as possible.
836 With SPECIFIC-HEADER, returns only that header's score.
837 Will not return a nil score."
841 (list specific-header)
842 (spam-necessary-extra-headers)))
844 (spam-extra-header-to-number header headers))
849 (defun spam-generic-score (&optional recheck)
850 "Invoke whatever scoring method we can."
853 ((or spam-use-spamassassin spam-use-spamassassin-headers)
854 (spam-spamassassin-score recheck))
855 ((or spam-use-bsfilter spam-use-bsfilter-headers)
856 (spam-bsfilter-score recheck))
859 (t (spam-bogofilter-score recheck))))
861 ;;; Summary entry and exit processing.
863 (defun spam-summary-prepare ()
864 (setq spam-old-ham-articles
865 (spam-list-articles gnus-newsgroup-articles 'ham))
866 (setq spam-old-spam-articles
867 (spam-list-articles gnus-newsgroup-articles 'spam))
868 (spam-mark-junk-as-spam-routine))
870 ;; The spam processors are invoked for any group, spam or ham or neither
871 (defun spam-summary-prepare-exit ()
872 (unless gnus-group-is-exiting-without-update-p
873 (gnus-message 6 "Exiting summary buffer and applying spam rules")
875 ;; first of all, unregister any articles that are no longer ham or spam
876 ;; we have to iterate over the processors, or else we'll be too slow
877 (dolist (classification '(spam ham))
878 (let* ((old-articles (if (eq classification 'spam)
879 spam-old-spam-articles
880 spam-old-ham-articles))
881 (new-articles (spam-list-articles
882 gnus-newsgroup-articles
884 (changed-articles (spam-set-difference new-articles old-articles)))
885 ;; now that we have the changed articles, we go through the processors
886 (dolist (processor-param spam-list-of-processors)
887 (let ((processor (nth 0 processor-param))
888 (processor-classification (nth 1 processor-param))
889 (check (nth 2 processor-param))
891 (dolist (article changed-articles)
892 (let ((id (spam-fetch-field-message-id-fast article)))
893 (when (spam-log-unregistration-needed-p
894 id 'process classification check)
895 (push article unregister-list))))
896 ;; call spam-register-routine with specific articles to unregister,
897 ;; when there are articles to unregister and the check is enabled
898 (when (and unregister-list (symbol-value check))
899 (spam-register-routine
900 classification check t unregister-list))))))
902 ;; find all the spam processors applicable to this group
903 (dolist (processor-param spam-list-of-processors)
904 (let ((processor (nth 0 processor-param))
905 (classification (nth 1 processor-param))
906 (check (nth 2 processor-param)))
907 (when (and (eq 'spam classification)
908 (spam-group-processor-p gnus-newsgroup-name check classification))
909 (spam-register-routine classification check))))
911 (unless (and spam-move-spam-nonspam-groups-only
912 (spam-group-spam-contents-p gnus-newsgroup-name))
913 (let* ((group (gnus-parameter-spam-process-destination
914 gnus-newsgroup-name))
915 (num (spam-mark-spam-as-expired-and-move-routine group)))
918 "%d spam messages are marked as expired%s."
921 (format " and moved it to %s" group)
924 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
925 ;; expire spam, in case the above did not expire them
926 (let ((num (spam-mark-spam-as-expired-and-move-routine nil)))
929 "%d spam messages were marked as expired."
932 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
933 (and (spam-group-spam-contents-p gnus-newsgroup-name)
934 spam-process-ham-in-spam-groups)
935 spam-process-ham-in-nonham-groups)
936 ;; find all the ham processors applicable to this group
937 (dolist (processor-param spam-list-of-processors)
938 (let ((processor (nth 0 processor-param))
939 (classification (nth 1 processor-param))
940 (check (nth 2 processor-param)))
941 (when (and (eq 'ham classification)
942 (spam-group-processor-p gnus-newsgroup-name check classification))
943 (spam-register-routine classification check)))))
945 (when (spam-group-processor-p gnus-newsgroup-name 'ham 'spam-use-ham-copy)
947 (spam-ham-copy-routine
948 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
950 (gnus-message 6 "%d ham messages were copied" num))))
952 ;; now move all ham articles out of spam groups
953 (when (spam-group-spam-contents-p gnus-newsgroup-name)
955 (spam-ham-move-routine
956 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
958 (gnus-message 6 "%d ham messages were moved from spam group" num)))))
960 (setq spam-old-ham-articles nil)
961 (setq spam-old-spam-articles nil))
963 (defun spam-set-difference (list1 list2)
964 "Return a set difference of LIST1 and LIST2.
965 When either list is nil, the other is returned."
966 (if (and list1 list2)
967 ;; we have two non-nil lists
969 (dolist (item (append list1 list2))
970 (when (and (memq item list1) (memq item list2))
971 (setq list1 (delq item list1))
972 (setq list2 (delq item list2))))
973 (append list1 list2))
974 ;; if either of the lists was nil, return the other one
975 (if list1 list1 list2)))
977 (defun spam-mark-junk-as-spam-routine ()
978 ;; check the global list of group names spam-junk-mailgroups and the
980 (when (spam-group-spam-contents-p gnus-newsgroup-name)
981 (gnus-message 6 "Marking %s articles as spam"
982 (if spam-mark-only-unseen-as-spam
985 (let ((articles (if spam-mark-only-unseen-as-spam
986 gnus-newsgroup-unseen
987 gnus-newsgroup-unreads)))
988 (if spam-mark-new-messages-in-spam-group-as-spam
989 (dolist (article articles)
990 (gnus-summary-mark-article article gnus-spam-mark))
991 (gnus-message 9 "Did not mark new messages as spam.")))))
993 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
994 (if (and (car-safe groups) (listp (car-safe groups)))
995 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
996 (gnus-summary-kill-process-mark)
997 (let ((articles gnus-newsgroup-articles)
998 (backend-supports-deletions
999 (gnus-check-backend-function
1000 'request-move-article gnus-newsgroup-name))
1001 article tomove deletep)
1002 (dolist (article articles)
1003 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
1004 (gnus-summary-mark-article article gnus-expirable-mark)
1005 (push article tomove)))
1007 ;; now do the actual copies
1008 (dolist (group groups)
1011 (dolist (article tomove)
1012 (gnus-summary-set-process-mark article))
1014 (if (or (not backend-supports-deletions)
1015 (> (length groups) 1))
1017 (gnus-summary-copy-article nil group)
1019 (gnus-summary-move-article nil group)))))
1021 ;; now delete the articles, if there was a copy done, and the
1022 ;; backend allows it
1023 (when (and deletep backend-supports-deletions)
1024 (dolist (article tomove)
1025 (gnus-summary-set-process-mark article))
1027 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1028 (gnus-summary-delete-article nil))))
1030 (gnus-summary-yank-process-mark)
1033 (defun spam-ham-copy-or-move-routine (copy groups)
1034 (gnus-summary-kill-process-mark)
1035 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
1036 (backend-supports-deletions
1037 (gnus-check-backend-function
1038 'request-move-article gnus-newsgroup-name))
1039 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1040 article mark deletep respool)
1042 (when (member 'respool groups)
1043 (setq respool t) ; boolean for later
1044 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1046 ;; now do the actual move
1047 (dolist (group groups)
1048 (when (and todo (stringp group))
1049 (dolist (article todo)
1050 (when spam-mark-ham-unread-before-move-from-spam-group
1051 (gnus-summary-mark-article article gnus-unread-mark))
1052 (gnus-summary-set-process-mark article))
1054 (if respool ; respooling is with a "fake" group
1055 (let ((spam-split-disabled
1056 (or spam-split-disabled
1057 spam-disable-spam-split-during-ham-respool)))
1058 (gnus-summary-respool-article nil respool-method))
1059 (if (or (not backend-supports-deletions) ; else, we are not respooling
1060 (> (length groups) 1))
1061 (progn ; if copying, copy and set deletep
1062 (gnus-summary-copy-article nil group)
1064 (gnus-summary-move-article nil group))))) ; else move articles
1066 ;; now delete the articles, unless a) copy is t, and there was a copy done
1067 ;; b) a move was done to a single group
1068 ;; c) backend-supports-deletions is nil
1070 (when (and deletep backend-supports-deletions)
1071 (dolist (article todo)
1072 (gnus-summary-set-process-mark article))
1074 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1075 (gnus-summary-delete-article nil)))))
1077 (gnus-summary-yank-process-mark)
1080 (defun spam-ham-copy-routine (&rest groups)
1081 (if (and (car-safe groups) (listp (car-safe groups)))
1082 (apply 'spam-ham-copy-routine (car groups))
1083 (spam-ham-copy-or-move-routine t groups)))
1085 (defun spam-ham-move-routine (&rest groups)
1086 (if (and (car-safe groups) (listp (car-safe groups)))
1087 (apply 'spam-ham-move-routine (car groups))
1088 (spam-ham-copy-or-move-routine nil groups)))
1090 (defun spam-get-article-as-string (article)
1091 (when (numberp article)
1093 (gnus-request-article-this-buffer
1095 gnus-newsgroup-name)
1099 ;; (defun spam-get-article-as-filename (article)
1100 ;; (let ((article-filename))
1101 ;; (when (numberp article)
1102 ;; (nnml-possibly-change-directory
1103 ;; (gnus-group-real-name gnus-newsgroup-name))
1104 ;; (setq article-filename (expand-file-name
1105 ;; (int-to-string article) nnml-current-directory)))
1106 ;; (if (file-exists-p article-filename)
1110 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1111 "Fetch a field quickly, using the internal gnus-data-list function"
1112 (when (numberp article)
1113 (let* ((data-header (or prepared-data-header
1114 (spam-fetch-article-header article))))
1115 (if (arrayp data-header)
1117 ((equal field 'from)
1118 (mail-header-from data-header))
1119 ((equal field 'message-id)
1120 (mail-header-message-id data-header))
1121 ((equal field 'subject)
1122 (mail-header-subject data-header))
1123 ((equal field 'references)
1124 (mail-header-references data-header))
1125 ((equal field 'date)
1126 (mail-header-date data-header))
1127 ((equal field 'xref)
1128 (mail-header-xref data-header))
1129 ((equal field 'extra)
1130 (mail-header-extra data-header))
1133 (gnus-message 6 "Article %d has a nil data header" article)))))
1135 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1136 (spam-fetch-field-fast article 'from prepared-data-header))
1138 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1139 (spam-fetch-field-fast article 'subject prepared-data-header))
1141 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1142 (spam-fetch-field-fast article 'message-id prepared-data-header))
1144 (defun spam-generate-fake-headers (article)
1145 (let ((dh (spam-fetch-article-header article)))
1149 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1150 "Date: %s\nReferences: %s\nXref: %s\n")
1151 (spam-fetch-field-fast article 'from dh)
1152 (spam-fetch-field-fast article 'subject dh)
1153 (spam-fetch-field-fast article 'message-id dh)
1154 (spam-fetch-field-fast article 'date dh)
1155 (spam-fetch-field-fast article 'references dh)
1156 (spam-fetch-field-fast article 'xref dh))
1157 (when (spam-fetch-field-fast article 'extra dh)
1158 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1161 "spam-generate-fake-headers: article %d didn't have a valid header"
1164 (defun spam-fetch-article-header (article)
1166 (set-buffer gnus-summary-buffer)
1167 (gnus-read-header article)
1168 (nth 3 (assq article gnus-newsgroup-data))))
1171 ;;;; Spam determination.
1173 (defvar spam-list-of-checks
1174 '((spam-use-blacklist . spam-check-blacklist)
1175 (spam-use-regex-headers . spam-check-regex-headers)
1176 (spam-use-gmane-xref . spam-check-gmane-xref)
1177 (spam-use-regex-body . spam-check-regex-body)
1178 (spam-use-whitelist . spam-check-whitelist)
1179 (spam-use-BBDB . spam-check-BBDB)
1180 (spam-use-BBDB-exclusive . spam-check-BBDB)
1181 (spam-use-ifile . spam-check-ifile)
1182 (spam-use-spamoracle . spam-check-spamoracle)
1183 (spam-use-stat . spam-check-stat)
1184 (spam-use-blackholes . spam-check-blackholes)
1185 (spam-use-hashcash . spam-check-hashcash)
1186 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1187 (spam-use-spamassassin . spam-check-spamassassin)
1188 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1189 (spam-use-bogofilter . spam-check-bogofilter)
1190 (spam-use-bsfilter-headers . spam-check-bsfilter-headers)
1191 (spam-use-bsfilter . spam-check-bsfilter)
1192 (spam-use-crm114 . spam-check-crm114))
1193 "The spam-list-of-checks list contains pairs associating a
1194 parameter variable with a spam checking function. If the
1195 parameter variable is true, then the checking function is called,
1196 and its value decides what happens. Each individual check may
1197 return nil, t, or a mailgroup name. The value nil means that the
1198 check does not yield a decision, and so, that further checks are
1199 needed. The value t means that the message is definitely not
1200 spam, and that further spam checks should be inhibited.
1201 Otherwise, a mailgroup name or the symbol 'spam (depending on
1202 spam-split-symbolic-return) is returned where the mail should go,
1203 and further checks are also inhibited. The usual mailgroup name
1204 is the value of `spam-split-group', meaning that the message is
1205 definitely a spam.")
1207 (defvar spam-list-of-statistical-checks
1214 spam-use-spamassassin
1217 "The spam-list-of-statistical-checks list contains all the mail
1218 splitters that need to have the full message body available.
1219 Note that you should fetch extra headers if you don't like this,
1220 e.g. fetch the 'Received' header for spam-use-blackholes.")
1222 (defun spam-split (&rest specific-checks)
1223 "Split this message into the `spam' group if it is spam.
1224 This function can be used as an entry in the variable `nnmail-split-fancy',
1225 for example like this: (: spam-split). It can take checks as
1226 parameters. A string as a parameter will set the
1227 spam-split-group to that string.
1229 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1231 (setq spam-split-last-successful-check nil)
1232 (unless spam-split-disabled
1233 (let ((spam-split-group-choice spam-split-group))
1234 (dolist (check specific-checks)
1235 (when (stringp check)
1236 (setq spam-split-group-choice check)
1237 (setq specific-checks (delq check specific-checks))))
1239 (let ((spam-split-group spam-split-group-choice))
1242 (dolist (check spam-list-of-statistical-checks)
1243 (when (and (symbolp check)
1244 (or (symbol-value check)
1245 (memq check specific-checks)))
1247 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1248 (symbol-name check))
1250 ;; (progn (widen) (debug (buffer-string)))
1251 (let ((list-of-checks spam-list-of-checks)
1253 (while (and list-of-checks (not decision))
1254 (let ((pair (pop list-of-checks)))
1256 ;; either, given specific checks, this is one of them
1257 (and specific-checks (memq (car pair) specific-checks))
1258 ;; or, given no specific checks, spam-use-CHECK is set
1259 (and (null specific-checks) (symbol-value (car pair))))
1260 (gnus-message 6 "spam-split: calling the %s function"
1261 (symbol-name (cdr pair)))
1262 (setq decision (funcall (cdr pair)))
1263 ;; if we got a decision at all, save the current check
1265 (setq spam-split-last-successful-check (car pair)))
1267 (when (eq decision 'spam)
1268 (unless spam-split-symbolic-return
1271 (format "spam-split got %s but %s is nil"
1272 (symbol-name decision)
1273 (symbol-name spam-split-symbolic-return))))))))
1275 (if spam-split-symbolic-return-positive 'ham nil)
1278 (defun spam-find-spam ()
1279 "This function will detect spam in the current newsgroup using spam-split."
1282 (let* ((group gnus-newsgroup-name)
1283 (autodetect (gnus-parameter-spam-autodetect group))
1284 (methods (gnus-parameter-spam-autodetect-methods group))
1285 (first-method (nth 0 methods))
1286 (articles (if spam-autodetect-recheck-messages
1287 gnus-newsgroup-articles
1288 gnus-newsgroup-unseen))
1289 article-cannot-be-faked)
1291 (dolist (check spam-list-of-statistical-checks)
1292 (when (and (symbolp check)
1293 (memq check methods))
1294 (setq article-cannot-be-faked t)
1297 (when (memq 'default methods)
1298 (setq article-cannot-be-faked t))
1300 (when (and autodetect
1301 (not (equal first-method 'none)))
1304 (let ((id (spam-fetch-field-message-id-fast article))
1305 (subject (spam-fetch-field-subject-fast article))
1306 (sender (spam-fetch-field-from-fast article))
1310 (gnus-message 6 "Article %d has no message ID!" article))
1312 (when (and id spam-log-to-registry)
1313 (setq registry-lookup (spam-log-registration-type id 'incoming))
1314 (when registry-lookup
1317 "spam-find-spam: message %s was already registered incoming"
1320 (let* ((spam-split-symbolic-return t)
1321 (spam-split-symbolic-return-positive t)
1322 (fake-headers (spam-generate-fake-headers article))
1326 (if article-cannot-be-faked
1327 (gnus-request-article-this-buffer
1330 ;; else, we fake the article
1331 (when fake-headers (insert fake-headers)))
1332 (if (or (null first-method)
1333 (equal first-method 'default))
1335 (apply 'spam-split methods))))))
1336 (if (equal split-return 'spam)
1337 (gnus-summary-mark-article article gnus-spam-mark))
1339 (when (and id split-return spam-log-to-registry)
1340 (when (zerop (gnus-registry-group-count id))
1341 (gnus-registry-add-group
1342 id group subject sender))
1344 (unless registry-lookup
1345 (spam-log-processing-to-registry
1349 spam-split-last-successful-check
1353 (defvar spam-registration-functions
1354 ;; first the ham register, second the spam register function
1355 ;; third the ham unregister, fourth the spam unregister function
1356 '((spam-use-blacklist nil
1357 spam-blacklist-register-routine
1359 spam-blacklist-unregister-routine)
1360 (spam-use-whitelist spam-whitelist-register-routine
1362 spam-whitelist-unregister-routine
1364 (spam-use-ham-copy nil
1368 (spam-use-BBDB spam-BBDB-register-routine
1370 spam-BBDB-unregister-routine
1372 (spam-use-ifile spam-ifile-register-ham-routine
1373 spam-ifile-register-spam-routine
1374 spam-ifile-unregister-ham-routine
1375 spam-ifile-unregister-spam-routine)
1376 (spam-use-spamoracle spam-spamoracle-learn-ham
1377 spam-spamoracle-learn-spam
1378 spam-spamoracle-unlearn-ham
1379 spam-spamoracle-unlearn-spam)
1380 (spam-use-stat spam-stat-register-ham-routine
1381 spam-stat-register-spam-routine
1382 spam-stat-unregister-ham-routine
1383 spam-stat-unregister-spam-routine)
1384 ;; note that spam-use-gmane and spam-use-resend are not legitimate checks
1386 spam-report-gmane-register-routine
1387 ;; does Gmane support unregistration?
1390 (spam-use-resend spam-report-resend-register-ham-routine
1391 spam-report-resend-register-routine
1394 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1395 spam-spamassassin-register-spam-routine
1396 spam-spamassassin-unregister-ham-routine
1397 spam-spamassassin-unregister-spam-routine)
1398 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1399 spam-bogofilter-register-spam-routine
1400 spam-bogofilter-unregister-ham-routine
1401 spam-bogofilter-unregister-spam-routine)
1402 (spam-use-bsfilter spam-bsfilter-register-ham-routine
1403 spam-bsfilter-register-spam-routine
1404 spam-bsfilter-unregister-ham-routine
1405 spam-bsfilter-unregister-spam-routine)
1406 (spam-use-crm114 spam-crm114-register-ham-routine
1407 spam-crm114-register-spam-routine
1408 ;; does CRM114 Mailfilter support unregistration?
1411 "The spam-registration-functions list contains pairs
1412 associating a parameter variable with the ham and spam
1413 registration functions, and the ham and spam unregistration
1416 (defun spam-classification-valid-p (classification)
1417 (or (eq classification 'spam)
1418 (eq classification 'ham)))
1420 (defun spam-process-type-valid-p (process-type)
1421 (or (eq process-type 'incoming)
1422 (eq process-type 'process)))
1424 (defun spam-registration-check-valid-p (check)
1425 (assoc check spam-registration-functions))
1427 (defun spam-unregistration-check-valid-p (check)
1428 (assoc check spam-registration-functions))
1430 (defun spam-registration-function (classification check)
1431 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1432 (if (eq classification 'spam)
1436 (defun spam-unregistration-function (classification check)
1437 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1438 (if (eq classification 'spam)
1442 (defun spam-list-articles (articles classification)
1443 (let ((mark-check (if (eq classification 'spam)
1444 'spam-group-spam-mark-p
1445 'spam-group-ham-mark-p))
1446 alist mark-cache-yes mark-cache-no)
1447 (dolist (article articles)
1448 (let ((mark (gnus-summary-article-mark article)))
1449 (unless (or (memq mark mark-cache-yes)
1450 (memq mark mark-cache-no))
1451 (if (funcall mark-check
1454 (push mark mark-cache-yes)
1455 (push mark mark-cache-no)))
1456 (when (memq mark mark-cache-yes)
1457 (push article alist))))
1460 (defun spam-register-routine (classification
1462 &optional unregister
1464 (when (and (spam-classification-valid-p classification)
1465 (spam-registration-check-valid-p check))
1466 (let* ((register-function
1467 (spam-registration-function classification check))
1468 (unregister-function
1469 (spam-unregistration-function classification check))
1470 (run-function (if unregister
1473 (log-function (if unregister
1474 'spam-log-undo-registration
1475 'spam-log-processing-to-registry))
1479 ;; make list of articles, using specific-articles if given
1480 (setq articles (or specific-articles
1482 gnus-newsgroup-articles
1485 (when (> (length articles) 0)
1486 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1487 (if unregister "Unregistering" "Registering")
1489 (if specific-articles "specific" "")
1490 (symbol-name classification)
1491 (symbol-name check))
1492 (funcall run-function articles)
1493 ;; now log all the registrations (or undo them, depending on unregister)
1494 (dolist (article articles)
1495 (funcall log-function
1496 (spam-fetch-field-message-id-fast article)
1500 gnus-newsgroup-name)))))))
1502 ;;; log a ham- or spam-processor invocation to the registry
1503 (defun spam-log-processing-to-registry (id type classification check group)
1504 (when spam-log-to-registry
1505 (if (and (stringp id)
1507 (spam-process-type-valid-p type)
1508 (spam-classification-valid-p classification)
1509 (spam-registration-check-valid-p check))
1510 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1511 (cell (list classification check group)))
1512 (push cell cell-list)
1513 (gnus-registry-store-extra-entry
1520 (format "%s call with bad ID, type, classification, spam-check, or group"
1521 "spam-log-processing-to-registry")))))
1523 ;;; check if a ham- or spam-processor registration has been done
1524 (defun spam-log-registered-p (id type)
1525 (when spam-log-to-registry
1526 (if (and (stringp id)
1527 (spam-process-type-valid-p type))
1528 (cdr-safe (gnus-registry-fetch-extra id type))
1532 (format "%s called with bad ID, type, classification, or spam-check"
1533 "spam-log-registered-p"))
1536 ;;; check what a ham- or spam-processor registration says
1537 ;;; returns nil if conflicting registrations are found
1538 (defun spam-log-registration-type (id type)
1541 (dolist (reg (spam-log-registered-p id type))
1542 (let ((classification (nth 0 reg)))
1543 (when (spam-classification-valid-p classification)
1545 (not (eq classification decision)))
1546 (setq count (+ 1 count)))
1547 (setq decision classification))))
1553 ;;; check if a ham- or spam-processor registration needs to be undone
1554 (defun spam-log-unregistration-needed-p (id type classification check)
1555 (when spam-log-to-registry
1556 (if (and (stringp id)
1557 (spam-process-type-valid-p type)
1558 (spam-classification-valid-p classification)
1559 (spam-registration-check-valid-p check))
1560 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1562 (dolist (cell cell-list)
1564 (when (and (eq classification (nth 0 cell))
1565 (eq check (nth 1 cell)))
1571 (format "%s called with bad ID, type, classification, or spam-check"
1572 "spam-log-unregistration-needed-p"))
1576 ;;; undo a ham- or spam-processor registration (the group is not used)
1577 (defun spam-log-undo-registration (id type classification check &optional group)
1578 (when (and spam-log-to-registry
1579 (spam-log-unregistration-needed-p id type classification check))
1580 (if (and (stringp id)
1581 (spam-process-type-valid-p type)
1582 (spam-classification-valid-p classification)
1583 (spam-registration-check-valid-p check))
1584 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1585 new-cell-list found)
1586 (dolist (cell cell-list)
1587 (unless (and (eq classification (nth 0 cell))
1588 (eq check (nth 1 cell)))
1589 (push cell new-cell-list)))
1590 (gnus-registry-store-extra-entry
1595 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1596 "spam-log-undo-registration"))
1599 ;;; set up IMAP widening if it's necessary
1600 (defun spam-setup-widening ()
1601 (dolist (check spam-list-of-statistical-checks)
1602 (when (symbol-value check)
1603 (setq nnimap-split-download-body-default t))))
1607 (defun spam-check-gmane-xref ()
1609 (message-fetch-field "Xref")
1610 (message-fetch-field "Newsgroups")))
1611 (spam-split-group (if spam-split-symbolic-return
1614 (when header ; return nil when no header
1615 (when (string-match spam-gmane-xref-spam-group
1617 spam-split-group))))
1622 (defun spam-check-regex-body ()
1623 (let ((spam-regex-headers-ham spam-regex-body-ham)
1624 (spam-regex-headers-spam spam-regex-body-spam))
1625 (spam-check-regex-headers t)))
1630 (defun spam-check-regex-headers (&optional body)
1631 (let ((type (if body "body" "header"))
1632 (spam-split-group (if spam-split-symbolic-return
1636 (dolist (h-regex spam-regex-headers-ham)
1638 (goto-char (point-min))
1639 (when (re-search-forward h-regex nil t)
1640 (message "Ham regex %s search positive." type)
1642 (dolist (s-regex spam-regex-headers-spam)
1644 (goto-char (point-min))
1645 (when (re-search-forward s-regex nil t)
1646 (message "Spam regex %s search positive." type)
1648 (setq ret spam-split-group))))
1654 (defun spam-reverse-ip-string (ip)
1656 (mapconcat 'identity
1657 (nreverse (split-string ip "\\."))
1660 (defun spam-check-blackholes ()
1661 "Check the Received headers for blackholed relays."
1662 (let ((headers (message-fetch-field "received"))
1663 (spam-split-group (if spam-split-symbolic-return
1670 (goto-char (point-min))
1671 (gnus-message 6 "Checking headers for relay addresses")
1672 (while (re-search-forward
1673 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1674 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1675 (push (spam-reverse-ip-string (match-string 1))
1677 (dolist (server spam-blackhole-servers)
1679 (unless (and spam-blackhole-good-server-regex
1680 ;; match the good-server-regex against the reversed (again) IP string
1682 spam-blackhole-good-server-regex
1683 (spam-reverse-ip-string ip)))
1685 (let ((query-string (concat ip "." server)))
1687 (let ((query-result (query-dig query-string)))
1689 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1691 (push (list ip server query-result)
1693 ;; else, if not using dig.el
1694 (when (query-dns query-string)
1695 (gnus-message 6 "positive blackhole check")
1696 (push (list ip server (query-dns query-string 'TXT))
1707 (defun spam-check-hashcash ()
1708 "Check the headers for hashcash payments."
1709 (mail-check-payment))) ;mail-check-payment returns a boolean
1712 (defalias 'mail-check-payment 'ignore)
1713 (defalias 'spam-check-hashcash 'ignore))))
1717 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1718 ;;; <sacha@giotto.sj.ru>
1720 ;; all this is done inside a condition-case to trap errors
1727 ;; when the BBDB changes, we want to clear out our cache
1728 (defun spam-clear-cache-BBDB (&rest immaterial)
1729 (spam-clear-cache 'spam-use-BBDB))
1731 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1733 (defun spam-enter-ham-BBDB (addresses &optional remove)
1734 "Enter an address into the BBDB; implies ham (non-spam) sender"
1735 (dolist (from addresses)
1736 (when (stringp from)
1737 (let* ((parsed-address (gnus-extract-address-components from))
1738 (name (or (nth 0 parsed-address) "Ham Sender"))
1739 (remove-function (if remove
1740 'bbdb-delete-record-internal
1742 (net-address (nth 1 parsed-address))
1743 (record (and net-address
1744 (bbdb-search-simple nil net-address))))
1746 (gnus-message 6 "%s address %s %s BBDB"
1747 (if remove "Deleting" "Adding")
1749 (if remove "from" "to"))
1751 (funcall remove-function record)
1752 (bbdb-create-internal name nil net-address nil nil
1753 "ham sender added by spam.el")))))))
1755 (defun spam-BBDB-register-routine (articles &optional unregister)
1757 (dolist (article articles)
1758 (when (stringp (spam-fetch-field-from-fast article))
1759 (push (spam-fetch-field-from-fast article) addresses)))
1760 ;; now do the register/unregister action
1761 (spam-enter-ham-BBDB addresses unregister)))
1763 (defun spam-BBDB-unregister-routine (articles)
1764 (spam-BBDB-register-routine articles t))
1766 (defun spam-check-BBDB ()
1767 "Mail from people in the BBDB is classified as ham or non-spam"
1768 (let ((who (message-fetch-field "from"))
1769 (spam-split-group (if spam-split-symbolic-return
1772 bbdb-cache bbdb-hashtable)
1773 (when spam-cache-lookups
1774 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1777 ;; this is the expanded (bbdb-hashtable) macro
1778 ;; without the debugging support
1779 (with-current-buffer (bbdb-buffer)
1781 (save-window-excursion
1782 (bbdb-records nil t)
1784 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1786 (setq who (nth 1 (gnus-extract-address-components who)))
1788 (if spam-cache-lookups
1790 (intern-soft who bbdb-cache))
1791 (bbdb-search-simple nil who))
1793 (if spam-use-BBDB-exclusive
1798 (defalias 'bbdb-search-simple 'ignore)
1799 (defalias 'bbdb-records 'ignore)
1800 (defalias 'bbdb-buffer 'ignore)
1801 (defalias 'spam-check-BBDB 'ignore)
1802 (defalias 'spam-BBDB-register-routine 'ignore)
1803 (defalias 'spam-enter-ham-BBDB 'ignore)
1804 (defalias 'bbdb-create-internal 'ignore)
1805 (defalias 'bbdb-delete-record-internal 'ignore)
1806 (defalias 'bbdb-records 'ignore))))
1811 ;;; check the ifile backend; return nil if the mail was NOT classified
1814 (defun spam-get-ifile-database-parameter ()
1815 "Get the command-line parameter for ifile's database from
1816 spam-ifile-database-path."
1817 (if spam-ifile-database-path
1818 (format "--db-file=%s" spam-ifile-database-path)
1821 (defun spam-check-ifile ()
1822 "Check the ifile backend for the classification of this message."
1823 (let ((article-buffer-name (buffer-name))
1824 (spam-split-group (if spam-split-symbolic-return
1829 (let ((temp-buffer-name (buffer-name))
1830 (db-param (spam-get-ifile-database-parameter)))
1832 (set-buffer article-buffer-name)
1833 (apply 'call-process-region
1834 (point-min) (point-max) spam-ifile-path
1835 nil temp-buffer-name nil "-c"
1836 (if db-param `(,db-param "-q") `("-q"))))
1837 ;; check the return now (we're back in the temp buffer)
1838 (goto-char (point-min))
1840 (setq category (buffer-substring (point) (point-at-eol))))
1841 (when (not (zerop (length category))) ; we need a category here
1842 (if spam-ifile-all-categories
1843 (setq return category)
1844 ;; else, if spam-ifile-all-categories is not set...
1845 (when (string-equal spam-ifile-spam-category category)
1846 (setq return spam-split-group)))))) ; note return is nil otherwise
1849 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1850 "Register an article, given as a string, with a category.
1851 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1852 (let ((category (or category gnus-newsgroup-name))
1853 (add-or-delete-option (if unregister "-d" "-i"))
1854 (db (spam-get-ifile-database-parameter))
1857 (dolist (article articles)
1858 (let ((article-string (spam-get-article-as-string article)))
1859 (when (stringp article-string)
1860 (insert article-string))))
1861 (apply 'call-process-region
1862 (point-min) (point-max) spam-ifile-path
1864 add-or-delete-option category
1865 (if db `(,db "-h") `("-h"))))))
1867 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1868 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1870 (defun spam-ifile-unregister-spam-routine (articles)
1871 (spam-ifile-register-spam-routine articles t))
1873 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1874 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1876 (defun spam-ifile-unregister-ham-routine (articles)
1877 (spam-ifile-register-ham-routine articles t))
1884 (let ((spam-stat-install-hooks nil))
1885 (require 'spam-stat))
1887 (defun spam-check-stat ()
1888 "Check the spam-stat backend for the classification of this message"
1889 (let ((spam-split-group (if spam-split-symbolic-return
1892 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1893 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1895 (spam-stat-split-fancy)))
1897 (defun spam-stat-register-spam-routine (articles &optional unregister)
1898 (dolist (article articles)
1899 (let ((article-string (spam-get-article-as-string article)))
1901 (insert article-string)
1903 (spam-stat-buffer-change-to-non-spam)
1904 (spam-stat-buffer-is-spam))))))
1906 (defun spam-stat-unregister-spam-routine (articles)
1907 (spam-stat-register-spam-routine articles t))
1909 (defun spam-stat-register-ham-routine (articles &optional unregister)
1910 (dolist (article articles)
1911 (let ((article-string (spam-get-article-as-string article)))
1913 (insert article-string)
1915 (spam-stat-buffer-change-to-spam)
1916 (spam-stat-buffer-is-non-spam))))))
1918 (defun spam-stat-unregister-ham-routine (articles)
1919 (spam-stat-register-ham-routine articles t))
1921 (defun spam-maybe-spam-stat-load ()
1922 (when spam-use-stat (spam-stat-load)))
1924 (defun spam-maybe-spam-stat-save ()
1925 (when spam-use-stat (spam-stat-save))))
1928 (defalias 'spam-stat-load 'ignore)
1929 (defalias 'spam-stat-save 'ignore)
1930 (defalias 'spam-maybe-spam-stat-load 'ignore)
1931 (defalias 'spam-maybe-spam-stat-save 'ignore)
1932 (defalias 'spam-stat-register-ham-routine 'ignore)
1933 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1934 (defalias 'spam-stat-register-spam-routine 'ignore)
1935 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1936 (defalias 'spam-stat-buffer-is-spam 'ignore)
1937 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1938 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1939 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1940 (defalias 'spam-stat-split-fancy 'ignore)
1941 (defalias 'spam-check-stat 'ignore))))
1945 ;;;; Blacklists and whitelists.
1947 (defvar spam-whitelist-cache nil)
1948 (defvar spam-blacklist-cache nil)
1950 (defun spam-kill-whole-line ()
1952 (let ((kill-whole-line t))
1955 ;;; address can be a list, too
1956 (defun spam-enter-whitelist (address &optional remove)
1957 "Enter ADDRESS (list or single) into the whitelist.
1958 With a non-nil REMOVE, remove them."
1959 (interactive "sAddress: ")
1960 (spam-enter-list address spam-whitelist remove)
1961 (setq spam-whitelist-cache nil)
1962 (spam-clear-cache 'spam-use-whitelist))
1964 ;;; address can be a list, too
1965 (defun spam-enter-blacklist (address &optional remove)
1966 "Enter ADDRESS (list or single) into the blacklist.
1967 With a non-nil REMOVE, remove them."
1968 (interactive "sAddress: ")
1969 (spam-enter-list address spam-blacklist remove)
1970 (setq spam-blacklist-cache nil)
1971 (spam-clear-cache 'spam-use-whitelist))
1973 (defun spam-enter-list (addresses file &optional remove)
1974 "Enter ADDRESSES into the given FILE.
1975 Either the whitelist or the blacklist files can be used. With
1976 REMOVE not nil, remove the ADDRESSES."
1977 (if (stringp addresses)
1978 (spam-enter-list (list addresses) file remove)
1979 ;; else, we have a list of addresses here
1980 (unless (file-exists-p (file-name-directory file))
1981 (make-directory (file-name-directory file) t))
1984 (find-file-noselect file))
1985 (dolist (a addresses)
1987 (goto-char (point-min))
1988 (if (re-search-forward (regexp-quote a) nil t)
1989 ;; found the address
1991 (spam-kill-whole-line))
1992 ;; else, the address was not found
1994 (goto-char (point-max))
2000 (defun spam-filelist-build-cache (type)
2001 (let ((cache (if (eq type 'spam-use-blacklist)
2002 spam-blacklist-cache
2003 spam-whitelist-cache))
2005 (unless (gethash type spam-caches)
2007 (let ((address (pop cache)))
2008 (unless (zerop (length address)) ; 0 for a nil address too
2009 (setq address (regexp-quote address))
2010 ;; fix regexp-quote's treatment of user-intended regexes
2011 (while (string-match "\\\\\\*" address)
2012 (setq address (replace-match ".*" t t address))))
2013 (push address parsed-cache)))
2014 (puthash type parsed-cache spam-caches))))
2016 (defun spam-filelist-check-cache (type from)
2017 (when (stringp from)
2018 (spam-filelist-build-cache type)
2020 (dolist (address (gethash type spam-caches))
2021 (when (and address (string-match address from))
2026 ;;; returns t if the sender is in the whitelist, nil or
2027 ;;; spam-split-group otherwise
2028 (defun spam-check-whitelist ()
2029 ;; FIXME! Should it detect when file timestamps change?
2030 (let ((spam-split-group (if spam-split-symbolic-return
2033 (unless spam-whitelist-cache
2034 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2035 (if (spam-from-listed-p 'spam-use-whitelist)
2037 (if spam-use-whitelist-exclusive
2041 (defun spam-check-blacklist ()
2042 ;; FIXME! Should it detect when file timestamps change?
2043 (let ((spam-split-group (if spam-split-symbolic-return
2046 (unless spam-blacklist-cache
2047 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2048 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
2050 (defun spam-parse-list (file)
2051 (when (file-readable-p file)
2052 (let (contents address)
2054 (insert-file-contents file)
2056 (setq address (buffer-substring (point) (point-at-eol)))
2058 ;; insert the e-mail address if detected, otherwise the raw data
2059 (unless (zerop (length address))
2060 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2061 (push (or pure-address address) contents)))))
2062 (nreverse contents))))
2064 (defun spam-from-listed-p (type)
2065 (let ((from (message-fetch-field "from"))
2067 (spam-filelist-check-cache type from)))
2069 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2070 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2071 (declassification (if blacklist 'ham 'spam))
2073 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2075 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2076 from addresses unregister-list article-unregister-list)
2077 (dolist (article articles)
2078 (let ((from (spam-fetch-field-from-fast article))
2079 (id (spam-fetch-field-message-id-fast article))
2081 (when (stringp from)
2082 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2083 (when (and (not sender-ignored)
2084 (stringp ignore-regex)
2085 (string-match ignore-regex from))
2086 (setq sender-ignored t)))
2087 ;; remember the messages we need to unregister, unless remove is set
2090 (spam-log-unregistration-needed-p
2091 id 'process declassification de-symbol))
2092 (push article article-unregister-list)
2093 (push from unregister-list))
2094 (unless sender-ignored
2095 (push from addresses)))))
2098 (funcall enter-function addresses t) ; unregister all these addresses
2099 ;; else, register normally and unregister what we need to
2100 (funcall remove-function unregister-list t)
2101 (dolist (article article-unregister-list)
2102 (spam-log-undo-registration
2103 (spam-fetch-field-message-id-fast article)
2107 (funcall enter-function addresses nil))))
2109 (defun spam-blacklist-unregister-routine (articles)
2110 (spam-blacklist-register-routine articles t))
2112 (defun spam-blacklist-register-routine (articles &optional unregister)
2113 (spam-filelist-register-routine articles t unregister))
2115 (defun spam-whitelist-unregister-routine (articles)
2116 (spam-whitelist-register-routine articles t))
2118 (defun spam-whitelist-register-routine (articles &optional unregister)
2119 (spam-filelist-register-routine articles nil unregister))
2122 ;;;; Spam-report glue
2123 (defun spam-report-gmane-register-routine (articles)
2125 (apply 'spam-report-gmane articles)))
2127 (defun spam-report-resend-register-ham-routine (articles)
2128 (spam-report-resend-register-routine articles t))
2130 (defun spam-report-resend-register-routine (articles &optional ham)
2131 (let* ((resend-to-gp
2133 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2134 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2135 (spam-report-resend-to (or (car-safe resend-to-gp)
2136 spam-report-resend-to)))
2137 (spam-report-resend articles ham)))
2141 (defun spam-check-bogofilter-headers (&optional score)
2142 (let ((header (message-fetch-field spam-bogofilter-header))
2143 (spam-split-group (if spam-split-symbolic-return
2146 (when header ; return nil when no header
2147 (if score ; scoring mode
2148 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2149 (match-string 1 header)
2151 ;; spam detection mode
2152 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2154 spam-split-group)))))
2156 ;; return something sensible if the score can't be determined
2157 (defun spam-bogofilter-score (&optional recheck)
2158 "Get the Bogofilter spamicity score"
2160 (save-window-excursion
2161 (gnus-summary-show-article t)
2162 (set-buffer gnus-article-buffer)
2163 (let ((score (or (unless recheck
2164 (spam-check-bogofilter-headers t))
2165 (spam-check-bogofilter t))))
2166 (gnus-summary-show-article)
2167 (message "Spamicity score %s" score)
2170 (defun spam-check-bogofilter (&optional score)
2171 "Check the Bogofilter backend for the classification of this message"
2172 (let ((article-buffer-name (buffer-name))
2173 (db spam-bogofilter-database-directory)
2176 (let ((temp-buffer-name (buffer-name)))
2178 (set-buffer article-buffer-name)
2179 (apply 'call-process-region
2180 (point-min) (point-max)
2181 spam-bogofilter-path
2182 nil temp-buffer-name nil
2183 (if db `("-d" ,db "-v") `("-v"))))
2184 (setq return (spam-check-bogofilter-headers score))))
2187 (defun spam-bogofilter-register-with-bogofilter (articles
2189 &optional unregister)
2190 "Register an article, given as a string, as spam or non-spam."
2191 (dolist (article articles)
2192 (let ((article-string (spam-get-article-as-string article))
2193 (db spam-bogofilter-database-directory)
2194 (switch (if unregister
2196 spam-bogofilter-spam-strong-switch
2197 spam-bogofilter-ham-strong-switch)
2199 spam-bogofilter-spam-switch
2200 spam-bogofilter-ham-switch))))
2201 (when (stringp article-string)
2203 (insert article-string)
2205 (apply 'call-process-region
2206 (point-min) (point-max)
2207 spam-bogofilter-path
2209 (if db `("-d" ,db "-v") `("-v"))))))))
2211 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2212 (spam-bogofilter-register-with-bogofilter articles t unregister))
2214 (defun spam-bogofilter-unregister-spam-routine (articles)
2215 (spam-bogofilter-register-spam-routine articles t))
2217 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2218 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2220 (defun spam-bogofilter-unregister-ham-routine (articles)
2221 (spam-bogofilter-register-ham-routine articles t))
2226 (defun spam-check-spamoracle ()
2227 "Run spamoracle on an article to determine whether it's spam."
2228 (let ((article-buffer-name (buffer-name))
2229 (spam-split-group (if spam-split-symbolic-return
2233 (let ((temp-buffer-name (buffer-name)))
2235 (set-buffer article-buffer-name)
2237 (apply 'call-process-region
2238 (point-min) (point-max)
2239 spam-spamoracle-binary
2240 nil temp-buffer-name nil
2241 (if spam-spamoracle-database
2242 `("-f" ,spam-spamoracle-database "mark")
2246 (set-buffer temp-buffer-name)
2247 (goto-char (point-min))
2248 (when (re-search-forward "^X-Spam: yes;" nil t)
2250 (error "Error running spamoracle: %s" status))))))))
2252 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2253 "Run spamoracle in training mode."
2255 (let ((temp-buffer-name (buffer-name)))
2257 (goto-char (point-min))
2258 (dolist (article articles)
2259 (insert (spam-get-article-as-string article)))
2260 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2264 (apply 'call-process-region
2265 (point-min) (point-max)
2266 spam-spamoracle-binary
2267 nil temp-buffer-name nil
2268 (if spam-spamoracle-database
2269 `("-f" ,spam-spamoracle-database
2272 (unless (eq 0 status)
2273 (error "Error running spamoracle: %s" status)))))))
2275 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2276 (spam-spamoracle-learn articles nil unregister))
2278 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2279 (spam-spamoracle-learn-ham articles t))
2281 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2282 (spam-spamoracle-learn articles t unregister))
2284 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2285 (spam-spamoracle-learn-spam articles t))
2289 ;;; based mostly on the bogofilter code
2290 (defun spam-check-spamassassin-headers (&optional score)
2291 "Check the SpamAssassin headers for the classification of this message."
2292 (if score ; scoring mode
2293 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2295 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2296 (match-string 1 header)
2298 ;; spam detection mode
2299 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2300 (spam-split-group (if spam-split-symbolic-return
2303 (when header ; return nil when no header
2304 (when (string-match spam-spamassassin-positive-spam-flag-header
2306 spam-split-group)))))
2308 (defun spam-check-spamassassin (&optional score)
2309 "Check the SpamAssassin backend for the classification of this message."
2310 (let ((article-buffer-name (buffer-name)))
2312 (let ((temp-buffer-name (buffer-name)))
2314 (set-buffer article-buffer-name)
2315 (apply 'call-process-region
2316 (point-min) (point-max) spam-spamassassin-path
2317 nil temp-buffer-name nil spam-spamassassin-arguments))
2318 ;; check the return now (we're back in the temp buffer)
2319 (goto-char (point-min))
2320 (spam-check-spamassassin-headers score)))))
2322 ;; return something sensible if the score can't be determined
2323 (defun spam-spamassassin-score (&optional recheck)
2324 "Get the SpamAssassin score"
2326 (save-window-excursion
2327 (gnus-summary-show-article t)
2328 (set-buffer gnus-article-buffer)
2329 (let ((score (or (unless recheck
2330 (spam-check-spamassassin-headers t))
2331 (spam-check-spamassassin t))))
2332 (gnus-summary-show-article)
2333 (message "SpamAssassin score %s" score)
2336 (defun spam-spamassassin-register-with-sa-learn (articles spam
2337 &optional unregister)
2338 "Register articles with spamassassin's sa-learn as spam or non-spam."
2340 (let ((action (if unregister spam-sa-learn-unregister-switch
2341 (if spam spam-sa-learn-spam-switch
2342 spam-sa-learn-ham-switch)))
2343 (summary-buffer-name (buffer-name)))
2345 ;; group the articles into mbox format
2346 (dolist (article articles)
2347 (let (article-string)
2349 (set-buffer summary-buffer-name)
2350 (setq article-string (spam-get-article-as-string article)))
2351 (when (stringp article-string)
2352 (insert "From \n") ; mbox separator (sa-learn only checks the
2353 ; first five chars, so we can get away with
2355 (insert article-string)
2357 ;; call sa-learn on all messages at the same time
2358 (apply 'call-process-region
2359 (point-min) (point-max)
2361 nil nil nil "--mbox"
2362 (if spam-sa-learn-rebuild
2364 `("--no-rebuild" ,action)))))))
2366 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2367 (spam-spamassassin-register-with-sa-learn articles t unregister))
2369 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2370 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2372 (defun spam-spamassassin-unregister-spam-routine (articles)
2373 (spam-spamassassin-register-with-sa-learn articles t t))
2375 (defun spam-spamassassin-unregister-ham-routine (articles)
2376 (spam-spamassassin-register-with-sa-learn articles nil t))
2380 ;;; based mostly on the bogofilter code
2381 (defun spam-check-bsfilter-headers (&optional score)
2383 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2385 (let ((header (nnmail-fetch-field spam-bsfilter-header))
2386 (spam-split-group (if spam-split-symbolic-return
2389 (when header ; return nil when no header
2390 (when (string-match "YES" header)
2391 spam-split-group)))))
2393 ;; return something sensible if the score can't be determined
2394 (defun spam-bsfilter-score (&optional recheck)
2395 "Get the Bsfilter spamicity score"
2397 (save-window-excursion
2398 (gnus-summary-show-article t)
2399 (set-buffer gnus-article-buffer)
2400 (let ((score (or (unless recheck
2401 (spam-check-bsfilter-headers t))
2402 (spam-check-bsfilter t))))
2403 (gnus-summary-show-article)
2404 (message "Spamicity score %s" score)
2407 (defun spam-check-bsfilter (&optional score)
2408 "Check the Bsfilter backend for the classification of this message"
2409 (let ((article-buffer-name (buffer-name))
2410 (dir spam-bsfilter-database-directory)
2413 (let ((temp-buffer-name (buffer-name)))
2415 (set-buffer article-buffer-name)
2416 (apply 'call-process-region
2417 (point-min) (point-max)
2419 nil temp-buffer-name nil
2422 "--insert-probability"
2424 (list "--homedir" dir))))
2425 (setq return (spam-check-bsfilter-headers score))))
2428 (defun spam-bsfilter-register-with-bsfilter (articles
2430 &optional unregister)
2431 "Register an article, given as a string, as spam or non-spam."
2432 (dolist (article articles)
2433 (let ((article-string (spam-get-article-as-string article))
2434 (switch (if unregister
2436 spam-bsfilter-spam-strong-switch
2437 spam-bsfilter-ham-strong-switch)
2439 spam-bsfilter-spam-switch
2440 spam-bsfilter-ham-switch))))
2441 (when (stringp article-string)
2443 (insert article-string)
2444 (apply 'call-process-region
2445 (point-min) (point-max)
2449 (when spam-bsfilter-database-directory
2451 spam-bsfilter-database-directory))))))))
2453 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2454 (spam-bsfilter-register-with-bsfilter articles t unregister))
2456 (defun spam-bsfilter-unregister-spam-routine (articles)
2457 (spam-bsfilter-register-spam-routine articles t))
2459 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2460 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2462 (defun spam-bsfilter-unregister-ham-routine (articles)
2463 (spam-bsfilter-register-ham-routine articles t))
2466 ;;;; CRM114 Mailfilter
2467 (defun spam-check-crm114-headers (&optional score)
2468 (let ((header (message-fetch-field spam-crm114-header))
2469 (spam-split-group (if spam-split-symbolic-return
2472 (when header ; return nil when no header
2473 (if score ; scoring mode
2474 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2475 (match-string 1 header)
2477 ;; spam detection mode
2478 (when (string-match spam-crm114-positive-spam-header
2480 spam-split-group)))))
2482 ;; return something sensible if the score can't be determined
2483 (defun spam-crm114-score ()
2484 "Get the CRM114 Mailfilter pR"
2486 (save-window-excursion
2487 (gnus-summary-show-article t)
2488 (set-buffer gnus-article-buffer)
2489 (let ((score (or (spam-check-crm114-headers t)
2490 (spam-check-crm114 t))))
2491 (gnus-summary-show-article)
2492 (message "pR: %s" score)
2495 (defun spam-check-crm114 (&optional score)
2496 "Check the CRM114 Mailfilter backend for the classification of this message"
2497 (let ((article-buffer-name (buffer-name))
2498 (db spam-crm114-database-directory)
2501 (let ((temp-buffer-name (buffer-name)))
2503 (set-buffer article-buffer-name)
2504 (apply 'call-process-region
2505 (point-min) (point-max)
2507 nil temp-buffer-name nil
2508 (when db (list (concat "--fileprefix=" db)))))
2509 (setq return (spam-check-crm114-headers score))))
2512 (defun spam-crm114-register-with-crm114 (articles
2514 &optional unregister)
2515 "Register an article, given as a string, as spam or non-spam."
2516 (dolist (article articles)
2517 (let ((article-string (spam-get-article-as-string article))
2518 (db spam-crm114-database-directory)
2519 (switch (if unregister
2521 spam-crm114-spam-strong-switch
2522 spam-crm114-ham-strong-switch)
2524 spam-crm114-spam-switch
2525 spam-crm114-ham-switch))))
2526 (when (stringp article-string)
2528 (insert article-string)
2530 (apply 'call-process-region
2531 (point-min) (point-max)
2534 (when db (list switch (concat "--fileprefix=" db)))))))))
2536 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2537 (spam-crm114-register-with-crm114 articles t unregister))
2539 (defun spam-crm114-unregister-spam-routine (articles)
2540 (spam-crm114-register-spam-routine articles t))
2542 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2543 (spam-crm114-register-with-crm114 articles nil unregister))
2545 (defun spam-crm114-unregister-ham-routine (articles)
2546 (spam-crm114-register-ham-routine articles t))
2552 (defun spam-initialize (&rest symbols)
2553 "Install the spam.el hooks and do other initialization.
2554 When SYMBOLS is given, set those variables to t. This is so you
2555 can call spam-initialize before you set spam-use-* variables on
2556 explicitly, and matters only if you need the extra headers
2557 installed through spam-necessary-extra-headers."
2560 (dolist (var symbols)
2563 (dolist (header (spam-necessary-extra-headers))
2564 (add-to-list 'nnmail-extra-headers header)
2565 (add-to-list 'gnus-extra-headers header))
2567 (setq spam-install-hooks t)
2568 ;; TODO: How do we redo this every time spam-face is customized?
2569 (push '((eq mark gnus-spam-mark) . spam-face)
2570 gnus-summary-highlight)
2571 ;; Add hooks for loading and saving the spam stats
2572 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2573 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2574 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2575 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2576 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2577 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2578 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2580 (defun spam-unload-hook ()
2581 "Uninstall the spam.el hooks"
2583 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2584 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2585 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2586 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2587 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2588 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2589 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2591 (when spam-install-hooks
2596 ;;; spam.el ends here