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
42 (eval-when-compile (require 'cl))
46 (require 'gnus-uu) ; because of key prefix issues
47 ;;; for the definitions of group content classification and spam processors
49 (require 'message) ;for the message-fetch-field functions
51 ;; for nnimap-split-download-body-default
52 (eval-when-compile (require 'nnimap))
56 (autoload 'query-dig "dig"))
58 ;; autoload spam-report
60 (autoload 'spam-report-gmane "spam-report"))
62 ;; autoload gnus-registry
64 (autoload 'gnus-registry-group-count "gnus-registry")
65 (autoload 'gnus-registry-add-group "gnus-registry")
66 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
67 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
71 (autoload 'query-dns "dns"))
76 "Spam configuration.")
78 (defcustom spam-directory "~/News/spam/"
79 "Directory for spam whitelists and blacklists."
83 (defcustom spam-move-spam-nonspam-groups-only t
84 "Whether spam should be moved in non-spam groups only.
85 When t, only ham and unclassified groups will have their spam moved
86 to the spam-process-destination. When nil, spam will also be moved from
91 (defcustom spam-process-ham-in-nonham-groups nil
92 "Whether ham should be processed in non-ham groups."
96 (defcustom spam-log-to-registry nil
97 "Whether spam/ham processing should be logged in the registry."
101 (defcustom spam-split-symbolic-return nil
102 "Whether `spam-split' should work with symbols or group names."
106 (defcustom spam-split-symbolic-return-positive nil
107 "Whether `spam-split' should ALWAYS work with symbols or group names.
108 Do not set this if you use `spam-split' in a fancy split
113 (defcustom spam-process-ham-in-spam-groups nil
114 "Whether ham should be processed in spam groups."
118 (defcustom spam-mark-only-unseen-as-spam t
119 "Whether only unseen articles should be marked as spam in spam groups.
120 When nil, all unread articles in a spam group are marked as
121 spam. Set this if you want to leave an article unread in a spam group
122 without losing it to the automatic spam-marking process."
126 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
127 "Whether ham should be marked unread before it's moved.
128 The article is moved out of a spam group according to ham-process-destination.
129 This variable is an official entry in the international Longest Variable Name
134 (defcustom spam-disable-spam-split-during-ham-respool nil
135 "Whether `spam-split' should be ignored while resplitting ham in a process
136 destination. This is useful to prevent ham from ending up in the same spam
137 group after the resplit. Don't set this to t if you have spam-split as the
138 last rule in your split configuration."
142 (defcustom spam-autodetect-recheck-messages nil
143 "Should spam.el recheck all meessages when autodetecting?
144 Normally this is nil, so only unseen messages will be checked."
148 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
149 "The location of the whitelist.
150 The file format is one regular expression per line.
151 The regular expression is matched against the address."
155 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
156 "The location of the blacklist.
157 The file format is one regular expression per line.
158 The regular expression is matched against the address."
162 (defcustom spam-use-dig t
163 "Whether `query-dig' should be used instead of `query-dns'."
167 (defcustom spam-use-blacklist nil
168 "Whether the blacklist should be used by `spam-split'."
172 (defcustom spam-blacklist-ignored-regexes nil
173 "Regular expressions that the blacklist should ignore."
174 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
177 (defcustom spam-use-whitelist nil
178 "Whether the whitelist should be used by `spam-split'."
182 (defcustom spam-use-whitelist-exclusive nil
183 "Whether whitelist-exclusive should be used by `spam-split'.
184 Exclusive whitelisting means that all messages from senders not in the whitelist
185 are considered spam."
189 (defcustom spam-use-blackholes nil
190 "Whether blackholes should be used by `spam-split'."
194 (defcustom spam-use-hashcash nil
195 "Whether hashcash payments should be detected by `spam-split'."
199 (defcustom spam-use-regex-headers nil
200 "Whether a header regular expression match should be used by `spam-split'.
201 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
205 (defcustom spam-use-regex-body nil
206 "Whether a body regular expression match should be used by `spam-split'.
207 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
211 (defcustom spam-use-bogofilter-headers nil
212 "Whether bogofilter headers should be used by `spam-split'.
213 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
217 (defcustom spam-use-bogofilter nil
218 "Whether bogofilter should be invoked by `spam-split'.
219 Enable this if you want Gnus to invoke Bogofilter on new messages."
223 (defcustom spam-use-BBDB nil
224 "Whether BBDB should be used by `spam-split'."
228 (defcustom spam-use-BBDB-exclusive nil
229 "Whether BBDB-exclusive should be used by `spam-split'.
230 Exclusive BBDB means that all messages from senders not in the BBDB are
235 (defcustom spam-use-ifile nil
236 "Whether ifile should be used by `spam-split'."
240 (defcustom spam-use-stat nil
241 "Whether `spam-stat' should be used by `spam-split'."
245 (defcustom spam-use-spamoracle nil
246 "Whether spamoracle should be used by `spam-split'."
250 (defcustom spam-install-hooks (or
254 spam-use-whitelist-exclusive
257 spam-use-regex-headers
259 spam-use-bogofilter-headers
262 spam-use-BBDB-exclusive
266 "Whether the spam hooks should be installed.
267 Default to t if one of the spam-use-* variables is set."
271 (defcustom spam-split-group "spam"
272 "Group name where incoming spam should be put by `spam-split'."
276 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
277 ;;; not regular expressions
278 (defcustom spam-junk-mailgroups (cons
280 '("mail.junk" "poste.pourriel"))
281 "Mailgroups with spam contents.
282 All unmarked article in such group receive the spam mark on group entry."
283 :type '(repeat (string :tag "Group"))
286 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
287 "dev.null.dk" "relays.visi.com")
288 "List of blackhole servers."
289 :type '(repeat (string :tag "Server"))
292 (defcustom spam-blackhole-good-server-regex nil
293 "String matching IP addresses that should not be checked in the blackholes."
294 :type '(radio (const nil)
295 (regexp :format "%t: %v\n" :size 0))
298 (defcustom spam-face 'gnus-splash-face
299 "Face for spam-marked articles."
303 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
304 "Regular expression for positive header spam matches."
305 :type '(repeat (regexp :tag "Regular expression to match spam header"))
308 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
309 "Regular expression for positive header ham matches."
310 :type '(repeat (regexp :tag "Regular expression to match ham header"))
313 (defcustom spam-regex-body-spam '()
314 "Regular expression for positive body spam matches."
315 :type '(repeat (regexp :tag "Regular expression to match spam body"))
318 (defcustom spam-regex-body-ham '()
319 "Regular expression for positive body ham matches."
320 :type '(repeat (regexp :tag "Regular expression to match ham body"))
323 (defgroup spam-ifile nil
324 "Spam ifile configuration."
327 (defcustom spam-ifile-path (exec-installed-p "ifile")
328 "File path of the ifile executable program."
329 :type '(choice (file :tag "Location of ifile")
330 (const :tag "ifile is not installed"))
333 (defcustom spam-ifile-database-path nil
334 "File path of the ifile database."
335 :type '(choice (file :tag "Location of the ifile database")
336 (const :tag "Use the default"))
339 (defcustom spam-ifile-spam-category "spam"
340 "Name of the spam ifile category."
344 (defcustom spam-ifile-ham-category nil
345 "Name of the ham ifile category.
346 If nil, the current group name will be used."
347 :type '(choice (string :tag "Use a fixed category")
348 (const :tag "Use the current group name"))
351 (defcustom spam-ifile-all-categories nil
352 "Whether the ifile check will return all categories, or just spam.
353 Set this to t if you want to use the `spam-split' invocation of ifile as
354 your main source of newsgroup names."
358 (defgroup spam-bogofilter nil
359 "Spam bogofilter configuration."
362 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
363 "File path of the Bogofilter executable program."
364 :type '(choice (file :tag "Location of bogofilter")
365 (const :tag "Bogofilter is not installed"))
366 :group 'spam-bogofilter)
368 (defcustom spam-bogofilter-header "X-Bogosity"
369 "The header that Bogofilter inserts in messages."
371 :group 'spam-bogofilter)
373 (defcustom spam-bogofilter-spam-switch "-s"
374 "The switch that Bogofilter uses to register spam messages."
376 :group 'spam-bogofilter)
378 (defcustom spam-bogofilter-ham-switch "-n"
379 "The switch that Bogofilter uses to register ham messages."
381 :group 'spam-bogofilter)
383 (defcustom spam-bogofilter-spam-strong-switch "-S"
384 "The switch that Bogofilter uses to unregister ham messages."
386 :group 'spam-bogofilter)
388 (defcustom spam-bogofilter-ham-strong-switch "-N"
389 "The switch that Bogofilter uses to unregister spam messages."
391 :group 'spam-bogofilter)
393 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
394 "The regex on `spam-bogofilter-header' for positive spam identification."
396 :group 'spam-bogofilter)
398 (defcustom spam-bogofilter-database-directory nil
399 "Directory path of the Bogofilter databases."
400 :type '(choice (directory
401 :tag "Location of the Bogofilter database directory")
402 (const :tag "Use the default"))
403 :group 'spam-bogofilter)
405 (defgroup spam-spamoracle nil
406 "Spam spamoracle configuration."
409 (defcustom spam-spamoracle-database nil
410 "Location of spamoracle database file. When nil, use the default
411 spamoracle database."
412 :type '(choice (directory :tag "Location of spamoracle database file.")
413 (const :tag "Use the default"))
414 :group 'spam-spamoracle)
416 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
417 "Location of the spamoracle binary."
418 :type '(choice (directory :tag "Location of the spamoracle binary")
419 (const :tag "Use the default"))
420 :group 'spam-spamoracle)
422 ;;; Key bindings for spam control.
424 (gnus-define-keys gnus-summary-mode-map
425 "St" spam-bogofilter-score
426 "Sx" gnus-summary-mark-as-spam
427 "Mst" spam-bogofilter-score
428 "Msx" gnus-summary-mark-as-spam
429 "\M-d" gnus-summary-mark-as-spam)
431 (defvar spam-cache-lookups nil
432 "Whether spam.el will try to cache lookups using spam-caches.")
434 (defvar spam-caches (make-hash-table
437 "Cache of spam detection entries.")
439 (defvar spam-old-ham-articles nil
440 "List of old ham articles, generated when a group is entered.")
442 (defvar spam-old-spam-articles nil
443 "List of old spam articles, generated when a group is entered.")
445 (defvar spam-split-disabled nil
446 "If non-nil, `spam-split' is disabled, and always returns nil.")
448 (defvar spam-split-last-successful-check nil
449 "`spam-split' will set this to nil or a spam-use-XYZ check if it
452 ;; convenience functions
453 (defun spam-clear-cache (symbol)
454 (remhash symbol spam-caches))
456 (defun spam-xor (a b)
457 "Logical exclusive `or'."
458 (and (or a b) (not (and a b))))
460 (defun spam-group-ham-mark-p (group mark &optional spam)
461 (when (stringp group)
462 (let* ((marks (spam-group-ham-marks group spam))
463 (marks (if (symbolp mark)
465 (mapcar 'symbol-value marks))))
468 (defun spam-group-spam-mark-p (group mark)
469 (spam-group-ham-mark-p group mark t))
471 (defun spam-group-ham-marks (group &optional spam)
472 (when (stringp group)
473 (let* ((marks (if spam
474 (gnus-parameter-spam-marks group)
475 (gnus-parameter-ham-marks group)))
477 (marks (if (listp (car marks)) (car marks) marks)))
480 (defun spam-group-spam-marks (group)
481 (spam-group-ham-marks group t))
483 (defun spam-group-spam-contents-p (group)
485 (or (member group spam-junk-mailgroups)
486 (memq 'gnus-group-spam-classification-spam
487 (gnus-parameter-spam-contents group)))
490 (defun spam-group-ham-contents-p (group)
492 (memq 'gnus-group-spam-classification-ham
493 (gnus-parameter-spam-contents group))
496 (defvar spam-list-of-processors
497 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
498 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
499 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
500 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
501 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
502 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
503 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
504 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
505 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
506 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
507 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
508 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
509 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
510 "The spam-list-of-processors list contains pairs associating a
511 ham/spam exit processor variable with a classification and a
512 spam-use-* variable.")
514 (defun spam-group-processor-p (group processor)
515 (if (and (stringp group)
517 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
518 (spam-group-processor-multiple-p
520 (cdr-safe (assoc processor spam-list-of-processors))))
523 (defun spam-group-processor-multiple-p (group processor-info)
524 (let* ((classification (nth 0 processor-info))
525 (check (nth 1 processor-info))
526 (parameters (nth 0 (gnus-parameter-spam-process group)))
528 (dolist (parameter parameters)
529 (when (and (null found)
531 (eq classification (nth 0 parameter))
532 (eq check (nth 1 parameter)))
536 (defun spam-group-spam-processor-report-gmane-p (group)
537 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
539 (defun spam-group-spam-processor-bogofilter-p (group)
540 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
542 (defun spam-group-spam-processor-blacklist-p (group)
543 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
545 (defun spam-group-spam-processor-ifile-p (group)
546 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
548 (defun spam-group-ham-processor-ifile-p (group)
549 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
551 (defun spam-group-spam-processor-spamoracle-p (group)
552 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
554 (defun spam-group-ham-processor-bogofilter-p (group)
555 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
557 (defun spam-group-spam-processor-stat-p (group)
558 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
560 (defun spam-group-ham-processor-stat-p (group)
561 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
563 (defun spam-group-ham-processor-whitelist-p (group)
564 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
566 (defun spam-group-ham-processor-BBDB-p (group)
567 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
569 (defun spam-group-ham-processor-copy-p (group)
570 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
572 (defun spam-group-ham-processor-spamoracle-p (group)
573 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
575 (defun spam-report-articles-gmane (n)
576 "Report the current message as spam.
577 Respects the process/prefix convention."
579 (dolist (article (gnus-summary-work-articles n))
580 (gnus-summary-remove-process-mark article)
581 (spam-report-gmane article)))
583 ;;; Summary entry and exit processing.
585 (defun spam-summary-prepare ()
586 (setq spam-old-ham-articles
587 (spam-list-articles gnus-newsgroup-articles 'ham))
588 (setq spam-old-spam-articles
589 (spam-list-articles gnus-newsgroup-articles 'spam))
590 (spam-mark-junk-as-spam-routine))
592 ;; The spam processors are invoked for any group, spam or ham or neither
593 (defun spam-summary-prepare-exit ()
594 (unless gnus-group-is-exiting-without-update-p
595 (gnus-message 6 "Exiting summary buffer and applying spam rules")
597 ;; first of all, unregister any articles that are no longer ham or spam
598 ;; we have to iterate over the processors, or else we'll be too slow
599 (dolist (classification '(spam ham))
600 (let* ((old-articles (if (eq classification 'spam)
601 spam-old-spam-articles
602 spam-old-ham-articles))
603 (new-articles (spam-list-articles
604 gnus-newsgroup-articles
606 (changed-articles (gnus-set-difference old-articles new-articles)))
607 ;; now that we have the changed articles, we go through the processors
608 (dolist (processor-param spam-list-of-processors)
609 (let ((processor (nth 0 processor-param))
610 (processor-classification (nth 1 processor-param))
611 (check (nth 2 processor-param))
613 (dolist (article changed-articles)
614 (let ((id (spam-fetch-field-message-id-fast article)))
615 (when (spam-log-unregistration-needed-p
616 id 'process classification check)
617 (push article unregister-list))))
618 ;; call spam-register-routine with specific articles to unregister,
619 ;; when there are articles to unregister and the check is enabled
620 (when (and unregister-list (symbol-value check))
621 (spam-register-routine classification check t unregister-list))))))
623 ;; find all the spam processors applicable to this group
624 (dolist (processor-param spam-list-of-processors)
625 (let ((processor (nth 0 processor-param))
626 (classification (nth 1 processor-param))
627 (check (nth 2 processor-param)))
628 (when (and (eq 'spam classification)
629 (spam-group-processor-p gnus-newsgroup-name processor))
630 (spam-register-routine classification check))))
632 (if spam-move-spam-nonspam-groups-only
633 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
634 (spam-mark-spam-as-expired-and-move-routine
635 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
636 (gnus-message 5 "Marking spam as expired and moving it to %s"
638 (spam-mark-spam-as-expired-and-move-routine
639 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
641 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
642 ;; expire spam, in case the above did not expire them
643 (gnus-message 5 "Marking spam as expired without moving it")
644 (spam-mark-spam-as-expired-and-move-routine nil)
646 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
647 (and (spam-group-spam-contents-p gnus-newsgroup-name)
648 spam-process-ham-in-spam-groups)
649 spam-process-ham-in-nonham-groups)
650 ;; find all the ham processors applicable to this group
651 (dolist (processor-param spam-list-of-processors)
652 (let ((processor (nth 0 processor-param))
653 (classification (nth 1 processor-param))
654 (check (nth 2 processor-param)))
655 (when (and (eq 'ham classification)
656 (spam-group-processor-p gnus-newsgroup-name processor))
657 (spam-register-routine classification check)))))
659 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
660 (gnus-message 5 "Copying ham")
661 (spam-ham-copy-routine
662 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
664 ;; now move all ham articles out of spam groups
665 (when (spam-group-spam-contents-p gnus-newsgroup-name)
666 (gnus-message 5 "Moving ham messages from spam group")
667 (spam-ham-move-routine
668 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
670 (setq spam-old-ham-articles nil)
671 (setq spam-old-spam-articles nil))
673 (defun spam-mark-junk-as-spam-routine ()
674 ;; check the global list of group names spam-junk-mailgroups and the
676 (when (spam-group-spam-contents-p gnus-newsgroup-name)
677 (gnus-message 5 "Marking %s articles as spam"
678 (if spam-mark-only-unseen-as-spam
681 (let ((articles (if spam-mark-only-unseen-as-spam
682 gnus-newsgroup-unseen
683 gnus-newsgroup-unreads)))
684 (dolist (article articles)
685 (gnus-summary-mark-article article gnus-spam-mark)))))
687 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
688 (if (and (car-safe groups) (listp (car-safe groups)))
689 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
690 (gnus-summary-kill-process-mark)
691 (let ((articles gnus-newsgroup-articles)
692 (backend-supports-deletions
693 (gnus-check-backend-function
694 'request-move-article gnus-newsgroup-name))
695 article tomove deletep)
696 (dolist (article articles)
697 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
698 (gnus-summary-mark-article article gnus-expirable-mark)
699 (push article tomove)))
701 ;; now do the actual copies
702 (dolist (group groups)
705 (dolist (article tomove)
706 (gnus-summary-set-process-mark article))
708 (if (or (not backend-supports-deletions)
709 (> (length groups) 1))
711 (gnus-summary-copy-article nil group)
713 (gnus-summary-move-article nil group)))))
715 ;; now delete the articles, if there was a copy done, and the
717 (when (and deletep backend-supports-deletions)
718 (dolist (article tomove)
719 (gnus-summary-set-process-mark article))
721 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
722 (gnus-summary-delete-article nil))))
724 (gnus-summary-yank-process-mark))))
726 (defun spam-ham-copy-or-move-routine (copy groups)
727 (gnus-summary-kill-process-mark)
728 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
729 (backend-supports-deletions
730 (gnus-check-backend-function
731 'request-move-article gnus-newsgroup-name))
732 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
733 article mark todo deletep respool)
735 (when (member 'respool groups)
736 (setq respool t) ; boolean for later
737 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
739 ;; now do the actual move
740 (dolist (group groups)
741 (when (and todo (stringp group))
742 (dolist (article todo)
743 (when spam-mark-ham-unread-before-move-from-spam-group
744 (gnus-summary-mark-article article gnus-unread-mark))
745 (gnus-summary-set-process-mark article))
747 (if respool ; respooling is with a "fake" group
748 (let ((spam-split-disabled
749 (or spam-split-disabled
750 spam-disable-spam-split-during-ham-respool)))
751 (gnus-summary-respool-article nil respool-method))
752 (if (or (not backend-supports-deletions) ; else, we are not respooling
753 (> (length groups) 1))
754 (progn ; if copying, copy and set deletep
755 (gnus-summary-copy-article nil group)
757 (gnus-summary-move-article nil group))))) ; else move articles
759 ;; now delete the articles, unless a) copy is t, and there was a copy done
760 ;; b) a move was done to a single group
761 ;; c) backend-supports-deletions is nil
763 (when (and deletep backend-supports-deletions)
764 (dolist (article todo)
765 (gnus-summary-set-process-mark article))
767 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
768 (gnus-summary-delete-article nil))))))
770 (gnus-summary-yank-process-mark))
772 (defun spam-ham-copy-routine (&rest groups)
773 (if (and (car-safe groups) (listp (car-safe groups)))
774 (apply 'spam-ham-copy-routine (car groups))
775 (spam-ham-copy-or-move-routine t groups)))
777 (defun spam-ham-move-routine (&rest groups)
778 (if (and (car-safe groups) (listp (car-safe groups)))
779 (apply 'spam-ham-move-routine (car groups))
780 (spam-ham-copy-or-move-routine nil groups)))
782 (defun spam-get-article-as-string (article)
783 (let ((article-buffer (spam-get-article-as-buffer article))
786 (save-window-excursion
787 (set-buffer article-buffer)
788 (setq article-string (buffer-string))))
791 (defun spam-get-article-as-buffer (article)
792 (let ((article-buffer))
793 (when (numberp article)
794 (save-window-excursion
795 (gnus-summary-goto-subject article)
796 (gnus-summary-show-article t)
797 (setq article-buffer (get-buffer gnus-article-buffer))))
801 ;; (defun spam-get-article-as-filename (article)
802 ;; (let ((article-filename))
803 ;; (when (numberp article)
804 ;; (nnml-possibly-change-directory
805 ;; (gnus-group-real-name gnus-newsgroup-name))
806 ;; (setq article-filename (expand-file-name
807 ;; (int-to-string article) nnml-current-directory)))
808 ;; (if (file-exists-p article-filename)
812 (defun spam-fetch-field-from-fast (article)
813 "Fetch the `from' field quickly, using the internal gnus-data-list function"
814 (if (and (numberp article)
815 (assoc article (gnus-data-list nil)))
817 (gnus-data-header (assoc article (gnus-data-list nil))))
820 (defun spam-fetch-field-subject-fast (article)
821 "Fetch the `subject' field quickly, using the internal
822 gnus-data-list function"
823 (if (and (numberp article)
824 (assoc article (gnus-data-list nil)))
826 (gnus-data-header (assoc article (gnus-data-list nil))))
829 (defun spam-fetch-field-message-id-fast (article)
830 "Fetch the `Message-ID' field quickly, using the internal
831 gnus-data-list function"
832 (if (and (numberp article)
833 (assoc article (gnus-data-list nil)))
834 (mail-header-message-id
835 (gnus-data-header (assoc article (gnus-data-list nil))))
839 ;;;; Spam determination.
841 (defvar spam-list-of-checks
842 '((spam-use-blacklist . spam-check-blacklist)
843 (spam-use-regex-headers . spam-check-regex-headers)
844 (spam-use-regex-body . spam-check-regex-body)
845 (spam-use-whitelist . spam-check-whitelist)
846 (spam-use-BBDB . spam-check-BBDB)
847 (spam-use-ifile . spam-check-ifile)
848 (spam-use-spamoracle . spam-check-spamoracle)
849 (spam-use-stat . spam-check-stat)
850 (spam-use-blackholes . spam-check-blackholes)
851 (spam-use-hashcash . spam-check-hashcash)
852 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
853 (spam-use-bogofilter . spam-check-bogofilter))
854 "The spam-list-of-checks list contains pairs associating a
855 parameter variable with a spam checking function. If the
856 parameter variable is true, then the checking function is called,
857 and its value decides what happens. Each individual check may
858 return nil, t, or a mailgroup name. The value nil means that the
859 check does not yield a decision, and so, that further checks are
860 needed. The value t means that the message is definitely not
861 spam, and that further spam checks should be inhibited.
862 Otherwise, a mailgroup name or the symbol 'spam (depending on
863 spam-split-symbolic-return) is returned where the mail should go,
864 and further checks are also inhibited. The usual mailgroup name
865 is the value of `spam-split-group', meaning that the message is
868 (defvar spam-list-of-statistical-checks
874 "The spam-list-of-statistical-checks list contains all the mail
875 splitters that need to have the full message body available.")
877 ;;;TODO: modify to invoke self with each check if invoked without specifics
878 (defun spam-split (&rest specific-checks)
879 "Split this message into the `spam' group if it is spam.
880 This function can be used as an entry in the variable `nnmail-split-fancy',
881 for example like this: (: spam-split). It can take checks as
882 parameters. A string as a parameter will set the
883 spam-split-group to that string.
885 See the Info node `(gnus)Fancy Mail Splitting' for more details."
887 (setq spam-split-last-successful-check nil)
888 (unless spam-split-disabled
889 (let ((spam-split-group-choice spam-split-group))
890 (dolist (check specific-checks)
891 (when (stringp check)
892 (setq spam-split-group-choice check)
893 (setq specific-checks (delq check specific-checks))))
895 (let ((spam-split-group spam-split-group-choice))
898 (dolist (check spam-list-of-statistical-checks)
899 (when (and (symbolp check) (symbol-value check))
901 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
904 ;; (progn (widen) (debug (buffer-string)))
905 (let ((list-of-checks spam-list-of-checks)
907 (while (and list-of-checks (not decision))
908 (let ((pair (pop list-of-checks)))
909 (when (and (symbol-value (car pair))
910 (or (null specific-checks)
911 (memq (car pair) specific-checks)))
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 (if spam-split-symbolic-return
921 (setq decision spam-split-group)
924 (format "spam-split got %s but %s is nil"
925 (symbol-name decision)
926 (symbol-name spam-split-symbolic-return))))))))
928 (if spam-split-symbolic-return-positive 'ham nil)
931 (defun spam-find-spam ()
932 "This function will detect spam in the current newsgroup using spam-split."
935 (let* ((group gnus-newsgroup-name)
936 (autodetect (gnus-parameter-spam-autodetect group))
937 (methods (gnus-parameter-spam-autodetect-methods group))
938 (first-method (nth 0 methods))
939 (articles (if spam-autodetect-recheck-messages
940 gnus-newsgroup-articles
941 gnus-newsgroup-unseen))
942 (spam-cache-lookups (< 2 (length articles))))
944 (when (and autodetect
945 (not (equal first-method 'none)))
948 (let ((id (spam-fetch-field-message-id-fast article))
949 (subject (spam-fetch-field-subject-fast article))
950 (sender (spam-fetch-field-from-fast article)))
951 (unless (and spam-log-to-registry
952 (spam-log-registered-p id 'incoming))
953 (let* ((spam-split-symbolic-return t)
954 (spam-split-symbolic-return-positive t)
957 (gnus-request-article-this-buffer
960 (if (or (null first-method)
961 (equal first-method 'default))
963 (apply 'spam-split methods)))))
964 (if (equal split-return 'spam)
965 (gnus-summary-mark-article article gnus-spam-mark))
967 (when (and split-return spam-log-to-registry)
968 (when (zerop (gnus-registry-group-count id))
969 (gnus-registry-add-group
970 id group subject sender))
972 (spam-log-processing-to-registry
976 spam-split-last-successful-check
980 (defvar spam-registration-functions
981 ;; first the ham register, second the spam register function
982 ;; third the ham unregister, fourth the spam unregister function
983 '((spam-use-blacklist nil
984 spam-blacklist-register-routine
986 spam-blacklist-unregister-routine)
987 (spam-use-whitelist spam-whitelist-register-routine
989 spam-whitelist-unregister-routine
991 (spam-use-BBDB spam-BBDB-register-routine
993 spam-BBDB-unregister-routine
995 (spam-use-ifile spam-ifile-register-ham-routine
996 spam-ifile-register-spam-routine
997 spam-ifile-unregister-ham-routine
998 spam-ifile-unregister-spam-routine)
999 (spam-use-spamoracle spam-spamoracle-learn-ham
1000 spam-spamoracle-learn-spam
1001 spam-spamoracle-unlearn-ham
1002 spam-spamoracle-unlearn-spam)
1003 (spam-use-stat spam-stat-register-ham-routine
1004 spam-stat-register-spam-routine
1005 spam-stat-unregister-ham-routine
1006 spam-stat-unregister-spam-routine)
1007 ;; note that spam-use-gmane is not a legitimate check
1009 spam-report-gmane-register-routine
1010 ;; does Gmane support unregistration?
1013 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1014 spam-bogofilter-register-spam-routine
1015 spam-bogofilter-unregister-ham-routine
1016 spam-bogofilter-unregister-spam-routine))
1017 "The spam-registration-functions list contains pairs
1018 associating a parameter variable with the ham and spam
1019 registration functions, and the ham and spam unregistration
1022 (defun spam-classification-valid-p (classification)
1023 (or (eq classification 'spam)
1024 (eq classification 'ham)))
1026 (defun spam-process-type-valid-p (process-type)
1027 (or (eq process-type 'incoming)
1028 (eq process-type 'process)))
1030 (defun spam-registration-check-valid-p (check)
1031 (assoc check spam-registration-functions))
1033 (defun spam-unregistration-check-valid-p (check)
1034 (assoc check spam-registration-functions))
1036 (defun spam-registration-function (classification check)
1037 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1038 (if (eq classification 'spam)
1042 (defun spam-unregistration-function (classification check)
1043 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1044 (if (eq classification 'spam)
1048 (defun spam-list-articles (articles classification)
1049 (let ((mark-check (if (eq classification 'spam)
1050 'spam-group-spam-mark-p
1051 'spam-group-ham-mark-p))
1052 list mark-cache-yes mark-cache-no)
1053 (dolist (article articles)
1054 (let ((mark (gnus-summary-article-mark article)))
1055 (unless (memq mark mark-cache-no)
1056 (if (memq mark mark-cache-yes)
1058 ;; else, we have to actually check the mark
1059 (if (funcall mark-check
1064 (push mark mark-cache-yes))
1065 (push mark mark-cache-no))))))
1068 (defun spam-register-routine (classification
1070 &optional unregister
1072 (when (and (spam-classification-valid-p classification)
1073 (spam-registration-check-valid-p check))
1074 (let* ((register-function
1075 (spam-registration-function classification check))
1076 (unregister-function
1077 (spam-unregistration-function classification check))
1078 (run-function (if unregister
1081 (log-function (if unregister
1082 'spam-log-undo-registration
1083 'spam-log-processing-to-registry))
1087 ;; make list of articles, using specific-articles if given
1088 (setq articles (or specific-articles
1090 gnus-newsgroup-articles
1093 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1094 (if unregister "Unregistering" "Registering")
1096 (if specific-articles "specific" "")
1097 (symbol-name classification)
1098 (symbol-name check))
1099 (funcall run-function articles)
1100 ;; now log all the registrations (or undo them, depending on unregister)
1101 (dolist (article articles)
1102 (funcall log-function
1103 (spam-fetch-field-message-id-fast article)
1107 gnus-newsgroup-name))))))
1109 ;;; log a ham- or spam-processor invocation to the registry
1110 (defun spam-log-processing-to-registry (id type classification check group)
1111 (when spam-log-to-registry
1112 (if (and (stringp id)
1114 (spam-process-type-valid-p type)
1115 (spam-classification-valid-p classification)
1116 (spam-registration-check-valid-p check))
1117 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1118 (cell (list classification check group)))
1119 (push cell cell-list)
1120 (gnus-registry-store-extra-entry
1125 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1126 "spam-log-processing-to-registry")))))
1128 ;;; check if a ham- or spam-processor registration has been done
1129 (defun spam-log-registered-p (id type)
1130 (when spam-log-to-registry
1131 (if (and (stringp id)
1132 (spam-process-type-valid-p type))
1133 (cdr-safe (gnus-registry-fetch-extra id type))
1135 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1136 "spam-log-registered-p"))
1139 ;;; check if a ham- or spam-processor registration needs to be undone
1140 (defun spam-log-unregistration-needed-p (id type classification check)
1141 (when spam-log-to-registry
1142 (if (and (stringp id)
1143 (spam-process-type-valid-p type)
1144 (spam-classification-valid-p classification)
1145 (spam-registration-check-valid-p check))
1146 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1148 (dolist (cell cell-list)
1150 (when (and (eq classification (nth 0 cell))
1151 (eq check (nth 1 cell)))
1155 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1156 "spam-log-unregistration-needed-p"))
1160 ;;; undo a ham- or spam-processor registration (the group is not used)
1161 (defun spam-log-undo-registration (id type classification check &optional group)
1162 (when (and spam-log-to-registry
1163 (spam-log-unregistration-needed-p id type classification check))
1164 (if (and (stringp id)
1165 (spam-process-type-valid-p type)
1166 (spam-classification-valid-p classification)
1167 (spam-registration-check-valid-p check))
1168 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1169 new-cell-list found)
1170 (dolist (cell cell-list)
1171 (unless (and (eq classification (nth 0 cell))
1172 (eq check (nth 1 cell)))
1173 (push cell new-cell-list)))
1174 (gnus-registry-store-extra-entry
1179 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1180 "spam-log-undo-registration"))
1183 ;;; set up IMAP widening if it's necessary
1184 (defun spam-setup-widening ()
1185 (dolist (check spam-list-of-statistical-checks)
1186 (when (symbol-value check)
1187 (setq nnimap-split-download-body-default t))))
1192 (defun spam-check-regex-body ()
1193 (let ((spam-regex-headers-ham spam-regex-body-ham)
1194 (spam-regex-headers-spam spam-regex-body-spam))
1195 (spam-check-regex-headers t)))
1200 (defun spam-check-regex-headers (&optional body)
1201 (let ((type (if body "body" "header"))
1202 (spam-split-group (if spam-split-symbolic-return
1206 (dolist (h-regex spam-regex-headers-ham)
1208 (goto-char (point-min))
1209 (when (re-search-forward h-regex nil t)
1210 (message "Ham regex %s search positive." type)
1212 (dolist (s-regex spam-regex-headers-spam)
1214 (goto-char (point-min))
1215 (when (re-search-forward s-regex nil t)
1216 (message "Spam regex %s search positive." type)
1218 (setq ret spam-split-group))))
1224 (defun spam-reverse-ip-string (ip)
1226 (mapconcat 'identity
1227 (nreverse (split-string ip "\\."))
1230 (defun spam-check-blackholes ()
1231 "Check the Received headers for blackholed relays."
1232 (let ((headers (nnmail-fetch-field "received"))
1233 (spam-split-group (if spam-split-symbolic-return
1240 (goto-char (point-min))
1241 (gnus-message 5 "Checking headers for relay addresses")
1242 (while (re-search-forward
1243 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1244 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1245 (push (spam-reverse-ip-string (match-string 1))
1247 (dolist (server spam-blackhole-servers)
1249 (unless (and spam-blackhole-good-server-regex
1250 ;; match the good-server-regex against the reversed (again) IP string
1252 spam-blackhole-good-server-regex
1253 (spam-reverse-ip-string ip)))
1255 (let ((query-string (concat ip "." server)))
1257 (let ((query-result (query-dig query-string)))
1259 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1261 (push (list ip server query-result)
1263 ;; else, if not using dig.el
1264 (when (query-dns query-string)
1265 (gnus-message 5 "positive blackhole check")
1266 (push (list ip server (query-dns query-string 'TXT))
1277 (defun spam-check-hashcash ()
1278 "Check the headers for hashcash payments."
1279 (mail-check-payment))) ;mail-check-payment returns a boolean
1282 (defalias 'mail-check-payment 'ignore)
1283 (defalias 'spam-check-hashcash 'ignore))))
1287 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1288 ;;; <sacha@giotto.sj.ru>
1290 ;; all this is done inside a condition-case to trap errors
1297 ;; when the BBDB changes, we want to clear out our cache
1298 (defun spam-clear-cache-BBDB (&rest immaterial)
1299 (spam-clear-cache 'spam-use-BBDB))
1301 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1303 (defun spam-enter-ham-BBDB (addresses &optional remove)
1304 "Enter an address into the BBDB; implies ham (non-spam) sender"
1305 (dolist (from addresses)
1306 (when (stringp from)
1307 (let* ((parsed-address (gnus-extract-address-components from))
1308 (name (or (nth 0 parsed-address) "Ham Sender"))
1309 (remove-function (if remove
1310 'bbdb-delete-record-internal
1312 (net-address (nth 1 parsed-address))
1313 (record (and net-address
1314 (bbdb-search-simple nil net-address))))
1316 (gnus-message 5 "%s address %s %s BBDB"
1317 (if remove "Deleting" "Adding")
1319 (if remove "from" "to"))
1321 (funcall remove-function record)
1322 (bbdb-create-internal name nil net-address nil nil
1323 "ham sender added by spam.el")))))))
1325 (defun spam-BBDB-register-routine (articles &optional unregister)
1327 (dolist (article articles)
1328 (when (stringp (spam-fetch-field-from-fast article))
1329 (push (spam-fetch-field-from-fast article) addresses)))
1330 ;; now do the register/unregister action
1331 (spam-enter-ham-BBDB addresses unregister)))
1333 (defun spam-BBDB-unregister-routine (articles)
1334 (spam-BBDB-register-routine articles t))
1336 (defun spam-check-BBDB ()
1337 "Mail from people in the BBDB is classified as ham or non-spam"
1338 (let ((who (nnmail-fetch-field "from"))
1339 (spam-split-group (if spam-split-symbolic-return
1344 (when spam-cache-lookups
1345 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1347 (setq bbdb-cache (bbdb-hashtable))
1348 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1351 (setq who (nth 1 (gnus-extract-address-components who)))
1353 (if spam-cache-lookups
1354 (bbdb-gethash who bbdb-cache)
1355 (bbdb-search-simple nil who))
1357 (if spam-use-BBDB-exclusive
1362 (defalias 'bbdb-search-simple 'ignore)
1363 (defalias 'bbdb-hashtable 'ignore)
1364 (defalias 'bbdb-gethash 'ignore)
1365 (defalias 'spam-check-BBDB 'ignore)
1366 (defalias 'spam-BBDB-register-routine 'ignore)
1367 (defalias 'spam-enter-ham-BBDB 'ignore)
1368 (defalias 'bbdb-create-internal 'ignore)
1369 (defalias 'bbdb-delete-record-internal 'ignore)
1370 (defalias 'bbdb-records 'ignore))))
1375 ;;; check the ifile backend; return nil if the mail was NOT classified
1378 (defun spam-get-ifile-database-parameter ()
1379 "Get the command-line parameter for ifile's database from
1380 spam-ifile-database-path."
1381 (if spam-ifile-database-path
1382 (format "--db-file=%s" spam-ifile-database-path)
1385 (defun spam-check-ifile ()
1386 "Check the ifile backend for the classification of this message."
1387 (let ((article-buffer-name (buffer-name))
1388 (spam-split-group (if spam-split-symbolic-return
1393 (let ((temp-buffer-name (buffer-name))
1394 (db-param (spam-get-ifile-database-parameter)))
1396 (set-buffer article-buffer-name)
1397 (apply 'call-process-region
1398 (point-min) (point-max) spam-ifile-path
1399 nil temp-buffer-name nil "-c"
1400 (if db-param `(,db-param "-q") `("-q"))))
1401 ;; check the return now (we're back in the temp buffer)
1402 (goto-char (point-min))
1404 (setq category (buffer-substring (point) (point-at-eol))))
1405 (when (not (zerop (length category))) ; we need a category here
1406 (if spam-ifile-all-categories
1407 (setq return category)
1408 ;; else, if spam-ifile-all-categories is not set...
1409 (when (string-equal spam-ifile-spam-category category)
1410 (setq return spam-split-group)))))) ; note return is nil otherwise
1413 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1414 "Register an article, given as a string, with a category.
1415 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1416 (let ((category (or category gnus-newsgroup-name))
1417 (add-or-delete-option (if unregister "-d" "-i"))
1418 (db (spam-get-ifile-database-parameter))
1421 (dolist (article articles)
1422 (let ((article-string (spam-get-article-as-string article)))
1423 (when (stringp article-string)
1424 (insert article-string))))
1425 (apply 'call-process-region
1426 (point-min) (point-max) spam-ifile-path
1428 add-or-delete-option category
1429 (if db `(,db "-h") `("-h"))))))
1431 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1432 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1434 (defun spam-ifile-unregister-spam-routine (articles)
1435 (spam-ifile-register-spam-routine articles t))
1437 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1438 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1440 (defun spam-ifile-unregister-ham-routine (articles)
1441 (spam-ifile-register-ham-routine articles t))
1448 (let ((spam-stat-install-hooks nil))
1449 (require 'spam-stat))
1451 (defun spam-check-stat ()
1452 "Check the spam-stat backend for the classification of this message"
1453 (let ((spam-split-group (if spam-split-symbolic-return
1456 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1457 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1459 (spam-stat-split-fancy)))
1461 (defun spam-stat-register-spam-routine (articles &optional unregister)
1462 (dolist (article articles)
1463 (let ((article-string (spam-get-article-as-string article)))
1465 (insert article-string)
1467 (spam-stat-buffer-change-to-non-spam)
1468 (spam-stat-buffer-is-spam))))))
1470 (defun spam-stat-unregister-spam-routine (articles)
1471 (spam-stat-register-spam-routine articles t))
1473 (defun spam-stat-register-ham-routine (articles &optional unregister)
1474 (dolist (article articles)
1475 (let ((article-string (spam-get-article-as-string article)))
1477 (insert article-string)
1479 (spam-stat-buffer-change-to-spam)
1480 (spam-stat-buffer-is-non-spam))))))
1482 (defun spam-stat-unregister-ham-routine (articles)
1483 (spam-stat-register-ham-routine articles t))
1485 (defun spam-maybe-spam-stat-load ()
1486 (when spam-use-stat (spam-stat-load)))
1488 (defun spam-maybe-spam-stat-save ()
1489 (when spam-use-stat (spam-stat-save))))
1492 (defalias 'spam-stat-load 'ignore)
1493 (defalias 'spam-stat-save 'ignore)
1494 (defalias 'spam-maybe-spam-stat-load 'ignore)
1495 (defalias 'spam-maybe-spam-stat-save 'ignore)
1496 (defalias 'spam-stat-register-ham-routine 'ignore)
1497 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1498 (defalias 'spam-stat-register-spam-routine 'ignore)
1499 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1500 (defalias 'spam-stat-buffer-is-spam 'ignore)
1501 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1502 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1503 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1504 (defalias 'spam-stat-split-fancy 'ignore)
1505 (defalias 'spam-check-stat 'ignore))))
1509 ;;;; Blacklists and whitelists.
1511 (defvar spam-whitelist-cache nil)
1512 (defvar spam-blacklist-cache nil)
1514 (defun spam-kill-whole-line ()
1516 (let ((kill-whole-line t))
1519 ;;; address can be a list, too
1520 (defun spam-enter-whitelist (address &optional remove)
1521 "Enter ADDRESS (list or single) into the whitelist.
1522 With a non-nil REMOVE, remove them."
1523 (interactive "sAddress: ")
1524 (spam-enter-list address spam-whitelist remove)
1525 (setq spam-whitelist-cache nil))
1527 ;;; address can be a list, too
1528 (defun spam-enter-blacklist (address &optional remove)
1529 "Enter ADDRESS (list or single) into the blacklist.
1530 With a non-nil REMOVE, remove them."
1531 (interactive "sAddress: ")
1532 (spam-enter-list address spam-blacklist remove)
1533 (setq spam-blacklist-cache nil))
1535 (defun spam-enter-list (addresses file &optional remove)
1536 "Enter ADDRESSES into the given FILE.
1537 Either the whitelist or the blacklist files can be used. With
1538 REMOVE not nil, remove the ADDRESSES."
1539 (if (stringp addresses)
1540 (spam-enter-list (list addresses) file remove)
1541 ;; else, we have a list of addresses here
1542 (unless (file-exists-p (file-name-directory file))
1543 (make-directory (file-name-directory file) t))
1546 (find-file-noselect file))
1547 (dolist (a addresses)
1549 (goto-char (point-min))
1550 (if (re-search-forward (regexp-quote a) nil t)
1551 ;; found the address
1553 (spam-kill-whole-line))
1554 ;; else, the address was not found
1556 (goto-char (point-max))
1562 ;;; returns t if the sender is in the whitelist, nil or
1563 ;;; spam-split-group otherwise
1564 (defun spam-check-whitelist ()
1565 ;; FIXME! Should it detect when file timestamps change?
1566 (let ((spam-split-group (if spam-split-symbolic-return
1569 (unless spam-whitelist-cache
1570 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1571 (if (spam-from-listed-p spam-whitelist-cache)
1573 (if spam-use-whitelist-exclusive
1577 (defun spam-check-blacklist ()
1578 ;; FIXME! Should it detect when file timestamps change?
1579 (let ((spam-split-group (if spam-split-symbolic-return
1582 (unless spam-blacklist-cache
1583 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1584 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1586 (defun spam-parse-list (file)
1587 (when (file-readable-p file)
1588 (let (contents address)
1590 (insert-file-contents file)
1592 (setq address (buffer-substring (point) (point-at-eol)))
1594 ;; insert the e-mail address if detected, otherwise the raw data
1595 (unless (zerop (length address))
1596 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1597 (push (or pure-address address) contents)))))
1598 (nreverse contents))))
1600 (defun spam-from-listed-p (cache)
1601 (let ((from (nnmail-fetch-field "from"))
1604 (let ((address (pop cache)))
1605 (unless (zerop (length address)) ; 0 for a nil address too
1606 (setq address (regexp-quote address))
1607 ;; fix regexp-quote's treatment of user-intended regexes
1608 (while (string-match "\\\\\\*" address)
1609 (setq address (replace-match ".*" t t address))))
1610 (when (and address (string-match address from))
1615 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1616 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1617 (declassification (if blacklist 'ham 'spam))
1619 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1621 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1622 from addresses unregister-list)
1623 (dolist (article articles)
1624 (let ((from (spam-fetch-field-from-fast article))
1625 (id (spam-fetch-field-message-id-fast article))
1627 (when (stringp from)
1628 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1629 (when (and (not sender-ignored)
1630 (stringp ignore-regex)
1631 (string-match ignore-regex from))
1632 (setq sender-ignored t)))
1633 ;; remember the messages we need to unregister, unless remove is set
1636 (spam-log-unregistration-needed-p
1637 id 'process declassification de-symbol))
1638 (push from unregister-list))
1639 (unless sender-ignored
1640 (push from addresses)))))
1643 (funcall enter-function addresses t) ; unregister all these addresses
1644 ;; else, register normally and unregister what we need to
1645 (funcall remove-function unregister-list t)
1646 (dolist (article unregister-list)
1647 (spam-log-undo-registration
1648 (spam-fetch-field-message-id-fast article)
1652 (funcall enter-function addresses nil))))
1654 (defun spam-blacklist-unregister-routine (articles)
1655 (spam-blacklist-register-routine articles t))
1657 (defun spam-blacklist-register-routine (articles &optional unregister)
1658 (spam-filelist-register-routine articles t unregister))
1660 (defun spam-whitelist-unregister-routine (articles)
1661 (spam-whitelist-register-routine articles t))
1663 (defun spam-whitelist-register-routine (articles &optional unregister)
1664 (spam-filelist-register-routine articles nil unregister))
1667 ;;;; Spam-report glue
1668 (defun spam-report-gmane-register-routine (articles)
1670 (apply 'spam-report-gmane articles)))
1674 (defun spam-check-bogofilter-headers (&optional score)
1675 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1676 (spam-split-group (if spam-split-symbolic-return
1679 (when header ; return nil when no header
1680 (if score ; scoring mode
1681 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1682 (match-string 1 header)
1684 ;; spam detection mode
1685 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1687 spam-split-group)))))
1689 ;; return something sensible if the score can't be determined
1690 (defun spam-bogofilter-score ()
1691 "Get the Bogofilter spamicity score"
1693 (save-window-excursion
1694 (gnus-summary-show-article t)
1695 (set-buffer gnus-article-buffer)
1696 (let ((score (or (spam-check-bogofilter-headers t)
1697 (spam-check-bogofilter t))))
1698 (message "Spamicity score %s" score)
1700 (gnus-summary-show-article)))
1702 (defun spam-check-bogofilter (&optional score)
1703 "Check the Bogofilter backend for the classification of this message"
1704 (let ((article-buffer-name (buffer-name))
1705 (db spam-bogofilter-database-directory)
1708 (let ((temp-buffer-name (buffer-name)))
1710 (set-buffer article-buffer-name)
1711 (apply 'call-process-region
1712 (point-min) (point-max)
1713 spam-bogofilter-path
1714 nil temp-buffer-name nil
1715 (if db `("-d" ,db "-v") `("-v"))))
1716 (setq return (spam-check-bogofilter-headers score))))
1719 (defun spam-bogofilter-register-with-bogofilter (articles
1721 &optional unregister)
1722 "Register an article, given as a string, as spam or non-spam."
1723 (dolist (article articles)
1724 (let ((article-string (spam-get-article-as-string article))
1725 (db spam-bogofilter-database-directory)
1726 (switch (if unregister
1728 spam-bogofilter-spam-strong-switch
1729 spam-bogofilter-ham-strong-switch)
1731 spam-bogofilter-spam-switch
1732 spam-bogofilter-ham-switch))))
1733 (when (stringp article-string)
1735 (insert article-string)
1737 (apply 'call-process-region
1738 (point-min) (point-max)
1739 spam-bogofilter-path
1741 (if db `("-d" ,db "-v") `("-v"))))))))
1743 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1744 (spam-bogofilter-register-with-bogofilter articles t unregister))
1746 (defun spam-bogofilter-unregister-spam-routine (articles)
1747 (spam-bogofilter-register-spam-routine articles t))
1749 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1750 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1752 (defun spam-bogofilter-unregister-ham-routine (articles)
1753 (spam-bogofilter-register-ham-routine articles t))
1758 (defun spam-check-spamoracle ()
1759 "Run spamoracle on an article to determine whether it's spam."
1760 (let ((article-buffer-name (buffer-name))
1761 (spam-split-group (if spam-split-symbolic-return
1765 (let ((temp-buffer-name (buffer-name)))
1767 (set-buffer article-buffer-name)
1769 (apply 'call-process-region
1770 (point-min) (point-max)
1771 spam-spamoracle-binary
1772 nil temp-buffer-name nil
1773 (if spam-spamoracle-database
1774 `("-f" ,spam-spamoracle-database "mark")
1778 (set-buffer temp-buffer-name)
1779 (goto-char (point-min))
1780 (when (re-search-forward "^X-Spam: yes;" nil t)
1782 (error "Error running spamoracle" status))))))))
1784 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1785 "Run spamoracle in training mode."
1787 (let ((temp-buffer-name (buffer-name)))
1789 (goto-char (point-min))
1790 (dolist (article articles)
1791 (insert (spam-get-article-as-string article)))
1792 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1796 (apply 'call-process-region
1797 (point-min) (point-max)
1798 spam-spamoracle-binary
1799 nil temp-buffer-name nil
1800 (if spam-spamoracle-database
1801 `("-f" ,spam-spamoracle-database
1804 (when (not (eq 0 status))
1805 (error "Error running spamoracle" status)))))))
1807 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1808 (spam-spamoracle-learn articles nil unregister))
1810 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1811 (spam-spamoracle-learn-ham articles t))
1813 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1814 (spam-spamoracle-learn articles t unregister))
1816 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1817 (spam-spamoracle-learn-spam articles t))
1823 (defun spam-initialize ()
1824 "Install the spam.el hooks and do other initialization"
1826 (setq spam-install-hooks t)
1827 ;; TODO: How do we redo this every time spam-face is customized?
1828 (push '((eq mark gnus-spam-mark) . spam-face)
1829 gnus-summary-highlight)
1830 ;; Add hooks for loading and saving the spam stats
1831 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1832 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1833 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1834 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1835 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1836 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1837 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1839 (defun spam-unload-hook ()
1840 "Uninstall the spam.el hooks"
1842 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1843 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1844 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1845 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1846 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1847 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1848 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1850 (when spam-install-hooks
1855 ;;; spam.el ends here.