1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;;; This module addresses a few aspects of spam control under Gnus. Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
30 ;;; The integration with Gnus is not yet complete. See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
33 ;;; Several TODO items are marked as such
35 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting,
36 ;; remote processing, training through files
40 (eval-when-compile (require 'cl))
44 (require 'gnus-uu) ; because of key prefix issues
45 ;;; for the definitions of group content classification and spam processors
47 (require 'message) ;for the message-fetch-field functions
49 ;; for nnimap-split-download-body-default
50 (eval-when-compile (require 'nnimap))
54 (autoload 'query-dig "dig"))
56 ;; autoload spam-report
58 (autoload 'spam-report-gmane "spam-report"))
60 ;; autoload gnus-registry
62 (autoload 'gnus-registry-group-count "gnus-registry")
63 (autoload 'gnus-registry-add-group "gnus-registry")
64 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
65 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
69 (autoload 'query-dns "dns"))
74 "Spam configuration.")
76 (defcustom spam-directory "~/News/spam/"
77 "Directory for spam whitelists and blacklists."
81 (defcustom spam-move-spam-nonspam-groups-only t
82 "Whether spam should be moved in non-spam groups only.
83 When t, only ham and unclassified groups will have their spam moved
84 to the spam-process-destination. When nil, spam will also be moved from
89 (defcustom spam-process-ham-in-nonham-groups nil
90 "Whether ham should be processed in non-ham groups."
94 (defcustom spam-log-to-registry nil
95 "Whether spam/ham processing should be logged in the registry."
99 (defcustom spam-split-symbolic-return nil
100 "Whether `spam-split' should work with symbols or group names."
104 (defcustom spam-split-symbolic-return-positive nil
105 "Whether `spam-split' should ALWAYS work with symbols or group names.
106 Do not set this if you use `spam-split' in a fancy split
111 (defcustom spam-process-ham-in-spam-groups nil
112 "Whether ham should be processed in spam groups."
116 (defcustom spam-mark-only-unseen-as-spam t
117 "Whether only unseen articles should be marked as spam in spam groups.
118 When nil, all unread articles in a spam group are marked as
119 spam. Set this if you want to leave an article unread in a spam group
120 without losing it to the automatic spam-marking process."
124 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
125 "Whether ham should be marked unread before it's moved.
126 The article is moved out of a spam group according to ham-process-destination.
127 This variable is an official entry in the international Longest Variable Name
132 (defcustom spam-disable-spam-split-during-ham-respool nil
133 "Whether `spam-split' should be ignored while resplitting ham in a process
134 destination. This is useful to prevent ham from ending up in the same spam
135 group after the resplit. Don't set this to t if you have spam-split as the
136 last rule in your split configuration."
140 (defcustom spam-autodetect-recheck-messages nil
141 "Should spam.el recheck all meessages when autodetecting?
142 Normally this is nil, so only unseen messages will be checked."
146 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
147 "The location of the whitelist.
148 The file format is one regular expression per line.
149 The regular expression is matched against the address."
153 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
154 "The location of the blacklist.
155 The file format is one regular expression per line.
156 The regular expression is matched against the address."
160 (defcustom spam-use-dig t
161 "Whether `query-dig' should be used instead of `query-dns'."
165 (defcustom spam-use-blacklist nil
166 "Whether the blacklist should be used by `spam-split'."
170 (defcustom spam-blacklist-ignored-regexes nil
171 "Regular expressions that the blacklist should ignore."
172 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
175 (defcustom spam-use-whitelist nil
176 "Whether the whitelist should be used by `spam-split'."
180 (defcustom spam-use-whitelist-exclusive nil
181 "Whether whitelist-exclusive should be used by `spam-split'.
182 Exclusive whitelisting means that all messages from senders not in the whitelist
183 are considered spam."
187 (defcustom spam-use-blackholes nil
188 "Whether blackholes should be used by `spam-split'."
192 (defcustom spam-use-hashcash nil
193 "Whether hashcash payments should be detected by `spam-split'."
197 (defcustom spam-use-regex-headers nil
198 "Whether a header regular expression match should be used by `spam-split'.
199 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
203 (defcustom spam-use-regex-body nil
204 "Whether a body regular expression match should be used by `spam-split'.
205 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
209 (defcustom spam-use-bogofilter-headers nil
210 "Whether bogofilter headers should be used by `spam-split'.
211 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
215 (defcustom spam-use-bogofilter nil
216 "Whether bogofilter should be invoked by `spam-split'.
217 Enable this if you want Gnus to invoke Bogofilter on new messages."
221 (defcustom spam-use-BBDB nil
222 "Whether BBDB should be used by `spam-split'."
226 (defcustom spam-use-BBDB-exclusive nil
227 "Whether BBDB-exclusive should be used by `spam-split'.
228 Exclusive BBDB means that all messages from senders not in the BBDB are
233 (defcustom spam-use-ifile nil
234 "Whether ifile should be used by `spam-split'."
238 (defcustom spam-use-stat nil
239 "Whether `spam-stat' should be used by `spam-split'."
243 (defcustom spam-use-spamoracle nil
244 "Whether spamoracle should be used by `spam-split'."
248 (defcustom spam-install-hooks (or
252 spam-use-whitelist-exclusive
255 spam-use-regex-headers
257 spam-use-bogofilter-headers
260 spam-use-BBDB-exclusive
264 "Whether the spam hooks should be installed.
265 Default to t if one of the spam-use-* variables is set."
269 (defcustom spam-split-group "spam"
270 "Group name where incoming spam should be put by `spam-split'."
274 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
275 ;;; not regular expressions
276 (defcustom spam-junk-mailgroups (cons
278 '("mail.junk" "poste.pourriel"))
279 "Mailgroups with spam contents.
280 All unmarked article in such group receive the spam mark on group entry."
281 :type '(repeat (string :tag "Group"))
284 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
285 "dev.null.dk" "relays.visi.com")
286 "List of blackhole servers."
287 :type '(repeat (string :tag "Server"))
290 (defcustom spam-blackhole-good-server-regex nil
291 "String matching IP addresses that should not be checked in the blackholes."
292 :type '(radio (const nil)
293 (regexp :format "%t: %v\n" :size 0))
296 (defcustom spam-face 'gnus-splash-face
297 "Face for spam-marked articles."
301 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
302 "Regular expression for positive header spam matches."
303 :type '(repeat (regexp :tag "Regular expression to match spam header"))
306 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
307 "Regular expression for positive header ham matches."
308 :type '(repeat (regexp :tag "Regular expression to match ham header"))
311 (defcustom spam-regex-body-spam '()
312 "Regular expression for positive body spam matches."
313 :type '(repeat (regexp :tag "Regular expression to match spam body"))
316 (defcustom spam-regex-body-ham '()
317 "Regular expression for positive body ham matches."
318 :type '(repeat (regexp :tag "Regular expression to match ham body"))
321 (defgroup spam-ifile nil
322 "Spam ifile configuration."
325 (defcustom spam-ifile-path (executable-find "ifile")
326 "File path of the ifile executable program."
327 :type '(choice (file :tag "Location of ifile")
328 (const :tag "ifile is not installed"))
331 (defcustom spam-ifile-database-path nil
332 "File path of the ifile database."
333 :type '(choice (file :tag "Location of the ifile database")
334 (const :tag "Use the default"))
337 (defcustom spam-ifile-spam-category "spam"
338 "Name of the spam ifile category."
342 (defcustom spam-ifile-ham-category nil
343 "Name of the ham ifile category.
344 If nil, the current group name will be used."
345 :type '(choice (string :tag "Use a fixed category")
346 (const :tag "Use the current group name"))
349 (defcustom spam-ifile-all-categories nil
350 "Whether the ifile check will return all categories, or just spam.
351 Set this to t if you want to use the `spam-split' invocation of ifile as
352 your main source of newsgroup names."
356 (defgroup spam-bogofilter nil
357 "Spam bogofilter configuration."
360 (defcustom spam-bogofilter-path (executable-find "bogofilter")
361 "File path of the Bogofilter executable program."
362 :type '(choice (file :tag "Location of bogofilter")
363 (const :tag "Bogofilter is not installed"))
364 :group 'spam-bogofilter)
366 (defcustom spam-bogofilter-header "X-Bogosity"
367 "The header that Bogofilter inserts in messages."
369 :group 'spam-bogofilter)
371 (defcustom spam-bogofilter-spam-switch "-s"
372 "The switch that Bogofilter uses to register spam messages."
374 :group 'spam-bogofilter)
376 (defcustom spam-bogofilter-ham-switch "-n"
377 "The switch that Bogofilter uses to register ham messages."
379 :group 'spam-bogofilter)
381 (defcustom spam-bogofilter-spam-strong-switch "-S"
382 "The switch that Bogofilter uses to unregister ham messages."
384 :group 'spam-bogofilter)
386 (defcustom spam-bogofilter-ham-strong-switch "-N"
387 "The switch that Bogofilter uses to unregister spam messages."
389 :group 'spam-bogofilter)
391 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
392 "The regex on `spam-bogofilter-header' for positive spam identification."
394 :group 'spam-bogofilter)
396 (defcustom spam-bogofilter-database-directory nil
397 "Directory path of the Bogofilter databases."
398 :type '(choice (directory
399 :tag "Location of the Bogofilter database directory")
400 (const :tag "Use the default"))
401 :group 'spam-bogofilter)
403 (defgroup spam-spamoracle nil
404 "Spam spamoracle configuration."
407 (defcustom spam-spamoracle-database nil
408 "Location of spamoracle database file. When nil, use the default
409 spamoracle database."
410 :type '(choice (directory :tag "Location of spamoracle database file.")
411 (const :tag "Use the default"))
412 :group 'spam-spamoracle)
414 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
415 "Location of the spamoracle binary."
416 :type '(choice (directory :tag "Location of the spamoracle binary")
417 (const :tag "Use the default"))
418 :group 'spam-spamoracle)
420 ;;; Key bindings for spam control.
422 (gnus-define-keys gnus-summary-mode-map
423 "St" spam-bogofilter-score
424 "Sx" gnus-summary-mark-as-spam
425 "Mst" spam-bogofilter-score
426 "Msx" gnus-summary-mark-as-spam
427 "\M-d" gnus-summary-mark-as-spam)
429 (defvar spam-old-ham-articles nil
430 "List of old ham articles, generated when a group is entered.")
432 (defvar spam-old-spam-articles nil
433 "List of old spam articles, generated when a group is entered.")
435 (defvar spam-split-disabled nil
436 "If non-nil, `spam-split' is disabled, and always returns nil.")
438 (defvar spam-split-last-successful-check nil
439 "`spam-split' will set this to nil or a spam-use-XYZ check if it
442 ;; convenience functions
443 (defun spam-xor (a b)
444 "Logical exclusive `or'."
445 (and (or a b) (not (and a b))))
447 (defun spam-group-ham-mark-p (group mark &optional spam)
448 (when (stringp group)
449 (let* ((marks (spam-group-ham-marks group spam))
450 (marks (if (symbolp mark)
452 (mapcar 'symbol-value marks))))
455 (defun spam-group-spam-mark-p (group mark)
456 (spam-group-ham-mark-p group mark t))
458 (defun spam-group-ham-marks (group &optional spam)
459 (when (stringp group)
460 (let* ((marks (if spam
461 (gnus-parameter-spam-marks group)
462 (gnus-parameter-ham-marks group)))
464 (marks (if (listp (car marks)) (car marks) marks)))
467 (defun spam-group-spam-marks (group)
468 (spam-group-ham-marks group t))
470 (defun spam-group-spam-contents-p (group)
472 (or (member group spam-junk-mailgroups)
473 (memq 'gnus-group-spam-classification-spam
474 (gnus-parameter-spam-contents group)))
477 (defun spam-group-ham-contents-p (group)
479 (memq 'gnus-group-spam-classification-ham
480 (gnus-parameter-spam-contents group))
483 (defvar spam-list-of-processors
484 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
485 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
486 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
487 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
488 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
489 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
490 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
491 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
492 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
493 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
494 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
495 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
496 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
497 "The spam-list-of-processors list contains pairs associating a
498 ham/spam exit processor variable with a classification and a
499 spam-use-* variable.")
501 (defun spam-group-processor-p (group processor)
502 (if (and (stringp group)
504 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
505 (spam-group-processor-multiple-p
507 (cdr-safe (assoc processor spam-list-of-processors))))
510 (defun spam-group-processor-multiple-p (group processor-info)
511 (let* ((classification (nth 0 processor-info))
512 (check (nth 1 processor-info))
513 (parameters (nth 0 (gnus-parameter-spam-process group)))
515 (dolist (parameter parameters)
516 (when (and (null found)
518 (eq classification (nth 0 parameter))
519 (eq check (nth 1 parameter)))
523 (defun spam-group-spam-processor-report-gmane-p (group)
524 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
526 (defun spam-group-spam-processor-bogofilter-p (group)
527 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
529 (defun spam-group-spam-processor-blacklist-p (group)
530 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
532 (defun spam-group-spam-processor-ifile-p (group)
533 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
535 (defun spam-group-ham-processor-ifile-p (group)
536 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
538 (defun spam-group-spam-processor-spamoracle-p (group)
539 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
541 (defun spam-group-ham-processor-bogofilter-p (group)
542 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
544 (defun spam-group-spam-processor-stat-p (group)
545 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
547 (defun spam-group-ham-processor-stat-p (group)
548 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
550 (defun spam-group-ham-processor-whitelist-p (group)
551 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
553 (defun spam-group-ham-processor-BBDB-p (group)
554 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
556 (defun spam-group-ham-processor-copy-p (group)
557 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
559 (defun spam-group-ham-processor-spamoracle-p (group)
560 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
562 ;;; Summary entry and exit processing.
564 (defun spam-summary-prepare ()
565 (setq spam-old-ham-articles
566 (spam-list-articles gnus-newsgroup-articles 'ham))
567 (setq spam-old-spam-articles
568 (spam-list-articles gnus-newsgroup-articles 'spam))
569 (spam-mark-junk-as-spam-routine))
571 ;; The spam processors are invoked for any group, spam or ham or neither
572 (defun spam-summary-prepare-exit ()
573 (unless gnus-group-is-exiting-without-update-p
574 (gnus-message 6 "Exiting summary buffer and applying spam rules")
576 ;; first of all, unregister any articles that are no longer ham or spam
577 ;; we have to iterate over the processors, or else we'll be too slow
578 (dolist (classification '(spam ham))
579 (let* ((old-articles (if (eq classification 'spam)
580 spam-old-spam-articles
581 spam-old-ham-articles))
582 (new-articles (spam-list-articles
583 gnus-newsgroup-articles
585 (changed-articles (gnus-set-difference old-articles new-articles)))
586 ;; now that we have the changed articles, we go through the processors
587 (dolist (processor-param spam-list-of-processors)
588 (let ((processor (nth 0 processor-param))
589 (processor-classification (nth 1 processor-param))
590 (check (nth 2 processor-param))
592 (dolist (article changed-articles)
593 (let ((id (spam-fetch-field-message-id-fast article)))
594 (when (spam-log-unregistration-needed-p
595 id 'process classification check)
596 (push article unregister-list))))
597 ;; call spam-register-routine with specific articles to unregister,
598 ;; when there are articles to unregister and the check is enabled
599 (when (and unregister-list (symbol-value check))
600 (spam-register-routine classification check t unregister-list))))))
602 ;; find all the spam processors applicable to this group
603 (dolist (processor-param spam-list-of-processors)
604 (let ((processor (nth 0 processor-param))
605 (classification (nth 1 processor-param))
606 (check (nth 2 processor-param)))
607 (when (and (eq 'spam classification)
608 (spam-group-processor-p gnus-newsgroup-name processor))
609 (spam-register-routine classification check))))
611 (if spam-move-spam-nonspam-groups-only
612 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
613 (spam-mark-spam-as-expired-and-move-routine
614 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
615 (gnus-message 5 "Marking spam as expired and moving it to %s"
617 (spam-mark-spam-as-expired-and-move-routine
618 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
620 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
621 ;; expire spam, in case the above did not expire them
622 (gnus-message 5 "Marking spam as expired without moving it")
623 (spam-mark-spam-as-expired-and-move-routine nil)
625 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
626 (and (spam-group-spam-contents-p gnus-newsgroup-name)
627 spam-process-ham-in-spam-groups)
628 spam-process-ham-in-nonham-groups)
629 ;; find all the ham 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 'ham classification)
635 (spam-group-processor-p gnus-newsgroup-name processor))
636 (spam-register-routine classification check)))))
638 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
639 (gnus-message 5 "Copying ham")
640 (spam-ham-copy-routine
641 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
643 ;; now move all ham articles out of spam groups
644 (when (spam-group-spam-contents-p gnus-newsgroup-name)
645 (gnus-message 5 "Moving ham messages from spam group")
646 (spam-ham-move-routine
647 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
649 (setq spam-old-ham-articles nil)
650 (setq spam-old-spam-articles nil))
652 (defun spam-mark-junk-as-spam-routine ()
653 ;; check the global list of group names spam-junk-mailgroups and the
655 (when (spam-group-spam-contents-p gnus-newsgroup-name)
656 (gnus-message 5 "Marking %s articles as spam"
657 (if spam-mark-only-unseen-as-spam
660 (let ((articles (if spam-mark-only-unseen-as-spam
661 gnus-newsgroup-unseen
662 gnus-newsgroup-unreads)))
663 (dolist (article articles)
664 (gnus-summary-mark-article article gnus-spam-mark)))))
666 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
667 (if (and (car-safe groups) (listp (car-safe groups)))
668 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
669 (gnus-summary-kill-process-mark)
670 (let ((articles gnus-newsgroup-articles)
671 (backend-supports-deletions
672 (gnus-check-backend-function
673 'request-move-article gnus-newsgroup-name))
674 article tomove deletep)
675 (dolist (article articles)
676 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
677 (gnus-summary-mark-article article gnus-expirable-mark)
678 (push article tomove)))
680 ;; now do the actual copies
681 (dolist (group groups)
684 (dolist (article tomove)
685 (gnus-summary-set-process-mark article))
687 (if (or (not backend-supports-deletions)
688 (> (length groups) 1))
690 (gnus-summary-copy-article nil group)
692 (gnus-summary-move-article nil group)))))
694 ;; now delete the articles, if there was a copy done, and the
696 (when (and deletep backend-supports-deletions)
697 (dolist (article tomove)
698 (gnus-summary-set-process-mark article))
700 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
701 (gnus-summary-delete-article nil))))
703 (gnus-summary-yank-process-mark))))
705 (defun spam-ham-copy-or-move-routine (copy groups)
706 (gnus-summary-kill-process-mark)
707 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
708 (backend-supports-deletions
709 (gnus-check-backend-function
710 'request-move-article gnus-newsgroup-name))
711 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
712 article mark todo deletep respool)
714 (when (member 'respool groups)
715 (setq respool t) ; boolean for later
716 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
718 ;; now do the actual move
719 (dolist (group groups)
720 (when (and todo (stringp group))
721 (dolist (article todo)
722 (when spam-mark-ham-unread-before-move-from-spam-group
723 (gnus-summary-mark-article article gnus-unread-mark))
724 (gnus-summary-set-process-mark article))
726 (if respool ; respooling is with a "fake" group
727 (let ((spam-split-disabled
728 (or spam-split-disabled
729 spam-disable-spam-split-during-ham-respool)))
730 (gnus-summary-respool-article nil respool-method))
731 (if (or (not backend-supports-deletions) ; else, we are not respooling
732 (> (length groups) 1))
733 (progn ; if copying, copy and set deletep
734 (gnus-summary-copy-article nil group)
736 (gnus-summary-move-article nil group))))) ; else move articles
738 ;; now delete the articles, unless a) copy is t, and there was a copy done
739 ;; b) a move was done to a single group
740 ;; c) backend-supports-deletions is nil
742 (when (and deletep backend-supports-deletions)
743 (dolist (article todo)
744 (gnus-summary-set-process-mark article))
746 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
747 (gnus-summary-delete-article nil))))))
749 (gnus-summary-yank-process-mark))
751 (defun spam-ham-copy-routine (&rest groups)
752 (if (and (car-safe groups) (listp (car-safe groups)))
753 (apply 'spam-ham-copy-routine (car groups))
754 (spam-ham-copy-or-move-routine t groups)))
756 (defun spam-ham-move-routine (&rest groups)
757 (if (and (car-safe groups) (listp (car-safe groups)))
758 (apply 'spam-ham-move-routine (car groups))
759 (spam-ham-copy-or-move-routine nil groups)))
761 (defun spam-get-article-as-string (article)
762 (let ((article-buffer (spam-get-article-as-buffer article))
765 (save-window-excursion
766 (set-buffer article-buffer)
767 (setq article-string (buffer-string))))
770 (defun spam-get-article-as-buffer (article)
771 (let ((article-buffer))
772 (when (numberp article)
773 (save-window-excursion
774 (gnus-summary-goto-subject article)
775 (gnus-summary-show-article t)
776 (setq article-buffer (get-buffer gnus-article-buffer))))
780 ;; (defun spam-get-article-as-filename (article)
781 ;; (let ((article-filename))
782 ;; (when (numberp article)
783 ;; (nnml-possibly-change-directory
784 ;; (gnus-group-real-name gnus-newsgroup-name))
785 ;; (setq article-filename (expand-file-name
786 ;; (int-to-string article) nnml-current-directory)))
787 ;; (if (file-exists-p article-filename)
791 (defun spam-fetch-field-from-fast (article)
792 "Fetch the `from' field quickly, using the internal gnus-data-list function"
793 (if (and (numberp article)
794 (assoc article (gnus-data-list nil)))
796 (gnus-data-header (assoc article (gnus-data-list nil))))
799 (defun spam-fetch-field-subject-fast (article)
800 "Fetch the `subject' field quickly, using the internal
801 gnus-data-list function"
802 (if (and (numberp article)
803 (assoc article (gnus-data-list nil)))
805 (gnus-data-header (assoc article (gnus-data-list nil))))
808 (defun spam-fetch-field-message-id-fast (article)
809 "Fetch the `Message-ID' field quickly, using the internal
810 gnus-data-list function"
811 (if (and (numberp article)
812 (assoc article (gnus-data-list nil)))
813 (mail-header-message-id
814 (gnus-data-header (assoc article (gnus-data-list nil))))
818 ;;;; Spam determination.
820 (defvar spam-list-of-checks
821 '((spam-use-blacklist . spam-check-blacklist)
822 (spam-use-regex-headers . spam-check-regex-headers)
823 (spam-use-regex-body . spam-check-regex-body)
824 (spam-use-whitelist . spam-check-whitelist)
825 (spam-use-BBDB . spam-check-BBDB)
826 (spam-use-ifile . spam-check-ifile)
827 (spam-use-spamoracle . spam-check-spamoracle)
828 (spam-use-stat . spam-check-stat)
829 (spam-use-blackholes . spam-check-blackholes)
830 (spam-use-hashcash . spam-check-hashcash)
831 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
832 (spam-use-bogofilter . spam-check-bogofilter))
833 "The spam-list-of-checks list contains pairs associating a
834 parameter variable with a spam checking function. If the
835 parameter variable is true, then the checking function is called,
836 and its value decides what happens. Each individual check may
837 return nil, t, or a mailgroup name. The value nil means that the
838 check does not yield a decision, and so, that further checks are
839 needed. The value t means that the message is definitely not
840 spam, and that further spam checks should be inhibited.
841 Otherwise, a mailgroup name or the symbol 'spam (depending on
842 spam-split-symbolic-return) is returned where the mail should go,
843 and further checks are also inhibited. The usual mailgroup name
844 is the value of `spam-split-group', meaning that the message is
847 (defvar spam-list-of-statistical-checks
853 "The spam-list-of-statistical-checks list contains all the mail
854 splitters that need to have the full message body available.")
856 ;;;TODO: modify to invoke self with each check if invoked without specifics
857 (defun spam-split (&rest specific-checks)
858 "Split this message into the `spam' group if it is spam.
859 This function can be used as an entry in the variable `nnmail-split-fancy',
860 for example like this: (: spam-split). It can take checks as
861 parameters. A string as a parameter will set the
862 spam-split-group to that string.
864 See the Info node `(gnus)Fancy Mail Splitting' for more details."
866 (setq spam-split-last-successful-check nil)
867 (unless spam-split-disabled
868 (let ((spam-split-group-choice spam-split-group))
869 (dolist (check specific-checks)
870 (when (stringp check)
871 (setq spam-split-group-choice check)
872 (setq specific-checks (delq check specific-checks))))
874 (let ((spam-split-group spam-split-group-choice))
877 (dolist (check spam-list-of-statistical-checks)
878 (when (and (symbolp check) (symbol-value check))
880 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
883 ;; (progn (widen) (debug (buffer-string)))
884 (let ((list-of-checks spam-list-of-checks)
886 (while (and list-of-checks (not decision))
887 (let ((pair (pop list-of-checks)))
888 (when (and (symbol-value (car pair))
889 (or (null specific-checks)
890 (memq (car pair) specific-checks)))
891 (gnus-message 5 "spam-split: calling the %s function"
892 (symbol-name (cdr pair)))
893 (setq decision (funcall (cdr pair)))
894 ;; if we got a decision at all, save the current check
896 (setq spam-split-last-successful-check (car pair)))
898 (when (eq decision 'spam)
899 (if spam-split-symbolic-return
900 (setq decision spam-split-group)
903 (format "spam-split got %s but %s is nil"
904 (symbol-name decision)
905 (symbol-name spam-split-symbolic-return))))))))
907 (if spam-split-symbolic-return-positive 'ham nil)
910 (defun spam-find-spam ()
911 "This function will detect spam in the current newsgroup using spam-split."
914 (let* ((group gnus-newsgroup-name)
915 (autodetect (gnus-parameter-spam-autodetect group))
916 (methods (gnus-parameter-spam-autodetect-methods group))
917 (first-method (nth 0 methods)))
918 (when (and autodetect
919 (not (equal first-method 'none)))
922 (let ((id (spam-fetch-field-message-id-fast article))
923 (subject (spam-fetch-field-subject-fast article))
924 (sender (spam-fetch-field-from-fast article)))
925 (unless (and spam-log-to-registry
926 (spam-log-registered-p id 'incoming))
927 (let* ((spam-split-symbolic-return t)
928 (spam-split-symbolic-return-positive t)
931 (gnus-request-article-this-buffer
934 (if (or (null first-method)
935 (equal first-method 'default))
937 (apply 'spam-split methods)))))
938 (if (equal split-return 'spam)
939 (gnus-summary-mark-article article gnus-spam-mark))
941 (when (and split-return spam-log-to-registry)
942 (when (zerop (gnus-registry-group-count id))
943 (gnus-registry-add-group
944 id group subject sender))
946 (spam-log-processing-to-registry
950 spam-split-last-successful-check
952 (if spam-autodetect-recheck-messages
953 gnus-newsgroup-articles
954 gnus-newsgroup-unseen)))))
956 (defvar spam-registration-functions
957 ;; first the ham register, second the spam register function
958 ;; third the ham unregister, fourth the spam unregister function
959 '((spam-use-blacklist nil
960 spam-blacklist-register-routine
962 spam-blacklist-unregister-routine)
963 (spam-use-whitelist spam-whitelist-register-routine
965 spam-whitelist-unregister-routine
967 (spam-use-BBDB spam-BBDB-register-routine
969 spam-BBDB-unregister-routine
971 (spam-use-ifile spam-ifile-register-ham-routine
972 spam-ifile-register-spam-routine
973 spam-ifile-unregister-ham-routine
974 spam-ifile-unregister-spam-routine)
975 (spam-use-spamoracle spam-spamoracle-learn-ham
976 spam-spamoracle-learn-spam
977 spam-spamoracle-unlearn-ham
978 spam-spamoracle-unlearn-spam)
979 (spam-use-stat spam-stat-register-ham-routine
980 spam-stat-register-spam-routine
981 spam-stat-unregister-ham-routine
982 spam-stat-unregister-spam-routine)
983 ;; note that spam-use-gmane is not a legitimate check
985 spam-report-gmane-register-routine
986 ;; does Gmane support unregistration?
989 (spam-use-bogofilter spam-bogofilter-register-ham-routine
990 spam-bogofilter-register-spam-routine
991 spam-bogofilter-unregister-ham-routine
992 spam-bogofilter-unregister-spam-routine))
993 "The spam-registration-functions list contains pairs
994 associating a parameter variable with the ham and spam
995 registration functions, and the ham and spam unregistration
998 (defun spam-classification-valid-p (classification)
999 (or (eq classification 'spam)
1000 (eq classification 'ham)))
1002 (defun spam-process-type-valid-p (process-type)
1003 (or (eq process-type 'incoming)
1004 (eq process-type 'process)))
1006 (defun spam-registration-check-valid-p (check)
1007 (assoc check spam-registration-functions))
1009 (defun spam-unregistration-check-valid-p (check)
1010 (assoc check spam-registration-functions))
1012 (defun spam-registration-function (classification check)
1013 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1014 (if (eq classification 'spam)
1018 (defun spam-unregistration-function (classification check)
1019 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1020 (if (eq classification 'spam)
1024 (defun spam-list-articles (articles classification)
1025 (let ((mark-check (if (eq classification 'spam)
1026 'spam-group-spam-mark-p
1027 'spam-group-ham-mark-p))
1028 list mark-cache-yes mark-cache-no)
1029 (dolist (article articles)
1030 (let ((mark (gnus-summary-article-mark article)))
1031 (unless (memq mark mark-cache-no)
1032 (if (memq mark mark-cache-yes)
1034 ;; else, we have to actually check the mark
1035 (if (funcall mark-check
1040 (push mark mark-cache-yes))
1041 (push mark mark-cache-no))))))
1044 (defun spam-register-routine (classification
1046 &optional unregister
1048 (when (and (spam-classification-valid-p classification)
1049 (spam-registration-check-valid-p check))
1050 (let* ((register-function
1051 (spam-registration-function classification check))
1052 (unregister-function
1053 (spam-unregistration-function classification check))
1054 (run-function (if unregister
1057 (log-function (if unregister
1058 'spam-log-undo-registration
1059 'spam-log-processing-to-registry))
1063 ;; make list of articles, using specific-articles if given
1064 (setq articles (or specific-articles
1066 gnus-newsgroup-articles
1069 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1070 (if unregister "Unregistering" "Registering")
1072 (if specific-articles "specific" "")
1073 (symbol-name classification)
1074 (symbol-name check))
1075 (funcall run-function articles)
1076 ;; now log all the registrations (or undo them, depending on unregister)
1077 (dolist (article articles)
1078 (funcall log-function
1079 (spam-fetch-field-message-id-fast article)
1083 gnus-newsgroup-name))))))
1085 ;;; log a ham- or spam-processor invocation to the registry
1086 (defun spam-log-processing-to-registry (id type classification check group)
1087 (when spam-log-to-registry
1088 (if (and (stringp id)
1090 (spam-process-type-valid-p type)
1091 (spam-classification-valid-p classification)
1092 (spam-registration-check-valid-p check))
1093 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1094 (cell (list classification check group)))
1095 (push cell cell-list)
1096 (gnus-registry-store-extra-entry
1101 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1102 "spam-log-processing-to-registry")))))
1104 ;;; check if a ham- or spam-processor registration has been done
1105 (defun spam-log-registered-p (id type)
1106 (when spam-log-to-registry
1107 (if (and (stringp id)
1108 (spam-process-type-valid-p type))
1109 (cdr-safe (gnus-registry-fetch-extra id type))
1111 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1112 "spam-log-registered-p"))
1115 ;;; check if a ham- or spam-processor registration needs to be undone
1116 (defun spam-log-unregistration-needed-p (id type classification check)
1117 (when spam-log-to-registry
1118 (if (and (stringp id)
1119 (spam-process-type-valid-p type)
1120 (spam-classification-valid-p classification)
1121 (spam-registration-check-valid-p check))
1122 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1124 (dolist (cell cell-list)
1126 (when (and (eq classification (nth 0 cell))
1127 (eq check (nth 1 cell)))
1131 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1132 "spam-log-unregistration-needed-p"))
1136 ;;; undo a ham- or spam-processor registration (the group is not used)
1137 (defun spam-log-undo-registration (id type classification check &optional group)
1138 (when (and spam-log-to-registry
1139 (spam-log-unregistration-needed-p id type classification check))
1140 (if (and (stringp id)
1141 (spam-process-type-valid-p type)
1142 (spam-classification-valid-p classification)
1143 (spam-registration-check-valid-p check))
1144 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1145 new-cell-list found)
1146 (dolist (cell cell-list)
1147 (unless (and (eq classification (nth 0 cell))
1148 (eq check (nth 1 cell)))
1149 (push cell new-cell-list)))
1150 (gnus-registry-store-extra-entry
1155 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1156 "spam-log-undo-registration"))
1159 ;;; set up IMAP widening if it's necessary
1160 (defun spam-setup-widening ()
1161 (dolist (check spam-list-of-statistical-checks)
1162 (when (symbol-value check)
1163 (setq nnimap-split-download-body-default t))))
1168 (defun spam-check-regex-body ()
1169 (let ((spam-regex-headers-ham spam-regex-body-ham)
1170 (spam-regex-headers-spam spam-regex-body-spam))
1171 (spam-check-regex-headers t)))
1176 (defun spam-check-regex-headers (&optional body)
1177 (let ((type (if body "body" "header"))
1178 (spam-split-group (if spam-split-symbolic-return
1182 (dolist (h-regex spam-regex-headers-ham)
1184 (goto-char (point-min))
1185 (when (re-search-forward h-regex nil t)
1186 (message "Ham regex %s search positive." type)
1188 (dolist (s-regex spam-regex-headers-spam)
1190 (goto-char (point-min))
1191 (when (re-search-forward s-regex nil t)
1192 (message "Spam regex %s search positive." type)
1194 (setq ret spam-split-group))))
1200 (defun spam-reverse-ip-string (ip)
1202 (mapconcat 'identity
1203 (nreverse (split-string ip "\\."))
1206 (defun spam-check-blackholes ()
1207 "Check the Received headers for blackholed relays."
1208 (let ((headers (nnmail-fetch-field "received"))
1209 (spam-split-group (if spam-split-symbolic-return
1216 (goto-char (point-min))
1217 (gnus-message 5 "Checking headers for relay addresses")
1218 (while (re-search-forward
1219 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1220 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1221 (push (spam-reverse-ip-string (match-string 1))
1223 (dolist (server spam-blackhole-servers)
1225 (unless (and spam-blackhole-good-server-regex
1226 ;; match the good-server-regex against the reversed (again) IP string
1228 spam-blackhole-good-server-regex
1229 (spam-reverse-ip-string ip)))
1231 (let ((query-string (concat ip "." server)))
1233 (let ((query-result (query-dig query-string)))
1235 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1237 (push (list ip server query-result)
1239 ;; else, if not using dig.el
1240 (when (query-dns query-string)
1241 (gnus-message 5 "positive blackhole check")
1242 (push (list ip server (query-dns query-string 'TXT))
1253 (defun spam-check-hashcash ()
1254 "Check the headers for hashcash payments."
1255 (mail-check-payment))) ;mail-check-payment returns a boolean
1258 (defalias 'mail-check-payment 'ignore)
1259 (defalias 'spam-check-hashcash 'ignore))))
1263 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1264 ;;; <sacha@giotto.sj.ru>
1266 ;; all this is done inside a condition-case to trap errors
1273 (defun spam-enter-ham-BBDB (addresses &optional remove)
1274 "Enter an address into the BBDB; implies ham (non-spam) sender"
1275 (dolist (from addresses)
1276 (when (stringp from)
1277 (let* ((parsed-address (gnus-extract-address-components from))
1278 (name (or (nth 0 parsed-address) "Ham Sender"))
1279 (remove-function (if remove
1280 'bbdb-delete-record-internal
1282 (net-address (nth 1 parsed-address))
1283 (record (and net-address
1284 (bbdb-search-simple nil net-address))))
1286 (gnus-message 5 "%s address %s %s BBDB"
1287 (if remove "Deleting" "Adding")
1289 (if remove "from" "to"))
1291 (funcall remove-function record)
1292 (bbdb-create-internal name nil net-address nil nil
1293 "ham sender added by spam.el")))))))
1295 (defun spam-BBDB-register-routine (articles &optional unregister)
1297 (dolist (article articles)
1298 (when (stringp (spam-fetch-field-from-fast article))
1299 (push (spam-fetch-field-from-fast article) addresses)))
1300 ;; now do the register/unregister action
1301 (spam-enter-ham-BBDB addresses unregister)))
1303 (defun spam-BBDB-unregister-routine (articles)
1304 (spam-BBDB-register-routine articles t))
1306 (defun spam-check-BBDB ()
1307 "Mail from people in the BBDB is classified as ham or non-spam"
1308 (let ((who (nnmail-fetch-field "from"))
1309 (spam-split-group (if spam-split-symbolic-return
1313 (setq who (nth 1 (gnus-extract-address-components who)))
1314 (if (bbdb-search-simple nil who)
1316 (if spam-use-BBDB-exclusive
1321 (defalias 'bbdb-search-simple 'ignore)
1322 (defalias 'spam-check-BBDB 'ignore)
1323 (defalias 'spam-BBDB-register-routine 'ignore)
1324 (defalias 'spam-enter-ham-BBDB 'ignore)
1325 (defalias 'bbdb-create-internal 'ignore)
1326 (defalias 'bbdb-delete-record-internal 'ignore)
1327 (defalias 'bbdb-records 'ignore))))
1332 ;;; check the ifile backend; return nil if the mail was NOT classified
1335 (defun spam-get-ifile-database-parameter ()
1336 "Get the command-line parameter for ifile's database from
1337 spam-ifile-database-path."
1338 (if spam-ifile-database-path
1339 (format "--db-file=%s" spam-ifile-database-path)
1342 (defun spam-check-ifile ()
1343 "Check the ifile backend for the classification of this message."
1344 (let ((article-buffer-name (buffer-name))
1345 (spam-split-group (if spam-split-symbolic-return
1350 (let ((temp-buffer-name (buffer-name))
1351 (db-param (spam-get-ifile-database-parameter)))
1353 (set-buffer article-buffer-name)
1354 (apply 'call-process-region
1355 (point-min) (point-max) spam-ifile-path
1356 nil temp-buffer-name nil "-c"
1357 (if db-param `(,db-param "-q") `("-q"))))
1358 ;; check the return now (we're back in the temp buffer)
1359 (goto-char (point-min))
1361 (setq category (buffer-substring (point) (point-at-eol))))
1362 (when (not (zerop (length category))) ; we need a category here
1363 (if spam-ifile-all-categories
1364 (setq return category)
1365 ;; else, if spam-ifile-all-categories is not set...
1366 (when (string-equal spam-ifile-spam-category category)
1367 (setq return spam-split-group)))))) ; note return is nil otherwise
1370 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1371 "Register an article, given as a string, with a category.
1372 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1373 (let ((category (or category gnus-newsgroup-name))
1374 (add-or-delete-option (if unregister "-d" "-i"))
1375 (db (spam-get-ifile-database-parameter))
1378 (dolist (article articles)
1379 (let ((article-string (spam-get-article-as-string article)))
1380 (when (stringp article-string)
1381 (insert article-string))))
1382 (apply 'call-process-region
1383 (point-min) (point-max) spam-ifile-path
1385 add-or-delete-option category
1386 (if db `(,db "-h") `("-h"))))))
1388 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1389 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1391 (defun spam-ifile-unregister-spam-routine (articles)
1392 (spam-ifile-register-spam-routine articles t))
1394 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1395 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1397 (defun spam-ifile-unregister-ham-routine (articles)
1398 (spam-ifile-register-ham-routine articles t))
1405 (let ((spam-stat-install-hooks nil))
1406 (require 'spam-stat))
1408 (defun spam-check-stat ()
1409 "Check the spam-stat backend for the classification of this message"
1410 (let ((spam-split-group (if spam-split-symbolic-return
1413 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1414 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1416 (spam-stat-split-fancy)))
1418 (defun spam-stat-register-spam-routine (articles &optional unregister)
1419 (dolist (article articles)
1420 (let ((article-string (spam-get-article-as-string article)))
1422 (insert article-string)
1424 (spam-stat-buffer-change-to-non-spam)
1425 (spam-stat-buffer-is-spam))))))
1427 (defun spam-stat-unregister-spam-routine (articles)
1428 (spam-stat-register-spam-routine articles t))
1430 (defun spam-stat-register-ham-routine (articles &optional unregister)
1431 (dolist (article articles)
1432 (let ((article-string (spam-get-article-as-string article)))
1434 (insert article-string)
1436 (spam-stat-buffer-change-to-spam)
1437 (spam-stat-buffer-is-non-spam))))))
1439 (defun spam-stat-unregister-ham-routine (articles)
1440 (spam-stat-register-ham-routine articles t))
1442 (defun spam-maybe-spam-stat-load ()
1443 (when spam-use-stat (spam-stat-load)))
1445 (defun spam-maybe-spam-stat-save ()
1446 (when spam-use-stat (spam-stat-save))))
1449 (defalias 'spam-stat-load 'ignore)
1450 (defalias 'spam-stat-save 'ignore)
1451 (defalias 'spam-maybe-spam-stat-load 'ignore)
1452 (defalias 'spam-maybe-spam-stat-save 'ignore)
1453 (defalias 'spam-stat-register-ham-routine 'ignore)
1454 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1455 (defalias 'spam-stat-register-spam-routine 'ignore)
1456 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1457 (defalias 'spam-stat-buffer-is-spam 'ignore)
1458 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1459 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1460 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1461 (defalias 'spam-stat-split-fancy 'ignore)
1462 (defalias 'spam-check-stat 'ignore))))
1466 ;;;; Blacklists and whitelists.
1468 (defvar spam-whitelist-cache nil)
1469 (defvar spam-blacklist-cache nil)
1471 (defun spam-kill-whole-line ()
1473 (let ((kill-whole-line t))
1476 ;;; address can be a list, too
1477 (defun spam-enter-whitelist (address &optional remove)
1478 "Enter ADDRESS (list or single) into the whitelist.
1479 With a non-nil REMOVE, remove them."
1480 (interactive "sAddress: ")
1481 (spam-enter-list address spam-whitelist remove)
1482 (setq spam-whitelist-cache nil))
1484 ;;; address can be a list, too
1485 (defun spam-enter-blacklist (address &optional remove)
1486 "Enter ADDRESS (list or single) into the blacklist.
1487 With a non-nil REMOVE, remove them."
1488 (interactive "sAddress: ")
1489 (spam-enter-list address spam-blacklist remove)
1490 (setq spam-blacklist-cache nil))
1492 (defun spam-enter-list (addresses file &optional remove)
1493 "Enter ADDRESSES into the given FILE.
1494 Either the whitelist or the blacklist files can be used. With
1495 REMOVE not nil, remove the ADDRESSES."
1496 (if (stringp addresses)
1497 (spam-enter-list (list addresses) file remove)
1498 ;; else, we have a list of addresses here
1499 (unless (file-exists-p (file-name-directory file))
1500 (make-directory (file-name-directory file) t))
1503 (find-file-noselect file))
1504 (dolist (a addresses)
1506 (goto-char (point-min))
1507 (if (re-search-forward (regexp-quote a) nil t)
1508 ;; found the address
1510 (spam-kill-whole-line))
1511 ;; else, the address was not found
1513 (goto-char (point-max))
1519 ;;; returns t if the sender is in the whitelist, nil or
1520 ;;; spam-split-group otherwise
1521 (defun spam-check-whitelist ()
1522 ;; FIXME! Should it detect when file timestamps change?
1523 (let ((spam-split-group (if spam-split-symbolic-return
1526 (unless spam-whitelist-cache
1527 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1528 (if (spam-from-listed-p spam-whitelist-cache)
1530 (if spam-use-whitelist-exclusive
1534 (defun spam-check-blacklist ()
1535 ;; FIXME! Should it detect when file timestamps change?
1536 (let ((spam-split-group (if spam-split-symbolic-return
1539 (unless spam-blacklist-cache
1540 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1541 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1543 (defun spam-parse-list (file)
1544 (when (file-readable-p file)
1545 (let (contents address)
1547 (insert-file-contents file)
1549 (setq address (buffer-substring (point) (point-at-eol)))
1551 ;; insert the e-mail address if detected, otherwise the raw data
1552 (unless (zerop (length address))
1553 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1554 (push (or pure-address address) contents)))))
1555 (nreverse contents))))
1557 (defun spam-from-listed-p (cache)
1558 (let ((from (nnmail-fetch-field "from"))
1561 (let ((address (pop cache)))
1562 (unless (zerop (length address)) ; 0 for a nil address too
1563 (setq address (regexp-quote address))
1564 ;; fix regexp-quote's treatment of user-intended regexes
1565 (while (string-match "\\\\\\*" address)
1566 (setq address (replace-match ".*" t t address))))
1567 (when (and address (string-match address from))
1572 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1573 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1574 (declassification (if blacklist 'ham 'spam))
1576 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1578 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1579 from addresses unregister-list)
1580 (dolist (article articles)
1581 (let ((from (spam-fetch-field-from-fast article))
1582 (id (spam-fetch-field-message-id-fast article))
1584 (when (stringp from)
1585 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1586 (when (and (not sender-ignored)
1587 (stringp ignore-regex)
1588 (string-match ignore-regex from))
1589 (setq sender-ignored t)))
1590 ;; remember the messages we need to unregister, unless remove is set
1593 (spam-log-unregistration-needed-p
1594 id 'process declassification de-symbol))
1595 (push from unregister-list))
1596 (unless sender-ignored
1597 (push from addresses)))))
1600 (funcall enter-function addresses t) ; unregister all these addresses
1601 ;; else, register normally and unregister what we need to
1602 (funcall remove-function unregister-list t)
1603 (dolist (article unregister-list)
1604 (spam-log-undo-registration
1605 (spam-fetch-field-message-id-fast article)
1609 (funcall enter-function addresses nil))))
1611 (defun spam-blacklist-unregister-routine (articles)
1612 (spam-blacklist-register-routine articles t))
1614 (defun spam-blacklist-register-routine (articles &optional unregister)
1615 (spam-filelist-register-routine articles t unregister))
1617 (defun spam-whitelist-unregister-routine (articles)
1618 (spam-whitelist-register-routine articles t))
1620 (defun spam-whitelist-register-routine (articles &optional unregister)
1621 (spam-filelist-register-routine articles nil unregister))
1624 ;;;; Spam-report glue
1625 (defun spam-report-gmane-register-routine (articles)
1627 (apply 'spam-report-gmane articles)))
1631 (defun spam-check-bogofilter-headers (&optional score)
1632 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1633 (spam-split-group (if spam-split-symbolic-return
1636 (when header ; return nil when no header
1637 (if score ; scoring mode
1638 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1639 (match-string 1 header)
1641 ;; spam detection mode
1642 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1644 spam-split-group)))))
1646 ;; return something sensible if the score can't be determined
1647 (defun spam-bogofilter-score ()
1648 "Get the Bogofilter spamicity score"
1650 (save-window-excursion
1651 (gnus-summary-show-article t)
1652 (set-buffer gnus-article-buffer)
1653 (let ((score (or (spam-check-bogofilter-headers t)
1654 (spam-check-bogofilter t))))
1655 (message "Spamicity score %s" score)
1657 (gnus-summary-show-article)))
1659 (defun spam-check-bogofilter (&optional score)
1660 "Check the Bogofilter backend for the classification of this message"
1661 (let ((article-buffer-name (buffer-name))
1662 (db spam-bogofilter-database-directory)
1665 (let ((temp-buffer-name (buffer-name)))
1667 (set-buffer article-buffer-name)
1668 (apply 'call-process-region
1669 (point-min) (point-max)
1670 spam-bogofilter-path
1671 nil temp-buffer-name nil
1672 (if db `("-d" ,db "-v") `("-v"))))
1673 (setq return (spam-check-bogofilter-headers score))))
1676 (defun spam-bogofilter-register-with-bogofilter (articles
1678 &optional unregister)
1679 "Register an article, given as a string, as spam or non-spam."
1680 (dolist (article articles)
1681 (let ((article-string (spam-get-article-as-string article))
1682 (db spam-bogofilter-database-directory)
1683 (switch (if unregister
1685 spam-bogofilter-spam-strong-switch
1686 spam-bogofilter-ham-strong-switch)
1688 spam-bogofilter-spam-switch
1689 spam-bogofilter-ham-switch))))
1690 (when (stringp article-string)
1692 (insert article-string)
1694 (apply 'call-process-region
1695 (point-min) (point-max)
1696 spam-bogofilter-path
1698 (if db `("-d" ,db "-v") `("-v"))))))))
1700 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1701 (spam-bogofilter-register-with-bogofilter articles t unregister))
1703 (defun spam-bogofilter-unregister-spam-routine (articles)
1704 (spam-bogofilter-register-spam-routine articles t))
1706 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1707 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1709 (defun spam-bogofilter-unregister-ham-routine (articles)
1710 (spam-bogofilter-register-ham-routine articles t))
1715 (defun spam-check-spamoracle ()
1716 "Run spamoracle on an article to determine whether it's spam."
1717 (let ((article-buffer-name (buffer-name))
1718 (spam-split-group (if spam-split-symbolic-return
1722 (let ((temp-buffer-name (buffer-name)))
1724 (set-buffer article-buffer-name)
1726 (apply 'call-process-region
1727 (point-min) (point-max)
1728 spam-spamoracle-binary
1729 nil temp-buffer-name nil
1730 (if spam-spamoracle-database
1731 `("-f" ,spam-spamoracle-database "mark")
1735 (set-buffer temp-buffer-name)
1736 (goto-char (point-min))
1737 (when (re-search-forward "^X-Spam: yes;" nil t)
1739 (error "Error running spamoracle" status))))))))
1741 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1742 "Run spamoracle in training mode."
1744 (let ((temp-buffer-name (buffer-name)))
1746 (goto-char (point-min))
1747 (dolist (article articles)
1748 (insert (spam-get-article-as-string article)))
1749 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1753 (apply 'call-process-region
1754 (point-min) (point-max)
1755 spam-spamoracle-binary
1756 nil temp-buffer-name nil
1757 (if spam-spamoracle-database
1758 `("-f" ,spam-spamoracle-database
1761 (when (not (eq 0 status))
1762 (error "Error running spamoracle" status)))))))
1764 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1765 (spam-spamoracle-learn articles nil unregister))
1767 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1768 (spam-spamoracle-learn-ham articles t))
1770 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1771 (spam-spamoracle-learn articles t unregister))
1773 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1774 (spam-spamoracle-learn-spam articles t))
1780 (defun spam-initialize ()
1781 "Install the spam.el hooks and do other initialization"
1783 (setq spam-install-hooks t)
1784 ;; TODO: How do we redo this every time spam-face is customized?
1785 (push '((eq mark gnus-spam-mark) . spam-face)
1786 gnus-summary-highlight)
1787 ;; Add hooks for loading and saving the spam stats
1788 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1789 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1790 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1791 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1792 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1793 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1794 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1796 (defun spam-unload-hook ()
1797 "Uninstall the spam.el hooks"
1799 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1800 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1801 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1802 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1803 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1804 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1805 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1807 (when spam-install-hooks
1812 ;;; spam.el ends here.
1816 ;;; spam.el ends here