1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;;; This module addresses a few aspects of spam control under Gnus. Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
30 ;;; The integration with Gnus is not yet complete. See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
33 ;;; Several TODO items are marked as such
35 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting,
36 ;; remote processing, training through files
40 (eval-when-compile (require 'cl))
44 (require 'gnus-uu) ; because of key prefix issues
45 ;;; for the definitions of group content classification and spam processors
47 (require 'message) ;for the message-fetch-field functions
49 ;; for nnimap-split-download-body-default
50 (eval-when-compile (require 'nnimap))
54 (autoload 'query-dig "dig"))
56 ;; autoload spam-report
58 (autoload 'spam-report-gmane "spam-report"))
60 ;; autoload gnus-registry
62 (autoload 'gnus-registry-group-count "gnus-registry")
63 (autoload 'gnus-registry-add-group "gnus-registry")
64 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
65 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
69 (autoload 'query-dns "dns"))
74 "Spam configuration.")
76 (defcustom spam-directory "~/News/spam/"
77 "Directory for spam whitelists and blacklists."
81 (defcustom spam-move-spam-nonspam-groups-only t
82 "Whether spam should be moved in non-spam groups only.
83 When t, only ham and unclassified groups will have their spam moved
84 to the spam-process-destination. When nil, spam will also be moved from
89 (defcustom spam-process-ham-in-nonham-groups nil
90 "Whether ham should be processed in non-ham groups."
94 (defcustom spam-log-to-registry nil
95 "Whether spam/ham processing should be logged in the registry."
99 (defcustom spam-split-symbolic-return nil
100 "Whether `spam-split' should work with symbols or group names."
104 (defcustom spam-split-symbolic-return-positive nil
105 "Whether `spam-split' should ALWAYS work with symbols or group names.
106 Do not set this if you use `spam-split' in a fancy split
111 (defcustom spam-process-ham-in-spam-groups nil
112 "Whether ham should be processed in spam groups."
116 (defcustom spam-mark-only-unseen-as-spam t
117 "Whether only unseen articles should be marked as spam in spam groups.
118 When nil, all unread articles in a spam group are marked as
119 spam. Set this if you want to leave an article unread in a spam group
120 without losing it to the automatic spam-marking process."
124 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
125 "Whether ham should be marked unread before it's moved.
126 The article is moved out of a spam group according to ham-process-destination.
127 This variable is an official entry in the international Longest Variable Name
132 (defcustom spam-disable-spam-split-during-ham-respool nil
133 "Whether `spam-split' should be ignored while resplitting ham in a process
134 destination. This is useful to prevent ham from ending up in the same spam
135 group after the resplit. Don't set this to t if you have spam-split as the
136 last rule in your split configuration."
140 (defcustom spam-autodetect-recheck-messages nil
141 "Should spam.el recheck all meessages when autodetecting?
142 Normally this is nil, so only unseen messages will be checked."
146 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
147 "The location of the whitelist.
148 The file format is one regular expression per line.
149 The regular expression is matched against the address."
153 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
154 "The location of the blacklist.
155 The file format is one regular expression per line.
156 The regular expression is matched against the address."
160 (defcustom spam-use-dig t
161 "Whether `query-dig' should be used instead of `query-dns'."
165 (defcustom spam-use-blacklist nil
166 "Whether the blacklist should be used by `spam-split'."
170 (defcustom spam-blacklist-ignored-regexes nil
171 "Regular expressions that the blacklist should ignore."
172 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
175 (defcustom spam-use-whitelist nil
176 "Whether the whitelist should be used by `spam-split'."
180 (defcustom spam-use-whitelist-exclusive nil
181 "Whether whitelist-exclusive should be used by `spam-split'.
182 Exclusive whitelisting means that all messages from senders not in the whitelist
183 are considered spam."
187 (defcustom spam-use-blackholes nil
188 "Whether blackholes should be used by `spam-split'."
192 (defcustom spam-use-hashcash nil
193 "Whether hashcash payments should be detected by `spam-split'."
197 (defcustom spam-use-regex-headers nil
198 "Whether a header regular expression match should be used by `spam-split'.
199 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
203 (defcustom spam-use-regex-body nil
204 "Whether a body regular expression match should be used by `spam-split'.
205 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
209 (defcustom spam-use-bogofilter-headers nil
210 "Whether bogofilter headers should be used by `spam-split'.
211 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
215 (defcustom spam-use-bogofilter nil
216 "Whether bogofilter should be invoked by `spam-split'.
217 Enable this if you want Gnus to invoke Bogofilter on new messages."
221 (defcustom spam-use-BBDB nil
222 "Whether BBDB should be used by `spam-split'."
226 (defcustom spam-use-BBDB-exclusive nil
227 "Whether BBDB-exclusive should be used by `spam-split'.
228 Exclusive BBDB means that all messages from senders not in the BBDB are
233 (defcustom spam-use-ifile nil
234 "Whether ifile should be used by `spam-split'."
238 (defcustom spam-use-stat nil
239 "Whether `spam-stat' should be used by `spam-split'."
243 (defcustom spam-use-spamoracle nil
244 "Whether spamoracle should be used by `spam-split'."
248 (defcustom spam-install-hooks (or
252 spam-use-whitelist-exclusive
255 spam-use-regex-headers
257 spam-use-bogofilter-headers
260 spam-use-BBDB-exclusive
264 "Whether the spam hooks should be installed.
265 Default to t if one of the spam-use-* variables is set."
269 (defcustom spam-split-group "spam"
270 "Group name where incoming spam should be put by `spam-split'."
274 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
275 ;;; not regular expressions
276 (defcustom spam-junk-mailgroups (cons
278 '("mail.junk" "poste.pourriel"))
279 "Mailgroups with spam contents.
280 All unmarked article in such group receive the spam mark on group entry."
281 :type '(repeat (string :tag "Group"))
284 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
285 "dev.null.dk" "relays.visi.com")
286 "List of blackhole servers."
287 :type '(repeat (string :tag "Server"))
290 (defcustom spam-blackhole-good-server-regex nil
291 "String matching IP addresses that should not be checked in the blackholes."
292 :type '(radio (const nil)
293 (regexp :format "%t: %v\n" :size 0))
296 (defcustom spam-face 'gnus-splash-face
297 "Face for spam-marked articles."
301 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
302 "Regular expression for positive header spam matches."
303 :type '(repeat (regexp :tag "Regular expression to match spam header"))
306 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
307 "Regular expression for positive header ham matches."
308 :type '(repeat (regexp :tag "Regular expression to match ham header"))
311 (defcustom spam-regex-body-spam '()
312 "Regular expression for positive body spam matches."
313 :type '(repeat (regexp :tag "Regular expression to match spam body"))
316 (defcustom spam-regex-body-ham '()
317 "Regular expression for positive body ham matches."
318 :type '(repeat (regexp :tag "Regular expression to match ham body"))
321 (defgroup spam-ifile nil
322 "Spam ifile configuration."
325 (defcustom spam-ifile-path (executable-find "ifile")
326 "File path of the ifile executable program."
327 :type '(choice (file :tag "Location of ifile")
328 (const :tag "ifile is not installed"))
331 (defcustom spam-ifile-database-path nil
332 "File path of the ifile database."
333 :type '(choice (file :tag "Location of the ifile database")
334 (const :tag "Use the default"))
337 (defcustom spam-ifile-spam-category "spam"
338 "Name of the spam ifile category."
342 (defcustom spam-ifile-ham-category nil
343 "Name of the ham ifile category.
344 If nil, the current group name will be used."
345 :type '(choice (string :tag "Use a fixed category")
346 (const :tag "Use the current group name"))
349 (defcustom spam-ifile-all-categories nil
350 "Whether the ifile check will return all categories, or just spam.
351 Set this to t if you want to use the `spam-split' invocation of ifile as
352 your main source of newsgroup names."
356 (defgroup spam-bogofilter nil
357 "Spam bogofilter configuration."
360 (defcustom spam-bogofilter-path (executable-find "bogofilter")
361 "File path of the Bogofilter executable program."
362 :type '(choice (file :tag "Location of bogofilter")
363 (const :tag "Bogofilter is not installed"))
364 :group 'spam-bogofilter)
366 (defcustom spam-bogofilter-header "X-Bogosity"
367 "The header that Bogofilter inserts in messages."
369 :group 'spam-bogofilter)
371 (defcustom spam-bogofilter-spam-switch "-s"
372 "The switch that Bogofilter uses to register spam messages."
374 :group 'spam-bogofilter)
376 (defcustom spam-bogofilter-ham-switch "-n"
377 "The switch that Bogofilter uses to register ham messages."
379 :group 'spam-bogofilter)
381 (defcustom spam-bogofilter-spam-strong-switch "-S"
382 "The switch that Bogofilter uses to unregister ham messages."
384 :group 'spam-bogofilter)
386 (defcustom spam-bogofilter-ham-strong-switch "-N"
387 "The switch that Bogofilter uses to unregister spam messages."
389 :group 'spam-bogofilter)
391 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
392 "The regex on `spam-bogofilter-header' for positive spam identification."
394 :group 'spam-bogofilter)
396 (defcustom spam-bogofilter-database-directory nil
397 "Directory path of the Bogofilter databases."
398 :type '(choice (directory
399 :tag "Location of the Bogofilter database directory")
400 (const :tag "Use the default"))
401 :group 'spam-bogofilter)
403 (defgroup spam-spamoracle nil
404 "Spam spamoracle configuration."
407 (defcustom spam-spamoracle-database nil
408 "Location of spamoracle database file. When nil, use the default
409 spamoracle database."
410 :type '(choice (directory :tag "Location of spamoracle database file.")
411 (const :tag "Use the default"))
412 :group 'spam-spamoracle)
414 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
415 "Location of the spamoracle binary."
416 :type '(choice (directory :tag "Location of the spamoracle binary")
417 (const :tag "Use the default"))
418 :group 'spam-spamoracle)
420 ;;; Key bindings for spam control.
422 (gnus-define-keys gnus-summary-mode-map
423 "St" spam-bogofilter-score
424 "Sx" gnus-summary-mark-as-spam
425 "Mst" spam-bogofilter-score
426 "Msx" gnus-summary-mark-as-spam
427 "\M-d" gnus-summary-mark-as-spam)
429 (defvar spam-cache-lookups nil
430 "Whether spam.el will try to cache lookups using spam-caches.")
432 (defvar spam-caches (make-hash-table
435 "Cache of spam detection entries.")
437 (defvar spam-old-ham-articles nil
438 "List of old ham articles, generated when a group is entered.")
440 (defvar spam-old-spam-articles nil
441 "List of old spam articles, generated when a group is entered.")
443 (defvar spam-split-disabled nil
444 "If non-nil, `spam-split' is disabled, and always returns nil.")
446 (defvar spam-split-last-successful-check nil
447 "`spam-split' will set this to nil or a spam-use-XYZ check if it
450 ;; convenience functions
451 (defun spam-clear-cache (symbol)
452 (remhash symbol spam-caches))
454 (defun spam-xor (a b)
455 "Logical exclusive `or'."
456 (and (or a b) (not (and a b))))
458 (defun spam-group-ham-mark-p (group mark &optional spam)
459 (when (stringp group)
460 (let* ((marks (spam-group-ham-marks group spam))
461 (marks (if (symbolp mark)
463 (mapcar 'symbol-value marks))))
466 (defun spam-group-spam-mark-p (group mark)
467 (spam-group-ham-mark-p group mark t))
469 (defun spam-group-ham-marks (group &optional spam)
470 (when (stringp group)
471 (let* ((marks (if spam
472 (gnus-parameter-spam-marks group)
473 (gnus-parameter-ham-marks group)))
475 (marks (if (listp (car marks)) (car marks) marks)))
478 (defun spam-group-spam-marks (group)
479 (spam-group-ham-marks group t))
481 (defun spam-group-spam-contents-p (group)
483 (or (member group spam-junk-mailgroups)
484 (memq 'gnus-group-spam-classification-spam
485 (gnus-parameter-spam-contents group)))
488 (defun spam-group-ham-contents-p (group)
490 (memq 'gnus-group-spam-classification-ham
491 (gnus-parameter-spam-contents group))
494 (defvar spam-list-of-processors
495 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
496 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
497 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
498 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
499 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
500 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
501 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
502 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
503 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
504 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
505 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
506 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
507 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
508 "The spam-list-of-processors list contains pairs associating a
509 ham/spam exit processor variable with a classification and a
510 spam-use-* variable.")
512 (defun spam-group-processor-p (group processor)
513 (if (and (stringp group)
515 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
516 (spam-group-processor-multiple-p
518 (cdr-safe (assoc processor spam-list-of-processors))))
521 (defun spam-group-processor-multiple-p (group processor-info)
522 (let* ((classification (nth 0 processor-info))
523 (check (nth 1 processor-info))
524 (parameters (nth 0 (gnus-parameter-spam-process group)))
526 (dolist (parameter parameters)
527 (when (and (null found)
529 (eq classification (nth 0 parameter))
530 (eq check (nth 1 parameter)))
534 (defun spam-group-spam-processor-report-gmane-p (group)
535 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
537 (defun spam-group-spam-processor-bogofilter-p (group)
538 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
540 (defun spam-group-spam-processor-blacklist-p (group)
541 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
543 (defun spam-group-spam-processor-ifile-p (group)
544 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
546 (defun spam-group-ham-processor-ifile-p (group)
547 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
549 (defun spam-group-spam-processor-spamoracle-p (group)
550 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
552 (defun spam-group-ham-processor-bogofilter-p (group)
553 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
555 (defun spam-group-spam-processor-stat-p (group)
556 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
558 (defun spam-group-ham-processor-stat-p (group)
559 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
561 (defun spam-group-ham-processor-whitelist-p (group)
562 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
564 (defun spam-group-ham-processor-BBDB-p (group)
565 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
567 (defun spam-group-ham-processor-copy-p (group)
568 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
570 (defun spam-group-ham-processor-spamoracle-p (group)
571 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
573 ;;; Summary entry and exit processing.
575 (defun spam-summary-prepare ()
576 (setq spam-old-ham-articles
577 (spam-list-articles gnus-newsgroup-articles 'ham))
578 (setq spam-old-spam-articles
579 (spam-list-articles gnus-newsgroup-articles 'spam))
580 (spam-mark-junk-as-spam-routine))
582 ;; The spam processors are invoked for any group, spam or ham or neither
583 (defun spam-summary-prepare-exit ()
584 (unless gnus-group-is-exiting-without-update-p
585 (gnus-message 6 "Exiting summary buffer and applying spam rules")
587 ;; first of all, unregister any articles that are no longer ham or spam
588 ;; we have to iterate over the processors, or else we'll be too slow
589 (dolist (classification '(spam ham))
590 (let* ((old-articles (if (eq classification 'spam)
591 spam-old-spam-articles
592 spam-old-ham-articles))
593 (new-articles (spam-list-articles
594 gnus-newsgroup-articles
596 (changed-articles (gnus-set-difference old-articles new-articles)))
597 ;; now that we have the changed articles, we go through the processors
598 (dolist (processor-param spam-list-of-processors)
599 (let ((processor (nth 0 processor-param))
600 (processor-classification (nth 1 processor-param))
601 (check (nth 2 processor-param))
603 (dolist (article changed-articles)
604 (let ((id (spam-fetch-field-message-id-fast article)))
605 (when (spam-log-unregistration-needed-p
606 id 'process classification check)
607 (push article unregister-list))))
608 ;; call spam-register-routine with specific articles to unregister,
609 ;; when there are articles to unregister and the check is enabled
610 (when (and unregister-list (symbol-value check))
611 (spam-register-routine classification check t unregister-list))))))
613 ;; find all the spam processors applicable to this group
614 (dolist (processor-param spam-list-of-processors)
615 (let ((processor (nth 0 processor-param))
616 (classification (nth 1 processor-param))
617 (check (nth 2 processor-param)))
618 (when (and (eq 'spam classification)
619 (spam-group-processor-p gnus-newsgroup-name processor))
620 (spam-register-routine classification check))))
622 (if spam-move-spam-nonspam-groups-only
623 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
624 (spam-mark-spam-as-expired-and-move-routine
625 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
626 (gnus-message 5 "Marking spam as expired and moving it to %s"
628 (spam-mark-spam-as-expired-and-move-routine
629 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
631 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
632 ;; expire spam, in case the above did not expire them
633 (gnus-message 5 "Marking spam as expired without moving it")
634 (spam-mark-spam-as-expired-and-move-routine nil)
636 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
637 (and (spam-group-spam-contents-p gnus-newsgroup-name)
638 spam-process-ham-in-spam-groups)
639 spam-process-ham-in-nonham-groups)
640 ;; find all the ham processors applicable to this group
641 (dolist (processor-param spam-list-of-processors)
642 (let ((processor (nth 0 processor-param))
643 (classification (nth 1 processor-param))
644 (check (nth 2 processor-param)))
645 (when (and (eq 'ham classification)
646 (spam-group-processor-p gnus-newsgroup-name processor))
647 (spam-register-routine classification check)))))
649 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
650 (gnus-message 5 "Copying ham")
651 (spam-ham-copy-routine
652 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
654 ;; now move all ham articles out of spam groups
655 (when (spam-group-spam-contents-p gnus-newsgroup-name)
656 (gnus-message 5 "Moving ham messages from spam group")
657 (spam-ham-move-routine
658 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
660 (setq spam-old-ham-articles nil)
661 (setq spam-old-spam-articles nil))
663 (defun spam-mark-junk-as-spam-routine ()
664 ;; check the global list of group names spam-junk-mailgroups and the
666 (when (spam-group-spam-contents-p gnus-newsgroup-name)
667 (gnus-message 5 "Marking %s articles as spam"
668 (if spam-mark-only-unseen-as-spam
671 (let ((articles (if spam-mark-only-unseen-as-spam
672 gnus-newsgroup-unseen
673 gnus-newsgroup-unreads)))
674 (dolist (article articles)
675 (gnus-summary-mark-article article gnus-spam-mark)))))
677 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
678 (if (and (car-safe groups) (listp (car-safe groups)))
679 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
680 (gnus-summary-kill-process-mark)
681 (let ((articles gnus-newsgroup-articles)
682 (backend-supports-deletions
683 (gnus-check-backend-function
684 'request-move-article gnus-newsgroup-name))
685 article tomove deletep)
686 (dolist (article articles)
687 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
688 (gnus-summary-mark-article article gnus-expirable-mark)
689 (push article tomove)))
691 ;; now do the actual copies
692 (dolist (group groups)
695 (dolist (article tomove)
696 (gnus-summary-set-process-mark article))
698 (if (or (not backend-supports-deletions)
699 (> (length groups) 1))
701 (gnus-summary-copy-article nil group)
703 (gnus-summary-move-article nil group)))))
705 ;; now delete the articles, if there was a copy done, and the
707 (when (and deletep backend-supports-deletions)
708 (dolist (article tomove)
709 (gnus-summary-set-process-mark article))
711 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
712 (gnus-summary-delete-article nil))))
714 (gnus-summary-yank-process-mark))))
716 (defun spam-ham-copy-or-move-routine (copy groups)
717 (gnus-summary-kill-process-mark)
718 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
719 (backend-supports-deletions
720 (gnus-check-backend-function
721 'request-move-article gnus-newsgroup-name))
722 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
723 article mark todo deletep respool)
725 (when (member 'respool groups)
726 (setq respool t) ; boolean for later
727 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
729 ;; now do the actual move
730 (dolist (group groups)
731 (when (and todo (stringp group))
732 (dolist (article todo)
733 (when spam-mark-ham-unread-before-move-from-spam-group
734 (gnus-summary-mark-article article gnus-unread-mark))
735 (gnus-summary-set-process-mark article))
737 (if respool ; respooling is with a "fake" group
738 (let ((spam-split-disabled
739 (or spam-split-disabled
740 spam-disable-spam-split-during-ham-respool)))
741 (gnus-summary-respool-article nil respool-method))
742 (if (or (not backend-supports-deletions) ; else, we are not respooling
743 (> (length groups) 1))
744 (progn ; if copying, copy and set deletep
745 (gnus-summary-copy-article nil group)
747 (gnus-summary-move-article nil group))))) ; else move articles
749 ;; now delete the articles, unless a) copy is t, and there was a copy done
750 ;; b) a move was done to a single group
751 ;; c) backend-supports-deletions is nil
753 (when (and deletep backend-supports-deletions)
754 (dolist (article todo)
755 (gnus-summary-set-process-mark article))
757 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
758 (gnus-summary-delete-article nil))))))
760 (gnus-summary-yank-process-mark))
762 (defun spam-ham-copy-routine (&rest groups)
763 (if (and (car-safe groups) (listp (car-safe groups)))
764 (apply 'spam-ham-copy-routine (car groups))
765 (spam-ham-copy-or-move-routine t groups)))
767 (defun spam-ham-move-routine (&rest groups)
768 (if (and (car-safe groups) (listp (car-safe groups)))
769 (apply 'spam-ham-move-routine (car groups))
770 (spam-ham-copy-or-move-routine nil groups)))
772 (defun spam-get-article-as-string (article)
773 (let ((article-buffer (spam-get-article-as-buffer article))
776 (save-window-excursion
777 (set-buffer article-buffer)
778 (setq article-string (buffer-string))))
781 (defun spam-get-article-as-buffer (article)
782 (let ((article-buffer))
783 (when (numberp article)
784 (save-window-excursion
785 (gnus-summary-goto-subject article)
786 (gnus-summary-show-article t)
787 (setq article-buffer (get-buffer gnus-article-buffer))))
791 ;; (defun spam-get-article-as-filename (article)
792 ;; (let ((article-filename))
793 ;; (when (numberp article)
794 ;; (nnml-possibly-change-directory
795 ;; (gnus-group-real-name gnus-newsgroup-name))
796 ;; (setq article-filename (expand-file-name
797 ;; (int-to-string article) nnml-current-directory)))
798 ;; (if (file-exists-p article-filename)
802 (defun spam-fetch-field-from-fast (article)
803 "Fetch the `from' field quickly, using the internal gnus-data-list function"
804 (if (and (numberp article)
805 (assoc article (gnus-data-list nil)))
807 (gnus-data-header (assoc article (gnus-data-list nil))))
810 (defun spam-fetch-field-subject-fast (article)
811 "Fetch the `subject' field quickly, using the internal
812 gnus-data-list function"
813 (if (and (numberp article)
814 (assoc article (gnus-data-list nil)))
816 (gnus-data-header (assoc article (gnus-data-list nil))))
819 (defun spam-fetch-field-message-id-fast (article)
820 "Fetch the `Message-ID' field quickly, using the internal
821 gnus-data-list function"
822 (if (and (numberp article)
823 (assoc article (gnus-data-list nil)))
824 (mail-header-message-id
825 (gnus-data-header (assoc article (gnus-data-list nil))))
829 ;;;; Spam determination.
831 (defvar spam-list-of-checks
832 '((spam-use-blacklist . spam-check-blacklist)
833 (spam-use-regex-headers . spam-check-regex-headers)
834 (spam-use-regex-body . spam-check-regex-body)
835 (spam-use-whitelist . spam-check-whitelist)
836 (spam-use-BBDB . spam-check-BBDB)
837 (spam-use-ifile . spam-check-ifile)
838 (spam-use-spamoracle . spam-check-spamoracle)
839 (spam-use-stat . spam-check-stat)
840 (spam-use-blackholes . spam-check-blackholes)
841 (spam-use-hashcash . spam-check-hashcash)
842 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
843 (spam-use-bogofilter . spam-check-bogofilter))
844 "The spam-list-of-checks list contains pairs associating a
845 parameter variable with a spam checking function. If the
846 parameter variable is true, then the checking function is called,
847 and its value decides what happens. Each individual check may
848 return nil, t, or a mailgroup name. The value nil means that the
849 check does not yield a decision, and so, that further checks are
850 needed. The value t means that the message is definitely not
851 spam, and that further spam checks should be inhibited.
852 Otherwise, a mailgroup name or the symbol 'spam (depending on
853 spam-split-symbolic-return) is returned where the mail should go,
854 and further checks are also inhibited. The usual mailgroup name
855 is the value of `spam-split-group', meaning that the message is
858 (defvar spam-list-of-statistical-checks
864 "The spam-list-of-statistical-checks list contains all the mail
865 splitters that need to have the full message body available.")
867 ;;;TODO: modify to invoke self with each check if invoked without specifics
868 (defun spam-split (&rest specific-checks)
869 "Split this message into the `spam' group if it is spam.
870 This function can be used as an entry in the variable `nnmail-split-fancy',
871 for example like this: (: spam-split). It can take checks as
872 parameters. A string as a parameter will set the
873 spam-split-group to that string.
875 See the Info node `(gnus)Fancy Mail Splitting' for more details."
877 (setq spam-split-last-successful-check nil)
878 (unless spam-split-disabled
879 (let ((spam-split-group-choice spam-split-group))
880 (dolist (check specific-checks)
881 (when (stringp check)
882 (setq spam-split-group-choice check)
883 (setq specific-checks (delq check specific-checks))))
885 (let ((spam-split-group spam-split-group-choice))
888 (dolist (check spam-list-of-statistical-checks)
889 (when (and (symbolp check) (symbol-value check))
891 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
894 ;; (progn (widen) (debug (buffer-string)))
895 (let ((list-of-checks spam-list-of-checks)
897 (while (and list-of-checks (not decision))
898 (let ((pair (pop list-of-checks)))
899 (when (and (symbol-value (car pair))
900 (or (null specific-checks)
901 (memq (car pair) specific-checks)))
902 (gnus-message 5 "spam-split: calling the %s function"
903 (symbol-name (cdr pair)))
904 (setq decision (funcall (cdr pair)))
905 ;; if we got a decision at all, save the current check
907 (setq spam-split-last-successful-check (car pair)))
909 (when (eq decision 'spam)
910 (if spam-split-symbolic-return
911 (setq decision spam-split-group)
914 (format "spam-split got %s but %s is nil"
915 (symbol-name decision)
916 (symbol-name spam-split-symbolic-return))))))))
918 (if spam-split-symbolic-return-positive 'ham nil)
921 (defun spam-find-spam ()
922 "This function will detect spam in the current newsgroup using spam-split."
925 (let* ((group gnus-newsgroup-name)
926 (autodetect (gnus-parameter-spam-autodetect group))
927 (methods (gnus-parameter-spam-autodetect-methods group))
928 (first-method (nth 0 methods))
929 (articles (if spam-autodetect-recheck-messages
930 gnus-newsgroup-articles
931 gnus-newsgroup-unseen))
932 (spam-cache-lookups (< 2 (length articles))))
934 (when (and autodetect
935 (not (equal first-method 'none)))
938 (let ((id (spam-fetch-field-message-id-fast article))
939 (subject (spam-fetch-field-subject-fast article))
940 (sender (spam-fetch-field-from-fast article)))
941 (unless (and spam-log-to-registry
942 (spam-log-registered-p id 'incoming))
943 (let* ((spam-split-symbolic-return t)
944 (spam-split-symbolic-return-positive t)
947 (gnus-request-article-this-buffer
950 (if (or (null first-method)
951 (equal first-method 'default))
953 (apply 'spam-split methods)))))
954 (if (equal split-return 'spam)
955 (gnus-summary-mark-article article gnus-spam-mark))
957 (when (and split-return spam-log-to-registry)
958 (when (zerop (gnus-registry-group-count id))
959 (gnus-registry-add-group
960 id group subject sender))
962 (spam-log-processing-to-registry
966 spam-split-last-successful-check
970 (defvar spam-registration-functions
971 ;; first the ham register, second the spam register function
972 ;; third the ham unregister, fourth the spam unregister function
973 '((spam-use-blacklist nil
974 spam-blacklist-register-routine
976 spam-blacklist-unregister-routine)
977 (spam-use-whitelist spam-whitelist-register-routine
979 spam-whitelist-unregister-routine
981 (spam-use-BBDB spam-BBDB-register-routine
983 spam-BBDB-unregister-routine
985 (spam-use-ifile spam-ifile-register-ham-routine
986 spam-ifile-register-spam-routine
987 spam-ifile-unregister-ham-routine
988 spam-ifile-unregister-spam-routine)
989 (spam-use-spamoracle spam-spamoracle-learn-ham
990 spam-spamoracle-learn-spam
991 spam-spamoracle-unlearn-ham
992 spam-spamoracle-unlearn-spam)
993 (spam-use-stat spam-stat-register-ham-routine
994 spam-stat-register-spam-routine
995 spam-stat-unregister-ham-routine
996 spam-stat-unregister-spam-routine)
997 ;; note that spam-use-gmane is not a legitimate check
999 spam-report-gmane-register-routine
1000 ;; does Gmane support unregistration?
1003 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1004 spam-bogofilter-register-spam-routine
1005 spam-bogofilter-unregister-ham-routine
1006 spam-bogofilter-unregister-spam-routine))
1007 "The spam-registration-functions list contains pairs
1008 associating a parameter variable with the ham and spam
1009 registration functions, and the ham and spam unregistration
1012 (defun spam-classification-valid-p (classification)
1013 (or (eq classification 'spam)
1014 (eq classification 'ham)))
1016 (defun spam-process-type-valid-p (process-type)
1017 (or (eq process-type 'incoming)
1018 (eq process-type 'process)))
1020 (defun spam-registration-check-valid-p (check)
1021 (assoc check spam-registration-functions))
1023 (defun spam-unregistration-check-valid-p (check)
1024 (assoc check spam-registration-functions))
1026 (defun spam-registration-function (classification check)
1027 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1028 (if (eq classification 'spam)
1032 (defun spam-unregistration-function (classification check)
1033 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1034 (if (eq classification 'spam)
1038 (defun spam-list-articles (articles classification)
1039 (let ((mark-check (if (eq classification 'spam)
1040 'spam-group-spam-mark-p
1041 'spam-group-ham-mark-p))
1042 list mark-cache-yes mark-cache-no)
1043 (dolist (article articles)
1044 (let ((mark (gnus-summary-article-mark article)))
1045 (unless (memq mark mark-cache-no)
1046 (if (memq mark mark-cache-yes)
1048 ;; else, we have to actually check the mark
1049 (if (funcall mark-check
1054 (push mark mark-cache-yes))
1055 (push mark mark-cache-no))))))
1058 (defun spam-register-routine (classification
1060 &optional unregister
1062 (when (and (spam-classification-valid-p classification)
1063 (spam-registration-check-valid-p check))
1064 (let* ((register-function
1065 (spam-registration-function classification check))
1066 (unregister-function
1067 (spam-unregistration-function classification check))
1068 (run-function (if unregister
1071 (log-function (if unregister
1072 'spam-log-undo-registration
1073 'spam-log-processing-to-registry))
1077 ;; make list of articles, using specific-articles if given
1078 (setq articles (or specific-articles
1080 gnus-newsgroup-articles
1083 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1084 (if unregister "Unregistering" "Registering")
1086 (if specific-articles "specific" "")
1087 (symbol-name classification)
1088 (symbol-name check))
1089 (funcall run-function articles)
1090 ;; now log all the registrations (or undo them, depending on unregister)
1091 (dolist (article articles)
1092 (funcall log-function
1093 (spam-fetch-field-message-id-fast article)
1097 gnus-newsgroup-name))))))
1099 ;;; log a ham- or spam-processor invocation to the registry
1100 (defun spam-log-processing-to-registry (id type classification check group)
1101 (when spam-log-to-registry
1102 (if (and (stringp id)
1104 (spam-process-type-valid-p type)
1105 (spam-classification-valid-p classification)
1106 (spam-registration-check-valid-p check))
1107 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1108 (cell (list classification check group)))
1109 (push cell cell-list)
1110 (gnus-registry-store-extra-entry
1115 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1116 "spam-log-processing-to-registry")))))
1118 ;;; check if a ham- or spam-processor registration has been done
1119 (defun spam-log-registered-p (id type)
1120 (when spam-log-to-registry
1121 (if (and (stringp id)
1122 (spam-process-type-valid-p type))
1123 (cdr-safe (gnus-registry-fetch-extra id type))
1125 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1126 "spam-log-registered-p"))
1129 ;;; check if a ham- or spam-processor registration needs to be undone
1130 (defun spam-log-unregistration-needed-p (id type classification check)
1131 (when spam-log-to-registry
1132 (if (and (stringp id)
1133 (spam-process-type-valid-p type)
1134 (spam-classification-valid-p classification)
1135 (spam-registration-check-valid-p check))
1136 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1138 (dolist (cell cell-list)
1140 (when (and (eq classification (nth 0 cell))
1141 (eq check (nth 1 cell)))
1145 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1146 "spam-log-unregistration-needed-p"))
1150 ;;; undo a ham- or spam-processor registration (the group is not used)
1151 (defun spam-log-undo-registration (id type classification check &optional group)
1152 (when (and spam-log-to-registry
1153 (spam-log-unregistration-needed-p id type classification check))
1154 (if (and (stringp id)
1155 (spam-process-type-valid-p type)
1156 (spam-classification-valid-p classification)
1157 (spam-registration-check-valid-p check))
1158 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1159 new-cell-list found)
1160 (dolist (cell cell-list)
1161 (unless (and (eq classification (nth 0 cell))
1162 (eq check (nth 1 cell)))
1163 (push cell new-cell-list)))
1164 (gnus-registry-store-extra-entry
1169 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1170 "spam-log-undo-registration"))
1173 ;;; set up IMAP widening if it's necessary
1174 (defun spam-setup-widening ()
1175 (dolist (check spam-list-of-statistical-checks)
1176 (when (symbol-value check)
1177 (setq nnimap-split-download-body-default t))))
1182 (defun spam-check-regex-body ()
1183 (let ((spam-regex-headers-ham spam-regex-body-ham)
1184 (spam-regex-headers-spam spam-regex-body-spam))
1185 (spam-check-regex-headers t)))
1190 (defun spam-check-regex-headers (&optional body)
1191 (let ((type (if body "body" "header"))
1192 (spam-split-group (if spam-split-symbolic-return
1196 (dolist (h-regex spam-regex-headers-ham)
1198 (goto-char (point-min))
1199 (when (re-search-forward h-regex nil t)
1200 (message "Ham regex %s search positive." type)
1202 (dolist (s-regex spam-regex-headers-spam)
1204 (goto-char (point-min))
1205 (when (re-search-forward s-regex nil t)
1206 (message "Spam regex %s search positive." type)
1208 (setq ret spam-split-group))))
1214 (defun spam-reverse-ip-string (ip)
1216 (mapconcat 'identity
1217 (nreverse (split-string ip "\\."))
1220 (defun spam-check-blackholes ()
1221 "Check the Received headers for blackholed relays."
1222 (let ((headers (nnmail-fetch-field "received"))
1223 (spam-split-group (if spam-split-symbolic-return
1230 (goto-char (point-min))
1231 (gnus-message 5 "Checking headers for relay addresses")
1232 (while (re-search-forward
1233 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1234 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1235 (push (spam-reverse-ip-string (match-string 1))
1237 (dolist (server spam-blackhole-servers)
1239 (unless (and spam-blackhole-good-server-regex
1240 ;; match the good-server-regex against the reversed (again) IP string
1242 spam-blackhole-good-server-regex
1243 (spam-reverse-ip-string ip)))
1245 (let ((query-string (concat ip "." server)))
1247 (let ((query-result (query-dig query-string)))
1249 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1251 (push (list ip server query-result)
1253 ;; else, if not using dig.el
1254 (when (query-dns query-string)
1255 (gnus-message 5 "positive blackhole check")
1256 (push (list ip server (query-dns query-string 'TXT))
1267 (defun spam-check-hashcash ()
1268 "Check the headers for hashcash payments."
1269 (mail-check-payment))) ;mail-check-payment returns a boolean
1272 (defalias 'mail-check-payment 'ignore)
1273 (defalias 'spam-check-hashcash 'ignore))))
1277 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1278 ;;; <sacha@giotto.sj.ru>
1280 ;; all this is done inside a condition-case to trap errors
1287 ;; when the BBDB changes, we want to clear out our cache
1288 (defun spam-clear-cache-BBDB (&rest immaterial)
1289 (spam-clear-cache 'spam-use-BBDB))
1291 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1293 (defun spam-enter-ham-BBDB (addresses &optional remove)
1294 "Enter an address into the BBDB; implies ham (non-spam) sender"
1295 (dolist (from addresses)
1296 (when (stringp from)
1297 (let* ((parsed-address (gnus-extract-address-components from))
1298 (name (or (nth 0 parsed-address) "Ham Sender"))
1299 (remove-function (if remove
1300 'bbdb-delete-record-internal
1302 (net-address (nth 1 parsed-address))
1303 (record (and net-address
1304 (bbdb-search-simple nil net-address))))
1306 (gnus-message 5 "%s address %s %s BBDB"
1307 (if remove "Deleting" "Adding")
1309 (if remove "from" "to"))
1311 (funcall remove-function record)
1312 (bbdb-create-internal name nil net-address nil nil
1313 "ham sender added by spam.el")))))))
1315 (defun spam-BBDB-register-routine (articles &optional unregister)
1317 (dolist (article articles)
1318 (when (stringp (spam-fetch-field-from-fast article))
1319 (push (spam-fetch-field-from-fast article) addresses)))
1320 ;; now do the register/unregister action
1321 (spam-enter-ham-BBDB addresses unregister)))
1323 (defun spam-BBDB-unregister-routine (articles)
1324 (spam-BBDB-register-routine articles t))
1326 (defun spam-check-BBDB ()
1327 "Mail from people in the BBDB is classified as ham or non-spam"
1328 (let ((who (nnmail-fetch-field "from"))
1329 (spam-split-group (if spam-split-symbolic-return
1334 (when spam-cache-lookups
1335 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1337 (setq bbdb-cache (bbdb-hashtable))
1338 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1341 (setq who (nth 1 (gnus-extract-address-components who)))
1343 (if spam-cache-lookups
1344 (bbdb-gethash who bbdb-cache)
1345 (bbdb-search-simple nil who))
1347 (if spam-use-BBDB-exclusive
1352 (defalias 'bbdb-search-simple 'ignore)
1353 (defalias 'bbdb-hashtable 'ignore)
1354 (defalias 'bbdb-gethash 'ignore)
1355 (defalias 'spam-check-BBDB 'ignore)
1356 (defalias 'spam-BBDB-register-routine 'ignore)
1357 (defalias 'spam-enter-ham-BBDB 'ignore)
1358 (defalias 'bbdb-create-internal 'ignore)
1359 (defalias 'bbdb-delete-record-internal 'ignore)
1360 (defalias 'bbdb-records 'ignore))))
1365 ;;; check the ifile backend; return nil if the mail was NOT classified
1368 (defun spam-get-ifile-database-parameter ()
1369 "Get the command-line parameter for ifile's database from
1370 spam-ifile-database-path."
1371 (if spam-ifile-database-path
1372 (format "--db-file=%s" spam-ifile-database-path)
1375 (defun spam-check-ifile ()
1376 "Check the ifile backend for the classification of this message."
1377 (let ((article-buffer-name (buffer-name))
1378 (spam-split-group (if spam-split-symbolic-return
1383 (let ((temp-buffer-name (buffer-name))
1384 (db-param (spam-get-ifile-database-parameter)))
1386 (set-buffer article-buffer-name)
1387 (apply 'call-process-region
1388 (point-min) (point-max) spam-ifile-path
1389 nil temp-buffer-name nil "-c"
1390 (if db-param `(,db-param "-q") `("-q"))))
1391 ;; check the return now (we're back in the temp buffer)
1392 (goto-char (point-min))
1394 (setq category (buffer-substring (point) (point-at-eol))))
1395 (when (not (zerop (length category))) ; we need a category here
1396 (if spam-ifile-all-categories
1397 (setq return category)
1398 ;; else, if spam-ifile-all-categories is not set...
1399 (when (string-equal spam-ifile-spam-category category)
1400 (setq return spam-split-group)))))) ; note return is nil otherwise
1403 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1404 "Register an article, given as a string, with a category.
1405 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1406 (let ((category (or category gnus-newsgroup-name))
1407 (add-or-delete-option (if unregister "-d" "-i"))
1408 (db (spam-get-ifile-database-parameter))
1411 (dolist (article articles)
1412 (let ((article-string (spam-get-article-as-string article)))
1413 (when (stringp article-string)
1414 (insert article-string))))
1415 (apply 'call-process-region
1416 (point-min) (point-max) spam-ifile-path
1418 add-or-delete-option category
1419 (if db `(,db "-h") `("-h"))))))
1421 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1422 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1424 (defun spam-ifile-unregister-spam-routine (articles)
1425 (spam-ifile-register-spam-routine articles t))
1427 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1428 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1430 (defun spam-ifile-unregister-ham-routine (articles)
1431 (spam-ifile-register-ham-routine articles t))
1438 (let ((spam-stat-install-hooks nil))
1439 (require 'spam-stat))
1441 (defun spam-check-stat ()
1442 "Check the spam-stat backend for the classification of this message"
1443 (let ((spam-split-group (if spam-split-symbolic-return
1446 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1447 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1449 (spam-stat-split-fancy)))
1451 (defun spam-stat-register-spam-routine (articles &optional unregister)
1452 (dolist (article articles)
1453 (let ((article-string (spam-get-article-as-string article)))
1455 (insert article-string)
1457 (spam-stat-buffer-change-to-non-spam)
1458 (spam-stat-buffer-is-spam))))))
1460 (defun spam-stat-unregister-spam-routine (articles)
1461 (spam-stat-register-spam-routine articles t))
1463 (defun spam-stat-register-ham-routine (articles &optional unregister)
1464 (dolist (article articles)
1465 (let ((article-string (spam-get-article-as-string article)))
1467 (insert article-string)
1469 (spam-stat-buffer-change-to-spam)
1470 (spam-stat-buffer-is-non-spam))))))
1472 (defun spam-stat-unregister-ham-routine (articles)
1473 (spam-stat-register-ham-routine articles t))
1475 (defun spam-maybe-spam-stat-load ()
1476 (when spam-use-stat (spam-stat-load)))
1478 (defun spam-maybe-spam-stat-save ()
1479 (when spam-use-stat (spam-stat-save))))
1482 (defalias 'spam-stat-load 'ignore)
1483 (defalias 'spam-stat-save 'ignore)
1484 (defalias 'spam-maybe-spam-stat-load 'ignore)
1485 (defalias 'spam-maybe-spam-stat-save 'ignore)
1486 (defalias 'spam-stat-register-ham-routine 'ignore)
1487 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1488 (defalias 'spam-stat-register-spam-routine 'ignore)
1489 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1490 (defalias 'spam-stat-buffer-is-spam 'ignore)
1491 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1492 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1493 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1494 (defalias 'spam-stat-split-fancy 'ignore)
1495 (defalias 'spam-check-stat 'ignore))))
1499 ;;;; Blacklists and whitelists.
1501 (defvar spam-whitelist-cache nil)
1502 (defvar spam-blacklist-cache nil)
1504 (defun spam-kill-whole-line ()
1506 (let ((kill-whole-line t))
1509 ;;; address can be a list, too
1510 (defun spam-enter-whitelist (address &optional remove)
1511 "Enter ADDRESS (list or single) into the whitelist.
1512 With a non-nil REMOVE, remove them."
1513 (interactive "sAddress: ")
1514 (spam-enter-list address spam-whitelist remove)
1515 (setq spam-whitelist-cache nil))
1517 ;;; address can be a list, too
1518 (defun spam-enter-blacklist (address &optional remove)
1519 "Enter ADDRESS (list or single) into the blacklist.
1520 With a non-nil REMOVE, remove them."
1521 (interactive "sAddress: ")
1522 (spam-enter-list address spam-blacklist remove)
1523 (setq spam-blacklist-cache nil))
1525 (defun spam-enter-list (addresses file &optional remove)
1526 "Enter ADDRESSES into the given FILE.
1527 Either the whitelist or the blacklist files can be used. With
1528 REMOVE not nil, remove the ADDRESSES."
1529 (if (stringp addresses)
1530 (spam-enter-list (list addresses) file remove)
1531 ;; else, we have a list of addresses here
1532 (unless (file-exists-p (file-name-directory file))
1533 (make-directory (file-name-directory file) t))
1536 (find-file-noselect file))
1537 (dolist (a addresses)
1539 (goto-char (point-min))
1540 (if (re-search-forward (regexp-quote a) nil t)
1541 ;; found the address
1543 (spam-kill-whole-line))
1544 ;; else, the address was not found
1546 (goto-char (point-max))
1552 ;;; returns t if the sender is in the whitelist, nil or
1553 ;;; spam-split-group otherwise
1554 (defun spam-check-whitelist ()
1555 ;; FIXME! Should it detect when file timestamps change?
1556 (let ((spam-split-group (if spam-split-symbolic-return
1559 (unless spam-whitelist-cache
1560 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1561 (if (spam-from-listed-p spam-whitelist-cache)
1563 (if spam-use-whitelist-exclusive
1567 (defun spam-check-blacklist ()
1568 ;; FIXME! Should it detect when file timestamps change?
1569 (let ((spam-split-group (if spam-split-symbolic-return
1572 (unless spam-blacklist-cache
1573 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1574 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1576 (defun spam-parse-list (file)
1577 (when (file-readable-p file)
1578 (let (contents address)
1580 (insert-file-contents file)
1582 (setq address (buffer-substring (point) (point-at-eol)))
1584 ;; insert the e-mail address if detected, otherwise the raw data
1585 (unless (zerop (length address))
1586 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1587 (push (or pure-address address) contents)))))
1588 (nreverse contents))))
1590 (defun spam-from-listed-p (cache)
1591 (let ((from (nnmail-fetch-field "from"))
1594 (let ((address (pop cache)))
1595 (unless (zerop (length address)) ; 0 for a nil address too
1596 (setq address (regexp-quote address))
1597 ;; fix regexp-quote's treatment of user-intended regexes
1598 (while (string-match "\\\\\\*" address)
1599 (setq address (replace-match ".*" t t address))))
1600 (when (and address (string-match address from))
1605 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1606 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1607 (declassification (if blacklist 'ham 'spam))
1609 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1611 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1612 from addresses unregister-list)
1613 (dolist (article articles)
1614 (let ((from (spam-fetch-field-from-fast article))
1615 (id (spam-fetch-field-message-id-fast article))
1617 (when (stringp from)
1618 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1619 (when (and (not sender-ignored)
1620 (stringp ignore-regex)
1621 (string-match ignore-regex from))
1622 (setq sender-ignored t)))
1623 ;; remember the messages we need to unregister, unless remove is set
1626 (spam-log-unregistration-needed-p
1627 id 'process declassification de-symbol))
1628 (push from unregister-list))
1629 (unless sender-ignored
1630 (push from addresses)))))
1633 (funcall enter-function addresses t) ; unregister all these addresses
1634 ;; else, register normally and unregister what we need to
1635 (funcall remove-function unregister-list t)
1636 (dolist (article unregister-list)
1637 (spam-log-undo-registration
1638 (spam-fetch-field-message-id-fast article)
1642 (funcall enter-function addresses nil))))
1644 (defun spam-blacklist-unregister-routine (articles)
1645 (spam-blacklist-register-routine articles t))
1647 (defun spam-blacklist-register-routine (articles &optional unregister)
1648 (spam-filelist-register-routine articles t unregister))
1650 (defun spam-whitelist-unregister-routine (articles)
1651 (spam-whitelist-register-routine articles t))
1653 (defun spam-whitelist-register-routine (articles &optional unregister)
1654 (spam-filelist-register-routine articles nil unregister))
1657 ;;;; Spam-report glue
1658 (defun spam-report-gmane-register-routine (articles)
1660 (apply 'spam-report-gmane articles)))
1664 (defun spam-check-bogofilter-headers (&optional score)
1665 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1666 (spam-split-group (if spam-split-symbolic-return
1669 (when header ; return nil when no header
1670 (if score ; scoring mode
1671 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1672 (match-string 1 header)
1674 ;; spam detection mode
1675 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1677 spam-split-group)))))
1679 ;; return something sensible if the score can't be determined
1680 (defun spam-bogofilter-score ()
1681 "Get the Bogofilter spamicity score"
1683 (save-window-excursion
1684 (gnus-summary-show-article t)
1685 (set-buffer gnus-article-buffer)
1686 (let ((score (or (spam-check-bogofilter-headers t)
1687 (spam-check-bogofilter t))))
1688 (message "Spamicity score %s" score)
1690 (gnus-summary-show-article)))
1692 (defun spam-check-bogofilter (&optional score)
1693 "Check the Bogofilter backend for the classification of this message"
1694 (let ((article-buffer-name (buffer-name))
1695 (db spam-bogofilter-database-directory)
1698 (let ((temp-buffer-name (buffer-name)))
1700 (set-buffer article-buffer-name)
1701 (apply 'call-process-region
1702 (point-min) (point-max)
1703 spam-bogofilter-path
1704 nil temp-buffer-name nil
1705 (if db `("-d" ,db "-v") `("-v"))))
1706 (setq return (spam-check-bogofilter-headers score))))
1709 (defun spam-bogofilter-register-with-bogofilter (articles
1711 &optional unregister)
1712 "Register an article, given as a string, as spam or non-spam."
1713 (dolist (article articles)
1714 (let ((article-string (spam-get-article-as-string article))
1715 (db spam-bogofilter-database-directory)
1716 (switch (if unregister
1718 spam-bogofilter-spam-strong-switch
1719 spam-bogofilter-ham-strong-switch)
1721 spam-bogofilter-spam-switch
1722 spam-bogofilter-ham-switch))))
1723 (when (stringp article-string)
1725 (insert article-string)
1727 (apply 'call-process-region
1728 (point-min) (point-max)
1729 spam-bogofilter-path
1731 (if db `("-d" ,db "-v") `("-v"))))))))
1733 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1734 (spam-bogofilter-register-with-bogofilter articles t unregister))
1736 (defun spam-bogofilter-unregister-spam-routine (articles)
1737 (spam-bogofilter-register-spam-routine articles t))
1739 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1740 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1742 (defun spam-bogofilter-unregister-ham-routine (articles)
1743 (spam-bogofilter-register-ham-routine articles t))
1748 (defun spam-check-spamoracle ()
1749 "Run spamoracle on an article to determine whether it's spam."
1750 (let ((article-buffer-name (buffer-name))
1751 (spam-split-group (if spam-split-symbolic-return
1755 (let ((temp-buffer-name (buffer-name)))
1757 (set-buffer article-buffer-name)
1759 (apply 'call-process-region
1760 (point-min) (point-max)
1761 spam-spamoracle-binary
1762 nil temp-buffer-name nil
1763 (if spam-spamoracle-database
1764 `("-f" ,spam-spamoracle-database "mark")
1768 (set-buffer temp-buffer-name)
1769 (goto-char (point-min))
1770 (when (re-search-forward "^X-Spam: yes;" nil t)
1772 (error "Error running spamoracle" status))))))))
1774 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1775 "Run spamoracle in training mode."
1777 (let ((temp-buffer-name (buffer-name)))
1779 (goto-char (point-min))
1780 (dolist (article articles)
1781 (insert (spam-get-article-as-string article)))
1782 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1786 (apply 'call-process-region
1787 (point-min) (point-max)
1788 spam-spamoracle-binary
1789 nil temp-buffer-name nil
1790 (if spam-spamoracle-database
1791 `("-f" ,spam-spamoracle-database
1794 (when (not (eq 0 status))
1795 (error "Error running spamoracle" status)))))))
1797 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1798 (spam-spamoracle-learn articles nil unregister))
1800 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1801 (spam-spamoracle-learn-ham articles t))
1803 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1804 (spam-spamoracle-learn articles t unregister))
1806 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1807 (spam-spamoracle-learn-spam articles t))
1813 (defun spam-initialize ()
1814 "Install the spam.el hooks and do other initialization"
1816 (setq spam-install-hooks t)
1817 ;; TODO: How do we redo this every time spam-face is customized?
1818 (push '((eq mark gnus-spam-mark) . spam-face)
1819 gnus-summary-highlight)
1820 ;; Add hooks for loading and saving the spam stats
1821 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1822 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1823 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1824 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1825 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1826 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1827 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1829 (defun spam-unload-hook ()
1830 "Uninstall the spam.el hooks"
1832 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1833 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1834 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1835 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1836 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1837 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1838 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1840 (when spam-install-hooks
1845 ;;; spam.el ends here.