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)
733 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
734 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
735 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
736 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
737 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
738 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
739 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
740 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
741 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle)
742 (nil ham spam-use-crm114))
743 "The `spam-list-of-processors' list.
744 This list contains pairs associating the obsolete ham/spam exit
745 processor variables with a classification and a spam-use-*
746 variable. When the processor variable is nil, just the
747 classification and spam-use-* check variable are used.")
749 (defun spam-group-processor-p (group check &optional classification)
750 "Checks if GROUP has a CHECK with CLASSIFICATION registered.
751 Also accepts the obsolete processors, which can be found in
752 gnus.el and in spam-list-of-processors."
753 (if (and (stringp group)
755 (let ((old-style (assq check spam-list-of-processors))
756 (parameters (nth 0 (gnus-parameter-spam-process group)))
758 (if old-style ; old-style processor
759 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
760 ;; now search for the parameter
761 (dolist (parameter parameters)
762 (when (and (null found)
764 (eq classification (nth 0 parameter))
765 (eq check (nth 1 parameter)))
770 (defun spam-report-articles-gmane (n)
771 "Report the current message as spam via Gmane.
772 Respects the process/prefix convention."
774 (dolist (article (gnus-summary-work-articles n))
775 (gnus-summary-remove-process-mark article)
776 (spam-report-gmane article)))
778 (defun spam-report-articles-resend (n)
779 "Report the current message as spam by resending it.
780 Respects the process/prefix convention. Also see
781 `spam-report-resend-to'."
784 (gnus-parameter-spam-resend-to gnus-newsgroup-name))
785 (spam-report-resend-to (or (car-safe gp)
786 spam-report-resend-to))
787 (articles (gnus-summary-work-articles n)))
788 (spam-report-resend articles)
789 (dolist (article articles)
790 (gnus-summary-remove-process-mark article))))
792 (defun spam-necessary-extra-headers ()
793 "Return the extra headers spam.el thinks are necessary."
795 (when (or spam-use-spamassassin
796 spam-use-spamassassin-headers
797 spam-use-regex-headers)
798 (push 'X-Spam-Status list))
801 (defun spam-user-format-function-S (headers)
803 (spam-summary-score headers)))
805 (defun spam-article-sort-by-spam-status (h1 h2)
806 "Sort articles by score."
808 (dolist (header (spam-necessary-extra-headers))
809 (let ((s1 (spam-summary-score h1 header))
810 (s2 (spam-summary-score h2 header)))
812 (setq result (< s1 s2))
816 (defun spam-extra-header-to-number (header headers)
817 "Transform an extra header to a number."
818 (if (gnus-extra-header header headers)
820 ((eq header 'X-Spam-Status)
821 (string-to-number (gnus-replace-in-string
822 (gnus-extra-header header headers)
824 ;; for CRM checking, it's probably faster to just do the string match
825 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
826 (match-string 1 header))
830 (defun spam-summary-score (headers &optional specific-header)
831 "Score an article for the summary buffer, as fast as possible.
832 With SPECIFIC-HEADER, returns only that header's score.
833 Will not return a nil score."
837 (list specific-header)
838 (spam-necessary-extra-headers)))
840 (spam-extra-header-to-number header headers))
845 (defun spam-generic-score (&optional recheck)
846 "Invoke whatever scoring method we can."
849 ((or spam-use-spamassassin spam-use-spamassassin-headers)
850 (spam-spamassassin-score recheck))
851 ((or spam-use-bsfilter spam-use-bsfilter-headers)
852 (spam-bsfilter-score recheck))
855 (t (spam-bogofilter-score recheck))))
857 ;;; Summary entry and exit processing.
859 (defun spam-summary-prepare ()
860 (setq spam-old-ham-articles
861 (spam-list-articles gnus-newsgroup-articles 'ham))
862 (setq spam-old-spam-articles
863 (spam-list-articles gnus-newsgroup-articles 'spam))
864 (spam-mark-junk-as-spam-routine))
866 ;; The spam processors are invoked for any group, spam or ham or neither
867 (defun spam-summary-prepare-exit ()
868 (unless gnus-group-is-exiting-without-update-p
869 (gnus-message 6 "Exiting summary buffer and applying spam rules")
871 ;; first of all, unregister any articles that are no longer ham or spam
872 ;; we have to iterate over the processors, or else we'll be too slow
873 (dolist (classification '(spam ham))
874 (let* ((old-articles (if (eq classification 'spam)
875 spam-old-spam-articles
876 spam-old-ham-articles))
877 (new-articles (spam-list-articles
878 gnus-newsgroup-articles
880 (changed-articles (spam-set-difference new-articles old-articles)))
881 ;; now that we have the changed articles, we go through the processors
882 (dolist (processor-param spam-list-of-processors)
883 (let ((processor (nth 0 processor-param))
884 (processor-classification (nth 1 processor-param))
885 (check (nth 2 processor-param))
887 (dolist (article changed-articles)
888 (let ((id (spam-fetch-field-message-id-fast article)))
889 (when (spam-log-unregistration-needed-p
890 id 'process classification check)
891 (push article unregister-list))))
892 ;; call spam-register-routine with specific articles to unregister,
893 ;; when there are articles to unregister and the check is enabled
894 (when (and unregister-list (symbol-value check))
895 (spam-register-routine
896 classification check t unregister-list))))))
898 ;; find all the spam processors applicable to this group
899 (dolist (processor-param spam-list-of-processors)
900 (let ((processor (nth 0 processor-param))
901 (classification (nth 1 processor-param))
902 (check (nth 2 processor-param)))
903 (when (and (eq 'spam classification)
904 (spam-group-processor-p gnus-newsgroup-name check classification))
905 (spam-register-routine classification check))))
907 (unless (and spam-move-spam-nonspam-groups-only
908 (spam-group-spam-contents-p gnus-newsgroup-name))
909 (let* ((group (gnus-parameter-spam-process-destination
910 gnus-newsgroup-name))
911 (num (spam-mark-spam-as-expired-and-move-routine group)))
914 "%d spam messages are marked as expired%s."
917 (format " and moved it to %s" group)
920 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
921 ;; expire spam, in case the above did not expire them
922 (let ((num (spam-mark-spam-as-expired-and-move-routine nil)))
925 "%d spam messages were marked as expired."
928 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
929 (and (spam-group-spam-contents-p gnus-newsgroup-name)
930 spam-process-ham-in-spam-groups)
931 spam-process-ham-in-nonham-groups)
932 ;; find all the ham processors applicable to this group
933 (dolist (processor-param spam-list-of-processors)
934 (let ((processor (nth 0 processor-param))
935 (classification (nth 1 processor-param))
936 (check (nth 2 processor-param)))
937 (when (and (eq 'ham classification)
938 (spam-group-processor-p gnus-newsgroup-name check classification))
939 (spam-register-routine classification check)))))
941 (when (spam-group-processor-p gnus-newsgroup-name 'ham 'spam-use-ham-copy)
943 (spam-ham-copy-routine
944 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
946 (gnus-message 6 "%d ham messages were copied" num))))
948 ;; now move all ham articles out of spam groups
949 (when (spam-group-spam-contents-p gnus-newsgroup-name)
951 (spam-ham-move-routine
952 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
954 (gnus-message 6 "%d ham messages were moved from spam group" num)))))
956 (setq spam-old-ham-articles nil)
957 (setq spam-old-spam-articles nil))
959 (defun spam-set-difference (list1 list2)
960 "Return a set difference of LIST1 and LIST2.
961 When either list is nil, the other is returned."
962 (if (and list1 list2)
963 ;; we have two non-nil lists
965 (dolist (item (append list1 list2))
966 (when (and (memq item list1) (memq item list2))
967 (setq list1 (delq item list1))
968 (setq list2 (delq item list2))))
969 (append list1 list2))
970 ;; if either of the lists was nil, return the other one
971 (if list1 list1 list2)))
973 (defun spam-mark-junk-as-spam-routine ()
974 ;; check the global list of group names spam-junk-mailgroups and the
976 (when (spam-group-spam-contents-p gnus-newsgroup-name)
977 (gnus-message 6 "Marking %s articles as spam"
978 (if spam-mark-only-unseen-as-spam
981 (let ((articles (if spam-mark-only-unseen-as-spam
982 gnus-newsgroup-unseen
983 gnus-newsgroup-unreads)))
984 (if spam-mark-new-messages-in-spam-group-as-spam
985 (dolist (article articles)
986 (gnus-summary-mark-article article gnus-spam-mark))
987 (gnus-message 9 "Did not mark new messages as spam.")))))
989 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
990 (if (and (car-safe groups) (listp (car-safe groups)))
991 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
992 (gnus-summary-kill-process-mark)
993 (let ((articles gnus-newsgroup-articles)
994 (backend-supports-deletions
995 (gnus-check-backend-function
996 'request-move-article gnus-newsgroup-name))
997 article tomove deletep)
998 (dolist (article articles)
999 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
1000 (gnus-summary-mark-article article gnus-expirable-mark)
1001 (push article tomove)))
1003 ;; now do the actual copies
1004 (dolist (group groups)
1007 (dolist (article tomove)
1008 (gnus-summary-set-process-mark article))
1010 (if (or (not backend-supports-deletions)
1011 (> (length groups) 1))
1013 (gnus-summary-copy-article nil group)
1015 (gnus-summary-move-article nil group)))))
1017 ;; now delete the articles, if there was a copy done, and the
1018 ;; backend allows it
1019 (when (and deletep backend-supports-deletions)
1020 (dolist (article tomove)
1021 (gnus-summary-set-process-mark article))
1023 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1024 (gnus-summary-delete-article nil))))
1026 (gnus-summary-yank-process-mark)
1029 (defun spam-ham-copy-or-move-routine (copy groups)
1030 (gnus-summary-kill-process-mark)
1031 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
1032 (backend-supports-deletions
1033 (gnus-check-backend-function
1034 'request-move-article gnus-newsgroup-name))
1035 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1036 article mark deletep respool)
1038 (when (member 'respool groups)
1039 (setq respool t) ; boolean for later
1040 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1042 ;; now do the actual move
1043 (dolist (group groups)
1044 (when (and todo (stringp group))
1045 (dolist (article todo)
1046 (when spam-mark-ham-unread-before-move-from-spam-group
1047 (gnus-summary-mark-article article gnus-unread-mark))
1048 (gnus-summary-set-process-mark article))
1050 (if respool ; respooling is with a "fake" group
1051 (let ((spam-split-disabled
1052 (or spam-split-disabled
1053 spam-disable-spam-split-during-ham-respool)))
1054 (gnus-summary-respool-article nil respool-method))
1055 (if (or (not backend-supports-deletions) ; else, we are not respooling
1056 (> (length groups) 1))
1057 (progn ; if copying, copy and set deletep
1058 (gnus-summary-copy-article nil group)
1060 (gnus-summary-move-article nil group))))) ; else move articles
1062 ;; now delete the articles, unless a) copy is t, and there was a copy done
1063 ;; b) a move was done to a single group
1064 ;; c) backend-supports-deletions is nil
1066 (when (and deletep backend-supports-deletions)
1067 (dolist (article todo)
1068 (gnus-summary-set-process-mark article))
1070 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1071 (gnus-summary-delete-article nil)))))
1073 (gnus-summary-yank-process-mark)
1076 (defun spam-ham-copy-routine (&rest groups)
1077 (if (and (car-safe groups) (listp (car-safe groups)))
1078 (apply 'spam-ham-copy-routine (car groups))
1079 (spam-ham-copy-or-move-routine t groups)))
1081 (defun spam-ham-move-routine (&rest groups)
1082 (if (and (car-safe groups) (listp (car-safe groups)))
1083 (apply 'spam-ham-move-routine (car groups))
1084 (spam-ham-copy-or-move-routine nil groups)))
1086 (defun spam-get-article-as-string (article)
1087 (when (numberp article)
1089 (gnus-request-article-this-buffer
1091 gnus-newsgroup-name)
1095 ;; (defun spam-get-article-as-filename (article)
1096 ;; (let ((article-filename))
1097 ;; (when (numberp article)
1098 ;; (nnml-possibly-change-directory
1099 ;; (gnus-group-real-name gnus-newsgroup-name))
1100 ;; (setq article-filename (expand-file-name
1101 ;; (int-to-string article) nnml-current-directory)))
1102 ;; (if (file-exists-p article-filename)
1106 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1107 "Fetch a field quickly, using the internal gnus-data-list function"
1108 (when (numberp article)
1109 (let* ((data-header (or prepared-data-header
1110 (spam-fetch-article-header article))))
1111 (if (arrayp data-header)
1113 ((equal field 'from)
1114 (mail-header-from data-header))
1115 ((equal field 'message-id)
1116 (mail-header-message-id data-header))
1117 ((equal field 'subject)
1118 (mail-header-subject data-header))
1119 ((equal field 'references)
1120 (mail-header-references data-header))
1121 ((equal field 'date)
1122 (mail-header-date data-header))
1123 ((equal field 'xref)
1124 (mail-header-xref data-header))
1125 ((equal field 'extra)
1126 (mail-header-extra data-header))
1129 (gnus-message 6 "Article %d has a nil data header" article)))))
1131 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1132 (spam-fetch-field-fast article 'from prepared-data-header))
1134 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1135 (spam-fetch-field-fast article 'subject prepared-data-header))
1137 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1138 (spam-fetch-field-fast article 'message-id prepared-data-header))
1140 (defun spam-generate-fake-headers (article)
1141 (let ((dh (spam-fetch-article-header article)))
1145 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1146 "Date: %s\nReferences: %s\nXref: %s\n")
1147 (spam-fetch-field-fast article 'from dh)
1148 (spam-fetch-field-fast article 'subject dh)
1149 (spam-fetch-field-fast article 'message-id dh)
1150 (spam-fetch-field-fast article 'date dh)
1151 (spam-fetch-field-fast article 'references dh)
1152 (spam-fetch-field-fast article 'xref dh))
1153 (when (spam-fetch-field-fast article 'extra dh)
1154 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1157 "spam-generate-fake-headers: article %d didn't have a valid header"
1160 (defun spam-fetch-article-header (article)
1162 (set-buffer gnus-summary-buffer)
1163 (gnus-read-header article)
1164 (nth 3 (assq article gnus-newsgroup-data))))
1167 ;;;; Spam determination.
1169 (defvar spam-list-of-checks
1170 '((spam-use-blacklist . spam-check-blacklist)
1171 (spam-use-regex-headers . spam-check-regex-headers)
1172 (spam-use-gmane-xref . spam-check-gmane-xref)
1173 (spam-use-regex-body . spam-check-regex-body)
1174 (spam-use-whitelist . spam-check-whitelist)
1175 (spam-use-BBDB . spam-check-BBDB)
1176 (spam-use-BBDB-exclusive . spam-check-BBDB)
1177 (spam-use-ifile . spam-check-ifile)
1178 (spam-use-spamoracle . spam-check-spamoracle)
1179 (spam-use-stat . spam-check-stat)
1180 (spam-use-blackholes . spam-check-blackholes)
1181 (spam-use-hashcash . spam-check-hashcash)
1182 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1183 (spam-use-spamassassin . spam-check-spamassassin)
1184 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1185 (spam-use-bogofilter . spam-check-bogofilter)
1186 (spam-use-bsfilter-headers . spam-check-bsfilter-headers)
1187 (spam-use-bsfilter . spam-check-bsfilter)
1188 (spam-use-crm114 . spam-check-crm114))
1189 "The spam-list-of-checks list contains pairs associating a
1190 parameter variable with a spam checking function. If the
1191 parameter variable is true, then the checking function is called,
1192 and its value decides what happens. Each individual check may
1193 return nil, t, or a mailgroup name. The value nil means that the
1194 check does not yield a decision, and so, that further checks are
1195 needed. The value t means that the message is definitely not
1196 spam, and that further spam checks should be inhibited.
1197 Otherwise, a mailgroup name or the symbol 'spam (depending on
1198 spam-split-symbolic-return) is returned where the mail should go,
1199 and further checks are also inhibited. The usual mailgroup name
1200 is the value of `spam-split-group', meaning that the message is
1201 definitely a spam.")
1203 (defvar spam-list-of-statistical-checks
1210 spam-use-spamassassin
1213 "The spam-list-of-statistical-checks list contains all the mail
1214 splitters that need to have the full message body available.
1215 Note that you should fetch extra headers if you don't like this,
1216 e.g. fetch the 'Received' header for spam-use-blackholes.")
1218 (defun spam-split (&rest specific-checks)
1219 "Split this message into the `spam' group if it is spam.
1220 This function can be used as an entry in the variable `nnmail-split-fancy',
1221 for example like this: (: spam-split). It can take checks as
1222 parameters. A string as a parameter will set the
1223 spam-split-group to that string.
1225 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1227 (setq spam-split-last-successful-check nil)
1228 (unless spam-split-disabled
1229 (let ((spam-split-group-choice spam-split-group))
1230 (dolist (check specific-checks)
1231 (when (stringp check)
1232 (setq spam-split-group-choice check)
1233 (setq specific-checks (delq check specific-checks))))
1235 (let ((spam-split-group spam-split-group-choice))
1238 (dolist (check spam-list-of-statistical-checks)
1239 (when (and (symbolp check)
1240 (or (symbol-value check)
1241 (memq check specific-checks)))
1243 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1244 (symbol-name check))
1246 ;; (progn (widen) (debug (buffer-string)))
1247 (let ((list-of-checks spam-list-of-checks)
1249 (while (and list-of-checks (not decision))
1250 (let ((pair (pop list-of-checks)))
1252 ;; either, given specific checks, this is one of them
1253 (and specific-checks (memq (car pair) specific-checks))
1254 ;; or, given no specific checks, spam-use-CHECK is set
1255 (and (null specific-checks) (symbol-value (car pair))))
1256 (gnus-message 6 "spam-split: calling the %s function"
1257 (symbol-name (cdr pair)))
1258 (setq decision (funcall (cdr pair)))
1259 ;; if we got a decision at all, save the current check
1261 (setq spam-split-last-successful-check (car pair)))
1263 (when (eq decision 'spam)
1264 (unless spam-split-symbolic-return
1267 (format "spam-split got %s but %s is nil"
1268 (symbol-name decision)
1269 (symbol-name spam-split-symbolic-return))))))))
1271 (if spam-split-symbolic-return-positive 'ham nil)
1274 (defun spam-find-spam ()
1275 "This function will detect spam in the current newsgroup using spam-split."
1278 (let* ((group gnus-newsgroup-name)
1279 (autodetect (gnus-parameter-spam-autodetect group))
1280 (methods (gnus-parameter-spam-autodetect-methods group))
1281 (first-method (nth 0 methods))
1282 (articles (if spam-autodetect-recheck-messages
1283 gnus-newsgroup-articles
1284 gnus-newsgroup-unseen))
1285 article-cannot-be-faked)
1287 (dolist (check spam-list-of-statistical-checks)
1288 (when (and (symbolp check)
1289 (memq check methods))
1290 (setq article-cannot-be-faked t)
1293 (when (memq 'default methods)
1294 (setq article-cannot-be-faked t))
1296 (when (and autodetect
1297 (not (equal first-method 'none)))
1300 (let ((id (spam-fetch-field-message-id-fast article))
1301 (subject (spam-fetch-field-subject-fast article))
1302 (sender (spam-fetch-field-from-fast article))
1306 (gnus-message 6 "Article %d has no message ID!" article))
1308 (when (and id spam-log-to-registry)
1309 (setq registry-lookup (spam-log-registration-type id 'incoming))
1310 (when registry-lookup
1313 "spam-find-spam: message %s was already registered incoming"
1316 (let* ((spam-split-symbolic-return t)
1317 (spam-split-symbolic-return-positive t)
1318 (fake-headers (spam-generate-fake-headers article))
1322 (if article-cannot-be-faked
1323 (gnus-request-article-this-buffer
1326 ;; else, we fake the article
1327 (when fake-headers (insert fake-headers)))
1328 (if (or (null first-method)
1329 (equal first-method 'default))
1331 (apply 'spam-split methods))))))
1332 (if (equal split-return 'spam)
1333 (gnus-summary-mark-article article gnus-spam-mark))
1335 (when (and id split-return spam-log-to-registry)
1336 (when (zerop (gnus-registry-group-count id))
1337 (gnus-registry-add-group
1338 id group subject sender))
1340 (unless registry-lookup
1341 (spam-log-processing-to-registry
1345 spam-split-last-successful-check
1349 (defvar spam-registration-functions
1350 ;; first the ham register, second the spam register function
1351 ;; third the ham unregister, fourth the spam unregister function
1352 '((spam-use-blacklist nil
1353 spam-blacklist-register-routine
1355 spam-blacklist-unregister-routine)
1356 (spam-use-whitelist spam-whitelist-register-routine
1358 spam-whitelist-unregister-routine
1360 (spam-use-ham-copy nil
1364 (spam-use-BBDB spam-BBDB-register-routine
1366 spam-BBDB-unregister-routine
1368 (spam-use-ifile spam-ifile-register-ham-routine
1369 spam-ifile-register-spam-routine
1370 spam-ifile-unregister-ham-routine
1371 spam-ifile-unregister-spam-routine)
1372 (spam-use-spamoracle spam-spamoracle-learn-ham
1373 spam-spamoracle-learn-spam
1374 spam-spamoracle-unlearn-ham
1375 spam-spamoracle-unlearn-spam)
1376 (spam-use-stat spam-stat-register-ham-routine
1377 spam-stat-register-spam-routine
1378 spam-stat-unregister-ham-routine
1379 spam-stat-unregister-spam-routine)
1380 ;; note that spam-use-gmane and spam-use-resend are not legitimate checks
1382 spam-report-gmane-register-routine
1383 ;; does Gmane support unregistration?
1386 (spam-use-resend nil
1387 spam-report-resend-register-routine
1390 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1391 spam-spamassassin-register-spam-routine
1392 spam-spamassassin-unregister-ham-routine
1393 spam-spamassassin-unregister-spam-routine)
1394 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1395 spam-bogofilter-register-spam-routine
1396 spam-bogofilter-unregister-ham-routine
1397 spam-bogofilter-unregister-spam-routine)
1398 (spam-use-bsfilter spam-bsfilter-register-ham-routine
1399 spam-bsfilter-register-spam-routine
1400 spam-bsfilter-unregister-ham-routine
1401 spam-bsfilter-unregister-spam-routine)
1402 (spam-use-crm114 spam-crm114-register-ham-routine
1403 spam-crm114-register-spam-routine
1404 ;; does CRM114 Mailfilter support unregistration?
1407 "The spam-registration-functions list contains pairs
1408 associating a parameter variable with the ham and spam
1409 registration functions, and the ham and spam unregistration
1412 (defun spam-classification-valid-p (classification)
1413 (or (eq classification 'spam)
1414 (eq classification 'ham)))
1416 (defun spam-process-type-valid-p (process-type)
1417 (or (eq process-type 'incoming)
1418 (eq process-type 'process)))
1420 (defun spam-registration-check-valid-p (check)
1421 (assoc check spam-registration-functions))
1423 (defun spam-unregistration-check-valid-p (check)
1424 (assoc check spam-registration-functions))
1426 (defun spam-registration-function (classification check)
1427 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1428 (if (eq classification 'spam)
1432 (defun spam-unregistration-function (classification check)
1433 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1434 (if (eq classification 'spam)
1438 (defun spam-list-articles (articles classification)
1439 (let ((mark-check (if (eq classification 'spam)
1440 'spam-group-spam-mark-p
1441 'spam-group-ham-mark-p))
1442 alist mark-cache-yes mark-cache-no)
1443 (dolist (article articles)
1444 (let ((mark (gnus-summary-article-mark article)))
1445 (unless (or (memq mark mark-cache-yes)
1446 (memq mark mark-cache-no))
1447 (if (funcall mark-check
1450 (push mark mark-cache-yes)
1451 (push mark mark-cache-no)))
1452 (when (memq mark mark-cache-yes)
1453 (push article alist))))
1456 (defun spam-register-routine (classification
1458 &optional unregister
1460 (when (and (spam-classification-valid-p classification)
1461 (spam-registration-check-valid-p check))
1462 (let* ((register-function
1463 (spam-registration-function classification check))
1464 (unregister-function
1465 (spam-unregistration-function classification check))
1466 (run-function (if unregister
1469 (log-function (if unregister
1470 'spam-log-undo-registration
1471 'spam-log-processing-to-registry))
1475 ;; make list of articles, using specific-articles if given
1476 (setq articles (or specific-articles
1478 gnus-newsgroup-articles
1481 (when (> (length articles) 0)
1482 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1483 (if unregister "Unregistering" "Registering")
1485 (if specific-articles "specific" "")
1486 (symbol-name classification)
1487 (symbol-name check))
1488 (funcall run-function articles)
1489 ;; now log all the registrations (or undo them, depending on unregister)
1490 (dolist (article articles)
1491 (funcall log-function
1492 (spam-fetch-field-message-id-fast article)
1496 gnus-newsgroup-name)))))))
1498 ;;; log a ham- or spam-processor invocation to the registry
1499 (defun spam-log-processing-to-registry (id type classification check group)
1500 (when spam-log-to-registry
1501 (if (and (stringp id)
1503 (spam-process-type-valid-p type)
1504 (spam-classification-valid-p classification)
1505 (spam-registration-check-valid-p check))
1506 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1507 (cell (list classification check group)))
1508 (push cell cell-list)
1509 (gnus-registry-store-extra-entry
1516 (format "%s call with bad ID, type, classification, spam-check, or group"
1517 "spam-log-processing-to-registry")))))
1519 ;;; check if a ham- or spam-processor registration has been done
1520 (defun spam-log-registered-p (id type)
1521 (when spam-log-to-registry
1522 (if (and (stringp id)
1523 (spam-process-type-valid-p type))
1524 (cdr-safe (gnus-registry-fetch-extra id type))
1528 (format "%s called with bad ID, type, classification, or spam-check"
1529 "spam-log-registered-p"))
1532 ;;; check what a ham- or spam-processor registration says
1533 ;;; returns nil if conflicting registrations are found
1534 (defun spam-log-registration-type (id type)
1537 (dolist (reg (spam-log-registered-p id type))
1538 (let ((classification (nth 0 reg)))
1539 (when (spam-classification-valid-p classification)
1541 (not (eq classification decision)))
1542 (setq count (+ 1 count)))
1543 (setq decision classification))))
1549 ;;; check if a ham- or spam-processor registration needs to be undone
1550 (defun spam-log-unregistration-needed-p (id type classification check)
1551 (when spam-log-to-registry
1552 (if (and (stringp id)
1553 (spam-process-type-valid-p type)
1554 (spam-classification-valid-p classification)
1555 (spam-registration-check-valid-p check))
1556 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1558 (dolist (cell cell-list)
1560 (when (and (eq classification (nth 0 cell))
1561 (eq check (nth 1 cell)))
1567 (format "%s called with bad ID, type, classification, or spam-check"
1568 "spam-log-unregistration-needed-p"))
1572 ;;; undo a ham- or spam-processor registration (the group is not used)
1573 (defun spam-log-undo-registration (id type classification check &optional group)
1574 (when (and spam-log-to-registry
1575 (spam-log-unregistration-needed-p id type classification check))
1576 (if (and (stringp id)
1577 (spam-process-type-valid-p type)
1578 (spam-classification-valid-p classification)
1579 (spam-registration-check-valid-p check))
1580 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1581 new-cell-list found)
1582 (dolist (cell cell-list)
1583 (unless (and (eq classification (nth 0 cell))
1584 (eq check (nth 1 cell)))
1585 (push cell new-cell-list)))
1586 (gnus-registry-store-extra-entry
1591 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1592 "spam-log-undo-registration"))
1595 ;;; set up IMAP widening if it's necessary
1596 (defun spam-setup-widening ()
1597 (dolist (check spam-list-of-statistical-checks)
1598 (when (symbol-value check)
1599 (setq nnimap-split-download-body-default t))))
1603 (defun spam-check-gmane-xref ()
1605 (message-fetch-field "Xref")
1606 (message-fetch-field "Newsgroups")))
1607 (spam-split-group (if spam-split-symbolic-return
1610 (when header ; return nil when no header
1611 (when (string-match spam-gmane-xref-spam-group
1613 spam-split-group))))
1618 (defun spam-check-regex-body ()
1619 (let ((spam-regex-headers-ham spam-regex-body-ham)
1620 (spam-regex-headers-spam spam-regex-body-spam))
1621 (spam-check-regex-headers t)))
1626 (defun spam-check-regex-headers (&optional body)
1627 (let ((type (if body "body" "header"))
1628 (spam-split-group (if spam-split-symbolic-return
1632 (dolist (h-regex spam-regex-headers-ham)
1634 (goto-char (point-min))
1635 (when (re-search-forward h-regex nil t)
1636 (message "Ham regex %s search positive." type)
1638 (dolist (s-regex spam-regex-headers-spam)
1640 (goto-char (point-min))
1641 (when (re-search-forward s-regex nil t)
1642 (message "Spam regex %s search positive." type)
1644 (setq ret spam-split-group))))
1650 (defun spam-reverse-ip-string (ip)
1652 (mapconcat 'identity
1653 (nreverse (split-string ip "\\."))
1656 (defun spam-check-blackholes ()
1657 "Check the Received headers for blackholed relays."
1658 (let ((headers (message-fetch-field "received"))
1659 (spam-split-group (if spam-split-symbolic-return
1666 (goto-char (point-min))
1667 (gnus-message 6 "Checking headers for relay addresses")
1668 (while (re-search-forward
1669 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1670 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1671 (push (spam-reverse-ip-string (match-string 1))
1673 (dolist (server spam-blackhole-servers)
1675 (unless (and spam-blackhole-good-server-regex
1676 ;; match the good-server-regex against the reversed (again) IP string
1678 spam-blackhole-good-server-regex
1679 (spam-reverse-ip-string ip)))
1681 (let ((query-string (concat ip "." server)))
1683 (let ((query-result (query-dig query-string)))
1685 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1687 (push (list ip server query-result)
1689 ;; else, if not using dig.el
1690 (when (query-dns query-string)
1691 (gnus-message 6 "positive blackhole check")
1692 (push (list ip server (query-dns query-string 'TXT))
1703 (defun spam-check-hashcash ()
1704 "Check the headers for hashcash payments."
1705 (mail-check-payment))) ;mail-check-payment returns a boolean
1708 (defalias 'mail-check-payment 'ignore)
1709 (defalias 'spam-check-hashcash 'ignore))))
1713 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1714 ;;; <sacha@giotto.sj.ru>
1716 ;; all this is done inside a condition-case to trap errors
1723 ;; when the BBDB changes, we want to clear out our cache
1724 (defun spam-clear-cache-BBDB (&rest immaterial)
1725 (spam-clear-cache 'spam-use-BBDB))
1727 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1729 (defun spam-enter-ham-BBDB (addresses &optional remove)
1730 "Enter an address into the BBDB; implies ham (non-spam) sender"
1731 (dolist (from addresses)
1732 (when (stringp from)
1733 (let* ((parsed-address (gnus-extract-address-components from))
1734 (name (or (nth 0 parsed-address) "Ham Sender"))
1735 (remove-function (if remove
1736 'bbdb-delete-record-internal
1738 (net-address (nth 1 parsed-address))
1739 (record (and net-address
1740 (bbdb-search-simple nil net-address))))
1742 (gnus-message 6 "%s address %s %s BBDB"
1743 (if remove "Deleting" "Adding")
1745 (if remove "from" "to"))
1747 (funcall remove-function record)
1748 (bbdb-create-internal name nil net-address nil nil
1749 "ham sender added by spam.el")))))))
1751 (defun spam-BBDB-register-routine (articles &optional unregister)
1753 (dolist (article articles)
1754 (when (stringp (spam-fetch-field-from-fast article))
1755 (push (spam-fetch-field-from-fast article) addresses)))
1756 ;; now do the register/unregister action
1757 (spam-enter-ham-BBDB addresses unregister)))
1759 (defun spam-BBDB-unregister-routine (articles)
1760 (spam-BBDB-register-routine articles t))
1762 (defun spam-check-BBDB ()
1763 "Mail from people in the BBDB is classified as ham or non-spam"
1764 (let ((who (message-fetch-field "from"))
1765 (spam-split-group (if spam-split-symbolic-return
1768 bbdb-cache bbdb-hashtable)
1769 (when spam-cache-lookups
1770 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1773 ;; this is the expanded (bbdb-hashtable) macro
1774 ;; without the debugging support
1775 (with-current-buffer (bbdb-buffer)
1777 (save-window-excursion
1778 (bbdb-records nil t)
1780 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1782 (setq who (nth 1 (gnus-extract-address-components who)))
1784 (if spam-cache-lookups
1786 (intern-soft who bbdb-cache))
1787 (bbdb-search-simple nil who))
1789 (if spam-use-BBDB-exclusive
1794 (defalias 'bbdb-search-simple 'ignore)
1795 (defalias 'bbdb-records 'ignore)
1796 (defalias 'bbdb-buffer 'ignore)
1797 (defalias 'spam-check-BBDB 'ignore)
1798 (defalias 'spam-BBDB-register-routine 'ignore)
1799 (defalias 'spam-enter-ham-BBDB 'ignore)
1800 (defalias 'bbdb-create-internal 'ignore)
1801 (defalias 'bbdb-delete-record-internal 'ignore)
1802 (defalias 'bbdb-records 'ignore))))
1807 ;;; check the ifile backend; return nil if the mail was NOT classified
1810 (defun spam-get-ifile-database-parameter ()
1811 "Get the command-line parameter for ifile's database from
1812 spam-ifile-database-path."
1813 (if spam-ifile-database-path
1814 (format "--db-file=%s" spam-ifile-database-path)
1817 (defun spam-check-ifile ()
1818 "Check the ifile backend for the classification of this message."
1819 (let ((article-buffer-name (buffer-name))
1820 (spam-split-group (if spam-split-symbolic-return
1825 (let ((temp-buffer-name (buffer-name))
1826 (db-param (spam-get-ifile-database-parameter)))
1828 (set-buffer article-buffer-name)
1829 (apply 'call-process-region
1830 (point-min) (point-max) spam-ifile-path
1831 nil temp-buffer-name nil "-c"
1832 (if db-param `(,db-param "-q") `("-q"))))
1833 ;; check the return now (we're back in the temp buffer)
1834 (goto-char (point-min))
1836 (setq category (buffer-substring (point) (point-at-eol))))
1837 (when (not (zerop (length category))) ; we need a category here
1838 (if spam-ifile-all-categories
1839 (setq return category)
1840 ;; else, if spam-ifile-all-categories is not set...
1841 (when (string-equal spam-ifile-spam-category category)
1842 (setq return spam-split-group)))))) ; note return is nil otherwise
1845 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1846 "Register an article, given as a string, with a category.
1847 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1848 (let ((category (or category gnus-newsgroup-name))
1849 (add-or-delete-option (if unregister "-d" "-i"))
1850 (db (spam-get-ifile-database-parameter))
1853 (dolist (article articles)
1854 (let ((article-string (spam-get-article-as-string article)))
1855 (when (stringp article-string)
1856 (insert article-string))))
1857 (apply 'call-process-region
1858 (point-min) (point-max) spam-ifile-path
1860 add-or-delete-option category
1861 (if db `(,db "-h") `("-h"))))))
1863 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1864 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1866 (defun spam-ifile-unregister-spam-routine (articles)
1867 (spam-ifile-register-spam-routine articles t))
1869 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1870 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1872 (defun spam-ifile-unregister-ham-routine (articles)
1873 (spam-ifile-register-ham-routine articles t))
1880 (let ((spam-stat-install-hooks nil))
1881 (require 'spam-stat))
1883 (defun spam-check-stat ()
1884 "Check the spam-stat backend for the classification of this message"
1885 (let ((spam-split-group (if spam-split-symbolic-return
1888 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1889 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1891 (spam-stat-split-fancy)))
1893 (defun spam-stat-register-spam-routine (articles &optional unregister)
1894 (dolist (article articles)
1895 (let ((article-string (spam-get-article-as-string article)))
1897 (insert article-string)
1899 (spam-stat-buffer-change-to-non-spam)
1900 (spam-stat-buffer-is-spam))))))
1902 (defun spam-stat-unregister-spam-routine (articles)
1903 (spam-stat-register-spam-routine articles t))
1905 (defun spam-stat-register-ham-routine (articles &optional unregister)
1906 (dolist (article articles)
1907 (let ((article-string (spam-get-article-as-string article)))
1909 (insert article-string)
1911 (spam-stat-buffer-change-to-spam)
1912 (spam-stat-buffer-is-non-spam))))))
1914 (defun spam-stat-unregister-ham-routine (articles)
1915 (spam-stat-register-ham-routine articles t))
1917 (defun spam-maybe-spam-stat-load ()
1918 (when spam-use-stat (spam-stat-load)))
1920 (defun spam-maybe-spam-stat-save ()
1921 (when spam-use-stat (spam-stat-save))))
1924 (defalias 'spam-stat-load 'ignore)
1925 (defalias 'spam-stat-save 'ignore)
1926 (defalias 'spam-maybe-spam-stat-load 'ignore)
1927 (defalias 'spam-maybe-spam-stat-save 'ignore)
1928 (defalias 'spam-stat-register-ham-routine 'ignore)
1929 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1930 (defalias 'spam-stat-register-spam-routine 'ignore)
1931 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1932 (defalias 'spam-stat-buffer-is-spam 'ignore)
1933 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1934 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1935 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1936 (defalias 'spam-stat-split-fancy 'ignore)
1937 (defalias 'spam-check-stat 'ignore))))
1941 ;;;; Blacklists and whitelists.
1943 (defvar spam-whitelist-cache nil)
1944 (defvar spam-blacklist-cache nil)
1946 (defun spam-kill-whole-line ()
1948 (let ((kill-whole-line t))
1951 ;;; address can be a list, too
1952 (defun spam-enter-whitelist (address &optional remove)
1953 "Enter ADDRESS (list or single) into the whitelist.
1954 With a non-nil REMOVE, remove them."
1955 (interactive "sAddress: ")
1956 (spam-enter-list address spam-whitelist remove)
1957 (setq spam-whitelist-cache nil)
1958 (spam-clear-cache 'spam-use-whitelist))
1960 ;;; address can be a list, too
1961 (defun spam-enter-blacklist (address &optional remove)
1962 "Enter ADDRESS (list or single) into the blacklist.
1963 With a non-nil REMOVE, remove them."
1964 (interactive "sAddress: ")
1965 (spam-enter-list address spam-blacklist remove)
1966 (setq spam-blacklist-cache nil)
1967 (spam-clear-cache 'spam-use-whitelist))
1969 (defun spam-enter-list (addresses file &optional remove)
1970 "Enter ADDRESSES into the given FILE.
1971 Either the whitelist or the blacklist files can be used. With
1972 REMOVE not nil, remove the ADDRESSES."
1973 (if (stringp addresses)
1974 (spam-enter-list (list addresses) file remove)
1975 ;; else, we have a list of addresses here
1976 (unless (file-exists-p (file-name-directory file))
1977 (make-directory (file-name-directory file) t))
1980 (find-file-noselect file))
1981 (dolist (a addresses)
1983 (goto-char (point-min))
1984 (if (re-search-forward (regexp-quote a) nil t)
1985 ;; found the address
1987 (spam-kill-whole-line))
1988 ;; else, the address was not found
1990 (goto-char (point-max))
1996 (defun spam-filelist-build-cache (type)
1997 (let ((cache (if (eq type 'spam-use-blacklist)
1998 spam-blacklist-cache
1999 spam-whitelist-cache))
2001 (unless (gethash type spam-caches)
2003 (let ((address (pop cache)))
2004 (unless (zerop (length address)) ; 0 for a nil address too
2005 (setq address (regexp-quote address))
2006 ;; fix regexp-quote's treatment of user-intended regexes
2007 (while (string-match "\\\\\\*" address)
2008 (setq address (replace-match ".*" t t address))))
2009 (push address parsed-cache)))
2010 (puthash type parsed-cache spam-caches))))
2012 (defun spam-filelist-check-cache (type from)
2013 (when (stringp from)
2014 (spam-filelist-build-cache type)
2016 (dolist (address (gethash type spam-caches))
2017 (when (and address (string-match address from))
2022 ;;; returns t if the sender is in the whitelist, nil or
2023 ;;; spam-split-group otherwise
2024 (defun spam-check-whitelist ()
2025 ;; FIXME! Should it detect when file timestamps change?
2026 (let ((spam-split-group (if spam-split-symbolic-return
2029 (unless spam-whitelist-cache
2030 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2031 (if (spam-from-listed-p 'spam-use-whitelist)
2033 (if spam-use-whitelist-exclusive
2037 (defun spam-check-blacklist ()
2038 ;; FIXME! Should it detect when file timestamps change?
2039 (let ((spam-split-group (if spam-split-symbolic-return
2042 (unless spam-blacklist-cache
2043 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2044 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
2046 (defun spam-parse-list (file)
2047 (when (file-readable-p file)
2048 (let (contents address)
2050 (insert-file-contents file)
2052 (setq address (buffer-substring (point) (point-at-eol)))
2054 ;; insert the e-mail address if detected, otherwise the raw data
2055 (unless (zerop (length address))
2056 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2057 (push (or pure-address address) contents)))))
2058 (nreverse contents))))
2060 (defun spam-from-listed-p (type)
2061 (let ((from (message-fetch-field "from"))
2063 (spam-filelist-check-cache type from)))
2065 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2066 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2067 (declassification (if blacklist 'ham 'spam))
2069 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2071 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2072 from addresses unregister-list article-unregister-list)
2073 (dolist (article articles)
2074 (let ((from (spam-fetch-field-from-fast article))
2075 (id (spam-fetch-field-message-id-fast article))
2077 (when (stringp from)
2078 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2079 (when (and (not sender-ignored)
2080 (stringp ignore-regex)
2081 (string-match ignore-regex from))
2082 (setq sender-ignored t)))
2083 ;; remember the messages we need to unregister, unless remove is set
2086 (spam-log-unregistration-needed-p
2087 id 'process declassification de-symbol))
2088 (push article article-unregister-list)
2089 (push from unregister-list))
2090 (unless sender-ignored
2091 (push from addresses)))))
2094 (funcall enter-function addresses t) ; unregister all these addresses
2095 ;; else, register normally and unregister what we need to
2096 (funcall remove-function unregister-list t)
2097 (dolist (article article-unregister-list)
2098 (spam-log-undo-registration
2099 (spam-fetch-field-message-id-fast article)
2103 (funcall enter-function addresses nil))))
2105 (defun spam-blacklist-unregister-routine (articles)
2106 (spam-blacklist-register-routine articles t))
2108 (defun spam-blacklist-register-routine (articles &optional unregister)
2109 (spam-filelist-register-routine articles t unregister))
2111 (defun spam-whitelist-unregister-routine (articles)
2112 (spam-whitelist-register-routine articles t))
2114 (defun spam-whitelist-register-routine (articles &optional unregister)
2115 (spam-filelist-register-routine articles nil unregister))
2118 ;;;; Spam-report glue
2119 (defun spam-report-gmane-register-routine (articles)
2121 (apply 'spam-report-gmane articles)))
2123 (defun spam-report-resend-register-routine (articles)
2124 (let* ((resend-to-gp (gnus-parameter-spam-resend-to gnus-newsgroup-name))
2125 (spam-report-resend-to (or (car-safe resend-to-gp)
2126 spam-report-resend-to)))
2127 (spam-report-resend articles)))
2131 (defun spam-check-bogofilter-headers (&optional score)
2132 (let ((header (message-fetch-field spam-bogofilter-header))
2133 (spam-split-group (if spam-split-symbolic-return
2136 (when header ; return nil when no header
2137 (if score ; scoring mode
2138 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2139 (match-string 1 header)
2141 ;; spam detection mode
2142 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2144 spam-split-group)))))
2146 ;; return something sensible if the score can't be determined
2147 (defun spam-bogofilter-score (&optional recheck)
2148 "Get the Bogofilter spamicity score"
2150 (save-window-excursion
2151 (gnus-summary-show-article t)
2152 (set-buffer gnus-article-buffer)
2153 (let ((score (or (unless recheck
2154 (spam-check-bogofilter-headers t))
2155 (spam-check-bogofilter t))))
2156 (gnus-summary-show-article)
2157 (message "Spamicity score %s" score)
2160 (defun spam-check-bogofilter (&optional score)
2161 "Check the Bogofilter backend for the classification of this message"
2162 (let ((article-buffer-name (buffer-name))
2163 (db spam-bogofilter-database-directory)
2166 (let ((temp-buffer-name (buffer-name)))
2168 (set-buffer article-buffer-name)
2169 (apply 'call-process-region
2170 (point-min) (point-max)
2171 spam-bogofilter-path
2172 nil temp-buffer-name nil
2173 (if db `("-d" ,db "-v") `("-v"))))
2174 (setq return (spam-check-bogofilter-headers score))))
2177 (defun spam-bogofilter-register-with-bogofilter (articles
2179 &optional unregister)
2180 "Register an article, given as a string, as spam or non-spam."
2181 (dolist (article articles)
2182 (let ((article-string (spam-get-article-as-string article))
2183 (db spam-bogofilter-database-directory)
2184 (switch (if unregister
2186 spam-bogofilter-spam-strong-switch
2187 spam-bogofilter-ham-strong-switch)
2189 spam-bogofilter-spam-switch
2190 spam-bogofilter-ham-switch))))
2191 (when (stringp article-string)
2193 (insert article-string)
2195 (apply 'call-process-region
2196 (point-min) (point-max)
2197 spam-bogofilter-path
2199 (if db `("-d" ,db "-v") `("-v"))))))))
2201 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2202 (spam-bogofilter-register-with-bogofilter articles t unregister))
2204 (defun spam-bogofilter-unregister-spam-routine (articles)
2205 (spam-bogofilter-register-spam-routine articles t))
2207 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2208 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2210 (defun spam-bogofilter-unregister-ham-routine (articles)
2211 (spam-bogofilter-register-ham-routine articles t))
2216 (defun spam-check-spamoracle ()
2217 "Run spamoracle on an article to determine whether it's spam."
2218 (let ((article-buffer-name (buffer-name))
2219 (spam-split-group (if spam-split-symbolic-return
2223 (let ((temp-buffer-name (buffer-name)))
2225 (set-buffer article-buffer-name)
2227 (apply 'call-process-region
2228 (point-min) (point-max)
2229 spam-spamoracle-binary
2230 nil temp-buffer-name nil
2231 (if spam-spamoracle-database
2232 `("-f" ,spam-spamoracle-database "mark")
2236 (set-buffer temp-buffer-name)
2237 (goto-char (point-min))
2238 (when (re-search-forward "^X-Spam: yes;" nil t)
2240 (error "Error running spamoracle: %s" status))))))))
2242 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2243 "Run spamoracle in training mode."
2245 (let ((temp-buffer-name (buffer-name)))
2247 (goto-char (point-min))
2248 (dolist (article articles)
2249 (insert (spam-get-article-as-string article)))
2250 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2254 (apply 'call-process-region
2255 (point-min) (point-max)
2256 spam-spamoracle-binary
2257 nil temp-buffer-name nil
2258 (if spam-spamoracle-database
2259 `("-f" ,spam-spamoracle-database
2262 (unless (eq 0 status)
2263 (error "Error running spamoracle: %s" status)))))))
2265 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2266 (spam-spamoracle-learn articles nil unregister))
2268 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2269 (spam-spamoracle-learn-ham articles t))
2271 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2272 (spam-spamoracle-learn articles t unregister))
2274 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2275 (spam-spamoracle-learn-spam articles t))
2279 ;;; based mostly on the bogofilter code
2280 (defun spam-check-spamassassin-headers (&optional score)
2281 "Check the SpamAssassin headers for the classification of this message."
2282 (if score ; scoring mode
2283 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2285 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2286 (match-string 1 header)
2288 ;; spam detection mode
2289 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2290 (spam-split-group (if spam-split-symbolic-return
2293 (when header ; return nil when no header
2294 (when (string-match spam-spamassassin-positive-spam-flag-header
2296 spam-split-group)))))
2298 (defun spam-check-spamassassin (&optional score)
2299 "Check the SpamAssassin backend for the classification of this message."
2300 (let ((article-buffer-name (buffer-name)))
2302 (let ((temp-buffer-name (buffer-name)))
2304 (set-buffer article-buffer-name)
2305 (apply 'call-process-region
2306 (point-min) (point-max) spam-spamassassin-path
2307 nil temp-buffer-name nil spam-spamassassin-arguments))
2308 ;; check the return now (we're back in the temp buffer)
2309 (goto-char (point-min))
2310 (spam-check-spamassassin-headers score)))))
2312 ;; return something sensible if the score can't be determined
2313 (defun spam-spamassassin-score (&optional recheck)
2314 "Get the SpamAssassin score"
2316 (save-window-excursion
2317 (gnus-summary-show-article t)
2318 (set-buffer gnus-article-buffer)
2319 (let ((score (or (unless recheck
2320 (spam-check-spamassassin-headers t))
2321 (spam-check-spamassassin t))))
2322 (gnus-summary-show-article)
2323 (message "SpamAssassin score %s" score)
2326 (defun spam-spamassassin-register-with-sa-learn (articles spam
2327 &optional unregister)
2328 "Register articles with spamassassin's sa-learn as spam or non-spam."
2330 (let ((action (if unregister spam-sa-learn-unregister-switch
2331 (if spam spam-sa-learn-spam-switch
2332 spam-sa-learn-ham-switch)))
2333 (summary-buffer-name (buffer-name)))
2335 ;; group the articles into mbox format
2336 (dolist (article articles)
2337 (let (article-string)
2339 (set-buffer summary-buffer-name)
2340 (setq article-string (spam-get-article-as-string article)))
2341 (when (stringp article-string)
2342 (insert "From \n") ; mbox separator (sa-learn only checks the
2343 ; first five chars, so we can get away with
2345 (insert article-string)
2347 ;; call sa-learn on all messages at the same time
2348 (apply 'call-process-region
2349 (point-min) (point-max)
2351 nil nil nil "--mbox"
2352 (if spam-sa-learn-rebuild
2354 `("--no-rebuild" ,action)))))))
2356 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2357 (spam-spamassassin-register-with-sa-learn articles t unregister))
2359 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2360 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2362 (defun spam-spamassassin-unregister-spam-routine (articles)
2363 (spam-spamassassin-register-with-sa-learn articles t t))
2365 (defun spam-spamassassin-unregister-ham-routine (articles)
2366 (spam-spamassassin-register-with-sa-learn articles nil t))
2370 ;;; based mostly on the bogofilter code
2371 (defun spam-check-bsfilter-headers (&optional score)
2373 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2375 (let ((header (nnmail-fetch-field spam-bsfilter-header))
2376 (spam-split-group (if spam-split-symbolic-return
2379 (when header ; return nil when no header
2380 (when (string-match "YES" header)
2381 spam-split-group)))))
2383 ;; return something sensible if the score can't be determined
2384 (defun spam-bsfilter-score (&optional recheck)
2385 "Get the Bsfilter spamicity score"
2387 (save-window-excursion
2388 (gnus-summary-show-article t)
2389 (set-buffer gnus-article-buffer)
2390 (let ((score (or (unless recheck
2391 (spam-check-bsfilter-headers t))
2392 (spam-check-bsfilter t))))
2393 (gnus-summary-show-article)
2394 (message "Spamicity score %s" score)
2397 (defun spam-check-bsfilter (&optional score)
2398 "Check the Bsfilter backend for the classification of this message"
2399 (let ((article-buffer-name (buffer-name))
2400 (dir spam-bsfilter-database-directory)
2403 (let ((temp-buffer-name (buffer-name)))
2405 (set-buffer article-buffer-name)
2406 (apply 'call-process-region
2407 (point-min) (point-max)
2409 nil temp-buffer-name nil
2412 "--insert-probability"
2414 (list "--homedir" dir))))
2415 (setq return (spam-check-bsfilter-headers score))))
2418 (defun spam-bsfilter-register-with-bsfilter (articles
2420 &optional unregister)
2421 "Register an article, given as a string, as spam or non-spam."
2422 (dolist (article articles)
2423 (let ((article-string (spam-get-article-as-string article))
2424 (switch (if unregister
2426 spam-bsfilter-spam-strong-switch
2427 spam-bsfilter-ham-strong-switch)
2429 spam-bsfilter-spam-switch
2430 spam-bsfilter-ham-switch))))
2431 (when (stringp article-string)
2433 (insert article-string)
2434 (apply 'call-process-region
2435 (point-min) (point-max)
2439 (when spam-bsfilter-database-directory
2441 spam-bsfilter-database-directory))))))))
2443 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2444 (spam-bsfilter-register-with-bsfilter articles t unregister))
2446 (defun spam-bsfilter-unregister-spam-routine (articles)
2447 (spam-bsfilter-register-spam-routine articles t))
2449 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2450 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2452 (defun spam-bsfilter-unregister-ham-routine (articles)
2453 (spam-bsfilter-register-ham-routine articles t))
2456 ;;;; CRM114 Mailfilter
2457 (defun spam-check-crm114-headers (&optional score)
2458 (let ((header (message-fetch-field spam-crm114-header))
2459 (spam-split-group (if spam-split-symbolic-return
2462 (when header ; return nil when no header
2463 (if score ; scoring mode
2464 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2465 (match-string 1 header)
2467 ;; spam detection mode
2468 (when (string-match spam-crm114-positive-spam-header
2470 spam-split-group)))))
2472 ;; return something sensible if the score can't be determined
2473 (defun spam-crm114-score ()
2474 "Get the CRM114 Mailfilter pR"
2476 (save-window-excursion
2477 (gnus-summary-show-article t)
2478 (set-buffer gnus-article-buffer)
2479 (let ((score (or (spam-check-crm114-headers t)
2480 (spam-check-crm114 t))))
2481 (gnus-summary-show-article)
2482 (message "pR: %s" score)
2485 (defun spam-check-crm114 (&optional score)
2486 "Check the CRM114 Mailfilter backend for the classification of this message"
2487 (let ((article-buffer-name (buffer-name))
2488 (db spam-crm114-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)
2497 nil temp-buffer-name nil
2498 (when db (list (concat "--fileprefix=" db)))))
2499 (setq return (spam-check-crm114-headers score))))
2502 (defun spam-crm114-register-with-crm114 (articles
2504 &optional unregister)
2505 "Register an article, given as a string, as spam or non-spam."
2506 (dolist (article articles)
2507 (let ((article-string (spam-get-article-as-string article))
2508 (db spam-crm114-database-directory)
2509 (switch (if unregister
2511 spam-crm114-spam-strong-switch
2512 spam-crm114-ham-strong-switch)
2514 spam-crm114-spam-switch
2515 spam-crm114-ham-switch))))
2516 (when (stringp article-string)
2518 (insert article-string)
2520 (apply 'call-process-region
2521 (point-min) (point-max)
2524 (when db (list switch (concat "--fileprefix=" db)))))))))
2526 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2527 (spam-crm114-register-with-crm114 articles t unregister))
2529 (defun spam-crm114-unregister-spam-routine (articles)
2530 (spam-crm114-register-spam-routine articles t))
2532 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2533 (spam-crm114-register-with-crm114 articles nil unregister))
2535 (defun spam-crm114-unregister-ham-routine (articles)
2536 (spam-crm114-register-ham-routine articles t))
2542 (defun spam-initialize (&rest symbols)
2543 "Install the spam.el hooks and do other initialization.
2544 When SYMBOLS is given, set those variables to t. This is so you
2545 can call spam-initialize before you set spam-use-* variables on
2546 explicitly, and matters only if you need the extra headers
2547 installed through spam-necessary-extra-headers."
2550 (dolist (var symbols)
2553 (dolist (header (spam-necessary-extra-headers))
2554 (add-to-list 'nnmail-extra-headers header)
2555 (add-to-list 'gnus-extra-headers header))
2557 (setq spam-install-hooks t)
2558 ;; TODO: How do we redo this every time spam-face is customized?
2559 (push '((eq mark gnus-spam-mark) . spam-face)
2560 gnus-summary-highlight)
2561 ;; Add hooks for loading and saving the spam stats
2562 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2563 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2564 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2565 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2566 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2567 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2568 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2570 (defun spam-unload-hook ()
2571 "Uninstall the spam.el hooks"
2573 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2574 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2575 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2576 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2577 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2578 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2579 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2581 (when spam-install-hooks
2586 ;;; spam.el ends here