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-bsfilter-headers nil
227 "Whether bsfilter headers should be used by `spam-split'.
228 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
232 (defcustom spam-use-bsfilter nil
233 "Whether bsfilter should be invoked by `spam-split'.
234 Enable this if you want Gnus to invoke Bsfilter on new messages."
238 (defcustom spam-use-BBDB nil
239 "Whether BBDB should be used by `spam-split'."
243 (defcustom spam-use-BBDB-exclusive nil
244 "Whether BBDB-exclusive should be used by `spam-split'.
245 Exclusive BBDB means that all messages from senders not in the BBDB are
250 (defcustom spam-use-ifile nil
251 "Whether ifile should be used by `spam-split'."
255 (defcustom spam-use-stat nil
256 "Whether `spam-stat' should be used by `spam-split'."
260 (defcustom spam-use-spamoracle nil
261 "Whether spamoracle should be used by `spam-split'."
265 (defcustom spam-use-spamassassin nil
266 "Whether spamassassin should be invoked by `spam-split'.
267 Enable this if you want Gnus to invoke SpamAssassin on new messages."
271 (defcustom spam-use-spamassassin-headers nil
272 "Whether spamassassin headers should be checked by `spam-split'.
273 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
278 (defcustom spam-install-hooks (or
283 spam-use-whitelist-exclusive
286 spam-use-regex-headers
289 spam-use-bogofilter-headers
290 spam-use-spamassassin
291 spam-use-spamassassin-headers
293 spam-use-bsfilter-headers
295 spam-use-BBDB-exclusive
299 "Whether the spam hooks should be installed.
300 Default to t if one of the spam-use-* variables is set."
304 (defcustom spam-split-group "spam"
305 "Group name where incoming spam should be put by `spam-split'."
309 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
310 ;;; not regular expressions
311 (defcustom spam-junk-mailgroups (cons
313 '("mail.junk" "poste.pourriel"))
314 "Mailgroups with spam contents.
315 All unmarked article in such group receive the spam mark on group entry."
316 :type '(repeat (string :tag "Group"))
320 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
321 "The group where spam xrefs can be found on Gmane.
322 Only meaningful if you enable `spam-use-gmane-xref'."
326 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
327 "dev.null.dk" "relays.visi.com")
328 "List of blackhole servers.
329 Only meaningful if you enable `spam-use-blackholes'."
330 :type '(repeat (string :tag "Server"))
333 (defcustom spam-blackhole-good-server-regex nil
334 "String matching IP addresses that should not be checked in the blackholes.
335 Only meaningful if you enable `spam-use-blackholes'."
336 :type '(radio (const nil)
337 (regexp :format "%t: %v\n" :size 0))
340 (defcustom spam-face 'gnus-splash-face
341 "Face for spam-marked articles."
345 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
346 "Regular expression for positive header spam matches.
347 Only meaningful if you enable `spam-use-regex-headers'."
348 :type '(repeat (regexp :tag "Regular expression to match spam header"))
351 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
352 "Regular expression for positive header ham matches.
353 Only meaningful if you enable `spam-use-regex-headers'."
354 :type '(repeat (regexp :tag "Regular expression to match ham header"))
357 (defcustom spam-regex-body-spam '()
358 "Regular expression for positive body spam matches.
359 Only meaningful if you enable `spam-use-regex-body'."
360 :type '(repeat (regexp :tag "Regular expression to match spam body"))
363 (defcustom spam-regex-body-ham '()
364 "Regular expression for positive body ham matches.
365 Only meaningful if you enable `spam-use-regex-body'."
366 :type '(repeat (regexp :tag "Regular expression to match ham body"))
369 (defgroup spam-ifile nil
370 "Spam ifile configuration."
373 (defcustom spam-ifile-path (exec-installed-p "ifile")
374 "File path of the ifile executable program."
375 :type '(choice (file :tag "Location of ifile")
376 (const :tag "ifile is not installed"))
379 (defcustom spam-ifile-database-path nil
380 "File path of the ifile database."
381 :type '(choice (file :tag "Location of the ifile database")
382 (const :tag "Use the default"))
385 (defcustom spam-ifile-spam-category "spam"
386 "Name of the spam ifile category."
390 (defcustom spam-ifile-ham-category nil
391 "Name of the ham ifile category.
392 If nil, the current group name will be used."
393 :type '(choice (string :tag "Use a fixed category")
394 (const :tag "Use the current group name"))
397 (defcustom spam-ifile-all-categories nil
398 "Whether the ifile check will return all categories, or just spam.
399 Set this to t if you want to use the `spam-split' invocation of ifile as
400 your main source of newsgroup names."
404 (defgroup spam-bogofilter nil
405 "Spam bogofilter configuration."
408 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
409 "File path of the Bogofilter executable program."
410 :type '(choice (file :tag "Location of bogofilter")
411 (const :tag "Bogofilter is not installed"))
412 :group 'spam-bogofilter)
414 (defcustom spam-bogofilter-header "X-Bogosity"
415 "The header that Bogofilter inserts in messages."
417 :group 'spam-bogofilter)
419 (defcustom spam-bogofilter-spam-switch "-s"
420 "The switch that Bogofilter uses to register spam messages."
422 :group 'spam-bogofilter)
424 (defcustom spam-bogofilter-ham-switch "-n"
425 "The switch that Bogofilter uses to register ham messages."
427 :group 'spam-bogofilter)
429 (defcustom spam-bogofilter-spam-strong-switch "-S"
430 "The switch that Bogofilter uses to unregister ham messages."
432 :group 'spam-bogofilter)
434 (defcustom spam-bogofilter-ham-strong-switch "-N"
435 "The switch that Bogofilter uses to unregister spam messages."
437 :group 'spam-bogofilter)
439 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
440 "The regex on `spam-bogofilter-header' for positive spam identification."
442 :group 'spam-bogofilter)
444 (defcustom spam-bogofilter-database-directory nil
445 "Directory path of the Bogofilter databases."
446 :type '(choice (directory
447 :tag "Location of the Bogofilter database directory")
448 (const :tag "Use the default"))
449 :group 'spam-bogofilter)
451 (defgroup spam-bsfilter nil
452 "Spam bsfilter configuration."
455 (defcustom spam-bsfilter-path (executable-find "bsfilter")
456 "File path of the Bsfilter executable program."
457 :type '(choice (file :tag "Location of bsfilter")
458 (const :tag "Bsfilter is not installed"))
459 :group 'spam-bsfilter)
461 (defcustom spam-bsfilter-header "X-Spam-Flag"
462 "The header inserted by Bsfilter to flag spam."
464 :group 'spam-bsfilter)
466 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
467 "The header that Bsfilter inserts in messages."
469 :group 'spam-bsfilter)
471 (defcustom spam-bsfilter-spam-switch "--add-spam"
472 "The switch that Bsfilter uses to register spam messages."
474 :group 'spam-bsfilter)
476 (defcustom spam-bsfilter-ham-switch "--add-ham"
477 "The switch that Bsfilter uses to register ham messages."
479 :group 'spam-bsfilter)
481 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
482 "The switch that Bsfilter uses to unregister ham messages."
484 :group 'spam-bsfilter)
486 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
487 "The switch that Bsfilter uses to unregister spam messages."
489 :group 'spam-bsfilter)
491 (defcustom spam-bsfilter-database-directory nil
492 "Directory path of the Bsfilter databases."
493 :type '(choice (directory
494 :tag "Location of the Bsfilter database directory")
495 (const :tag "Use the default"))
496 :group 'spam-bsfilter)
498 (defgroup spam-spamoracle nil
499 "Spam spamoracle configuration."
502 (defcustom spam-spamoracle-database nil
503 "Location of spamoracle database file.
504 When nil, use the default spamoracle database."
505 :type '(choice (directory :tag "Location of spamoracle database file.")
506 (const :tag "Use the default"))
507 :group 'spam-spamoracle)
509 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
510 "Location of the spamoracle binary."
511 :type '(choice (directory :tag "Location of the spamoracle binary")
512 (const :tag "Use the default"))
513 :group 'spam-spamoracle)
515 (defgroup spam-spamassassin nil
516 "Spam SpamAssassin configuration."
519 (defcustom spam-spamassassin-path (executable-find "spamassassin")
520 "File path of the spamassassin executable program.
521 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
522 spamd man pages for more information on these programs."
523 :type '(choice (file :tag "Location of spamc")
524 (const :tag "spamassassin is not installed"))
525 :group 'spam-spamassassin)
527 (defcustom spam-spamassassin-arguments ()
528 "Arguments to pass to the spamassassin executable.
529 This must be a list. For example, `(\"-C\" \"configfile\")'."
530 :type '(restricted-sexp :match-alternatives (listp))
531 :group 'spam-spamassassin)
533 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
534 "The header inserted by SpamAssassin to flag spam."
536 :group 'spam-spamassassin)
538 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
539 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
542 :group 'spam-spamassassin)
544 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
545 "The header inserted by SpamAssassin, giving extended scoring information"
547 :group 'spam-spamassassin)
549 (defcustom spam-sa-learn-path (executable-find "sa-learn")
550 "File path of the sa-learn executable program."
551 :type '(choice (file :tag "Location of spamassassin")
552 (const :tag "spamassassin is not installed"))
553 :group 'spam-spamassassin)
555 (defcustom spam-sa-learn-rebuild t
556 "Whether sa-learn should rebuild the database every time it is called
557 Enable this if you want sa-learn to rebuild the database automatically. Doing
558 this will slightly increase the running time of the spam registration process.
559 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
560 order for SpamAssassin to recognize the new registered spam."
562 :group 'spam-spamassassin)
564 (defcustom spam-sa-learn-spam-switch "--spam"
565 "The switch that sa-learn uses to register spam messages"
567 :group 'spam-spamassassin)
569 (defcustom spam-sa-learn-ham-switch "--ham"
570 "The switch that sa-learn uses to register ham messages"
572 :group 'spam-spamassassin)
574 (defcustom spam-sa-learn-unregister-switch "--forget"
575 "The switch that sa-learn uses to unregister messages messages"
577 :group 'spam-spamassassin)
579 ;;; Key bindings for spam control.
581 (gnus-define-keys gnus-summary-mode-map
582 "St" spam-generic-score
583 "Sx" gnus-summary-mark-as-spam
584 "Mst" spam-generic-score
585 "Msx" gnus-summary-mark-as-spam
586 "\M-d" gnus-summary-mark-as-spam)
588 (defvar spam-cache-lookups t
589 "Whether spam.el will try to cache lookups using `spam-caches'.")
591 (defvar spam-caches (make-hash-table
594 "Cache of spam detection entries.")
596 (defvar spam-old-ham-articles nil
597 "List of old ham articles, generated when a group is entered.")
599 (defvar spam-old-spam-articles nil
600 "List of old spam articles, generated when a group is entered.")
602 (defvar spam-split-disabled nil
603 "If non-nil, `spam-split' is disabled, and always returns nil.")
605 (defvar spam-split-last-successful-check nil
607 `spam-split' will set this to nil or a spam-use-XYZ check if it
610 ;; convenience functions
611 (defun spam-clear-cache (symbol)
612 "Clear the spam-caches entry for a check."
613 (remhash symbol spam-caches))
615 (defun spam-xor (a b)
617 (and (or a b) (not (and a b))))
619 (defun spam-group-ham-mark-p (group mark &optional spam)
620 "Checks if MARK is considered a ham mark in GROUP."
621 (when (stringp group)
622 (let* ((marks (spam-group-ham-marks group spam))
623 (marks (if (symbolp mark)
625 (mapcar 'symbol-value marks))))
628 (defun spam-group-spam-mark-p (group mark)
629 "Checks if MARK is considered a spam mark in GROUP."
630 (spam-group-ham-mark-p group mark t))
632 (defun spam-group-ham-marks (group &optional spam)
633 "In GROUP, get all the ham marks."
634 (when (stringp group)
635 (let* ((marks (if spam
636 (gnus-parameter-spam-marks group)
637 (gnus-parameter-ham-marks group)))
639 (marks (if (listp (car marks)) (car marks) marks)))
642 (defun spam-group-spam-marks (group)
643 "In GROUP, get all the spam marks."
644 (spam-group-ham-marks group t))
646 (defun spam-group-spam-contents-p (group)
647 "Is GROUP a spam group?"
649 (or (member group spam-junk-mailgroups)
650 (memq 'gnus-group-spam-classification-spam
651 (gnus-parameter-spam-contents group)))
654 (defun spam-group-ham-contents-p (group)
655 "Is GROUP a ham group?"
657 (memq 'gnus-group-spam-classification-ham
658 (gnus-parameter-spam-contents group))
661 (defvar spam-list-of-processors
662 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
663 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
664 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
665 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
666 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
667 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
668 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
669 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
670 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
671 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
672 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
673 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
674 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
675 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
676 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
677 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
678 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
679 "The `spam-list-of-processors' list.
680 This list contains pairs associating a ham/spam exit processor
681 variable with a classification and a spam-use-* variable.")
683 (defun spam-group-processor-p (group processor)
684 (if (and (stringp group)
686 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
687 (spam-group-processor-multiple-p
689 (cdr-safe (assoc processor spam-list-of-processors))))
692 (defun spam-group-processor-multiple-p (group processor-info)
693 (let* ((classification (nth 0 processor-info))
694 (check (nth 1 processor-info))
695 (parameters (nth 0 (gnus-parameter-spam-process group)))
697 (dolist (parameter parameters)
698 (when (and (null found)
700 (eq classification (nth 0 parameter))
701 (eq check (nth 1 parameter)))
705 (defun spam-group-spam-processor-report-gmane-p (group)
706 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
708 (defun spam-group-spam-processor-bogofilter-p (group)
709 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
711 (defun spam-group-spam-processor-blacklist-p (group)
712 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
714 (defun spam-group-spam-processor-ifile-p (group)
715 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
717 (defun spam-group-ham-processor-ifile-p (group)
718 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
720 (defun spam-group-spam-processor-spamoracle-p (group)
721 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
723 (defun spam-group-ham-processor-bogofilter-p (group)
724 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
726 (defun spam-group-spam-processor-stat-p (group)
727 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
729 (defun spam-group-ham-processor-stat-p (group)
730 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
732 (defun spam-group-ham-processor-whitelist-p (group)
733 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
735 (defun spam-group-ham-processor-BBDB-p (group)
736 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
738 (defun spam-group-ham-processor-copy-p (group)
739 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
741 (defun spam-group-ham-processor-spamoracle-p (group)
742 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
744 (defun spam-report-articles-gmane (n)
745 "Report the current message as spam.
746 Respects the process/prefix convention."
748 (dolist (article (gnus-summary-work-articles n))
749 (gnus-summary-remove-process-mark article)
750 (spam-report-gmane article)))
752 (defun spam-necessary-extra-headers ()
753 "Return the extra headers spam.el thinks are necessary."
755 (when (or spam-use-spamassassin
756 spam-use-spamassassin-headers
757 spam-use-regex-headers)
758 (push 'X-Spam-Status list))
761 (defun spam-user-format-function-S (headers)
763 (spam-summary-score headers)))
765 (defun spam-article-sort-by-spam-status (h1 h2)
766 "Sort articles by score."
768 (dolist (header (spam-necessary-extra-headers))
769 (let ((s1 (spam-summary-score h1 header))
770 (s2 (spam-summary-score h2 header)))
772 (setq result (< s1 s2))
776 (defun spam-extra-header-to-number (header headers)
777 "Transform an extra header to a number."
778 (if (gnus-extra-header header headers)
780 ((eq header 'X-Spam-Status)
781 (string-to-number (gnus-replace-in-string
782 (gnus-extra-header header headers)
787 (defun spam-summary-score (headers &optional specific-header)
788 "Score an article for the summary buffer, as fast as possible.
789 With SPECIFIC-HEADER, returns only that header's score.
790 Will not return a nil score."
794 (list specific-header)
795 (spam-necessary-extra-headers)))
797 (spam-extra-header-to-number header headers))
802 (defun spam-generic-score (&optional recheck)
803 "Invoke whatever scoring method we can."
806 ((or spam-use-spamassassin spam-use-spamassassin-headers)
807 (spam-spamassassin-score recheck))
808 ((or spam-use-bsfilter spam-use-bsfilter-headers)
809 (spam-bsfilter-score recheck))
810 (t (spam-bogofilter-score recheck))))
812 ;;; Summary entry and exit processing.
814 (defun spam-summary-prepare ()
815 (setq spam-old-ham-articles
816 (spam-list-articles gnus-newsgroup-articles 'ham))
817 (setq spam-old-spam-articles
818 (spam-list-articles gnus-newsgroup-articles 'spam))
819 (spam-mark-junk-as-spam-routine))
821 ;; The spam processors are invoked for any group, spam or ham or neither
822 (defun spam-summary-prepare-exit ()
823 (unless gnus-group-is-exiting-without-update-p
824 (gnus-message 6 "Exiting summary buffer and applying spam rules")
826 ;; first of all, unregister any articles that are no longer ham or spam
827 ;; we have to iterate over the processors, or else we'll be too slow
828 (dolist (classification '(spam ham))
829 (let* ((old-articles (if (eq classification 'spam)
830 spam-old-spam-articles
831 spam-old-ham-articles))
832 (new-articles (spam-list-articles
833 gnus-newsgroup-articles
835 (changed-articles (spam-set-difference new-articles old-articles)))
836 ;; now that we have the changed articles, we go through the processors
837 (dolist (processor-param spam-list-of-processors)
838 (let ((processor (nth 0 processor-param))
839 (processor-classification (nth 1 processor-param))
840 (check (nth 2 processor-param))
842 (dolist (article changed-articles)
843 (let ((id (spam-fetch-field-message-id-fast article)))
844 (when (spam-log-unregistration-needed-p
845 id 'process classification check)
846 (push article unregister-list))))
847 ;; call spam-register-routine with specific articles to unregister,
848 ;; when there are articles to unregister and the check is enabled
849 (when (and unregister-list (symbol-value check))
850 (spam-register-routine
851 classification check t unregister-list))))))
853 ;; find all the spam processors applicable to this group
854 (dolist (processor-param spam-list-of-processors)
855 (let ((processor (nth 0 processor-param))
856 (classification (nth 1 processor-param))
857 (check (nth 2 processor-param)))
858 (when (and (eq 'spam classification)
859 (spam-group-processor-p gnus-newsgroup-name processor))
860 (spam-register-routine classification check))))
862 (unless (and spam-move-spam-nonspam-groups-only
863 (spam-group-spam-contents-p gnus-newsgroup-name))
864 (when (< 0 (length (spam-list-articles
865 gnus-newsgroup-articles
867 (gnus-message 6 "Marking spam as expired and moving it to %s"
868 (gnus-parameter-spam-process-destination
869 gnus-newsgroup-name))
870 (spam-mark-spam-as-expired-and-move-routine
871 (gnus-parameter-spam-process-destination gnus-newsgroup-name))))
873 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
874 ;; expire spam, in case the above did not expire them
875 (when (< 0 (length (spam-list-articles
876 gnus-newsgroup-articles
878 (gnus-message 6 "Marking spam as expired without moving it")
879 (spam-mark-spam-as-expired-and-move-routine nil))
881 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
882 (and (spam-group-spam-contents-p gnus-newsgroup-name)
883 spam-process-ham-in-spam-groups)
884 spam-process-ham-in-nonham-groups)
885 ;; find all the ham processors applicable to this group
886 (dolist (processor-param spam-list-of-processors)
887 (let ((processor (nth 0 processor-param))
888 (classification (nth 1 processor-param))
889 (check (nth 2 processor-param)))
890 (when (and (eq 'ham classification)
891 (spam-group-processor-p gnus-newsgroup-name processor))
892 (spam-register-routine classification check)))))
894 (when (< 0 (length (spam-list-articles
895 gnus-newsgroup-articles
897 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
898 (gnus-message 6 "Copying ham")
899 (spam-ham-copy-routine
900 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
902 ;; now move all ham articles out of spam groups
903 (when (spam-group-spam-contents-p gnus-newsgroup-name)
904 (gnus-message 6 "Moving ham messages from spam group")
905 (spam-ham-move-routine
906 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
908 (setq spam-old-ham-articles nil)
909 (setq spam-old-spam-articles nil))
911 (defun spam-set-difference (list1 list2)
912 "Return a set difference of LIST1 and LIST2.
913 When either list is nil, the other is returned."
914 (if (and list1 list2)
915 ;; we have two non-nil lists
917 (dolist (item (append list1 list2))
918 (when (and (memq item list1) (memq item list2))
919 (setq list1 (delq item list1))
920 (setq list2 (delq item list2))))
921 (append list1 list2))
922 ;; if either of the lists was nil, return the other one
923 (if list1 list1 list2)))
925 (defun spam-mark-junk-as-spam-routine ()
926 ;; check the global list of group names spam-junk-mailgroups and the
928 (when (spam-group-spam-contents-p gnus-newsgroup-name)
929 (gnus-message 6 "Marking %s articles as spam"
930 (if spam-mark-only-unseen-as-spam
933 (let ((articles (if spam-mark-only-unseen-as-spam
934 gnus-newsgroup-unseen
935 gnus-newsgroup-unreads)))
936 (dolist (article articles)
937 (gnus-summary-mark-article article gnus-spam-mark)))))
939 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
940 (if (and (car-safe groups) (listp (car-safe groups)))
941 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
942 (gnus-summary-kill-process-mark)
943 (let ((articles gnus-newsgroup-articles)
944 (backend-supports-deletions
945 (gnus-check-backend-function
946 'request-move-article gnus-newsgroup-name))
947 article tomove deletep)
948 (dolist (article articles)
949 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
950 (gnus-summary-mark-article article gnus-expirable-mark)
951 (push article tomove)))
953 ;; now do the actual copies
954 (dolist (group groups)
957 (dolist (article tomove)
958 (gnus-summary-set-process-mark article))
960 (if (or (not backend-supports-deletions)
961 (> (length groups) 1))
963 (gnus-summary-copy-article nil group)
965 (gnus-summary-move-article nil group)))))
967 ;; now delete the articles, if there was a copy done, and the
969 (when (and deletep backend-supports-deletions)
970 (dolist (article tomove)
971 (gnus-summary-set-process-mark article))
973 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
974 (gnus-summary-delete-article nil))))
976 (gnus-summary-yank-process-mark))))
978 (defun spam-ham-copy-or-move-routine (copy groups)
979 (gnus-summary-kill-process-mark)
980 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
981 (backend-supports-deletions
982 (gnus-check-backend-function
983 'request-move-article gnus-newsgroup-name))
984 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
985 article mark todo deletep respool)
987 (when (member 'respool groups)
988 (setq respool t) ; boolean for later
989 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
991 ;; now do the actual move
992 (dolist (group groups)
993 (when (and todo (stringp group))
994 (dolist (article todo)
995 (when spam-mark-ham-unread-before-move-from-spam-group
996 (gnus-summary-mark-article article gnus-unread-mark))
997 (gnus-summary-set-process-mark article))
999 (if respool ; respooling is with a "fake" group
1000 (let ((spam-split-disabled
1001 (or spam-split-disabled
1002 spam-disable-spam-split-during-ham-respool)))
1003 (gnus-summary-respool-article nil respool-method))
1004 (if (or (not backend-supports-deletions) ; else, we are not respooling
1005 (> (length groups) 1))
1006 (progn ; if copying, copy and set deletep
1007 (gnus-summary-copy-article nil group)
1009 (gnus-summary-move-article nil group))))) ; else move articles
1011 ;; now delete the articles, unless a) copy is t, and there was a copy done
1012 ;; b) a move was done to a single group
1013 ;; c) backend-supports-deletions is nil
1015 (when (and deletep backend-supports-deletions)
1016 (dolist (article todo)
1017 (gnus-summary-set-process-mark article))
1019 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1020 (gnus-summary-delete-article nil))))))
1022 (gnus-summary-yank-process-mark))
1024 (defun spam-ham-copy-routine (&rest groups)
1025 (if (and (car-safe groups) (listp (car-safe groups)))
1026 (apply 'spam-ham-copy-routine (car groups))
1027 (spam-ham-copy-or-move-routine t groups)))
1029 (defun spam-ham-move-routine (&rest groups)
1030 (if (and (car-safe groups) (listp (car-safe groups)))
1031 (apply 'spam-ham-move-routine (car groups))
1032 (spam-ham-copy-or-move-routine nil groups)))
1034 (defun spam-get-article-as-string (article)
1035 (when (numberp article)
1037 (gnus-request-article-this-buffer
1039 gnus-newsgroup-name)
1043 ;; (defun spam-get-article-as-filename (article)
1044 ;; (let ((article-filename))
1045 ;; (when (numberp article)
1046 ;; (nnml-possibly-change-directory
1047 ;; (gnus-group-real-name gnus-newsgroup-name))
1048 ;; (setq article-filename (expand-file-name
1049 ;; (int-to-string article) nnml-current-directory)))
1050 ;; (if (file-exists-p article-filename)
1054 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1055 "Fetch a field quickly, using the internal gnus-data-list function"
1056 (when (numberp article)
1057 (let* ((data-header (or prepared-data-header
1058 (spam-fetch-article-header article))))
1059 (if (arrayp data-header)
1061 ((equal field 'from)
1062 (mail-header-from data-header))
1063 ((equal field 'message-id)
1064 (mail-header-message-id data-header))
1065 ((equal field 'subject)
1066 (mail-header-subject data-header))
1067 ((equal field 'references)
1068 (mail-header-references data-header))
1069 ((equal field 'date)
1070 (mail-header-date data-header))
1071 ((equal field 'xref)
1072 (mail-header-xref data-header))
1073 ((equal field 'extra)
1074 (mail-header-extra data-header))
1077 (gnus-message 6 "Article %d has a nil data header" article)))))
1079 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1080 (spam-fetch-field-fast article 'from prepared-data-header))
1082 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1083 (spam-fetch-field-fast article 'subject prepared-data-header))
1085 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1086 (spam-fetch-field-fast article 'message-id prepared-data-header))
1088 (defun spam-generate-fake-headers (article)
1089 (let ((dh (spam-fetch-article-header article)))
1093 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1094 "Date: %s\nReferences: %s\nXref: %s\n")
1095 (spam-fetch-field-fast article 'from dh)
1096 (spam-fetch-field-fast article 'subject dh)
1097 (spam-fetch-field-fast article 'message-id dh)
1098 (spam-fetch-field-fast article 'date dh)
1099 (spam-fetch-field-fast article 'references dh)
1100 (spam-fetch-field-fast article 'xref dh))
1101 (when (spam-fetch-field-fast article 'extra dh)
1102 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1105 "spam-generate-fake-headers: article %d didn't have a valid header"
1108 (defun spam-fetch-article-header (article)
1110 (set-buffer gnus-summary-buffer)
1111 (gnus-read-header article)
1112 (nth 3 (assq article gnus-newsgroup-data))))
1115 ;;;; Spam determination.
1117 (defvar spam-list-of-checks
1118 '((spam-use-blacklist . spam-check-blacklist)
1119 (spam-use-regex-headers . spam-check-regex-headers)
1120 (spam-use-gmane-xref . spam-check-gmane-xref)
1121 (spam-use-regex-body . spam-check-regex-body)
1122 (spam-use-whitelist . spam-check-whitelist)
1123 (spam-use-BBDB . spam-check-BBDB)
1124 (spam-use-BBDB-exclusive . spam-check-BBDB)
1125 (spam-use-ifile . spam-check-ifile)
1126 (spam-use-spamoracle . spam-check-spamoracle)
1127 (spam-use-stat . spam-check-stat)
1128 (spam-use-blackholes . spam-check-blackholes)
1129 (spam-use-hashcash . spam-check-hashcash)
1130 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1131 (spam-use-spamassassin . spam-check-spamassassin)
1132 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1133 (spam-use-bogofilter . spam-check-bogofilter)
1134 (spam-use-bsfilter-headers . spam-check-bsfilter-headers)
1135 (spam-use-bsfilter . spam-check-bsfilter))
1136 "The spam-list-of-checks list contains pairs associating a
1137 parameter variable with a spam checking function. If the
1138 parameter variable is true, then the checking function is called,
1139 and its value decides what happens. Each individual check may
1140 return nil, t, or a mailgroup name. The value nil means that the
1141 check does not yield a decision, and so, that further checks are
1142 needed. The value t means that the message is definitely not
1143 spam, and that further spam checks should be inhibited.
1144 Otherwise, a mailgroup name or the symbol 'spam (depending on
1145 spam-split-symbolic-return) is returned where the mail should go,
1146 and further checks are also inhibited. The usual mailgroup name
1147 is the value of `spam-split-group', meaning that the message is
1148 definitely a spam.")
1150 (defvar spam-list-of-statistical-checks
1157 spam-use-spamassassin
1158 spam-use-spamoracle)
1159 "The spam-list-of-statistical-checks list contains all the mail
1160 splitters that need to have the full message body available.
1161 Note that you should fetch extra headers if you don't like this,
1162 e.g. fetch the 'Received' header for spam-use-blackholes.")
1164 (defun spam-split (&rest specific-checks)
1165 "Split this message into the `spam' group if it is spam.
1166 This function can be used as an entry in the variable `nnmail-split-fancy',
1167 for example like this: (: spam-split). It can take checks as
1168 parameters. A string as a parameter will set the
1169 spam-split-group to that string.
1171 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1173 (setq spam-split-last-successful-check nil)
1174 (unless spam-split-disabled
1175 (let ((spam-split-group-choice spam-split-group))
1176 (dolist (check specific-checks)
1177 (when (stringp check)
1178 (setq spam-split-group-choice check)
1179 (setq specific-checks (delq check specific-checks))))
1181 (let ((spam-split-group spam-split-group-choice))
1184 (dolist (check spam-list-of-statistical-checks)
1185 (when (and (symbolp check)
1186 (or (symbol-value check)
1187 (memq check specific-checks)))
1189 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1190 (symbol-name check))
1192 ;; (progn (widen) (debug (buffer-string)))
1193 (let ((list-of-checks spam-list-of-checks)
1195 (while (and list-of-checks (not decision))
1196 (let ((pair (pop list-of-checks)))
1198 ;; either, given specific checks, this is one of them
1199 (and specific-checks (memq (car pair) specific-checks))
1200 ;; or, given no specific checks, spam-use-CHECK is set
1201 (and (null specific-checks) (symbol-value (car pair))))
1202 (gnus-message 6 "spam-split: calling the %s function"
1203 (symbol-name (cdr pair)))
1204 (setq decision (funcall (cdr pair)))
1205 ;; if we got a decision at all, save the current check
1207 (setq spam-split-last-successful-check (car pair)))
1209 (when (eq decision 'spam)
1210 (unless spam-split-symbolic-return
1213 (format "spam-split got %s but %s is nil"
1214 (symbol-name decision)
1215 (symbol-name spam-split-symbolic-return))))))))
1217 (if spam-split-symbolic-return-positive 'ham nil)
1220 (defun spam-find-spam ()
1221 "This function will detect spam in the current newsgroup using spam-split."
1224 (let* ((group gnus-newsgroup-name)
1225 (autodetect (gnus-parameter-spam-autodetect group))
1226 (methods (gnus-parameter-spam-autodetect-methods group))
1227 (first-method (nth 0 methods))
1228 (articles (if spam-autodetect-recheck-messages
1229 gnus-newsgroup-articles
1230 gnus-newsgroup-unseen))
1231 article-cannot-be-faked)
1233 (dolist (check spam-list-of-statistical-checks)
1234 (when (and (symbolp check)
1235 (memq check methods))
1236 (setq article-cannot-be-faked t)
1239 (when (memq 'default methods)
1240 (setq article-cannot-be-faked t))
1242 (when (and autodetect
1243 (not (equal first-method 'none)))
1246 (let ((id (spam-fetch-field-message-id-fast article))
1247 (subject (spam-fetch-field-subject-fast article))
1248 (sender (spam-fetch-field-from-fast article))
1252 (gnus-message 6 "Article %d has no message ID!" article))
1254 (when (and id spam-log-to-registry)
1255 (setq registry-lookup (spam-log-registration-type id 'incoming))
1256 (when registry-lookup
1259 "spam-find-spam: message %s was already registered incoming"
1262 (let* ((spam-split-symbolic-return t)
1263 (spam-split-symbolic-return-positive t)
1264 (fake-headers (spam-generate-fake-headers article))
1268 (if article-cannot-be-faked
1269 (gnus-request-article-this-buffer
1272 ;; else, we fake the article
1273 (when fake-headers (insert fake-headers)))
1274 (if (or (null first-method)
1275 (equal first-method 'default))
1277 (apply 'spam-split methods))))))
1278 (if (equal split-return 'spam)
1279 (gnus-summary-mark-article article gnus-spam-mark))
1281 (when (and id split-return spam-log-to-registry)
1282 (when (zerop (gnus-registry-group-count id))
1283 (gnus-registry-add-group
1284 id group subject sender))
1286 (unless registry-lookup
1287 (spam-log-processing-to-registry
1291 spam-split-last-successful-check
1295 (defvar spam-registration-functions
1296 ;; first the ham register, second the spam register function
1297 ;; third the ham unregister, fourth the spam unregister function
1298 '((spam-use-blacklist nil
1299 spam-blacklist-register-routine
1301 spam-blacklist-unregister-routine)
1302 (spam-use-whitelist spam-whitelist-register-routine
1304 spam-whitelist-unregister-routine
1306 (spam-use-ham-copy nil
1310 (spam-use-BBDB spam-BBDB-register-routine
1312 spam-BBDB-unregister-routine
1314 (spam-use-ifile spam-ifile-register-ham-routine
1315 spam-ifile-register-spam-routine
1316 spam-ifile-unregister-ham-routine
1317 spam-ifile-unregister-spam-routine)
1318 (spam-use-spamoracle spam-spamoracle-learn-ham
1319 spam-spamoracle-learn-spam
1320 spam-spamoracle-unlearn-ham
1321 spam-spamoracle-unlearn-spam)
1322 (spam-use-stat spam-stat-register-ham-routine
1323 spam-stat-register-spam-routine
1324 spam-stat-unregister-ham-routine
1325 spam-stat-unregister-spam-routine)
1326 ;; note that spam-use-gmane is not a legitimate check
1328 spam-report-gmane-register-routine
1329 ;; does Gmane support unregistration?
1332 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1333 spam-spamassassin-register-spam-routine
1334 spam-spamassassin-unregister-ham-routine
1335 spam-spamassassin-unregister-spam-routine)
1336 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1337 spam-bogofilter-register-spam-routine
1338 spam-bogofilter-unregister-ham-routine
1339 spam-bogofilter-unregister-spam-routine)
1340 (spam-use-bsfilter spam-bsfilter-register-ham-routine
1341 spam-bsfilter-register-spam-routine
1342 spam-bsfilter-unregister-ham-routine
1343 spam-bsfilter-unregister-spam-routine))
1344 "The spam-registration-functions list contains pairs
1345 associating a parameter variable with the ham and spam
1346 registration functions, and the ham and spam unregistration
1349 (defun spam-classification-valid-p (classification)
1350 (or (eq classification 'spam)
1351 (eq classification 'ham)))
1353 (defun spam-process-type-valid-p (process-type)
1354 (or (eq process-type 'incoming)
1355 (eq process-type 'process)))
1357 (defun spam-registration-check-valid-p (check)
1358 (assoc check spam-registration-functions))
1360 (defun spam-unregistration-check-valid-p (check)
1361 (assoc check spam-registration-functions))
1363 (defun spam-registration-function (classification check)
1364 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1365 (if (eq classification 'spam)
1369 (defun spam-unregistration-function (classification check)
1370 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1371 (if (eq classification 'spam)
1375 (defun spam-list-articles (articles classification)
1376 (let ((mark-check (if (eq classification 'spam)
1377 'spam-group-spam-mark-p
1378 'spam-group-ham-mark-p))
1379 alist mark-cache-yes mark-cache-no)
1380 (dolist (article articles)
1381 (let ((mark (gnus-summary-article-mark article)))
1382 (unless (or (memq mark mark-cache-yes)
1383 (memq mark mark-cache-no))
1384 (if (funcall mark-check
1387 (push mark mark-cache-yes)
1388 (push mark mark-cache-no)))
1389 (when (memq mark mark-cache-yes)
1390 (push article alist))))
1393 (defun spam-register-routine (classification
1395 &optional unregister
1397 (when (and (spam-classification-valid-p classification)
1398 (spam-registration-check-valid-p check))
1399 (let* ((register-function
1400 (spam-registration-function classification check))
1401 (unregister-function
1402 (spam-unregistration-function classification check))
1403 (run-function (if unregister
1406 (log-function (if unregister
1407 'spam-log-undo-registration
1408 'spam-log-processing-to-registry))
1412 ;; make list of articles, using specific-articles if given
1413 (setq articles (or specific-articles
1415 gnus-newsgroup-articles
1418 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1419 (if unregister "Unregistering" "Registering")
1421 (if specific-articles "specific" "")
1422 (symbol-name classification)
1423 (symbol-name check))
1424 (funcall run-function articles)
1425 ;; now log all the registrations (or undo them, depending on unregister)
1426 (dolist (article articles)
1427 (funcall log-function
1428 (spam-fetch-field-message-id-fast article)
1432 gnus-newsgroup-name))))))
1434 ;;; log a ham- or spam-processor invocation to the registry
1435 (defun spam-log-processing-to-registry (id type classification check group)
1436 (when spam-log-to-registry
1437 (if (and (stringp id)
1439 (spam-process-type-valid-p type)
1440 (spam-classification-valid-p classification)
1441 (spam-registration-check-valid-p check))
1442 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1443 (cell (list classification check group)))
1444 (push cell cell-list)
1445 (gnus-registry-store-extra-entry
1452 (format "%s call with bad ID, type, classification, spam-check, or group"
1453 "spam-log-processing-to-registry")))))
1455 ;;; check if a ham- or spam-processor registration has been done
1456 (defun spam-log-registered-p (id type)
1457 (when spam-log-to-registry
1458 (if (and (stringp id)
1459 (spam-process-type-valid-p type))
1460 (cdr-safe (gnus-registry-fetch-extra id type))
1464 (format "%s called with bad ID, type, classification, or spam-check"
1465 "spam-log-registered-p"))
1468 ;;; check what a ham- or spam-processor registration says
1469 ;;; returns nil if conflicting registrations are found
1470 (defun spam-log-registration-type (id type)
1473 (dolist (reg (spam-log-registered-p id type))
1474 (let ((classification (nth 0 reg)))
1475 (when (spam-classification-valid-p classification)
1477 (not (eq classification decision)))
1478 (setq count (+ 1 count)))
1479 (setq decision classification))))
1485 ;;; check if a ham- or spam-processor registration needs to be undone
1486 (defun spam-log-unregistration-needed-p (id type classification check)
1487 (when spam-log-to-registry
1488 (if (and (stringp id)
1489 (spam-process-type-valid-p type)
1490 (spam-classification-valid-p classification)
1491 (spam-registration-check-valid-p check))
1492 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1494 (dolist (cell cell-list)
1496 (when (and (eq classification (nth 0 cell))
1497 (eq check (nth 1 cell)))
1503 (format "%s called with bad ID, type, classification, or spam-check"
1504 "spam-log-unregistration-needed-p"))
1508 ;;; undo a ham- or spam-processor registration (the group is not used)
1509 (defun spam-log-undo-registration (id type classification check &optional group)
1510 (when (and spam-log-to-registry
1511 (spam-log-unregistration-needed-p id type classification check))
1512 (if (and (stringp id)
1513 (spam-process-type-valid-p type)
1514 (spam-classification-valid-p classification)
1515 (spam-registration-check-valid-p check))
1516 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1517 new-cell-list found)
1518 (dolist (cell cell-list)
1519 (unless (and (eq classification (nth 0 cell))
1520 (eq check (nth 1 cell)))
1521 (push cell new-cell-list)))
1522 (gnus-registry-store-extra-entry
1527 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1528 "spam-log-undo-registration"))
1531 ;;; set up IMAP widening if it's necessary
1532 (defun spam-setup-widening ()
1533 (dolist (check spam-list-of-statistical-checks)
1534 (when (symbol-value check)
1535 (setq nnimap-split-download-body-default t))))
1539 (defun spam-check-gmane-xref ()
1541 (message-fetch-field "Xref")
1542 (message-fetch-field "Newsgroups")))
1543 (spam-split-group (if spam-split-symbolic-return
1546 (when header ; return nil when no header
1547 (when (string-match spam-gmane-xref-spam-group
1549 spam-split-group))))
1554 (defun spam-check-regex-body ()
1555 (let ((spam-regex-headers-ham spam-regex-body-ham)
1556 (spam-regex-headers-spam spam-regex-body-spam))
1557 (spam-check-regex-headers t)))
1562 (defun spam-check-regex-headers (&optional body)
1563 (let ((type (if body "body" "header"))
1564 (spam-split-group (if spam-split-symbolic-return
1568 (dolist (h-regex spam-regex-headers-ham)
1570 (goto-char (point-min))
1571 (when (re-search-forward h-regex nil t)
1572 (message "Ham regex %s search positive." type)
1574 (dolist (s-regex spam-regex-headers-spam)
1576 (goto-char (point-min))
1577 (when (re-search-forward s-regex nil t)
1578 (message "Spam regex %s search positive." type)
1580 (setq ret spam-split-group))))
1586 (defun spam-reverse-ip-string (ip)
1588 (mapconcat 'identity
1589 (nreverse (split-string ip "\\."))
1592 (defun spam-check-blackholes ()
1593 "Check the Received headers for blackholed relays."
1594 (let ((headers (message-fetch-field "received"))
1595 (spam-split-group (if spam-split-symbolic-return
1602 (goto-char (point-min))
1603 (gnus-message 6 "Checking headers for relay addresses")
1604 (while (re-search-forward
1605 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1606 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1607 (push (spam-reverse-ip-string (match-string 1))
1609 (dolist (server spam-blackhole-servers)
1611 (unless (and spam-blackhole-good-server-regex
1612 ;; match the good-server-regex against the reversed (again) IP string
1614 spam-blackhole-good-server-regex
1615 (spam-reverse-ip-string ip)))
1617 (let ((query-string (concat ip "." server)))
1619 (let ((query-result (query-dig query-string)))
1621 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1623 (push (list ip server query-result)
1625 ;; else, if not using dig.el
1626 (when (query-dns query-string)
1627 (gnus-message 6 "positive blackhole check")
1628 (push (list ip server (query-dns query-string 'TXT))
1639 (defun spam-check-hashcash ()
1640 "Check the headers for hashcash payments."
1641 (mail-check-payment))) ;mail-check-payment returns a boolean
1644 (defalias 'mail-check-payment 'ignore)
1645 (defalias 'spam-check-hashcash 'ignore))))
1649 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1650 ;;; <sacha@giotto.sj.ru>
1652 ;; all this is done inside a condition-case to trap errors
1659 ;; when the BBDB changes, we want to clear out our cache
1660 (defun spam-clear-cache-BBDB (&rest immaterial)
1661 (spam-clear-cache 'spam-use-BBDB))
1663 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1665 (defun spam-enter-ham-BBDB (addresses &optional remove)
1666 "Enter an address into the BBDB; implies ham (non-spam) sender"
1667 (dolist (from addresses)
1668 (when (stringp from)
1669 (let* ((parsed-address (gnus-extract-address-components from))
1670 (name (or (nth 0 parsed-address) "Ham Sender"))
1671 (remove-function (if remove
1672 'bbdb-delete-record-internal
1674 (net-address (nth 1 parsed-address))
1675 (record (and net-address
1676 (bbdb-search-simple nil net-address))))
1678 (gnus-message 6 "%s address %s %s BBDB"
1679 (if remove "Deleting" "Adding")
1681 (if remove "from" "to"))
1683 (funcall remove-function record)
1684 (bbdb-create-internal name nil net-address nil nil
1685 "ham sender added by spam.el")))))))
1687 (defun spam-BBDB-register-routine (articles &optional unregister)
1689 (dolist (article articles)
1690 (when (stringp (spam-fetch-field-from-fast article))
1691 (push (spam-fetch-field-from-fast article) addresses)))
1692 ;; now do the register/unregister action
1693 (spam-enter-ham-BBDB addresses unregister)))
1695 (defun spam-BBDB-unregister-routine (articles)
1696 (spam-BBDB-register-routine articles t))
1698 (defun spam-check-BBDB ()
1699 "Mail from people in the BBDB is classified as ham or non-spam"
1700 (let ((who (message-fetch-field "from"))
1701 (spam-split-group (if spam-split-symbolic-return
1704 bbdb-cache bbdb-hashtable)
1705 (when spam-cache-lookups
1706 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1709 ;; this is the expanded (bbdb-hashtable) macro
1710 ;; without the debugging support
1711 (with-current-buffer (bbdb-buffer)
1713 (save-window-excursion
1714 (bbdb-records nil t)
1716 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1718 (setq who (nth 1 (gnus-extract-address-components who)))
1720 (if spam-cache-lookups
1722 (intern-soft who bbdb-cache))
1723 (bbdb-search-simple nil who))
1725 (if spam-use-BBDB-exclusive
1730 (defalias 'bbdb-search-simple 'ignore)
1731 (defalias 'bbdb-records 'ignore)
1732 (defalias 'bbdb-buffer 'ignore)
1733 (defalias 'spam-check-BBDB 'ignore)
1734 (defalias 'spam-BBDB-register-routine 'ignore)
1735 (defalias 'spam-enter-ham-BBDB 'ignore)
1736 (defalias 'bbdb-create-internal 'ignore)
1737 (defalias 'bbdb-delete-record-internal 'ignore)
1738 (defalias 'bbdb-records 'ignore))))
1743 ;;; check the ifile backend; return nil if the mail was NOT classified
1746 (defun spam-get-ifile-database-parameter ()
1747 "Get the command-line parameter for ifile's database from
1748 spam-ifile-database-path."
1749 (if spam-ifile-database-path
1750 (format "--db-file=%s" spam-ifile-database-path)
1753 (defun spam-check-ifile ()
1754 "Check the ifile backend for the classification of this message."
1755 (let ((article-buffer-name (buffer-name))
1756 (spam-split-group (if spam-split-symbolic-return
1761 (let ((temp-buffer-name (buffer-name))
1762 (db-param (spam-get-ifile-database-parameter)))
1764 (set-buffer article-buffer-name)
1765 (apply 'call-process-region
1766 (point-min) (point-max) spam-ifile-path
1767 nil temp-buffer-name nil "-c"
1768 (if db-param `(,db-param "-q") `("-q"))))
1769 ;; check the return now (we're back in the temp buffer)
1770 (goto-char (point-min))
1772 (setq category (buffer-substring (point) (point-at-eol))))
1773 (when (not (zerop (length category))) ; we need a category here
1774 (if spam-ifile-all-categories
1775 (setq return category)
1776 ;; else, if spam-ifile-all-categories is not set...
1777 (when (string-equal spam-ifile-spam-category category)
1778 (setq return spam-split-group)))))) ; note return is nil otherwise
1781 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1782 "Register an article, given as a string, with a category.
1783 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1784 (let ((category (or category gnus-newsgroup-name))
1785 (add-or-delete-option (if unregister "-d" "-i"))
1786 (db (spam-get-ifile-database-parameter))
1789 (dolist (article articles)
1790 (let ((article-string (spam-get-article-as-string article)))
1791 (when (stringp article-string)
1792 (insert article-string))))
1793 (apply 'call-process-region
1794 (point-min) (point-max) spam-ifile-path
1796 add-or-delete-option category
1797 (if db `(,db "-h") `("-h"))))))
1799 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1800 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1802 (defun spam-ifile-unregister-spam-routine (articles)
1803 (spam-ifile-register-spam-routine articles t))
1805 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1806 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1808 (defun spam-ifile-unregister-ham-routine (articles)
1809 (spam-ifile-register-ham-routine articles t))
1816 (let ((spam-stat-install-hooks nil))
1817 (require 'spam-stat))
1819 (defun spam-check-stat ()
1820 "Check the spam-stat backend for the classification of this message"
1821 (let ((spam-split-group (if spam-split-symbolic-return
1824 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1825 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1827 (spam-stat-split-fancy)))
1829 (defun spam-stat-register-spam-routine (articles &optional unregister)
1830 (dolist (article articles)
1831 (let ((article-string (spam-get-article-as-string article)))
1833 (insert article-string)
1835 (spam-stat-buffer-change-to-non-spam)
1836 (spam-stat-buffer-is-spam))))))
1838 (defun spam-stat-unregister-spam-routine (articles)
1839 (spam-stat-register-spam-routine articles t))
1841 (defun spam-stat-register-ham-routine (articles &optional unregister)
1842 (dolist (article articles)
1843 (let ((article-string (spam-get-article-as-string article)))
1845 (insert article-string)
1847 (spam-stat-buffer-change-to-spam)
1848 (spam-stat-buffer-is-non-spam))))))
1850 (defun spam-stat-unregister-ham-routine (articles)
1851 (spam-stat-register-ham-routine articles t))
1853 (defun spam-maybe-spam-stat-load ()
1854 (when spam-use-stat (spam-stat-load)))
1856 (defun spam-maybe-spam-stat-save ()
1857 (when spam-use-stat (spam-stat-save))))
1860 (defalias 'spam-stat-load 'ignore)
1861 (defalias 'spam-stat-save 'ignore)
1862 (defalias 'spam-maybe-spam-stat-load 'ignore)
1863 (defalias 'spam-maybe-spam-stat-save 'ignore)
1864 (defalias 'spam-stat-register-ham-routine 'ignore)
1865 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1866 (defalias 'spam-stat-register-spam-routine 'ignore)
1867 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1868 (defalias 'spam-stat-buffer-is-spam 'ignore)
1869 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1870 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1871 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1872 (defalias 'spam-stat-split-fancy 'ignore)
1873 (defalias 'spam-check-stat 'ignore))))
1877 ;;;; Blacklists and whitelists.
1879 (defvar spam-whitelist-cache nil)
1880 (defvar spam-blacklist-cache nil)
1882 (defun spam-kill-whole-line ()
1884 (let ((kill-whole-line t))
1887 ;;; address can be a list, too
1888 (defun spam-enter-whitelist (address &optional remove)
1889 "Enter ADDRESS (list or single) into the whitelist.
1890 With a non-nil REMOVE, remove them."
1891 (interactive "sAddress: ")
1892 (spam-enter-list address spam-whitelist remove)
1893 (setq spam-whitelist-cache nil)
1894 (spam-clear-cache 'spam-use-whitelist))
1896 ;;; address can be a list, too
1897 (defun spam-enter-blacklist (address &optional remove)
1898 "Enter ADDRESS (list or single) into the blacklist.
1899 With a non-nil REMOVE, remove them."
1900 (interactive "sAddress: ")
1901 (spam-enter-list address spam-blacklist remove)
1902 (setq spam-blacklist-cache nil)
1903 (spam-clear-cache 'spam-use-whitelist))
1905 (defun spam-enter-list (addresses file &optional remove)
1906 "Enter ADDRESSES into the given FILE.
1907 Either the whitelist or the blacklist files can be used. With
1908 REMOVE not nil, remove the ADDRESSES."
1909 (if (stringp addresses)
1910 (spam-enter-list (list addresses) file remove)
1911 ;; else, we have a list of addresses here
1912 (unless (file-exists-p (file-name-directory file))
1913 (make-directory (file-name-directory file) t))
1916 (find-file-noselect file))
1917 (dolist (a addresses)
1919 (goto-char (point-min))
1920 (if (re-search-forward (regexp-quote a) nil t)
1921 ;; found the address
1923 (spam-kill-whole-line))
1924 ;; else, the address was not found
1926 (goto-char (point-max))
1932 (defun spam-filelist-build-cache (type)
1933 (let ((cache (if (eq type 'spam-use-blacklist)
1934 spam-blacklist-cache
1935 spam-whitelist-cache))
1937 (unless (gethash type spam-caches)
1939 (let ((address (pop cache)))
1940 (unless (zerop (length address)) ; 0 for a nil address too
1941 (setq address (regexp-quote address))
1942 ;; fix regexp-quote's treatment of user-intended regexes
1943 (while (string-match "\\\\\\*" address)
1944 (setq address (replace-match ".*" t t address))))
1945 (push address parsed-cache)))
1946 (puthash type parsed-cache spam-caches))))
1948 (defun spam-filelist-check-cache (type from)
1949 (when (stringp from)
1950 (spam-filelist-build-cache type)
1952 (dolist (address (gethash type spam-caches))
1953 (when (and address (string-match address from))
1958 ;;; returns t if the sender is in the whitelist, nil or
1959 ;;; spam-split-group otherwise
1960 (defun spam-check-whitelist ()
1961 ;; FIXME! Should it detect when file timestamps change?
1962 (let ((spam-split-group (if spam-split-symbolic-return
1965 (unless spam-whitelist-cache
1966 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1967 (if (spam-from-listed-p 'spam-use-whitelist)
1969 (if spam-use-whitelist-exclusive
1973 (defun spam-check-blacklist ()
1974 ;; FIXME! Should it detect when file timestamps change?
1975 (let ((spam-split-group (if spam-split-symbolic-return
1978 (unless spam-blacklist-cache
1979 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1980 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1982 (defun spam-parse-list (file)
1983 (when (file-readable-p file)
1984 (let (contents address)
1986 (insert-file-contents file)
1988 (setq address (buffer-substring (point) (point-at-eol)))
1990 ;; insert the e-mail address if detected, otherwise the raw data
1991 (unless (zerop (length address))
1992 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1993 (push (or pure-address address) contents)))))
1994 (nreverse contents))))
1996 (defun spam-from-listed-p (type)
1997 (let ((from (message-fetch-field "from"))
1999 (spam-filelist-check-cache type from)))
2001 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2002 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2003 (declassification (if blacklist 'ham 'spam))
2005 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2007 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2008 from addresses unregister-list article-unregister-list)
2009 (dolist (article articles)
2010 (let ((from (spam-fetch-field-from-fast article))
2011 (id (spam-fetch-field-message-id-fast article))
2013 (when (stringp from)
2014 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2015 (when (and (not sender-ignored)
2016 (stringp ignore-regex)
2017 (string-match ignore-regex from))
2018 (setq sender-ignored t)))
2019 ;; remember the messages we need to unregister, unless remove is set
2022 (spam-log-unregistration-needed-p
2023 id 'process declassification de-symbol))
2024 (push article article-unregister-list)
2025 (push from unregister-list))
2026 (unless sender-ignored
2027 (push from addresses)))))
2030 (funcall enter-function addresses t) ; unregister all these addresses
2031 ;; else, register normally and unregister what we need to
2032 (funcall remove-function unregister-list t)
2033 (dolist (article article-unregister-list)
2034 (spam-log-undo-registration
2035 (spam-fetch-field-message-id-fast article)
2039 (funcall enter-function addresses nil))))
2041 (defun spam-blacklist-unregister-routine (articles)
2042 (spam-blacklist-register-routine articles t))
2044 (defun spam-blacklist-register-routine (articles &optional unregister)
2045 (spam-filelist-register-routine articles t unregister))
2047 (defun spam-whitelist-unregister-routine (articles)
2048 (spam-whitelist-register-routine articles t))
2050 (defun spam-whitelist-register-routine (articles &optional unregister)
2051 (spam-filelist-register-routine articles nil unregister))
2054 ;;;; Spam-report glue
2055 (defun spam-report-gmane-register-routine (articles)
2057 (apply 'spam-report-gmane articles)))
2061 (defun spam-check-bogofilter-headers (&optional score)
2062 (let ((header (message-fetch-field spam-bogofilter-header))
2063 (spam-split-group (if spam-split-symbolic-return
2066 (when header ; return nil when no header
2067 (if score ; scoring mode
2068 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2069 (match-string 1 header)
2071 ;; spam detection mode
2072 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2074 spam-split-group)))))
2076 ;; return something sensible if the score can't be determined
2077 (defun spam-bogofilter-score (&optional recheck)
2078 "Get the Bogofilter spamicity score"
2080 (save-window-excursion
2081 (gnus-summary-show-article t)
2082 (set-buffer gnus-article-buffer)
2083 (let ((score (or (unless recheck
2084 (spam-check-bogofilter-headers t))
2085 (spam-check-bogofilter t))))
2086 (gnus-summary-show-article)
2087 (message "Spamicity score %s" score)
2090 (defun spam-check-bogofilter (&optional score)
2091 "Check the Bogofilter backend for the classification of this message"
2092 (let ((article-buffer-name (buffer-name))
2093 (db spam-bogofilter-database-directory)
2096 (let ((temp-buffer-name (buffer-name)))
2098 (set-buffer article-buffer-name)
2099 (apply 'call-process-region
2100 (point-min) (point-max)
2101 spam-bogofilter-path
2102 nil temp-buffer-name nil
2103 (if db `("-d" ,db "-v") `("-v"))))
2104 (setq return (spam-check-bogofilter-headers score))))
2107 (defun spam-bogofilter-register-with-bogofilter (articles
2109 &optional unregister)
2110 "Register an article, given as a string, as spam or non-spam."
2111 (dolist (article articles)
2112 (let ((article-string (spam-get-article-as-string article))
2113 (db spam-bogofilter-database-directory)
2114 (switch (if unregister
2116 spam-bogofilter-spam-strong-switch
2117 spam-bogofilter-ham-strong-switch)
2119 spam-bogofilter-spam-switch
2120 spam-bogofilter-ham-switch))))
2121 (when (stringp article-string)
2123 (insert article-string)
2125 (apply 'call-process-region
2126 (point-min) (point-max)
2127 spam-bogofilter-path
2129 (if db `("-d" ,db "-v") `("-v"))))))))
2131 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2132 (spam-bogofilter-register-with-bogofilter articles t unregister))
2134 (defun spam-bogofilter-unregister-spam-routine (articles)
2135 (spam-bogofilter-register-spam-routine articles t))
2137 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2138 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2140 (defun spam-bogofilter-unregister-ham-routine (articles)
2141 (spam-bogofilter-register-ham-routine articles t))
2146 (defun spam-check-spamoracle ()
2147 "Run spamoracle on an article to determine whether it's spam."
2148 (let ((article-buffer-name (buffer-name))
2149 (spam-split-group (if spam-split-symbolic-return
2153 (let ((temp-buffer-name (buffer-name)))
2155 (set-buffer article-buffer-name)
2157 (apply 'call-process-region
2158 (point-min) (point-max)
2159 spam-spamoracle-binary
2160 nil temp-buffer-name nil
2161 (if spam-spamoracle-database
2162 `("-f" ,spam-spamoracle-database "mark")
2166 (set-buffer temp-buffer-name)
2167 (goto-char (point-min))
2168 (when (re-search-forward "^X-Spam: yes;" nil t)
2170 (error "Error running spamoracle: %s" status))))))))
2172 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2173 "Run spamoracle in training mode."
2175 (let ((temp-buffer-name (buffer-name)))
2177 (goto-char (point-min))
2178 (dolist (article articles)
2179 (insert (spam-get-article-as-string article)))
2180 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2184 (apply 'call-process-region
2185 (point-min) (point-max)
2186 spam-spamoracle-binary
2187 nil temp-buffer-name nil
2188 (if spam-spamoracle-database
2189 `("-f" ,spam-spamoracle-database
2192 (unless (eq 0 status)
2193 (error "Error running spamoracle: %s" status)))))))
2195 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2196 (spam-spamoracle-learn articles nil unregister))
2198 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2199 (spam-spamoracle-learn-ham articles t))
2201 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2202 (spam-spamoracle-learn articles t unregister))
2204 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2205 (spam-spamoracle-learn-spam articles t))
2209 ;;; based mostly on the bogofilter code
2210 (defun spam-check-spamassassin-headers (&optional score)
2211 "Check the SpamAssassin headers for the classification of this message."
2212 (if score ; scoring mode
2213 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2215 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2216 (match-string 1 header)
2218 ;; spam detection mode
2219 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2220 (spam-split-group (if spam-split-symbolic-return
2223 (when header ; return nil when no header
2224 (when (string-match spam-spamassassin-positive-spam-flag-header
2226 spam-split-group)))))
2228 (defun spam-check-spamassassin (&optional score)
2229 "Check the SpamAssassin backend for the classification of this message."
2230 (let ((article-buffer-name (buffer-name)))
2232 (let ((temp-buffer-name (buffer-name)))
2234 (set-buffer article-buffer-name)
2235 (apply 'call-process-region
2236 (point-min) (point-max) spam-spamassassin-path
2237 nil temp-buffer-name nil spam-spamassassin-arguments))
2238 ;; check the return now (we're back in the temp buffer)
2239 (goto-char (point-min))
2240 (spam-check-spamassassin-headers score)))))
2242 ;; return something sensible if the score can't be determined
2243 (defun spam-spamassassin-score (&optional recheck)
2244 "Get the SpamAssassin score"
2246 (save-window-excursion
2247 (gnus-summary-show-article t)
2248 (set-buffer gnus-article-buffer)
2249 (let ((score (or (unless recheck
2250 (spam-check-spamassassin-headers t))
2251 (spam-check-spamassassin t))))
2252 (gnus-summary-show-article)
2253 (message "SpamAssassin score %s" score)
2256 (defun spam-spamassassin-register-with-sa-learn (articles spam
2257 &optional unregister)
2258 "Register articles with spamassassin's sa-learn as spam or non-spam."
2260 (let ((action (if unregister spam-sa-learn-unregister-switch
2261 (if spam spam-sa-learn-spam-switch
2262 spam-sa-learn-ham-switch)))
2263 (summary-buffer-name (buffer-name)))
2265 ;; group the articles into mbox format
2266 (dolist (article articles)
2267 (let (article-string)
2269 (set-buffer summary-buffer-name)
2270 (setq article-string (spam-get-article-as-string article)))
2271 (when (stringp article-string)
2272 (insert "From \n") ; mbox separator (sa-learn only checks the
2273 ; first five chars, so we can get away with
2275 (insert article-string)
2277 ;; call sa-learn on all messages at the same time
2278 (apply 'call-process-region
2279 (point-min) (point-max)
2281 nil nil nil "--mbox"
2282 (if spam-sa-learn-rebuild
2284 `("--no-rebuild" ,action)))))))
2286 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2287 (spam-spamassassin-register-with-sa-learn articles t unregister))
2289 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2290 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2292 (defun spam-spamassassin-unregister-spam-routine (articles)
2293 (spam-spamassassin-register-with-sa-learn articles t t))
2295 (defun spam-spamassassin-unregister-ham-routine (articles)
2296 (spam-spamassassin-register-with-sa-learn articles nil t))
2300 ;;; based mostly on the bogofilter code
2301 (defun spam-check-bsfilter-headers (&optional score)
2303 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2305 (let ((header (nnmail-fetch-field spam-bsfilter-header))
2306 (spam-split-group (if spam-split-symbolic-return
2309 (when header ; return nil when no header
2310 (when (string-match "YES" header)
2311 spam-split-group)))))
2313 ;; return something sensible if the score can't be determined
2314 (defun spam-bsfilter-score (&optional recheck)
2315 "Get the Bsfilter spamicity score"
2317 (save-window-excursion
2318 (gnus-summary-show-article t)
2319 (set-buffer gnus-article-buffer)
2320 (let ((score (or (unless recheck
2321 (spam-check-bsfilter-headers t))
2322 (spam-check-bsfilter t))))
2323 (gnus-summary-show-article)
2324 (message "Spamicity score %s" score)
2327 (defun spam-check-bsfilter (&optional score)
2328 "Check the Bsfilter backend for the classification of this message"
2329 (let ((article-buffer-name (buffer-name))
2330 (dir spam-bsfilter-database-directory)
2333 (let ((temp-buffer-name (buffer-name)))
2335 (set-buffer article-buffer-name)
2336 (apply 'call-process-region
2337 (point-min) (point-max)
2339 nil temp-buffer-name nil
2342 "--insert-probability"
2344 (list "--homedir" dir))))
2345 (setq return (spam-check-bsfilter-headers score))))
2348 (defun spam-bsfilter-register-with-bsfilter (articles
2350 &optional unregister)
2351 "Register an article, given as a string, as spam or non-spam."
2352 (dolist (article articles)
2353 (let ((article-string (spam-get-article-as-string article))
2354 (switch (if unregister
2356 spam-bsfilter-spam-strong-switch
2357 spam-bsfilter-ham-strong-switch)
2359 spam-bsfilter-spam-switch
2360 spam-bsfilter-ham-switch))))
2361 (when (stringp article-string)
2363 (insert article-string)
2364 (apply 'call-process-region
2365 (point-min) (point-max)
2369 (when spam-bsfilter-database-directory
2371 spam-bsfilter-database-directory))))))))
2373 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2374 (spam-bsfilter-register-with-bsfilter articles t unregister))
2376 (defun spam-bsfilter-unregister-spam-routine (articles)
2377 (spam-bsfilter-register-spam-routine articles t))
2379 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2380 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2382 (defun spam-bsfilter-unregister-ham-routine (articles)
2383 (spam-bsfilter-register-ham-routine articles t))
2389 (defun spam-initialize (&rest symbols)
2390 "Install the spam.el hooks and do other initialization.
2391 When SYMBOLS is given, set those variables to t. This is so you
2392 can call spam-initialize before you set spam-use-* variables on
2393 explicitly, and matters only if you need the extra headers
2394 installed through spam-necessary-extra-headers."
2397 (dolist (var symbols)
2400 (dolist (header (spam-necessary-extra-headers))
2401 (add-to-list 'nnmail-extra-headers header)
2402 (add-to-list 'gnus-extra-headers header))
2404 (setq spam-install-hooks t)
2405 ;; TODO: How do we redo this every time spam-face is customized?
2406 (push '((eq mark gnus-spam-mark) . spam-face)
2407 gnus-summary-highlight)
2408 ;; Add hooks for loading and saving the spam stats
2409 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2410 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2411 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2412 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2413 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2414 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2415 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2417 (defun spam-unload-hook ()
2418 "Uninstall the spam.el hooks"
2420 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2421 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2422 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2423 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2424 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2425 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2426 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2428 (when spam-install-hooks
2433 ;;; spam.el ends here