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")
59 (autoload 'spam-report-resend "spam-report"))
61 ;; autoload gnus-registry
63 (autoload 'gnus-registry-group-count "gnus-registry")
64 (autoload 'gnus-registry-add-group "gnus-registry")
65 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
66 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
70 (autoload 'query-dns "dns"))
75 "Spam configuration.")
77 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
78 "Directory for spam whitelists and blacklists."
82 (defcustom spam-move-spam-nonspam-groups-only t
83 "Whether spam should be moved in non-spam groups only.
84 When t, only ham and unclassified groups will have their spam moved
85 to the spam-process-destination. When nil, spam will also be moved from
90 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
91 "Whether new messages in a spam group should get the spam-mark."
95 (defcustom spam-process-ham-in-nonham-groups nil
96 "Whether ham should be processed in non-ham groups."
100 (defcustom spam-log-to-registry nil
101 "Whether spam/ham processing should be logged in the registry."
105 (defcustom spam-split-symbolic-return nil
106 "Whether `spam-split' should work with symbols or group names."
110 (defcustom spam-split-symbolic-return-positive nil
111 "Whether `spam-split' should ALWAYS work with symbols or group names.
112 Do not set this if you use `spam-split' in a fancy split
117 (defcustom spam-process-ham-in-spam-groups nil
118 "Whether ham should be processed in spam groups."
122 (defcustom spam-mark-only-unseen-as-spam t
123 "Whether only unseen articles should be marked as spam in spam groups.
124 When nil, all unread articles in a spam group are marked as
125 spam. Set this if you want to leave an article unread in a spam group
126 without losing it to the automatic spam-marking process."
130 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
131 "Whether ham should be marked unread before it's moved.
132 The article is moved out of a spam group according to ham-process-destination.
133 This variable is an official entry in the international Longest Variable Name
138 (defcustom spam-disable-spam-split-during-ham-respool nil
139 "Whether `spam-split' should be ignored while resplitting ham.
140 This is useful to prevent ham from ending up in the same spam
141 group after the resplit. Don't set this to t if you have `spam-split' as the
142 last rule in your split configuration."
146 (defcustom spam-autodetect-recheck-messages nil
147 "Should spam.el recheck all meessages when autodetecting?
148 Normally this is nil, so only unseen messages will be checked."
152 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
153 "The location of the whitelist.
154 The file format is one regular expression per line.
155 The regular expression is matched against the address."
159 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
160 "The location of the blacklist.
161 The file format is one regular expression per line.
162 The regular expression is matched against the address."
166 (defcustom spam-use-dig t
167 "Whether `query-dig' should be used instead of `query-dns'."
171 (defcustom spam-use-gmane-xref nil
172 "Whether the Gmane spam xref should be used by `spam-split'."
176 (defcustom spam-use-blacklist nil
177 "Whether the blacklist should be used by `spam-split'."
181 (defcustom spam-blacklist-ignored-regexes nil
182 "Regular expressions that the blacklist should ignore."
183 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
186 (defcustom spam-use-whitelist nil
187 "Whether the whitelist should be used by `spam-split'."
191 (defcustom spam-use-whitelist-exclusive nil
192 "Whether whitelist-exclusive should be used by `spam-split'.
193 Exclusive whitelisting means that all messages from senders not in the whitelist
194 are considered spam."
198 (defcustom spam-use-blackholes nil
199 "Whether blackholes should be used by `spam-split'."
203 (defcustom spam-use-hashcash nil
204 "Whether hashcash payments should be detected by `spam-split'."
208 (defcustom spam-use-regex-headers nil
209 "Whether a header regular expression match should be used by `spam-split'.
210 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
214 (defcustom spam-use-regex-body nil
215 "Whether a body regular expression match should be used by `spam-split'.
216 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
220 (defcustom spam-use-bogofilter-headers nil
221 "Whether bogofilter headers should be used by `spam-split'.
222 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
226 (defcustom spam-use-bogofilter nil
227 "Whether bogofilter should be invoked by `spam-split'.
228 Enable this if you want Gnus to invoke Bogofilter on new messages."
232 (defcustom spam-use-bsfilter-headers nil
233 "Whether bsfilter headers should be used by `spam-split'.
234 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
238 (defcustom spam-use-bsfilter nil
239 "Whether bsfilter should be invoked by `spam-split'.
240 Enable this if you want Gnus to invoke Bsfilter on new messages."
244 (defcustom spam-use-BBDB nil
245 "Whether BBDB should be used by `spam-split'."
249 (defcustom spam-use-BBDB-exclusive nil
250 "Whether BBDB-exclusive should be used by `spam-split'.
251 Exclusive BBDB means that all messages from senders not in the BBDB are
256 (defcustom spam-use-ifile nil
257 "Whether ifile should be used by `spam-split'."
261 (defcustom spam-use-stat nil
262 "Whether `spam-stat' should be used by `spam-split'."
266 (defcustom spam-use-spamoracle nil
267 "Whether spamoracle should be used by `spam-split'."
271 (defcustom spam-use-spamassassin nil
272 "Whether spamassassin should be invoked by `spam-split'.
273 Enable this if you want Gnus to invoke SpamAssassin on new messages."
277 (defcustom spam-use-spamassassin-headers nil
278 "Whether spamassassin headers should be checked by `spam-split'.
279 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
284 (defcustom spam-use-crm114 nil
285 "Whether the CRM114 Mailfilter should be used by `spam-split'."
289 (defcustom spam-install-hooks (or
294 spam-use-whitelist-exclusive
297 spam-use-regex-headers
300 spam-use-bogofilter-headers
301 spam-use-spamassassin
302 spam-use-spamassassin-headers
304 spam-use-bsfilter-headers
306 spam-use-BBDB-exclusive
311 "Whether the spam hooks should be installed.
312 Default to t if one of the spam-use-* variables is set."
316 (defcustom spam-split-group "spam"
317 "Group name where incoming spam should be put by `spam-split'."
321 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
322 ;;; not regular expressions
323 (defcustom spam-junk-mailgroups (cons
325 '("mail.junk" "poste.pourriel"))
326 "Mailgroups with spam contents.
327 All unmarked article in such group receive the spam mark on group entry."
328 :type '(repeat (string :tag "Group"))
332 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
333 "The group where spam xrefs can be found on Gmane.
334 Only meaningful if you enable `spam-use-gmane-xref'."
338 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
339 "dev.null.dk" "relays.visi.com")
340 "List of blackhole servers.
341 Only meaningful if you enable `spam-use-blackholes'."
342 :type '(repeat (string :tag "Server"))
345 (defcustom spam-blackhole-good-server-regex nil
346 "String matching IP addresses that should not be checked in the blackholes.
347 Only meaningful if you enable `spam-use-blackholes'."
348 :type '(radio (const nil)
349 (regexp :format "%t: %v\n" :size 0))
352 (defcustom spam-face 'gnus-splash-face
353 "Face for spam-marked articles."
357 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
358 "Regular expression for positive header spam matches.
359 Only meaningful if you enable `spam-use-regex-headers'."
360 :type '(repeat (regexp :tag "Regular expression to match spam header"))
363 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
364 "Regular expression for positive header ham matches.
365 Only meaningful if you enable `spam-use-regex-headers'."
366 :type '(repeat (regexp :tag "Regular expression to match ham header"))
369 (defcustom spam-regex-body-spam '()
370 "Regular expression for positive body spam matches.
371 Only meaningful if you enable `spam-use-regex-body'."
372 :type '(repeat (regexp :tag "Regular expression to match spam body"))
375 (defcustom spam-regex-body-ham '()
376 "Regular expression for positive body ham matches.
377 Only meaningful if you enable `spam-use-regex-body'."
378 :type '(repeat (regexp :tag "Regular expression to match ham body"))
381 (defgroup spam-ifile nil
382 "Spam ifile configuration."
385 (defcustom spam-ifile-path (exec-installed-p "ifile")
386 "File path of the ifile executable program."
387 :type '(choice (file :tag "Location of ifile")
388 (const :tag "ifile is not installed"))
391 (defcustom spam-ifile-database-path nil
392 "File path of the ifile database."
393 :type '(choice (file :tag "Location of the ifile database")
394 (const :tag "Use the default"))
397 (defcustom spam-ifile-spam-category "spam"
398 "Name of the spam ifile category."
402 (defcustom spam-ifile-ham-category nil
403 "Name of the ham ifile category.
404 If nil, the current group name will be used."
405 :type '(choice (string :tag "Use a fixed category")
406 (const :tag "Use the current group name"))
409 (defcustom spam-ifile-all-categories nil
410 "Whether the ifile check will return all categories, or just spam.
411 Set this to t if you want to use the `spam-split' invocation of ifile as
412 your main source of newsgroup names."
416 (defgroup spam-bogofilter nil
417 "Spam bogofilter configuration."
420 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
421 "File path of the Bogofilter executable program."
422 :type '(choice (file :tag "Location of bogofilter")
423 (const :tag "Bogofilter is not installed"))
424 :group 'spam-bogofilter)
426 (defcustom spam-bogofilter-header "X-Bogosity"
427 "The header that Bogofilter inserts in messages."
429 :group 'spam-bogofilter)
431 (defcustom spam-bogofilter-spam-switch "-s"
432 "The switch that Bogofilter uses to register spam messages."
434 :group 'spam-bogofilter)
436 (defcustom spam-bogofilter-ham-switch "-n"
437 "The switch that Bogofilter uses to register ham messages."
439 :group 'spam-bogofilter)
441 (defcustom spam-bogofilter-spam-strong-switch "-S"
442 "The switch that Bogofilter uses to unregister ham messages."
444 :group 'spam-bogofilter)
446 (defcustom spam-bogofilter-ham-strong-switch "-N"
447 "The switch that Bogofilter uses to unregister spam messages."
449 :group 'spam-bogofilter)
451 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
452 "The regex on `spam-bogofilter-header' for positive spam identification."
454 :group 'spam-bogofilter)
456 (defcustom spam-bogofilter-database-directory nil
457 "Directory path of the Bogofilter databases."
458 :type '(choice (directory
459 :tag "Location of the Bogofilter database directory")
460 (const :tag "Use the default"))
461 :group 'spam-bogofilter)
463 (defgroup spam-bsfilter nil
464 "Spam bsfilter configuration."
467 (defcustom spam-bsfilter-path (executable-find "bsfilter")
468 "File path of the Bsfilter executable program."
469 :type '(choice (file :tag "Location of bsfilter")
470 (const :tag "Bsfilter is not installed"))
471 :group 'spam-bsfilter)
473 (defcustom spam-bsfilter-header "X-Spam-Flag"
474 "The header inserted by Bsfilter to flag spam."
476 :group 'spam-bsfilter)
478 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
479 "The header that Bsfilter inserts in messages."
481 :group 'spam-bsfilter)
483 (defcustom spam-bsfilter-spam-switch "--add-spam"
484 "The switch that Bsfilter uses to register spam messages."
486 :group 'spam-bsfilter)
488 (defcustom spam-bsfilter-ham-switch "--add-ham"
489 "The switch that Bsfilter uses to register ham messages."
491 :group 'spam-bsfilter)
493 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
494 "The switch that Bsfilter uses to unregister ham messages."
496 :group 'spam-bsfilter)
498 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
499 "The switch that Bsfilter uses to unregister spam messages."
501 :group 'spam-bsfilter)
503 (defcustom spam-bsfilter-database-directory nil
504 "Directory path of the Bsfilter databases."
505 :type '(choice (directory
506 :tag "Location of the Bsfilter database directory")
507 (const :tag "Use the default"))
508 :group 'spam-bsfilter)
510 (defgroup spam-spamoracle nil
511 "Spam spamoracle configuration."
514 (defcustom spam-spamoracle-database nil
515 "Location of spamoracle database file.
516 When nil, use the default spamoracle database."
517 :type '(choice (directory :tag "Location of spamoracle database file.")
518 (const :tag "Use the default"))
519 :group 'spam-spamoracle)
521 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
522 "Location of the spamoracle binary."
523 :type '(choice (directory :tag "Location of the spamoracle binary")
524 (const :tag "Use the default"))
525 :group 'spam-spamoracle)
527 (defgroup spam-spamassassin nil
528 "Spam SpamAssassin configuration."
531 (defcustom spam-spamassassin-path (executable-find "spamassassin")
532 "File path of the spamassassin executable program.
533 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
534 spamd man pages for more information on these programs."
535 :type '(choice (file :tag "Location of spamc")
536 (const :tag "spamassassin is not installed"))
537 :group 'spam-spamassassin)
539 (defcustom spam-spamassassin-arguments ()
540 "Arguments to pass to the spamassassin executable.
541 This must be a list. For example, `(\"-C\" \"configfile\")'."
542 :type '(restricted-sexp :match-alternatives (listp))
543 :group 'spam-spamassassin)
545 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
546 "The header inserted by SpamAssassin to flag spam."
548 :group 'spam-spamassassin)
550 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
551 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
554 :group 'spam-spamassassin)
556 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
557 "The header inserted by SpamAssassin, giving extended scoring information"
559 :group 'spam-spamassassin)
561 (defcustom spam-sa-learn-path (executable-find "sa-learn")
562 "File path of the sa-learn executable program."
563 :type '(choice (file :tag "Location of spamassassin")
564 (const :tag "spamassassin is not installed"))
565 :group 'spam-spamassassin)
567 (defcustom spam-sa-learn-rebuild t
568 "Whether sa-learn should rebuild the database every time it is called
569 Enable this if you want sa-learn to rebuild the database automatically. Doing
570 this will slightly increase the running time of the spam registration process.
571 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
572 order for SpamAssassin to recognize the new registered spam."
574 :group 'spam-spamassassin)
576 (defcustom spam-sa-learn-spam-switch "--spam"
577 "The switch that sa-learn uses to register spam messages"
579 :group 'spam-spamassassin)
581 (defcustom spam-sa-learn-ham-switch "--ham"
582 "The switch that sa-learn uses to register ham messages"
584 :group 'spam-spamassassin)
586 (defcustom spam-sa-learn-unregister-switch "--forget"
587 "The switch that sa-learn uses to unregister messages messages"
589 :group 'spam-spamassassin)
591 (defgroup spam-crm114 nil
592 "Spam CRM114 Mailfilter configuration."
595 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
596 "File path of the CRM114 Mailfilter executable program."
597 :type '(choice (file :tag "Location of CRM114 Mailfilter")
598 (const :tag "CRM114 Mailfilter is not installed"))
601 (defcustom spam-crm114-header "X-CRM114-Status"
602 "The header that CRM114 Mailfilter inserts in messages."
606 (defcustom spam-crm114-spam-switch "--learnspam"
607 "The switch that CRM114 Mailfilter uses to register spam messages."
611 (defcustom spam-crm114-ham-switch "--learnnonspam"
612 "The switch that CRM114 Mailfilter uses to register ham messages."
616 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
617 "The switch that CRM114 Mailfilter uses to unregister ham messages."
621 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
622 "The switch that CRM114 Mailfilter uses to unregister spam messages."
626 (defcustom spam-crm114-positive-spam-header "^SPAM"
627 "The regex on `spam-crm114-header' for positive spam identification."
631 (defcustom spam-crm114-database-directory nil
632 "Directory path of the CRM114 Mailfilter databases."
633 :type '(choice (directory
634 :tag "Location of the CRM114 Mailfilter database directory")
635 (const :tag "Use the default"))
638 ;;; Key bindings for spam control.
640 (gnus-define-keys gnus-summary-mode-map
641 "St" spam-generic-score
642 "Sx" gnus-summary-mark-as-spam
643 "Mst" spam-generic-score
644 "Msx" gnus-summary-mark-as-spam
645 "\M-d" gnus-summary-mark-as-spam)
647 (defvar spam-cache-lookups t
648 "Whether spam.el will try to cache lookups using `spam-caches'.")
650 (defvar spam-caches (make-hash-table
653 "Cache of spam detection entries.")
655 (defvar spam-old-ham-articles nil
656 "List of old ham articles, generated when a group is entered.")
658 (defvar spam-old-spam-articles nil
659 "List of old spam articles, generated when a group is entered.")
661 (defvar spam-split-disabled nil
662 "If non-nil, `spam-split' is disabled, and always returns nil.")
664 (defvar spam-split-last-successful-check nil
666 `spam-split' will set this to nil or a spam-use-XYZ check if it
669 ;; convenience functions
670 (defun spam-clear-cache (symbol)
671 "Clear the spam-caches entry for a check."
672 (remhash symbol spam-caches))
674 (defun spam-xor (a b)
676 (and (or a b) (not (and a b))))
678 (defun spam-group-ham-mark-p (group mark &optional spam)
679 "Checks if MARK is considered a ham mark in GROUP."
680 (when (stringp group)
681 (let* ((marks (spam-group-ham-marks group spam))
682 (marks (if (symbolp mark)
684 (mapcar 'symbol-value marks))))
687 (defun spam-group-spam-mark-p (group mark)
688 "Checks if MARK is considered a spam mark in GROUP."
689 (spam-group-ham-mark-p group mark t))
691 (defun spam-group-ham-marks (group &optional spam)
692 "In GROUP, get all the ham marks."
693 (when (stringp group)
694 (let* ((marks (if spam
695 (gnus-parameter-spam-marks group)
696 (gnus-parameter-ham-marks group)))
698 (marks (if (listp (car marks)) (car marks) marks)))
701 (defun spam-group-spam-marks (group)
702 "In GROUP, get all the spam marks."
703 (spam-group-ham-marks group t))
705 (defun spam-group-spam-contents-p (group)
706 "Is GROUP a spam group?"
708 (or (member group spam-junk-mailgroups)
709 (memq 'gnus-group-spam-classification-spam
710 (gnus-parameter-spam-contents group)))
713 (defun spam-group-ham-contents-p (group)
714 "Is GROUP a ham group?"
716 (memq 'gnus-group-spam-classification-ham
717 (gnus-parameter-spam-contents group))
720 (defvar spam-list-of-processors
721 ;; note the nil processors are not defined in gnus.el
722 '((nil spam spam-use-gmane)
723 (nil spam spam-use-resend)
724 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
725 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
726 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
727 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
728 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
729 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
730 (nil spam spam-use-crm114)
731 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
732 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
733 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
734 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
735 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
736 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
737 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
738 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
739 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
740 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle)
741 (nil ham spam-use-crm114))
742 "The `spam-list-of-processors' list.
743 This list contains pairs associating the obsolete ham/spam exit
744 processor variables with a classification and a spam-use-*
745 variable. When the processor variable is nil, just the
746 classification and spam-use-* check variable are used.")
748 (defun spam-group-processor-p (group check &optional classification)
749 "Checks if GROUP has a CHECK with CLASSIFICATION registered.
750 Also accepts the obsolete processors, which can be found in
751 gnus.el and in spam-list-of-processors."
752 (if (and (stringp group)
754 (let ((old-style (assq check spam-list-of-processors))
755 (parameters (nth 0 (gnus-parameter-spam-process group)))
757 (if old-style ; old-style processor
758 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
759 ;; now search for the parameter
760 (dolist (parameter parameters)
761 (when (and (null found)
763 (eq classification (nth 0 parameter))
764 (eq check (nth 1 parameter)))
769 (defun spam-report-articles-gmane (n)
770 "Report the current message as spam via Gmane.
771 Respects the process/prefix convention."
773 (dolist (article (gnus-summary-work-articles n))
774 (gnus-summary-remove-process-mark article)
775 (spam-report-gmane article)))
777 (defun spam-report-articles-resend (n)
778 "Report the current message as spam by resending it.
779 Respects the process/prefix convention. Also see
780 `spam-report-resend-to'."
782 (let ((spam-report-resend-to
783 (gnus-parameter-spam-resend-to gnus-newsgroup-name))
784 (articles (gnus-summary-work-articles n)))
785 (spam-report-resend articles)
786 (dolist (article articles)
787 (gnus-summary-remove-process-mark article))))
789 (defun spam-necessary-extra-headers ()
790 "Return the extra headers spam.el thinks are necessary."
792 (when (or spam-use-spamassassin
793 spam-use-spamassassin-headers
794 spam-use-regex-headers)
795 (push 'X-Spam-Status list))
798 (defun spam-user-format-function-S (headers)
800 (spam-summary-score headers)))
802 (defun spam-article-sort-by-spam-status (h1 h2)
803 "Sort articles by score."
805 (dolist (header (spam-necessary-extra-headers))
806 (let ((s1 (spam-summary-score h1 header))
807 (s2 (spam-summary-score h2 header)))
809 (setq result (< s1 s2))
813 (defun spam-extra-header-to-number (header headers)
814 "Transform an extra header to a number."
815 (if (gnus-extra-header header headers)
817 ((eq header 'X-Spam-Status)
818 (string-to-number (gnus-replace-in-string
819 (gnus-extra-header header headers)
821 ;; for CRM checking, it's probably faster to just do the string match
822 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
823 (match-string 1 header))
827 (defun spam-summary-score (headers &optional specific-header)
828 "Score an article for the summary buffer, as fast as possible.
829 With SPECIFIC-HEADER, returns only that header's score.
830 Will not return a nil score."
834 (list specific-header)
835 (spam-necessary-extra-headers)))
837 (spam-extra-header-to-number header headers))
842 (defun spam-generic-score (&optional recheck)
843 "Invoke whatever scoring method we can."
846 ((or spam-use-spamassassin spam-use-spamassassin-headers)
847 (spam-spamassassin-score recheck))
848 ((or spam-use-bsfilter spam-use-bsfilter-headers)
849 (spam-bsfilter-score recheck))
852 (t (spam-bogofilter-score recheck))))
854 ;;; Summary entry and exit processing.
856 (defun spam-summary-prepare ()
857 (setq spam-old-ham-articles
858 (spam-list-articles gnus-newsgroup-articles 'ham))
859 (setq spam-old-spam-articles
860 (spam-list-articles gnus-newsgroup-articles 'spam))
861 (spam-mark-junk-as-spam-routine))
863 ;; The spam processors are invoked for any group, spam or ham or neither
864 (defun spam-summary-prepare-exit ()
865 (unless gnus-group-is-exiting-without-update-p
866 (gnus-message 6 "Exiting summary buffer and applying spam rules")
868 ;; first of all, unregister any articles that are no longer ham or spam
869 ;; we have to iterate over the processors, or else we'll be too slow
870 (dolist (classification '(spam ham))
871 (let* ((old-articles (if (eq classification 'spam)
872 spam-old-spam-articles
873 spam-old-ham-articles))
874 (new-articles (spam-list-articles
875 gnus-newsgroup-articles
877 (changed-articles (spam-set-difference new-articles old-articles)))
878 ;; now that we have the changed articles, we go through the processors
879 (dolist (processor-param spam-list-of-processors)
880 (let ((processor (nth 0 processor-param))
881 (processor-classification (nth 1 processor-param))
882 (check (nth 2 processor-param))
884 (dolist (article changed-articles)
885 (let ((id (spam-fetch-field-message-id-fast article)))
886 (when (spam-log-unregistration-needed-p
887 id 'process classification check)
888 (push article unregister-list))))
889 ;; call spam-register-routine with specific articles to unregister,
890 ;; when there are articles to unregister and the check is enabled
891 (when (and unregister-list (symbol-value check))
892 (spam-register-routine
893 classification check t unregister-list))))))
895 ;; find all the spam processors applicable to this group
896 (dolist (processor-param spam-list-of-processors)
897 (let ((processor (nth 0 processor-param))
898 (classification (nth 1 processor-param))
899 (check (nth 2 processor-param)))
900 (when (and (eq 'spam classification)
901 (spam-group-processor-p gnus-newsgroup-name check classification))
902 (spam-register-routine classification check))))
904 (unless (and spam-move-spam-nonspam-groups-only
905 (spam-group-spam-contents-p gnus-newsgroup-name))
906 (let* ((group (gnus-parameter-spam-process-destination
907 gnus-newsgroup-name))
908 (num (spam-mark-spam-as-expired-and-move-routine group)))
911 "%d spam messages are marked as expired and moved it to %s"
914 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
915 ;; expire spam, in case the above did not expire them
916 (let ((num (spam-mark-spam-as-expired-and-move-routine nil)))
919 "%d spam messages are markd as expired without moving it"
922 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
923 (and (spam-group-spam-contents-p gnus-newsgroup-name)
924 spam-process-ham-in-spam-groups)
925 spam-process-ham-in-nonham-groups)
926 ;; find all the ham processors applicable to this group
927 (dolist (processor-param spam-list-of-processors)
928 (let ((processor (nth 0 processor-param))
929 (classification (nth 1 processor-param))
930 (check (nth 2 processor-param)))
931 (when (and (eq 'ham classification)
932 (spam-group-processor-p gnus-newsgroup-name check classification))
933 (spam-register-routine classification check)))))
935 (when (spam-group-processor-p gnus-newsgroup-name 'ham 'spam-use-ham-copy)
937 (spam-ham-copy-routine
938 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
940 (gnus-message 6 "%d ham messages are copied" num))))
942 ;; now move all ham articles out of spam groups
943 (when (spam-group-spam-contents-p gnus-newsgroup-name)
945 (spam-ham-move-routine
946 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
948 (gnus-message 6 "%d ham messages are moved from spam group" num)))))
950 (setq spam-old-ham-articles nil)
951 (setq spam-old-spam-articles nil))
953 (defun spam-set-difference (list1 list2)
954 "Return a set difference of LIST1 and LIST2.
955 When either list is nil, the other is returned."
956 (if (and list1 list2)
957 ;; we have two non-nil lists
959 (dolist (item (append list1 list2))
960 (when (and (memq item list1) (memq item list2))
961 (setq list1 (delq item list1))
962 (setq list2 (delq item list2))))
963 (append list1 list2))
964 ;; if either of the lists was nil, return the other one
965 (if list1 list1 list2)))
967 (defun spam-mark-junk-as-spam-routine ()
968 ;; check the global list of group names spam-junk-mailgroups and the
970 (when (spam-group-spam-contents-p gnus-newsgroup-name)
971 (gnus-message 6 "Marking %s articles as spam"
972 (if spam-mark-only-unseen-as-spam
975 (let ((articles (if spam-mark-only-unseen-as-spam
976 gnus-newsgroup-unseen
977 gnus-newsgroup-unreads)))
978 (if spam-mark-new-messages-in-spam-group-as-spam
979 (dolist (article articles)
980 (gnus-summary-mark-article article gnus-spam-mark))
981 (gnus-message 9 "Did not mark new messages as spam.")))))
983 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
984 (if (and (car-safe groups) (listp (car-safe groups)))
985 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
986 (gnus-summary-kill-process-mark)
987 (let ((articles gnus-newsgroup-articles)
988 (backend-supports-deletions
989 (gnus-check-backend-function
990 'request-move-article gnus-newsgroup-name))
991 article tomove deletep)
992 (dolist (article articles)
993 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
994 (gnus-summary-mark-article article gnus-expirable-mark)
995 (push article tomove)))
997 ;; now do the actual copies
998 (dolist (group groups)
1001 (dolist (article tomove)
1002 (gnus-summary-set-process-mark article))
1004 (if (or (not backend-supports-deletions)
1005 (> (length groups) 1))
1007 (gnus-summary-copy-article nil group)
1009 (gnus-summary-move-article nil group)))))
1011 ;; now delete the articles, if there was a copy done, and the
1012 ;; backend allows it
1013 (when (and deletep backend-supports-deletions)
1014 (dolist (article tomove)
1015 (gnus-summary-set-process-mark article))
1017 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1018 (gnus-summary-delete-article nil))))
1020 (gnus-summary-yank-process-mark)
1023 (defun spam-ham-copy-or-move-routine (copy groups)
1024 (gnus-summary-kill-process-mark)
1025 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
1026 (backend-supports-deletions
1027 (gnus-check-backend-function
1028 'request-move-article gnus-newsgroup-name))
1029 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1030 article mark deletep respool)
1032 (when (member 'respool groups)
1033 (setq respool t) ; boolean for later
1034 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1036 ;; now do the actual move
1037 (dolist (group groups)
1038 (when (and todo (stringp group))
1039 (dolist (article todo)
1040 (when spam-mark-ham-unread-before-move-from-spam-group
1041 (gnus-summary-mark-article article gnus-unread-mark))
1042 (gnus-summary-set-process-mark article))
1044 (if respool ; respooling is with a "fake" group
1045 (let ((spam-split-disabled
1046 (or spam-split-disabled
1047 spam-disable-spam-split-during-ham-respool)))
1048 (gnus-summary-respool-article nil respool-method))
1049 (if (or (not backend-supports-deletions) ; else, we are not respooling
1050 (> (length groups) 1))
1051 (progn ; if copying, copy and set deletep
1052 (gnus-summary-copy-article nil group)
1054 (gnus-summary-move-article nil group))))) ; else move articles
1056 ;; now delete the articles, unless a) copy is t, and there was a copy done
1057 ;; b) a move was done to a single group
1058 ;; c) backend-supports-deletions is nil
1060 (when (and deletep backend-supports-deletions)
1061 (dolist (article todo)
1062 (gnus-summary-set-process-mark article))
1064 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1065 (gnus-summary-delete-article nil)))))
1067 (gnus-summary-yank-process-mark)
1070 (defun spam-ham-copy-routine (&rest groups)
1071 (if (and (car-safe groups) (listp (car-safe groups)))
1072 (apply 'spam-ham-copy-routine (car groups))
1073 (spam-ham-copy-or-move-routine t groups)))
1075 (defun spam-ham-move-routine (&rest groups)
1076 (if (and (car-safe groups) (listp (car-safe groups)))
1077 (apply 'spam-ham-move-routine (car groups))
1078 (spam-ham-copy-or-move-routine nil groups)))
1080 (defun spam-get-article-as-string (article)
1081 (when (numberp article)
1083 (gnus-request-article-this-buffer
1085 gnus-newsgroup-name)
1089 ;; (defun spam-get-article-as-filename (article)
1090 ;; (let ((article-filename))
1091 ;; (when (numberp article)
1092 ;; (nnml-possibly-change-directory
1093 ;; (gnus-group-real-name gnus-newsgroup-name))
1094 ;; (setq article-filename (expand-file-name
1095 ;; (int-to-string article) nnml-current-directory)))
1096 ;; (if (file-exists-p article-filename)
1100 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1101 "Fetch a field quickly, using the internal gnus-data-list function"
1102 (when (numberp article)
1103 (let* ((data-header (or prepared-data-header
1104 (spam-fetch-article-header article))))
1105 (if (arrayp data-header)
1107 ((equal field 'from)
1108 (mail-header-from data-header))
1109 ((equal field 'message-id)
1110 (mail-header-message-id data-header))
1111 ((equal field 'subject)
1112 (mail-header-subject data-header))
1113 ((equal field 'references)
1114 (mail-header-references data-header))
1115 ((equal field 'date)
1116 (mail-header-date data-header))
1117 ((equal field 'xref)
1118 (mail-header-xref data-header))
1119 ((equal field 'extra)
1120 (mail-header-extra data-header))
1123 (gnus-message 6 "Article %d has a nil data header" article)))))
1125 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1126 (spam-fetch-field-fast article 'from prepared-data-header))
1128 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1129 (spam-fetch-field-fast article 'subject prepared-data-header))
1131 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1132 (spam-fetch-field-fast article 'message-id prepared-data-header))
1134 (defun spam-generate-fake-headers (article)
1135 (let ((dh (spam-fetch-article-header article)))
1139 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1140 "Date: %s\nReferences: %s\nXref: %s\n")
1141 (spam-fetch-field-fast article 'from dh)
1142 (spam-fetch-field-fast article 'subject dh)
1143 (spam-fetch-field-fast article 'message-id dh)
1144 (spam-fetch-field-fast article 'date dh)
1145 (spam-fetch-field-fast article 'references dh)
1146 (spam-fetch-field-fast article 'xref dh))
1147 (when (spam-fetch-field-fast article 'extra dh)
1148 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1151 "spam-generate-fake-headers: article %d didn't have a valid header"
1154 (defun spam-fetch-article-header (article)
1156 (set-buffer gnus-summary-buffer)
1157 (gnus-read-header article)
1158 (nth 3 (assq article gnus-newsgroup-data))))
1161 ;;;; Spam determination.
1163 (defvar spam-list-of-checks
1164 '((spam-use-blacklist . spam-check-blacklist)
1165 (spam-use-regex-headers . spam-check-regex-headers)
1166 (spam-use-gmane-xref . spam-check-gmane-xref)
1167 (spam-use-regex-body . spam-check-regex-body)
1168 (spam-use-whitelist . spam-check-whitelist)
1169 (spam-use-BBDB . spam-check-BBDB)
1170 (spam-use-BBDB-exclusive . spam-check-BBDB)
1171 (spam-use-ifile . spam-check-ifile)
1172 (spam-use-spamoracle . spam-check-spamoracle)
1173 (spam-use-stat . spam-check-stat)
1174 (spam-use-blackholes . spam-check-blackholes)
1175 (spam-use-hashcash . spam-check-hashcash)
1176 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1177 (spam-use-spamassassin . spam-check-spamassassin)
1178 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1179 (spam-use-bogofilter . spam-check-bogofilter)
1180 (spam-use-bsfilter-headers . spam-check-bsfilter-headers)
1181 (spam-use-bsfilter . spam-check-bsfilter)
1182 (spam-use-crm114 . spam-check-crm114))
1183 "The spam-list-of-checks list contains pairs associating a
1184 parameter variable with a spam checking function. If the
1185 parameter variable is true, then the checking function is called,
1186 and its value decides what happens. Each individual check may
1187 return nil, t, or a mailgroup name. The value nil means that the
1188 check does not yield a decision, and so, that further checks are
1189 needed. The value t means that the message is definitely not
1190 spam, and that further spam checks should be inhibited.
1191 Otherwise, a mailgroup name or the symbol 'spam (depending on
1192 spam-split-symbolic-return) is returned where the mail should go,
1193 and further checks are also inhibited. The usual mailgroup name
1194 is the value of `spam-split-group', meaning that the message is
1195 definitely a spam.")
1197 (defvar spam-list-of-statistical-checks
1204 spam-use-spamassassin
1207 "The spam-list-of-statistical-checks list contains all the mail
1208 splitters that need to have the full message body available.
1209 Note that you should fetch extra headers if you don't like this,
1210 e.g. fetch the 'Received' header for spam-use-blackholes.")
1212 (defun spam-split (&rest specific-checks)
1213 "Split this message into the `spam' group if it is spam.
1214 This function can be used as an entry in the variable `nnmail-split-fancy',
1215 for example like this: (: spam-split). It can take checks as
1216 parameters. A string as a parameter will set the
1217 spam-split-group to that string.
1219 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1221 (setq spam-split-last-successful-check nil)
1222 (unless spam-split-disabled
1223 (let ((spam-split-group-choice spam-split-group))
1224 (dolist (check specific-checks)
1225 (when (stringp check)
1226 (setq spam-split-group-choice check)
1227 (setq specific-checks (delq check specific-checks))))
1229 (let ((spam-split-group spam-split-group-choice))
1232 (dolist (check spam-list-of-statistical-checks)
1233 (when (and (symbolp check)
1234 (or (symbol-value check)
1235 (memq check specific-checks)))
1237 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1238 (symbol-name check))
1240 ;; (progn (widen) (debug (buffer-string)))
1241 (let ((list-of-checks spam-list-of-checks)
1243 (while (and list-of-checks (not decision))
1244 (let ((pair (pop list-of-checks)))
1246 ;; either, given specific checks, this is one of them
1247 (and specific-checks (memq (car pair) specific-checks))
1248 ;; or, given no specific checks, spam-use-CHECK is set
1249 (and (null specific-checks) (symbol-value (car pair))))
1250 (gnus-message 6 "spam-split: calling the %s function"
1251 (symbol-name (cdr pair)))
1252 (setq decision (funcall (cdr pair)))
1253 ;; if we got a decision at all, save the current check
1255 (setq spam-split-last-successful-check (car pair)))
1257 (when (eq decision 'spam)
1258 (unless spam-split-symbolic-return
1261 (format "spam-split got %s but %s is nil"
1262 (symbol-name decision)
1263 (symbol-name spam-split-symbolic-return))))))))
1265 (if spam-split-symbolic-return-positive 'ham nil)
1268 (defun spam-find-spam ()
1269 "This function will detect spam in the current newsgroup using spam-split."
1272 (let* ((group gnus-newsgroup-name)
1273 (autodetect (gnus-parameter-spam-autodetect group))
1274 (methods (gnus-parameter-spam-autodetect-methods group))
1275 (first-method (nth 0 methods))
1276 (articles (if spam-autodetect-recheck-messages
1277 gnus-newsgroup-articles
1278 gnus-newsgroup-unseen))
1279 article-cannot-be-faked)
1281 (dolist (check spam-list-of-statistical-checks)
1282 (when (and (symbolp check)
1283 (memq check methods))
1284 (setq article-cannot-be-faked t)
1287 (when (memq 'default methods)
1288 (setq article-cannot-be-faked t))
1290 (when (and autodetect
1291 (not (equal first-method 'none)))
1294 (let ((id (spam-fetch-field-message-id-fast article))
1295 (subject (spam-fetch-field-subject-fast article))
1296 (sender (spam-fetch-field-from-fast article))
1300 (gnus-message 6 "Article %d has no message ID!" article))
1302 (when (and id spam-log-to-registry)
1303 (setq registry-lookup (spam-log-registration-type id 'incoming))
1304 (when registry-lookup
1307 "spam-find-spam: message %s was already registered incoming"
1310 (let* ((spam-split-symbolic-return t)
1311 (spam-split-symbolic-return-positive t)
1312 (fake-headers (spam-generate-fake-headers article))
1316 (if article-cannot-be-faked
1317 (gnus-request-article-this-buffer
1320 ;; else, we fake the article
1321 (when fake-headers (insert fake-headers)))
1322 (if (or (null first-method)
1323 (equal first-method 'default))
1325 (apply 'spam-split methods))))))
1326 (if (equal split-return 'spam)
1327 (gnus-summary-mark-article article gnus-spam-mark))
1329 (when (and id split-return spam-log-to-registry)
1330 (when (zerop (gnus-registry-group-count id))
1331 (gnus-registry-add-group
1332 id group subject sender))
1334 (unless registry-lookup
1335 (spam-log-processing-to-registry
1339 spam-split-last-successful-check
1343 (defvar spam-registration-functions
1344 ;; first the ham register, second the spam register function
1345 ;; third the ham unregister, fourth the spam unregister function
1346 '((spam-use-blacklist nil
1347 spam-blacklist-register-routine
1349 spam-blacklist-unregister-routine)
1350 (spam-use-whitelist spam-whitelist-register-routine
1352 spam-whitelist-unregister-routine
1354 (spam-use-ham-copy nil
1358 (spam-use-BBDB spam-BBDB-register-routine
1360 spam-BBDB-unregister-routine
1362 (spam-use-ifile spam-ifile-register-ham-routine
1363 spam-ifile-register-spam-routine
1364 spam-ifile-unregister-ham-routine
1365 spam-ifile-unregister-spam-routine)
1366 (spam-use-spamoracle spam-spamoracle-learn-ham
1367 spam-spamoracle-learn-spam
1368 spam-spamoracle-unlearn-ham
1369 spam-spamoracle-unlearn-spam)
1370 (spam-use-stat spam-stat-register-ham-routine
1371 spam-stat-register-spam-routine
1372 spam-stat-unregister-ham-routine
1373 spam-stat-unregister-spam-routine)
1374 ;; note that spam-use-gmane and spam-use-resend are not legitimate checks
1376 spam-report-gmane-register-routine
1377 ;; does Gmane support unregistration?
1380 (spam-use-resend nil
1381 spam-report-resend-register-routine
1384 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1385 spam-spamassassin-register-spam-routine
1386 spam-spamassassin-unregister-ham-routine
1387 spam-spamassassin-unregister-spam-routine)
1388 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1389 spam-bogofilter-register-spam-routine
1390 spam-bogofilter-unregister-ham-routine
1391 spam-bogofilter-unregister-spam-routine)
1392 (spam-use-bsfilter spam-bsfilter-register-ham-routine
1393 spam-bsfilter-register-spam-routine
1394 spam-bsfilter-unregister-ham-routine
1395 spam-bsfilter-unregister-spam-routine)
1396 (spam-use-crm114 spam-crm114-register-ham-routine
1397 spam-crm114-register-spam-routine
1398 ;; does CRM114 Mailfilter support unregistration?
1401 "The spam-registration-functions list contains pairs
1402 associating a parameter variable with the ham and spam
1403 registration functions, and the ham and spam unregistration
1406 (defun spam-classification-valid-p (classification)
1407 (or (eq classification 'spam)
1408 (eq classification 'ham)))
1410 (defun spam-process-type-valid-p (process-type)
1411 (or (eq process-type 'incoming)
1412 (eq process-type 'process)))
1414 (defun spam-registration-check-valid-p (check)
1415 (assoc check spam-registration-functions))
1417 (defun spam-unregistration-check-valid-p (check)
1418 (assoc check spam-registration-functions))
1420 (defun spam-registration-function (classification check)
1421 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1422 (if (eq classification 'spam)
1426 (defun spam-unregistration-function (classification check)
1427 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1428 (if (eq classification 'spam)
1432 (defun spam-list-articles (articles classification)
1433 (let ((mark-check (if (eq classification 'spam)
1434 'spam-group-spam-mark-p
1435 'spam-group-ham-mark-p))
1436 alist mark-cache-yes mark-cache-no)
1437 (dolist (article articles)
1438 (let ((mark (gnus-summary-article-mark article)))
1439 (unless (or (memq mark mark-cache-yes)
1440 (memq mark mark-cache-no))
1441 (if (funcall mark-check
1444 (push mark mark-cache-yes)
1445 (push mark mark-cache-no)))
1446 (when (memq mark mark-cache-yes)
1447 (push article alist))))
1450 (defun spam-register-routine (classification
1452 &optional unregister
1454 (when (and (spam-classification-valid-p classification)
1455 (spam-registration-check-valid-p check))
1456 (let* ((register-function
1457 (spam-registration-function classification check))
1458 (unregister-function
1459 (spam-unregistration-function classification check))
1460 (run-function (if unregister
1463 (log-function (if unregister
1464 'spam-log-undo-registration
1465 'spam-log-processing-to-registry))
1469 ;; make list of articles, using specific-articles if given
1470 (setq articles (or specific-articles
1472 gnus-newsgroup-articles
1475 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1476 (if unregister "Unregistering" "Registering")
1478 (if specific-articles "specific" "")
1479 (symbol-name classification)
1480 (symbol-name check))
1481 (funcall run-function articles)
1482 ;; now log all the registrations (or undo them, depending on unregister)
1483 (dolist (article articles)
1484 (funcall log-function
1485 (spam-fetch-field-message-id-fast article)
1489 gnus-newsgroup-name))))))
1491 ;;; log a ham- or spam-processor invocation to the registry
1492 (defun spam-log-processing-to-registry (id type classification check group)
1493 (when spam-log-to-registry
1494 (if (and (stringp id)
1496 (spam-process-type-valid-p type)
1497 (spam-classification-valid-p classification)
1498 (spam-registration-check-valid-p check))
1499 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1500 (cell (list classification check group)))
1501 (push cell cell-list)
1502 (gnus-registry-store-extra-entry
1509 (format "%s call with bad ID, type, classification, spam-check, or group"
1510 "spam-log-processing-to-registry")))))
1512 ;;; check if a ham- or spam-processor registration has been done
1513 (defun spam-log-registered-p (id type)
1514 (when spam-log-to-registry
1515 (if (and (stringp id)
1516 (spam-process-type-valid-p type))
1517 (cdr-safe (gnus-registry-fetch-extra id type))
1521 (format "%s called with bad ID, type, classification, or spam-check"
1522 "spam-log-registered-p"))
1525 ;;; check what a ham- or spam-processor registration says
1526 ;;; returns nil if conflicting registrations are found
1527 (defun spam-log-registration-type (id type)
1530 (dolist (reg (spam-log-registered-p id type))
1531 (let ((classification (nth 0 reg)))
1532 (when (spam-classification-valid-p classification)
1534 (not (eq classification decision)))
1535 (setq count (+ 1 count)))
1536 (setq decision classification))))
1542 ;;; check if a ham- or spam-processor registration needs to be undone
1543 (defun spam-log-unregistration-needed-p (id type classification check)
1544 (when spam-log-to-registry
1545 (if (and (stringp id)
1546 (spam-process-type-valid-p type)
1547 (spam-classification-valid-p classification)
1548 (spam-registration-check-valid-p check))
1549 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1551 (dolist (cell cell-list)
1553 (when (and (eq classification (nth 0 cell))
1554 (eq check (nth 1 cell)))
1560 (format "%s called with bad ID, type, classification, or spam-check"
1561 "spam-log-unregistration-needed-p"))
1565 ;;; undo a ham- or spam-processor registration (the group is not used)
1566 (defun spam-log-undo-registration (id type classification check &optional group)
1567 (when (and spam-log-to-registry
1568 (spam-log-unregistration-needed-p id type classification check))
1569 (if (and (stringp id)
1570 (spam-process-type-valid-p type)
1571 (spam-classification-valid-p classification)
1572 (spam-registration-check-valid-p check))
1573 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1574 new-cell-list found)
1575 (dolist (cell cell-list)
1576 (unless (and (eq classification (nth 0 cell))
1577 (eq check (nth 1 cell)))
1578 (push cell new-cell-list)))
1579 (gnus-registry-store-extra-entry
1584 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1585 "spam-log-undo-registration"))
1588 ;;; set up IMAP widening if it's necessary
1589 (defun spam-setup-widening ()
1590 (dolist (check spam-list-of-statistical-checks)
1591 (when (symbol-value check)
1592 (setq nnimap-split-download-body-default t))))
1596 (defun spam-check-gmane-xref ()
1598 (message-fetch-field "Xref")
1599 (message-fetch-field "Newsgroups")))
1600 (spam-split-group (if spam-split-symbolic-return
1603 (when header ; return nil when no header
1604 (when (string-match spam-gmane-xref-spam-group
1606 spam-split-group))))
1611 (defun spam-check-regex-body ()
1612 (let ((spam-regex-headers-ham spam-regex-body-ham)
1613 (spam-regex-headers-spam spam-regex-body-spam))
1614 (spam-check-regex-headers t)))
1619 (defun spam-check-regex-headers (&optional body)
1620 (let ((type (if body "body" "header"))
1621 (spam-split-group (if spam-split-symbolic-return
1625 (dolist (h-regex spam-regex-headers-ham)
1627 (goto-char (point-min))
1628 (when (re-search-forward h-regex nil t)
1629 (message "Ham regex %s search positive." type)
1631 (dolist (s-regex spam-regex-headers-spam)
1633 (goto-char (point-min))
1634 (when (re-search-forward s-regex nil t)
1635 (message "Spam regex %s search positive." type)
1637 (setq ret spam-split-group))))
1643 (defun spam-reverse-ip-string (ip)
1645 (mapconcat 'identity
1646 (nreverse (split-string ip "\\."))
1649 (defun spam-check-blackholes ()
1650 "Check the Received headers for blackholed relays."
1651 (let ((headers (message-fetch-field "received"))
1652 (spam-split-group (if spam-split-symbolic-return
1659 (goto-char (point-min))
1660 (gnus-message 6 "Checking headers for relay addresses")
1661 (while (re-search-forward
1662 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1663 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1664 (push (spam-reverse-ip-string (match-string 1))
1666 (dolist (server spam-blackhole-servers)
1668 (unless (and spam-blackhole-good-server-regex
1669 ;; match the good-server-regex against the reversed (again) IP string
1671 spam-blackhole-good-server-regex
1672 (spam-reverse-ip-string ip)))
1674 (let ((query-string (concat ip "." server)))
1676 (let ((query-result (query-dig query-string)))
1678 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1680 (push (list ip server query-result)
1682 ;; else, if not using dig.el
1683 (when (query-dns query-string)
1684 (gnus-message 6 "positive blackhole check")
1685 (push (list ip server (query-dns query-string 'TXT))
1696 (defun spam-check-hashcash ()
1697 "Check the headers for hashcash payments."
1698 (mail-check-payment))) ;mail-check-payment returns a boolean
1701 (defalias 'mail-check-payment 'ignore)
1702 (defalias 'spam-check-hashcash 'ignore))))
1706 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1707 ;;; <sacha@giotto.sj.ru>
1709 ;; all this is done inside a condition-case to trap errors
1716 ;; when the BBDB changes, we want to clear out our cache
1717 (defun spam-clear-cache-BBDB (&rest immaterial)
1718 (spam-clear-cache 'spam-use-BBDB))
1720 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1722 (defun spam-enter-ham-BBDB (addresses &optional remove)
1723 "Enter an address into the BBDB; implies ham (non-spam) sender"
1724 (dolist (from addresses)
1725 (when (stringp from)
1726 (let* ((parsed-address (gnus-extract-address-components from))
1727 (name (or (nth 0 parsed-address) "Ham Sender"))
1728 (remove-function (if remove
1729 'bbdb-delete-record-internal
1731 (net-address (nth 1 parsed-address))
1732 (record (and net-address
1733 (bbdb-search-simple nil net-address))))
1735 (gnus-message 6 "%s address %s %s BBDB"
1736 (if remove "Deleting" "Adding")
1738 (if remove "from" "to"))
1740 (funcall remove-function record)
1741 (bbdb-create-internal name nil net-address nil nil
1742 "ham sender added by spam.el")))))))
1744 (defun spam-BBDB-register-routine (articles &optional unregister)
1746 (dolist (article articles)
1747 (when (stringp (spam-fetch-field-from-fast article))
1748 (push (spam-fetch-field-from-fast article) addresses)))
1749 ;; now do the register/unregister action
1750 (spam-enter-ham-BBDB addresses unregister)))
1752 (defun spam-BBDB-unregister-routine (articles)
1753 (spam-BBDB-register-routine articles t))
1755 (defun spam-check-BBDB ()
1756 "Mail from people in the BBDB is classified as ham or non-spam"
1757 (let ((who (message-fetch-field "from"))
1758 (spam-split-group (if spam-split-symbolic-return
1761 bbdb-cache bbdb-hashtable)
1762 (when spam-cache-lookups
1763 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1766 ;; this is the expanded (bbdb-hashtable) macro
1767 ;; without the debugging support
1768 (with-current-buffer (bbdb-buffer)
1770 (save-window-excursion
1771 (bbdb-records nil t)
1773 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1775 (setq who (nth 1 (gnus-extract-address-components who)))
1777 (if spam-cache-lookups
1779 (intern-soft who bbdb-cache))
1780 (bbdb-search-simple nil who))
1782 (if spam-use-BBDB-exclusive
1787 (defalias 'bbdb-search-simple 'ignore)
1788 (defalias 'bbdb-records 'ignore)
1789 (defalias 'bbdb-buffer 'ignore)
1790 (defalias 'spam-check-BBDB 'ignore)
1791 (defalias 'spam-BBDB-register-routine 'ignore)
1792 (defalias 'spam-enter-ham-BBDB 'ignore)
1793 (defalias 'bbdb-create-internal 'ignore)
1794 (defalias 'bbdb-delete-record-internal 'ignore)
1795 (defalias 'bbdb-records 'ignore))))
1800 ;;; check the ifile backend; return nil if the mail was NOT classified
1803 (defun spam-get-ifile-database-parameter ()
1804 "Get the command-line parameter for ifile's database from
1805 spam-ifile-database-path."
1806 (if spam-ifile-database-path
1807 (format "--db-file=%s" spam-ifile-database-path)
1810 (defun spam-check-ifile ()
1811 "Check the ifile backend for the classification of this message."
1812 (let ((article-buffer-name (buffer-name))
1813 (spam-split-group (if spam-split-symbolic-return
1818 (let ((temp-buffer-name (buffer-name))
1819 (db-param (spam-get-ifile-database-parameter)))
1821 (set-buffer article-buffer-name)
1822 (apply 'call-process-region
1823 (point-min) (point-max) spam-ifile-path
1824 nil temp-buffer-name nil "-c"
1825 (if db-param `(,db-param "-q") `("-q"))))
1826 ;; check the return now (we're back in the temp buffer)
1827 (goto-char (point-min))
1829 (setq category (buffer-substring (point) (point-at-eol))))
1830 (when (not (zerop (length category))) ; we need a category here
1831 (if spam-ifile-all-categories
1832 (setq return category)
1833 ;; else, if spam-ifile-all-categories is not set...
1834 (when (string-equal spam-ifile-spam-category category)
1835 (setq return spam-split-group)))))) ; note return is nil otherwise
1838 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1839 "Register an article, given as a string, with a category.
1840 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1841 (let ((category (or category gnus-newsgroup-name))
1842 (add-or-delete-option (if unregister "-d" "-i"))
1843 (db (spam-get-ifile-database-parameter))
1846 (dolist (article articles)
1847 (let ((article-string (spam-get-article-as-string article)))
1848 (when (stringp article-string)
1849 (insert article-string))))
1850 (apply 'call-process-region
1851 (point-min) (point-max) spam-ifile-path
1853 add-or-delete-option category
1854 (if db `(,db "-h") `("-h"))))))
1856 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1857 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1859 (defun spam-ifile-unregister-spam-routine (articles)
1860 (spam-ifile-register-spam-routine articles t))
1862 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1863 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1865 (defun spam-ifile-unregister-ham-routine (articles)
1866 (spam-ifile-register-ham-routine articles t))
1873 (let ((spam-stat-install-hooks nil))
1874 (require 'spam-stat))
1876 (defun spam-check-stat ()
1877 "Check the spam-stat backend for the classification of this message"
1878 (let ((spam-split-group (if spam-split-symbolic-return
1881 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1882 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1884 (spam-stat-split-fancy)))
1886 (defun spam-stat-register-spam-routine (articles &optional unregister)
1887 (dolist (article articles)
1888 (let ((article-string (spam-get-article-as-string article)))
1890 (insert article-string)
1892 (spam-stat-buffer-change-to-non-spam)
1893 (spam-stat-buffer-is-spam))))))
1895 (defun spam-stat-unregister-spam-routine (articles)
1896 (spam-stat-register-spam-routine articles t))
1898 (defun spam-stat-register-ham-routine (articles &optional unregister)
1899 (dolist (article articles)
1900 (let ((article-string (spam-get-article-as-string article)))
1902 (insert article-string)
1904 (spam-stat-buffer-change-to-spam)
1905 (spam-stat-buffer-is-non-spam))))))
1907 (defun spam-stat-unregister-ham-routine (articles)
1908 (spam-stat-register-ham-routine articles t))
1910 (defun spam-maybe-spam-stat-load ()
1911 (when spam-use-stat (spam-stat-load)))
1913 (defun spam-maybe-spam-stat-save ()
1914 (when spam-use-stat (spam-stat-save))))
1917 (defalias 'spam-stat-load 'ignore)
1918 (defalias 'spam-stat-save 'ignore)
1919 (defalias 'spam-maybe-spam-stat-load 'ignore)
1920 (defalias 'spam-maybe-spam-stat-save 'ignore)
1921 (defalias 'spam-stat-register-ham-routine 'ignore)
1922 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1923 (defalias 'spam-stat-register-spam-routine 'ignore)
1924 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1925 (defalias 'spam-stat-buffer-is-spam 'ignore)
1926 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1927 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1928 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1929 (defalias 'spam-stat-split-fancy 'ignore)
1930 (defalias 'spam-check-stat 'ignore))))
1934 ;;;; Blacklists and whitelists.
1936 (defvar spam-whitelist-cache nil)
1937 (defvar spam-blacklist-cache nil)
1939 (defun spam-kill-whole-line ()
1941 (let ((kill-whole-line t))
1944 ;;; address can be a list, too
1945 (defun spam-enter-whitelist (address &optional remove)
1946 "Enter ADDRESS (list or single) into the whitelist.
1947 With a non-nil REMOVE, remove them."
1948 (interactive "sAddress: ")
1949 (spam-enter-list address spam-whitelist remove)
1950 (setq spam-whitelist-cache nil)
1951 (spam-clear-cache 'spam-use-whitelist))
1953 ;;; address can be a list, too
1954 (defun spam-enter-blacklist (address &optional remove)
1955 "Enter ADDRESS (list or single) into the blacklist.
1956 With a non-nil REMOVE, remove them."
1957 (interactive "sAddress: ")
1958 (spam-enter-list address spam-blacklist remove)
1959 (setq spam-blacklist-cache nil)
1960 (spam-clear-cache 'spam-use-whitelist))
1962 (defun spam-enter-list (addresses file &optional remove)
1963 "Enter ADDRESSES into the given FILE.
1964 Either the whitelist or the blacklist files can be used. With
1965 REMOVE not nil, remove the ADDRESSES."
1966 (if (stringp addresses)
1967 (spam-enter-list (list addresses) file remove)
1968 ;; else, we have a list of addresses here
1969 (unless (file-exists-p (file-name-directory file))
1970 (make-directory (file-name-directory file) t))
1973 (find-file-noselect file))
1974 (dolist (a addresses)
1976 (goto-char (point-min))
1977 (if (re-search-forward (regexp-quote a) nil t)
1978 ;; found the address
1980 (spam-kill-whole-line))
1981 ;; else, the address was not found
1983 (goto-char (point-max))
1989 (defun spam-filelist-build-cache (type)
1990 (let ((cache (if (eq type 'spam-use-blacklist)
1991 spam-blacklist-cache
1992 spam-whitelist-cache))
1994 (unless (gethash type spam-caches)
1996 (let ((address (pop cache)))
1997 (unless (zerop (length address)) ; 0 for a nil address too
1998 (setq address (regexp-quote address))
1999 ;; fix regexp-quote's treatment of user-intended regexes
2000 (while (string-match "\\\\\\*" address)
2001 (setq address (replace-match ".*" t t address))))
2002 (push address parsed-cache)))
2003 (puthash type parsed-cache spam-caches))))
2005 (defun spam-filelist-check-cache (type from)
2006 (when (stringp from)
2007 (spam-filelist-build-cache type)
2009 (dolist (address (gethash type spam-caches))
2010 (when (and address (string-match address from))
2015 ;;; returns t if the sender is in the whitelist, nil or
2016 ;;; spam-split-group otherwise
2017 (defun spam-check-whitelist ()
2018 ;; FIXME! Should it detect when file timestamps change?
2019 (let ((spam-split-group (if spam-split-symbolic-return
2022 (unless spam-whitelist-cache
2023 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2024 (if (spam-from-listed-p 'spam-use-whitelist)
2026 (if spam-use-whitelist-exclusive
2030 (defun spam-check-blacklist ()
2031 ;; FIXME! Should it detect when file timestamps change?
2032 (let ((spam-split-group (if spam-split-symbolic-return
2035 (unless spam-blacklist-cache
2036 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2037 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
2039 (defun spam-parse-list (file)
2040 (when (file-readable-p file)
2041 (let (contents address)
2043 (insert-file-contents file)
2045 (setq address (buffer-substring (point) (point-at-eol)))
2047 ;; insert the e-mail address if detected, otherwise the raw data
2048 (unless (zerop (length address))
2049 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2050 (push (or pure-address address) contents)))))
2051 (nreverse contents))))
2053 (defun spam-from-listed-p (type)
2054 (let ((from (message-fetch-field "from"))
2056 (spam-filelist-check-cache type from)))
2058 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2059 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2060 (declassification (if blacklist 'ham 'spam))
2062 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2064 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2065 from addresses unregister-list article-unregister-list)
2066 (dolist (article articles)
2067 (let ((from (spam-fetch-field-from-fast article))
2068 (id (spam-fetch-field-message-id-fast article))
2070 (when (stringp from)
2071 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2072 (when (and (not sender-ignored)
2073 (stringp ignore-regex)
2074 (string-match ignore-regex from))
2075 (setq sender-ignored t)))
2076 ;; remember the messages we need to unregister, unless remove is set
2079 (spam-log-unregistration-needed-p
2080 id 'process declassification de-symbol))
2081 (push article article-unregister-list)
2082 (push from unregister-list))
2083 (unless sender-ignored
2084 (push from addresses)))))
2087 (funcall enter-function addresses t) ; unregister all these addresses
2088 ;; else, register normally and unregister what we need to
2089 (funcall remove-function unregister-list t)
2090 (dolist (article article-unregister-list)
2091 (spam-log-undo-registration
2092 (spam-fetch-field-message-id-fast article)
2096 (funcall enter-function addresses nil))))
2098 (defun spam-blacklist-unregister-routine (articles)
2099 (spam-blacklist-register-routine articles t))
2101 (defun spam-blacklist-register-routine (articles &optional unregister)
2102 (spam-filelist-register-routine articles t unregister))
2104 (defun spam-whitelist-unregister-routine (articles)
2105 (spam-whitelist-register-routine articles t))
2107 (defun spam-whitelist-register-routine (articles &optional unregister)
2108 (spam-filelist-register-routine articles nil unregister))
2111 ;;;; Spam-report glue
2112 (defun spam-report-gmane-register-routine (articles)
2114 (apply 'spam-report-gmane articles)))
2116 (defun spam-report-resend-register-routine (articles)
2117 (let ((spam-report-resend-to (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2118 (spam-report-resend articles)))
2122 (defun spam-check-bogofilter-headers (&optional score)
2123 (let ((header (message-fetch-field spam-bogofilter-header))
2124 (spam-split-group (if spam-split-symbolic-return
2127 (when header ; return nil when no header
2128 (if score ; scoring mode
2129 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2130 (match-string 1 header)
2132 ;; spam detection mode
2133 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2135 spam-split-group)))))
2137 ;; return something sensible if the score can't be determined
2138 (defun spam-bogofilter-score (&optional recheck)
2139 "Get the Bogofilter spamicity score"
2141 (save-window-excursion
2142 (gnus-summary-show-article t)
2143 (set-buffer gnus-article-buffer)
2144 (let ((score (or (unless recheck
2145 (spam-check-bogofilter-headers t))
2146 (spam-check-bogofilter t))))
2147 (gnus-summary-show-article)
2148 (message "Spamicity score %s" score)
2151 (defun spam-check-bogofilter (&optional score)
2152 "Check the Bogofilter backend for the classification of this message"
2153 (let ((article-buffer-name (buffer-name))
2154 (db spam-bogofilter-database-directory)
2157 (let ((temp-buffer-name (buffer-name)))
2159 (set-buffer article-buffer-name)
2160 (apply 'call-process-region
2161 (point-min) (point-max)
2162 spam-bogofilter-path
2163 nil temp-buffer-name nil
2164 (if db `("-d" ,db "-v") `("-v"))))
2165 (setq return (spam-check-bogofilter-headers score))))
2168 (defun spam-bogofilter-register-with-bogofilter (articles
2170 &optional unregister)
2171 "Register an article, given as a string, as spam or non-spam."
2172 (dolist (article articles)
2173 (let ((article-string (spam-get-article-as-string article))
2174 (db spam-bogofilter-database-directory)
2175 (switch (if unregister
2177 spam-bogofilter-spam-strong-switch
2178 spam-bogofilter-ham-strong-switch)
2180 spam-bogofilter-spam-switch
2181 spam-bogofilter-ham-switch))))
2182 (when (stringp article-string)
2184 (insert article-string)
2186 (apply 'call-process-region
2187 (point-min) (point-max)
2188 spam-bogofilter-path
2190 (if db `("-d" ,db "-v") `("-v"))))))))
2192 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2193 (spam-bogofilter-register-with-bogofilter articles t unregister))
2195 (defun spam-bogofilter-unregister-spam-routine (articles)
2196 (spam-bogofilter-register-spam-routine articles t))
2198 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2199 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2201 (defun spam-bogofilter-unregister-ham-routine (articles)
2202 (spam-bogofilter-register-ham-routine articles t))
2207 (defun spam-check-spamoracle ()
2208 "Run spamoracle on an article to determine whether it's spam."
2209 (let ((article-buffer-name (buffer-name))
2210 (spam-split-group (if spam-split-symbolic-return
2214 (let ((temp-buffer-name (buffer-name)))
2216 (set-buffer article-buffer-name)
2218 (apply 'call-process-region
2219 (point-min) (point-max)
2220 spam-spamoracle-binary
2221 nil temp-buffer-name nil
2222 (if spam-spamoracle-database
2223 `("-f" ,spam-spamoracle-database "mark")
2227 (set-buffer temp-buffer-name)
2228 (goto-char (point-min))
2229 (when (re-search-forward "^X-Spam: yes;" nil t)
2231 (error "Error running spamoracle: %s" status))))))))
2233 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2234 "Run spamoracle in training mode."
2236 (let ((temp-buffer-name (buffer-name)))
2238 (goto-char (point-min))
2239 (dolist (article articles)
2240 (insert (spam-get-article-as-string article)))
2241 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2245 (apply 'call-process-region
2246 (point-min) (point-max)
2247 spam-spamoracle-binary
2248 nil temp-buffer-name nil
2249 (if spam-spamoracle-database
2250 `("-f" ,spam-spamoracle-database
2253 (unless (eq 0 status)
2254 (error "Error running spamoracle: %s" status)))))))
2256 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2257 (spam-spamoracle-learn articles nil unregister))
2259 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2260 (spam-spamoracle-learn-ham articles t))
2262 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2263 (spam-spamoracle-learn articles t unregister))
2265 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2266 (spam-spamoracle-learn-spam articles t))
2270 ;;; based mostly on the bogofilter code
2271 (defun spam-check-spamassassin-headers (&optional score)
2272 "Check the SpamAssassin headers for the classification of this message."
2273 (if score ; scoring mode
2274 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2276 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2277 (match-string 1 header)
2279 ;; spam detection mode
2280 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2281 (spam-split-group (if spam-split-symbolic-return
2284 (when header ; return nil when no header
2285 (when (string-match spam-spamassassin-positive-spam-flag-header
2287 spam-split-group)))))
2289 (defun spam-check-spamassassin (&optional score)
2290 "Check the SpamAssassin backend for the classification of this message."
2291 (let ((article-buffer-name (buffer-name)))
2293 (let ((temp-buffer-name (buffer-name)))
2295 (set-buffer article-buffer-name)
2296 (apply 'call-process-region
2297 (point-min) (point-max) spam-spamassassin-path
2298 nil temp-buffer-name nil spam-spamassassin-arguments))
2299 ;; check the return now (we're back in the temp buffer)
2300 (goto-char (point-min))
2301 (spam-check-spamassassin-headers score)))))
2303 ;; return something sensible if the score can't be determined
2304 (defun spam-spamassassin-score (&optional recheck)
2305 "Get the SpamAssassin score"
2307 (save-window-excursion
2308 (gnus-summary-show-article t)
2309 (set-buffer gnus-article-buffer)
2310 (let ((score (or (unless recheck
2311 (spam-check-spamassassin-headers t))
2312 (spam-check-spamassassin t))))
2313 (gnus-summary-show-article)
2314 (message "SpamAssassin score %s" score)
2317 (defun spam-spamassassin-register-with-sa-learn (articles spam
2318 &optional unregister)
2319 "Register articles with spamassassin's sa-learn as spam or non-spam."
2321 (let ((action (if unregister spam-sa-learn-unregister-switch
2322 (if spam spam-sa-learn-spam-switch
2323 spam-sa-learn-ham-switch)))
2324 (summary-buffer-name (buffer-name)))
2326 ;; group the articles into mbox format
2327 (dolist (article articles)
2328 (let (article-string)
2330 (set-buffer summary-buffer-name)
2331 (setq article-string (spam-get-article-as-string article)))
2332 (when (stringp article-string)
2333 (insert "From \n") ; mbox separator (sa-learn only checks the
2334 ; first five chars, so we can get away with
2336 (insert article-string)
2338 ;; call sa-learn on all messages at the same time
2339 (apply 'call-process-region
2340 (point-min) (point-max)
2342 nil nil nil "--mbox"
2343 (if spam-sa-learn-rebuild
2345 `("--no-rebuild" ,action)))))))
2347 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2348 (spam-spamassassin-register-with-sa-learn articles t unregister))
2350 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2351 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2353 (defun spam-spamassassin-unregister-spam-routine (articles)
2354 (spam-spamassassin-register-with-sa-learn articles t t))
2356 (defun spam-spamassassin-unregister-ham-routine (articles)
2357 (spam-spamassassin-register-with-sa-learn articles nil t))
2361 ;;; based mostly on the bogofilter code
2362 (defun spam-check-bsfilter-headers (&optional score)
2364 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2366 (let ((header (nnmail-fetch-field spam-bsfilter-header))
2367 (spam-split-group (if spam-split-symbolic-return
2370 (when header ; return nil when no header
2371 (when (string-match "YES" header)
2372 spam-split-group)))))
2374 ;; return something sensible if the score can't be determined
2375 (defun spam-bsfilter-score (&optional recheck)
2376 "Get the Bsfilter spamicity score"
2378 (save-window-excursion
2379 (gnus-summary-show-article t)
2380 (set-buffer gnus-article-buffer)
2381 (let ((score (or (unless recheck
2382 (spam-check-bsfilter-headers t))
2383 (spam-check-bsfilter t))))
2384 (gnus-summary-show-article)
2385 (message "Spamicity score %s" score)
2388 (defun spam-check-bsfilter (&optional score)
2389 "Check the Bsfilter backend for the classification of this message"
2390 (let ((article-buffer-name (buffer-name))
2391 (dir spam-bsfilter-database-directory)
2394 (let ((temp-buffer-name (buffer-name)))
2396 (set-buffer article-buffer-name)
2397 (apply 'call-process-region
2398 (point-min) (point-max)
2400 nil temp-buffer-name nil
2403 "--insert-probability"
2405 (list "--homedir" dir))))
2406 (setq return (spam-check-bsfilter-headers score))))
2409 (defun spam-bsfilter-register-with-bsfilter (articles
2411 &optional unregister)
2412 "Register an article, given as a string, as spam or non-spam."
2413 (dolist (article articles)
2414 (let ((article-string (spam-get-article-as-string article))
2415 (switch (if unregister
2417 spam-bsfilter-spam-strong-switch
2418 spam-bsfilter-ham-strong-switch)
2420 spam-bsfilter-spam-switch
2421 spam-bsfilter-ham-switch))))
2422 (when (stringp article-string)
2424 (insert article-string)
2425 (apply 'call-process-region
2426 (point-min) (point-max)
2430 (when spam-bsfilter-database-directory
2432 spam-bsfilter-database-directory))))))))
2434 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2435 (spam-bsfilter-register-with-bsfilter articles t unregister))
2437 (defun spam-bsfilter-unregister-spam-routine (articles)
2438 (spam-bsfilter-register-spam-routine articles t))
2440 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2441 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2443 (defun spam-bsfilter-unregister-ham-routine (articles)
2444 (spam-bsfilter-register-ham-routine articles t))
2447 ;;;; CRM114 Mailfilter
2448 (defun spam-check-crm114-headers (&optional score)
2449 (let ((header (message-fetch-field spam-crm114-header))
2450 (spam-split-group (if spam-split-symbolic-return
2453 (when header ; return nil when no header
2454 (if score ; scoring mode
2455 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2456 (match-string 1 header)
2458 ;; spam detection mode
2459 (when (string-match spam-crm114-positive-spam-header
2461 spam-split-group)))))
2463 ;; return something sensible if the score can't be determined
2464 (defun spam-crm114-score ()
2465 "Get the CRM114 Mailfilter pR"
2467 (save-window-excursion
2468 (gnus-summary-show-article t)
2469 (set-buffer gnus-article-buffer)
2470 (let ((score (or (spam-check-crm114-headers t)
2471 (spam-check-crm114 t))))
2472 (gnus-summary-show-article)
2473 (message "pR: %s" score)
2476 (defun spam-check-crm114 (&optional score)
2477 "Check the CRM114 Mailfilter backend for the classification of this message"
2478 (let ((article-buffer-name (buffer-name))
2479 (db spam-crm114-database-directory)
2482 (let ((temp-buffer-name (buffer-name)))
2484 (set-buffer article-buffer-name)
2485 (apply 'call-process-region
2486 (point-min) (point-max)
2488 nil temp-buffer-name nil
2489 (when db (list (concat "--fileprefix=" db)))))
2490 (setq return (spam-check-crm114-headers score))))
2493 (defun spam-crm114-register-with-crm114 (articles
2495 &optional unregister)
2496 "Register an article, given as a string, as spam or non-spam."
2497 (dolist (article articles)
2498 (let ((article-string (spam-get-article-as-string article))
2499 (db spam-crm114-database-directory)
2500 (switch (if unregister
2502 spam-crm114-spam-strong-switch
2503 spam-crm114-ham-strong-switch)
2505 spam-crm114-spam-switch
2506 spam-crm114-ham-switch))))
2507 (when (stringp article-string)
2509 (insert article-string)
2511 (apply 'call-process-region
2512 (point-min) (point-max)
2515 (when db (list switch (concat "--fileprefix=" db)))))))))
2517 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2518 (spam-crm114-register-with-crm114 articles t unregister))
2520 (defun spam-crm114-unregister-spam-routine (articles)
2521 (spam-crm114-register-spam-routine articles t))
2523 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2524 (spam-crm114-register-with-crm114 articles nil unregister))
2526 (defun spam-crm114-unregister-ham-routine (articles)
2527 (spam-crm114-register-ham-routine articles t))
2533 (defun spam-initialize (&rest symbols)
2534 "Install the spam.el hooks and do other initialization.
2535 When SYMBOLS is given, set those variables to t. This is so you
2536 can call spam-initialize before you set spam-use-* variables on
2537 explicitly, and matters only if you need the extra headers
2538 installed through spam-necessary-extra-headers."
2541 (dolist (var symbols)
2544 (dolist (header (spam-necessary-extra-headers))
2545 (add-to-list 'nnmail-extra-headers header)
2546 (add-to-list 'gnus-extra-headers header))
2548 (setq spam-install-hooks t)
2549 ;; TODO: How do we redo this every time spam-face is customized?
2550 (push '((eq mark gnus-spam-mark) . spam-face)
2551 gnus-summary-highlight)
2552 ;; Add hooks for loading and saving the spam stats
2553 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2554 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2555 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2556 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2557 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2558 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2559 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2561 (defun spam-unload-hook ()
2562 "Uninstall the spam.el hooks"
2564 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2565 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2566 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2567 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2568 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2569 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2570 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2572 (when spam-install-hooks
2577 ;;; spam.el ends here