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-install-hooks (or
284 spam-use-whitelist-exclusive
287 spam-use-regex-headers
290 spam-use-bogofilter-headers
291 spam-use-spamassassin
292 spam-use-spamassassin-headers
294 spam-use-bsfilter-headers
296 spam-use-BBDB-exclusive
300 "Whether the spam hooks should be installed.
301 Default to t if one of the spam-use-* variables is set."
305 (defcustom spam-split-group "spam"
306 "Group name where incoming spam should be put by `spam-split'."
310 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
311 ;;; not regular expressions
312 (defcustom spam-junk-mailgroups (cons
314 '("mail.junk" "poste.pourriel"))
315 "Mailgroups with spam contents.
316 All unmarked article in such group receive the spam mark on group entry."
317 :type '(repeat (string :tag "Group"))
321 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
322 "The group where spam xrefs can be found on Gmane.
323 Only meaningful if you enable `spam-use-gmane-xref'."
327 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
328 "dev.null.dk" "relays.visi.com")
329 "List of blackhole servers.
330 Only meaningful if you enable `spam-use-blackholes'."
331 :type '(repeat (string :tag "Server"))
334 (defcustom spam-blackhole-good-server-regex nil
335 "String matching IP addresses that should not be checked in the blackholes.
336 Only meaningful if you enable `spam-use-blackholes'."
337 :type '(radio (const nil)
338 (regexp :format "%t: %v\n" :size 0))
341 (defcustom spam-face 'gnus-splash-face
342 "Face for spam-marked articles."
346 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
347 "Regular expression for positive header spam matches.
348 Only meaningful if you enable `spam-use-regex-headers'."
349 :type '(repeat (regexp :tag "Regular expression to match spam header"))
352 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
353 "Regular expression for positive header ham matches.
354 Only meaningful if you enable `spam-use-regex-headers'."
355 :type '(repeat (regexp :tag "Regular expression to match ham header"))
358 (defcustom spam-regex-body-spam '()
359 "Regular expression for positive body spam matches.
360 Only meaningful if you enable `spam-use-regex-body'."
361 :type '(repeat (regexp :tag "Regular expression to match spam body"))
364 (defcustom spam-regex-body-ham '()
365 "Regular expression for positive body ham matches.
366 Only meaningful if you enable `spam-use-regex-body'."
367 :type '(repeat (regexp :tag "Regular expression to match ham body"))
370 (defgroup spam-ifile nil
371 "Spam ifile configuration."
374 (defcustom spam-ifile-path (executable-find "ifile")
375 "File path of the ifile executable program."
376 :type '(choice (file :tag "Location of ifile")
377 (const :tag "ifile is not installed"))
380 (defcustom spam-ifile-database-path nil
381 "File path of the ifile database."
382 :type '(choice (file :tag "Location of the ifile database")
383 (const :tag "Use the default"))
386 (defcustom spam-ifile-spam-category "spam"
387 "Name of the spam ifile category."
391 (defcustom spam-ifile-ham-category nil
392 "Name of the ham ifile category.
393 If nil, the current group name will be used."
394 :type '(choice (string :tag "Use a fixed category")
395 (const :tag "Use the current group name"))
398 (defcustom spam-ifile-all-categories nil
399 "Whether the ifile check will return all categories, or just spam.
400 Set this to t if you want to use the `spam-split' invocation of ifile as
401 your main source of newsgroup names."
405 (defgroup spam-bogofilter nil
406 "Spam bogofilter configuration."
409 (defcustom spam-bogofilter-path (executable-find "bogofilter")
410 "File path of the Bogofilter executable program."
411 :type '(choice (file :tag "Location of bogofilter")
412 (const :tag "Bogofilter is not installed"))
413 :group 'spam-bogofilter)
415 (defcustom spam-bogofilter-header "X-Bogosity"
416 "The header that Bogofilter inserts in messages."
418 :group 'spam-bogofilter)
420 (defcustom spam-bogofilter-spam-switch "-s"
421 "The switch that Bogofilter uses to register spam messages."
423 :group 'spam-bogofilter)
425 (defcustom spam-bogofilter-ham-switch "-n"
426 "The switch that Bogofilter uses to register ham messages."
428 :group 'spam-bogofilter)
430 (defcustom spam-bogofilter-spam-strong-switch "-S"
431 "The switch that Bogofilter uses to unregister ham messages."
433 :group 'spam-bogofilter)
435 (defcustom spam-bogofilter-ham-strong-switch "-N"
436 "The switch that Bogofilter uses to unregister spam messages."
438 :group 'spam-bogofilter)
440 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
441 "The regex on `spam-bogofilter-header' for positive spam identification."
443 :group 'spam-bogofilter)
445 (defcustom spam-bogofilter-database-directory nil
446 "Directory path of the Bogofilter databases."
447 :type '(choice (directory
448 :tag "Location of the Bogofilter database directory")
449 (const :tag "Use the default"))
450 :group 'spam-bogofilter)
452 (defgroup spam-bsfilter nil
453 "Spam bsfilter configuration."
456 (defcustom spam-bsfilter-path (executable-find "bsfilter")
457 "File path of the Bsfilter executable program."
458 :type '(choice (file :tag "Location of bsfilter")
459 (const :tag "Bsfilter is not installed"))
460 :group 'spam-bsfilter)
462 (defcustom spam-bsfilter-header "X-Spam-Flag"
463 "The header inserted by Bsfilter to flag spam."
465 :group 'spam-bsfilter)
467 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
468 "The header that Bsfilter inserts in messages."
470 :group 'spam-bsfilter)
472 (defcustom spam-bsfilter-spam-switch "--add-spam"
473 "The switch that Bsfilter uses to register spam messages."
475 :group 'spam-bsfilter)
477 (defcustom spam-bsfilter-ham-switch "--add-ham"
478 "The switch that Bsfilter uses to register ham messages."
480 :group 'spam-bsfilter)
482 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
483 "The switch that Bsfilter uses to unregister ham messages."
485 :group 'spam-bsfilter)
487 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
488 "The switch that Bsfilter uses to unregister spam messages."
490 :group 'spam-bsfilter)
492 (defcustom spam-bsfilter-database-directory nil
493 "Directory path of the Bsfilter databases."
494 :type '(choice (directory
495 :tag "Location of the Bsfilter database directory")
496 (const :tag "Use the default"))
497 :group 'spam-bsfilter)
499 (defgroup spam-spamoracle nil
500 "Spam spamoracle configuration."
503 (defcustom spam-spamoracle-database nil
504 "Location of spamoracle database file.
505 When nil, use the default spamoracle database."
506 :type '(choice (directory :tag "Location of spamoracle database file.")
507 (const :tag "Use the default"))
508 :group 'spam-spamoracle)
510 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
511 "Location of the spamoracle binary."
512 :type '(choice (directory :tag "Location of the spamoracle binary")
513 (const :tag "Use the default"))
514 :group 'spam-spamoracle)
516 (defgroup spam-spamassassin nil
517 "Spam SpamAssassin configuration."
520 (defcustom spam-spamassassin-path (executable-find "spamassassin")
521 "File path of the spamassassin executable program.
522 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
523 spamd man pages for more information on these programs."
524 :type '(choice (file :tag "Location of spamc")
525 (const :tag "spamassassin is not installed"))
526 :group 'spam-spamassassin)
528 (defcustom spam-spamassassin-arguments ()
529 "Arguments to pass to the spamassassin executable.
530 This must be a list. For example, `(\"-C\" \"configfile\")'."
531 :type '(restricted-sexp :match-alternatives (listp))
532 :group 'spam-spamassassin)
534 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
535 "The header inserted by SpamAssassin to flag spam."
537 :group 'spam-spamassassin)
539 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
540 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
543 :group 'spam-spamassassin)
545 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
546 "The header inserted by SpamAssassin, giving extended scoring information"
548 :group 'spam-spamassassin)
550 (defcustom spam-sa-learn-path (executable-find "sa-learn")
551 "File path of the sa-learn executable program."
552 :type '(choice (file :tag "Location of spamassassin")
553 (const :tag "spamassassin is not installed"))
554 :group 'spam-spamassassin)
556 (defcustom spam-sa-learn-rebuild t
557 "Whether sa-learn should rebuild the database every time it is called
558 Enable this if you want sa-learn to rebuild the database automatically. Doing
559 this will slightly increase the running time of the spam registration process.
560 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
561 order for SpamAssassin to recognize the new registered spam."
563 :group 'spam-spamassassin)
565 (defcustom spam-sa-learn-spam-switch "--spam"
566 "The switch that sa-learn uses to register spam messages"
568 :group 'spam-spamassassin)
570 (defcustom spam-sa-learn-ham-switch "--ham"
571 "The switch that sa-learn uses to register ham messages"
573 :group 'spam-spamassassin)
575 (defcustom spam-sa-learn-unregister-switch "--forget"
576 "The switch that sa-learn uses to unregister messages messages"
578 :group 'spam-spamassassin)
580 ;;; Key bindings for spam control.
582 (gnus-define-keys gnus-summary-mode-map
583 "St" spam-generic-score
584 "Sx" gnus-summary-mark-as-spam
585 "Mst" spam-generic-score
586 "Msx" gnus-summary-mark-as-spam
587 "\M-d" gnus-summary-mark-as-spam)
589 (defvar spam-cache-lookups t
590 "Whether spam.el will try to cache lookups using `spam-caches'.")
592 (defvar spam-caches (make-hash-table
595 "Cache of spam detection entries.")
597 (defvar spam-old-ham-articles nil
598 "List of old ham articles, generated when a group is entered.")
600 (defvar spam-old-spam-articles nil
601 "List of old spam articles, generated when a group is entered.")
603 (defvar spam-split-disabled nil
604 "If non-nil, `spam-split' is disabled, and always returns nil.")
606 (defvar spam-split-last-successful-check nil
608 `spam-split' will set this to nil or a spam-use-XYZ check if it
611 ;; convenience functions
612 (defun spam-clear-cache (symbol)
613 "Clear the spam-caches entry for a check."
614 (remhash symbol spam-caches))
616 (defun spam-xor (a b)
618 (and (or a b) (not (and a b))))
620 (defun spam-group-ham-mark-p (group mark &optional spam)
621 "Checks if MARK is considered a ham mark in GROUP."
622 (when (stringp group)
623 (let* ((marks (spam-group-ham-marks group spam))
624 (marks (if (symbolp mark)
626 (mapcar 'symbol-value marks))))
629 (defun spam-group-spam-mark-p (group mark)
630 "Checks if MARK is considered a spam mark in GROUP."
631 (spam-group-ham-mark-p group mark t))
633 (defun spam-group-ham-marks (group &optional spam)
634 "In GROUP, get all the ham marks."
635 (when (stringp group)
636 (let* ((marks (if spam
637 (gnus-parameter-spam-marks group)
638 (gnus-parameter-ham-marks group)))
640 (marks (if (listp (car marks)) (car marks) marks)))
643 (defun spam-group-spam-marks (group)
644 "In GROUP, get all the spam marks."
645 (spam-group-ham-marks group t))
647 (defun spam-group-spam-contents-p (group)
648 "Is GROUP a spam group?"
650 (or (member group spam-junk-mailgroups)
651 (memq 'gnus-group-spam-classification-spam
652 (gnus-parameter-spam-contents group)))
655 (defun spam-group-ham-contents-p (group)
656 "Is GROUP a ham group?"
658 (memq 'gnus-group-spam-classification-ham
659 (gnus-parameter-spam-contents group))
662 (defvar spam-list-of-processors
663 ;; note the resend and gmane processors are not defined in gnus.el
664 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
665 (gnus-group-spam-exit-processor-report-resend spam spam-use-resend)
666 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
667 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
668 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
669 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
670 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
671 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
672 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
673 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
674 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
675 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
676 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
677 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
678 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
679 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
680 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
681 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
682 "The `spam-list-of-processors' list.
683 This list contains pairs associating a ham/spam exit processor
684 variable with a classification and a spam-use-* variable.")
686 (defun spam-group-processor-p (group processor)
687 (if (and (stringp group)
689 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
690 (spam-group-processor-multiple-p
692 (cdr-safe (assoc processor spam-list-of-processors))))
695 (defun spam-group-processor-multiple-p (group processor-info)
696 (let* ((classification (nth 0 processor-info))
697 (check (nth 1 processor-info))
698 (parameters (nth 0 (gnus-parameter-spam-process group)))
700 (dolist (parameter parameters)
701 (when (and (null found)
703 (eq classification (nth 0 parameter))
704 (eq check (nth 1 parameter)))
708 (defun spam-group-spam-processor-report-gmane-p (group)
709 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
711 (defun spam-group-spam-processor-report-resend-p (group)
712 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-resend))
714 (defun spam-group-spam-processor-bogofilter-p (group)
715 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
717 (defun spam-group-spam-processor-blacklist-p (group)
718 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
720 (defun spam-group-spam-processor-ifile-p (group)
721 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
723 (defun spam-group-ham-processor-ifile-p (group)
724 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
726 (defun spam-group-spam-processor-spamoracle-p (group)
727 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
729 (defun spam-group-ham-processor-bogofilter-p (group)
730 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
732 (defun spam-group-spam-processor-stat-p (group)
733 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
735 (defun spam-group-ham-processor-stat-p (group)
736 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
738 (defun spam-group-ham-processor-whitelist-p (group)
739 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
741 (defun spam-group-ham-processor-BBDB-p (group)
742 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
744 (defun spam-group-ham-processor-copy-p (group)
745 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
747 (defun spam-group-ham-processor-spamoracle-p (group)
748 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
750 (defun spam-report-articles-gmane (n)
751 "Report the current message as spam via Gmane.
752 Respects the process/prefix convention."
754 (dolist (article (gnus-summary-work-articles n))
755 (gnus-summary-remove-process-mark article)
756 (spam-report-gmane article)))
758 (defun spam-report-articles-resend (n)
759 "Report the current message as spam by resending it.
760 Respects the process/prefix convention. Also see
761 `spam-report-resend-to'."
763 (let ((articles (gnus-summary-work-articles n)))
764 (spam-report-resend articles)
765 (dolist (article articles)
766 (gnus-summary-remove-process-mark article))))
768 (defun spam-necessary-extra-headers ()
769 "Return the extra headers spam.el thinks are necessary."
771 (when (or spam-use-spamassassin
772 spam-use-spamassassin-headers
773 spam-use-regex-headers)
774 (push 'X-Spam-Status list))
777 (defun spam-user-format-function-S (headers)
779 (spam-summary-score headers)))
781 (defun spam-article-sort-by-spam-status (h1 h2)
782 "Sort articles by score."
784 (dolist (header (spam-necessary-extra-headers))
785 (let ((s1 (spam-summary-score h1 header))
786 (s2 (spam-summary-score h2 header)))
788 (setq result (< s1 s2))
792 (defun spam-extra-header-to-number (header headers)
793 "Transform an extra header to a number."
794 (if (gnus-extra-header header headers)
796 ((eq header 'X-Spam-Status)
797 (string-to-number (gnus-replace-in-string
798 (gnus-extra-header header headers)
803 (defun spam-summary-score (headers &optional specific-header)
804 "Score an article for the summary buffer, as fast as possible.
805 With SPECIFIC-HEADER, returns only that header's score.
806 Will not return a nil score."
810 (list specific-header)
811 (spam-necessary-extra-headers)))
813 (spam-extra-header-to-number header headers))
818 (defun spam-generic-score (&optional recheck)
819 "Invoke whatever scoring method we can."
822 ((or spam-use-spamassassin spam-use-spamassassin-headers)
823 (spam-spamassassin-score recheck))
824 ((or spam-use-bsfilter spam-use-bsfilter-headers)
825 (spam-bsfilter-score recheck))
826 (t (spam-bogofilter-score recheck))))
828 ;;; Summary entry and exit processing.
830 (defun spam-summary-prepare ()
831 (setq spam-old-ham-articles
832 (spam-list-articles gnus-newsgroup-articles 'ham))
833 (setq spam-old-spam-articles
834 (spam-list-articles gnus-newsgroup-articles 'spam))
835 (spam-mark-junk-as-spam-routine))
837 ;; The spam processors are invoked for any group, spam or ham or neither
838 (defun spam-summary-prepare-exit ()
839 (unless gnus-group-is-exiting-without-update-p
840 (gnus-message 6 "Exiting summary buffer and applying spam rules")
842 ;; first of all, unregister any articles that are no longer ham or spam
843 ;; we have to iterate over the processors, or else we'll be too slow
844 (dolist (classification '(spam ham))
845 (let* ((old-articles (if (eq classification 'spam)
846 spam-old-spam-articles
847 spam-old-ham-articles))
848 (new-articles (spam-list-articles
849 gnus-newsgroup-articles
851 (changed-articles (spam-set-difference new-articles old-articles)))
852 ;; now that we have the changed articles, we go through the processors
853 (dolist (processor-param spam-list-of-processors)
854 (let ((processor (nth 0 processor-param))
855 (processor-classification (nth 1 processor-param))
856 (check (nth 2 processor-param))
858 (dolist (article changed-articles)
859 (let ((id (spam-fetch-field-message-id-fast article)))
860 (when (spam-log-unregistration-needed-p
861 id 'process classification check)
862 (push article unregister-list))))
863 ;; call spam-register-routine with specific articles to unregister,
864 ;; when there are articles to unregister and the check is enabled
865 (when (and unregister-list (symbol-value check))
866 (spam-register-routine
867 classification check t unregister-list))))))
869 ;; find all the spam processors applicable to this group
870 (dolist (processor-param spam-list-of-processors)
871 (let ((processor (nth 0 processor-param))
872 (classification (nth 1 processor-param))
873 (check (nth 2 processor-param)))
874 (when (and (eq 'spam classification)
875 (spam-group-processor-p gnus-newsgroup-name processor))
876 (spam-register-routine classification check))))
878 (unless (and spam-move-spam-nonspam-groups-only
879 (spam-group-spam-contents-p gnus-newsgroup-name))
880 (let* ((group (gnus-parameter-spam-process-destination
881 gnus-newsgroup-name))
882 (num (spam-mark-spam-as-expired-and-move-routine group)))
885 "%d spam messages are marked as expired and moved it to %s"
888 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
889 ;; expire spam, in case the above did not expire them
890 (let ((num (spam-mark-spam-as-expired-and-move-routine nil)))
893 "%d spam messages are markd as expired without moving it"
896 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
897 (and (spam-group-spam-contents-p gnus-newsgroup-name)
898 spam-process-ham-in-spam-groups)
899 spam-process-ham-in-nonham-groups)
900 ;; find all the ham processors applicable to this group
901 (dolist (processor-param spam-list-of-processors)
902 (let ((processor (nth 0 processor-param))
903 (classification (nth 1 processor-param))
904 (check (nth 2 processor-param)))
905 (when (and (eq 'ham classification)
906 (spam-group-processor-p gnus-newsgroup-name processor))
907 (spam-register-routine classification check)))))
909 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
911 (spam-ham-copy-routine
912 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
914 (gnus-message 6 "%d ham messages are copied" num))))
916 ;; now move all ham articles out of spam groups
917 (when (spam-group-spam-contents-p gnus-newsgroup-name)
919 (spam-ham-move-routine
920 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
922 (gnus-message 6 "%d ham messages are moved from spam group" num)))))
924 (setq spam-old-ham-articles nil)
925 (setq spam-old-spam-articles nil))
927 (defun spam-set-difference (list1 list2)
928 "Return a set difference of LIST1 and LIST2.
929 When either list is nil, the other is returned."
930 (if (and list1 list2)
931 ;; we have two non-nil lists
933 (dolist (item (append list1 list2))
934 (when (and (memq item list1) (memq item list2))
935 (setq list1 (delq item list1))
936 (setq list2 (delq item list2))))
937 (append list1 list2))
938 ;; if either of the lists was nil, return the other one
939 (if list1 list1 list2)))
941 (defun spam-mark-junk-as-spam-routine ()
942 ;; check the global list of group names spam-junk-mailgroups and the
944 (when (spam-group-spam-contents-p gnus-newsgroup-name)
945 (gnus-message 6 "Marking %s articles as spam"
946 (if spam-mark-only-unseen-as-spam
949 (let ((articles (if spam-mark-only-unseen-as-spam
950 gnus-newsgroup-unseen
951 gnus-newsgroup-unreads)))
952 (dolist (article articles)
953 (gnus-summary-mark-article article gnus-spam-mark)))))
955 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
956 (if (and (car-safe groups) (listp (car-safe groups)))
957 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
958 (gnus-summary-kill-process-mark)
959 (let ((articles gnus-newsgroup-articles)
960 (backend-supports-deletions
961 (gnus-check-backend-function
962 'request-move-article gnus-newsgroup-name))
963 article tomove deletep)
964 (dolist (article articles)
965 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
966 (gnus-summary-mark-article article gnus-expirable-mark)
967 (push article tomove)))
969 ;; now do the actual copies
970 (dolist (group groups)
973 (dolist (article tomove)
974 (gnus-summary-set-process-mark article))
976 (if (or (not backend-supports-deletions)
977 (> (length groups) 1))
979 (gnus-summary-copy-article nil group)
981 (gnus-summary-move-article nil group)))))
983 ;; now delete the articles, if there was a copy done, and the
985 (when (and deletep backend-supports-deletions)
986 (dolist (article tomove)
987 (gnus-summary-set-process-mark article))
989 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
990 (gnus-summary-delete-article nil))))
992 (gnus-summary-yank-process-mark)
995 (defun spam-ham-copy-or-move-routine (copy groups)
996 (gnus-summary-kill-process-mark)
997 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
998 (backend-supports-deletions
999 (gnus-check-backend-function
1000 'request-move-article gnus-newsgroup-name))
1001 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1002 article mark todo deletep respool)
1004 (when (member 'respool groups)
1005 (setq respool t) ; boolean for later
1006 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1008 ;; now do the actual move
1009 (dolist (group groups)
1010 (when (and todo (stringp group))
1011 (dolist (article todo)
1012 (when spam-mark-ham-unread-before-move-from-spam-group
1013 (gnus-summary-mark-article article gnus-unread-mark))
1014 (gnus-summary-set-process-mark article))
1016 (if respool ; respooling is with a "fake" group
1017 (let ((spam-split-disabled
1018 (or spam-split-disabled
1019 spam-disable-spam-split-during-ham-respool)))
1020 (gnus-summary-respool-article nil respool-method))
1021 (if (or (not backend-supports-deletions) ; else, we are not respooling
1022 (> (length groups) 1))
1023 (progn ; if copying, copy and set deletep
1024 (gnus-summary-copy-article nil group)
1026 (gnus-summary-move-article nil group))))) ; else move articles
1028 ;; now delete the articles, unless a) copy is t, and there was a copy done
1029 ;; b) a move was done to a single group
1030 ;; c) backend-supports-deletions is nil
1032 (when (and deletep backend-supports-deletions)
1033 (dolist (article todo)
1034 (gnus-summary-set-process-mark article))
1036 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1037 (gnus-summary-delete-article nil)))))
1039 (gnus-summary-yank-process-mark)
1042 (defun spam-ham-copy-routine (&rest groups)
1043 (if (and (car-safe groups) (listp (car-safe groups)))
1044 (apply 'spam-ham-copy-routine (car groups))
1045 (spam-ham-copy-or-move-routine t groups)))
1047 (defun spam-ham-move-routine (&rest groups)
1048 (if (and (car-safe groups) (listp (car-safe groups)))
1049 (apply 'spam-ham-move-routine (car groups))
1050 (spam-ham-copy-or-move-routine nil groups)))
1052 (defun spam-get-article-as-string (article)
1053 (when (numberp article)
1055 (gnus-request-article-this-buffer
1057 gnus-newsgroup-name)
1061 ;; (defun spam-get-article-as-filename (article)
1062 ;; (let ((article-filename))
1063 ;; (when (numberp article)
1064 ;; (nnml-possibly-change-directory
1065 ;; (gnus-group-real-name gnus-newsgroup-name))
1066 ;; (setq article-filename (expand-file-name
1067 ;; (int-to-string article) nnml-current-directory)))
1068 ;; (if (file-exists-p article-filename)
1072 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1073 "Fetch a field quickly, using the internal gnus-data-list function"
1074 (when (numberp article)
1075 (let* ((data-header (or prepared-data-header
1076 (spam-fetch-article-header article))))
1077 (if (arrayp data-header)
1079 ((equal field 'from)
1080 (mail-header-from data-header))
1081 ((equal field 'message-id)
1082 (mail-header-message-id data-header))
1083 ((equal field 'subject)
1084 (mail-header-subject data-header))
1085 ((equal field 'references)
1086 (mail-header-references data-header))
1087 ((equal field 'date)
1088 (mail-header-date data-header))
1089 ((equal field 'xref)
1090 (mail-header-xref data-header))
1091 ((equal field 'extra)
1092 (mail-header-extra data-header))
1095 (gnus-message 6 "Article %d has a nil data header" article)))))
1097 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1098 (spam-fetch-field-fast article 'from prepared-data-header))
1100 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1101 (spam-fetch-field-fast article 'subject prepared-data-header))
1103 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1104 (spam-fetch-field-fast article 'message-id prepared-data-header))
1106 (defun spam-generate-fake-headers (article)
1107 (let ((dh (spam-fetch-article-header article)))
1111 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1112 "Date: %s\nReferences: %s\nXref: %s\n")
1113 (spam-fetch-field-fast article 'from dh)
1114 (spam-fetch-field-fast article 'subject dh)
1115 (spam-fetch-field-fast article 'message-id dh)
1116 (spam-fetch-field-fast article 'date dh)
1117 (spam-fetch-field-fast article 'references dh)
1118 (spam-fetch-field-fast article 'xref dh))
1119 (when (spam-fetch-field-fast article 'extra dh)
1120 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1123 "spam-generate-fake-headers: article %d didn't have a valid header"
1126 (defun spam-fetch-article-header (article)
1128 (set-buffer gnus-summary-buffer)
1129 (gnus-read-header article)
1130 (nth 3 (assq article gnus-newsgroup-data))))
1133 ;;;; Spam determination.
1135 (defvar spam-list-of-checks
1136 '((spam-use-blacklist . spam-check-blacklist)
1137 (spam-use-regex-headers . spam-check-regex-headers)
1138 (spam-use-gmane-xref . spam-check-gmane-xref)
1139 (spam-use-regex-body . spam-check-regex-body)
1140 (spam-use-whitelist . spam-check-whitelist)
1141 (spam-use-BBDB . spam-check-BBDB)
1142 (spam-use-BBDB-exclusive . spam-check-BBDB)
1143 (spam-use-ifile . spam-check-ifile)
1144 (spam-use-spamoracle . spam-check-spamoracle)
1145 (spam-use-stat . spam-check-stat)
1146 (spam-use-blackholes . spam-check-blackholes)
1147 (spam-use-hashcash . spam-check-hashcash)
1148 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
1149 (spam-use-spamassassin . spam-check-spamassassin)
1150 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
1151 (spam-use-bogofilter . spam-check-bogofilter)
1152 (spam-use-bsfilter-headers . spam-check-bsfilter-headers)
1153 (spam-use-bsfilter . spam-check-bsfilter))
1154 "The spam-list-of-checks list contains pairs associating a
1155 parameter variable with a spam checking function. If the
1156 parameter variable is true, then the checking function is called,
1157 and its value decides what happens. Each individual check may
1158 return nil, t, or a mailgroup name. The value nil means that the
1159 check does not yield a decision, and so, that further checks are
1160 needed. The value t means that the message is definitely not
1161 spam, and that further spam checks should be inhibited.
1162 Otherwise, a mailgroup name or the symbol 'spam (depending on
1163 spam-split-symbolic-return) is returned where the mail should go,
1164 and further checks are also inhibited. The usual mailgroup name
1165 is the value of `spam-split-group', meaning that the message is
1166 definitely a spam.")
1168 (defvar spam-list-of-statistical-checks
1175 spam-use-spamassassin
1176 spam-use-spamoracle)
1177 "The spam-list-of-statistical-checks list contains all the mail
1178 splitters that need to have the full message body available.
1179 Note that you should fetch extra headers if you don't like this,
1180 e.g. fetch the 'Received' header for spam-use-blackholes.")
1182 (defun spam-split (&rest specific-checks)
1183 "Split this message into the `spam' group if it is spam.
1184 This function can be used as an entry in the variable `nnmail-split-fancy',
1185 for example like this: (: spam-split). It can take checks as
1186 parameters. A string as a parameter will set the
1187 spam-split-group to that string.
1189 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1191 (setq spam-split-last-successful-check nil)
1192 (unless spam-split-disabled
1193 (let ((spam-split-group-choice spam-split-group))
1194 (dolist (check specific-checks)
1195 (when (stringp check)
1196 (setq spam-split-group-choice check)
1197 (setq specific-checks (delq check specific-checks))))
1199 (let ((spam-split-group spam-split-group-choice))
1202 (dolist (check spam-list-of-statistical-checks)
1203 (when (and (symbolp check)
1204 (or (symbol-value check)
1205 (memq check specific-checks)))
1207 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1208 (symbol-name check))
1210 ;; (progn (widen) (debug (buffer-string)))
1211 (let ((list-of-checks spam-list-of-checks)
1213 (while (and list-of-checks (not decision))
1214 (let ((pair (pop list-of-checks)))
1216 ;; either, given specific checks, this is one of them
1217 (and specific-checks (memq (car pair) specific-checks))
1218 ;; or, given no specific checks, spam-use-CHECK is set
1219 (and (null specific-checks) (symbol-value (car pair))))
1220 (gnus-message 6 "spam-split: calling the %s function"
1221 (symbol-name (cdr pair)))
1222 (setq decision (funcall (cdr pair)))
1223 ;; if we got a decision at all, save the current check
1225 (setq spam-split-last-successful-check (car pair)))
1227 (when (eq decision 'spam)
1228 (unless spam-split-symbolic-return
1231 (format "spam-split got %s but %s is nil"
1232 (symbol-name decision)
1233 (symbol-name spam-split-symbolic-return))))))))
1235 (if spam-split-symbolic-return-positive 'ham nil)
1238 (defun spam-find-spam ()
1239 "This function will detect spam in the current newsgroup using spam-split."
1242 (let* ((group gnus-newsgroup-name)
1243 (autodetect (gnus-parameter-spam-autodetect group))
1244 (methods (gnus-parameter-spam-autodetect-methods group))
1245 (first-method (nth 0 methods))
1246 (articles (if spam-autodetect-recheck-messages
1247 gnus-newsgroup-articles
1248 gnus-newsgroup-unseen))
1249 article-cannot-be-faked)
1251 (dolist (check spam-list-of-statistical-checks)
1252 (when (and (symbolp check)
1253 (memq check methods))
1254 (setq article-cannot-be-faked t)
1257 (when (memq 'default methods)
1258 (setq article-cannot-be-faked t))
1260 (when (and autodetect
1261 (not (equal first-method 'none)))
1264 (let ((id (spam-fetch-field-message-id-fast article))
1265 (subject (spam-fetch-field-subject-fast article))
1266 (sender (spam-fetch-field-from-fast article))
1270 (gnus-message 6 "Article %d has no message ID!" article))
1272 (when (and id spam-log-to-registry)
1273 (setq registry-lookup (spam-log-registration-type id 'incoming))
1274 (when registry-lookup
1277 "spam-find-spam: message %s was already registered incoming"
1280 (let* ((spam-split-symbolic-return t)
1281 (spam-split-symbolic-return-positive t)
1282 (fake-headers (spam-generate-fake-headers article))
1286 (if article-cannot-be-faked
1287 (gnus-request-article-this-buffer
1290 ;; else, we fake the article
1291 (when fake-headers (insert fake-headers)))
1292 (if (or (null first-method)
1293 (equal first-method 'default))
1295 (apply 'spam-split methods))))))
1296 (if (equal split-return 'spam)
1297 (gnus-summary-mark-article article gnus-spam-mark))
1299 (when (and id split-return spam-log-to-registry)
1300 (when (zerop (gnus-registry-group-count id))
1301 (gnus-registry-add-group
1302 id group subject sender))
1304 (unless registry-lookup
1305 (spam-log-processing-to-registry
1309 spam-split-last-successful-check
1313 (defvar spam-registration-functions
1314 ;; first the ham register, second the spam register function
1315 ;; third the ham unregister, fourth the spam unregister function
1316 '((spam-use-blacklist nil
1317 spam-blacklist-register-routine
1319 spam-blacklist-unregister-routine)
1320 (spam-use-whitelist spam-whitelist-register-routine
1322 spam-whitelist-unregister-routine
1324 (spam-use-ham-copy nil
1328 (spam-use-BBDB spam-BBDB-register-routine
1330 spam-BBDB-unregister-routine
1332 (spam-use-ifile spam-ifile-register-ham-routine
1333 spam-ifile-register-spam-routine
1334 spam-ifile-unregister-ham-routine
1335 spam-ifile-unregister-spam-routine)
1336 (spam-use-spamoracle spam-spamoracle-learn-ham
1337 spam-spamoracle-learn-spam
1338 spam-spamoracle-unlearn-ham
1339 spam-spamoracle-unlearn-spam)
1340 (spam-use-stat spam-stat-register-ham-routine
1341 spam-stat-register-spam-routine
1342 spam-stat-unregister-ham-routine
1343 spam-stat-unregister-spam-routine)
1344 ;; note that spam-use-gmane and spam-use-resend are not legitimate checks
1346 spam-report-gmane-register-routine
1347 ;; does Gmane support unregistration?
1350 (spam-use-resend nil
1351 spam-report-resend-register-routine
1354 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1355 spam-spamassassin-register-spam-routine
1356 spam-spamassassin-unregister-ham-routine
1357 spam-spamassassin-unregister-spam-routine)
1358 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1359 spam-bogofilter-register-spam-routine
1360 spam-bogofilter-unregister-ham-routine
1361 spam-bogofilter-unregister-spam-routine)
1362 (spam-use-bsfilter spam-bsfilter-register-ham-routine
1363 spam-bsfilter-register-spam-routine
1364 spam-bsfilter-unregister-ham-routine
1365 spam-bsfilter-unregister-spam-routine))
1366 "The spam-registration-functions list contains pairs
1367 associating a parameter variable with the ham and spam
1368 registration functions, and the ham and spam unregistration
1371 (defun spam-classification-valid-p (classification)
1372 (or (eq classification 'spam)
1373 (eq classification 'ham)))
1375 (defun spam-process-type-valid-p (process-type)
1376 (or (eq process-type 'incoming)
1377 (eq process-type 'process)))
1379 (defun spam-registration-check-valid-p (check)
1380 (assoc check spam-registration-functions))
1382 (defun spam-unregistration-check-valid-p (check)
1383 (assoc check spam-registration-functions))
1385 (defun spam-registration-function (classification check)
1386 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1387 (if (eq classification 'spam)
1391 (defun spam-unregistration-function (classification check)
1392 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1393 (if (eq classification 'spam)
1397 (defun spam-list-articles (articles classification)
1398 (let ((mark-check (if (eq classification 'spam)
1399 'spam-group-spam-mark-p
1400 'spam-group-ham-mark-p))
1401 alist mark-cache-yes mark-cache-no)
1402 (dolist (article articles)
1403 (let ((mark (gnus-summary-article-mark article)))
1404 (unless (or (memq mark mark-cache-yes)
1405 (memq mark mark-cache-no))
1406 (if (funcall mark-check
1409 (push mark mark-cache-yes)
1410 (push mark mark-cache-no)))
1411 (when (memq mark mark-cache-yes)
1412 (push article alist))))
1415 (defun spam-register-routine (classification
1417 &optional unregister
1419 (when (and (spam-classification-valid-p classification)
1420 (spam-registration-check-valid-p check))
1421 (let* ((register-function
1422 (spam-registration-function classification check))
1423 (unregister-function
1424 (spam-unregistration-function classification check))
1425 (run-function (if unregister
1428 (log-function (if unregister
1429 'spam-log-undo-registration
1430 'spam-log-processing-to-registry))
1434 ;; make list of articles, using specific-articles if given
1435 (setq articles (or specific-articles
1437 gnus-newsgroup-articles
1440 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1441 (if unregister "Unregistering" "Registering")
1443 (if specific-articles "specific" "")
1444 (symbol-name classification)
1445 (symbol-name check))
1446 (funcall run-function articles)
1447 ;; now log all the registrations (or undo them, depending on unregister)
1448 (dolist (article articles)
1449 (funcall log-function
1450 (spam-fetch-field-message-id-fast article)
1454 gnus-newsgroup-name))))))
1456 ;;; log a ham- or spam-processor invocation to the registry
1457 (defun spam-log-processing-to-registry (id type classification check group)
1458 (when spam-log-to-registry
1459 (if (and (stringp id)
1461 (spam-process-type-valid-p type)
1462 (spam-classification-valid-p classification)
1463 (spam-registration-check-valid-p check))
1464 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1465 (cell (list classification check group)))
1466 (push cell cell-list)
1467 (gnus-registry-store-extra-entry
1474 (format "%s call with bad ID, type, classification, spam-check, or group"
1475 "spam-log-processing-to-registry")))))
1477 ;;; check if a ham- or spam-processor registration has been done
1478 (defun spam-log-registered-p (id type)
1479 (when spam-log-to-registry
1480 (if (and (stringp id)
1481 (spam-process-type-valid-p type))
1482 (cdr-safe (gnus-registry-fetch-extra id type))
1486 (format "%s called with bad ID, type, classification, or spam-check"
1487 "spam-log-registered-p"))
1490 ;;; check what a ham- or spam-processor registration says
1491 ;;; returns nil if conflicting registrations are found
1492 (defun spam-log-registration-type (id type)
1495 (dolist (reg (spam-log-registered-p id type))
1496 (let ((classification (nth 0 reg)))
1497 (when (spam-classification-valid-p classification)
1499 (not (eq classification decision)))
1500 (setq count (+ 1 count)))
1501 (setq decision classification))))
1507 ;;; check if a ham- or spam-processor registration needs to be undone
1508 (defun spam-log-unregistration-needed-p (id type classification check)
1509 (when spam-log-to-registry
1510 (if (and (stringp id)
1511 (spam-process-type-valid-p type)
1512 (spam-classification-valid-p classification)
1513 (spam-registration-check-valid-p check))
1514 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1516 (dolist (cell cell-list)
1518 (when (and (eq classification (nth 0 cell))
1519 (eq check (nth 1 cell)))
1525 (format "%s called with bad ID, type, classification, or spam-check"
1526 "spam-log-unregistration-needed-p"))
1530 ;;; undo a ham- or spam-processor registration (the group is not used)
1531 (defun spam-log-undo-registration (id type classification check &optional group)
1532 (when (and spam-log-to-registry
1533 (spam-log-unregistration-needed-p id type classification check))
1534 (if (and (stringp id)
1535 (spam-process-type-valid-p type)
1536 (spam-classification-valid-p classification)
1537 (spam-registration-check-valid-p check))
1538 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1539 new-cell-list found)
1540 (dolist (cell cell-list)
1541 (unless (and (eq classification (nth 0 cell))
1542 (eq check (nth 1 cell)))
1543 (push cell new-cell-list)))
1544 (gnus-registry-store-extra-entry
1549 (gnus-message 6 (format "%s call with bad ID, type, spam-check, or group"
1550 "spam-log-undo-registration"))
1553 ;;; set up IMAP widening if it's necessary
1554 (defun spam-setup-widening ()
1555 (dolist (check spam-list-of-statistical-checks)
1556 (when (symbol-value check)
1557 (setq nnimap-split-download-body-default t))))
1561 (defun spam-check-gmane-xref ()
1563 (message-fetch-field "Xref")
1564 (message-fetch-field "Newsgroups")))
1565 (spam-split-group (if spam-split-symbolic-return
1568 (when header ; return nil when no header
1569 (when (string-match spam-gmane-xref-spam-group
1571 spam-split-group))))
1576 (defun spam-check-regex-body ()
1577 (let ((spam-regex-headers-ham spam-regex-body-ham)
1578 (spam-regex-headers-spam spam-regex-body-spam))
1579 (spam-check-regex-headers t)))
1584 (defun spam-check-regex-headers (&optional body)
1585 (let ((type (if body "body" "header"))
1586 (spam-split-group (if spam-split-symbolic-return
1590 (dolist (h-regex spam-regex-headers-ham)
1592 (goto-char (point-min))
1593 (when (re-search-forward h-regex nil t)
1594 (message "Ham regex %s search positive." type)
1596 (dolist (s-regex spam-regex-headers-spam)
1598 (goto-char (point-min))
1599 (when (re-search-forward s-regex nil t)
1600 (message "Spam regex %s search positive." type)
1602 (setq ret spam-split-group))))
1608 (defun spam-reverse-ip-string (ip)
1610 (mapconcat 'identity
1611 (nreverse (split-string ip "\\."))
1614 (defun spam-check-blackholes ()
1615 "Check the Received headers for blackholed relays."
1616 (let ((headers (message-fetch-field "received"))
1617 (spam-split-group (if spam-split-symbolic-return
1624 (goto-char (point-min))
1625 (gnus-message 6 "Checking headers for relay addresses")
1626 (while (re-search-forward
1627 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1628 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1629 (push (spam-reverse-ip-string (match-string 1))
1631 (dolist (server spam-blackhole-servers)
1633 (unless (and spam-blackhole-good-server-regex
1634 ;; match the good-server-regex against the reversed (again) IP string
1636 spam-blackhole-good-server-regex
1637 (spam-reverse-ip-string ip)))
1639 (let ((query-string (concat ip "." server)))
1641 (let ((query-result (query-dig query-string)))
1643 (gnus-message 6 "(DIG): positive blackhole check '%s'"
1645 (push (list ip server query-result)
1647 ;; else, if not using dig.el
1648 (when (query-dns query-string)
1649 (gnus-message 6 "positive blackhole check")
1650 (push (list ip server (query-dns query-string 'TXT))
1661 (defun spam-check-hashcash ()
1662 "Check the headers for hashcash payments."
1663 (mail-check-payment))) ;mail-check-payment returns a boolean
1666 (defalias 'mail-check-payment 'ignore)
1667 (defalias 'spam-check-hashcash 'ignore))))
1671 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1672 ;;; <sacha@giotto.sj.ru>
1674 ;; all this is done inside a condition-case to trap errors
1681 ;; when the BBDB changes, we want to clear out our cache
1682 (defun spam-clear-cache-BBDB (&rest immaterial)
1683 (spam-clear-cache 'spam-use-BBDB))
1685 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1687 (defun spam-enter-ham-BBDB (addresses &optional remove)
1688 "Enter an address into the BBDB; implies ham (non-spam) sender"
1689 (dolist (from addresses)
1690 (when (stringp from)
1691 (let* ((parsed-address (gnus-extract-address-components from))
1692 (name (or (nth 0 parsed-address) "Ham Sender"))
1693 (remove-function (if remove
1694 'bbdb-delete-record-internal
1696 (net-address (nth 1 parsed-address))
1697 (record (and net-address
1698 (bbdb-search-simple nil net-address))))
1700 (gnus-message 6 "%s address %s %s BBDB"
1701 (if remove "Deleting" "Adding")
1703 (if remove "from" "to"))
1705 (funcall remove-function record)
1706 (bbdb-create-internal name nil net-address nil nil
1707 "ham sender added by spam.el")))))))
1709 (defun spam-BBDB-register-routine (articles &optional unregister)
1711 (dolist (article articles)
1712 (when (stringp (spam-fetch-field-from-fast article))
1713 (push (spam-fetch-field-from-fast article) addresses)))
1714 ;; now do the register/unregister action
1715 (spam-enter-ham-BBDB addresses unregister)))
1717 (defun spam-BBDB-unregister-routine (articles)
1718 (spam-BBDB-register-routine articles t))
1720 (defun spam-check-BBDB ()
1721 "Mail from people in the BBDB is classified as ham or non-spam"
1722 (let ((who (message-fetch-field "from"))
1723 (spam-split-group (if spam-split-symbolic-return
1726 bbdb-cache bbdb-hashtable)
1727 (when spam-cache-lookups
1728 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1731 ;; this is the expanded (bbdb-hashtable) macro
1732 ;; without the debugging support
1733 (with-current-buffer (bbdb-buffer)
1735 (save-window-excursion
1736 (bbdb-records nil t)
1738 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1740 (setq who (nth 1 (gnus-extract-address-components who)))
1742 (if spam-cache-lookups
1744 (intern-soft who bbdb-cache))
1745 (bbdb-search-simple nil who))
1747 (if spam-use-BBDB-exclusive
1752 (defalias 'bbdb-search-simple 'ignore)
1753 (defalias 'bbdb-records 'ignore)
1754 (defalias 'bbdb-buffer 'ignore)
1755 (defalias 'spam-check-BBDB 'ignore)
1756 (defalias 'spam-BBDB-register-routine 'ignore)
1757 (defalias 'spam-enter-ham-BBDB 'ignore)
1758 (defalias 'bbdb-create-internal 'ignore)
1759 (defalias 'bbdb-delete-record-internal 'ignore)
1760 (defalias 'bbdb-records 'ignore))))
1765 ;;; check the ifile backend; return nil if the mail was NOT classified
1768 (defun spam-get-ifile-database-parameter ()
1769 "Get the command-line parameter for ifile's database from
1770 spam-ifile-database-path."
1771 (if spam-ifile-database-path
1772 (format "--db-file=%s" spam-ifile-database-path)
1775 (defun spam-check-ifile ()
1776 "Check the ifile backend for the classification of this message."
1777 (let ((article-buffer-name (buffer-name))
1778 (spam-split-group (if spam-split-symbolic-return
1783 (let ((temp-buffer-name (buffer-name))
1784 (db-param (spam-get-ifile-database-parameter)))
1786 (set-buffer article-buffer-name)
1787 (apply 'call-process-region
1788 (point-min) (point-max) spam-ifile-path
1789 nil temp-buffer-name nil "-c"
1790 (if db-param `(,db-param "-q") `("-q"))))
1791 ;; check the return now (we're back in the temp buffer)
1792 (goto-char (point-min))
1794 (setq category (buffer-substring (point) (point-at-eol))))
1795 (when (not (zerop (length category))) ; we need a category here
1796 (if spam-ifile-all-categories
1797 (setq return category)
1798 ;; else, if spam-ifile-all-categories is not set...
1799 (when (string-equal spam-ifile-spam-category category)
1800 (setq return spam-split-group)))))) ; note return is nil otherwise
1803 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1804 "Register an article, given as a string, with a category.
1805 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1806 (let ((category (or category gnus-newsgroup-name))
1807 (add-or-delete-option (if unregister "-d" "-i"))
1808 (db (spam-get-ifile-database-parameter))
1811 (dolist (article articles)
1812 (let ((article-string (spam-get-article-as-string article)))
1813 (when (stringp article-string)
1814 (insert article-string))))
1815 (apply 'call-process-region
1816 (point-min) (point-max) spam-ifile-path
1818 add-or-delete-option category
1819 (if db `(,db "-h") `("-h"))))))
1821 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1822 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1824 (defun spam-ifile-unregister-spam-routine (articles)
1825 (spam-ifile-register-spam-routine articles t))
1827 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1828 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1830 (defun spam-ifile-unregister-ham-routine (articles)
1831 (spam-ifile-register-ham-routine articles t))
1838 (let ((spam-stat-install-hooks nil))
1839 (require 'spam-stat))
1841 (defun spam-check-stat ()
1842 "Check the spam-stat backend for the classification of this message"
1843 (let ((spam-split-group (if spam-split-symbolic-return
1846 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1847 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1849 (spam-stat-split-fancy)))
1851 (defun spam-stat-register-spam-routine (articles &optional unregister)
1852 (dolist (article articles)
1853 (let ((article-string (spam-get-article-as-string article)))
1855 (insert article-string)
1857 (spam-stat-buffer-change-to-non-spam)
1858 (spam-stat-buffer-is-spam))))))
1860 (defun spam-stat-unregister-spam-routine (articles)
1861 (spam-stat-register-spam-routine articles t))
1863 (defun spam-stat-register-ham-routine (articles &optional unregister)
1864 (dolist (article articles)
1865 (let ((article-string (spam-get-article-as-string article)))
1867 (insert article-string)
1869 (spam-stat-buffer-change-to-spam)
1870 (spam-stat-buffer-is-non-spam))))))
1872 (defun spam-stat-unregister-ham-routine (articles)
1873 (spam-stat-register-ham-routine articles t))
1875 (defun spam-maybe-spam-stat-load ()
1876 (when spam-use-stat (spam-stat-load)))
1878 (defun spam-maybe-spam-stat-save ()
1879 (when spam-use-stat (spam-stat-save))))
1882 (defalias 'spam-stat-load 'ignore)
1883 (defalias 'spam-stat-save 'ignore)
1884 (defalias 'spam-maybe-spam-stat-load 'ignore)
1885 (defalias 'spam-maybe-spam-stat-save 'ignore)
1886 (defalias 'spam-stat-register-ham-routine 'ignore)
1887 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1888 (defalias 'spam-stat-register-spam-routine 'ignore)
1889 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1890 (defalias 'spam-stat-buffer-is-spam 'ignore)
1891 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1892 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1893 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1894 (defalias 'spam-stat-split-fancy 'ignore)
1895 (defalias 'spam-check-stat 'ignore))))
1899 ;;;; Blacklists and whitelists.
1901 (defvar spam-whitelist-cache nil)
1902 (defvar spam-blacklist-cache nil)
1904 (defun spam-kill-whole-line ()
1906 (let ((kill-whole-line t))
1909 ;;; address can be a list, too
1910 (defun spam-enter-whitelist (address &optional remove)
1911 "Enter ADDRESS (list or single) into the whitelist.
1912 With a non-nil REMOVE, remove them."
1913 (interactive "sAddress: ")
1914 (spam-enter-list address spam-whitelist remove)
1915 (setq spam-whitelist-cache nil)
1916 (spam-clear-cache 'spam-use-whitelist))
1918 ;;; address can be a list, too
1919 (defun spam-enter-blacklist (address &optional remove)
1920 "Enter ADDRESS (list or single) into the blacklist.
1921 With a non-nil REMOVE, remove them."
1922 (interactive "sAddress: ")
1923 (spam-enter-list address spam-blacklist remove)
1924 (setq spam-blacklist-cache nil)
1925 (spam-clear-cache 'spam-use-whitelist))
1927 (defun spam-enter-list (addresses file &optional remove)
1928 "Enter ADDRESSES into the given FILE.
1929 Either the whitelist or the blacklist files can be used. With
1930 REMOVE not nil, remove the ADDRESSES."
1931 (if (stringp addresses)
1932 (spam-enter-list (list addresses) file remove)
1933 ;; else, we have a list of addresses here
1934 (unless (file-exists-p (file-name-directory file))
1935 (make-directory (file-name-directory file) t))
1938 (find-file-noselect file))
1939 (dolist (a addresses)
1941 (goto-char (point-min))
1942 (if (re-search-forward (regexp-quote a) nil t)
1943 ;; found the address
1945 (spam-kill-whole-line))
1946 ;; else, the address was not found
1948 (goto-char (point-max))
1954 (defun spam-filelist-build-cache (type)
1955 (let ((cache (if (eq type 'spam-use-blacklist)
1956 spam-blacklist-cache
1957 spam-whitelist-cache))
1959 (unless (gethash type spam-caches)
1961 (let ((address (pop cache)))
1962 (unless (zerop (length address)) ; 0 for a nil address too
1963 (setq address (regexp-quote address))
1964 ;; fix regexp-quote's treatment of user-intended regexes
1965 (while (string-match "\\\\\\*" address)
1966 (setq address (replace-match ".*" t t address))))
1967 (push address parsed-cache)))
1968 (puthash type parsed-cache spam-caches))))
1970 (defun spam-filelist-check-cache (type from)
1971 (when (stringp from)
1972 (spam-filelist-build-cache type)
1974 (dolist (address (gethash type spam-caches))
1975 (when (and address (string-match address from))
1980 ;;; returns t if the sender is in the whitelist, nil or
1981 ;;; spam-split-group otherwise
1982 (defun spam-check-whitelist ()
1983 ;; FIXME! Should it detect when file timestamps change?
1984 (let ((spam-split-group (if spam-split-symbolic-return
1987 (unless spam-whitelist-cache
1988 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1989 (if (spam-from-listed-p 'spam-use-whitelist)
1991 (if spam-use-whitelist-exclusive
1995 (defun spam-check-blacklist ()
1996 ;; FIXME! Should it detect when file timestamps change?
1997 (let ((spam-split-group (if spam-split-symbolic-return
2000 (unless spam-blacklist-cache
2001 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2002 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
2004 (defun spam-parse-list (file)
2005 (when (file-readable-p file)
2006 (let (contents address)
2008 (insert-file-contents file)
2010 (setq address (buffer-substring (point) (point-at-eol)))
2012 ;; insert the e-mail address if detected, otherwise the raw data
2013 (unless (zerop (length address))
2014 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
2015 (push (or pure-address address) contents)))))
2016 (nreverse contents))))
2018 (defun spam-from-listed-p (type)
2019 (let ((from (message-fetch-field "from"))
2021 (spam-filelist-check-cache type from)))
2023 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2024 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2025 (declassification (if blacklist 'ham 'spam))
2027 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2029 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2030 from addresses unregister-list article-unregister-list)
2031 (dolist (article articles)
2032 (let ((from (spam-fetch-field-from-fast article))
2033 (id (spam-fetch-field-message-id-fast article))
2035 (when (stringp from)
2036 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2037 (when (and (not sender-ignored)
2038 (stringp ignore-regex)
2039 (string-match ignore-regex from))
2040 (setq sender-ignored t)))
2041 ;; remember the messages we need to unregister, unless remove is set
2044 (spam-log-unregistration-needed-p
2045 id 'process declassification de-symbol))
2046 (push article article-unregister-list)
2047 (push from unregister-list))
2048 (unless sender-ignored
2049 (push from addresses)))))
2052 (funcall enter-function addresses t) ; unregister all these addresses
2053 ;; else, register normally and unregister what we need to
2054 (funcall remove-function unregister-list t)
2055 (dolist (article article-unregister-list)
2056 (spam-log-undo-registration
2057 (spam-fetch-field-message-id-fast article)
2061 (funcall enter-function addresses nil))))
2063 (defun spam-blacklist-unregister-routine (articles)
2064 (spam-blacklist-register-routine articles t))
2066 (defun spam-blacklist-register-routine (articles &optional unregister)
2067 (spam-filelist-register-routine articles t unregister))
2069 (defun spam-whitelist-unregister-routine (articles)
2070 (spam-whitelist-register-routine articles t))
2072 (defun spam-whitelist-register-routine (articles &optional unregister)
2073 (spam-filelist-register-routine articles nil unregister))
2076 ;;;; Spam-report glue
2077 (defun spam-report-gmane-register-routine (articles)
2079 (apply 'spam-report-gmane articles)))
2081 (defun spam-report-resend-register-routine (articles)
2082 (spam-report-resend articles))
2086 (defun spam-check-bogofilter-headers (&optional score)
2087 (let ((header (message-fetch-field spam-bogofilter-header))
2088 (spam-split-group (if spam-split-symbolic-return
2091 (when header ; return nil when no header
2092 (if score ; scoring mode
2093 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2094 (match-string 1 header)
2096 ;; spam detection mode
2097 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2099 spam-split-group)))))
2101 ;; return something sensible if the score can't be determined
2102 (defun spam-bogofilter-score (&optional recheck)
2103 "Get the Bogofilter spamicity score"
2105 (save-window-excursion
2106 (gnus-summary-show-article t)
2107 (set-buffer gnus-article-buffer)
2108 (let ((score (or (unless recheck
2109 (spam-check-bogofilter-headers t))
2110 (spam-check-bogofilter t))))
2111 (gnus-summary-show-article)
2112 (message "Spamicity score %s" score)
2115 (defun spam-check-bogofilter (&optional score)
2116 "Check the Bogofilter backend for the classification of this message"
2117 (let ((article-buffer-name (buffer-name))
2118 (db spam-bogofilter-database-directory)
2121 (let ((temp-buffer-name (buffer-name)))
2123 (set-buffer article-buffer-name)
2124 (apply 'call-process-region
2125 (point-min) (point-max)
2126 spam-bogofilter-path
2127 nil temp-buffer-name nil
2128 (if db `("-d" ,db "-v") `("-v"))))
2129 (setq return (spam-check-bogofilter-headers score))))
2132 (defun spam-bogofilter-register-with-bogofilter (articles
2134 &optional unregister)
2135 "Register an article, given as a string, as spam or non-spam."
2136 (dolist (article articles)
2137 (let ((article-string (spam-get-article-as-string article))
2138 (db spam-bogofilter-database-directory)
2139 (switch (if unregister
2141 spam-bogofilter-spam-strong-switch
2142 spam-bogofilter-ham-strong-switch)
2144 spam-bogofilter-spam-switch
2145 spam-bogofilter-ham-switch))))
2146 (when (stringp article-string)
2148 (insert article-string)
2150 (apply 'call-process-region
2151 (point-min) (point-max)
2152 spam-bogofilter-path
2154 (if db `("-d" ,db "-v") `("-v"))))))))
2156 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2157 (spam-bogofilter-register-with-bogofilter articles t unregister))
2159 (defun spam-bogofilter-unregister-spam-routine (articles)
2160 (spam-bogofilter-register-spam-routine articles t))
2162 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2163 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2165 (defun spam-bogofilter-unregister-ham-routine (articles)
2166 (spam-bogofilter-register-ham-routine articles t))
2171 (defun spam-check-spamoracle ()
2172 "Run spamoracle on an article to determine whether it's spam."
2173 (let ((article-buffer-name (buffer-name))
2174 (spam-split-group (if spam-split-symbolic-return
2178 (let ((temp-buffer-name (buffer-name)))
2180 (set-buffer article-buffer-name)
2182 (apply 'call-process-region
2183 (point-min) (point-max)
2184 spam-spamoracle-binary
2185 nil temp-buffer-name nil
2186 (if spam-spamoracle-database
2187 `("-f" ,spam-spamoracle-database "mark")
2191 (set-buffer temp-buffer-name)
2192 (goto-char (point-min))
2193 (when (re-search-forward "^X-Spam: yes;" nil t)
2195 (error "Error running spamoracle: %s" status))))))))
2197 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2198 "Run spamoracle in training mode."
2200 (let ((temp-buffer-name (buffer-name)))
2202 (goto-char (point-min))
2203 (dolist (article articles)
2204 (insert (spam-get-article-as-string article)))
2205 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2209 (apply 'call-process-region
2210 (point-min) (point-max)
2211 spam-spamoracle-binary
2212 nil temp-buffer-name nil
2213 (if spam-spamoracle-database
2214 `("-f" ,spam-spamoracle-database
2217 (unless (eq 0 status)
2218 (error "Error running spamoracle: %s" status)))))))
2220 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2221 (spam-spamoracle-learn articles nil unregister))
2223 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2224 (spam-spamoracle-learn-ham articles t))
2226 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2227 (spam-spamoracle-learn articles t unregister))
2229 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2230 (spam-spamoracle-learn-spam articles t))
2234 ;;; based mostly on the bogofilter code
2235 (defun spam-check-spamassassin-headers (&optional score)
2236 "Check the SpamAssassin headers for the classification of this message."
2237 (if score ; scoring mode
2238 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2240 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2241 (match-string 1 header)
2243 ;; spam detection mode
2244 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2245 (spam-split-group (if spam-split-symbolic-return
2248 (when header ; return nil when no header
2249 (when (string-match spam-spamassassin-positive-spam-flag-header
2251 spam-split-group)))))
2253 (defun spam-check-spamassassin (&optional score)
2254 "Check the SpamAssassin backend for the classification of this message."
2255 (let ((article-buffer-name (buffer-name)))
2257 (let ((temp-buffer-name (buffer-name)))
2259 (set-buffer article-buffer-name)
2260 (apply 'call-process-region
2261 (point-min) (point-max) spam-spamassassin-path
2262 nil temp-buffer-name nil spam-spamassassin-arguments))
2263 ;; check the return now (we're back in the temp buffer)
2264 (goto-char (point-min))
2265 (spam-check-spamassassin-headers score)))))
2267 ;; return something sensible if the score can't be determined
2268 (defun spam-spamassassin-score (&optional recheck)
2269 "Get the SpamAssassin score"
2271 (save-window-excursion
2272 (gnus-summary-show-article t)
2273 (set-buffer gnus-article-buffer)
2274 (let ((score (or (unless recheck
2275 (spam-check-spamassassin-headers t))
2276 (spam-check-spamassassin t))))
2277 (gnus-summary-show-article)
2278 (message "SpamAssassin score %s" score)
2281 (defun spam-spamassassin-register-with-sa-learn (articles spam
2282 &optional unregister)
2283 "Register articles with spamassassin's sa-learn as spam or non-spam."
2285 (let ((action (if unregister spam-sa-learn-unregister-switch
2286 (if spam spam-sa-learn-spam-switch
2287 spam-sa-learn-ham-switch)))
2288 (summary-buffer-name (buffer-name)))
2290 ;; group the articles into mbox format
2291 (dolist (article articles)
2292 (let (article-string)
2294 (set-buffer summary-buffer-name)
2295 (setq article-string (spam-get-article-as-string article)))
2296 (when (stringp article-string)
2297 (insert "From \n") ; mbox separator (sa-learn only checks the
2298 ; first five chars, so we can get away with
2300 (insert article-string)
2302 ;; call sa-learn on all messages at the same time
2303 (apply 'call-process-region
2304 (point-min) (point-max)
2306 nil nil nil "--mbox"
2307 (if spam-sa-learn-rebuild
2309 `("--no-rebuild" ,action)))))))
2311 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2312 (spam-spamassassin-register-with-sa-learn articles t unregister))
2314 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2315 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2317 (defun spam-spamassassin-unregister-spam-routine (articles)
2318 (spam-spamassassin-register-with-sa-learn articles t t))
2320 (defun spam-spamassassin-unregister-ham-routine (articles)
2321 (spam-spamassassin-register-with-sa-learn articles nil t))
2325 ;;; based mostly on the bogofilter code
2326 (defun spam-check-bsfilter-headers (&optional score)
2328 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2330 (let ((header (nnmail-fetch-field spam-bsfilter-header))
2331 (spam-split-group (if spam-split-symbolic-return
2334 (when header ; return nil when no header
2335 (when (string-match "YES" header)
2336 spam-split-group)))))
2338 ;; return something sensible if the score can't be determined
2339 (defun spam-bsfilter-score (&optional recheck)
2340 "Get the Bsfilter spamicity score"
2342 (save-window-excursion
2343 (gnus-summary-show-article t)
2344 (set-buffer gnus-article-buffer)
2345 (let ((score (or (unless recheck
2346 (spam-check-bsfilter-headers t))
2347 (spam-check-bsfilter t))))
2348 (gnus-summary-show-article)
2349 (message "Spamicity score %s" score)
2352 (defun spam-check-bsfilter (&optional score)
2353 "Check the Bsfilter backend for the classification of this message"
2354 (let ((article-buffer-name (buffer-name))
2355 (dir spam-bsfilter-database-directory)
2358 (let ((temp-buffer-name (buffer-name)))
2360 (set-buffer article-buffer-name)
2361 (apply 'call-process-region
2362 (point-min) (point-max)
2364 nil temp-buffer-name nil
2367 "--insert-probability"
2369 (list "--homedir" dir))))
2370 (setq return (spam-check-bsfilter-headers score))))
2373 (defun spam-bsfilter-register-with-bsfilter (articles
2375 &optional unregister)
2376 "Register an article, given as a string, as spam or non-spam."
2377 (dolist (article articles)
2378 (let ((article-string (spam-get-article-as-string article))
2379 (switch (if unregister
2381 spam-bsfilter-spam-strong-switch
2382 spam-bsfilter-ham-strong-switch)
2384 spam-bsfilter-spam-switch
2385 spam-bsfilter-ham-switch))))
2386 (when (stringp article-string)
2388 (insert article-string)
2389 (apply 'call-process-region
2390 (point-min) (point-max)
2394 (when spam-bsfilter-database-directory
2396 spam-bsfilter-database-directory))))))))
2398 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2399 (spam-bsfilter-register-with-bsfilter articles t unregister))
2401 (defun spam-bsfilter-unregister-spam-routine (articles)
2402 (spam-bsfilter-register-spam-routine articles t))
2404 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2405 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2407 (defun spam-bsfilter-unregister-ham-routine (articles)
2408 (spam-bsfilter-register-ham-routine articles t))
2414 (defun spam-initialize (&rest symbols)
2415 "Install the spam.el hooks and do other initialization.
2416 When SYMBOLS is given, set those variables to t. This is so you
2417 can call spam-initialize before you set spam-use-* variables on
2418 explicitly, and matters only if you need the extra headers
2419 installed through spam-necessary-extra-headers."
2422 (dolist (var symbols)
2425 (dolist (header (spam-necessary-extra-headers))
2426 (add-to-list 'nnmail-extra-headers header)
2427 (add-to-list 'gnus-extra-headers header))
2429 (setq spam-install-hooks t)
2430 ;; TODO: How do we redo this every time spam-face is customized?
2431 (push '((eq mark gnus-spam-mark) . spam-face)
2432 gnus-summary-highlight)
2433 ;; Add hooks for loading and saving the spam stats
2434 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2435 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2436 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2437 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2438 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2439 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2440 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2442 (defun spam-unload-hook ()
2443 "Uninstall the spam.el hooks"
2445 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2446 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2447 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2448 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2449 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2450 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2451 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2453 (when spam-install-hooks
2458 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
2459 ;;; spam.el ends here