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.
134 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.
409 When nil, use the default 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
448 `spam-split' will set this to nil or a spam-use-XYZ check if it
451 ;; convenience functions
452 (defun spam-clear-cache (symbol)
453 "Clear the spam-caches entry for a check."
454 (remhash symbol spam-caches))
456 (defun spam-xor (a b)
458 (and (or a b) (not (and a b))))
460 (defun spam-group-ham-mark-p (group mark &optional spam)
461 "Checks if MARK is considered a ham mark in GROUP."
462 (when (stringp group)
463 (let* ((marks (spam-group-ham-marks group spam))
464 (marks (if (symbolp mark)
466 (mapcar 'symbol-value marks))))
469 (defun spam-group-spam-mark-p (group mark)
470 "Checks if MARK is considered a spam mark in GROUP."
471 (spam-group-ham-mark-p group mark t))
473 (defun spam-group-ham-marks (group &optional spam)
474 "In GROUP, get all the ham marks."
475 (when (stringp group)
476 (let* ((marks (if spam
477 (gnus-parameter-spam-marks group)
478 (gnus-parameter-ham-marks group)))
480 (marks (if (listp (car marks)) (car marks) marks)))
483 (defun spam-group-spam-marks (group)
484 "In GROUP, get all the spam marks."
485 (spam-group-ham-marks group t))
487 (defun spam-group-spam-contents-p (group)
488 "Is GROUP a spam group?"
490 (or (member group spam-junk-mailgroups)
491 (memq 'gnus-group-spam-classification-spam
492 (gnus-parameter-spam-contents group)))
495 (defun spam-group-ham-contents-p (group)
496 "Is GROUP a ham group?"
498 (memq 'gnus-group-spam-classification-ham
499 (gnus-parameter-spam-contents group))
502 (defvar spam-list-of-processors
503 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
504 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
505 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
506 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
507 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
508 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
509 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
510 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
511 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
512 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
513 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
514 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
515 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
516 "The `spam-list-of-processors' list.
517 This list contains pairs associating a ham/spam exit processor
518 variable with a classification and a spam-use-* variable.")
520 (defun spam-group-processor-p (group processor)
521 (if (and (stringp group)
523 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
524 (spam-group-processor-multiple-p
526 (cdr-safe (assoc processor spam-list-of-processors))))
529 (defun spam-group-processor-multiple-p (group processor-info)
530 (let* ((classification (nth 0 processor-info))
531 (check (nth 1 processor-info))
532 (parameters (nth 0 (gnus-parameter-spam-process group)))
534 (dolist (parameter parameters)
535 (when (and (null found)
537 (eq classification (nth 0 parameter))
538 (eq check (nth 1 parameter)))
542 (defun spam-group-spam-processor-report-gmane-p (group)
543 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
545 (defun spam-group-spam-processor-bogofilter-p (group)
546 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
548 (defun spam-group-spam-processor-blacklist-p (group)
549 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
551 (defun spam-group-spam-processor-ifile-p (group)
552 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
554 (defun spam-group-ham-processor-ifile-p (group)
555 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
557 (defun spam-group-spam-processor-spamoracle-p (group)
558 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
560 (defun spam-group-ham-processor-bogofilter-p (group)
561 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
563 (defun spam-group-spam-processor-stat-p (group)
564 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
566 (defun spam-group-ham-processor-stat-p (group)
567 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
569 (defun spam-group-ham-processor-whitelist-p (group)
570 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
572 (defun spam-group-ham-processor-BBDB-p (group)
573 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
575 (defun spam-group-ham-processor-copy-p (group)
576 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
578 (defun spam-group-ham-processor-spamoracle-p (group)
579 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
581 (defun spam-report-articles-gmane (n)
582 "Report the current message as spam.
583 Respects the process/prefix convention."
585 (dolist (article (gnus-summary-work-articles n))
586 (gnus-summary-remove-process-mark article)
587 (spam-report-gmane article)))
589 ;;; Summary entry and exit processing.
591 (defun spam-summary-prepare ()
592 (setq spam-old-ham-articles
593 (spam-list-articles gnus-newsgroup-articles 'ham))
594 (setq spam-old-spam-articles
595 (spam-list-articles gnus-newsgroup-articles 'spam))
596 (spam-mark-junk-as-spam-routine))
598 ;; The spam processors are invoked for any group, spam or ham or neither
599 (defun spam-summary-prepare-exit ()
600 (unless gnus-group-is-exiting-without-update-p
601 (gnus-message 6 "Exiting summary buffer and applying spam rules")
603 ;; first of all, unregister any articles that are no longer ham or spam
604 ;; we have to iterate over the processors, or else we'll be too slow
605 (dolist (classification '(spam ham))
606 (let* ((old-articles (if (eq classification 'spam)
607 spam-old-spam-articles
608 spam-old-ham-articles))
609 (new-articles (spam-list-articles
610 gnus-newsgroup-articles
612 (changed-articles (gnus-set-difference old-articles new-articles)))
613 ;; now that we have the changed articles, we go through the processors
614 (dolist (processor-param spam-list-of-processors)
615 (let ((processor (nth 0 processor-param))
616 (processor-classification (nth 1 processor-param))
617 (check (nth 2 processor-param))
619 (dolist (article changed-articles)
620 (let ((id (spam-fetch-field-message-id-fast article)))
621 (when (spam-log-unregistration-needed-p
622 id 'process classification check)
623 (push article unregister-list))))
624 ;; call spam-register-routine with specific articles to unregister,
625 ;; when there are articles to unregister and the check is enabled
626 (when (and unregister-list (symbol-value check))
627 (spam-register-routine classification check t unregister-list))))))
629 ;; find all the spam processors applicable to this group
630 (dolist (processor-param spam-list-of-processors)
631 (let ((processor (nth 0 processor-param))
632 (classification (nth 1 processor-param))
633 (check (nth 2 processor-param)))
634 (when (and (eq 'spam classification)
635 (spam-group-processor-p gnus-newsgroup-name processor))
636 (spam-register-routine classification check))))
638 (if spam-move-spam-nonspam-groups-only
639 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
640 (spam-mark-spam-as-expired-and-move-routine
641 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
642 (gnus-message 5 "Marking spam as expired and moving it to %s"
644 (spam-mark-spam-as-expired-and-move-routine
645 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
647 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
648 ;; expire spam, in case the above did not expire them
649 (gnus-message 5 "Marking spam as expired without moving it")
650 (spam-mark-spam-as-expired-and-move-routine nil)
652 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
653 (and (spam-group-spam-contents-p gnus-newsgroup-name)
654 spam-process-ham-in-spam-groups)
655 spam-process-ham-in-nonham-groups)
656 ;; find all the ham processors applicable to this group
657 (dolist (processor-param spam-list-of-processors)
658 (let ((processor (nth 0 processor-param))
659 (classification (nth 1 processor-param))
660 (check (nth 2 processor-param)))
661 (when (and (eq 'ham classification)
662 (spam-group-processor-p gnus-newsgroup-name processor))
663 (spam-register-routine classification check)))))
665 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
666 (gnus-message 5 "Copying ham")
667 (spam-ham-copy-routine
668 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
670 ;; now move all ham articles out of spam groups
671 (when (spam-group-spam-contents-p gnus-newsgroup-name)
672 (gnus-message 5 "Moving ham messages from spam group")
673 (spam-ham-move-routine
674 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
676 (setq spam-old-ham-articles nil)
677 (setq spam-old-spam-articles nil))
679 (defun spam-mark-junk-as-spam-routine ()
680 ;; check the global list of group names spam-junk-mailgroups and the
682 (when (spam-group-spam-contents-p gnus-newsgroup-name)
683 (gnus-message 5 "Marking %s articles as spam"
684 (if spam-mark-only-unseen-as-spam
687 (let ((articles (if spam-mark-only-unseen-as-spam
688 gnus-newsgroup-unseen
689 gnus-newsgroup-unreads)))
690 (dolist (article articles)
691 (gnus-summary-mark-article article gnus-spam-mark)))))
693 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
694 (if (and (car-safe groups) (listp (car-safe groups)))
695 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
696 (gnus-summary-kill-process-mark)
697 (let ((articles gnus-newsgroup-articles)
698 (backend-supports-deletions
699 (gnus-check-backend-function
700 'request-move-article gnus-newsgroup-name))
701 article tomove deletep)
702 (dolist (article articles)
703 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
704 (gnus-summary-mark-article article gnus-expirable-mark)
705 (push article tomove)))
707 ;; now do the actual copies
708 (dolist (group groups)
711 (dolist (article tomove)
712 (gnus-summary-set-process-mark article))
714 (if (or (not backend-supports-deletions)
715 (> (length groups) 1))
717 (gnus-summary-copy-article nil group)
719 (gnus-summary-move-article nil group)))))
721 ;; now delete the articles, if there was a copy done, and the
723 (when (and deletep backend-supports-deletions)
724 (dolist (article tomove)
725 (gnus-summary-set-process-mark article))
727 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
728 (gnus-summary-delete-article nil))))
730 (gnus-summary-yank-process-mark))))
732 (defun spam-ham-copy-or-move-routine (copy groups)
733 (gnus-summary-kill-process-mark)
734 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
735 (backend-supports-deletions
736 (gnus-check-backend-function
737 'request-move-article gnus-newsgroup-name))
738 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
739 article mark todo deletep respool)
741 (when (member 'respool groups)
742 (setq respool t) ; boolean for later
743 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
745 ;; now do the actual move
746 (dolist (group groups)
747 (when (and todo (stringp group))
748 (dolist (article todo)
749 (when spam-mark-ham-unread-before-move-from-spam-group
750 (gnus-summary-mark-article article gnus-unread-mark))
751 (gnus-summary-set-process-mark article))
753 (if respool ; respooling is with a "fake" group
754 (let ((spam-split-disabled
755 (or spam-split-disabled
756 spam-disable-spam-split-during-ham-respool)))
757 (gnus-summary-respool-article nil respool-method))
758 (if (or (not backend-supports-deletions) ; else, we are not respooling
759 (> (length groups) 1))
760 (progn ; if copying, copy and set deletep
761 (gnus-summary-copy-article nil group)
763 (gnus-summary-move-article nil group))))) ; else move articles
765 ;; now delete the articles, unless a) copy is t, and there was a copy done
766 ;; b) a move was done to a single group
767 ;; c) backend-supports-deletions is nil
769 (when (and deletep backend-supports-deletions)
770 (dolist (article todo)
771 (gnus-summary-set-process-mark article))
773 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
774 (gnus-summary-delete-article nil))))))
776 (gnus-summary-yank-process-mark))
778 (defun spam-ham-copy-routine (&rest groups)
779 (if (and (car-safe groups) (listp (car-safe groups)))
780 (apply 'spam-ham-copy-routine (car groups))
781 (spam-ham-copy-or-move-routine t groups)))
783 (defun spam-ham-move-routine (&rest groups)
784 (if (and (car-safe groups) (listp (car-safe groups)))
785 (apply 'spam-ham-move-routine (car groups))
786 (spam-ham-copy-or-move-routine nil groups)))
788 (defun spam-get-article-as-string (article)
789 (when (numberp article)
791 (gnus-request-article-this-buffer
797 ;; (defun spam-get-article-as-filename (article)
798 ;; (let ((article-filename))
799 ;; (when (numberp article)
800 ;; (nnml-possibly-change-directory
801 ;; (gnus-group-real-name gnus-newsgroup-name))
802 ;; (setq article-filename (expand-file-name
803 ;; (int-to-string article) nnml-current-directory)))
804 ;; (if (file-exists-p article-filename)
808 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
809 "Fetch a field quickly, using the internal gnus-data-list function"
810 (when (numberp article)
811 (let* ((data-header (or prepared-data-header
812 (spam-fetch-article-header article))))
813 (if (arrayp data-header)
816 (mail-header-from data-header))
817 ((equal field 'message-id)
818 (mail-header-message-id data-header))
819 ((equal field 'subject)
820 (mail-header-subject data-header))
821 ((equal field 'references)
822 (mail-header-references data-header))
824 (mail-header-date data-header))
826 (mail-header-xref data-header))
827 ((equal field 'extra)
828 (mail-header-extra data-header))
831 (gnus-error 5 "Article %d has a nil data header" article)))))
833 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
834 (spam-fetch-field-fast article 'from prepared-data-header))
836 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
837 (spam-fetch-field-fast article 'subject prepared-data-header))
839 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
840 (spam-fetch-field-fast article 'message-id prepared-data-header))
842 (defun spam-generate-fake-headers (article)
843 (let ((dh (spam-fetch-article-header article)))
847 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
848 "Date: %s\nReferences: %s\nXref: %s\n")
849 (spam-fetch-field-fast article 'from dh)
850 (spam-fetch-field-fast article 'subject dh)
851 (spam-fetch-field-fast article 'message-id dh)
852 (spam-fetch-field-fast article 'date dh)
853 (spam-fetch-field-fast article 'references dh)
854 (spam-fetch-field-fast article 'xref dh))
855 (when (spam-fetch-field-fast article 'extra dh)
856 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
859 "spam-generate-fake-headers: article %d didn't have a valid header"
862 (defun spam-fetch-article-header (article)
864 (set-buffer gnus-summary-buffer)
865 (nth 3 (assq article gnus-newsgroup-data))))
868 ;;;; Spam determination.
870 (defvar spam-list-of-checks
871 '((spam-use-blacklist . spam-check-blacklist)
872 (spam-use-regex-headers . spam-check-regex-headers)
873 (spam-use-regex-body . spam-check-regex-body)
874 (spam-use-whitelist . spam-check-whitelist)
875 (spam-use-BBDB . spam-check-BBDB)
876 (spam-use-ifile . spam-check-ifile)
877 (spam-use-spamoracle . spam-check-spamoracle)
878 (spam-use-stat . spam-check-stat)
879 (spam-use-blackholes . spam-check-blackholes)
880 (spam-use-hashcash . spam-check-hashcash)
881 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
882 (spam-use-bogofilter . spam-check-bogofilter))
883 "The spam-list-of-checks list contains pairs associating a
884 parameter variable with a spam checking function. If the
885 parameter variable is true, then the checking function is called,
886 and its value decides what happens. Each individual check may
887 return nil, t, or a mailgroup name. The value nil means that the
888 check does not yield a decision, and so, that further checks are
889 needed. The value t means that the message is definitely not
890 spam, and that further spam checks should be inhibited.
891 Otherwise, a mailgroup name or the symbol 'spam (depending on
892 spam-split-symbolic-return) is returned where the mail should go,
893 and further checks are also inhibited. The usual mailgroup name
894 is the value of `spam-split-group', meaning that the message is
897 (defvar spam-list-of-statistical-checks
904 "The spam-list-of-statistical-checks list contains all the mail
905 splitters that need to have the full message body available.
906 Note that you should fetch extra headers if you don't like this,
907 e.g. fetch the 'Received' header for spam-use-blackholes.")
909 (defun spam-split (&rest specific-checks)
910 "Split this message into the `spam' group if it is spam.
911 This function can be used as an entry in the variable `nnmail-split-fancy',
912 for example like this: (: spam-split). It can take checks as
913 parameters. A string as a parameter will set the
914 spam-split-group to that string.
916 See the Info node `(gnus)Fancy Mail Splitting' for more details."
918 (setq spam-split-last-successful-check nil)
919 (unless spam-split-disabled
920 (let ((spam-split-group-choice spam-split-group))
921 (dolist (check specific-checks)
922 (when (stringp check)
923 (setq spam-split-group-choice check)
924 (setq specific-checks (delq check specific-checks))))
926 (let ((spam-split-group spam-split-group-choice))
929 (dolist (check spam-list-of-statistical-checks)
930 (when (and (symbolp check)
931 (or (symbol-value check)
932 (memq check specific-checks)))
934 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
937 ;; (progn (widen) (debug (buffer-string)))
938 (let ((list-of-checks spam-list-of-checks)
940 (while (and list-of-checks (not decision))
941 (let ((pair (pop list-of-checks)))
943 ;; either, given specific checks, this is one of them
944 (and specific-checks (memq (car pair) specific-checks))
945 ;; or, given no specific checks, spam-use-CHECK is set
946 (and (null specific-checks) (symbol-value (car pair))))
947 (gnus-message 5 "spam-split: calling the %s function"
948 (symbol-name (cdr pair)))
949 (setq decision (funcall (cdr pair)))
950 ;; if we got a decision at all, save the current check
952 (setq spam-split-last-successful-check (car pair)))
954 (when (eq decision 'spam)
955 (unless spam-split-symbolic-return
958 (format "spam-split got %s but %s is nil"
959 (symbol-name decision)
960 (symbol-name spam-split-symbolic-return))))))))
962 (if spam-split-symbolic-return-positive 'ham nil)
965 (defun spam-find-spam ()
966 "This function will detect spam in the current newsgroup using spam-split."
969 (let* ((group gnus-newsgroup-name)
970 (autodetect (gnus-parameter-spam-autodetect group))
971 (methods (gnus-parameter-spam-autodetect-methods group))
972 (first-method (nth 0 methods))
973 (articles (if spam-autodetect-recheck-messages
974 gnus-newsgroup-articles
975 gnus-newsgroup-unseen))
976 article-cannot-be-faked)
978 (dolist (check spam-list-of-statistical-checks)
979 (when (and (symbolp check)
980 (memq check methods))
981 (setq article-cannot-be-faked t)
984 (when (memq 'default methods)
985 (setq article-cannot-be-faked t))
987 (when (and autodetect
988 (not (equal first-method 'none)))
991 (let ((id (spam-fetch-field-message-id-fast article))
992 (subject (spam-fetch-field-subject-fast article))
993 (sender (spam-fetch-field-from-fast article))
997 (gnus-error 5 "Article %d has no message ID!" article))
999 (when (and id spam-log-to-registry)
1000 (setq registry-lookup (spam-log-registration-type id 'incoming))
1001 (when registry-lookup
1004 "spam-find-spam: message %s was already registered incoming"
1007 (let* ((spam-split-symbolic-return t)
1008 (spam-split-symbolic-return-positive t)
1009 (fake-headers (spam-generate-fake-headers article))
1013 (if article-cannot-be-faked
1014 (gnus-request-article-this-buffer
1017 ;; else, we fake the article
1018 (when fake-headers (insert fake-headers)))
1019 (if (or (null first-method)
1020 (equal first-method 'default))
1022 (apply 'spam-split methods))))))
1023 (if (equal split-return 'spam)
1024 (gnus-summary-mark-article article gnus-spam-mark))
1026 (when (and id split-return spam-log-to-registry)
1027 (when (zerop (gnus-registry-group-count id))
1028 (gnus-registry-add-group
1029 id group subject sender))
1031 (unless registry-lookup
1032 (spam-log-processing-to-registry
1036 spam-split-last-successful-check
1040 (defvar spam-registration-functions
1041 ;; first the ham register, second the spam register function
1042 ;; third the ham unregister, fourth the spam unregister function
1043 '((spam-use-blacklist nil
1044 spam-blacklist-register-routine
1046 spam-blacklist-unregister-routine)
1047 (spam-use-whitelist spam-whitelist-register-routine
1049 spam-whitelist-unregister-routine
1051 (spam-use-BBDB spam-BBDB-register-routine
1053 spam-BBDB-unregister-routine
1055 (spam-use-ifile spam-ifile-register-ham-routine
1056 spam-ifile-register-spam-routine
1057 spam-ifile-unregister-ham-routine
1058 spam-ifile-unregister-spam-routine)
1059 (spam-use-spamoracle spam-spamoracle-learn-ham
1060 spam-spamoracle-learn-spam
1061 spam-spamoracle-unlearn-ham
1062 spam-spamoracle-unlearn-spam)
1063 (spam-use-stat spam-stat-register-ham-routine
1064 spam-stat-register-spam-routine
1065 spam-stat-unregister-ham-routine
1066 spam-stat-unregister-spam-routine)
1067 ;; note that spam-use-gmane is not a legitimate check
1069 spam-report-gmane-register-routine
1070 ;; does Gmane support unregistration?
1073 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1074 spam-bogofilter-register-spam-routine
1075 spam-bogofilter-unregister-ham-routine
1076 spam-bogofilter-unregister-spam-routine))
1077 "The spam-registration-functions list contains pairs
1078 associating a parameter variable with the ham and spam
1079 registration functions, and the ham and spam unregistration
1082 (defun spam-classification-valid-p (classification)
1083 (or (eq classification 'spam)
1084 (eq classification 'ham)))
1086 (defun spam-process-type-valid-p (process-type)
1087 (or (eq process-type 'incoming)
1088 (eq process-type 'process)))
1090 (defun spam-registration-check-valid-p (check)
1091 (assoc check spam-registration-functions))
1093 (defun spam-unregistration-check-valid-p (check)
1094 (assoc check spam-registration-functions))
1096 (defun spam-registration-function (classification check)
1097 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1098 (if (eq classification 'spam)
1102 (defun spam-unregistration-function (classification check)
1103 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1104 (if (eq classification 'spam)
1108 (defun spam-list-articles (articles classification)
1109 (let ((mark-check (if (eq classification 'spam)
1110 'spam-group-spam-mark-p
1111 'spam-group-ham-mark-p))
1112 list mark-cache-yes mark-cache-no)
1113 (dolist (article articles)
1114 (let ((mark (gnus-summary-article-mark article)))
1115 (unless (memq mark mark-cache-no)
1116 (if (memq mark mark-cache-yes)
1118 ;; else, we have to actually check the mark
1119 (if (funcall mark-check
1124 (push mark mark-cache-yes))
1125 (push mark mark-cache-no))))))
1128 (defun spam-register-routine (classification
1130 &optional unregister
1132 (when (and (spam-classification-valid-p classification)
1133 (spam-registration-check-valid-p check))
1134 (let* ((register-function
1135 (spam-registration-function classification check))
1136 (unregister-function
1137 (spam-unregistration-function classification check))
1138 (run-function (if unregister
1141 (log-function (if unregister
1142 'spam-log-undo-registration
1143 'spam-log-processing-to-registry))
1147 ;; make list of articles, using specific-articles if given
1148 (setq articles (or specific-articles
1150 gnus-newsgroup-articles
1153 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1154 (if unregister "Unregistering" "Registering")
1156 (if specific-articles "specific" "")
1157 (symbol-name classification)
1158 (symbol-name check))
1159 (funcall run-function articles)
1160 ;; now log all the registrations (or undo them, depending on unregister)
1161 (dolist (article articles)
1162 (funcall log-function
1163 (spam-fetch-field-message-id-fast article)
1167 gnus-newsgroup-name))))))
1169 ;;; log a ham- or spam-processor invocation to the registry
1170 (defun spam-log-processing-to-registry (id type classification check group)
1171 (when spam-log-to-registry
1172 (if (and (stringp id)
1174 (spam-process-type-valid-p type)
1175 (spam-classification-valid-p classification)
1176 (spam-registration-check-valid-p check))
1177 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1178 (cell (list classification check group)))
1179 (push cell cell-list)
1180 (gnus-registry-store-extra-entry
1185 (gnus-error 5 (format "%s called with bad ID, type, classification, check, or group"
1186 "spam-log-processing-to-registry")))))
1188 ;;; check if a ham- or spam-processor registration has been done
1189 (defun spam-log-registered-p (id type)
1190 (when spam-log-to-registry
1191 (if (and (stringp id)
1192 (spam-process-type-valid-p type))
1193 (cdr-safe (gnus-registry-fetch-extra id type))
1195 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1196 "spam-log-registered-p"))
1199 ;;; check what a ham- or spam-processor registration says
1200 ;;; returns nil if conflicting registrations are found
1201 (defun spam-log-registration-type (id type)
1204 (dolist (reg (spam-log-registered-p id type))
1205 (let ((classification (nth 0 reg)))
1206 (when (spam-classification-valid-p classification)
1208 (not (eq classification decision)))
1209 (setq count (+ 1 count)))
1210 (setq decision classification))))
1215 ;;; check if a ham- or spam-processor registration needs to be undone
1216 (defun spam-log-unregistration-needed-p (id type classification check)
1217 (when spam-log-to-registry
1218 (if (and (stringp id)
1219 (spam-process-type-valid-p type)
1220 (spam-classification-valid-p classification)
1221 (spam-registration-check-valid-p check))
1222 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1224 (dolist (cell cell-list)
1226 (when (and (eq classification (nth 0 cell))
1227 (eq check (nth 1 cell)))
1231 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1232 "spam-log-unregistration-needed-p"))
1236 ;;; undo a ham- or spam-processor registration (the group is not used)
1237 (defun spam-log-undo-registration (id type classification check &optional group)
1238 (when (and spam-log-to-registry
1239 (spam-log-unregistration-needed-p id type classification check))
1240 (if (and (stringp id)
1241 (spam-process-type-valid-p type)
1242 (spam-classification-valid-p classification)
1243 (spam-registration-check-valid-p check))
1244 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1245 new-cell-list found)
1246 (dolist (cell cell-list)
1247 (unless (and (eq classification (nth 0 cell))
1248 (eq check (nth 1 cell)))
1249 (push cell new-cell-list)))
1250 (gnus-registry-store-extra-entry
1255 (gnus-error 5 (format "%s called with bad ID, type, check, or group"
1256 "spam-log-undo-registration"))
1259 ;;; set up IMAP widening if it's necessary
1260 (defun spam-setup-widening ()
1261 (dolist (check spam-list-of-statistical-checks)
1262 (when (symbol-value check)
1263 (setq nnimap-split-download-body-default t))))
1268 (defun spam-check-regex-body ()
1269 (let ((spam-regex-headers-ham spam-regex-body-ham)
1270 (spam-regex-headers-spam spam-regex-body-spam))
1271 (spam-check-regex-headers t)))
1276 (defun spam-check-regex-headers (&optional body)
1277 (let ((type (if body "body" "header"))
1278 (spam-split-group (if spam-split-symbolic-return
1282 (dolist (h-regex spam-regex-headers-ham)
1284 (goto-char (point-min))
1285 (when (re-search-forward h-regex nil t)
1286 (message "Ham regex %s search positive." type)
1288 (dolist (s-regex spam-regex-headers-spam)
1290 (goto-char (point-min))
1291 (when (re-search-forward s-regex nil t)
1292 (message "Spam regex %s search positive." type)
1294 (setq ret spam-split-group))))
1300 (defun spam-reverse-ip-string (ip)
1302 (mapconcat 'identity
1303 (nreverse (split-string ip "\\."))
1306 (defun spam-check-blackholes ()
1307 "Check the Received headers for blackholed relays."
1308 (let ((headers (message-fetch-field "received"))
1309 (spam-split-group (if spam-split-symbolic-return
1316 (goto-char (point-min))
1317 (gnus-message 5 "Checking headers for relay addresses")
1318 (while (re-search-forward
1319 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1320 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1321 (push (spam-reverse-ip-string (match-string 1))
1323 (dolist (server spam-blackhole-servers)
1325 (unless (and spam-blackhole-good-server-regex
1326 ;; match the good-server-regex against the reversed (again) IP string
1328 spam-blackhole-good-server-regex
1329 (spam-reverse-ip-string ip)))
1331 (let ((query-string (concat ip "." server)))
1333 (let ((query-result (query-dig query-string)))
1335 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1337 (push (list ip server query-result)
1339 ;; else, if not using dig.el
1340 (when (query-dns query-string)
1341 (gnus-message 5 "positive blackhole check")
1342 (push (list ip server (query-dns query-string 'TXT))
1353 (defun spam-check-hashcash ()
1354 "Check the headers for hashcash payments."
1355 (mail-check-payment))) ;mail-check-payment returns a boolean
1358 (defalias 'mail-check-payment 'ignore)
1359 (defalias 'spam-check-hashcash 'ignore))))
1363 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1364 ;;; <sacha@giotto.sj.ru>
1366 ;; all this is done inside a condition-case to trap errors
1373 ;; when the BBDB changes, we want to clear out our cache
1374 (defun spam-clear-cache-BBDB (&rest immaterial)
1375 (spam-clear-cache 'spam-use-BBDB))
1377 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1379 (defun spam-enter-ham-BBDB (addresses &optional remove)
1380 "Enter an address into the BBDB; implies ham (non-spam) sender"
1381 (dolist (from addresses)
1382 (when (stringp from)
1383 (let* ((parsed-address (gnus-extract-address-components from))
1384 (name (or (nth 0 parsed-address) "Ham Sender"))
1385 (remove-function (if remove
1386 'bbdb-delete-record-internal
1388 (net-address (nth 1 parsed-address))
1389 (record (and net-address
1390 (bbdb-search-simple nil net-address))))
1392 (gnus-message 5 "%s address %s %s BBDB"
1393 (if remove "Deleting" "Adding")
1395 (if remove "from" "to"))
1397 (funcall remove-function record)
1398 (bbdb-create-internal name nil net-address nil nil
1399 "ham sender added by spam.el")))))))
1401 (defun spam-BBDB-register-routine (articles &optional unregister)
1403 (dolist (article articles)
1404 (when (stringp (spam-fetch-field-from-fast article))
1405 (push (spam-fetch-field-from-fast article) addresses)))
1406 ;; now do the register/unregister action
1407 (spam-enter-ham-BBDB addresses unregister)))
1409 (defun spam-BBDB-unregister-routine (articles)
1410 (spam-BBDB-register-routine articles t))
1412 (defun spam-check-BBDB ()
1413 "Mail from people in the BBDB is classified as ham or non-spam"
1414 (let ((who (message-fetch-field "from"))
1415 (spam-split-group (if spam-split-symbolic-return
1418 bbdb-cache bbdb-hashtable)
1419 (when spam-cache-lookups
1420 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1423 ;; this is the expanded (bbdb-hashtable) macro
1424 ;; without the debugging support
1425 (with-current-buffer (bbdb-buffer)
1427 (save-window-excursion
1428 (bbdb-records nil t)
1430 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1432 (setq who (nth 1 (gnus-extract-address-components who)))
1434 (if spam-cache-lookups
1436 (intern-soft who bbdb-cache))
1437 (bbdb-search-simple nil who))
1439 (if spam-use-BBDB-exclusive
1444 (defalias 'bbdb-search-simple 'ignore)
1445 (defalias 'bbdb-records 'ignore)
1446 (defalias 'bbdb-buffer 'ignore)
1447 (defalias 'spam-check-BBDB 'ignore)
1448 (defalias 'spam-BBDB-register-routine 'ignore)
1449 (defalias 'spam-enter-ham-BBDB 'ignore)
1450 (defalias 'bbdb-create-internal 'ignore)
1451 (defalias 'bbdb-delete-record-internal 'ignore)
1452 (defalias 'bbdb-records 'ignore))))
1457 ;;; check the ifile backend; return nil if the mail was NOT classified
1460 (defun spam-get-ifile-database-parameter ()
1461 "Get the command-line parameter for ifile's database from
1462 spam-ifile-database-path."
1463 (if spam-ifile-database-path
1464 (format "--db-file=%s" spam-ifile-database-path)
1467 (defun spam-check-ifile ()
1468 "Check the ifile backend for the classification of this message."
1469 (let ((article-buffer-name (buffer-name))
1470 (spam-split-group (if spam-split-symbolic-return
1475 (let ((temp-buffer-name (buffer-name))
1476 (db-param (spam-get-ifile-database-parameter)))
1478 (set-buffer article-buffer-name)
1479 (apply 'call-process-region
1480 (point-min) (point-max) spam-ifile-path
1481 nil temp-buffer-name nil "-c"
1482 (if db-param `(,db-param "-q") `("-q"))))
1483 ;; check the return now (we're back in the temp buffer)
1484 (goto-char (point-min))
1486 (setq category (buffer-substring (point) (point-at-eol))))
1487 (when (not (zerop (length category))) ; we need a category here
1488 (if spam-ifile-all-categories
1489 (setq return category)
1490 ;; else, if spam-ifile-all-categories is not set...
1491 (when (string-equal spam-ifile-spam-category category)
1492 (setq return spam-split-group)))))) ; note return is nil otherwise
1495 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1496 "Register an article, given as a string, with a category.
1497 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1498 (let ((category (or category gnus-newsgroup-name))
1499 (add-or-delete-option (if unregister "-d" "-i"))
1500 (db (spam-get-ifile-database-parameter))
1503 (dolist (article articles)
1504 (let ((article-string (spam-get-article-as-string article)))
1505 (when (stringp article-string)
1506 (insert article-string))))
1507 (apply 'call-process-region
1508 (point-min) (point-max) spam-ifile-path
1510 add-or-delete-option category
1511 (if db `(,db "-h") `("-h"))))))
1513 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1514 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1516 (defun spam-ifile-unregister-spam-routine (articles)
1517 (spam-ifile-register-spam-routine articles t))
1519 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1520 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1522 (defun spam-ifile-unregister-ham-routine (articles)
1523 (spam-ifile-register-ham-routine articles t))
1530 (let ((spam-stat-install-hooks nil))
1531 (require 'spam-stat))
1533 (defun spam-check-stat ()
1534 "Check the spam-stat backend for the classification of this message"
1535 (let ((spam-split-group (if spam-split-symbolic-return
1538 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1539 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1541 (spam-stat-split-fancy)))
1543 (defun spam-stat-register-spam-routine (articles &optional unregister)
1544 (dolist (article articles)
1545 (let ((article-string (spam-get-article-as-string article)))
1547 (insert article-string)
1549 (spam-stat-buffer-change-to-non-spam)
1550 (spam-stat-buffer-is-spam))))))
1552 (defun spam-stat-unregister-spam-routine (articles)
1553 (spam-stat-register-spam-routine articles t))
1555 (defun spam-stat-register-ham-routine (articles &optional unregister)
1556 (dolist (article articles)
1557 (let ((article-string (spam-get-article-as-string article)))
1559 (insert article-string)
1561 (spam-stat-buffer-change-to-spam)
1562 (spam-stat-buffer-is-non-spam))))))
1564 (defun spam-stat-unregister-ham-routine (articles)
1565 (spam-stat-register-ham-routine articles t))
1567 (defun spam-maybe-spam-stat-load ()
1568 (when spam-use-stat (spam-stat-load)))
1570 (defun spam-maybe-spam-stat-save ()
1571 (when spam-use-stat (spam-stat-save))))
1574 (defalias 'spam-stat-load 'ignore)
1575 (defalias 'spam-stat-save 'ignore)
1576 (defalias 'spam-maybe-spam-stat-load 'ignore)
1577 (defalias 'spam-maybe-spam-stat-save 'ignore)
1578 (defalias 'spam-stat-register-ham-routine 'ignore)
1579 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1580 (defalias 'spam-stat-register-spam-routine 'ignore)
1581 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1582 (defalias 'spam-stat-buffer-is-spam 'ignore)
1583 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1584 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1585 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1586 (defalias 'spam-stat-split-fancy 'ignore)
1587 (defalias 'spam-check-stat 'ignore))))
1591 ;;;; Blacklists and whitelists.
1593 (defvar spam-whitelist-cache nil)
1594 (defvar spam-blacklist-cache nil)
1596 (defun spam-kill-whole-line ()
1598 (let ((kill-whole-line t))
1601 ;;; address can be a list, too
1602 (defun spam-enter-whitelist (address &optional remove)
1603 "Enter ADDRESS (list or single) into the whitelist.
1604 With a non-nil REMOVE, remove them."
1605 (interactive "sAddress: ")
1606 (spam-enter-list address spam-whitelist remove)
1607 (setq spam-whitelist-cache nil)
1608 (spam-clear-cache 'spam-use-whitelist))
1610 ;;; address can be a list, too
1611 (defun spam-enter-blacklist (address &optional remove)
1612 "Enter ADDRESS (list or single) into the blacklist.
1613 With a non-nil REMOVE, remove them."
1614 (interactive "sAddress: ")
1615 (spam-enter-list address spam-blacklist remove)
1616 (setq spam-blacklist-cache nil)
1617 (spam-clear-cache 'spam-use-whitelist))
1619 (defun spam-enter-list (addresses file &optional remove)
1620 "Enter ADDRESSES into the given FILE.
1621 Either the whitelist or the blacklist files can be used. With
1622 REMOVE not nil, remove the ADDRESSES."
1623 (if (stringp addresses)
1624 (spam-enter-list (list addresses) file remove)
1625 ;; else, we have a list of addresses here
1626 (unless (file-exists-p (file-name-directory file))
1627 (make-directory (file-name-directory file) t))
1630 (find-file-noselect file))
1631 (dolist (a addresses)
1633 (goto-char (point-min))
1634 (if (re-search-forward (regexp-quote a) nil t)
1635 ;; found the address
1637 (spam-kill-whole-line))
1638 ;; else, the address was not found
1640 (goto-char (point-max))
1646 (defun spam-filelist-build-cache (type)
1647 (let ((cache (if (eq type 'spam-use-blacklist)
1648 spam-blacklist-cache
1649 spam-whitelist-cache))
1651 (unless (gethash type spam-caches)
1653 (let ((address (pop cache)))
1654 (unless (zerop (length address)) ; 0 for a nil address too
1655 (setq address (regexp-quote address))
1656 ;; fix regexp-quote's treatment of user-intended regexes
1657 (while (string-match "\\\\\\*" address)
1658 (setq address (replace-match ".*" t t address))))
1659 (push address parsed-cache)))
1660 (puthash type parsed-cache spam-caches))))
1662 (defun spam-filelist-check-cache (type from)
1663 (when (stringp from)
1664 (spam-filelist-build-cache type)
1666 (dolist (address (gethash type spam-caches))
1667 (when (and address (string-match address from))
1672 ;;; returns t if the sender is in the whitelist, nil or
1673 ;;; spam-split-group otherwise
1674 (defun spam-check-whitelist ()
1675 ;; FIXME! Should it detect when file timestamps change?
1676 (let ((spam-split-group (if spam-split-symbolic-return
1679 (unless spam-whitelist-cache
1680 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1681 (if (spam-from-listed-p 'spam-use-whitelist)
1683 (if spam-use-whitelist-exclusive
1687 (defun spam-check-blacklist ()
1688 ;; FIXME! Should it detect when file timestamps change?
1689 (let ((spam-split-group (if spam-split-symbolic-return
1692 (unless spam-blacklist-cache
1693 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1694 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1696 (defun spam-parse-list (file)
1697 (when (file-readable-p file)
1698 (let (contents address)
1700 (insert-file-contents file)
1702 (setq address (buffer-substring (point) (point-at-eol)))
1704 ;; insert the e-mail address if detected, otherwise the raw data
1705 (unless (zerop (length address))
1706 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1707 (push (or pure-address address) contents)))))
1708 (nreverse contents))))
1710 (defun spam-from-listed-p (type)
1711 (let ((from (message-fetch-field "from"))
1713 (spam-filelist-check-cache type from)))
1715 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1716 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1717 (declassification (if blacklist 'ham 'spam))
1719 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1721 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1722 from addresses unregister-list)
1723 (dolist (article articles)
1724 (let ((from (spam-fetch-field-from-fast article))
1725 (id (spam-fetch-field-message-id-fast article))
1727 (when (stringp from)
1728 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1729 (when (and (not sender-ignored)
1730 (stringp ignore-regex)
1731 (string-match ignore-regex from))
1732 (setq sender-ignored t)))
1733 ;; remember the messages we need to unregister, unless remove is set
1736 (spam-log-unregistration-needed-p
1737 id 'process declassification de-symbol))
1738 (push from unregister-list))
1739 (unless sender-ignored
1740 (push from addresses)))))
1743 (funcall enter-function addresses t) ; unregister all these addresses
1744 ;; else, register normally and unregister what we need to
1745 (funcall remove-function unregister-list t)
1746 (dolist (article unregister-list)
1747 (spam-log-undo-registration
1748 (spam-fetch-field-message-id-fast article)
1752 (funcall enter-function addresses nil))))
1754 (defun spam-blacklist-unregister-routine (articles)
1755 (spam-blacklist-register-routine articles t))
1757 (defun spam-blacklist-register-routine (articles &optional unregister)
1758 (spam-filelist-register-routine articles t unregister))
1760 (defun spam-whitelist-unregister-routine (articles)
1761 (spam-whitelist-register-routine articles t))
1763 (defun spam-whitelist-register-routine (articles &optional unregister)
1764 (spam-filelist-register-routine articles nil unregister))
1767 ;;;; Spam-report glue
1768 (defun spam-report-gmane-register-routine (articles)
1770 (apply 'spam-report-gmane articles)))
1774 (defun spam-check-bogofilter-headers (&optional score)
1775 (let ((header (message-fetch-field spam-bogofilter-header))
1776 (spam-split-group (if spam-split-symbolic-return
1779 (when header ; return nil when no header
1780 (if score ; scoring mode
1781 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1782 (match-string 1 header)
1784 ;; spam detection mode
1785 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1787 spam-split-group)))))
1789 ;; return something sensible if the score can't be determined
1790 (defun spam-bogofilter-score ()
1791 "Get the Bogofilter spamicity score"
1793 (save-window-excursion
1794 (gnus-summary-show-article t)
1795 (set-buffer gnus-article-buffer)
1796 (let ((score (or (spam-check-bogofilter-headers t)
1797 (spam-check-bogofilter t))))
1798 (message "Spamicity score %s" score)
1800 (gnus-summary-show-article)))
1802 (defun spam-check-bogofilter (&optional score)
1803 "Check the Bogofilter backend for the classification of this message"
1804 (let ((article-buffer-name (buffer-name))
1805 (db spam-bogofilter-database-directory)
1808 (let ((temp-buffer-name (buffer-name)))
1810 (set-buffer article-buffer-name)
1811 (apply 'call-process-region
1812 (point-min) (point-max)
1813 spam-bogofilter-path
1814 nil temp-buffer-name nil
1815 (if db `("-d" ,db "-v") `("-v"))))
1816 (setq return (spam-check-bogofilter-headers score))))
1819 (defun spam-bogofilter-register-with-bogofilter (articles
1821 &optional unregister)
1822 "Register an article, given as a string, as spam or non-spam."
1823 (dolist (article articles)
1824 (let ((article-string (spam-get-article-as-string article))
1825 (db spam-bogofilter-database-directory)
1826 (switch (if unregister
1828 spam-bogofilter-spam-strong-switch
1829 spam-bogofilter-ham-strong-switch)
1831 spam-bogofilter-spam-switch
1832 spam-bogofilter-ham-switch))))
1833 (when (stringp article-string)
1835 (insert article-string)
1837 (apply 'call-process-region
1838 (point-min) (point-max)
1839 spam-bogofilter-path
1841 (if db `("-d" ,db "-v") `("-v"))))))))
1843 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1844 (spam-bogofilter-register-with-bogofilter articles t unregister))
1846 (defun spam-bogofilter-unregister-spam-routine (articles)
1847 (spam-bogofilter-register-spam-routine articles t))
1849 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1850 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1852 (defun spam-bogofilter-unregister-ham-routine (articles)
1853 (spam-bogofilter-register-ham-routine articles t))
1858 (defun spam-check-spamoracle ()
1859 "Run spamoracle on an article to determine whether it's spam."
1860 (let ((article-buffer-name (buffer-name))
1861 (spam-split-group (if spam-split-symbolic-return
1865 (let ((temp-buffer-name (buffer-name)))
1867 (set-buffer article-buffer-name)
1869 (apply 'call-process-region
1870 (point-min) (point-max)
1871 spam-spamoracle-binary
1872 nil temp-buffer-name nil
1873 (if spam-spamoracle-database
1874 `("-f" ,spam-spamoracle-database "mark")
1878 (set-buffer temp-buffer-name)
1879 (goto-char (point-min))
1880 (when (re-search-forward "^X-Spam: yes;" nil t)
1882 (error "Error running spamoracle" status))))))))
1884 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1885 "Run spamoracle in training mode."
1887 (let ((temp-buffer-name (buffer-name)))
1889 (goto-char (point-min))
1890 (dolist (article articles)
1891 (insert (spam-get-article-as-string article)))
1892 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1896 (apply 'call-process-region
1897 (point-min) (point-max)
1898 spam-spamoracle-binary
1899 nil temp-buffer-name nil
1900 (if spam-spamoracle-database
1901 `("-f" ,spam-spamoracle-database
1904 (when (not (eq 0 status))
1905 (error "Error running spamoracle" status)))))))
1907 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1908 (spam-spamoracle-learn articles nil unregister))
1910 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1911 (spam-spamoracle-learn-ham articles t))
1913 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1914 (spam-spamoracle-learn articles t unregister))
1916 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1917 (spam-spamoracle-learn-spam articles t))
1923 (defun spam-initialize ()
1924 "Install the spam.el hooks and do other initialization"
1926 (setq spam-install-hooks t)
1927 ;; TODO: How do we redo this every time spam-face is customized?
1928 (push '((eq mark gnus-spam-mark) . spam-face)
1929 gnus-summary-highlight)
1930 ;; Add hooks for loading and saving the spam stats
1931 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1932 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1933 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1934 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1935 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1936 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1937 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
1939 (defun spam-unload-hook ()
1940 "Uninstall the spam.el hooks"
1942 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1943 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1944 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1945 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1946 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1947 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1948 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1950 (when spam-install-hooks
1955 ;;; spam.el ends here