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))
44 (require 'gnus-uu) ; because of key prefix issues
45 ;;; for the definitions of group content classification and spam processors
47 (require 'message) ;for the message-fetch-field functions
49 ;; for nnimap-split-download-body-default
50 (eval-when-compile (require 'nnimap))
54 (autoload 'query-dig "dig"))
56 ;; autoload spam-report
58 (autoload 'spam-report-gmane "spam-report"))
60 ;; autoload gnus-registry
62 (autoload 'gnus-registry-group-count "gnus-registry")
63 (autoload 'gnus-registry-add-group "gnus-registry")
64 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
65 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
69 (autoload 'query-dns "dns"))
74 "Spam configuration.")
76 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
77 "Directory for spam whitelists and blacklists."
81 (defcustom spam-move-spam-nonspam-groups-only t
82 "Whether spam should be moved in non-spam groups only.
83 When t, only ham and unclassified groups will have their spam moved
84 to the spam-process-destination. When nil, spam will also be moved from
89 (defcustom spam-process-ham-in-nonham-groups nil
90 "Whether ham should be processed in non-ham groups."
94 (defcustom spam-log-to-registry nil
95 "Whether spam/ham processing should be logged in the registry."
99 (defcustom spam-split-symbolic-return nil
100 "Whether `spam-split' should work with symbols or group names."
104 (defcustom spam-split-symbolic-return-positive nil
105 "Whether `spam-split' should ALWAYS work with symbols or group names.
106 Do not set this if you use `spam-split' in a fancy split
111 (defcustom spam-process-ham-in-spam-groups nil
112 "Whether ham should be processed in spam groups."
116 (defcustom spam-mark-only-unseen-as-spam t
117 "Whether only unseen articles should be marked as spam in spam groups.
118 When nil, all unread articles in a spam group are marked as
119 spam. Set this if you want to leave an article unread in a spam group
120 without losing it to the automatic spam-marking process."
124 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
125 "Whether ham should be marked unread before it's moved.
126 The article is moved out of a spam group according to ham-process-destination.
127 This variable is an official entry in the international Longest Variable Name
132 (defcustom spam-disable-spam-split-during-ham-respool nil
133 "Whether `spam-split' should be ignored while resplitting ham.
134 This is useful to prevent ham from ending up in the same spam
135 group after the resplit. Don't set this to t if you have `spam-split' as the
136 last rule in your split configuration."
140 (defcustom spam-autodetect-recheck-messages nil
141 "Should spam.el recheck all meessages when autodetecting?
142 Normally this is nil, so only unseen messages will be checked."
146 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
147 "The location of the whitelist.
148 The file format is one regular expression per line.
149 The regular expression is matched against the address."
153 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
154 "The location of the blacklist.
155 The file format is one regular expression per line.
156 The regular expression is matched against the address."
160 (defcustom spam-use-dig t
161 "Whether `query-dig' should be used instead of `query-dns'."
165 (defcustom spam-use-gmane-xref nil
166 "Whether the Gmane spam xref should be used by `spam-split'."
170 (defcustom spam-use-blacklist nil
171 "Whether the blacklist should be used by `spam-split'."
175 (defcustom spam-blacklist-ignored-regexes nil
176 "Regular expressions that the blacklist should ignore."
177 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
180 (defcustom spam-use-whitelist nil
181 "Whether the whitelist should be used by `spam-split'."
185 (defcustom spam-use-whitelist-exclusive nil
186 "Whether whitelist-exclusive should be used by `spam-split'.
187 Exclusive whitelisting means that all messages from senders not in the whitelist
188 are considered spam."
192 (defcustom spam-use-blackholes nil
193 "Whether blackholes should be used by `spam-split'."
197 (defcustom spam-use-hashcash nil
198 "Whether hashcash payments should be detected by `spam-split'."
202 (defcustom spam-use-regex-headers nil
203 "Whether a header regular expression match should be used by `spam-split'.
204 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
208 (defcustom spam-use-regex-body nil
209 "Whether a body regular expression match should be used by `spam-split'.
210 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
214 (defcustom spam-use-bogofilter-headers nil
215 "Whether bogofilter headers should be used by `spam-split'.
216 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
220 (defcustom spam-use-bogofilter nil
221 "Whether bogofilter should be invoked by `spam-split'.
222 Enable this if you want Gnus to invoke Bogofilter on new messages."
226 (defcustom spam-use-BBDB nil
227 "Whether BBDB should be used by `spam-split'."
231 (defcustom spam-use-BBDB-exclusive nil
232 "Whether BBDB-exclusive should be used by `spam-split'.
233 Exclusive BBDB means that all messages from senders not in the BBDB are
238 (defcustom spam-use-ifile nil
239 "Whether ifile should be used by `spam-split'."
243 (defcustom spam-use-stat nil
244 "Whether `spam-stat' should be used by `spam-split'."
248 (defcustom spam-use-spamoracle nil
249 "Whether spamoracle should be used by `spam-split'."
253 (defcustom spam-use-spamassassin nil
254 "Whether spamassassin should be invoked by `spam-split'.
255 Enable this if you want Gnus to invoke SpamAssassin on new messages."
259 (defcustom spam-use-spamassassin-headers nil
260 "Whether spamassassin headers should be checked by `spam-split'.
261 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
266 (defcustom spam-install-hooks (or
271 spam-use-whitelist-exclusive
274 spam-use-regex-headers
277 spam-use-bogofilter-headers
278 spam-use-spamassassin
279 spam-use-spamassassin-headers
281 spam-use-BBDB-exclusive
285 "Whether the spam hooks should be installed.
286 Default to t if one of the spam-use-* variables is set."
290 (defcustom spam-split-group "spam"
291 "Group name where incoming spam should be put by `spam-split'."
295 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
296 ;;; not regular expressions
297 (defcustom spam-junk-mailgroups (cons
299 '("mail.junk" "poste.pourriel"))
300 "Mailgroups with spam contents.
301 All unmarked article in such group receive the spam mark on group entry."
302 :type '(repeat (string :tag "Group"))
306 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
307 "The group where spam xrefs can be found on Gmane.
308 Only meaningful if you enable `spam-use-gmane-xref'."
312 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
313 "dev.null.dk" "relays.visi.com")
314 "List of blackhole servers.
315 Only meaningful if you enable `spam-use-blackholes'."
316 :type '(repeat (string :tag "Server"))
319 (defcustom spam-blackhole-good-server-regex nil
320 "String matching IP addresses that should not be checked in the blackholes.
321 Only meaningful if you enable `spam-use-blackholes'."
322 :type '(radio (const nil)
323 (regexp :format "%t: %v\n" :size 0))
326 (defcustom spam-face 'gnus-splash-face
327 "Face for spam-marked articles."
331 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
332 "Regular expression for positive header spam matches.
333 Only meaningful if you enable `spam-use-regex-headers'."
334 :type '(repeat (regexp :tag "Regular expression to match spam header"))
337 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
338 "Regular expression for positive header ham matches.
339 Only meaningful if you enable `spam-use-regex-headers'."
340 :type '(repeat (regexp :tag "Regular expression to match ham header"))
343 (defcustom spam-regex-body-spam '()
344 "Regular expression for positive body spam matches.
345 Only meaningful if you enable `spam-use-regex-body'."
346 :type '(repeat (regexp :tag "Regular expression to match spam body"))
349 (defcustom spam-regex-body-ham '()
350 "Regular expression for positive body ham matches.
351 Only meaningful if you enable `spam-use-regex-body'."
352 :type '(repeat (regexp :tag "Regular expression to match ham body"))
355 (defgroup spam-ifile nil
356 "Spam ifile configuration."
359 (defcustom spam-ifile-path (executable-find "ifile")
360 "File path of the ifile executable program."
361 :type '(choice (file :tag "Location of ifile")
362 (const :tag "ifile is not installed"))
365 (defcustom spam-ifile-database-path nil
366 "File path of the ifile database."
367 :type '(choice (file :tag "Location of the ifile database")
368 (const :tag "Use the default"))
371 (defcustom spam-ifile-spam-category "spam"
372 "Name of the spam ifile category."
376 (defcustom spam-ifile-ham-category nil
377 "Name of the ham ifile category.
378 If nil, the current group name will be used."
379 :type '(choice (string :tag "Use a fixed category")
380 (const :tag "Use the current group name"))
383 (defcustom spam-ifile-all-categories nil
384 "Whether the ifile check will return all categories, or just spam.
385 Set this to t if you want to use the `spam-split' invocation of ifile as
386 your main source of newsgroup names."
390 (defgroup spam-bogofilter nil
391 "Spam bogofilter configuration."
394 (defcustom spam-bogofilter-path (executable-find "bogofilter")
395 "File path of the Bogofilter executable program."
396 :type '(choice (file :tag "Location of bogofilter")
397 (const :tag "Bogofilter is not installed"))
398 :group 'spam-bogofilter)
400 (defcustom spam-bogofilter-header "X-Bogosity"
401 "The header that Bogofilter inserts in messages."
403 :group 'spam-bogofilter)
405 (defcustom spam-bogofilter-spam-switch "-s"
406 "The switch that Bogofilter uses to register spam messages."
408 :group 'spam-bogofilter)
410 (defcustom spam-bogofilter-ham-switch "-n"
411 "The switch that Bogofilter uses to register ham messages."
413 :group 'spam-bogofilter)
415 (defcustom spam-bogofilter-spam-strong-switch "-S"
416 "The switch that Bogofilter uses to unregister ham messages."
418 :group 'spam-bogofilter)
420 (defcustom spam-bogofilter-ham-strong-switch "-N"
421 "The switch that Bogofilter uses to unregister spam messages."
423 :group 'spam-bogofilter)
425 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
426 "The regex on `spam-bogofilter-header' for positive spam identification."
428 :group 'spam-bogofilter)
430 (defcustom spam-bogofilter-database-directory nil
431 "Directory path of the Bogofilter databases."
432 :type '(choice (directory
433 :tag "Location of the Bogofilter database directory")
434 (const :tag "Use the default"))
435 :group 'spam-bogofilter)
437 (defgroup spam-spamoracle nil
438 "Spam spamoracle configuration."
441 (defcustom spam-spamoracle-database nil
442 "Location of spamoracle database file.
443 When nil, use the default spamoracle database."
444 :type '(choice (directory :tag "Location of spamoracle database file.")
445 (const :tag "Use the default"))
446 :group 'spam-spamoracle)
448 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
449 "Location of the spamoracle binary."
450 :type '(choice (directory :tag "Location of the spamoracle binary")
451 (const :tag "Use the default"))
452 :group 'spam-spamoracle)
454 (defgroup spam-spamassassin nil
455 "Spam SpamAssassin configuration."
458 (defcustom spam-spamassassin-path (executable-find "spamassassin")
459 "File path of the spamassassin executable program.
460 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
461 spamd man pages for more information on these programs."
462 :type '(choice (file :tag "Location of spamc")
463 (const :tag "spamassassin is not installed"))
464 :group 'spam-spamassassin)
466 (defcustom spam-spamassassin-arguments ()
467 "Arguments to pass to the spamassassin executable.
468 This must be a list. For example, `(\"-C\" \"configfile\")'."
469 :type '(restricted-sexp :match-alternatives (listp))
470 :group 'spam-spamassassin)
472 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
473 "The header inserted by SpamAssassin to flag spam."
475 :group 'spam-spamassassin)
477 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
478 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
481 :group 'spam-spamassassin)
483 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
484 "The header inserted by SpamAssassin, giving extended scoring information"
486 :group 'spam-spamassassin)
488 (defcustom spam-sa-learn-path (executable-find "sa-learn")
489 "File path of the sa-learn executable program."
490 :type '(choice (file :tag "Location of spamassassin")
491 (const :tag "spamassassin is not installed"))
492 :group 'spam-spamassassin)
494 (defcustom spam-sa-learn-rebuild t
495 "Whether sa-learn should rebuild the database every time it is called
496 Enable this if you want sa-learn to rebuild the database automatically. Doing
497 this will slightly increase the running time of the spam registration process.
498 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
499 order for SpamAssassin to recognize the new registered spam."
501 :group 'spam-spamassassin)
503 (defcustom spam-sa-learn-spam-switch "--spam"
504 "The switch that sa-learn uses to register spam messages"
506 :group 'spam-spamassassin)
508 (defcustom spam-sa-learn-ham-switch "--ham"
509 "The switch that sa-learn uses to register ham messages"
511 :group 'spam-spamassassin)
513 (defcustom spam-sa-learn-unregister-switch "--forget"
514 "The switch that sa-learn uses to unregister messages messages"
516 :group 'spam-spamassassin)
518 ;;; Key bindings for spam control.
520 (gnus-define-keys gnus-summary-mode-map
521 "St" spam-generic-score
522 "Sx" gnus-summary-mark-as-spam
523 "Mst" spam-generic-score
524 "Msx" gnus-summary-mark-as-spam
525 "\M-d" gnus-summary-mark-as-spam)
527 (defvar spam-cache-lookups t
528 "Whether spam.el will try to cache lookups using `spam-caches'.")
530 (defvar spam-caches (make-hash-table
533 "Cache of spam detection entries.")
535 (defvar spam-old-ham-articles nil
536 "List of old ham articles, generated when a group is entered.")
538 (defvar spam-old-spam-articles nil
539 "List of old spam articles, generated when a group is entered.")
541 (defvar spam-split-disabled nil
542 "If non-nil, `spam-split' is disabled, and always returns nil.")
544 (defvar spam-split-last-successful-check nil
546 `spam-split' will set this to nil or a spam-use-XYZ check if it
549 ;; convenience functions
550 (defun spam-clear-cache (symbol)
551 "Clear the spam-caches entry for a check."
552 (remhash symbol spam-caches))
554 (defun spam-xor (a b)
556 (and (or a b) (not (and a b))))
558 (defun spam-group-ham-mark-p (group mark &optional spam)
559 "Checks if MARK is considered a ham mark in GROUP."
560 (when (stringp group)
561 (let* ((marks (spam-group-ham-marks group spam))
562 (marks (if (symbolp mark)
564 (mapcar 'symbol-value marks))))
567 (defun spam-group-spam-mark-p (group mark)
568 "Checks if MARK is considered a spam mark in GROUP."
569 (spam-group-ham-mark-p group mark t))
571 (defun spam-group-ham-marks (group &optional spam)
572 "In GROUP, get all the ham marks."
573 (when (stringp group)
574 (let* ((marks (if spam
575 (gnus-parameter-spam-marks group)
576 (gnus-parameter-ham-marks group)))
578 (marks (if (listp (car marks)) (car marks) marks)))
581 (defun spam-group-spam-marks (group)
582 "In GROUP, get all the spam marks."
583 (spam-group-ham-marks group t))
585 (defun spam-group-spam-contents-p (group)
586 "Is GROUP a spam group?"
588 (or (member group spam-junk-mailgroups)
589 (memq 'gnus-group-spam-classification-spam
590 (gnus-parameter-spam-contents group)))
593 (defun spam-group-ham-contents-p (group)
594 "Is GROUP a ham group?"
596 (memq 'gnus-group-spam-classification-ham
597 (gnus-parameter-spam-contents group))
600 (defvar spam-list-of-processors
601 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
602 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
603 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
604 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
605 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
606 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
607 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
608 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
609 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
610 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
611 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
612 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
613 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
614 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
615 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
616 "The `spam-list-of-processors' list.
617 This list contains pairs associating a ham/spam exit processor
618 variable with a classification and a spam-use-* variable.")
620 (defun spam-group-processor-p (group processor)
621 (if (and (stringp group)
623 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
624 (spam-group-processor-multiple-p
626 (cdr-safe (assoc processor spam-list-of-processors))))
629 (defun spam-group-processor-multiple-p (group processor-info)
630 (let* ((classification (nth 0 processor-info))
631 (check (nth 1 processor-info))
632 (parameters (nth 0 (gnus-parameter-spam-process group)))
634 (dolist (parameter parameters)
635 (when (and (null found)
637 (eq classification (nth 0 parameter))
638 (eq check (nth 1 parameter)))
642 (defun spam-group-spam-processor-report-gmane-p (group)
643 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
645 (defun spam-group-spam-processor-bogofilter-p (group)
646 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
648 (defun spam-group-spam-processor-blacklist-p (group)
649 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
651 (defun spam-group-spam-processor-ifile-p (group)
652 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
654 (defun spam-group-ham-processor-ifile-p (group)
655 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
657 (defun spam-group-spam-processor-spamoracle-p (group)
658 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
660 (defun spam-group-ham-processor-bogofilter-p (group)
661 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
663 (defun spam-group-spam-processor-stat-p (group)
664 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
666 (defun spam-group-ham-processor-stat-p (group)
667 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
669 (defun spam-group-ham-processor-whitelist-p (group)
670 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
672 (defun spam-group-ham-processor-BBDB-p (group)
673 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
675 (defun spam-group-ham-processor-copy-p (group)
676 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
678 (defun spam-group-ham-processor-spamoracle-p (group)
679 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
681 (defun spam-report-articles-gmane (n)
682 "Report the current message as spam.
683 Respects the process/prefix convention."
685 (dolist (article (gnus-summary-work-articles n))
686 (gnus-summary-remove-process-mark article)
687 (spam-report-gmane article)))
689 (defun spam-necessary-extra-headers ()
690 "Return the extra headers spam.el thinks are necessary."
692 (when (or spam-use-spamassassin
693 spam-use-spamassassin-headers
694 spam-use-regex-headers)
695 (push 'X-Spam-Status list))
698 (defun spam-user-format-function-S (headers)
700 (spam-summary-score headers)))
702 (defun spam-article-sort-by-spam-status (h1 h2)
703 "Sort articles by score."
705 (dolist (header (spam-necessary-extra-headers))
706 (let ((s1 (spam-summary-score h1 header))
707 (s2 (spam-summary-score h2 header)))
709 (setq result (< s1 s2))
713 (defun spam-extra-header-to-number (header headers)
714 "Transform an extra header to a number."
715 (if (gnus-extra-header header headers)
717 ((eq header 'X-Spam-Status)
718 (string-to-number (gnus-replace-in-string
719 (gnus-extra-header header headers)
724 (defun spam-summary-score (headers &optional specific-header)
725 "Score an article for the summary buffer, as fast as possible.
726 With SPECIFIC-HEADER, returns only that header's score.
727 Will not return a nil score."
731 (list specific-header)
732 (spam-necessary-extra-headers)))
734 (spam-extra-header-to-number header headers))
739 (defun spam-generic-score ()
740 "Invoke whatever scoring method we can."
743 spam-use-spamassassin
744 spam-use-spamassassin-headers)
745 (spam-spamassassin-score)
746 (spam-bogofilter-score)))
748 ;;; Summary entry and exit processing.
750 (defun spam-summary-prepare ()
751 (setq spam-old-ham-articles
752 (spam-list-articles gnus-newsgroup-articles 'ham))
753 (setq spam-old-spam-articles
754 (spam-list-articles gnus-newsgroup-articles 'spam))
755 (spam-mark-junk-as-spam-routine))
757 ;; The spam processors are invoked for any group, spam or ham or neither
758 (defun spam-summary-prepare-exit ()
759 (unless gnus-group-is-exiting-without-update-p
760 (gnus-message 6 "Exiting summary buffer and applying spam rules")
762 ;; first of all, unregister any articles that are no longer ham or spam
763 ;; we have to iterate over the processors, or else we'll be too slow
764 (dolist (classification '(spam ham))
765 (let* ((old-articles (if (eq classification 'spam)
766 spam-old-spam-articles
767 spam-old-ham-articles))
768 (new-articles (spam-list-articles
769 gnus-newsgroup-articles
771 (changed-articles (spam-set-difference new-articles old-articles)))
772 ;; now that we have the changed articles, we go through the processors
773 (dolist (processor-param spam-list-of-processors)
774 (let ((processor (nth 0 processor-param))
775 (processor-classification (nth 1 processor-param))
776 (check (nth 2 processor-param))
778 (dolist (article changed-articles)
779 (let ((id (spam-fetch-field-message-id-fast article)))
780 (when (spam-log-unregistration-needed-p
781 id 'process classification check)
782 (push article unregister-list))))
783 ;; call spam-register-routine with specific articles to unregister,
784 ;; when there are articles to unregister and the check is enabled
785 (when (and unregister-list (symbol-value check))
786 (spam-register-routine
787 classification check t unregister-list))))))
789 ;; find all the spam processors applicable to this group
790 (dolist (processor-param spam-list-of-processors)
791 (let ((processor (nth 0 processor-param))
792 (classification (nth 1 processor-param))
793 (check (nth 2 processor-param)))
794 (when (and (eq 'spam classification)
795 (spam-group-processor-p gnus-newsgroup-name processor))
796 (spam-register-routine classification check))))
798 (unless (and spam-move-spam-nonspam-groups-only
799 (spam-group-spam-contents-p gnus-newsgroup-name))
800 (gnus-message 6 "Marking spam as expired and moving it to %s"
801 (gnus-parameter-spam-process-destination
802 gnus-newsgroup-name))
803 (spam-mark-spam-as-expired-and-move-routine
804 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
806 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
807 ;; expire spam, in case the above did not expire them
808 (gnus-message 6 "Marking spam as expired without moving it")
809 (spam-mark-spam-as-expired-and-move-routine nil)
811 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
812 (and (spam-group-spam-contents-p gnus-newsgroup-name)
813 spam-process-ham-in-spam-groups)
814 spam-process-ham-in-nonham-groups)
815 ;; find all the ham processors applicable to this group
816 (dolist (processor-param spam-list-of-processors)
817 (let ((processor (nth 0 processor-param))
818 (classification (nth 1 processor-param))
819 (check (nth 2 processor-param)))
820 (when (and (eq 'ham classification)
821 (spam-group-processor-p gnus-newsgroup-name processor))
822 (spam-register-routine classification check)))))
824 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
825 (gnus-message 6 "Copying ham")
826 (spam-ham-copy-routine
827 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
829 ;; now move all ham articles out of spam groups
830 (when (spam-group-spam-contents-p gnus-newsgroup-name)
831 (gnus-message 6 "Moving ham messages from spam group")
832 (spam-ham-move-routine
833 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
835 (setq spam-old-ham-articles nil)
836 (setq spam-old-spam-articles nil))
838 (defun spam-set-difference (list1 list2)
839 "Return a set difference of LIST1 and LIST2.
840 When either list is nil, the other is returned."
841 (if (and list1 list2)
842 ;; we have two non-nil lists
844 (dolist (item (append list1 list2))
845 (when (and (memq item list1) (memq item list2))
846 (setq list1 (delq item list1))
847 (setq list2 (delq item list2))))
848 (append list1 list2))
849 ;; if either of the lists was nil, return the other one
850 (if list1 list1 list2)))
852 (defun spam-mark-junk-as-spam-routine ()
853 ;; check the global list of group names spam-junk-mailgroups and the
855 (when (spam-group-spam-contents-p gnus-newsgroup-name)
856 (gnus-message 6 "Marking %s articles as spam"
857 (if spam-mark-only-unseen-as-spam
860 (let ((articles (if spam-mark-only-unseen-as-spam
861 gnus-newsgroup-unseen
862 gnus-newsgroup-unreads)))
863 (dolist (article articles)
864 (gnus-summary-mark-article article gnus-spam-mark)))))
866 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
867 (if (and (car-safe groups) (listp (car-safe groups)))
868 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
869 (gnus-summary-kill-process-mark)
870 (let ((articles gnus-newsgroup-articles)
871 (backend-supports-deletions
872 (gnus-check-backend-function
873 'request-move-article gnus-newsgroup-name))
874 article tomove deletep)
875 (dolist (article articles)
876 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
877 (gnus-summary-mark-article article gnus-expirable-mark)
878 (push article tomove)))
880 ;; now do the actual copies
881 (dolist (group groups)
884 (dolist (article tomove)
885 (gnus-summary-set-process-mark article))
887 (if (or (not backend-supports-deletions)
888 (> (length groups) 1))
890 (gnus-summary-copy-article nil group)
892 (gnus-summary-move-article nil group)))))
894 ;; now delete the articles, if there was a copy done, and the
896 (when (and deletep backend-supports-deletions)
897 (dolist (article tomove)
898 (gnus-summary-set-process-mark article))
900 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
901 (gnus-summary-delete-article nil))))
903 (gnus-summary-yank-process-mark))))
905 (defun spam-ham-copy-or-move-routine (copy groups)
906 (gnus-summary-kill-process-mark)
907 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
908 (backend-supports-deletions
909 (gnus-check-backend-function
910 'request-move-article gnus-newsgroup-name))
911 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
912 article mark todo deletep respool)
914 (when (member 'respool groups)
915 (setq respool t) ; boolean for later
916 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
918 ;; now do the actual move
919 (dolist (group groups)
920 (when (and todo (stringp group))
921 (dolist (article todo)
922 (when spam-mark-ham-unread-before-move-from-spam-group
923 (gnus-summary-mark-article article gnus-unread-mark))
924 (gnus-summary-set-process-mark article))
926 (if respool ; respooling is with a "fake" group
927 (let ((spam-split-disabled
928 (or spam-split-disabled
929 spam-disable-spam-split-during-ham-respool)))
930 (gnus-summary-respool-article nil respool-method))
931 (if (or (not backend-supports-deletions) ; else, we are not respooling
932 (> (length groups) 1))
933 (progn ; if copying, copy and set deletep
934 (gnus-summary-copy-article nil group)
936 (gnus-summary-move-article nil group))))) ; else move articles
938 ;; now delete the articles, unless a) copy is t, and there was a copy done
939 ;; b) a move was done to a single group
940 ;; c) backend-supports-deletions is nil
942 (when (and deletep backend-supports-deletions)
943 (dolist (article todo)
944 (gnus-summary-set-process-mark article))
946 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
947 (gnus-summary-delete-article nil))))))
949 (gnus-summary-yank-process-mark))
951 (defun spam-ham-copy-routine (&rest groups)
952 (if (and (car-safe groups) (listp (car-safe groups)))
953 (apply 'spam-ham-copy-routine (car groups))
954 (spam-ham-copy-or-move-routine t groups)))
956 (defun spam-ham-move-routine (&rest groups)
957 (if (and (car-safe groups) (listp (car-safe groups)))
958 (apply 'spam-ham-move-routine (car groups))
959 (spam-ham-copy-or-move-routine nil groups)))
961 (defun spam-get-article-as-string (article)
962 (when (numberp article)
964 (gnus-request-article-this-buffer
970 ;; (defun spam-get-article-as-filename (article)
971 ;; (let ((article-filename))
972 ;; (when (numberp article)
973 ;; (nnml-possibly-change-directory
974 ;; (gnus-group-real-name gnus-newsgroup-name))
975 ;; (setq article-filename (expand-file-name
976 ;; (int-to-string article) nnml-current-directory)))
977 ;; (if (file-exists-p article-filename)
981 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
982 "Fetch a field quickly, using the internal gnus-data-list function"
983 (when (numberp article)
984 (let* ((data-header (or prepared-data-header
985 (spam-fetch-article-header article))))
986 (if (arrayp data-header)
989 (mail-header-from data-header))
990 ((equal field 'message-id)
991 (mail-header-message-id data-header))
992 ((equal field 'subject)
993 (mail-header-subject data-header))
994 ((equal field 'references)
995 (mail-header-references data-header))
997 (mail-header-date data-header))
999 (mail-header-xref data-header))
1000 ((equal field 'extra)
1001 (mail-header-extra data-header))
1004 (gnus-message 6 "Article %d has a nil data header" article)))))
1006 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1007 (spam-fetch-field-fast article 'from prepared-data-header))
1009 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1010 (spam-fetch-field-fast article 'subject prepared-data-header))
1012 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1013 (spam-fetch-field-fast article 'message-id prepared-data-header))
1015 (defun spam-generate-fake-headers (article)
1016 (let ((dh (spam-fetch-article-header article)))
1020 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1021 "Date: %s\nReferences: %s\nXref: %s\n")
1022 (spam-fetch-field-fast article 'from dh)
1023 (spam-fetch-field-fast article 'subject dh)
1024 (spam-fetch-field-fast article 'message-id dh)
1025 (spam-fetch-field-fast article 'date dh)
1026 (spam-fetch-field-fast article 'references dh)
1027 (spam-fetch-field-fast article 'xref dh))
1028 (when (spam-fetch-field-fast article 'extra dh)
1029 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1032 "spam-generate-fake-headers: article %d didn't have a valid header"
1035 (defun spam-fetch-article-header (article)
1037 (set-buffer gnus-summary-buffer)
1038 (gnus-read-header article)
1039 (nth 3 (assq article gnus-newsgroup-data))))
1042 ;;;; Spam determination.
1044 (defvar spam-list-of-checks
1045 '((spam-use-blacklist . spam-check-blacklist)
1046 (spam-use-regex-headers . spam-check-regex-headers)
1047 (spam-use-gmane-xref . spam-check-gmane-xref)
1048 (spam-use-regex-body . spam-check-regex-body)
1049 (spam-use-whitelist . spam-check-whitelist)
1050 (spam-use-BBDB . spam-check-BBDB)
1051 (spam-use-BBDB-exclusive . spam-check-BBDB)
1052 (spam-use-ifile . spam-check-ifile)
1053 (spam-use-spamoracle . spam-check-spamoracle)
1054 (spam-use-stat . spam-check-stat)
1055 (spam-use-blackholes . spam-check-blackholes)
1056 (spam-use-hashcash . spam-check-hashcash)
1057 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1058 (spam-use-spamassassin . spam-check-spamassassin)
1059 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1060 (spam-use-bogofilter . spam-check-bogofilter))
1061 "The spam-list-of-checks list contains pairs associating a
1062 parameter variable with a spam checking function. If the
1063 parameter variable is true, then the checking function is called,
1064 and its value decides what happens. Each individual check may
1065 return nil, t, or a mailgroup name. The value nil means that the
1066 check does not yield a decision, and so, that further checks are
1067 needed. The value t means that the message is definitely not
1068 spam, and that further spam checks should be inhibited.
1069 Otherwise, a mailgroup name or the symbol 'spam (depending on
1070 spam-split-symbolic-return) is returned where the mail should go,
1071 and further checks are also inhibited. The usual mailgroup name
1072 is the value of `spam-split-group', meaning that the message is
1073 definitely a spam.")
1075 (defvar spam-list-of-statistical-checks
1081 spam-use-spamassassin
1082 spam-use-spamoracle)
1083 "The spam-list-of-statistical-checks list contains all the mail
1084 splitters that need to have the full message body available.
1085 Note that you should fetch extra headers if you don't like this,
1086 e.g. fetch the 'Received' header for spam-use-blackholes.")
1088 (defun spam-split (&rest specific-checks)
1089 "Split this message into the `spam' group if it is spam.
1090 This function can be used as an entry in the variable `nnmail-split-fancy',
1091 for example like this: (: spam-split). It can take checks as
1092 parameters. A string as a parameter will set the
1093 spam-split-group to that string.
1095 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1097 (setq spam-split-last-successful-check nil)
1098 (unless spam-split-disabled
1099 (let ((spam-split-group-choice spam-split-group))
1100 (dolist (check specific-checks)
1101 (when (stringp check)
1102 (setq spam-split-group-choice check)
1103 (setq specific-checks (delq check specific-checks))))
1105 (let ((spam-split-group spam-split-group-choice))
1108 (dolist (check spam-list-of-statistical-checks)
1109 (when (and (symbolp check)
1110 (or (symbol-value check)
1111 (memq check specific-checks)))
1113 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1114 (symbol-name check))
1116 ;; (progn (widen) (debug (buffer-string)))
1117 (let ((list-of-checks spam-list-of-checks)
1119 (while (and list-of-checks (not decision))
1120 (let ((pair (pop list-of-checks)))
1122 ;; either, given specific checks, this is one of them
1123 (and specific-checks (memq (car pair) specific-checks))
1124 ;; or, given no specific checks, spam-use-CHECK is set
1125 (and (null specific-checks) (symbol-value (car pair))))
1126 (gnus-message 6 "spam-split: calling the %s function"
1127 (symbol-name (cdr pair)))
1128 (setq decision (funcall (cdr pair)))
1129 ;; if we got a decision at all, save the current check
1131 (setq spam-split-last-successful-check (car pair)))
1133 (when (eq decision 'spam)
1134 (unless spam-split-symbolic-return
1137 (format "spam-split got %s but %s is nil"
1138 (symbol-name decision)
1139 (symbol-name spam-split-symbolic-return))))))))
1141 (if spam-split-symbolic-return-positive 'ham nil)
1144 (defun spam-find-spam ()
1145 "This function will detect spam in the current newsgroup using spam-split."
1148 (let* ((group gnus-newsgroup-name)
1149 (autodetect (gnus-parameter-spam-autodetect group))
1150 (methods (gnus-parameter-spam-autodetect-methods group))
1151 (first-method (nth 0 methods))
1152 (articles (if spam-autodetect-recheck-messages
1153 gnus-newsgroup-articles
1154 gnus-newsgroup-unseen))
1155 article-cannot-be-faked)
1157 (dolist (check spam-list-of-statistical-checks)
1158 (when (and (symbolp check)
1159 (memq check methods))
1160 (setq article-cannot-be-faked t)
1163 (when (memq 'default methods)
1164 (setq article-cannot-be-faked t))
1166 (when (and autodetect
1167 (not (equal first-method 'none)))
1170 (let ((id (spam-fetch-field-message-id-fast article))
1171 (subject (spam-fetch-field-subject-fast article))
1172 (sender (spam-fetch-field-from-fast article))
1176 (gnus-message 6 "Article %d has no message ID!" article))
1178 (when (and id spam-log-to-registry)
1179 (setq registry-lookup (spam-log-registration-type id 'incoming))
1180 (when registry-lookup
1183 "spam-find-spam: message %s was already registered incoming"
1186 (let* ((spam-split-symbolic-return t)
1187 (spam-split-symbolic-return-positive t)
1188 (fake-headers (spam-generate-fake-headers article))
1192 (if article-cannot-be-faked
1193 (gnus-request-article-this-buffer
1196 ;; else, we fake the article
1197 (when fake-headers (insert fake-headers)))
1198 (if (or (null first-method)
1199 (equal first-method 'default))
1201 (apply 'spam-split methods))))))
1202 (if (equal split-return 'spam)
1203 (gnus-summary-mark-article article gnus-spam-mark))
1205 (when (and id split-return spam-log-to-registry)
1206 (when (zerop (gnus-registry-group-count id))
1207 (gnus-registry-add-group
1208 id group subject sender))
1210 (unless registry-lookup
1211 (spam-log-processing-to-registry
1215 spam-split-last-successful-check
1219 (defvar spam-registration-functions
1220 ;; first the ham register, second the spam register function
1221 ;; third the ham unregister, fourth the spam unregister function
1222 '((spam-use-blacklist nil
1223 spam-blacklist-register-routine
1225 spam-blacklist-unregister-routine)
1226 (spam-use-whitelist spam-whitelist-register-routine
1228 spam-whitelist-unregister-routine
1230 (spam-use-ham-copy nil
1234 (spam-use-BBDB spam-BBDB-register-routine
1236 spam-BBDB-unregister-routine
1238 (spam-use-ifile spam-ifile-register-ham-routine
1239 spam-ifile-register-spam-routine
1240 spam-ifile-unregister-ham-routine
1241 spam-ifile-unregister-spam-routine)
1242 (spam-use-spamoracle spam-spamoracle-learn-ham
1243 spam-spamoracle-learn-spam
1244 spam-spamoracle-unlearn-ham
1245 spam-spamoracle-unlearn-spam)
1246 (spam-use-stat spam-stat-register-ham-routine
1247 spam-stat-register-spam-routine
1248 spam-stat-unregister-ham-routine
1249 spam-stat-unregister-spam-routine)
1250 ;; note that spam-use-gmane is not a legitimate check
1252 spam-report-gmane-register-routine
1253 ;; does Gmane support unregistration?
1256 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1257 spam-spamassassin-register-spam-routine
1258 spam-spamassassin-unregister-ham-routine
1259 spam-spamassassin-unregister-spam-routine)
1260 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1261 spam-bogofilter-register-spam-routine
1262 spam-bogofilter-unregister-ham-routine
1263 spam-bogofilter-unregister-spam-routine))
1264 "The spam-registration-functions list contains pairs
1265 associating a parameter variable with the ham and spam
1266 registration functions, and the ham and spam unregistration
1269 (defun spam-classification-valid-p (classification)
1270 (or (eq classification 'spam)
1271 (eq classification 'ham)))
1273 (defun spam-process-type-valid-p (process-type)
1274 (or (eq process-type 'incoming)
1275 (eq process-type 'process)))
1277 (defun spam-registration-check-valid-p (check)
1278 (assoc check spam-registration-functions))
1280 (defun spam-unregistration-check-valid-p (check)
1281 (assoc check spam-registration-functions))
1283 (defun spam-registration-function (classification check)
1284 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1285 (if (eq classification 'spam)
1289 (defun spam-unregistration-function (classification check)
1290 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1291 (if (eq classification 'spam)
1295 (defun spam-list-articles (articles classification)
1296 (let ((mark-check (if (eq classification 'spam)
1297 'spam-group-spam-mark-p
1298 'spam-group-ham-mark-p))
1299 alist mark-cache-yes mark-cache-no)
1300 (dolist (article articles)
1301 (let ((mark (gnus-summary-article-mark article)))
1302 (unless (or (memq mark mark-cache-yes)
1303 (memq mark mark-cache-no))
1304 (if (funcall mark-check
1307 (push mark mark-cache-yes)
1308 (push mark mark-cache-no)))
1309 (when (memq mark mark-cache-yes)
1310 (push article alist))))
1313 (defun spam-register-routine (classification
1315 &optional unregister
1317 (when (and (spam-classification-valid-p classification)
1318 (spam-registration-check-valid-p check))
1319 (let* ((register-function
1320 (spam-registration-function classification check))
1321 (unregister-function
1322 (spam-unregistration-function classification check))
1323 (run-function (if unregister
1326 (log-function (if unregister
1327 'spam-log-undo-registration
1328 'spam-log-processing-to-registry))
1332 ;; make list of articles, using specific-articles if given
1333 (setq articles (or specific-articles
1335 gnus-newsgroup-articles
1338 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1339 (if unregister "Unregistering" "Registering")
1341 (if specific-articles "specific" "")
1342 (symbol-name classification)
1343 (symbol-name check))
1344 (funcall run-function articles)
1345 ;; now log all the registrations (or undo them, depending on unregister)
1346 (dolist (article articles)
1347 (funcall log-function
1348 (spam-fetch-field-message-id-fast article)
1352 gnus-newsgroup-name))))))
1354 ;;; log a ham- or spam-processor invocation to the registry
1355 (defun spam-log-processing-to-registry (id type classification check group)
1356 (when spam-log-to-registry
1357 (if (and (stringp id)
1359 (spam-process-type-valid-p type)
1360 (spam-classification-valid-p classification)
1361 (spam-registration-check-valid-p check))
1362 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1363 (cell (list classification check group)))
1364 (push cell cell-list)
1365 (gnus-registry-store-extra-entry
1372 (format "%s call with bad ID, type, classification, spam-check, or group"
1373 "spam-log-processing-to-registry")))))
1375 ;;; check if a ham- or spam-processor registration has been done
1376 (defun spam-log-registered-p (id type)
1377 (when spam-log-to-registry
1378 (if (and (stringp id)
1379 (spam-process-type-valid-p type))
1380 (cdr-safe (gnus-registry-fetch-extra id type))
1384 (format "%s called with bad ID, type, classification, or spam-check"
1385 "spam-log-registered-p"))
1388 ;;; check what a ham- or spam-processor registration says
1389 ;;; returns nil if conflicting registrations are found
1390 (defun spam-log-registration-type (id type)
1393 (dolist (reg (spam-log-registered-p id type))
1394 (let ((classification (nth 0 reg)))
1395 (when (spam-classification-valid-p classification)
1397 (not (eq classification decision)))
1398 (setq count (+ 1 count)))
1399 (setq decision classification))))
1405 ;;; check if a ham- or spam-processor registration needs to be undone
1406 (defun spam-log-unregistration-needed-p (id type classification check)
1407 (when spam-log-to-registry
1408 (if (and (stringp id)
1409 (spam-process-type-valid-p type)
1410 (spam-classification-valid-p classification)
1411 (spam-registration-check-valid-p check))
1412 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1414 (dolist (cell cell-list)
1416 (when (and (eq classification (nth 0 cell))
1417 (eq check (nth 1 cell)))
1423 (format "%s called with bad ID, type, classification, or spam-check"
1424 "spam-log-unregistration-needed-p"))
1428 ;;; undo a ham- or spam-processor registration (the group is not used)
1429 (defun spam-log-undo-registration (id type classification check &optional group)
1430 (when (and spam-log-to-registry
1431 (spam-log-unregistration-needed-p id type classification check))
1432 (if (and (stringp id)
1433 (spam-process-type-valid-p type)
1434 (spam-classification-valid-p classification)
1435 (spam-registration-check-valid-p check))
1436 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1437 new-cell-list found)
1438 (dolist (cell cell-list)
1439 (unless (and (eq classification (nth 0 cell))
1440 (eq check (nth 1 cell)))
1441 (push cell new-cell-list)))
1442 (gnus-registry-store-extra-entry
1447 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1448 "spam-log-undo-registration"))
1451 ;;; set up IMAP widening if it's necessary
1452 (defun spam-setup-widening ()
1453 (dolist (check spam-list-of-statistical-checks)
1454 (when (symbol-value check)
1455 (setq nnimap-split-download-body-default t))))
1459 (defun spam-check-gmane-xref ()
1461 (message-fetch-field "Xref")
1462 (message-fetch-field "Newsgroups")))
1463 (spam-split-group (if spam-split-symbolic-return
1466 (when header ; return nil when no header
1467 (when (string-match spam-gmane-xref-spam-group
1469 spam-split-group))))
1474 (defun spam-check-regex-body ()
1475 (let ((spam-regex-headers-ham spam-regex-body-ham)
1476 (spam-regex-headers-spam spam-regex-body-spam))
1477 (spam-check-regex-headers t)))
1482 (defun spam-check-regex-headers (&optional body)
1483 (let ((type (if body "body" "header"))
1484 (spam-split-group (if spam-split-symbolic-return
1488 (dolist (h-regex spam-regex-headers-ham)
1490 (goto-char (point-min))
1491 (when (re-search-forward h-regex nil t)
1492 (message "Ham regex %s search positive." type)
1494 (dolist (s-regex spam-regex-headers-spam)
1496 (goto-char (point-min))
1497 (when (re-search-forward s-regex nil t)
1498 (message "Spam regex %s search positive." type)
1500 (setq ret spam-split-group))))
1506 (defun spam-reverse-ip-string (ip)
1508 (mapconcat 'identity
1509 (nreverse (split-string ip "\\."))
1512 (defun spam-check-blackholes ()
1513 "Check the Received headers for blackholed relays."
1514 (let ((headers (message-fetch-field "received"))
1515 (spam-split-group (if spam-split-symbolic-return
1522 (goto-char (point-min))
1523 (gnus-message 6 "Checking headers for relay addresses")
1524 (while (re-search-forward
1525 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1526 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1527 (push (spam-reverse-ip-string (match-string 1))
1529 (dolist (server spam-blackhole-servers)
1531 (unless (and spam-blackhole-good-server-regex
1532 ;; match the good-server-regex against the reversed (again) IP string
1534 spam-blackhole-good-server-regex
1535 (spam-reverse-ip-string ip)))
1537 (let ((query-string (concat ip "." server)))
1539 (let ((query-result (query-dig query-string)))
1541 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1543 (push (list ip server query-result)
1545 ;; else, if not using dig.el
1546 (when (query-dns query-string)
1547 (gnus-message 6 "positive blackhole check")
1548 (push (list ip server (query-dns query-string 'TXT))
1559 (defun spam-check-hashcash ()
1560 "Check the headers for hashcash payments."
1561 (mail-check-payment))) ;mail-check-payment returns a boolean
1564 (defalias 'mail-check-payment 'ignore)
1565 (defalias 'spam-check-hashcash 'ignore))))
1569 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1570 ;;; <sacha@giotto.sj.ru>
1572 ;; all this is done inside a condition-case to trap errors
1579 ;; when the BBDB changes, we want to clear out our cache
1580 (defun spam-clear-cache-BBDB (&rest immaterial)
1581 (spam-clear-cache 'spam-use-BBDB))
1583 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1585 (defun spam-enter-ham-BBDB (addresses &optional remove)
1586 "Enter an address into the BBDB; implies ham (non-spam) sender"
1587 (dolist (from addresses)
1588 (when (stringp from)
1589 (let* ((parsed-address (gnus-extract-address-components from))
1590 (name (or (nth 0 parsed-address) "Ham Sender"))
1591 (remove-function (if remove
1592 'bbdb-delete-record-internal
1594 (net-address (nth 1 parsed-address))
1595 (record (and net-address
1596 (bbdb-search-simple nil net-address))))
1598 (gnus-message 6 "%s address %s %s BBDB"
1599 (if remove "Deleting" "Adding")
1601 (if remove "from" "to"))
1603 (funcall remove-function record)
1604 (bbdb-create-internal name nil net-address nil nil
1605 "ham sender added by spam.el")))))))
1607 (defun spam-BBDB-register-routine (articles &optional unregister)
1609 (dolist (article articles)
1610 (when (stringp (spam-fetch-field-from-fast article))
1611 (push (spam-fetch-field-from-fast article) addresses)))
1612 ;; now do the register/unregister action
1613 (spam-enter-ham-BBDB addresses unregister)))
1615 (defun spam-BBDB-unregister-routine (articles)
1616 (spam-BBDB-register-routine articles t))
1618 (defun spam-check-BBDB ()
1619 "Mail from people in the BBDB is classified as ham or non-spam"
1620 (let ((who (message-fetch-field "from"))
1621 (spam-split-group (if spam-split-symbolic-return
1624 bbdb-cache bbdb-hashtable)
1625 (when spam-cache-lookups
1626 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1629 ;; this is the expanded (bbdb-hashtable) macro
1630 ;; without the debugging support
1631 (with-current-buffer (bbdb-buffer)
1633 (save-window-excursion
1634 (bbdb-records nil t)
1636 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1638 (setq who (nth 1 (gnus-extract-address-components who)))
1640 (if spam-cache-lookups
1642 (intern-soft who bbdb-cache))
1643 (bbdb-search-simple nil who))
1645 (if spam-use-BBDB-exclusive
1650 (defalias 'bbdb-search-simple 'ignore)
1651 (defalias 'bbdb-records 'ignore)
1652 (defalias 'bbdb-buffer 'ignore)
1653 (defalias 'spam-check-BBDB 'ignore)
1654 (defalias 'spam-BBDB-register-routine 'ignore)
1655 (defalias 'spam-enter-ham-BBDB 'ignore)
1656 (defalias 'bbdb-create-internal 'ignore)
1657 (defalias 'bbdb-delete-record-internal 'ignore)
1658 (defalias 'bbdb-records 'ignore))))
1663 ;;; check the ifile backend; return nil if the mail was NOT classified
1666 (defun spam-get-ifile-database-parameter ()
1667 "Get the command-line parameter for ifile's database from
1668 spam-ifile-database-path."
1669 (if spam-ifile-database-path
1670 (format "--db-file=%s" spam-ifile-database-path)
1673 (defun spam-check-ifile ()
1674 "Check the ifile backend for the classification of this message."
1675 (let ((article-buffer-name (buffer-name))
1676 (spam-split-group (if spam-split-symbolic-return
1681 (let ((temp-buffer-name (buffer-name))
1682 (db-param (spam-get-ifile-database-parameter)))
1684 (set-buffer article-buffer-name)
1685 (apply 'call-process-region
1686 (point-min) (point-max) spam-ifile-path
1687 nil temp-buffer-name nil "-c"
1688 (if db-param `(,db-param "-q") `("-q"))))
1689 ;; check the return now (we're back in the temp buffer)
1690 (goto-char (point-min))
1692 (setq category (buffer-substring (point) (point-at-eol))))
1693 (when (not (zerop (length category))) ; we need a category here
1694 (if spam-ifile-all-categories
1695 (setq return category)
1696 ;; else, if spam-ifile-all-categories is not set...
1697 (when (string-equal spam-ifile-spam-category category)
1698 (setq return spam-split-group)))))) ; note return is nil otherwise
1701 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1702 "Register an article, given as a string, with a category.
1703 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1704 (let ((category (or category gnus-newsgroup-name))
1705 (add-or-delete-option (if unregister "-d" "-i"))
1706 (db (spam-get-ifile-database-parameter))
1709 (dolist (article articles)
1710 (let ((article-string (spam-get-article-as-string article)))
1711 (when (stringp article-string)
1712 (insert article-string))))
1713 (apply 'call-process-region
1714 (point-min) (point-max) spam-ifile-path
1716 add-or-delete-option category
1717 (if db `(,db "-h") `("-h"))))))
1719 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1720 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1722 (defun spam-ifile-unregister-spam-routine (articles)
1723 (spam-ifile-register-spam-routine articles t))
1725 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1726 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1728 (defun spam-ifile-unregister-ham-routine (articles)
1729 (spam-ifile-register-ham-routine articles t))
1736 (let ((spam-stat-install-hooks nil))
1737 (require 'spam-stat))
1739 (defun spam-check-stat ()
1740 "Check the spam-stat backend for the classification of this message"
1741 (let ((spam-split-group (if spam-split-symbolic-return
1744 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1745 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1747 (spam-stat-split-fancy)))
1749 (defun spam-stat-register-spam-routine (articles &optional unregister)
1750 (dolist (article articles)
1751 (let ((article-string (spam-get-article-as-string article)))
1753 (insert article-string)
1755 (spam-stat-buffer-change-to-non-spam)
1756 (spam-stat-buffer-is-spam))))))
1758 (defun spam-stat-unregister-spam-routine (articles)
1759 (spam-stat-register-spam-routine articles t))
1761 (defun spam-stat-register-ham-routine (articles &optional unregister)
1762 (dolist (article articles)
1763 (let ((article-string (spam-get-article-as-string article)))
1765 (insert article-string)
1767 (spam-stat-buffer-change-to-spam)
1768 (spam-stat-buffer-is-non-spam))))))
1770 (defun spam-stat-unregister-ham-routine (articles)
1771 (spam-stat-register-ham-routine articles t))
1773 (defun spam-maybe-spam-stat-load ()
1774 (when spam-use-stat (spam-stat-load)))
1776 (defun spam-maybe-spam-stat-save ()
1777 (when spam-use-stat (spam-stat-save))))
1780 (defalias 'spam-stat-load 'ignore)
1781 (defalias 'spam-stat-save 'ignore)
1782 (defalias 'spam-maybe-spam-stat-load 'ignore)
1783 (defalias 'spam-maybe-spam-stat-save 'ignore)
1784 (defalias 'spam-stat-register-ham-routine 'ignore)
1785 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1786 (defalias 'spam-stat-register-spam-routine 'ignore)
1787 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1788 (defalias 'spam-stat-buffer-is-spam 'ignore)
1789 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1790 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1791 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1792 (defalias 'spam-stat-split-fancy 'ignore)
1793 (defalias 'spam-check-stat 'ignore))))
1797 ;;;; Blacklists and whitelists.
1799 (defvar spam-whitelist-cache nil)
1800 (defvar spam-blacklist-cache nil)
1802 (defun spam-kill-whole-line ()
1804 (let ((kill-whole-line t))
1807 ;;; address can be a list, too
1808 (defun spam-enter-whitelist (address &optional remove)
1809 "Enter ADDRESS (list or single) into the whitelist.
1810 With a non-nil REMOVE, remove them."
1811 (interactive "sAddress: ")
1812 (spam-enter-list address spam-whitelist remove)
1813 (setq spam-whitelist-cache nil)
1814 (spam-clear-cache 'spam-use-whitelist))
1816 ;;; address can be a list, too
1817 (defun spam-enter-blacklist (address &optional remove)
1818 "Enter ADDRESS (list or single) into the blacklist.
1819 With a non-nil REMOVE, remove them."
1820 (interactive "sAddress: ")
1821 (spam-enter-list address spam-blacklist remove)
1822 (setq spam-blacklist-cache nil)
1823 (spam-clear-cache 'spam-use-whitelist))
1825 (defun spam-enter-list (addresses file &optional remove)
1826 "Enter ADDRESSES into the given FILE.
1827 Either the whitelist or the blacklist files can be used. With
1828 REMOVE not nil, remove the ADDRESSES."
1829 (if (stringp addresses)
1830 (spam-enter-list (list addresses) file remove)
1831 ;; else, we have a list of addresses here
1832 (unless (file-exists-p (file-name-directory file))
1833 (make-directory (file-name-directory file) t))
1836 (find-file-noselect file))
1837 (dolist (a addresses)
1839 (goto-char (point-min))
1840 (if (re-search-forward (regexp-quote a) nil t)
1841 ;; found the address
1843 (spam-kill-whole-line))
1844 ;; else, the address was not found
1846 (goto-char (point-max))
1852 (defun spam-filelist-build-cache (type)
1853 (let ((cache (if (eq type 'spam-use-blacklist)
1854 spam-blacklist-cache
1855 spam-whitelist-cache))
1857 (unless (gethash type spam-caches)
1859 (let ((address (pop cache)))
1860 (unless (zerop (length address)) ; 0 for a nil address too
1861 (setq address (regexp-quote address))
1862 ;; fix regexp-quote's treatment of user-intended regexes
1863 (while (string-match "\\\\\\*" address)
1864 (setq address (replace-match ".*" t t address))))
1865 (push address parsed-cache)))
1866 (puthash type parsed-cache spam-caches))))
1868 (defun spam-filelist-check-cache (type from)
1869 (when (stringp from)
1870 (spam-filelist-build-cache type)
1872 (dolist (address (gethash type spam-caches))
1873 (when (and address (string-match address from))
1878 ;;; returns t if the sender is in the whitelist, nil or
1879 ;;; spam-split-group otherwise
1880 (defun spam-check-whitelist ()
1881 ;; FIXME! Should it detect when file timestamps change?
1882 (let ((spam-split-group (if spam-split-symbolic-return
1885 (unless spam-whitelist-cache
1886 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1887 (if (spam-from-listed-p 'spam-use-whitelist)
1889 (if spam-use-whitelist-exclusive
1893 (defun spam-check-blacklist ()
1894 ;; FIXME! Should it detect when file timestamps change?
1895 (let ((spam-split-group (if spam-split-symbolic-return
1898 (unless spam-blacklist-cache
1899 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1900 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1902 (defun spam-parse-list (file)
1903 (when (file-readable-p file)
1904 (let (contents address)
1906 (insert-file-contents file)
1908 (setq address (buffer-substring (point) (point-at-eol)))
1910 ;; insert the e-mail address if detected, otherwise the raw data
1911 (unless (zerop (length address))
1912 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1913 (push (or pure-address address) contents)))))
1914 (nreverse contents))))
1916 (defun spam-from-listed-p (type)
1917 (let ((from (message-fetch-field "from"))
1919 (spam-filelist-check-cache type from)))
1921 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1922 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1923 (declassification (if blacklist 'ham 'spam))
1925 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1927 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1928 from addresses unregister-list article-unregister-list)
1929 (dolist (article articles)
1930 (let ((from (spam-fetch-field-from-fast article))
1931 (id (spam-fetch-field-message-id-fast article))
1933 (when (stringp from)
1934 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1935 (when (and (not sender-ignored)
1936 (stringp ignore-regex)
1937 (string-match ignore-regex from))
1938 (setq sender-ignored t)))
1939 ;; remember the messages we need to unregister, unless remove is set
1942 (spam-log-unregistration-needed-p
1943 id 'process declassification de-symbol))
1944 (push article article-unregister-list)
1945 (push from unregister-list))
1946 (unless sender-ignored
1947 (push from addresses)))))
1950 (funcall enter-function addresses t) ; unregister all these addresses
1951 ;; else, register normally and unregister what we need to
1952 (funcall remove-function unregister-list t)
1953 (dolist (article article-unregister-list)
1954 (spam-log-undo-registration
1955 (spam-fetch-field-message-id-fast article)
1959 (funcall enter-function addresses nil))))
1961 (defun spam-blacklist-unregister-routine (articles)
1962 (spam-blacklist-register-routine articles t))
1964 (defun spam-blacklist-register-routine (articles &optional unregister)
1965 (spam-filelist-register-routine articles t unregister))
1967 (defun spam-whitelist-unregister-routine (articles)
1968 (spam-whitelist-register-routine articles t))
1970 (defun spam-whitelist-register-routine (articles &optional unregister)
1971 (spam-filelist-register-routine articles nil unregister))
1974 ;;;; Spam-report glue
1975 (defun spam-report-gmane-register-routine (articles)
1977 (apply 'spam-report-gmane articles)))
1981 (defun spam-check-bogofilter-headers (&optional score)
1982 (let ((header (message-fetch-field spam-bogofilter-header))
1983 (spam-split-group (if spam-split-symbolic-return
1986 (when header ; return nil when no header
1987 (if score ; scoring mode
1988 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1989 (match-string 1 header)
1991 ;; spam detection mode
1992 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1994 spam-split-group)))))
1996 ;; return something sensible if the score can't be determined
1997 (defun spam-bogofilter-score ()
1998 "Get the Bogofilter spamicity score"
2000 (save-window-excursion
2001 (gnus-summary-show-article t)
2002 (set-buffer gnus-article-buffer)
2003 (let ((score (or (spam-check-bogofilter-headers t)
2004 (spam-check-bogofilter t))))
2005 (gnus-summary-show-article)
2006 (message "Spamicity score %s" score)
2009 (defun spam-check-bogofilter (&optional score)
2010 "Check the Bogofilter backend for the classification of this message"
2011 (let ((article-buffer-name (buffer-name))
2012 (db spam-bogofilter-database-directory)
2015 (let ((temp-buffer-name (buffer-name)))
2017 (set-buffer article-buffer-name)
2018 (apply 'call-process-region
2019 (point-min) (point-max)
2020 spam-bogofilter-path
2021 nil temp-buffer-name nil
2022 (if db `("-d" ,db "-v") `("-v"))))
2023 (setq return (spam-check-bogofilter-headers score))))
2026 (defun spam-bogofilter-register-with-bogofilter (articles
2028 &optional unregister)
2029 "Register an article, given as a string, as spam or non-spam."
2030 (dolist (article articles)
2031 (let ((article-string (spam-get-article-as-string article))
2032 (db spam-bogofilter-database-directory)
2033 (switch (if unregister
2035 spam-bogofilter-spam-strong-switch
2036 spam-bogofilter-ham-strong-switch)
2038 spam-bogofilter-spam-switch
2039 spam-bogofilter-ham-switch))))
2040 (when (stringp article-string)
2042 (insert article-string)
2044 (apply 'call-process-region
2045 (point-min) (point-max)
2046 spam-bogofilter-path
2048 (if db `("-d" ,db "-v") `("-v"))))))))
2050 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2051 (spam-bogofilter-register-with-bogofilter articles t unregister))
2053 (defun spam-bogofilter-unregister-spam-routine (articles)
2054 (spam-bogofilter-register-spam-routine articles t))
2056 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2057 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2059 (defun spam-bogofilter-unregister-ham-routine (articles)
2060 (spam-bogofilter-register-ham-routine articles t))
2065 (defun spam-check-spamoracle ()
2066 "Run spamoracle on an article to determine whether it's spam."
2067 (let ((article-buffer-name (buffer-name))
2068 (spam-split-group (if spam-split-symbolic-return
2072 (let ((temp-buffer-name (buffer-name)))
2074 (set-buffer article-buffer-name)
2076 (apply 'call-process-region
2077 (point-min) (point-max)
2078 spam-spamoracle-binary
2079 nil temp-buffer-name nil
2080 (if spam-spamoracle-database
2081 `("-f" ,spam-spamoracle-database "mark")
2085 (set-buffer temp-buffer-name)
2086 (goto-char (point-min))
2087 (when (re-search-forward "^X-Spam: yes;" nil t)
2089 (error "Error running spamoracle: %s" status))))))))
2091 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2092 "Run spamoracle in training mode."
2094 (let ((temp-buffer-name (buffer-name)))
2096 (goto-char (point-min))
2097 (dolist (article articles)
2098 (insert (spam-get-article-as-string article)))
2099 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2103 (apply 'call-process-region
2104 (point-min) (point-max)
2105 spam-spamoracle-binary
2106 nil temp-buffer-name nil
2107 (if spam-spamoracle-database
2108 `("-f" ,spam-spamoracle-database
2111 (unless (eq 0 status)
2112 (error "Error running spamoracle: %s" status)))))))
2114 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2115 (spam-spamoracle-learn articles nil unregister))
2117 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2118 (spam-spamoracle-learn-ham articles t))
2120 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2121 (spam-spamoracle-learn articles t unregister))
2123 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2124 (spam-spamoracle-learn-spam articles t))
2128 ;;; based mostly on the bogofilter code
2129 (defun spam-check-spamassassin-headers (&optional score)
2130 "Check the SpamAssassin headers for the classification of this message."
2131 (if score ; scoring mode
2132 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2134 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2135 (match-string 1 header)
2137 ;; spam detection mode
2138 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2139 (spam-split-group (if spam-split-symbolic-return
2142 (when header ; return nil when no header
2143 (when (string-match spam-spamassassin-positive-spam-flag-header
2145 spam-split-group)))))
2147 (defun spam-check-spamassassin (&optional score)
2148 "Check the SpamAssassin backend for the classification of this message."
2149 (let ((article-buffer-name (buffer-name)))
2151 (let ((temp-buffer-name (buffer-name)))
2153 (set-buffer article-buffer-name)
2154 (apply 'call-process-region
2155 (point-min) (point-max) spam-spamassassin-path
2156 nil temp-buffer-name nil spam-spamassassin-arguments))
2157 ;; check the return now (we're back in the temp buffer)
2158 (goto-char (point-min))
2159 (spam-check-spamassassin-headers score)))))
2161 ;; return something sensible if the score can't be determined
2162 (defun spam-spamassassin-score ()
2163 "Get the SpamAssassin score"
2165 (save-window-excursion
2166 (gnus-summary-show-article t)
2167 (set-buffer gnus-article-buffer)
2168 (let ((score (or (spam-check-spamassassin-headers t)
2169 (spam-check-spamassassin t))))
2170 (gnus-summary-show-article)
2171 (message "SpamAssassin score %s" score)
2174 (defun spam-spamassassin-register-with-sa-learn (articles spam
2175 &optional unregister)
2176 "Register articles with spamassassin's sa-learn as spam or non-spam."
2178 (let ((action (if unregister spam-sa-learn-unregister-switch
2179 (if spam spam-sa-learn-spam-switch
2180 spam-sa-learn-ham-switch)))
2181 (summary-buffer-name (buffer-name)))
2183 ;; group the articles into mbox format
2184 (dolist (article articles)
2185 (let (article-string)
2187 (set-buffer summary-buffer-name)
2188 (setq article-string (spam-get-article-as-string article)))
2189 (when (stringp article-string)
2190 (insert "From \n") ; mbox separator (sa-learn only checks the
2191 ; first five chars, so we can get away with
2193 (insert article-string)
2195 ;; call sa-learn on all messages at the same time
2196 (apply 'call-process-region
2197 (point-min) (point-max)
2199 nil nil nil "--mbox"
2200 (if spam-sa-learn-rebuild
2202 `("--no-rebuild" ,action)))))))
2204 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2205 (spam-spamassassin-register-with-sa-learn articles t unregister))
2207 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2208 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2210 (defun spam-spamassassin-unregister-spam-routine (articles)
2211 (spam-spamassassin-register-with-sa-learn articles t t))
2213 (defun spam-spamassassin-unregister-ham-routine (articles)
2214 (spam-spamassassin-register-with-sa-learn articles nil t))
2219 (defun spam-initialize (&rest symbols)
2220 "Install the spam.el hooks and do other initialization.
2221 When SYMBOLS is given, set those variables to t. This is so you
2222 can call spam-initialize before you set spam-use-* variables on
2223 explicitly, and matters only if you need the extra headers
2224 installed through spam-necessary-extra-headers."
2227 (dolist (var symbols)
2230 (dolist (header (spam-necessary-extra-headers))
2231 (add-to-list 'nnmail-extra-headers header)
2232 (add-to-list 'gnus-extra-headers header))
2234 (setq spam-install-hooks t)
2235 ;; TODO: How do we redo this every time spam-face is customized?
2236 (push '((eq mark gnus-spam-mark) . spam-face)
2237 gnus-summary-highlight)
2238 ;; Add hooks for loading and saving the spam stats
2239 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2240 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2241 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2242 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2243 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2244 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2245 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2247 (defun spam-unload-hook ()
2248 "Uninstall the spam.el hooks"
2250 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2251 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2252 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2253 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2254 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2255 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2256 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2258 (when spam-install-hooks
2263 ;;; spam.el ends here