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-process-ham-in-nonham-groups nil
91 "Whether ham should be processed in non-ham groups."
95 (defcustom spam-log-to-registry nil
96 "Whether spam/ham processing should be logged in the registry."
100 (defcustom spam-split-symbolic-return nil
101 "Whether `spam-split' should work with symbols or group names."
105 (defcustom spam-split-symbolic-return-positive nil
106 "Whether `spam-split' should ALWAYS work with symbols or group names.
107 Do not set this if you use `spam-split' in a fancy split
112 (defcustom spam-process-ham-in-spam-groups nil
113 "Whether ham should be processed in spam groups."
117 (defcustom spam-mark-only-unseen-as-spam t
118 "Whether only unseen articles should be marked as spam in spam groups.
119 When nil, all unread articles in a spam group are marked as
120 spam. Set this if you want to leave an article unread in a spam group
121 without losing it to the automatic spam-marking process."
125 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
126 "Whether ham should be marked unread before it's moved.
127 The article is moved out of a spam group according to ham-process-destination.
128 This variable is an official entry in the international Longest Variable Name
133 (defcustom spam-disable-spam-split-during-ham-respool nil
134 "Whether `spam-split' should be ignored while resplitting ham.
135 This is useful to prevent ham from ending up in the same spam
136 group after the resplit. Don't set this to t if you have `spam-split' as the
137 last rule in your split configuration."
141 (defcustom spam-autodetect-recheck-messages nil
142 "Should spam.el recheck all meessages when autodetecting?
143 Normally this is nil, so only unseen messages will be checked."
147 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
148 "The location of the whitelist.
149 The file format is one regular expression per line.
150 The regular expression is matched against the address."
154 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
155 "The location of the blacklist.
156 The file format is one regular expression per line.
157 The regular expression is matched against the address."
161 (defcustom spam-use-dig t
162 "Whether `query-dig' should be used instead of `query-dns'."
166 (defcustom spam-use-gmane-xref nil
167 "Whether the Gmane spam xref should be used by `spam-split'."
171 (defcustom spam-use-blacklist nil
172 "Whether the blacklist should be used by `spam-split'."
176 (defcustom spam-blacklist-ignored-regexes nil
177 "Regular expressions that the blacklist should ignore."
178 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
181 (defcustom spam-use-whitelist nil
182 "Whether the whitelist should be used by `spam-split'."
186 (defcustom spam-use-whitelist-exclusive nil
187 "Whether whitelist-exclusive should be used by `spam-split'.
188 Exclusive whitelisting means that all messages from senders not in the whitelist
189 are considered spam."
193 (defcustom spam-use-blackholes nil
194 "Whether blackholes should be used by `spam-split'."
198 (defcustom spam-use-hashcash nil
199 "Whether hashcash payments should be detected by `spam-split'."
203 (defcustom spam-use-regex-headers nil
204 "Whether a header regular expression match should be used by `spam-split'.
205 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
209 (defcustom spam-use-regex-body nil
210 "Whether a body regular expression match should be used by `spam-split'.
211 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
215 (defcustom spam-use-bogofilter-headers nil
216 "Whether bogofilter headers should be used by `spam-split'.
217 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
221 (defcustom spam-use-bogofilter nil
222 "Whether bogofilter should be invoked by `spam-split'.
223 Enable this if you want Gnus to invoke Bogofilter on new messages."
227 (defcustom spam-use-bsfilter-headers nil
228 "Whether bsfilter headers should be used by `spam-split'.
229 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
233 (defcustom spam-use-bsfilter nil
234 "Whether bsfilter should be invoked by `spam-split'.
235 Enable this if you want Gnus to invoke Bsfilter on new messages."
239 (defcustom spam-use-BBDB nil
240 "Whether BBDB should be used by `spam-split'."
244 (defcustom spam-use-BBDB-exclusive nil
245 "Whether BBDB-exclusive should be used by `spam-split'.
246 Exclusive BBDB means that all messages from senders not in the BBDB are
251 (defcustom spam-use-ifile nil
252 "Whether ifile should be used by `spam-split'."
256 (defcustom spam-use-stat nil
257 "Whether `spam-stat' should be used by `spam-split'."
261 (defcustom spam-use-spamoracle nil
262 "Whether spamoracle should be used by `spam-split'."
266 (defcustom spam-use-spamassassin nil
267 "Whether spamassassin should be invoked by `spam-split'.
268 Enable this if you want Gnus to invoke SpamAssassin on new messages."
272 (defcustom spam-use-spamassassin-headers nil
273 "Whether spamassassin headers should be checked by `spam-split'.
274 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
279 (defcustom spam-use-crm114 nil
280 "Whether the CRM114 Mailfilter should be used by `spam-split'."
284 (defcustom spam-install-hooks (or
289 spam-use-whitelist-exclusive
292 spam-use-regex-headers
295 spam-use-bogofilter-headers
296 spam-use-spamassassin
297 spam-use-spamassassin-headers
299 spam-use-bsfilter-headers
301 spam-use-BBDB-exclusive
306 "Whether the spam hooks should be installed.
307 Default to t if one of the spam-use-* variables is set."
311 (defcustom spam-split-group "spam"
312 "Group name where incoming spam should be put by `spam-split'."
316 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
317 ;;; not regular expressions
318 (defcustom spam-junk-mailgroups (cons
320 '("mail.junk" "poste.pourriel"))
321 "Mailgroups with spam contents.
322 All unmarked article in such group receive the spam mark on group entry."
323 :type '(repeat (string :tag "Group"))
327 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
328 "The group where spam xrefs can be found on Gmane.
329 Only meaningful if you enable `spam-use-gmane-xref'."
333 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
334 "dev.null.dk" "relays.visi.com")
335 "List of blackhole servers.
336 Only meaningful if you enable `spam-use-blackholes'."
337 :type '(repeat (string :tag "Server"))
340 (defcustom spam-blackhole-good-server-regex nil
341 "String matching IP addresses that should not be checked in the blackholes.
342 Only meaningful if you enable `spam-use-blackholes'."
343 :type '(radio (const nil)
344 (regexp :format "%t: %v\n" :size 0))
347 (defcustom spam-face 'gnus-splash-face
348 "Face for spam-marked articles."
352 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
353 "Regular expression for positive header spam matches.
354 Only meaningful if you enable `spam-use-regex-headers'."
355 :type '(repeat (regexp :tag "Regular expression to match spam header"))
358 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
359 "Regular expression for positive header ham matches.
360 Only meaningful if you enable `spam-use-regex-headers'."
361 :type '(repeat (regexp :tag "Regular expression to match ham header"))
364 (defcustom spam-regex-body-spam '()
365 "Regular expression for positive body spam matches.
366 Only meaningful if you enable `spam-use-regex-body'."
367 :type '(repeat (regexp :tag "Regular expression to match spam body"))
370 (defcustom spam-regex-body-ham '()
371 "Regular expression for positive body ham matches.
372 Only meaningful if you enable `spam-use-regex-body'."
373 :type '(repeat (regexp :tag "Regular expression to match ham body"))
376 (defgroup spam-ifile nil
377 "Spam ifile configuration."
380 (defcustom spam-ifile-path (executable-find "ifile")
381 "File path of the ifile executable program."
382 :type '(choice (file :tag "Location of ifile")
383 (const :tag "ifile is not installed"))
386 (defcustom spam-ifile-database-path nil
387 "File path of the ifile database."
388 :type '(choice (file :tag "Location of the ifile database")
389 (const :tag "Use the default"))
392 (defcustom spam-ifile-spam-category "spam"
393 "Name of the spam ifile category."
397 (defcustom spam-ifile-ham-category nil
398 "Name of the ham ifile category.
399 If nil, the current group name will be used."
400 :type '(choice (string :tag "Use a fixed category")
401 (const :tag "Use the current group name"))
404 (defcustom spam-ifile-all-categories nil
405 "Whether the ifile check will return all categories, or just spam.
406 Set this to t if you want to use the `spam-split' invocation of ifile as
407 your main source of newsgroup names."
411 (defgroup spam-bogofilter nil
412 "Spam bogofilter configuration."
415 (defcustom spam-bogofilter-path (executable-find "bogofilter")
416 "File path of the Bogofilter executable program."
417 :type '(choice (file :tag "Location of bogofilter")
418 (const :tag "Bogofilter is not installed"))
419 :group 'spam-bogofilter)
421 (defcustom spam-bogofilter-header "X-Bogosity"
422 "The header that Bogofilter inserts in messages."
424 :group 'spam-bogofilter)
426 (defcustom spam-bogofilter-spam-switch "-s"
427 "The switch that Bogofilter uses to register spam messages."
429 :group 'spam-bogofilter)
431 (defcustom spam-bogofilter-ham-switch "-n"
432 "The switch that Bogofilter uses to register ham messages."
434 :group 'spam-bogofilter)
436 (defcustom spam-bogofilter-spam-strong-switch "-S"
437 "The switch that Bogofilter uses to unregister ham messages."
439 :group 'spam-bogofilter)
441 (defcustom spam-bogofilter-ham-strong-switch "-N"
442 "The switch that Bogofilter uses to unregister spam messages."
444 :group 'spam-bogofilter)
446 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
447 "The regex on `spam-bogofilter-header' for positive spam identification."
449 :group 'spam-bogofilter)
451 (defcustom spam-bogofilter-database-directory nil
452 "Directory path of the Bogofilter databases."
453 :type '(choice (directory
454 :tag "Location of the Bogofilter database directory")
455 (const :tag "Use the default"))
456 :group 'spam-bogofilter)
458 (defgroup spam-bsfilter nil
459 "Spam bsfilter configuration."
462 (defcustom spam-bsfilter-path (executable-find "bsfilter")
463 "File path of the Bsfilter executable program."
464 :type '(choice (file :tag "Location of bsfilter")
465 (const :tag "Bsfilter is not installed"))
466 :group 'spam-bsfilter)
468 (defcustom spam-bsfilter-header "X-Spam-Flag"
469 "The header inserted by Bsfilter to flag spam."
471 :group 'spam-bsfilter)
473 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
474 "The header that Bsfilter inserts in messages."
476 :group 'spam-bsfilter)
478 (defcustom spam-bsfilter-spam-switch "--add-spam"
479 "The switch that Bsfilter uses to register spam messages."
481 :group 'spam-bsfilter)
483 (defcustom spam-bsfilter-ham-switch "--add-ham"
484 "The switch that Bsfilter uses to register ham messages."
486 :group 'spam-bsfilter)
488 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
489 "The switch that Bsfilter uses to unregister ham messages."
491 :group 'spam-bsfilter)
493 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
494 "The switch that Bsfilter uses to unregister spam messages."
496 :group 'spam-bsfilter)
498 (defcustom spam-bsfilter-database-directory nil
499 "Directory path of the Bsfilter databases."
500 :type '(choice (directory
501 :tag "Location of the Bsfilter database directory")
502 (const :tag "Use the default"))
503 :group 'spam-bsfilter)
505 (defgroup spam-spamoracle nil
506 "Spam spamoracle configuration."
509 (defcustom spam-spamoracle-database nil
510 "Location of spamoracle database file.
511 When nil, use the default spamoracle database."
512 :type '(choice (directory :tag "Location of spamoracle database file.")
513 (const :tag "Use the default"))
514 :group 'spam-spamoracle)
516 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
517 "Location of the spamoracle binary."
518 :type '(choice (directory :tag "Location of the spamoracle binary")
519 (const :tag "Use the default"))
520 :group 'spam-spamoracle)
522 (defgroup spam-spamassassin nil
523 "Spam SpamAssassin configuration."
526 (defcustom spam-spamassassin-path (executable-find "spamassassin")
527 "File path of the spamassassin executable program.
528 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
529 spamd man pages for more information on these programs."
530 :type '(choice (file :tag "Location of spamc")
531 (const :tag "spamassassin is not installed"))
532 :group 'spam-spamassassin)
534 (defcustom spam-spamassassin-arguments ()
535 "Arguments to pass to the spamassassin executable.
536 This must be a list. For example, `(\"-C\" \"configfile\")'."
537 :type '(restricted-sexp :match-alternatives (listp))
538 :group 'spam-spamassassin)
540 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
541 "The header inserted by SpamAssassin to flag spam."
543 :group 'spam-spamassassin)
545 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
546 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
549 :group 'spam-spamassassin)
551 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
552 "The header inserted by SpamAssassin, giving extended scoring information"
554 :group 'spam-spamassassin)
556 (defcustom spam-sa-learn-path (executable-find "sa-learn")
557 "File path of the sa-learn executable program."
558 :type '(choice (file :tag "Location of spamassassin")
559 (const :tag "spamassassin is not installed"))
560 :group 'spam-spamassassin)
562 (defcustom spam-sa-learn-rebuild t
563 "Whether sa-learn should rebuild the database every time it is called
564 Enable this if you want sa-learn to rebuild the database automatically. Doing
565 this will slightly increase the running time of the spam registration process.
566 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
567 order for SpamAssassin to recognize the new registered spam."
569 :group 'spam-spamassassin)
571 (defcustom spam-sa-learn-spam-switch "--spam"
572 "The switch that sa-learn uses to register spam messages"
574 :group 'spam-spamassassin)
576 (defcustom spam-sa-learn-ham-switch "--ham"
577 "The switch that sa-learn uses to register ham messages"
579 :group 'spam-spamassassin)
581 (defcustom spam-sa-learn-unregister-switch "--forget"
582 "The switch that sa-learn uses to unregister messages messages"
584 :group 'spam-spamassassin)
586 (defgroup spam-crm114 nil
587 "Spam CRM114 Mailfilter configuration."
590 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
591 "File path of the CRM114 Mailfilter executable program."
592 :type '(choice (file :tag "Location of CRM114 Mailfilter")
593 (const :tag "CRM114 Mailfilter is not installed"))
596 (defcustom spam-crm114-header "X-CRM114-Status"
597 "The header that CRM114 Mailfilter inserts in messages."
601 (defcustom spam-crm114-spam-switch "--learnspam"
602 "The switch that CRM114 Mailfilter uses to register spam messages."
606 (defcustom spam-crm114-ham-switch "--learnnonspam"
607 "The switch that CRM114 Mailfilter uses to register ham messages."
611 (defcustom spam-crm114-spam-strong-switch "--UNKNOWN"
612 "The switch that CRM114 Mailfilter uses to unregister ham messages."
616 (defcustom spam-crm114-ham-strong-switch "--UNKNOWN"
617 "The switch that CRM114 Mailfilter uses to unregister spam messages."
621 (defcustom spam-crm114-positive-spam-header "^SPAM"
622 "The regex on `spam-crm114-header' for positive spam identification."
626 (defcustom spam-crm114-database-directory nil
627 "Directory path of the CRM114 Mailfilter databases."
628 :type '(choice (directory
629 :tag "Location of the CRM114 Mailfilter database directory")
630 (const :tag "Use the default"))
633 ;;; Key bindings for spam control.
635 (gnus-define-keys gnus-summary-mode-map
636 "St" spam-generic-score
637 "Sx" gnus-summary-mark-as-spam
638 "Mst" spam-generic-score
639 "Msx" gnus-summary-mark-as-spam
640 "\M-d" gnus-summary-mark-as-spam)
642 (defvar spam-cache-lookups t
643 "Whether spam.el will try to cache lookups using `spam-caches'.")
645 (defvar spam-caches (make-hash-table
648 "Cache of spam detection entries.")
650 (defvar spam-old-ham-articles nil
651 "List of old ham articles, generated when a group is entered.")
653 (defvar spam-old-spam-articles nil
654 "List of old spam articles, generated when a group is entered.")
656 (defvar spam-split-disabled nil
657 "If non-nil, `spam-split' is disabled, and always returns nil.")
659 (defvar spam-split-last-successful-check nil
661 `spam-split' will set this to nil or a spam-use-XYZ check if it
664 ;; convenience functions
665 (defun spam-clear-cache (symbol)
666 "Clear the spam-caches entry for a check."
667 (remhash symbol spam-caches))
669 (defun spam-xor (a b)
671 (and (or a b) (not (and a b))))
673 (defun spam-group-ham-mark-p (group mark &optional spam)
674 "Checks if MARK is considered a ham mark in GROUP."
675 (when (stringp group)
676 (let* ((marks (spam-group-ham-marks group spam))
677 (marks (if (symbolp mark)
679 (mapcar 'symbol-value marks))))
682 (defun spam-group-spam-mark-p (group mark)
683 "Checks if MARK is considered a spam mark in GROUP."
684 (spam-group-ham-mark-p group mark t))
686 (defun spam-group-ham-marks (group &optional spam)
687 "In GROUP, get all the ham marks."
688 (when (stringp group)
689 (let* ((marks (if spam
690 (gnus-parameter-spam-marks group)
691 (gnus-parameter-ham-marks group)))
693 (marks (if (listp (car marks)) (car marks) marks)))
696 (defun spam-group-spam-marks (group)
697 "In GROUP, get all the spam marks."
698 (spam-group-ham-marks group t))
700 (defun spam-group-spam-contents-p (group)
701 "Is GROUP a spam group?"
703 (or (member group spam-junk-mailgroups)
704 (memq 'gnus-group-spam-classification-spam
705 (gnus-parameter-spam-contents group)))
708 (defun spam-group-ham-contents-p (group)
709 "Is GROUP a ham group?"
711 (memq 'gnus-group-spam-classification-ham
712 (gnus-parameter-spam-contents group))
715 (defvar spam-list-of-processors
716 ;; note the nil processors are not defined in gnus.el
717 '((nil spam spam-use-gmane)
718 (nil spam spam-use-resend)
719 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
720 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
721 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
722 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
723 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
724 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
725 (nil spam spam-use-crm114)
726 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
727 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
728 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
729 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
730 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
731 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
732 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
733 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
734 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
735 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle)
736 (nil ham spam-use-crm114))
737 "The `spam-list-of-processors' list.
738 This list contains pairs associating the obsolete ham/spam exit
739 processor variables with a classification and a spam-use-*
740 variable. When the processor variable is nil, just the
741 classification and spam-use-* check variable are used.")
743 (defun spam-group-processor-p (group check &optional classification)
744 "Checks if GROUP has a CHECK with CLASSIFICATION registered.
745 Also accepts the obsolete processors, which can be found in
746 gnus.el and in spam-list-of-processors."
747 (if (and (stringp group)
749 (let ((old-style (assq check spam-list-of-processors))
750 (parameters (nth 0 (gnus-parameter-spam-process group)))
752 (if old-style ; old-style processor
753 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
754 ;; now search for the parameter
755 (dolist (parameter parameters)
756 (when (and (null found)
758 (eq classification (nth 0 parameter))
759 (eq check (nth 1 parameter)))
764 (defun spam-report-articles-gmane (n)
765 "Report the current message as spam via Gmane.
766 Respects the process/prefix convention."
768 (dolist (article (gnus-summary-work-articles n))
769 (gnus-summary-remove-process-mark article)
770 (spam-report-gmane article)))
772 (defun spam-report-articles-resend (n)
773 "Report the current message as spam by resending it.
774 Respects the process/prefix convention. Also see
775 `spam-report-resend-to'."
777 (let ((spam-report-resend-to
778 (gnus-parameter-spam-resend-to gnus-newsgroup-name))
779 (articles (gnus-summary-work-articles n)))
780 (spam-report-resend articles)
781 (dolist (article articles)
782 (gnus-summary-remove-process-mark article))))
784 (defun spam-necessary-extra-headers ()
785 "Return the extra headers spam.el thinks are necessary."
787 (when (or spam-use-spamassassin
788 spam-use-spamassassin-headers
789 spam-use-regex-headers)
790 (push 'X-Spam-Status list))
793 (defun spam-user-format-function-S (headers)
795 (spam-summary-score headers)))
797 (defun spam-article-sort-by-spam-status (h1 h2)
798 "Sort articles by score."
800 (dolist (header (spam-necessary-extra-headers))
801 (let ((s1 (spam-summary-score h1 header))
802 (s2 (spam-summary-score h2 header)))
804 (setq result (< s1 s2))
808 (defun spam-extra-header-to-number (header headers)
809 "Transform an extra header to a number."
810 (if (gnus-extra-header header headers)
812 ((eq header 'X-Spam-Status)
813 (string-to-number (gnus-replace-in-string
814 (gnus-extra-header header headers)
816 ;; for CRM checking, it's probably faster to just do the string match
817 ((and spam-use-crm114 (string-match "( pR: \\([0-9.-]+\\)" header))
818 (match-string 1 header))
822 (defun spam-summary-score (headers &optional specific-header)
823 "Score an article for the summary buffer, as fast as possible.
824 With SPECIFIC-HEADER, returns only that header's score.
825 Will not return a nil score."
829 (list specific-header)
830 (spam-necessary-extra-headers)))
832 (spam-extra-header-to-number header headers))
837 (defun spam-generic-score (&optional recheck)
838 "Invoke whatever scoring method we can."
841 ((or spam-use-spamassassin spam-use-spamassassin-headers)
842 (spam-spamassassin-score recheck))
843 ((or spam-use-bsfilter spam-use-bsfilter-headers)
844 (spam-bsfilter-score recheck))
847 (t (spam-bogofilter-score recheck))))
849 ;;; Summary entry and exit processing.
851 (defun spam-summary-prepare ()
852 (setq spam-old-ham-articles
853 (spam-list-articles gnus-newsgroup-articles 'ham))
854 (setq spam-old-spam-articles
855 (spam-list-articles gnus-newsgroup-articles 'spam))
856 (spam-mark-junk-as-spam-routine))
858 ;; The spam processors are invoked for any group, spam or ham or neither
859 (defun spam-summary-prepare-exit ()
860 (unless gnus-group-is-exiting-without-update-p
861 (gnus-message 6 "Exiting summary buffer and applying spam rules")
863 ;; first of all, unregister any articles that are no longer ham or spam
864 ;; we have to iterate over the processors, or else we'll be too slow
865 (dolist (classification '(spam ham))
866 (let* ((old-articles (if (eq classification 'spam)
867 spam-old-spam-articles
868 spam-old-ham-articles))
869 (new-articles (spam-list-articles
870 gnus-newsgroup-articles
872 (changed-articles (spam-set-difference new-articles old-articles)))
873 ;; now that we have the changed articles, we go through the processors
874 (dolist (processor-param spam-list-of-processors)
875 (let ((processor (nth 0 processor-param))
876 (processor-classification (nth 1 processor-param))
877 (check (nth 2 processor-param))
879 (dolist (article changed-articles)
880 (let ((id (spam-fetch-field-message-id-fast article)))
881 (when (spam-log-unregistration-needed-p
882 id 'process classification check)
883 (push article unregister-list))))
884 ;; call spam-register-routine with specific articles to unregister,
885 ;; when there are articles to unregister and the check is enabled
886 (when (and unregister-list (symbol-value check))
887 (spam-register-routine
888 classification check t unregister-list))))))
890 ;; find all the spam processors applicable to this group
891 (dolist (processor-param spam-list-of-processors)
892 (let ((processor (nth 0 processor-param))
893 (classification (nth 1 processor-param))
894 (check (nth 2 processor-param)))
895 (when (and (eq 'spam classification)
896 (spam-group-processor-p gnus-newsgroup-name check classification))
897 (spam-register-routine classification check))))
899 (unless (and spam-move-spam-nonspam-groups-only
900 (spam-group-spam-contents-p gnus-newsgroup-name))
901 (let* ((group (gnus-parameter-spam-process-destination
902 gnus-newsgroup-name))
903 (num (spam-mark-spam-as-expired-and-move-routine group)))
906 "%d spam messages are marked as expired and moved it to %s"
909 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
910 ;; expire spam, in case the above did not expire them
911 (let ((num (spam-mark-spam-as-expired-and-move-routine nil)))
914 "%d spam messages are markd as expired without moving it"
917 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
918 (and (spam-group-spam-contents-p gnus-newsgroup-name)
919 spam-process-ham-in-spam-groups)
920 spam-process-ham-in-nonham-groups)
921 ;; find all the ham processors applicable to this group
922 (dolist (processor-param spam-list-of-processors)
923 (let ((processor (nth 0 processor-param))
924 (classification (nth 1 processor-param))
925 (check (nth 2 processor-param)))
926 (when (and (eq 'ham classification)
927 (spam-group-processor-p gnus-newsgroup-name check classification))
928 (spam-register-routine classification check)))))
930 (when (spam-group-processor-p gnus-newsgroup-name 'ham 'spam-use-ham-copy)
932 (spam-ham-copy-routine
933 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
935 (gnus-message 6 "%d ham messages are copied" num))))
937 ;; now move all ham articles out of spam groups
938 (when (spam-group-spam-contents-p gnus-newsgroup-name)
940 (spam-ham-move-routine
941 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
943 (gnus-message 6 "%d ham messages are moved from spam group" num)))))
945 (setq spam-old-ham-articles nil)
946 (setq spam-old-spam-articles nil))
948 (defun spam-set-difference (list1 list2)
949 "Return a set difference of LIST1 and LIST2.
950 When either list is nil, the other is returned."
951 (if (and list1 list2)
952 ;; we have two non-nil lists
954 (dolist (item (append list1 list2))
955 (when (and (memq item list1) (memq item list2))
956 (setq list1 (delq item list1))
957 (setq list2 (delq item list2))))
958 (append list1 list2))
959 ;; if either of the lists was nil, return the other one
960 (if list1 list1 list2)))
962 (defun spam-mark-junk-as-spam-routine ()
963 ;; check the global list of group names spam-junk-mailgroups and the
965 (when (spam-group-spam-contents-p gnus-newsgroup-name)
966 (gnus-message 6 "Marking %s articles as spam"
967 (if spam-mark-only-unseen-as-spam
970 (let ((articles (if spam-mark-only-unseen-as-spam
971 gnus-newsgroup-unseen
972 gnus-newsgroup-unreads)))
973 (dolist (article articles)
974 (gnus-summary-mark-article article gnus-spam-mark)))))
976 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
977 (if (and (car-safe groups) (listp (car-safe groups)))
978 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
979 (gnus-summary-kill-process-mark)
980 (let ((articles gnus-newsgroup-articles)
981 (backend-supports-deletions
982 (gnus-check-backend-function
983 'request-move-article gnus-newsgroup-name))
984 article tomove deletep)
985 (dolist (article articles)
986 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
987 (gnus-summary-mark-article article gnus-expirable-mark)
988 (push article tomove)))
990 ;; now do the actual copies
991 (dolist (group groups)
994 (dolist (article tomove)
995 (gnus-summary-set-process-mark article))
997 (if (or (not backend-supports-deletions)
998 (> (length groups) 1))
1000 (gnus-summary-copy-article nil group)
1002 (gnus-summary-move-article nil group)))))
1004 ;; now delete the articles, if there was a copy done, and the
1005 ;; backend allows it
1006 (when (and deletep backend-supports-deletions)
1007 (dolist (article tomove)
1008 (gnus-summary-set-process-mark article))
1010 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1011 (gnus-summary-delete-article nil))))
1013 (gnus-summary-yank-process-mark)
1016 (defun spam-ham-copy-or-move-routine (copy groups)
1017 (gnus-summary-kill-process-mark)
1018 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
1019 (backend-supports-deletions
1020 (gnus-check-backend-function
1021 'request-move-article gnus-newsgroup-name))
1022 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1023 article mark todo deletep respool)
1025 (when (member 'respool groups)
1026 (setq respool t) ; boolean for later
1027 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1029 ;; now do the actual move
1030 (dolist (group groups)
1031 (when (and todo (stringp group))
1032 (dolist (article todo)
1033 (when spam-mark-ham-unread-before-move-from-spam-group
1034 (gnus-summary-mark-article article gnus-unread-mark))
1035 (gnus-summary-set-process-mark article))
1037 (if respool ; respooling is with a "fake" group
1038 (let ((spam-split-disabled
1039 (or spam-split-disabled
1040 spam-disable-spam-split-during-ham-respool)))
1041 (gnus-summary-respool-article nil respool-method))
1042 (if (or (not backend-supports-deletions) ; else, we are not respooling
1043 (> (length groups) 1))
1044 (progn ; if copying, copy and set deletep
1045 (gnus-summary-copy-article nil group)
1047 (gnus-summary-move-article nil group))))) ; else move articles
1049 ;; now delete the articles, unless a) copy is t, and there was a copy done
1050 ;; b) a move was done to a single group
1051 ;; c) backend-supports-deletions is nil
1053 (when (and deletep backend-supports-deletions)
1054 (dolist (article todo)
1055 (gnus-summary-set-process-mark article))
1057 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1058 (gnus-summary-delete-article nil)))))
1060 (gnus-summary-yank-process-mark)
1063 (defun spam-ham-copy-routine (&rest groups)
1064 (if (and (car-safe groups) (listp (car-safe groups)))
1065 (apply 'spam-ham-copy-routine (car groups))
1066 (spam-ham-copy-or-move-routine t groups)))
1068 (defun spam-ham-move-routine (&rest groups)
1069 (if (and (car-safe groups) (listp (car-safe groups)))
1070 (apply 'spam-ham-move-routine (car groups))
1071 (spam-ham-copy-or-move-routine nil groups)))
1073 (defun spam-get-article-as-string (article)
1074 (when (numberp article)
1076 (gnus-request-article-this-buffer
1078 gnus-newsgroup-name)
1082 ;; (defun spam-get-article-as-filename (article)
1083 ;; (let ((article-filename))
1084 ;; (when (numberp article)
1085 ;; (nnml-possibly-change-directory
1086 ;; (gnus-group-real-name gnus-newsgroup-name))
1087 ;; (setq article-filename (expand-file-name
1088 ;; (int-to-string article) nnml-current-directory)))
1089 ;; (if (file-exists-p article-filename)
1093 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1094 "Fetch a field quickly, using the internal gnus-data-list function"
1095 (when (numberp article)
1096 (let* ((data-header (or prepared-data-header
1097 (spam-fetch-article-header article))))
1098 (if (arrayp data-header)
1100 ((equal field 'from)
1101 (mail-header-from data-header))
1102 ((equal field 'message-id)
1103 (mail-header-message-id data-header))
1104 ((equal field 'subject)
1105 (mail-header-subject data-header))
1106 ((equal field 'references)
1107 (mail-header-references data-header))
1108 ((equal field 'date)
1109 (mail-header-date data-header))
1110 ((equal field 'xref)
1111 (mail-header-xref data-header))
1112 ((equal field 'extra)
1113 (mail-header-extra data-header))
1116 (gnus-message 6 "Article %d has a nil data header" article)))))
1118 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1119 (spam-fetch-field-fast article 'from prepared-data-header))
1121 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1122 (spam-fetch-field-fast article 'subject prepared-data-header))
1124 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1125 (spam-fetch-field-fast article 'message-id prepared-data-header))
1127 (defun spam-generate-fake-headers (article)
1128 (let ((dh (spam-fetch-article-header article)))
1132 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1133 "Date: %s\nReferences: %s\nXref: %s\n")
1134 (spam-fetch-field-fast article 'from dh)
1135 (spam-fetch-field-fast article 'subject dh)
1136 (spam-fetch-field-fast article 'message-id dh)
1137 (spam-fetch-field-fast article 'date dh)
1138 (spam-fetch-field-fast article 'references dh)
1139 (spam-fetch-field-fast article 'xref dh))
1140 (when (spam-fetch-field-fast article 'extra dh)
1141 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1144 "spam-generate-fake-headers: article %d didn't have a valid header"
1147 (defun spam-fetch-article-header (article)
1149 (set-buffer gnus-summary-buffer)
1150 (gnus-read-header article)
1151 (nth 3 (assq article gnus-newsgroup-data))))
1154 ;;;; Spam determination.
1156 (defvar spam-list-of-checks
1157 '((spam-use-blacklist . spam-check-blacklist)
1158 (spam-use-regex-headers . spam-check-regex-headers)
1159 (spam-use-gmane-xref . spam-check-gmane-xref)
1160 (spam-use-regex-body . spam-check-regex-body)
1161 (spam-use-whitelist . spam-check-whitelist)
1162 (spam-use-BBDB . spam-check-BBDB)
1163 (spam-use-BBDB-exclusive . spam-check-BBDB)
1164 (spam-use-ifile . spam-check-ifile)
1165 (spam-use-spamoracle . spam-check-spamoracle)
1166 (spam-use-stat . spam-check-stat)
1167 (spam-use-blackholes . spam-check-blackholes)
1168 (spam-use-hashcash . spam-check-hashcash)
1169 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1170 (spam-use-spamassassin . spam-check-spamassassin)
1171 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1172 (spam-use-bogofilter . spam-check-bogofilter)
1173 (spam-use-bsfilter-headers . spam-check-bsfilter-headers)
1174 (spam-use-bsfilter . spam-check-bsfilter)
1175 (spam-use-crm114 . spam-check-crm114))
1176 "The spam-list-of-checks list contains pairs associating a
1177 parameter variable with a spam checking function. If the
1178 parameter variable is true, then the checking function is called,
1179 and its value decides what happens. Each individual check may
1180 return nil, t, or a mailgroup name. The value nil means that the
1181 check does not yield a decision, and so, that further checks are
1182 needed. The value t means that the message is definitely not
1183 spam, and that further spam checks should be inhibited.
1184 Otherwise, a mailgroup name or the symbol 'spam (depending on
1185 spam-split-symbolic-return) is returned where the mail should go,
1186 and further checks are also inhibited. The usual mailgroup name
1187 is the value of `spam-split-group', meaning that the message is
1188 definitely a spam.")
1190 (defvar spam-list-of-statistical-checks
1197 spam-use-spamassassin
1200 "The spam-list-of-statistical-checks list contains all the mail
1201 splitters that need to have the full message body available.
1202 Note that you should fetch extra headers if you don't like this,
1203 e.g. fetch the 'Received' header for spam-use-blackholes.")
1205 (defun spam-split (&rest specific-checks)
1206 "Split this message into the `spam' group if it is spam.
1207 This function can be used as an entry in the variable `nnmail-split-fancy',
1208 for example like this: (: spam-split). It can take checks as
1209 parameters. A string as a parameter will set the
1210 spam-split-group to that string.
1212 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1214 (setq spam-split-last-successful-check nil)
1215 (unless spam-split-disabled
1216 (let ((spam-split-group-choice spam-split-group))
1217 (dolist (check specific-checks)
1218 (when (stringp check)
1219 (setq spam-split-group-choice check)
1220 (setq specific-checks (delq check specific-checks))))
1222 (let ((spam-split-group spam-split-group-choice))
1225 (dolist (check spam-list-of-statistical-checks)
1226 (when (and (symbolp check)
1227 (or (symbol-value check)
1228 (memq check specific-checks)))
1230 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1231 (symbol-name check))
1233 ;; (progn (widen) (debug (buffer-string)))
1234 (let ((list-of-checks spam-list-of-checks)
1236 (while (and list-of-checks (not decision))
1237 (let ((pair (pop list-of-checks)))
1239 ;; either, given specific checks, this is one of them
1240 (and specific-checks (memq (car pair) specific-checks))
1241 ;; or, given no specific checks, spam-use-CHECK is set
1242 (and (null specific-checks) (symbol-value (car pair))))
1243 (gnus-message 6 "spam-split: calling the %s function"
1244 (symbol-name (cdr pair)))
1245 (setq decision (funcall (cdr pair)))
1246 ;; if we got a decision at all, save the current check
1248 (setq spam-split-last-successful-check (car pair)))
1250 (when (eq decision 'spam)
1251 (unless spam-split-symbolic-return
1254 (format "spam-split got %s but %s is nil"
1255 (symbol-name decision)
1256 (symbol-name spam-split-symbolic-return))))))))
1258 (if spam-split-symbolic-return-positive 'ham nil)
1261 (defun spam-find-spam ()
1262 "This function will detect spam in the current newsgroup using spam-split."
1265 (let* ((group gnus-newsgroup-name)
1266 (autodetect (gnus-parameter-spam-autodetect group))
1267 (methods (gnus-parameter-spam-autodetect-methods group))
1268 (first-method (nth 0 methods))
1269 (articles (if spam-autodetect-recheck-messages
1270 gnus-newsgroup-articles
1271 gnus-newsgroup-unseen))
1272 article-cannot-be-faked)
1274 (dolist (check spam-list-of-statistical-checks)
1275 (when (and (symbolp check)
1276 (memq check methods))
1277 (setq article-cannot-be-faked t)
1280 (when (memq 'default methods)
1281 (setq article-cannot-be-faked t))
1283 (when (and autodetect
1284 (not (equal first-method 'none)))
1287 (let ((id (spam-fetch-field-message-id-fast article))
1288 (subject (spam-fetch-field-subject-fast article))
1289 (sender (spam-fetch-field-from-fast article))
1293 (gnus-message 6 "Article %d has no message ID!" article))
1295 (when (and id spam-log-to-registry)
1296 (setq registry-lookup (spam-log-registration-type id 'incoming))
1297 (when registry-lookup
1300 "spam-find-spam: message %s was already registered incoming"
1303 (let* ((spam-split-symbolic-return t)
1304 (spam-split-symbolic-return-positive t)
1305 (fake-headers (spam-generate-fake-headers article))
1309 (if article-cannot-be-faked
1310 (gnus-request-article-this-buffer
1313 ;; else, we fake the article
1314 (when fake-headers (insert fake-headers)))
1315 (if (or (null first-method)
1316 (equal first-method 'default))
1318 (apply 'spam-split methods))))))
1319 (if (equal split-return 'spam)
1320 (gnus-summary-mark-article article gnus-spam-mark))
1322 (when (and id split-return spam-log-to-registry)
1323 (when (zerop (gnus-registry-group-count id))
1324 (gnus-registry-add-group
1325 id group subject sender))
1327 (unless registry-lookup
1328 (spam-log-processing-to-registry
1332 spam-split-last-successful-check
1336 (defvar spam-registration-functions
1337 ;; first the ham register, second the spam register function
1338 ;; third the ham unregister, fourth the spam unregister function
1339 '((spam-use-blacklist nil
1340 spam-blacklist-register-routine
1342 spam-blacklist-unregister-routine)
1343 (spam-use-whitelist spam-whitelist-register-routine
1345 spam-whitelist-unregister-routine
1347 (spam-use-ham-copy nil
1351 (spam-use-BBDB spam-BBDB-register-routine
1353 spam-BBDB-unregister-routine
1355 (spam-use-ifile spam-ifile-register-ham-routine
1356 spam-ifile-register-spam-routine
1357 spam-ifile-unregister-ham-routine
1358 spam-ifile-unregister-spam-routine)
1359 (spam-use-spamoracle spam-spamoracle-learn-ham
1360 spam-spamoracle-learn-spam
1361 spam-spamoracle-unlearn-ham
1362 spam-spamoracle-unlearn-spam)
1363 (spam-use-stat spam-stat-register-ham-routine
1364 spam-stat-register-spam-routine
1365 spam-stat-unregister-ham-routine
1366 spam-stat-unregister-spam-routine)
1367 ;; note that spam-use-gmane and spam-use-resend are not legitimate checks
1369 spam-report-gmane-register-routine
1370 ;; does Gmane support unregistration?
1373 (spam-use-resend nil
1374 spam-report-resend-register-routine
1377 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1378 spam-spamassassin-register-spam-routine
1379 spam-spamassassin-unregister-ham-routine
1380 spam-spamassassin-unregister-spam-routine)
1381 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1382 spam-bogofilter-register-spam-routine
1383 spam-bogofilter-unregister-ham-routine
1384 spam-bogofilter-unregister-spam-routine)
1385 (spam-use-bsfilter spam-bsfilter-register-ham-routine
1386 spam-bsfilter-register-spam-routine
1387 spam-bsfilter-unregister-ham-routine
1388 spam-bsfilter-unregister-spam-routine)
1389 (spam-use-crm114 spam-crm114-register-ham-routine
1390 spam-crm114-register-spam-routine
1391 ;; does CRM114 Mailfilter support unregistration?
1394 "The spam-registration-functions list contains pairs
1395 associating a parameter variable with the ham and spam
1396 registration functions, and the ham and spam unregistration
1399 (defun spam-classification-valid-p (classification)
1400 (or (eq classification 'spam)
1401 (eq classification 'ham)))
1403 (defun spam-process-type-valid-p (process-type)
1404 (or (eq process-type 'incoming)
1405 (eq process-type 'process)))
1407 (defun spam-registration-check-valid-p (check)
1408 (assoc check spam-registration-functions))
1410 (defun spam-unregistration-check-valid-p (check)
1411 (assoc check spam-registration-functions))
1413 (defun spam-registration-function (classification check)
1414 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1415 (if (eq classification 'spam)
1419 (defun spam-unregistration-function (classification check)
1420 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1421 (if (eq classification 'spam)
1425 (defun spam-list-articles (articles classification)
1426 (let ((mark-check (if (eq classification 'spam)
1427 'spam-group-spam-mark-p
1428 'spam-group-ham-mark-p))
1429 alist mark-cache-yes mark-cache-no)
1430 (dolist (article articles)
1431 (let ((mark (gnus-summary-article-mark article)))
1432 (unless (or (memq mark mark-cache-yes)
1433 (memq mark mark-cache-no))
1434 (if (funcall mark-check
1437 (push mark mark-cache-yes)
1438 (push mark mark-cache-no)))
1439 (when (memq mark mark-cache-yes)
1440 (push article alist))))
1443 (defun spam-register-routine (classification
1445 &optional unregister
1447 (when (and (spam-classification-valid-p classification)
1448 (spam-registration-check-valid-p check))
1449 (let* ((register-function
1450 (spam-registration-function classification check))
1451 (unregister-function
1452 (spam-unregistration-function classification check))
1453 (run-function (if unregister
1456 (log-function (if unregister
1457 'spam-log-undo-registration
1458 'spam-log-processing-to-registry))
1462 ;; make list of articles, using specific-articles if given
1463 (setq articles (or specific-articles
1465 gnus-newsgroup-articles
1468 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1469 (if unregister "Unregistering" "Registering")
1471 (if specific-articles "specific" "")
1472 (symbol-name classification)
1473 (symbol-name check))
1474 (funcall run-function articles)
1475 ;; now log all the registrations (or undo them, depending on unregister)
1476 (dolist (article articles)
1477 (funcall log-function
1478 (spam-fetch-field-message-id-fast article)
1482 gnus-newsgroup-name))))))
1484 ;;; log a ham- or spam-processor invocation to the registry
1485 (defun spam-log-processing-to-registry (id type classification check group)
1486 (when spam-log-to-registry
1487 (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)))
1493 (cell (list classification check group)))
1494 (push cell cell-list)
1495 (gnus-registry-store-extra-entry
1502 (format "%s call with bad ID, type, classification, spam-check, or group"
1503 "spam-log-processing-to-registry")))))
1505 ;;; check if a ham- or spam-processor registration has been done
1506 (defun spam-log-registered-p (id type)
1507 (when spam-log-to-registry
1508 (if (and (stringp id)
1509 (spam-process-type-valid-p type))
1510 (cdr-safe (gnus-registry-fetch-extra id type))
1514 (format "%s called with bad ID, type, classification, or spam-check"
1515 "spam-log-registered-p"))
1518 ;;; check what a ham- or spam-processor registration says
1519 ;;; returns nil if conflicting registrations are found
1520 (defun spam-log-registration-type (id type)
1523 (dolist (reg (spam-log-registered-p id type))
1524 (let ((classification (nth 0 reg)))
1525 (when (spam-classification-valid-p classification)
1527 (not (eq classification decision)))
1528 (setq count (+ 1 count)))
1529 (setq decision classification))))
1535 ;;; check if a ham- or spam-processor registration needs to be undone
1536 (defun spam-log-unregistration-needed-p (id type classification check)
1537 (when spam-log-to-registry
1538 (if (and (stringp id)
1539 (spam-process-type-valid-p type)
1540 (spam-classification-valid-p classification)
1541 (spam-registration-check-valid-p check))
1542 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1544 (dolist (cell cell-list)
1546 (when (and (eq classification (nth 0 cell))
1547 (eq check (nth 1 cell)))
1553 (format "%s called with bad ID, type, classification, or spam-check"
1554 "spam-log-unregistration-needed-p"))
1558 ;;; undo a ham- or spam-processor registration (the group is not used)
1559 (defun spam-log-undo-registration (id type classification check &optional group)
1560 (when (and spam-log-to-registry
1561 (spam-log-unregistration-needed-p id type classification check))
1562 (if (and (stringp id)
1563 (spam-process-type-valid-p type)
1564 (spam-classification-valid-p classification)
1565 (spam-registration-check-valid-p check))
1566 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1567 new-cell-list found)
1568 (dolist (cell cell-list)
1569 (unless (and (eq classification (nth 0 cell))
1570 (eq check (nth 1 cell)))
1571 (push cell new-cell-list)))
1572 (gnus-registry-store-extra-entry
1577 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1578 "spam-log-undo-registration"))
1581 ;;; set up IMAP widening if it's necessary
1582 (defun spam-setup-widening ()
1583 (dolist (check spam-list-of-statistical-checks)
1584 (when (symbol-value check)
1585 (setq nnimap-split-download-body-default t))))
1589 (defun spam-check-gmane-xref ()
1591 (message-fetch-field "Xref")
1592 (message-fetch-field "Newsgroups")))
1593 (spam-split-group (if spam-split-symbolic-return
1596 (when header ; return nil when no header
1597 (when (string-match spam-gmane-xref-spam-group
1599 spam-split-group))))
1604 (defun spam-check-regex-body ()
1605 (let ((spam-regex-headers-ham spam-regex-body-ham)
1606 (spam-regex-headers-spam spam-regex-body-spam))
1607 (spam-check-regex-headers t)))
1612 (defun spam-check-regex-headers (&optional body)
1613 (let ((type (if body "body" "header"))
1614 (spam-split-group (if spam-split-symbolic-return
1618 (dolist (h-regex spam-regex-headers-ham)
1620 (goto-char (point-min))
1621 (when (re-search-forward h-regex nil t)
1622 (message "Ham regex %s search positive." type)
1624 (dolist (s-regex spam-regex-headers-spam)
1626 (goto-char (point-min))
1627 (when (re-search-forward s-regex nil t)
1628 (message "Spam regex %s search positive." type)
1630 (setq ret spam-split-group))))
1636 (defun spam-reverse-ip-string (ip)
1638 (mapconcat 'identity
1639 (nreverse (split-string ip "\\."))
1642 (defun spam-check-blackholes ()
1643 "Check the Received headers for blackholed relays."
1644 (let ((headers (message-fetch-field "received"))
1645 (spam-split-group (if spam-split-symbolic-return
1652 (goto-char (point-min))
1653 (gnus-message 6 "Checking headers for relay addresses")
1654 (while (re-search-forward
1655 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1656 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1657 (push (spam-reverse-ip-string (match-string 1))
1659 (dolist (server spam-blackhole-servers)
1661 (unless (and spam-blackhole-good-server-regex
1662 ;; match the good-server-regex against the reversed (again) IP string
1664 spam-blackhole-good-server-regex
1665 (spam-reverse-ip-string ip)))
1667 (let ((query-string (concat ip "." server)))
1669 (let ((query-result (query-dig query-string)))
1671 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1673 (push (list ip server query-result)
1675 ;; else, if not using dig.el
1676 (when (query-dns query-string)
1677 (gnus-message 6 "positive blackhole check")
1678 (push (list ip server (query-dns query-string 'TXT))
1689 (defun spam-check-hashcash ()
1690 "Check the headers for hashcash payments."
1691 (mail-check-payment))) ;mail-check-payment returns a boolean
1694 (defalias 'mail-check-payment 'ignore)
1695 (defalias 'spam-check-hashcash 'ignore))))
1699 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1700 ;;; <sacha@giotto.sj.ru>
1702 ;; all this is done inside a condition-case to trap errors
1709 ;; when the BBDB changes, we want to clear out our cache
1710 (defun spam-clear-cache-BBDB (&rest immaterial)
1711 (spam-clear-cache 'spam-use-BBDB))
1713 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1715 (defun spam-enter-ham-BBDB (addresses &optional remove)
1716 "Enter an address into the BBDB; implies ham (non-spam) sender"
1717 (dolist (from addresses)
1718 (when (stringp from)
1719 (let* ((parsed-address (gnus-extract-address-components from))
1720 (name (or (nth 0 parsed-address) "Ham Sender"))
1721 (remove-function (if remove
1722 'bbdb-delete-record-internal
1724 (net-address (nth 1 parsed-address))
1725 (record (and net-address
1726 (bbdb-search-simple nil net-address))))
1728 (gnus-message 6 "%s address %s %s BBDB"
1729 (if remove "Deleting" "Adding")
1731 (if remove "from" "to"))
1733 (funcall remove-function record)
1734 (bbdb-create-internal name nil net-address nil nil
1735 "ham sender added by spam.el")))))))
1737 (defun spam-BBDB-register-routine (articles &optional unregister)
1739 (dolist (article articles)
1740 (when (stringp (spam-fetch-field-from-fast article))
1741 (push (spam-fetch-field-from-fast article) addresses)))
1742 ;; now do the register/unregister action
1743 (spam-enter-ham-BBDB addresses unregister)))
1745 (defun spam-BBDB-unregister-routine (articles)
1746 (spam-BBDB-register-routine articles t))
1748 (defun spam-check-BBDB ()
1749 "Mail from people in the BBDB is classified as ham or non-spam"
1750 (let ((who (message-fetch-field "from"))
1751 (spam-split-group (if spam-split-symbolic-return
1754 bbdb-cache bbdb-hashtable)
1755 (when spam-cache-lookups
1756 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1759 ;; this is the expanded (bbdb-hashtable) macro
1760 ;; without the debugging support
1761 (with-current-buffer (bbdb-buffer)
1763 (save-window-excursion
1764 (bbdb-records nil t)
1766 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1768 (setq who (nth 1 (gnus-extract-address-components who)))
1770 (if spam-cache-lookups
1772 (intern-soft who bbdb-cache))
1773 (bbdb-search-simple nil who))
1775 (if spam-use-BBDB-exclusive
1780 (defalias 'bbdb-search-simple 'ignore)
1781 (defalias 'bbdb-records 'ignore)
1782 (defalias 'bbdb-buffer 'ignore)
1783 (defalias 'spam-check-BBDB 'ignore)
1784 (defalias 'spam-BBDB-register-routine 'ignore)
1785 (defalias 'spam-enter-ham-BBDB 'ignore)
1786 (defalias 'bbdb-create-internal 'ignore)
1787 (defalias 'bbdb-delete-record-internal 'ignore)
1788 (defalias 'bbdb-records 'ignore))))
1793 ;;; check the ifile backend; return nil if the mail was NOT classified
1796 (defun spam-get-ifile-database-parameter ()
1797 "Get the command-line parameter for ifile's database from
1798 spam-ifile-database-path."
1799 (if spam-ifile-database-path
1800 (format "--db-file=%s" spam-ifile-database-path)
1803 (defun spam-check-ifile ()
1804 "Check the ifile backend for the classification of this message."
1805 (let ((article-buffer-name (buffer-name))
1806 (spam-split-group (if spam-split-symbolic-return
1811 (let ((temp-buffer-name (buffer-name))
1812 (db-param (spam-get-ifile-database-parameter)))
1814 (set-buffer article-buffer-name)
1815 (apply 'call-process-region
1816 (point-min) (point-max) spam-ifile-path
1817 nil temp-buffer-name nil "-c"
1818 (if db-param `(,db-param "-q") `("-q"))))
1819 ;; check the return now (we're back in the temp buffer)
1820 (goto-char (point-min))
1822 (setq category (buffer-substring (point) (point-at-eol))))
1823 (when (not (zerop (length category))) ; we need a category here
1824 (if spam-ifile-all-categories
1825 (setq return category)
1826 ;; else, if spam-ifile-all-categories is not set...
1827 (when (string-equal spam-ifile-spam-category category)
1828 (setq return spam-split-group)))))) ; note return is nil otherwise
1831 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1832 "Register an article, given as a string, with a category.
1833 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1834 (let ((category (or category gnus-newsgroup-name))
1835 (add-or-delete-option (if unregister "-d" "-i"))
1836 (db (spam-get-ifile-database-parameter))
1839 (dolist (article articles)
1840 (let ((article-string (spam-get-article-as-string article)))
1841 (when (stringp article-string)
1842 (insert article-string))))
1843 (apply 'call-process-region
1844 (point-min) (point-max) spam-ifile-path
1846 add-or-delete-option category
1847 (if db `(,db "-h") `("-h"))))))
1849 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1850 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1852 (defun spam-ifile-unregister-spam-routine (articles)
1853 (spam-ifile-register-spam-routine articles t))
1855 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1856 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1858 (defun spam-ifile-unregister-ham-routine (articles)
1859 (spam-ifile-register-ham-routine articles t))
1866 (let ((spam-stat-install-hooks nil))
1867 (require 'spam-stat))
1869 (defun spam-check-stat ()
1870 "Check the spam-stat backend for the classification of this message"
1871 (let ((spam-split-group (if spam-split-symbolic-return
1874 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1875 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1877 (spam-stat-split-fancy)))
1879 (defun spam-stat-register-spam-routine (articles &optional unregister)
1880 (dolist (article articles)
1881 (let ((article-string (spam-get-article-as-string article)))
1883 (insert article-string)
1885 (spam-stat-buffer-change-to-non-spam)
1886 (spam-stat-buffer-is-spam))))))
1888 (defun spam-stat-unregister-spam-routine (articles)
1889 (spam-stat-register-spam-routine articles t))
1891 (defun spam-stat-register-ham-routine (articles &optional unregister)
1892 (dolist (article articles)
1893 (let ((article-string (spam-get-article-as-string article)))
1895 (insert article-string)
1897 (spam-stat-buffer-change-to-spam)
1898 (spam-stat-buffer-is-non-spam))))))
1900 (defun spam-stat-unregister-ham-routine (articles)
1901 (spam-stat-register-ham-routine articles t))
1903 (defun spam-maybe-spam-stat-load ()
1904 (when spam-use-stat (spam-stat-load)))
1906 (defun spam-maybe-spam-stat-save ()
1907 (when spam-use-stat (spam-stat-save))))
1910 (defalias 'spam-stat-load 'ignore)
1911 (defalias 'spam-stat-save 'ignore)
1912 (defalias 'spam-maybe-spam-stat-load 'ignore)
1913 (defalias 'spam-maybe-spam-stat-save 'ignore)
1914 (defalias 'spam-stat-register-ham-routine 'ignore)
1915 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1916 (defalias 'spam-stat-register-spam-routine 'ignore)
1917 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1918 (defalias 'spam-stat-buffer-is-spam 'ignore)
1919 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1920 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1921 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1922 (defalias 'spam-stat-split-fancy 'ignore)
1923 (defalias 'spam-check-stat 'ignore))))
1927 ;;;; Blacklists and whitelists.
1929 (defvar spam-whitelist-cache nil)
1930 (defvar spam-blacklist-cache nil)
1932 (defun spam-kill-whole-line ()
1934 (let ((kill-whole-line t))
1937 ;;; address can be a list, too
1938 (defun spam-enter-whitelist (address &optional remove)
1939 "Enter ADDRESS (list or single) into the whitelist.
1940 With a non-nil REMOVE, remove them."
1941 (interactive "sAddress: ")
1942 (spam-enter-list address spam-whitelist remove)
1943 (setq spam-whitelist-cache nil)
1944 (spam-clear-cache 'spam-use-whitelist))
1946 ;;; address can be a list, too
1947 (defun spam-enter-blacklist (address &optional remove)
1948 "Enter ADDRESS (list or single) into the blacklist.
1949 With a non-nil REMOVE, remove them."
1950 (interactive "sAddress: ")
1951 (spam-enter-list address spam-blacklist remove)
1952 (setq spam-blacklist-cache nil)
1953 (spam-clear-cache 'spam-use-whitelist))
1955 (defun spam-enter-list (addresses file &optional remove)
1956 "Enter ADDRESSES into the given FILE.
1957 Either the whitelist or the blacklist files can be used. With
1958 REMOVE not nil, remove the ADDRESSES."
1959 (if (stringp addresses)
1960 (spam-enter-list (list addresses) file remove)
1961 ;; else, we have a list of addresses here
1962 (unless (file-exists-p (file-name-directory file))
1963 (make-directory (file-name-directory file) t))
1966 (find-file-noselect file))
1967 (dolist (a addresses)
1969 (goto-char (point-min))
1970 (if (re-search-forward (regexp-quote a) nil t)
1971 ;; found the address
1973 (spam-kill-whole-line))
1974 ;; else, the address was not found
1976 (goto-char (point-max))
1982 (defun spam-filelist-build-cache (type)
1983 (let ((cache (if (eq type 'spam-use-blacklist)
1984 spam-blacklist-cache
1985 spam-whitelist-cache))
1987 (unless (gethash type spam-caches)
1989 (let ((address (pop cache)))
1990 (unless (zerop (length address)) ; 0 for a nil address too
1991 (setq address (regexp-quote address))
1992 ;; fix regexp-quote's treatment of user-intended regexes
1993 (while (string-match "\\\\\\*" address)
1994 (setq address (replace-match ".*" t t address))))
1995 (push address parsed-cache)))
1996 (puthash type parsed-cache spam-caches))))
1998 (defun spam-filelist-check-cache (type from)
1999 (when (stringp from)
2000 (spam-filelist-build-cache type)
2002 (dolist (address (gethash type spam-caches))
2003 (when (and address (string-match address from))
2008 ;;; returns t if the sender is in the whitelist, nil or
2009 ;;; spam-split-group otherwise
2010 (defun spam-check-whitelist ()
2011 ;; FIXME! Should it detect when file timestamps change?
2012 (let ((spam-split-group (if spam-split-symbolic-return
2015 (unless spam-whitelist-cache
2016 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2017 (if (spam-from-listed-p 'spam-use-whitelist)
2019 (if spam-use-whitelist-exclusive
2023 (defun spam-check-blacklist ()
2024 ;; FIXME! Should it detect when file timestamps change?
2025 (let ((spam-split-group (if spam-split-symbolic-return
2028 (unless spam-blacklist-cache
2029 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2030 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
2032 (defun spam-parse-list (file)
2033 (when (file-readable-p file)
2034 (let (contents address)
2036 (insert-file-contents file)
2038 (setq address (buffer-substring (point) (point-at-eol)))
2040 ;; insert the e-mail address if detected, otherwise the raw data
2041 (unless (zerop (length address))
2042 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2043 (push (or pure-address address) contents)))))
2044 (nreverse contents))))
2046 (defun spam-from-listed-p (type)
2047 (let ((from (message-fetch-field "from"))
2049 (spam-filelist-check-cache type from)))
2051 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2052 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2053 (declassification (if blacklist 'ham 'spam))
2055 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2057 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2058 from addresses unregister-list article-unregister-list)
2059 (dolist (article articles)
2060 (let ((from (spam-fetch-field-from-fast article))
2061 (id (spam-fetch-field-message-id-fast article))
2063 (when (stringp from)
2064 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2065 (when (and (not sender-ignored)
2066 (stringp ignore-regex)
2067 (string-match ignore-regex from))
2068 (setq sender-ignored t)))
2069 ;; remember the messages we need to unregister, unless remove is set
2072 (spam-log-unregistration-needed-p
2073 id 'process declassification de-symbol))
2074 (push article article-unregister-list)
2075 (push from unregister-list))
2076 (unless sender-ignored
2077 (push from addresses)))))
2080 (funcall enter-function addresses t) ; unregister all these addresses
2081 ;; else, register normally and unregister what we need to
2082 (funcall remove-function unregister-list t)
2083 (dolist (article article-unregister-list)
2084 (spam-log-undo-registration
2085 (spam-fetch-field-message-id-fast article)
2089 (funcall enter-function addresses nil))))
2091 (defun spam-blacklist-unregister-routine (articles)
2092 (spam-blacklist-register-routine articles t))
2094 (defun spam-blacklist-register-routine (articles &optional unregister)
2095 (spam-filelist-register-routine articles t unregister))
2097 (defun spam-whitelist-unregister-routine (articles)
2098 (spam-whitelist-register-routine articles t))
2100 (defun spam-whitelist-register-routine (articles &optional unregister)
2101 (spam-filelist-register-routine articles nil unregister))
2104 ;;;; Spam-report glue
2105 (defun spam-report-gmane-register-routine (articles)
2107 (apply 'spam-report-gmane articles)))
2109 (defun spam-report-resend-register-routine (articles)
2110 (let ((spam-report-resend-to (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2111 (spam-report-resend articles)))
2115 (defun spam-check-bogofilter-headers (&optional score)
2116 (let ((header (message-fetch-field spam-bogofilter-header))
2117 (spam-split-group (if spam-split-symbolic-return
2120 (when header ; return nil when no header
2121 (if score ; scoring mode
2122 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2123 (match-string 1 header)
2125 ;; spam detection mode
2126 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2128 spam-split-group)))))
2130 ;; return something sensible if the score can't be determined
2131 (defun spam-bogofilter-score (&optional recheck)
2132 "Get the Bogofilter spamicity score"
2134 (save-window-excursion
2135 (gnus-summary-show-article t)
2136 (set-buffer gnus-article-buffer)
2137 (let ((score (or (unless recheck
2138 (spam-check-bogofilter-headers t))
2139 (spam-check-bogofilter t))))
2140 (gnus-summary-show-article)
2141 (message "Spamicity score %s" score)
2144 (defun spam-check-bogofilter (&optional score)
2145 "Check the Bogofilter backend for the classification of this message"
2146 (let ((article-buffer-name (buffer-name))
2147 (db spam-bogofilter-database-directory)
2150 (let ((temp-buffer-name (buffer-name)))
2152 (set-buffer article-buffer-name)
2153 (apply 'call-process-region
2154 (point-min) (point-max)
2155 spam-bogofilter-path
2156 nil temp-buffer-name nil
2157 (if db `("-d" ,db "-v") `("-v"))))
2158 (setq return (spam-check-bogofilter-headers score))))
2161 (defun spam-bogofilter-register-with-bogofilter (articles
2163 &optional unregister)
2164 "Register an article, given as a string, as spam or non-spam."
2165 (dolist (article articles)
2166 (let ((article-string (spam-get-article-as-string article))
2167 (db spam-bogofilter-database-directory)
2168 (switch (if unregister
2170 spam-bogofilter-spam-strong-switch
2171 spam-bogofilter-ham-strong-switch)
2173 spam-bogofilter-spam-switch
2174 spam-bogofilter-ham-switch))))
2175 (when (stringp article-string)
2177 (insert article-string)
2179 (apply 'call-process-region
2180 (point-min) (point-max)
2181 spam-bogofilter-path
2183 (if db `("-d" ,db "-v") `("-v"))))))))
2185 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2186 (spam-bogofilter-register-with-bogofilter articles t unregister))
2188 (defun spam-bogofilter-unregister-spam-routine (articles)
2189 (spam-bogofilter-register-spam-routine articles t))
2191 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2192 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2194 (defun spam-bogofilter-unregister-ham-routine (articles)
2195 (spam-bogofilter-register-ham-routine articles t))
2200 (defun spam-check-spamoracle ()
2201 "Run spamoracle on an article to determine whether it's spam."
2202 (let ((article-buffer-name (buffer-name))
2203 (spam-split-group (if spam-split-symbolic-return
2207 (let ((temp-buffer-name (buffer-name)))
2209 (set-buffer article-buffer-name)
2211 (apply 'call-process-region
2212 (point-min) (point-max)
2213 spam-spamoracle-binary
2214 nil temp-buffer-name nil
2215 (if spam-spamoracle-database
2216 `("-f" ,spam-spamoracle-database "mark")
2220 (set-buffer temp-buffer-name)
2221 (goto-char (point-min))
2222 (when (re-search-forward "^X-Spam: yes;" nil t)
2224 (error "Error running spamoracle: %s" status))))))))
2226 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2227 "Run spamoracle in training mode."
2229 (let ((temp-buffer-name (buffer-name)))
2231 (goto-char (point-min))
2232 (dolist (article articles)
2233 (insert (spam-get-article-as-string article)))
2234 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2238 (apply 'call-process-region
2239 (point-min) (point-max)
2240 spam-spamoracle-binary
2241 nil temp-buffer-name nil
2242 (if spam-spamoracle-database
2243 `("-f" ,spam-spamoracle-database
2246 (unless (eq 0 status)
2247 (error "Error running spamoracle: %s" status)))))))
2249 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2250 (spam-spamoracle-learn articles nil unregister))
2252 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2253 (spam-spamoracle-learn-ham articles t))
2255 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2256 (spam-spamoracle-learn articles t unregister))
2258 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2259 (spam-spamoracle-learn-spam articles t))
2263 ;;; based mostly on the bogofilter code
2264 (defun spam-check-spamassassin-headers (&optional score)
2265 "Check the SpamAssassin headers for the classification of this message."
2266 (if score ; scoring mode
2267 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2269 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2270 (match-string 1 header)
2272 ;; spam detection mode
2273 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2274 (spam-split-group (if spam-split-symbolic-return
2277 (when header ; return nil when no header
2278 (when (string-match spam-spamassassin-positive-spam-flag-header
2280 spam-split-group)))))
2282 (defun spam-check-spamassassin (&optional score)
2283 "Check the SpamAssassin backend for the classification of this message."
2284 (let ((article-buffer-name (buffer-name)))
2286 (let ((temp-buffer-name (buffer-name)))
2288 (set-buffer article-buffer-name)
2289 (apply 'call-process-region
2290 (point-min) (point-max) spam-spamassassin-path
2291 nil temp-buffer-name nil spam-spamassassin-arguments))
2292 ;; check the return now (we're back in the temp buffer)
2293 (goto-char (point-min))
2294 (spam-check-spamassassin-headers score)))))
2296 ;; return something sensible if the score can't be determined
2297 (defun spam-spamassassin-score (&optional recheck)
2298 "Get the SpamAssassin score"
2300 (save-window-excursion
2301 (gnus-summary-show-article t)
2302 (set-buffer gnus-article-buffer)
2303 (let ((score (or (unless recheck
2304 (spam-check-spamassassin-headers t))
2305 (spam-check-spamassassin t))))
2306 (gnus-summary-show-article)
2307 (message "SpamAssassin score %s" score)
2310 (defun spam-spamassassin-register-with-sa-learn (articles spam
2311 &optional unregister)
2312 "Register articles with spamassassin's sa-learn as spam or non-spam."
2314 (let ((action (if unregister spam-sa-learn-unregister-switch
2315 (if spam spam-sa-learn-spam-switch
2316 spam-sa-learn-ham-switch)))
2317 (summary-buffer-name (buffer-name)))
2319 ;; group the articles into mbox format
2320 (dolist (article articles)
2321 (let (article-string)
2323 (set-buffer summary-buffer-name)
2324 (setq article-string (spam-get-article-as-string article)))
2325 (when (stringp article-string)
2326 (insert "From \n") ; mbox separator (sa-learn only checks the
2327 ; first five chars, so we can get away with
2329 (insert article-string)
2331 ;; call sa-learn on all messages at the same time
2332 (apply 'call-process-region
2333 (point-min) (point-max)
2335 nil nil nil "--mbox"
2336 (if spam-sa-learn-rebuild
2338 `("--no-rebuild" ,action)))))))
2340 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2341 (spam-spamassassin-register-with-sa-learn articles t unregister))
2343 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2344 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2346 (defun spam-spamassassin-unregister-spam-routine (articles)
2347 (spam-spamassassin-register-with-sa-learn articles t t))
2349 (defun spam-spamassassin-unregister-ham-routine (articles)
2350 (spam-spamassassin-register-with-sa-learn articles nil t))
2354 ;;; based mostly on the bogofilter code
2355 (defun spam-check-bsfilter-headers (&optional score)
2357 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2359 (let ((header (nnmail-fetch-field spam-bsfilter-header))
2360 (spam-split-group (if spam-split-symbolic-return
2363 (when header ; return nil when no header
2364 (when (string-match "YES" header)
2365 spam-split-group)))))
2367 ;; return something sensible if the score can't be determined
2368 (defun spam-bsfilter-score (&optional recheck)
2369 "Get the Bsfilter spamicity score"
2371 (save-window-excursion
2372 (gnus-summary-show-article t)
2373 (set-buffer gnus-article-buffer)
2374 (let ((score (or (unless recheck
2375 (spam-check-bsfilter-headers t))
2376 (spam-check-bsfilter t))))
2377 (gnus-summary-show-article)
2378 (message "Spamicity score %s" score)
2381 (defun spam-check-bsfilter (&optional score)
2382 "Check the Bsfilter backend for the classification of this message"
2383 (let ((article-buffer-name (buffer-name))
2384 (dir spam-bsfilter-database-directory)
2387 (let ((temp-buffer-name (buffer-name)))
2389 (set-buffer article-buffer-name)
2390 (apply 'call-process-region
2391 (point-min) (point-max)
2393 nil temp-buffer-name nil
2396 "--insert-probability"
2398 (list "--homedir" dir))))
2399 (setq return (spam-check-bsfilter-headers score))))
2402 (defun spam-bsfilter-register-with-bsfilter (articles
2404 &optional unregister)
2405 "Register an article, given as a string, as spam or non-spam."
2406 (dolist (article articles)
2407 (let ((article-string (spam-get-article-as-string article))
2408 (switch (if unregister
2410 spam-bsfilter-spam-strong-switch
2411 spam-bsfilter-ham-strong-switch)
2413 spam-bsfilter-spam-switch
2414 spam-bsfilter-ham-switch))))
2415 (when (stringp article-string)
2417 (insert article-string)
2418 (apply 'call-process-region
2419 (point-min) (point-max)
2423 (when spam-bsfilter-database-directory
2425 spam-bsfilter-database-directory))))))))
2427 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2428 (spam-bsfilter-register-with-bsfilter articles t unregister))
2430 (defun spam-bsfilter-unregister-spam-routine (articles)
2431 (spam-bsfilter-register-spam-routine articles t))
2433 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2434 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2436 (defun spam-bsfilter-unregister-ham-routine (articles)
2437 (spam-bsfilter-register-ham-routine articles t))
2440 ;;;; CRM114 Mailfilter
2441 (defun spam-check-crm114-headers (&optional score)
2442 (let ((header (message-fetch-field spam-crm114-header))
2443 (spam-split-group (if spam-split-symbolic-return
2446 (when header ; return nil when no header
2447 (if score ; scoring mode
2448 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2449 (match-string 1 header)
2451 ;; spam detection mode
2452 (when (string-match spam-crm114-positive-spam-header
2454 spam-split-group)))))
2456 ;; return something sensible if the score can't be determined
2457 (defun spam-crm114-score ()
2458 "Get the CRM114 Mailfilter pR"
2460 (save-window-excursion
2461 (gnus-summary-show-article t)
2462 (set-buffer gnus-article-buffer)
2463 (let ((score (or (spam-check-crm114-headers t)
2464 (spam-check-crm114 t))))
2465 (gnus-summary-show-article)
2466 (message "pR: %s" score)
2469 (defun spam-check-crm114 (&optional score)
2470 "Check the CRM114 Mailfilter backend for the classification of this message"
2471 (let ((article-buffer-name (buffer-name))
2472 (db spam-crm114-database-directory)
2475 (let ((temp-buffer-name (buffer-name)))
2477 (set-buffer article-buffer-name)
2478 (apply 'call-process-region
2479 (point-min) (point-max)
2481 nil temp-buffer-name nil
2482 (when db (list (concat "--fileprefix=" db)))))
2483 (setq return (spam-check-crm114-headers score))))
2486 (defun spam-crm114-register-with-crm114 (articles
2488 &optional unregister)
2489 "Register an article, given as a string, as spam or non-spam."
2490 (dolist (article articles)
2491 (let ((article-string (spam-get-article-as-string article))
2492 (db spam-crm114-database-directory)
2493 (switch (if unregister
2495 spam-crm114-spam-strong-switch
2496 spam-crm114-ham-strong-switch)
2498 spam-crm114-spam-switch
2499 spam-crm114-ham-switch))))
2500 (when (stringp article-string)
2502 (insert article-string)
2504 (apply 'call-process-region
2505 (point-min) (point-max)
2508 (when db (list switch (concat "--fileprefix=" db)))))))))
2510 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2511 (spam-crm114-register-with-crm114 articles t unregister))
2513 (defun spam-crm114-unregister-spam-routine (articles)
2514 (spam-crm114-register-spam-routine articles t))
2516 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2517 (spam-crm114-register-with-crm114 articles nil unregister))
2519 (defun spam-crm114-unregister-ham-routine (articles)
2520 (spam-crm114-register-ham-routine articles t))
2526 (defun spam-initialize (&rest symbols)
2527 "Install the spam.el hooks and do other initialization.
2528 When SYMBOLS is given, set those variables to t. This is so you
2529 can call spam-initialize before you set spam-use-* variables on
2530 explicitly, and matters only if you need the extra headers
2531 installed through spam-necessary-extra-headers."
2534 (dolist (var symbols)
2537 (dolist (header (spam-necessary-extra-headers))
2538 (add-to-list 'nnmail-extra-headers header)
2539 (add-to-list 'gnus-extra-headers header))
2541 (setq spam-install-hooks t)
2542 ;; TODO: How do we redo this every time spam-face is customized?
2543 (push '((eq mark gnus-spam-mark) . spam-face)
2544 gnus-summary-highlight)
2545 ;; Add hooks for loading and saving the spam stats
2546 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2547 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2548 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2549 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2550 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2551 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2552 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2554 (defun spam-unload-hook ()
2555 "Uninstall the spam.el hooks"
2557 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2558 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2559 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2560 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2561 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2562 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2563 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2565 (when spam-install-hooks
2570 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
2571 ;;; spam.el ends here