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))
52 ;; autoload executable-find
54 ;; executable-find is not autoloaded in Emacs 20
55 (autoload 'executable-find "executable"))
59 (autoload 'query-dig "dig"))
61 ;; autoload spam-report
63 (autoload 'spam-report-gmane "spam-report"))
65 ;; autoload gnus-registry
67 (autoload 'gnus-registry-group-count "gnus-registry")
68 (autoload 'gnus-registry-add-group "gnus-registry")
69 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
70 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
74 (autoload 'query-dns "dns"))
82 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
83 "Directory for spam whitelists and blacklists."
87 (defcustom spam-move-spam-nonspam-groups-only t
88 "Whether spam should be moved in non-spam groups only.
89 When t, only ham and unclassified groups will have their spam moved
90 to the spam-process-destination. When nil, spam will also be moved from
95 (defcustom spam-process-ham-in-nonham-groups nil
96 "Whether ham should be processed in non-ham groups."
100 (defcustom spam-log-to-registry nil
101 "Whether spam/ham processing should be logged in the registry."
105 (defcustom spam-split-symbolic-return nil
106 "Whether `spam-split' should work with symbols or group names."
110 (defcustom spam-split-symbolic-return-positive nil
111 "Whether `spam-split' should ALWAYS work with symbols or group names.
112 Do not set this if you use `spam-split' in a fancy split
117 (defcustom spam-process-ham-in-spam-groups nil
118 "Whether ham should be processed in spam groups."
122 (defcustom spam-mark-only-unseen-as-spam t
123 "Whether only unseen articles should be marked as spam in spam groups.
124 When nil, all unread articles in a spam group are marked as
125 spam. Set this if you want to leave an article unread in a spam group
126 without losing it to the automatic spam-marking process."
130 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
131 "Whether ham should be marked unread before it's moved.
132 The article is moved out of a spam group according to ham-process-destination.
133 This variable is an official entry in the international Longest Variable Name
138 (defcustom spam-disable-spam-split-during-ham-respool nil
139 "Whether `spam-split' should be ignored while resplitting ham in a process
140 destination. This is useful to prevent ham from ending up in the same spam
141 group after the resplit. Don't set this to t if you have spam-split as the
142 last rule in your split configuration."
146 (defcustom spam-autodetect-recheck-messages nil
147 "Should spam.el recheck all meessages when autodetecting?
148 Normally this is nil, so only unseen messages will be checked."
152 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
153 "The location of the whitelist.
154 The file format is one regular expression per line.
155 The regular expression is matched against the address."
159 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
160 "The location of the blacklist.
161 The file format is one regular expression per line.
162 The regular expression is matched against the address."
166 (defcustom spam-use-dig t
167 "Whether `query-dig' should be used instead of `query-dns'."
171 (defcustom spam-use-blacklist nil
172 "Whether the blacklist should be used by `spam-split'."
176 (defcustom spam-blacklist-ignored-regexes nil
177 "Regular expressions that the blacklist should ignore."
178 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
181 (defcustom spam-use-whitelist nil
182 "Whether the whitelist should be used by `spam-split'."
186 (defcustom spam-use-whitelist-exclusive nil
187 "Whether whitelist-exclusive should be used by `spam-split'.
188 Exclusive whitelisting means that all messages from senders not in the whitelist
189 are considered spam."
193 (defcustom spam-use-blackholes nil
194 "Whether blackholes should be used by `spam-split'."
198 (defcustom spam-use-hashcash nil
199 "Whether hashcash payments should be detected by `spam-split'."
203 (defcustom spam-use-regex-headers nil
204 "Whether a header regular expression match should be used by `spam-split'.
205 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
209 (defcustom spam-use-regex-body nil
210 "Whether a body regular expression match should be used by `spam-split'.
211 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
215 (defcustom spam-use-bogofilter-headers nil
216 "Whether bogofilter headers should be used by `spam-split'.
217 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
221 (defcustom spam-use-bogofilter nil
222 "Whether bogofilter should be invoked by `spam-split'.
223 Enable this if you want Gnus to invoke Bogofilter on new messages."
227 (defcustom spam-use-BBDB nil
228 "Whether BBDB should be used by `spam-split'."
232 (defcustom spam-use-BBDB-exclusive nil
233 "Whether BBDB-exclusive should be used by `spam-split'.
234 Exclusive BBDB means that all messages from senders not in the BBDB are
239 (defcustom spam-use-ifile nil
240 "Whether ifile should be used by `spam-split'."
244 (defcustom spam-use-stat nil
245 "Whether `spam-stat' should be used by `spam-split'."
249 (defcustom spam-use-spamoracle nil
250 "Whether spamoracle should be used by `spam-split'."
254 (defcustom spam-install-hooks (or
258 spam-use-whitelist-exclusive
261 spam-use-regex-headers
263 spam-use-bogofilter-headers
266 spam-use-BBDB-exclusive
270 "Whether the spam hooks should be installed.
271 Default to t if one of the spam-use-* variables is set."
275 (defcustom spam-split-group "spam"
276 "Group name where incoming spam should be put by `spam-split'."
280 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
281 ;;; not regular expressions
282 (defcustom spam-junk-mailgroups (cons
284 '("mail.junk" "poste.pourriel"))
285 "Mailgroups with spam contents.
286 All unmarked article in such group receive the spam mark on group entry."
287 :type '(repeat (string :tag "Group"))
290 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
291 "dev.null.dk" "relays.visi.com")
292 "List of blackhole servers."
293 :type '(repeat (string :tag "Server"))
296 (defcustom spam-blackhole-good-server-regex nil
297 "String matching IP addresses that should not be checked in the blackholes."
298 :type '(radio (const nil) regexp)
302 '((((class color) (type tty) (background dark))
303 (:foreground "gray80" :background "gray50"))
304 (((class color) (type tty) (background light))
305 (:foreground "gray50" :background "gray80"))
306 (((class color) (background dark))
307 (:foreground "ivory2"))
308 (((class color) (background light))
309 (:foreground "ivory4"))
310 (t :inverse-video t))
311 "Face for spam-marked articles.")
313 (defcustom spam-face 'spam-face
314 "Face for spam-marked articles."
318 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
319 "Regular expression for positive header spam matches."
320 :type '(repeat (regexp :tag "Regular expression to match spam header"))
323 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
324 "Regular expression for positive header ham matches."
325 :type '(repeat (regexp :tag "Regular expression to match ham header"))
328 (defcustom spam-regex-body-spam '()
329 "Regular expression for positive body spam matches."
330 :type '(repeat (regexp :tag "Regular expression to match spam body"))
333 (defcustom spam-regex-body-ham '()
334 "Regular expression for positive body ham matches."
335 :type '(repeat (regexp :tag "Regular expression to match ham body"))
338 (defgroup spam-ifile nil
339 "Spam ifile configuration."
342 (defcustom spam-ifile-path (executable-find "ifile")
343 "File path of the ifile executable program."
344 :type '(choice (file :tag "Location of ifile")
345 (const :tag "ifile is not installed"))
348 (defcustom spam-ifile-database-path nil
349 "File path of the ifile database."
350 :type '(choice (file :tag "Location of the ifile database")
351 (const :tag "Use the default"))
354 (defcustom spam-ifile-spam-category "spam"
355 "Name of the spam ifile category."
359 (defcustom spam-ifile-ham-category nil
360 "Name of the ham ifile category.
361 If nil, the current group name will be used."
362 :type '(choice (string :tag "Use a fixed category")
363 (const :tag "Use the current group name"))
366 (defcustom spam-ifile-all-categories nil
367 "Whether the ifile check will return all categories, or just spam.
368 Set this to t if you want to use the `spam-split' invocation of ifile as
369 your main source of newsgroup names."
373 (defgroup spam-bogofilter nil
374 "Spam bogofilter configuration."
377 (defcustom spam-bogofilter-path (executable-find "bogofilter")
378 "File path of the Bogofilter executable program."
379 :type '(choice (file :tag "Location of bogofilter")
380 (const :tag "Bogofilter is not installed"))
381 :group 'spam-bogofilter)
383 (defcustom spam-bogofilter-header "X-Bogosity"
384 "The header that Bogofilter inserts in messages."
386 :group 'spam-bogofilter)
388 (defcustom spam-bogofilter-spam-switch "-s"
389 "The switch that Bogofilter uses to register spam messages."
391 :group 'spam-bogofilter)
393 (defcustom spam-bogofilter-ham-switch "-n"
394 "The switch that Bogofilter uses to register ham messages."
396 :group 'spam-bogofilter)
398 (defcustom spam-bogofilter-spam-strong-switch "-S"
399 "The switch that Bogofilter uses to unregister ham messages."
401 :group 'spam-bogofilter)
403 (defcustom spam-bogofilter-ham-strong-switch "-N"
404 "The switch that Bogofilter uses to unregister spam messages."
406 :group 'spam-bogofilter)
408 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
409 "The regex on `spam-bogofilter-header' for positive spam identification."
411 :group 'spam-bogofilter)
413 (defcustom spam-bogofilter-database-directory nil
414 "Directory path of the Bogofilter databases."
415 :type '(choice (directory
416 :tag "Location of the Bogofilter database directory")
417 (const :tag "Use the default"))
418 :group 'spam-bogofilter)
420 (defgroup spam-spamoracle nil
421 "Spam spamoracle configuration."
424 (defcustom spam-spamoracle-database nil
425 "Location of spamoracle database file. When nil, use the default
426 spamoracle database."
427 :type '(choice (directory :tag "Location of spamoracle database file.")
428 (const :tag "Use the default"))
429 :group 'spam-spamoracle)
431 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
432 "Location of the spamoracle binary."
433 :type '(choice (directory :tag "Location of the spamoracle binary")
434 (const :tag "Use the default"))
435 :group 'spam-spamoracle)
437 ;;; Key bindings for spam control.
439 (gnus-define-keys gnus-summary-mode-map
440 "St" spam-bogofilter-score
441 "Sx" gnus-summary-mark-as-spam
442 "Mst" spam-bogofilter-score
443 "Msx" gnus-summary-mark-as-spam
444 "\M-d" gnus-summary-mark-as-spam)
446 (defvar spam-old-ham-articles nil
447 "List of old ham articles, generated when a group is entered.")
449 (defvar spam-old-spam-articles nil
450 "List of old spam articles, generated when a group is entered.")
452 (defvar spam-split-disabled nil
453 "If non-nil, `spam-split' is disabled, and always returns nil.")
455 (defvar spam-split-last-successful-check nil
456 "`spam-split' will set this to nil or a spam-use-XYZ check if it
459 ;; convenience functions
460 (defun spam-xor (a b)
461 "Logical exclusive `or'."
462 (and (or a b) (not (and a b))))
464 (defun spam-group-ham-mark-p (group mark &optional spam)
465 (when (stringp group)
466 (let* ((marks (spam-group-ham-marks group spam))
467 (marks (if (symbolp mark)
469 (mapcar 'symbol-value marks))))
472 (defun spam-group-spam-mark-p (group mark)
473 (spam-group-ham-mark-p group mark t))
475 (defun spam-group-ham-marks (group &optional spam)
476 (when (stringp group)
477 (let* ((marks (if spam
478 (gnus-parameter-spam-marks group)
479 (gnus-parameter-ham-marks group)))
481 (marks (if (listp (car marks)) (car marks) marks)))
484 (defun spam-group-spam-marks (group)
485 (spam-group-ham-marks group t))
487 (defun spam-group-spam-contents-p (group)
489 (or (member group spam-junk-mailgroups)
490 (memq 'gnus-group-spam-classification-spam
491 (gnus-parameter-spam-contents group)))
494 (defun spam-group-ham-contents-p (group)
496 (memq 'gnus-group-spam-classification-ham
497 (gnus-parameter-spam-contents group))
500 (defvar spam-list-of-processors
501 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
502 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
503 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
504 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
505 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
506 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
507 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
508 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
509 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
510 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
511 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
512 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
513 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
514 "The spam-list-of-processors list contains pairs associating a
515 ham/spam exit processor variable with a classification and a
516 spam-use-* variable.")
518 (defun spam-group-processor-p (group processor)
519 (if (and (stringp group)
521 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
522 (spam-group-processor-multiple-p
524 (cdr-safe (assoc processor spam-list-of-processors))))
527 (defun spam-group-processor-multiple-p (group processor-info)
528 (let* ((classification (nth 0 processor-info))
529 (check (nth 1 processor-info))
530 (parameters (nth 0 (gnus-parameter-spam-process group)))
532 (dolist (parameter parameters)
533 (when (and (null found)
535 (eq classification (nth 0 parameter))
536 (eq check (nth 1 parameter)))
540 (defun spam-group-spam-processor-report-gmane-p (group)
541 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
543 (defun spam-group-spam-processor-bogofilter-p (group)
544 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
546 (defun spam-group-spam-processor-blacklist-p (group)
547 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
549 (defun spam-group-spam-processor-ifile-p (group)
550 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
552 (defun spam-group-ham-processor-ifile-p (group)
553 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
555 (defun spam-group-spam-processor-spamoracle-p (group)
556 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
558 (defun spam-group-ham-processor-bogofilter-p (group)
559 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
561 (defun spam-group-spam-processor-stat-p (group)
562 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
564 (defun spam-group-ham-processor-stat-p (group)
565 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
567 (defun spam-group-ham-processor-whitelist-p (group)
568 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
570 (defun spam-group-ham-processor-BBDB-p (group)
571 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
573 (defun spam-group-ham-processor-copy-p (group)
574 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
576 (defun spam-group-ham-processor-spamoracle-p (group)
577 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
579 ;;; Summary entry and exit processing.
581 (defun spam-summary-prepare ()
582 (setq spam-old-ham-articles
583 (spam-list-articles gnus-newsgroup-articles 'ham))
584 (setq spam-old-spam-articles
585 (spam-list-articles gnus-newsgroup-articles 'spam))
586 (spam-mark-junk-as-spam-routine))
588 ;; The spam processors are invoked for any group, spam or ham or neither
589 (defun spam-summary-prepare-exit ()
590 (unless gnus-group-is-exiting-without-update-p
591 (gnus-message 6 "Exiting summary buffer and applying spam rules")
593 ;; first of all, unregister any articles that are no longer ham or spam
594 ;; we have to iterate over the processors, or else we'll be too slow
595 (dolist (classification '(spam ham))
596 (let* ((old-articles (if (eq classification 'spam)
597 spam-old-spam-articles
598 spam-old-ham-articles))
599 (new-articles (spam-list-articles
600 gnus-newsgroup-articles
602 (changed-articles (gnus-set-difference old-articles new-articles)))
603 ;; now that we have the changed articles, we go through the processors
604 (dolist (processor-param spam-list-of-processors)
605 (let ((processor (nth 0 processor-param))
606 (processor-classification (nth 1 processor-param))
607 (check (nth 2 processor-param))
609 (dolist (article changed-articles)
610 (let ((id (spam-fetch-field-message-id-fast article)))
611 (when (spam-log-unregistration-needed-p
612 id 'process classification check)
613 (push article unregister-list))))
614 ;; call spam-register-routine with specific articles to unregister,
615 ;; when there are articles to unregister and the check is enabled
616 (when (and unregister-list (symbol-value check))
617 (spam-register-routine classification check t unregister-list))))))
619 ;; find all the spam processors applicable to this group
620 (dolist (processor-param spam-list-of-processors)
621 (let ((processor (nth 0 processor-param))
622 (classification (nth 1 processor-param))
623 (check (nth 2 processor-param)))
624 (when (and (eq 'spam classification)
625 (spam-group-processor-p gnus-newsgroup-name processor))
626 (spam-register-routine classification check))))
628 (if spam-move-spam-nonspam-groups-only
629 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
630 (spam-mark-spam-as-expired-and-move-routine
631 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
632 (gnus-message 5 "Marking spam as expired and moving it to %s"
634 (spam-mark-spam-as-expired-and-move-routine
635 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
637 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
638 ;; expire spam, in case the above did not expire them
639 (gnus-message 5 "Marking spam as expired without moving it")
640 (spam-mark-spam-as-expired-and-move-routine nil)
642 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
643 (and (spam-group-spam-contents-p gnus-newsgroup-name)
644 spam-process-ham-in-spam-groups)
645 spam-process-ham-in-nonham-groups)
646 ;; find all the ham processors applicable to this group
647 (dolist (processor-param spam-list-of-processors)
648 (let ((processor (nth 0 processor-param))
649 (classification (nth 1 processor-param))
650 (check (nth 2 processor-param)))
651 (when (and (eq 'ham classification)
652 (spam-group-processor-p gnus-newsgroup-name processor))
653 (spam-register-routine classification check)))))
655 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
656 (gnus-message 5 "Copying ham")
657 (spam-ham-copy-routine
658 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
660 ;; now move all ham articles out of spam groups
661 (when (spam-group-spam-contents-p gnus-newsgroup-name)
662 (gnus-message 5 "Moving ham messages from spam group")
663 (spam-ham-move-routine
664 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
666 (setq spam-old-ham-articles nil)
667 (setq spam-old-spam-articles nil))
669 (defun spam-mark-junk-as-spam-routine ()
670 ;; check the global list of group names spam-junk-mailgroups and the
672 (when (spam-group-spam-contents-p gnus-newsgroup-name)
673 (gnus-message 5 "Marking %s articles as spam"
674 (if spam-mark-only-unseen-as-spam
677 (let ((articles (if spam-mark-only-unseen-as-spam
678 gnus-newsgroup-unseen
679 gnus-newsgroup-unreads)))
680 (dolist (article articles)
681 (gnus-summary-mark-article article gnus-spam-mark)))))
683 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
684 (if (and (car-safe groups) (listp (car-safe groups)))
685 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
686 (gnus-summary-kill-process-mark)
687 (let ((articles gnus-newsgroup-articles)
688 (backend-supports-deletions
689 (gnus-check-backend-function
690 'request-move-article gnus-newsgroup-name))
691 article tomove deletep)
692 (dolist (article articles)
693 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
694 (gnus-summary-mark-article article gnus-expirable-mark)
695 (push article tomove)))
697 ;; now do the actual copies
698 (dolist (group groups)
701 (dolist (article tomove)
702 (gnus-summary-set-process-mark article))
704 (if (or (not backend-supports-deletions)
705 (> (length groups) 1))
707 (gnus-summary-copy-article nil group)
709 (gnus-summary-move-article nil group)))))
711 ;; now delete the articles, if there was a copy done, and the
713 (when (and deletep backend-supports-deletions)
714 (dolist (article tomove)
715 (gnus-summary-set-process-mark article))
717 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
718 (gnus-summary-delete-article nil))))
720 (gnus-summary-yank-process-mark))))
722 (defun spam-ham-copy-or-move-routine (copy groups)
723 (gnus-summary-kill-process-mark)
724 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
725 (backend-supports-deletions
726 (gnus-check-backend-function
727 'request-move-article gnus-newsgroup-name))
728 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
729 article mark todo deletep respool)
731 (when (member 'respool groups)
732 (setq respool t) ; boolean for later
733 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
735 ;; now do the actual move
736 (dolist (group groups)
737 (when (and todo (stringp group))
738 (dolist (article todo)
739 (when spam-mark-ham-unread-before-move-from-spam-group
740 (gnus-summary-mark-article article gnus-unread-mark))
741 (gnus-summary-set-process-mark article))
743 (if respool ; respooling is with a "fake" group
744 (let ((spam-split-disabled
745 (or spam-split-disabled
746 spam-disable-spam-split-during-ham-respool)))
747 (gnus-summary-respool-article nil respool-method))
748 (if (or (not backend-supports-deletions) ; else, we are not respooling
749 (> (length groups) 1))
750 (progn ; if copying, copy and set deletep
751 (gnus-summary-copy-article nil group)
753 (gnus-summary-move-article nil group))))) ; else move articles
755 ;; now delete the articles, unless a) copy is t, and there was a copy done
756 ;; b) a move was done to a single group
757 ;; c) backend-supports-deletions is nil
759 (when (and deletep backend-supports-deletions)
760 (dolist (article todo)
761 (gnus-summary-set-process-mark article))
763 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
764 (gnus-summary-delete-article nil))))))
766 (gnus-summary-yank-process-mark))
768 (defun spam-ham-copy-routine (&rest groups)
769 (if (and (car-safe groups) (listp (car-safe groups)))
770 (apply 'spam-ham-copy-routine (car groups))
771 (spam-ham-copy-or-move-routine t groups)))
773 (defun spam-ham-move-routine (&rest groups)
774 (if (and (car-safe groups) (listp (car-safe groups)))
775 (apply 'spam-ham-move-routine (car groups))
776 (spam-ham-copy-or-move-routine nil groups)))
779 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
781 'line-end-position)))
783 (defun spam-get-article-as-string (article)
784 (let ((article-buffer (spam-get-article-as-buffer article))
787 (save-window-excursion
788 (set-buffer article-buffer)
789 (setq article-string (buffer-string))))
792 (defun spam-get-article-as-buffer (article)
793 (let ((article-buffer))
794 (when (numberp article)
795 (save-window-excursion
796 (gnus-summary-goto-subject article)
797 (gnus-summary-show-article t)
798 (setq article-buffer (get-buffer gnus-article-buffer))))
802 ;; (defun spam-get-article-as-filename (article)
803 ;; (let ((article-filename))
804 ;; (when (numberp article)
805 ;; (nnml-possibly-change-directory
806 ;; (gnus-group-real-name gnus-newsgroup-name))
807 ;; (setq article-filename (expand-file-name
808 ;; (int-to-string article) nnml-current-directory)))
809 ;; (if (file-exists-p article-filename)
813 (defun spam-fetch-field-from-fast (article)
814 "Fetch the `from' field quickly, using the internal gnus-data-list function"
815 (if (and (numberp article)
816 (assoc article (gnus-data-list nil)))
818 (gnus-data-header (assoc article (gnus-data-list nil))))
821 (defun spam-fetch-field-subject-fast (article)
822 "Fetch the `subject' field quickly, using the internal
823 gnus-data-list function"
824 (if (and (numberp article)
825 (assoc article (gnus-data-list nil)))
827 (gnus-data-header (assoc article (gnus-data-list nil))))
830 (defun spam-fetch-field-message-id-fast (article)
831 "Fetch the `Message-ID' field quickly, using the internal
832 gnus-data-list function"
833 (if (and (numberp article)
834 (assoc article (gnus-data-list nil)))
835 (mail-header-message-id
836 (gnus-data-header (assoc article (gnus-data-list nil))))
840 ;;;; Spam determination.
842 (defvar spam-list-of-checks
843 '((spam-use-blacklist . spam-check-blacklist)
844 (spam-use-regex-headers . spam-check-regex-headers)
845 (spam-use-regex-body . spam-check-regex-body)
846 (spam-use-whitelist . spam-check-whitelist)
847 (spam-use-BBDB . spam-check-BBDB)
848 (spam-use-ifile . spam-check-ifile)
849 (spam-use-spamoracle . spam-check-spamoracle)
850 (spam-use-stat . spam-check-stat)
851 (spam-use-blackholes . spam-check-blackholes)
852 (spam-use-hashcash . spam-check-hashcash)
853 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
854 (spam-use-bogofilter . spam-check-bogofilter))
855 "The spam-list-of-checks list contains pairs associating a
856 parameter variable with a spam checking function. If the
857 parameter variable is true, then the checking function is called,
858 and its value decides what happens. Each individual check may
859 return nil, t, or a mailgroup name. The value nil means that the
860 check does not yield a decision, and so, that further checks are
861 needed. The value t means that the message is definitely not
862 spam, and that further spam checks should be inhibited.
863 Otherwise, a mailgroup name or the symbol 'spam (depending on
864 spam-split-symbolic-return) is returned where the mail should go,
865 and further checks are also inhibited. The usual mailgroup name
866 is the value of `spam-split-group', meaning that the message is
869 (defvar spam-list-of-statistical-checks
875 "The spam-list-of-statistical-checks list contains all the mail
876 splitters that need to have the full message body available.")
878 ;;;TODO: modify to invoke self with each check if invoked without specifics
879 (defun spam-split (&rest specific-checks)
880 "Split this message into the `spam' group if it is spam.
881 This function can be used as an entry in the variable `nnmail-split-fancy',
882 for example like this: (: spam-split). It can take checks as
883 parameters. A string as a parameter will set the
884 spam-split-group to that string.
886 See the Info node `(gnus)Fancy Mail Splitting' for more details."
888 (setq spam-split-last-successful-check nil)
889 (unless spam-split-disabled
890 (let ((spam-split-group-choice spam-split-group))
891 (dolist (check specific-checks)
892 (when (stringp check)
893 (setq spam-split-group-choice check)
894 (setq specific-checks (delq check specific-checks))))
896 (let ((spam-split-group spam-split-group-choice))
899 (dolist (check spam-list-of-statistical-checks)
900 (when (and (symbolp check) (symbol-value check))
902 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
905 ;; (progn (widen) (debug (buffer-string)))
906 (let ((list-of-checks spam-list-of-checks)
908 (while (and list-of-checks (not decision))
909 (let ((pair (pop list-of-checks)))
910 (when (and (symbol-value (car pair))
911 (or (null specific-checks)
912 (memq (car pair) specific-checks)))
913 (gnus-message 5 "spam-split: calling the %s function"
914 (symbol-name (cdr pair)))
915 (setq decision (funcall (cdr pair)))
916 ;; if we got a decision at all, save the current check
918 (setq spam-split-last-successful-check (car pair)))
920 (when (eq decision 'spam)
921 (if spam-split-symbolic-return
922 (setq decision spam-split-group)
925 (format "spam-split got %s but %s is nil"
926 (symbol-name decision)
927 (symbol-name spam-split-symbolic-return))))))))
929 (if spam-split-symbolic-return-positive 'ham nil)
932 (defun spam-find-spam ()
933 "This function will detect spam in the current newsgroup using spam-split."
936 (let* ((group gnus-newsgroup-name)
937 (autodetect (gnus-parameter-spam-autodetect group))
938 (methods (gnus-parameter-spam-autodetect-methods group))
939 (first-method (nth 0 methods)))
940 (when (and autodetect
941 (not (equal first-method 'none)))
944 (let ((id (spam-fetch-field-message-id-fast article))
945 (subject (spam-fetch-field-subject-fast article))
946 (sender (spam-fetch-field-from-fast article)))
947 (unless (and spam-log-to-registry
948 (spam-log-registered-p id 'incoming))
949 (let* ((spam-split-symbolic-return t)
950 (spam-split-symbolic-return-positive t)
953 (gnus-request-article-this-buffer
956 (if (or (null first-method)
957 (equal first-method 'default))
959 (apply 'spam-split methods)))))
960 (if (equal split-return 'spam)
961 (gnus-summary-mark-article article gnus-spam-mark))
963 (when (and split-return spam-log-to-registry)
964 (when (zerop (gnus-registry-group-count id))
965 (gnus-registry-add-group
966 id group subject sender))
968 (spam-log-processing-to-registry
972 spam-split-last-successful-check
974 (if spam-autodetect-recheck-messages
975 gnus-newsgroup-articles
976 gnus-newsgroup-unseen)))))
978 (defvar spam-registration-functions
979 ;; first the ham register, second the spam register function
980 ;; third the ham unregister, fourth the spam unregister function
981 '((spam-use-blacklist nil
982 spam-blacklist-register-routine
984 spam-blacklist-unregister-routine)
985 (spam-use-whitelist spam-whitelist-register-routine
987 spam-whitelist-unregister-routine
989 (spam-use-BBDB spam-BBDB-register-routine
991 spam-BBDB-unregister-routine
993 (spam-use-ifile spam-ifile-register-ham-routine
994 spam-ifile-register-spam-routine
995 spam-ifile-unregister-ham-routine
996 spam-ifile-unregister-spam-routine)
997 (spam-use-spamoracle spam-spamoracle-learn-ham
998 spam-spamoracle-learn-spam
999 spam-spamoracle-unlearn-ham
1000 spam-spamoracle-unlearn-spam)
1001 (spam-use-stat spam-stat-register-ham-routine
1002 spam-stat-register-spam-routine
1003 spam-stat-unregister-ham-routine
1004 spam-stat-unregister-spam-routine)
1005 ;; note that spam-use-gmane is not a legitimate check
1007 spam-report-gmane-register-routine
1008 ;; does Gmane support unregistration?
1011 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1012 spam-bogofilter-register-spam-routine
1013 spam-bogofilter-unregister-ham-routine
1014 spam-bogofilter-unregister-spam-routine))
1015 "The spam-registration-functions list contains pairs
1016 associating a parameter variable with the ham and spam
1017 registration functions, and the ham and spam unregistration
1020 (defun spam-classification-valid-p (classification)
1021 (or (eq classification 'spam)
1022 (eq classification 'ham)))
1024 (defun spam-process-type-valid-p (process-type)
1025 (or (eq process-type 'incoming)
1026 (eq process-type 'process)))
1028 (defun spam-registration-check-valid-p (check)
1029 (assoc check spam-registration-functions))
1031 (defun spam-unregistration-check-valid-p (check)
1032 (assoc check spam-registration-functions))
1034 (defun spam-registration-function (classification check)
1035 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1036 (if (eq classification 'spam)
1040 (defun spam-unregistration-function (classification check)
1041 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1042 (if (eq classification 'spam)
1046 (defun spam-list-articles (articles classification)
1047 (let ((mark-check (if (eq classification 'spam)
1048 'spam-group-spam-mark-p
1049 'spam-group-ham-mark-p))
1050 list mark-cache-yes mark-cache-no)
1051 (dolist (article articles)
1052 (let ((mark (gnus-summary-article-mark article)))
1053 (unless (memq mark mark-cache-no)
1054 (if (memq mark mark-cache-yes)
1056 ;; else, we have to actually check the mark
1057 (if (funcall mark-check
1062 (push mark mark-cache-yes))
1063 (push mark mark-cache-no))))))
1066 (defun spam-register-routine (classification
1068 &optional unregister
1070 (when (and (spam-classification-valid-p classification)
1071 (spam-registration-check-valid-p check))
1072 (let* ((register-function
1073 (spam-registration-function classification check))
1074 (unregister-function
1075 (spam-unregistration-function classification check))
1076 (run-function (if unregister
1079 (log-function (if unregister
1080 'spam-log-undo-registration
1081 'spam-log-processing-to-registry))
1085 ;; make list of articles, using specific-articles if given
1086 (setq articles (or specific-articles
1088 gnus-newsgroup-articles
1091 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1092 (if unregister "Unregistering" "Registering")
1094 (if specific-articles "specific" "")
1095 (symbol-name classification)
1096 (symbol-name check))
1097 (funcall run-function articles)
1098 ;; now log all the registrations (or undo them, depending on unregister)
1099 (dolist (article articles)
1100 (funcall log-function
1101 (spam-fetch-field-message-id-fast article)
1105 gnus-newsgroup-name))))))
1107 ;;; log a ham- or spam-processor invocation to the registry
1108 (defun spam-log-processing-to-registry (id type classification check group)
1109 (when spam-log-to-registry
1110 (if (and (stringp id)
1112 (spam-process-type-valid-p type)
1113 (spam-classification-valid-p classification)
1114 (spam-registration-check-valid-p check))
1115 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1116 (cell (list classification check group)))
1117 (push cell cell-list)
1118 (gnus-registry-store-extra-entry
1123 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1124 "spam-log-processing-to-registry")))))
1126 ;;; check if a ham- or spam-processor registration has been done
1127 (defun spam-log-registered-p (id type)
1128 (when spam-log-to-registry
1129 (if (and (stringp id)
1130 (spam-process-type-valid-p type))
1131 (cdr-safe (gnus-registry-fetch-extra id type))
1133 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1134 "spam-log-registered-p"))
1137 ;;; check if a ham- or spam-processor registration needs to be undone
1138 (defun spam-log-unregistration-needed-p (id type classification check)
1139 (when spam-log-to-registry
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)))
1146 (dolist (cell cell-list)
1148 (when (and (eq classification (nth 0 cell))
1149 (eq check (nth 1 cell)))
1153 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1154 "spam-log-unregistration-needed-p"))
1158 ;;; undo a ham- or spam-processor registration (the group is not used)
1159 (defun spam-log-undo-registration (id type classification check &optional group)
1160 (when (and spam-log-to-registry
1161 (spam-log-unregistration-needed-p id type classification check))
1162 (if (and (stringp id)
1163 (spam-process-type-valid-p type)
1164 (spam-classification-valid-p classification)
1165 (spam-registration-check-valid-p check))
1166 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1167 new-cell-list found)
1168 (dolist (cell cell-list)
1169 (unless (and (eq classification (nth 0 cell))
1170 (eq check (nth 1 cell)))
1171 (push cell new-cell-list)))
1172 (gnus-registry-store-extra-entry
1177 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1178 "spam-log-undo-registration"))
1181 ;;; set up IMAP widening if it's necessary
1182 (defun spam-setup-widening ()
1183 (dolist (check spam-list-of-statistical-checks)
1184 (when (symbol-value check)
1185 (setq nnimap-split-download-body-default t))))
1190 (defun spam-check-regex-body ()
1191 (let ((spam-regex-headers-ham spam-regex-body-ham)
1192 (spam-regex-headers-spam spam-regex-body-spam))
1193 (spam-check-regex-headers t)))
1198 (defun spam-check-regex-headers (&optional body)
1199 (let ((type (if body "body" "header"))
1200 (spam-split-group (if spam-split-symbolic-return
1204 (dolist (h-regex spam-regex-headers-ham)
1206 (goto-char (point-min))
1207 (when (re-search-forward h-regex nil t)
1208 (message "Ham regex %s search positive." type)
1210 (dolist (s-regex spam-regex-headers-spam)
1212 (goto-char (point-min))
1213 (when (re-search-forward s-regex nil t)
1214 (message "Spam regex %s search positive." type)
1216 (setq ret spam-split-group))))
1222 (defun spam-reverse-ip-string (ip)
1224 (mapconcat 'identity
1225 (nreverse (split-string ip "\\."))
1228 (defun spam-check-blackholes ()
1229 "Check the Received headers for blackholed relays."
1230 (let ((headers (nnmail-fetch-field "received"))
1231 (spam-split-group (if spam-split-symbolic-return
1238 (goto-char (point-min))
1239 (gnus-message 5 "Checking headers for relay addresses")
1240 (while (re-search-forward
1241 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1242 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1243 (push (spam-reverse-ip-string (match-string 1))
1245 (dolist (server spam-blackhole-servers)
1247 (unless (and spam-blackhole-good-server-regex
1248 ;; match the good-server-regex against the reversed (again) IP string
1250 spam-blackhole-good-server-regex
1251 (spam-reverse-ip-string ip)))
1253 (let ((query-string (concat ip "." server)))
1255 (let ((query-result (query-dig query-string)))
1257 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1259 (push (list ip server query-result)
1261 ;; else, if not using dig.el
1262 (when (query-dns query-string)
1263 (gnus-message 5 "positive blackhole check")
1264 (push (list ip server (query-dns query-string 'TXT))
1272 (autoload 'mail-check-payment "hashcash"))
1278 (defun spam-check-hashcash ()
1279 "Check the headers for hashcash payments."
1280 (mail-check-payment))) ;mail-check-payment returns a boolean
1286 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1287 ;;; <sacha@giotto.sj.ru>
1289 ;; all this is done inside a condition-case to trap errors
1292 (autoload 'bbdb-buffer "bbdb")
1293 (autoload 'bbdb-create-internal "bbdb")
1294 (autoload 'bbdb-search-simple "bbdb"))
1297 (when (condition-case nil
1300 (require 'bbdb-com))
1302 (defalias 'spam-BBDB-register-routine 'ignore)
1303 (defalias 'spam-enter-ham-BBDB 'ignore)
1306 (defun spam-enter-ham-BBDB (addresses &optional remove)
1307 "Enter an address into the BBDB; implies ham (non-spam) sender"
1308 (dolist (from addresses)
1309 (when (stringp from)
1310 (let* ((parsed-address (gnus-extract-address-components from))
1311 (name (or (nth 0 parsed-address) "Ham Sender"))
1312 (remove-function (if remove
1313 'bbdb-delete-record-internal
1315 (net-address (nth 1 parsed-address))
1316 (record (and net-address
1317 (bbdb-search-simple nil net-address))))
1319 (gnus-message 5 "%s address %s %s BBDB"
1320 (if remove "Deleting" "Adding")
1322 (if remove "from" "to"))
1324 (funcall remove-function record)
1325 (bbdb-create-internal name nil net-address nil nil
1326 "ham sender added by spam.el")))))))
1328 (defun spam-BBDB-register-routine (articles &optional unregister)
1330 (dolist (article articles)
1331 (when (stringp (spam-fetch-field-from-fast article))
1332 (push (spam-fetch-field-from-fast article) addresses)))
1333 ;; now do the register/unregister action
1334 (spam-enter-ham-BBDB addresses unregister)))
1336 (defun spam-BBDB-unregister-routine (articles)
1337 (spam-BBDB-register-routine articles t))
1339 (defun spam-check-BBDB ()
1340 "Mail from people in the BBDB is classified as ham or non-spam"
1341 (let ((who (nnmail-fetch-field "from"))
1342 (spam-split-group (if spam-split-symbolic-return
1346 (setq who (nth 1 (gnus-extract-address-components who)))
1347 (if (bbdb-search-simple nil who)
1349 (if spam-use-BBDB-exclusive
1356 ;;; check the ifile backend; return nil if the mail was NOT classified
1359 (defun spam-get-ifile-database-parameter ()
1360 "Get the command-line parameter for ifile's database from
1361 spam-ifile-database-path."
1362 (if spam-ifile-database-path
1363 (format "--db-file=%s" spam-ifile-database-path)
1366 (defun spam-check-ifile ()
1367 "Check the ifile backend for the classification of this message."
1368 (let ((article-buffer-name (buffer-name))
1369 (spam-split-group (if spam-split-symbolic-return
1374 (let ((temp-buffer-name (buffer-name))
1375 (db-param (spam-get-ifile-database-parameter)))
1377 (set-buffer article-buffer-name)
1378 (apply 'call-process-region
1379 (point-min) (point-max) spam-ifile-path
1380 nil temp-buffer-name nil "-c"
1381 (if db-param `(,db-param "-q") `("-q"))))
1382 ;; check the return now (we're back in the temp buffer)
1383 (goto-char (point-min))
1385 (setq category (buffer-substring (point) (spam-point-at-eol))))
1386 (when (not (zerop (length category))) ; we need a category here
1387 (if spam-ifile-all-categories
1388 (setq return category)
1389 ;; else, if spam-ifile-all-categories is not set...
1390 (when (string-equal spam-ifile-spam-category category)
1391 (setq return spam-split-group)))))) ; note return is nil otherwise
1394 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1395 "Register an article, given as a string, with a category.
1396 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1397 (let ((category (or category gnus-newsgroup-name))
1398 (add-or-delete-option (if unregister "-d" "-i"))
1399 (db (spam-get-ifile-database-parameter))
1402 (dolist (article articles)
1403 (let ((article-string (spam-get-article-as-string article)))
1404 (when (stringp article-string)
1405 (insert article-string))))
1406 (apply 'call-process-region
1407 (point-min) (point-max) spam-ifile-path
1409 add-or-delete-option category
1410 (if db `(,db "-h") `("-h"))))))
1412 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1413 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1415 (defun spam-ifile-unregister-spam-routine (articles)
1416 (spam-ifile-register-spam-routine articles t))
1418 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1419 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1421 (defun spam-ifile-unregister-ham-routine (articles)
1422 (spam-ifile-register-ham-routine articles t))
1428 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
1429 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
1430 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
1431 (autoload 'spam-stat-buffer-is-spam "spam-stat")
1432 (autoload 'spam-stat-load "spam-stat")
1433 (autoload 'spam-stat-save "spam-stat")
1434 (autoload 'spam-stat-split-fancy "spam-stat"))
1437 (when (condition-case nil
1438 (let ((spam-stat-install-hooks nil))
1439 (require 'spam-stat))
1441 (defalias 'spam-stat-register-ham-routine 'ignore)
1442 (defalias 'spam-stat-register-spam-routine 'ignore)
1445 (defun spam-check-stat ()
1446 "Check the spam-stat backend for the classification of this message"
1447 (let ((spam-split-group (if spam-split-symbolic-return
1450 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1451 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1453 (spam-stat-split-fancy)))
1455 (defun spam-stat-register-spam-routine (articles &optional unregister)
1456 (dolist (article articles)
1457 (let ((article-string (spam-get-article-as-string article)))
1459 (insert article-string)
1461 (spam-stat-buffer-change-to-non-spam)
1462 (spam-stat-buffer-is-spam))))))
1464 (defun spam-stat-unregister-spam-routine (articles)
1465 (spam-stat-register-spam-routine articles t))
1467 (defun spam-stat-register-ham-routine (articles &optional unregister)
1468 (dolist (article articles)
1469 (let ((article-string (spam-get-article-as-string article)))
1471 (insert article-string)
1473 (spam-stat-buffer-change-to-spam)
1474 (spam-stat-buffer-is-non-spam))))))
1476 (defun spam-stat-unregister-ham-routine (articles)
1477 (spam-stat-register-ham-routine articles t))
1479 (defun spam-maybe-spam-stat-load ()
1480 (when spam-use-stat (spam-stat-load)))
1482 (defun spam-maybe-spam-stat-save ()
1483 (when spam-use-stat (spam-stat-save)))))
1487 ;;;; Blacklists and whitelists.
1489 (defvar spam-whitelist-cache nil)
1490 (defvar spam-blacklist-cache nil)
1492 (defun spam-kill-whole-line ()
1494 (let ((kill-whole-line t))
1497 ;;; address can be a list, too
1498 (defun spam-enter-whitelist (address &optional remove)
1499 "Enter ADDRESS (list or single) into the whitelist.
1500 With a non-nil REMOVE, remove them."
1501 (interactive "sAddress: ")
1502 (spam-enter-list address spam-whitelist remove)
1503 (setq spam-whitelist-cache nil))
1505 ;;; address can be a list, too
1506 (defun spam-enter-blacklist (address &optional remove)
1507 "Enter ADDRESS (list or single) into the blacklist.
1508 With a non-nil REMOVE, remove them."
1509 (interactive "sAddress: ")
1510 (spam-enter-list address spam-blacklist remove)
1511 (setq spam-blacklist-cache nil))
1513 (defun spam-enter-list (addresses file &optional remove)
1514 "Enter ADDRESSES into the given FILE.
1515 Either the whitelist or the blacklist files can be used. With
1516 REMOVE not nil, remove the ADDRESSES."
1517 (if (stringp addresses)
1518 (spam-enter-list (list addresses) file remove)
1519 ;; else, we have a list of addresses here
1520 (unless (file-exists-p (file-name-directory file))
1521 (make-directory (file-name-directory file) t))
1524 (find-file-noselect file))
1525 (dolist (a addresses)
1527 (goto-char (point-min))
1528 (if (re-search-forward (regexp-quote a) nil t)
1529 ;; found the address
1531 (spam-kill-whole-line))
1532 ;; else, the address was not found
1534 (goto-char (point-max))
1540 ;;; returns t if the sender is in the whitelist, nil or
1541 ;;; spam-split-group otherwise
1542 (defun spam-check-whitelist ()
1543 ;; FIXME! Should it detect when file timestamps change?
1544 (let ((spam-split-group (if spam-split-symbolic-return
1547 (unless spam-whitelist-cache
1548 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1549 (if (spam-from-listed-p spam-whitelist-cache)
1551 (if spam-use-whitelist-exclusive
1555 (defun spam-check-blacklist ()
1556 ;; FIXME! Should it detect when file timestamps change?
1557 (let ((spam-split-group (if spam-split-symbolic-return
1560 (unless spam-blacklist-cache
1561 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1562 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1564 (defun spam-parse-list (file)
1565 (when (file-readable-p file)
1566 (let (contents address)
1568 (insert-file-contents file)
1570 (setq address (buffer-substring (point) (spam-point-at-eol)))
1572 ;; insert the e-mail address if detected, otherwise the raw data
1573 (unless (zerop (length address))
1574 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1575 (push (or pure-address address) contents)))))
1576 (nreverse contents))))
1578 (defun spam-from-listed-p (cache)
1579 (let ((from (nnmail-fetch-field "from"))
1582 (let ((address (pop cache)))
1583 (unless (zerop (length address)) ; 0 for a nil address too
1584 (setq address (regexp-quote address))
1585 ;; fix regexp-quote's treatment of user-intended regexes
1586 (while (string-match "\\\\\\*" address)
1587 (setq address (replace-match ".*" t t address))))
1588 (when (and address (string-match address from))
1593 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1594 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1595 (declassification (if blacklist 'ham 'spam))
1597 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1599 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1600 from addresses unregister-list)
1601 (dolist (article articles)
1602 (let ((from (spam-fetch-field-from-fast article))
1603 (id (spam-fetch-field-message-id-fast article))
1605 (when (stringp from)
1606 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1607 (when (and (not sender-ignored)
1608 (stringp ignore-regex)
1609 (string-match ignore-regex from))
1610 (setq sender-ignored t)))
1611 ;; remember the messages we need to unregister, unless remove is set
1614 (spam-log-unregistration-needed-p
1615 id 'process declassification de-symbol))
1616 (push from unregister-list))
1617 (unless sender-ignored
1618 (push from addresses)))))
1621 (funcall enter-function addresses t) ; unregister all these addresses
1622 ;; else, register normally and unregister what we need to
1623 (funcall remove-function unregister-list t)
1624 (dolist (article unregister-list)
1625 (spam-log-undo-registration
1626 (spam-fetch-field-message-id-fast article)
1630 (funcall enter-function addresses nil))))
1632 (defun spam-blacklist-unregister-routine (articles)
1633 (spam-blacklist-register-routine articles t))
1635 (defun spam-blacklist-register-routine (articles &optional unregister)
1636 (spam-filelist-register-routine articles t unregister))
1638 (defun spam-whitelist-unregister-routine (articles)
1639 (spam-whitelist-register-routine articles t))
1641 (defun spam-whitelist-register-routine (articles &optional unregister)
1642 (spam-filelist-register-routine articles nil unregister))
1645 ;;;; Spam-report glue
1646 (defun spam-report-gmane-register-routine (articles)
1648 (apply 'spam-report-gmane articles)))
1652 (defun spam-check-bogofilter-headers (&optional score)
1653 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1654 (spam-split-group (if spam-split-symbolic-return
1657 (when header ; return nil when no header
1658 (if score ; scoring mode
1659 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1660 (match-string 1 header)
1662 ;; spam detection mode
1663 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1665 spam-split-group)))))
1667 ;; return something sensible if the score can't be determined
1668 (defun spam-bogofilter-score ()
1669 "Get the Bogofilter spamicity score"
1671 (save-window-excursion
1672 (gnus-summary-show-article t)
1673 (set-buffer gnus-article-buffer)
1674 (let ((score (or (spam-check-bogofilter-headers t)
1675 (spam-check-bogofilter t))))
1676 (message "Spamicity score %s" score)
1678 (gnus-summary-show-article)))
1680 (defun spam-check-bogofilter (&optional score)
1681 "Check the Bogofilter backend for the classification of this message"
1682 (let ((article-buffer-name (buffer-name))
1683 (db spam-bogofilter-database-directory)
1686 (let ((temp-buffer-name (buffer-name)))
1688 (set-buffer article-buffer-name)
1689 (apply 'call-process-region
1690 (point-min) (point-max)
1691 spam-bogofilter-path
1692 nil temp-buffer-name nil
1693 (if db `("-d" ,db "-v") `("-v"))))
1694 (setq return (spam-check-bogofilter-headers score))))
1697 (defun spam-bogofilter-register-with-bogofilter (articles
1699 &optional unregister)
1700 "Register an article, given as a string, as spam or non-spam."
1701 (dolist (article articles)
1702 (let ((article-string (spam-get-article-as-string article))
1703 (db spam-bogofilter-database-directory)
1704 (switch (if unregister
1706 spam-bogofilter-spam-strong-switch
1707 spam-bogofilter-ham-strong-switch)
1709 spam-bogofilter-spam-switch
1710 spam-bogofilter-ham-switch))))
1711 (when (stringp article-string)
1713 (insert article-string)
1715 (apply 'call-process-region
1716 (point-min) (point-max)
1717 spam-bogofilter-path
1719 (if db `("-d" ,db "-v") `("-v"))))))))
1721 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1722 (spam-bogofilter-register-with-bogofilter articles t unregister))
1724 (defun spam-bogofilter-unregister-spam-routine (articles)
1725 (spam-bogofilter-register-spam-routine articles t))
1727 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1728 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1730 (defun spam-bogofilter-unregister-ham-routine (articles)
1731 (spam-bogofilter-register-ham-routine articles t))
1736 (defun spam-check-spamoracle ()
1737 "Run spamoracle on an article to determine whether it's spam."
1738 (let ((article-buffer-name (buffer-name))
1739 (spam-split-group (if spam-split-symbolic-return
1743 (let ((temp-buffer-name (buffer-name)))
1745 (set-buffer article-buffer-name)
1747 (apply 'call-process-region
1748 (point-min) (point-max)
1749 spam-spamoracle-binary
1750 nil temp-buffer-name nil
1751 (if spam-spamoracle-database
1752 `("-f" ,spam-spamoracle-database "mark")
1756 (set-buffer temp-buffer-name)
1757 (goto-char (point-min))
1758 (when (re-search-forward "^X-Spam: yes;" nil t)
1760 (error "Error running spamoracle: %s" status))))))))
1762 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1763 "Run spamoracle in training mode."
1765 (let ((temp-buffer-name (buffer-name)))
1767 (goto-char (point-min))
1768 (dolist (article articles)
1769 (insert (spam-get-article-as-string article)))
1770 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1774 (apply 'call-process-region
1775 (point-min) (point-max)
1776 spam-spamoracle-binary
1777 nil temp-buffer-name nil
1778 (if spam-spamoracle-database
1779 `("-f" ,spam-spamoracle-database
1782 (unless (eq 0 status)
1783 (error "Error running spamoracle: %s" status)))))))
1785 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1786 (spam-spamoracle-learn articles nil unregister))
1788 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1789 (spam-spamoracle-learn-ham articles t))
1791 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1792 (spam-spamoracle-learn articles t unregister))
1794 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1795 (spam-spamoracle-learn-spam articles t))
1801 (defun spam-initialize ()
1802 "Install the spam.el hooks and do other initialization"
1804 (setq spam-install-hooks t)
1805 ;; TODO: How do we redo this every time spam-face is customized?
1806 (push '((eq mark gnus-spam-mark) . spam-face)
1807 gnus-summary-highlight)
1808 ;; Add hooks for loading and saving the spam stats
1809 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1810 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1811 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1812 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1813 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1814 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1815 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1817 (defun spam-unload-hook ()
1818 "Uninstall the spam.el hooks"
1820 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1821 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1822 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1823 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1824 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1825 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1826 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1828 (add-hook 'spam-unload-hook 'spam-unload-hook)
1830 (when spam-install-hooks
1835 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1836 ;;; spam.el ends here