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 t
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 (defun spam-report-articles-gmane (n)
574 "Report the current message as spam.
575 Respects the process/prefix convention."
577 (dolist (article (gnus-summary-work-articles n))
578 (gnus-summary-remove-process-mark article)
579 (spam-report-gmane article)))
581 ;;; Summary entry and exit processing.
583 (defun spam-summary-prepare ()
584 (setq spam-old-ham-articles
585 (spam-list-articles gnus-newsgroup-articles 'ham))
586 (setq spam-old-spam-articles
587 (spam-list-articles gnus-newsgroup-articles 'spam))
588 (spam-mark-junk-as-spam-routine))
590 ;; The spam processors are invoked for any group, spam or ham or neither
591 (defun spam-summary-prepare-exit ()
592 (unless gnus-group-is-exiting-without-update-p
593 (gnus-message 6 "Exiting summary buffer and applying spam rules")
595 ;; first of all, unregister any articles that are no longer ham or spam
596 ;; we have to iterate over the processors, or else we'll be too slow
597 (dolist (classification '(spam ham))
598 (let* ((old-articles (if (eq classification 'spam)
599 spam-old-spam-articles
600 spam-old-ham-articles))
601 (new-articles (spam-list-articles
602 gnus-newsgroup-articles
604 (changed-articles (gnus-set-difference old-articles new-articles)))
605 ;; now that we have the changed articles, we go through the processors
606 (dolist (processor-param spam-list-of-processors)
607 (let ((processor (nth 0 processor-param))
608 (processor-classification (nth 1 processor-param))
609 (check (nth 2 processor-param))
611 (dolist (article changed-articles)
612 (let ((id (spam-fetch-field-message-id-fast article)))
613 (when (spam-log-unregistration-needed-p
614 id 'process classification check)
615 (push article unregister-list))))
616 ;; call spam-register-routine with specific articles to unregister,
617 ;; when there are articles to unregister and the check is enabled
618 (when (and unregister-list (symbol-value check))
619 (spam-register-routine classification check t unregister-list))))))
621 ;; find all the spam processors applicable to this group
622 (dolist (processor-param spam-list-of-processors)
623 (let ((processor (nth 0 processor-param))
624 (classification (nth 1 processor-param))
625 (check (nth 2 processor-param)))
626 (when (and (eq 'spam classification)
627 (spam-group-processor-p gnus-newsgroup-name processor))
628 (spam-register-routine classification check))))
630 (if spam-move-spam-nonspam-groups-only
631 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
632 (spam-mark-spam-as-expired-and-move-routine
633 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
634 (gnus-message 5 "Marking spam as expired and moving it to %s"
636 (spam-mark-spam-as-expired-and-move-routine
637 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
639 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
640 ;; expire spam, in case the above did not expire them
641 (gnus-message 5 "Marking spam as expired without moving it")
642 (spam-mark-spam-as-expired-and-move-routine nil)
644 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
645 (and (spam-group-spam-contents-p gnus-newsgroup-name)
646 spam-process-ham-in-spam-groups)
647 spam-process-ham-in-nonham-groups)
648 ;; find all the ham processors applicable to this group
649 (dolist (processor-param spam-list-of-processors)
650 (let ((processor (nth 0 processor-param))
651 (classification (nth 1 processor-param))
652 (check (nth 2 processor-param)))
653 (when (and (eq 'ham classification)
654 (spam-group-processor-p gnus-newsgroup-name processor))
655 (spam-register-routine classification check)))))
657 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
658 (gnus-message 5 "Copying ham")
659 (spam-ham-copy-routine
660 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
662 ;; now move all ham articles out of spam groups
663 (when (spam-group-spam-contents-p gnus-newsgroup-name)
664 (gnus-message 5 "Moving ham messages from spam group")
665 (spam-ham-move-routine
666 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
668 (setq spam-old-ham-articles nil)
669 (setq spam-old-spam-articles nil))
671 (defun spam-mark-junk-as-spam-routine ()
672 ;; check the global list of group names spam-junk-mailgroups and the
674 (when (spam-group-spam-contents-p gnus-newsgroup-name)
675 (gnus-message 5 "Marking %s articles as spam"
676 (if spam-mark-only-unseen-as-spam
679 (let ((articles (if spam-mark-only-unseen-as-spam
680 gnus-newsgroup-unseen
681 gnus-newsgroup-unreads)))
682 (dolist (article articles)
683 (gnus-summary-mark-article article gnus-spam-mark)))))
685 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
686 (if (and (car-safe groups) (listp (car-safe groups)))
687 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
688 (gnus-summary-kill-process-mark)
689 (let ((articles gnus-newsgroup-articles)
690 (backend-supports-deletions
691 (gnus-check-backend-function
692 'request-move-article gnus-newsgroup-name))
693 article tomove deletep)
694 (dolist (article articles)
695 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
696 (gnus-summary-mark-article article gnus-expirable-mark)
697 (push article tomove)))
699 ;; now do the actual copies
700 (dolist (group groups)
703 (dolist (article tomove)
704 (gnus-summary-set-process-mark article))
706 (if (or (not backend-supports-deletions)
707 (> (length groups) 1))
709 (gnus-summary-copy-article nil group)
711 (gnus-summary-move-article nil group)))))
713 ;; now delete the articles, if there was a copy done, and the
715 (when (and deletep backend-supports-deletions)
716 (dolist (article tomove)
717 (gnus-summary-set-process-mark article))
719 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
720 (gnus-summary-delete-article nil))))
722 (gnus-summary-yank-process-mark))))
724 (defun spam-ham-copy-or-move-routine (copy groups)
725 (gnus-summary-kill-process-mark)
726 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
727 (backend-supports-deletions
728 (gnus-check-backend-function
729 'request-move-article gnus-newsgroup-name))
730 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
731 article mark todo deletep respool)
733 (when (member 'respool groups)
734 (setq respool t) ; boolean for later
735 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
737 ;; now do the actual move
738 (dolist (group groups)
739 (when (and todo (stringp group))
740 (dolist (article todo)
741 (when spam-mark-ham-unread-before-move-from-spam-group
742 (gnus-summary-mark-article article gnus-unread-mark))
743 (gnus-summary-set-process-mark article))
745 (if respool ; respooling is with a "fake" group
746 (let ((spam-split-disabled
747 (or spam-split-disabled
748 spam-disable-spam-split-during-ham-respool)))
749 (gnus-summary-respool-article nil respool-method))
750 (if (or (not backend-supports-deletions) ; else, we are not respooling
751 (> (length groups) 1))
752 (progn ; if copying, copy and set deletep
753 (gnus-summary-copy-article nil group)
755 (gnus-summary-move-article nil group))))) ; else move articles
757 ;; now delete the articles, unless a) copy is t, and there was a copy done
758 ;; b) a move was done to a single group
759 ;; c) backend-supports-deletions is nil
761 (when (and deletep backend-supports-deletions)
762 (dolist (article todo)
763 (gnus-summary-set-process-mark article))
765 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
766 (gnus-summary-delete-article nil))))))
768 (gnus-summary-yank-process-mark))
770 (defun spam-ham-copy-routine (&rest groups)
771 (if (and (car-safe groups) (listp (car-safe groups)))
772 (apply 'spam-ham-copy-routine (car groups))
773 (spam-ham-copy-or-move-routine t groups)))
775 (defun spam-ham-move-routine (&rest groups)
776 (if (and (car-safe groups) (listp (car-safe groups)))
777 (apply 'spam-ham-move-routine (car groups))
778 (spam-ham-copy-or-move-routine nil groups)))
780 (defun spam-get-article-as-string (article)
781 (let ((article-buffer (spam-get-article-as-buffer article))
784 (save-window-excursion
785 (set-buffer article-buffer)
786 (setq article-string (buffer-string))))
789 (defun spam-get-article-as-buffer (article)
790 (let ((article-buffer))
791 (when (numberp article)
792 (save-window-excursion
793 (gnus-summary-goto-subject article)
794 (gnus-summary-show-article t)
795 (setq article-buffer (get-buffer gnus-article-buffer))))
799 ;; (defun spam-get-article-as-filename (article)
800 ;; (let ((article-filename))
801 ;; (when (numberp article)
802 ;; (nnml-possibly-change-directory
803 ;; (gnus-group-real-name gnus-newsgroup-name))
804 ;; (setq article-filename (expand-file-name
805 ;; (int-to-string article) nnml-current-directory)))
806 ;; (if (file-exists-p article-filename)
810 (defun spam-fetch-field-from-fast (article)
811 "Fetch the `from' field quickly, using the internal gnus-data-list function"
812 (if (and (numberp article)
813 (assoc article (gnus-data-list nil)))
815 (gnus-data-header (assoc article (gnus-data-list nil))))
818 (defun spam-fetch-field-subject-fast (article)
819 "Fetch the `subject' field quickly, using the internal
820 gnus-data-list function"
821 (if (and (numberp article)
822 (assoc article (gnus-data-list nil)))
824 (gnus-data-header (assoc article (gnus-data-list nil))))
827 (defun spam-fetch-field-message-id-fast (article)
828 "Fetch the `Message-ID' field quickly, using the internal
829 gnus-data-list function"
830 (if (and (numberp article)
831 (assoc article (gnus-data-list nil)))
832 (mail-header-message-id
833 (gnus-data-header (assoc article (gnus-data-list nil))))
837 ;;;; Spam determination.
839 (defvar spam-list-of-checks
840 '((spam-use-blacklist . spam-check-blacklist)
841 (spam-use-regex-headers . spam-check-regex-headers)
842 (spam-use-regex-body . spam-check-regex-body)
843 (spam-use-whitelist . spam-check-whitelist)
844 (spam-use-BBDB . spam-check-BBDB)
845 (spam-use-ifile . spam-check-ifile)
846 (spam-use-spamoracle . spam-check-spamoracle)
847 (spam-use-stat . spam-check-stat)
848 (spam-use-blackholes . spam-check-blackholes)
849 (spam-use-hashcash . spam-check-hashcash)
850 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
851 (spam-use-bogofilter . spam-check-bogofilter))
852 "The spam-list-of-checks list contains pairs associating a
853 parameter variable with a spam checking function. If the
854 parameter variable is true, then the checking function is called,
855 and its value decides what happens. Each individual check may
856 return nil, t, or a mailgroup name. The value nil means that the
857 check does not yield a decision, and so, that further checks are
858 needed. The value t means that the message is definitely not
859 spam, and that further spam checks should be inhibited.
860 Otherwise, a mailgroup name or the symbol 'spam (depending on
861 spam-split-symbolic-return) is returned where the mail should go,
862 and further checks are also inhibited. The usual mailgroup name
863 is the value of `spam-split-group', meaning that the message is
866 (defvar spam-list-of-statistical-checks
872 "The spam-list-of-statistical-checks list contains all the mail
873 splitters that need to have the full message body available.")
875 ;;;TODO: modify to invoke self with each check if invoked without specifics
876 (defun spam-split (&rest specific-checks)
877 "Split this message into the `spam' group if it is spam.
878 This function can be used as an entry in the variable `nnmail-split-fancy',
879 for example like this: (: spam-split). It can take checks as
880 parameters. A string as a parameter will set the
881 spam-split-group to that string.
883 See the Info node `(gnus)Fancy Mail Splitting' for more details."
885 (setq spam-split-last-successful-check nil)
886 (unless spam-split-disabled
887 (let ((spam-split-group-choice spam-split-group))
888 (dolist (check specific-checks)
889 (when (stringp check)
890 (setq spam-split-group-choice check)
891 (setq specific-checks (delq check specific-checks))))
893 (let ((spam-split-group spam-split-group-choice))
896 (dolist (check spam-list-of-statistical-checks)
897 (when (and (symbolp check) (symbol-value check))
899 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
902 ;; (progn (widen) (debug (buffer-string)))
903 (let ((list-of-checks spam-list-of-checks)
905 (while (and list-of-checks (not decision))
906 (let ((pair (pop list-of-checks)))
908 ;; either, given specific checks, this is one of them
909 (and specific-checks (memq (car pair) specific-checks))
910 ;; or, given no specific checks, spam-use-CHECK is set
911 (and (null specific-checks) (symbol-value (car pair))))
912 (gnus-message 5 "spam-split: calling the %s function"
913 (symbol-name (cdr pair)))
914 (setq decision (funcall (cdr pair)))
915 ;; if we got a decision at all, save the current check
917 (setq spam-split-last-successful-check (car pair)))
919 (when (eq decision 'spam)
920 (unless spam-split-symbolic-return
923 (format "spam-split got %s but %s is nil"
924 (symbol-name decision)
925 (symbol-name spam-split-symbolic-return))))))))
927 (if spam-split-symbolic-return-positive 'ham nil)
930 (defun spam-find-spam ()
931 "This function will detect spam in the current newsgroup using spam-split."
934 (let* ((group gnus-newsgroup-name)
935 (autodetect (gnus-parameter-spam-autodetect group))
936 (methods (gnus-parameter-spam-autodetect-methods group))
937 (first-method (nth 0 methods))
938 (articles (if spam-autodetect-recheck-messages
939 gnus-newsgroup-articles
940 gnus-newsgroup-unseen)))
942 (when (and autodetect
943 (not (equal first-method 'none)))
946 (let ((id (spam-fetch-field-message-id-fast article))
947 (subject (spam-fetch-field-subject-fast article))
948 (sender (spam-fetch-field-from-fast article))
952 (gnus-error 5 "Article %d has no message ID!" article))
954 (when (and id spam-log-to-registry)
955 (setq registry-lookup (spam-log-registration-type id 'incoming))
956 (when registry-lookup
959 "spam-find-spam: message %s was already registered incoming"
962 (let* ((spam-split-symbolic-return t)
963 (spam-split-symbolic-return-positive t)
967 (gnus-request-article-this-buffer
970 (if (or (null first-method)
971 (equal first-method 'default))
973 (apply 'spam-split methods))))))
974 (if (equal split-return 'spam)
975 (gnus-summary-mark-article article gnus-spam-mark))
977 (when (and id split-return spam-log-to-registry)
978 (when (zerop (gnus-registry-group-count id))
979 (gnus-registry-add-group
980 id group subject sender))
982 (unless registry-lookup
983 (spam-log-processing-to-registry
987 spam-split-last-successful-check
991 (defvar spam-registration-functions
992 ;; first the ham register, second the spam register function
993 ;; third the ham unregister, fourth the spam unregister function
994 '((spam-use-blacklist nil
995 spam-blacklist-register-routine
997 spam-blacklist-unregister-routine)
998 (spam-use-whitelist spam-whitelist-register-routine
1000 spam-whitelist-unregister-routine
1002 (spam-use-BBDB spam-BBDB-register-routine
1004 spam-BBDB-unregister-routine
1006 (spam-use-ifile spam-ifile-register-ham-routine
1007 spam-ifile-register-spam-routine
1008 spam-ifile-unregister-ham-routine
1009 spam-ifile-unregister-spam-routine)
1010 (spam-use-spamoracle spam-spamoracle-learn-ham
1011 spam-spamoracle-learn-spam
1012 spam-spamoracle-unlearn-ham
1013 spam-spamoracle-unlearn-spam)
1014 (spam-use-stat spam-stat-register-ham-routine
1015 spam-stat-register-spam-routine
1016 spam-stat-unregister-ham-routine
1017 spam-stat-unregister-spam-routine)
1018 ;; note that spam-use-gmane is not a legitimate check
1020 spam-report-gmane-register-routine
1021 ;; does Gmane support unregistration?
1024 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1025 spam-bogofilter-register-spam-routine
1026 spam-bogofilter-unregister-ham-routine
1027 spam-bogofilter-unregister-spam-routine))
1028 "The spam-registration-functions list contains pairs
1029 associating a parameter variable with the ham and spam
1030 registration functions, and the ham and spam unregistration
1033 (defun spam-classification-valid-p (classification)
1034 (or (eq classification 'spam)
1035 (eq classification 'ham)))
1037 (defun spam-process-type-valid-p (process-type)
1038 (or (eq process-type 'incoming)
1039 (eq process-type 'process)))
1041 (defun spam-registration-check-valid-p (check)
1042 (assoc check spam-registration-functions))
1044 (defun spam-unregistration-check-valid-p (check)
1045 (assoc check spam-registration-functions))
1047 (defun spam-registration-function (classification check)
1048 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1049 (if (eq classification 'spam)
1053 (defun spam-unregistration-function (classification check)
1054 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1055 (if (eq classification 'spam)
1059 (defun spam-list-articles (articles classification)
1060 (let ((mark-check (if (eq classification 'spam)
1061 'spam-group-spam-mark-p
1062 'spam-group-ham-mark-p))
1063 list mark-cache-yes mark-cache-no)
1064 (dolist (article articles)
1065 (let ((mark (gnus-summary-article-mark article)))
1066 (unless (memq mark mark-cache-no)
1067 (if (memq mark mark-cache-yes)
1069 ;; else, we have to actually check the mark
1070 (if (funcall mark-check
1075 (push mark mark-cache-yes))
1076 (push mark mark-cache-no))))))
1079 (defun spam-register-routine (classification
1081 &optional unregister
1083 (when (and (spam-classification-valid-p classification)
1084 (spam-registration-check-valid-p check))
1085 (let* ((register-function
1086 (spam-registration-function classification check))
1087 (unregister-function
1088 (spam-unregistration-function classification check))
1089 (run-function (if unregister
1092 (log-function (if unregister
1093 'spam-log-undo-registration
1094 'spam-log-processing-to-registry))
1098 ;; make list of articles, using specific-articles if given
1099 (setq articles (or specific-articles
1101 gnus-newsgroup-articles
1104 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1105 (if unregister "Unregistering" "Registering")
1107 (if specific-articles "specific" "")
1108 (symbol-name classification)
1109 (symbol-name check))
1110 (funcall run-function articles)
1111 ;; now log all the registrations (or undo them, depending on unregister)
1112 (dolist (article articles)
1113 (funcall log-function
1114 (spam-fetch-field-message-id-fast article)
1118 gnus-newsgroup-name))))))
1120 ;;; log a ham- or spam-processor invocation to the registry
1121 (defun spam-log-processing-to-registry (id type classification check group)
1122 (when spam-log-to-registry
1123 (if (and (stringp id)
1125 (spam-process-type-valid-p type)
1126 (spam-classification-valid-p classification)
1127 (spam-registration-check-valid-p check))
1128 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1129 (cell (list classification check group)))
1130 (push cell cell-list)
1131 (gnus-registry-store-extra-entry
1136 (gnus-error 5 (format "%s called with bad ID, type, classification, check, or group"
1137 "spam-log-processing-to-registry")))))
1139 ;;; check if a ham- or spam-processor registration has been done
1140 (defun spam-log-registered-p (id type)
1141 (when spam-log-to-registry
1142 (if (and (stringp id)
1143 (spam-process-type-valid-p type))
1144 (cdr-safe (gnus-registry-fetch-extra id type))
1146 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1147 "spam-log-registered-p"))
1150 ;;; check what a ham- or spam-processor registration says
1151 ;;; returns nil if conflicting registrations are found
1152 (defun spam-log-registration-type (id type)
1155 (dolist (reg (spam-log-registered-p id type))
1156 (let ((classification (nth 0 reg)))
1157 (when (spam-classification-valid-p classification)
1159 (not (eq classification decision)))
1160 (setq count (+ 1 count)))
1161 (setq decision classification))))
1166 ;;; check if a ham- or spam-processor registration needs to be undone
1167 (defun spam-log-unregistration-needed-p (id type classification check)
1168 (when spam-log-to-registry
1169 (if (and (stringp id)
1170 (spam-process-type-valid-p type)
1171 (spam-classification-valid-p classification)
1172 (spam-registration-check-valid-p check))
1173 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1175 (dolist (cell cell-list)
1177 (when (and (eq classification (nth 0 cell))
1178 (eq check (nth 1 cell)))
1182 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1183 "spam-log-unregistration-needed-p"))
1187 ;;; undo a ham- or spam-processor registration (the group is not used)
1188 (defun spam-log-undo-registration (id type classification check &optional group)
1189 (when (and spam-log-to-registry
1190 (spam-log-unregistration-needed-p id type classification check))
1191 (if (and (stringp id)
1192 (spam-process-type-valid-p type)
1193 (spam-classification-valid-p classification)
1194 (spam-registration-check-valid-p check))
1195 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1196 new-cell-list found)
1197 (dolist (cell cell-list)
1198 (unless (and (eq classification (nth 0 cell))
1199 (eq check (nth 1 cell)))
1200 (push cell new-cell-list)))
1201 (gnus-registry-store-extra-entry
1206 (gnus-error 5 (format "%s called with bad ID, type, check, or group"
1207 "spam-log-undo-registration"))
1210 ;;; set up IMAP widening if it's necessary
1211 (defun spam-setup-widening ()
1212 (dolist (check spam-list-of-statistical-checks)
1213 (when (symbol-value check)
1214 (setq nnimap-split-download-body-default t))))
1219 (defun spam-check-regex-body ()
1220 (let ((spam-regex-headers-ham spam-regex-body-ham)
1221 (spam-regex-headers-spam spam-regex-body-spam))
1222 (spam-check-regex-headers t)))
1227 (defun spam-check-regex-headers (&optional body)
1228 (let ((type (if body "body" "header"))
1229 (spam-split-group (if spam-split-symbolic-return
1233 (dolist (h-regex spam-regex-headers-ham)
1235 (goto-char (point-min))
1236 (when (re-search-forward h-regex nil t)
1237 (message "Ham regex %s search positive." type)
1239 (dolist (s-regex spam-regex-headers-spam)
1241 (goto-char (point-min))
1242 (when (re-search-forward s-regex nil t)
1243 (message "Spam regex %s search positive." type)
1245 (setq ret spam-split-group))))
1251 (defun spam-reverse-ip-string (ip)
1253 (mapconcat 'identity
1254 (nreverse (split-string ip "\\."))
1257 (defun spam-check-blackholes ()
1258 "Check the Received headers for blackholed relays."
1259 (let ((headers (nnmail-fetch-field "received"))
1260 (spam-split-group (if spam-split-symbolic-return
1267 (goto-char (point-min))
1268 (gnus-message 5 "Checking headers for relay addresses")
1269 (while (re-search-forward
1270 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1271 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1272 (push (spam-reverse-ip-string (match-string 1))
1274 (dolist (server spam-blackhole-servers)
1276 (unless (and spam-blackhole-good-server-regex
1277 ;; match the good-server-regex against the reversed (again) IP string
1279 spam-blackhole-good-server-regex
1280 (spam-reverse-ip-string ip)))
1282 (let ((query-string (concat ip "." server)))
1284 (let ((query-result (query-dig query-string)))
1286 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1288 (push (list ip server query-result)
1290 ;; else, if not using dig.el
1291 (when (query-dns query-string)
1292 (gnus-message 5 "positive blackhole check")
1293 (push (list ip server (query-dns query-string 'TXT))
1304 (defun spam-check-hashcash ()
1305 "Check the headers for hashcash payments."
1306 (mail-check-payment))) ;mail-check-payment returns a boolean
1309 (defalias 'mail-check-payment 'ignore)
1310 (defalias 'spam-check-hashcash 'ignore))))
1314 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1315 ;;; <sacha@giotto.sj.ru>
1317 ;; all this is done inside a condition-case to trap errors
1324 ;; when the BBDB changes, we want to clear out our cache
1325 (defun spam-clear-cache-BBDB (&rest immaterial)
1326 (spam-clear-cache 'spam-use-BBDB))
1328 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1330 (defun spam-enter-ham-BBDB (addresses &optional remove)
1331 "Enter an address into the BBDB; implies ham (non-spam) sender"
1332 (dolist (from addresses)
1333 (when (stringp from)
1334 (let* ((parsed-address (gnus-extract-address-components from))
1335 (name (or (nth 0 parsed-address) "Ham Sender"))
1336 (remove-function (if remove
1337 'bbdb-delete-record-internal
1339 (net-address (nth 1 parsed-address))
1340 (record (and net-address
1341 (bbdb-search-simple nil net-address))))
1343 (gnus-message 5 "%s address %s %s BBDB"
1344 (if remove "Deleting" "Adding")
1346 (if remove "from" "to"))
1348 (funcall remove-function record)
1349 (bbdb-create-internal name nil net-address nil nil
1350 "ham sender added by spam.el")))))))
1352 (defun spam-BBDB-register-routine (articles &optional unregister)
1354 (dolist (article articles)
1355 (when (stringp (spam-fetch-field-from-fast article))
1356 (push (spam-fetch-field-from-fast article) addresses)))
1357 ;; now do the register/unregister action
1358 (spam-enter-ham-BBDB addresses unregister)))
1360 (defun spam-BBDB-unregister-routine (articles)
1361 (spam-BBDB-register-routine articles t))
1363 (defun spam-check-BBDB ()
1364 "Mail from people in the BBDB is classified as ham or non-spam"
1365 (let ((who (nnmail-fetch-field "from"))
1366 (spam-split-group (if spam-split-symbolic-return
1369 bbdb-cache bbdb-hashtable)
1371 (when spam-cache-lookups
1372 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1375 ;; this is the expanded (bbdb-hashtable) macro
1376 ;; without the debugging support
1377 (with-current-buffer (bbdb-buffer)
1379 (save-window-excursion
1380 (bbdb-records nil t)
1382 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1384 (setq who (nth 1 (gnus-extract-address-components who)))
1386 (if spam-cache-lookups
1388 (intern-soft who bbdb-cache))
1389 (bbdb-search-simple nil who))
1391 (if spam-use-BBDB-exclusive
1396 (defalias 'bbdb-search-simple 'ignore)
1397 (defalias 'bbdb-records 'ignore)
1398 (defalias 'bbdb-buffer 'ignore)
1399 (defalias 'spam-check-BBDB 'ignore)
1400 (defalias 'spam-BBDB-register-routine 'ignore)
1401 (defalias 'spam-enter-ham-BBDB 'ignore)
1402 (defalias 'bbdb-create-internal 'ignore)
1403 (defalias 'bbdb-delete-record-internal 'ignore)
1404 (defalias 'bbdb-records 'ignore))))
1409 ;;; check the ifile backend; return nil if the mail was NOT classified
1412 (defun spam-get-ifile-database-parameter ()
1413 "Get the command-line parameter for ifile's database from
1414 spam-ifile-database-path."
1415 (if spam-ifile-database-path
1416 (format "--db-file=%s" spam-ifile-database-path)
1419 (defun spam-check-ifile ()
1420 "Check the ifile backend for the classification of this message."
1421 (let ((article-buffer-name (buffer-name))
1422 (spam-split-group (if spam-split-symbolic-return
1427 (let ((temp-buffer-name (buffer-name))
1428 (db-param (spam-get-ifile-database-parameter)))
1430 (set-buffer article-buffer-name)
1431 (apply 'call-process-region
1432 (point-min) (point-max) spam-ifile-path
1433 nil temp-buffer-name nil "-c"
1434 (if db-param `(,db-param "-q") `("-q"))))
1435 ;; check the return now (we're back in the temp buffer)
1436 (goto-char (point-min))
1438 (setq category (buffer-substring (point) (point-at-eol))))
1439 (when (not (zerop (length category))) ; we need a category here
1440 (if spam-ifile-all-categories
1441 (setq return category)
1442 ;; else, if spam-ifile-all-categories is not set...
1443 (when (string-equal spam-ifile-spam-category category)
1444 (setq return spam-split-group)))))) ; note return is nil otherwise
1447 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1448 "Register an article, given as a string, with a category.
1449 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1450 (let ((category (or category gnus-newsgroup-name))
1451 (add-or-delete-option (if unregister "-d" "-i"))
1452 (db (spam-get-ifile-database-parameter))
1455 (dolist (article articles)
1456 (let ((article-string (spam-get-article-as-string article)))
1457 (when (stringp article-string)
1458 (insert article-string))))
1459 (apply 'call-process-region
1460 (point-min) (point-max) spam-ifile-path
1462 add-or-delete-option category
1463 (if db `(,db "-h") `("-h"))))))
1465 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1466 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1468 (defun spam-ifile-unregister-spam-routine (articles)
1469 (spam-ifile-register-spam-routine articles t))
1471 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1472 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1474 (defun spam-ifile-unregister-ham-routine (articles)
1475 (spam-ifile-register-ham-routine articles t))
1482 (let ((spam-stat-install-hooks nil))
1483 (require 'spam-stat))
1485 (defun spam-check-stat ()
1486 "Check the spam-stat backend for the classification of this message"
1487 (let ((spam-split-group (if spam-split-symbolic-return
1490 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1491 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1493 (spam-stat-split-fancy)))
1495 (defun spam-stat-register-spam-routine (articles &optional unregister)
1496 (dolist (article articles)
1497 (let ((article-string (spam-get-article-as-string article)))
1499 (insert article-string)
1501 (spam-stat-buffer-change-to-non-spam)
1502 (spam-stat-buffer-is-spam))))))
1504 (defun spam-stat-unregister-spam-routine (articles)
1505 (spam-stat-register-spam-routine articles t))
1507 (defun spam-stat-register-ham-routine (articles &optional unregister)
1508 (dolist (article articles)
1509 (let ((article-string (spam-get-article-as-string article)))
1511 (insert article-string)
1513 (spam-stat-buffer-change-to-spam)
1514 (spam-stat-buffer-is-non-spam))))))
1516 (defun spam-stat-unregister-ham-routine (articles)
1517 (spam-stat-register-ham-routine articles t))
1519 (defun spam-maybe-spam-stat-load ()
1520 (when spam-use-stat (spam-stat-load)))
1522 (defun spam-maybe-spam-stat-save ()
1523 (when spam-use-stat (spam-stat-save))))
1526 (defalias 'spam-stat-load 'ignore)
1527 (defalias 'spam-stat-save 'ignore)
1528 (defalias 'spam-maybe-spam-stat-load 'ignore)
1529 (defalias 'spam-maybe-spam-stat-save 'ignore)
1530 (defalias 'spam-stat-register-ham-routine 'ignore)
1531 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1532 (defalias 'spam-stat-register-spam-routine 'ignore)
1533 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1534 (defalias 'spam-stat-buffer-is-spam 'ignore)
1535 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1536 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1537 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1538 (defalias 'spam-stat-split-fancy 'ignore)
1539 (defalias 'spam-check-stat 'ignore))))
1543 ;;;; Blacklists and whitelists.
1545 (defvar spam-whitelist-cache nil)
1546 (defvar spam-blacklist-cache nil)
1548 (defun spam-kill-whole-line ()
1550 (let ((kill-whole-line t))
1553 ;;; address can be a list, too
1554 (defun spam-enter-whitelist (address &optional remove)
1555 "Enter ADDRESS (list or single) into the whitelist.
1556 With a non-nil REMOVE, remove them."
1557 (interactive "sAddress: ")
1558 (spam-enter-list address spam-whitelist remove)
1559 (setq spam-whitelist-cache nil)
1560 (spam-clear-cache 'spam-use-whitelist))
1562 ;;; address can be a list, too
1563 (defun spam-enter-blacklist (address &optional remove)
1564 "Enter ADDRESS (list or single) into the blacklist.
1565 With a non-nil REMOVE, remove them."
1566 (interactive "sAddress: ")
1567 (spam-enter-list address spam-blacklist remove)
1568 (setq spam-blacklist-cache nil)
1569 (spam-clear-cache 'spam-use-whitelist))
1571 (defun spam-enter-list (addresses file &optional remove)
1572 "Enter ADDRESSES into the given FILE.
1573 Either the whitelist or the blacklist files can be used. With
1574 REMOVE not nil, remove the ADDRESSES."
1575 (if (stringp addresses)
1576 (spam-enter-list (list addresses) file remove)
1577 ;; else, we have a list of addresses here
1578 (unless (file-exists-p (file-name-directory file))
1579 (make-directory (file-name-directory file) t))
1582 (find-file-noselect file))
1583 (dolist (a addresses)
1585 (goto-char (point-min))
1586 (if (re-search-forward (regexp-quote a) nil t)
1587 ;; found the address
1589 (spam-kill-whole-line))
1590 ;; else, the address was not found
1592 (goto-char (point-max))
1598 (defun spam-filelist-build-cache (type)
1599 (let ((cache (if (eq type 'spam-use-blacklist)
1600 spam-blacklist-cache
1601 spam-whitelist-cache))
1603 (unless (gethash type spam-caches)
1605 (let ((address (pop cache)))
1606 (unless (zerop (length address)) ; 0 for a nil address too
1607 (setq address (regexp-quote address))
1608 ;; fix regexp-quote's treatment of user-intended regexes
1609 (while (string-match "\\\\\\*" address)
1610 (setq address (replace-match ".*" t t address))))
1611 (push address parsed-cache)))
1612 (puthash type parsed-cache spam-caches))))
1614 (defun spam-filelist-check-cache (type from)
1615 (when (stringp from)
1616 (spam-filelist-build-cache type)
1618 (dolist (address (gethash type spam-caches))
1619 (when (and address (string-match address from))
1624 ;;; returns t if the sender is in the whitelist, nil or
1625 ;;; spam-split-group otherwise
1626 (defun spam-check-whitelist ()
1627 ;; FIXME! Should it detect when file timestamps change?
1628 (let ((spam-split-group (if spam-split-symbolic-return
1631 (unless spam-whitelist-cache
1632 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1633 (if (spam-from-listed-p 'spam-use-whitelist)
1635 (if spam-use-whitelist-exclusive
1639 (defun spam-check-blacklist ()
1640 ;; FIXME! Should it detect when file timestamps change?
1641 (let ((spam-split-group (if spam-split-symbolic-return
1644 (unless spam-blacklist-cache
1645 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1646 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1648 (defun spam-parse-list (file)
1649 (when (file-readable-p file)
1650 (let (contents address)
1652 (insert-file-contents file)
1654 (setq address (buffer-substring (point) (point-at-eol)))
1656 ;; insert the e-mail address if detected, otherwise the raw data
1657 (unless (zerop (length address))
1658 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1659 (push (or pure-address address) contents)))))
1660 (nreverse contents))))
1662 (defun spam-from-listed-p (type)
1663 (let ((from (nnmail-fetch-field "from"))
1665 (spam-filelist-check-cache type from)))
1667 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1668 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1669 (declassification (if blacklist 'ham 'spam))
1671 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1673 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1674 from addresses unregister-list)
1675 (dolist (article articles)
1676 (let ((from (spam-fetch-field-from-fast article))
1677 (id (spam-fetch-field-message-id-fast article))
1679 (when (stringp from)
1680 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1681 (when (and (not sender-ignored)
1682 (stringp ignore-regex)
1683 (string-match ignore-regex from))
1684 (setq sender-ignored t)))
1685 ;; remember the messages we need to unregister, unless remove is set
1688 (spam-log-unregistration-needed-p
1689 id 'process declassification de-symbol))
1690 (push from unregister-list))
1691 (unless sender-ignored
1692 (push from addresses)))))
1695 (funcall enter-function addresses t) ; unregister all these addresses
1696 ;; else, register normally and unregister what we need to
1697 (funcall remove-function unregister-list t)
1698 (dolist (article unregister-list)
1699 (spam-log-undo-registration
1700 (spam-fetch-field-message-id-fast article)
1704 (funcall enter-function addresses nil))))
1706 (defun spam-blacklist-unregister-routine (articles)
1707 (spam-blacklist-register-routine articles t))
1709 (defun spam-blacklist-register-routine (articles &optional unregister)
1710 (spam-filelist-register-routine articles t unregister))
1712 (defun spam-whitelist-unregister-routine (articles)
1713 (spam-whitelist-register-routine articles t))
1715 (defun spam-whitelist-register-routine (articles &optional unregister)
1716 (spam-filelist-register-routine articles nil unregister))
1719 ;;;; Spam-report glue
1720 (defun spam-report-gmane-register-routine (articles)
1722 (apply 'spam-report-gmane articles)))
1726 (defun spam-check-bogofilter-headers (&optional score)
1727 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1728 (spam-split-group (if spam-split-symbolic-return
1731 (when header ; return nil when no header
1732 (if score ; scoring mode
1733 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1734 (match-string 1 header)
1736 ;; spam detection mode
1737 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1739 spam-split-group)))))
1741 ;; return something sensible if the score can't be determined
1742 (defun spam-bogofilter-score ()
1743 "Get the Bogofilter spamicity score"
1745 (save-window-excursion
1746 (gnus-summary-show-article t)
1747 (set-buffer gnus-article-buffer)
1748 (let ((score (or (spam-check-bogofilter-headers t)
1749 (spam-check-bogofilter t))))
1750 (message "Spamicity score %s" score)
1752 (gnus-summary-show-article)))
1754 (defun spam-check-bogofilter (&optional score)
1755 "Check the Bogofilter backend for the classification of this message"
1756 (let ((article-buffer-name (buffer-name))
1757 (db spam-bogofilter-database-directory)
1760 (let ((temp-buffer-name (buffer-name)))
1762 (set-buffer article-buffer-name)
1763 (apply 'call-process-region
1764 (point-min) (point-max)
1765 spam-bogofilter-path
1766 nil temp-buffer-name nil
1767 (if db `("-d" ,db "-v") `("-v"))))
1768 (setq return (spam-check-bogofilter-headers score))))
1771 (defun spam-bogofilter-register-with-bogofilter (articles
1773 &optional unregister)
1774 "Register an article, given as a string, as spam or non-spam."
1775 (dolist (article articles)
1776 (let ((article-string (spam-get-article-as-string article))
1777 (db spam-bogofilter-database-directory)
1778 (switch (if unregister
1780 spam-bogofilter-spam-strong-switch
1781 spam-bogofilter-ham-strong-switch)
1783 spam-bogofilter-spam-switch
1784 spam-bogofilter-ham-switch))))
1785 (when (stringp article-string)
1787 (insert article-string)
1789 (apply 'call-process-region
1790 (point-min) (point-max)
1791 spam-bogofilter-path
1793 (if db `("-d" ,db "-v") `("-v"))))))))
1795 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1796 (spam-bogofilter-register-with-bogofilter articles t unregister))
1798 (defun spam-bogofilter-unregister-spam-routine (articles)
1799 (spam-bogofilter-register-spam-routine articles t))
1801 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1802 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1804 (defun spam-bogofilter-unregister-ham-routine (articles)
1805 (spam-bogofilter-register-ham-routine articles t))
1810 (defun spam-check-spamoracle ()
1811 "Run spamoracle on an article to determine whether it's spam."
1812 (let ((article-buffer-name (buffer-name))
1813 (spam-split-group (if spam-split-symbolic-return
1817 (let ((temp-buffer-name (buffer-name)))
1819 (set-buffer article-buffer-name)
1821 (apply 'call-process-region
1822 (point-min) (point-max)
1823 spam-spamoracle-binary
1824 nil temp-buffer-name nil
1825 (if spam-spamoracle-database
1826 `("-f" ,spam-spamoracle-database "mark")
1830 (set-buffer temp-buffer-name)
1831 (goto-char (point-min))
1832 (when (re-search-forward "^X-Spam: yes;" nil t)
1834 (error "Error running spamoracle" status))))))))
1836 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1837 "Run spamoracle in training mode."
1839 (let ((temp-buffer-name (buffer-name)))
1841 (goto-char (point-min))
1842 (dolist (article articles)
1843 (insert (spam-get-article-as-string article)))
1844 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1848 (apply 'call-process-region
1849 (point-min) (point-max)
1850 spam-spamoracle-binary
1851 nil temp-buffer-name nil
1852 (if spam-spamoracle-database
1853 `("-f" ,spam-spamoracle-database
1856 (when (not (eq 0 status))
1857 (error "Error running spamoracle" status)))))))
1859 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1860 (spam-spamoracle-learn articles nil unregister))
1862 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1863 (spam-spamoracle-learn-ham articles t))
1865 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1866 (spam-spamoracle-learn articles t unregister))
1868 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1869 (spam-spamoracle-learn-spam articles t))
1875 (defun spam-initialize ()
1876 "Install the spam.el hooks and do other initialization"
1878 (setq spam-install-hooks t)
1879 ;; TODO: How do we redo this every time spam-face is customized?
1880 (push '((eq mark gnus-spam-mark) . spam-face)
1881 gnus-summary-highlight)
1882 ;; Add hooks for loading and saving the spam stats
1883 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1884 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1885 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1886 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1887 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1888 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1889 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1891 (defun spam-unload-hook ()
1892 "Uninstall the spam.el hooks"
1894 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1895 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1896 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1897 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1898 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1899 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1900 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1902 (when spam-install-hooks
1907 ;;; spam.el ends here.