1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004, 2005 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"))
84 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
85 "Directory for spam whitelists and blacklists."
89 (defcustom spam-move-spam-nonspam-groups-only t
90 "Whether spam should be moved in non-spam groups only.
91 When t, only ham and unclassified groups will have their spam moved
92 to the spam-process-destination. When nil, spam will also be moved from
97 (defcustom spam-process-ham-in-nonham-groups nil
98 "Whether ham should be processed in non-ham groups."
102 (defcustom spam-log-to-registry nil
103 "Whether spam/ham processing should be logged in the registry."
107 (defcustom spam-split-symbolic-return nil
108 "Whether `spam-split' should work with symbols or group names."
112 (defcustom spam-split-symbolic-return-positive nil
113 "Whether `spam-split' should ALWAYS work with symbols or group names.
114 Do not set this if you use `spam-split' in a fancy split
119 (defcustom spam-process-ham-in-spam-groups nil
120 "Whether ham should be processed in spam groups."
124 (defcustom spam-mark-only-unseen-as-spam t
125 "Whether only unseen articles should be marked as spam in spam groups.
126 When nil, all unread articles in a spam group are marked as
127 spam. Set this if you want to leave an article unread in a spam group
128 without losing it to the automatic spam-marking process."
132 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
133 "Whether ham should be marked unread before it's moved.
134 The article is moved out of a spam group according to ham-process-destination.
135 This variable is an official entry in the international Longest Variable Name
140 (defcustom spam-disable-spam-split-during-ham-respool nil
141 "Whether `spam-split' should be ignored while resplitting ham in a process
142 destination. This is useful to prevent ham from ending up in the same spam
143 group after the resplit. Don't set this to t if you have spam-split as the
144 last rule in your split configuration."
148 (defcustom spam-autodetect-recheck-messages nil
149 "Should spam.el recheck all meessages when autodetecting?
150 Normally this is nil, so only unseen messages will be checked."
154 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
155 "The location of the whitelist.
156 The file format is one regular expression per line.
157 The regular expression is matched against the address."
161 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
162 "The location of the blacklist.
163 The file format is one regular expression per line.
164 The regular expression is matched against the address."
168 (defcustom spam-use-dig t
169 "Whether `query-dig' should be used instead of `query-dns'."
173 (defcustom spam-use-blacklist nil
174 "Whether the blacklist should be used by `spam-split'."
178 (defcustom spam-blacklist-ignored-regexes nil
179 "Regular expressions that the blacklist should ignore."
180 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
183 (defcustom spam-use-whitelist nil
184 "Whether the whitelist should be used by `spam-split'."
188 (defcustom spam-use-whitelist-exclusive nil
189 "Whether whitelist-exclusive should be used by `spam-split'.
190 Exclusive whitelisting means that all messages from senders not in the whitelist
191 are considered spam."
195 (defcustom spam-use-blackholes nil
196 "Whether blackholes should be used by `spam-split'."
200 (defcustom spam-use-hashcash nil
201 "Whether hashcash payments should be detected by `spam-split'."
205 (defcustom spam-use-regex-headers nil
206 "Whether a header regular expression match should be used by `spam-split'.
207 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
211 (defcustom spam-use-regex-body nil
212 "Whether a body regular expression match should be used by `spam-split'.
213 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
217 (defcustom spam-use-bogofilter-headers nil
218 "Whether bogofilter headers should be used by `spam-split'.
219 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
223 (defcustom spam-use-bogofilter nil
224 "Whether bogofilter should be invoked by `spam-split'.
225 Enable this if you want Gnus to invoke Bogofilter on new messages."
229 (defcustom spam-use-BBDB nil
230 "Whether BBDB should be used by `spam-split'."
234 (defcustom spam-use-BBDB-exclusive nil
235 "Whether BBDB-exclusive should be used by `spam-split'.
236 Exclusive BBDB means that all messages from senders not in the BBDB are
241 (defcustom spam-use-ifile nil
242 "Whether ifile should be used by `spam-split'."
246 (defcustom spam-use-stat nil
247 "Whether `spam-stat' should be used by `spam-split'."
251 (defcustom spam-use-spamoracle nil
252 "Whether spamoracle should be used by `spam-split'."
256 (defcustom spam-install-hooks (or
260 spam-use-whitelist-exclusive
263 spam-use-regex-headers
265 spam-use-bogofilter-headers
268 spam-use-BBDB-exclusive
272 "Whether the spam hooks should be installed.
273 Default to t if one of the spam-use-* variables is set."
277 (defcustom spam-split-group "spam"
278 "Group name where incoming spam should be put by `spam-split'."
282 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
283 ;;; not regular expressions
284 (defcustom spam-junk-mailgroups (cons
286 '("mail.junk" "poste.pourriel"))
287 "Mailgroups with spam contents.
288 All unmarked article in such group receive the spam mark on group entry."
289 :type '(repeat (string :tag "Group"))
292 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
293 "dev.null.dk" "relays.visi.com")
294 "List of blackhole servers."
295 :type '(repeat (string :tag "Server"))
298 (defcustom spam-blackhole-good-server-regex nil
299 "String matching IP addresses that should not be checked in the blackholes."
300 :type '(radio (const nil) regexp)
304 '((((class color) (type tty) (background dark))
305 (:foreground "gray80" :background "gray50"))
306 (((class color) (type tty) (background light))
307 (:foreground "gray50" :background "gray80"))
308 (((class color) (background dark))
309 (:foreground "ivory2"))
310 (((class color) (background light))
311 (:foreground "ivory4"))
312 (t :inverse-video t))
313 "Face for spam-marked articles."
315 ;; backward-compatibility alias
316 (put 'spam-face 'face-alias 'spam)
318 (defcustom spam-face 'spam
319 "Face for spam-marked articles."
323 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
324 "Regular expression for positive header spam matches."
325 :type '(repeat (regexp :tag "Regular expression to match spam header"))
328 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
329 "Regular expression for positive header ham matches."
330 :type '(repeat (regexp :tag "Regular expression to match ham header"))
333 (defcustom spam-regex-body-spam '()
334 "Regular expression for positive body spam matches."
335 :type '(repeat (regexp :tag "Regular expression to match spam body"))
338 (defcustom spam-regex-body-ham '()
339 "Regular expression for positive body ham matches."
340 :type '(repeat (regexp :tag "Regular expression to match ham body"))
343 (defgroup spam-ifile nil
344 "Spam ifile configuration."
347 (defcustom spam-ifile-path (executable-find "ifile")
348 "File path of the ifile executable program."
349 :type '(choice (file :tag "Location of ifile")
350 (const :tag "ifile is not installed"))
353 (defcustom spam-ifile-database-path nil
354 "File path of the ifile database."
355 :type '(choice (file :tag "Location of the ifile database")
356 (const :tag "Use the default"))
359 (defcustom spam-ifile-spam-category "spam"
360 "Name of the spam ifile category."
364 (defcustom spam-ifile-ham-category nil
365 "Name of the ham ifile category.
366 If nil, the current group name will be used."
367 :type '(choice (string :tag "Use a fixed category")
368 (const :tag "Use the current group name"))
371 (defcustom spam-ifile-all-categories nil
372 "Whether the ifile check will return all categories, or just spam.
373 Set this to t if you want to use the `spam-split' invocation of ifile as
374 your main source of newsgroup names."
378 (defgroup spam-bogofilter nil
379 "Spam bogofilter configuration."
382 (defcustom spam-bogofilter-path (executable-find "bogofilter")
383 "File path of the Bogofilter executable program."
384 :type '(choice (file :tag "Location of bogofilter")
385 (const :tag "Bogofilter is not installed"))
386 :group 'spam-bogofilter)
388 (defcustom spam-bogofilter-header "X-Bogosity"
389 "The header that Bogofilter inserts in messages."
391 :group 'spam-bogofilter)
393 (defcustom spam-bogofilter-spam-switch "-s"
394 "The switch that Bogofilter uses to register spam messages."
396 :group 'spam-bogofilter)
398 (defcustom spam-bogofilter-ham-switch "-n"
399 "The switch that Bogofilter uses to register ham messages."
401 :group 'spam-bogofilter)
403 (defcustom spam-bogofilter-spam-strong-switch "-S"
404 "The switch that Bogofilter uses to unregister ham messages."
406 :group 'spam-bogofilter)
408 (defcustom spam-bogofilter-ham-strong-switch "-N"
409 "The switch that Bogofilter uses to unregister spam messages."
411 :group 'spam-bogofilter)
413 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
414 "The regex on `spam-bogofilter-header' for positive spam identification."
416 :group 'spam-bogofilter)
418 (defcustom spam-bogofilter-database-directory nil
419 "Directory path of the Bogofilter databases."
420 :type '(choice (directory
421 :tag "Location of the Bogofilter database directory")
422 (const :tag "Use the default"))
423 :group 'spam-bogofilter)
425 (defgroup spam-spamoracle nil
426 "Spam spamoracle configuration."
429 (defcustom spam-spamoracle-database nil
430 "Location of spamoracle database file. When nil, use the default
431 spamoracle database."
432 :type '(choice (directory :tag "Location of spamoracle database file.")
433 (const :tag "Use the default"))
434 :group 'spam-spamoracle)
436 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
437 "Location of the spamoracle binary."
438 :type '(choice (directory :tag "Location of the spamoracle binary")
439 (const :tag "Use the default"))
440 :group 'spam-spamoracle)
442 ;;; Key bindings for spam control.
444 (gnus-define-keys gnus-summary-mode-map
445 "St" spam-bogofilter-score
446 "Sx" gnus-summary-mark-as-spam
447 "Mst" spam-bogofilter-score
448 "Msx" gnus-summary-mark-as-spam
449 "\M-d" gnus-summary-mark-as-spam)
451 (defvar spam-old-ham-articles nil
452 "List of old ham articles, generated when a group is entered.")
454 (defvar spam-old-spam-articles nil
455 "List of old spam articles, generated when a group is entered.")
457 (defvar spam-split-disabled nil
458 "If non-nil, `spam-split' is disabled, and always returns nil.")
460 (defvar spam-split-last-successful-check nil
461 "`spam-split' will set this to nil or a spam-use-XYZ check if it
464 ;; convenience functions
465 (defun spam-xor (a b)
466 "Logical exclusive `or'."
467 (and (or a b) (not (and a b))))
469 (defun spam-group-ham-mark-p (group mark &optional spam)
470 (when (stringp group)
471 (let* ((marks (spam-group-ham-marks group spam))
472 (marks (if (symbolp mark)
474 (mapcar 'symbol-value marks))))
477 (defun spam-group-spam-mark-p (group mark)
478 (spam-group-ham-mark-p group mark t))
480 (defun spam-group-ham-marks (group &optional spam)
481 (when (stringp group)
482 (let* ((marks (if spam
483 (gnus-parameter-spam-marks group)
484 (gnus-parameter-ham-marks group)))
486 (marks (if (listp (car marks)) (car marks) marks)))
489 (defun spam-group-spam-marks (group)
490 (spam-group-ham-marks group t))
492 (defun spam-group-spam-contents-p (group)
494 (or (member group spam-junk-mailgroups)
495 (memq 'gnus-group-spam-classification-spam
496 (gnus-parameter-spam-contents group)))
499 (defun spam-group-ham-contents-p (group)
501 (memq 'gnus-group-spam-classification-ham
502 (gnus-parameter-spam-contents group))
505 (defvar spam-list-of-processors
506 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
507 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
508 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
509 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
510 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
511 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
512 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
513 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
514 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
515 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
516 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
517 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
518 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
519 "The spam-list-of-processors list contains pairs associating a
520 ham/spam exit processor variable with a classification and a
521 spam-use-* variable.")
523 (defun spam-group-processor-p (group processor)
524 (if (and (stringp group)
526 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
527 (spam-group-processor-multiple-p
529 (cdr-safe (assoc processor spam-list-of-processors))))
532 (defun spam-group-processor-multiple-p (group processor-info)
533 (let* ((classification (nth 0 processor-info))
534 (check (nth 1 processor-info))
535 (parameters (nth 0 (gnus-parameter-spam-process group)))
537 (dolist (parameter parameters)
538 (when (and (null found)
540 (eq classification (nth 0 parameter))
541 (eq check (nth 1 parameter)))
545 (defun spam-group-spam-processor-report-gmane-p (group)
546 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
548 (defun spam-group-spam-processor-bogofilter-p (group)
549 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
551 (defun spam-group-spam-processor-blacklist-p (group)
552 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
554 (defun spam-group-spam-processor-ifile-p (group)
555 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
557 (defun spam-group-ham-processor-ifile-p (group)
558 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
560 (defun spam-group-spam-processor-spamoracle-p (group)
561 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
563 (defun spam-group-ham-processor-bogofilter-p (group)
564 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
566 (defun spam-group-spam-processor-stat-p (group)
567 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
569 (defun spam-group-ham-processor-stat-p (group)
570 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
572 (defun spam-group-ham-processor-whitelist-p (group)
573 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
575 (defun spam-group-ham-processor-BBDB-p (group)
576 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
578 (defun spam-group-ham-processor-copy-p (group)
579 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
581 (defun spam-group-ham-processor-spamoracle-p (group)
582 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
584 ;;; Summary entry and exit processing.
586 (defun spam-summary-prepare ()
587 (setq spam-old-ham-articles
588 (spam-list-articles gnus-newsgroup-articles 'ham))
589 (setq spam-old-spam-articles
590 (spam-list-articles gnus-newsgroup-articles 'spam))
591 (spam-mark-junk-as-spam-routine))
593 ;; The spam processors are invoked for any group, spam or ham or neither
594 (defun spam-summary-prepare-exit ()
595 (unless gnus-group-is-exiting-without-update-p
596 (gnus-message 6 "Exiting summary buffer and applying spam rules")
598 ;; first of all, unregister any articles that are no longer ham or spam
599 ;; we have to iterate over the processors, or else we'll be too slow
600 (dolist (classification '(spam ham))
601 (let* ((old-articles (if (eq classification 'spam)
602 spam-old-spam-articles
603 spam-old-ham-articles))
604 (new-articles (spam-list-articles
605 gnus-newsgroup-articles
607 (changed-articles (gnus-set-difference old-articles new-articles)))
608 ;; now that we have the changed articles, we go through the processors
609 (dolist (processor-param spam-list-of-processors)
610 (let ((processor (nth 0 processor-param))
611 (processor-classification (nth 1 processor-param))
612 (check (nth 2 processor-param))
614 (dolist (article changed-articles)
615 (let ((id (spam-fetch-field-message-id-fast article)))
616 (when (spam-log-unregistration-needed-p
617 id 'process classification check)
618 (push article unregister-list))))
619 ;; call spam-register-routine with specific articles to unregister,
620 ;; when there are articles to unregister and the check is enabled
621 (when (and unregister-list (symbol-value check))
622 (spam-register-routine classification check t unregister-list))))))
624 ;; find all the spam processors applicable to this group
625 (dolist (processor-param spam-list-of-processors)
626 (let ((processor (nth 0 processor-param))
627 (classification (nth 1 processor-param))
628 (check (nth 2 processor-param)))
629 (when (and (eq 'spam classification)
630 (spam-group-processor-p gnus-newsgroup-name processor))
631 (spam-register-routine classification check))))
633 (if spam-move-spam-nonspam-groups-only
634 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
635 (spam-mark-spam-as-expired-and-move-routine
636 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
637 (gnus-message 5 "Marking spam as expired and moving it to %s"
639 (spam-mark-spam-as-expired-and-move-routine
640 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
642 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
643 ;; expire spam, in case the above did not expire them
644 (gnus-message 5 "Marking spam as expired without moving it")
645 (spam-mark-spam-as-expired-and-move-routine nil)
647 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
648 (and (spam-group-spam-contents-p gnus-newsgroup-name)
649 spam-process-ham-in-spam-groups)
650 spam-process-ham-in-nonham-groups)
651 ;; find all the ham processors applicable to this group
652 (dolist (processor-param spam-list-of-processors)
653 (let ((processor (nth 0 processor-param))
654 (classification (nth 1 processor-param))
655 (check (nth 2 processor-param)))
656 (when (and (eq 'ham classification)
657 (spam-group-processor-p gnus-newsgroup-name processor))
658 (spam-register-routine classification check)))))
660 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
661 (gnus-message 5 "Copying ham")
662 (spam-ham-copy-routine
663 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
665 ;; now move all ham articles out of spam groups
666 (when (spam-group-spam-contents-p gnus-newsgroup-name)
667 (gnus-message 5 "Moving ham messages from spam group")
668 (spam-ham-move-routine
669 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
671 (setq spam-old-ham-articles nil)
672 (setq spam-old-spam-articles nil))
674 (defun spam-mark-junk-as-spam-routine ()
675 ;; check the global list of group names spam-junk-mailgroups and the
677 (when (spam-group-spam-contents-p gnus-newsgroup-name)
678 (gnus-message 5 "Marking %s articles as spam"
679 (if spam-mark-only-unseen-as-spam
682 (let ((articles (if spam-mark-only-unseen-as-spam
683 gnus-newsgroup-unseen
684 gnus-newsgroup-unreads)))
685 (dolist (article articles)
686 (gnus-summary-mark-article article gnus-spam-mark)))))
688 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
689 (if (and (car-safe groups) (listp (car-safe groups)))
690 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
691 (gnus-summary-kill-process-mark)
692 (let ((articles gnus-newsgroup-articles)
693 (backend-supports-deletions
694 (gnus-check-backend-function
695 'request-move-article gnus-newsgroup-name))
696 article tomove deletep)
697 (dolist (article articles)
698 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
699 (gnus-summary-mark-article article gnus-expirable-mark)
700 (push article tomove)))
702 ;; now do the actual copies
703 (dolist (group groups)
706 (dolist (article tomove)
707 (gnus-summary-set-process-mark article))
709 (if (or (not backend-supports-deletions)
710 (> (length groups) 1))
712 (gnus-summary-copy-article nil group)
714 (gnus-summary-move-article nil group)))))
716 ;; now delete the articles, if there was a copy done, and the
718 (when (and deletep backend-supports-deletions)
719 (dolist (article tomove)
720 (gnus-summary-set-process-mark article))
722 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
723 (gnus-summary-delete-article nil))))
725 (gnus-summary-yank-process-mark))))
727 (defun spam-ham-copy-or-move-routine (copy groups)
728 (gnus-summary-kill-process-mark)
729 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
730 (backend-supports-deletions
731 (gnus-check-backend-function
732 'request-move-article gnus-newsgroup-name))
733 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
734 article mark todo deletep respool)
736 (when (member 'respool groups)
737 (setq respool t) ; boolean for later
738 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
740 ;; now do the actual move
741 (dolist (group groups)
742 (when (and todo (stringp group))
743 (dolist (article todo)
744 (when spam-mark-ham-unread-before-move-from-spam-group
745 (gnus-summary-mark-article article gnus-unread-mark))
746 (gnus-summary-set-process-mark article))
748 (if respool ; respooling is with a "fake" group
749 (let ((spam-split-disabled
750 (or spam-split-disabled
751 spam-disable-spam-split-during-ham-respool)))
752 (gnus-summary-respool-article nil respool-method))
753 (if (or (not backend-supports-deletions) ; else, we are not respooling
754 (> (length groups) 1))
755 (progn ; if copying, copy and set deletep
756 (gnus-summary-copy-article nil group)
758 (gnus-summary-move-article nil group))))) ; else move articles
760 ;; now delete the articles, unless a) copy is t, and there was a copy done
761 ;; b) a move was done to a single group
762 ;; c) backend-supports-deletions is nil
764 (when (and deletep backend-supports-deletions)
765 (dolist (article todo)
766 (gnus-summary-set-process-mark article))
768 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
769 (gnus-summary-delete-article nil))))))
771 (gnus-summary-yank-process-mark))
773 (defun spam-ham-copy-routine (&rest groups)
774 (if (and (car-safe groups) (listp (car-safe groups)))
775 (apply 'spam-ham-copy-routine (car groups))
776 (spam-ham-copy-or-move-routine t groups)))
778 (defun spam-ham-move-routine (&rest groups)
779 (if (and (car-safe groups) (listp (car-safe groups)))
780 (apply 'spam-ham-move-routine (car groups))
781 (spam-ham-copy-or-move-routine nil groups)))
784 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
786 'line-end-position)))
788 (defun spam-get-article-as-string (article)
789 (let ((article-buffer (spam-get-article-as-buffer article))
792 (save-window-excursion
793 (set-buffer article-buffer)
794 (setq article-string (buffer-string))))
797 (defun spam-get-article-as-buffer (article)
798 (let ((article-buffer))
799 (when (numberp article)
800 (save-window-excursion
801 (gnus-summary-goto-subject article)
802 (gnus-summary-show-article t)
803 (setq article-buffer (get-buffer gnus-article-buffer))))
807 ;; (defun spam-get-article-as-filename (article)
808 ;; (let ((article-filename))
809 ;; (when (numberp article)
810 ;; (nnml-possibly-change-directory
811 ;; (gnus-group-real-name gnus-newsgroup-name))
812 ;; (setq article-filename (expand-file-name
813 ;; (int-to-string article) nnml-current-directory)))
814 ;; (if (file-exists-p article-filename)
818 (defun spam-fetch-field-from-fast (article)
819 "Fetch the `from' field quickly, using the internal gnus-data-list function"
820 (if (and (numberp article)
821 (assoc article (gnus-data-list nil)))
823 (gnus-data-header (assoc article (gnus-data-list nil))))
826 (defun spam-fetch-field-subject-fast (article)
827 "Fetch the `subject' field quickly, using the internal
828 gnus-data-list function"
829 (if (and (numberp article)
830 (assoc article (gnus-data-list nil)))
832 (gnus-data-header (assoc article (gnus-data-list nil))))
835 (defun spam-fetch-field-message-id-fast (article)
836 "Fetch the `Message-ID' field quickly, using the internal
837 gnus-data-list function"
838 (if (and (numberp article)
839 (assoc article (gnus-data-list nil)))
840 (mail-header-message-id
841 (gnus-data-header (assoc article (gnus-data-list nil))))
845 ;;;; Spam determination.
847 (defvar spam-list-of-checks
848 '((spam-use-blacklist . spam-check-blacklist)
849 (spam-use-regex-headers . spam-check-regex-headers)
850 (spam-use-regex-body . spam-check-regex-body)
851 (spam-use-whitelist . spam-check-whitelist)
852 (spam-use-BBDB . spam-check-BBDB)
853 (spam-use-ifile . spam-check-ifile)
854 (spam-use-spamoracle . spam-check-spamoracle)
855 (spam-use-stat . spam-check-stat)
856 (spam-use-blackholes . spam-check-blackholes)
857 (spam-use-hashcash . spam-check-hashcash)
858 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
859 (spam-use-bogofilter . spam-check-bogofilter))
860 "The spam-list-of-checks list contains pairs associating a
861 parameter variable with a spam checking function. If the
862 parameter variable is true, then the checking function is called,
863 and its value decides what happens. Each individual check may
864 return nil, t, or a mailgroup name. The value nil means that the
865 check does not yield a decision, and so, that further checks are
866 needed. The value t means that the message is definitely not
867 spam, and that further spam checks should be inhibited.
868 Otherwise, a mailgroup name or the symbol 'spam (depending on
869 spam-split-symbolic-return) is returned where the mail should go,
870 and further checks are also inhibited. The usual mailgroup name
871 is the value of `spam-split-group', meaning that the message is
874 (defvar spam-list-of-statistical-checks
880 "The spam-list-of-statistical-checks list contains all the mail
881 splitters that need to have the full message body available.")
883 ;;;TODO: modify to invoke self with each check if invoked without specifics
884 (defun spam-split (&rest specific-checks)
885 "Split this message into the `spam' group if it is spam.
886 This function can be used as an entry in the variable `nnmail-split-fancy',
887 for example like this: (: spam-split). It can take checks as
888 parameters. A string as a parameter will set the
889 spam-split-group to that string.
891 See the Info node `(gnus)Fancy Mail Splitting' for more details."
893 (setq spam-split-last-successful-check nil)
894 (unless spam-split-disabled
895 (let ((spam-split-group-choice spam-split-group))
896 (dolist (check specific-checks)
897 (when (stringp check)
898 (setq spam-split-group-choice check)
899 (setq specific-checks (delq check specific-checks))))
901 (let ((spam-split-group spam-split-group-choice))
904 (dolist (check spam-list-of-statistical-checks)
905 (when (and (symbolp check) (symbol-value check))
907 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
910 ;; (progn (widen) (debug (buffer-string)))
911 (let ((list-of-checks spam-list-of-checks)
913 (while (and list-of-checks (not decision))
914 (let ((pair (pop list-of-checks)))
915 (when (and (symbol-value (car pair))
916 (or (null specific-checks)
917 (memq (car pair) specific-checks)))
918 (gnus-message 5 "spam-split: calling the %s function"
919 (symbol-name (cdr pair)))
920 (setq decision (funcall (cdr pair)))
921 ;; if we got a decision at all, save the current check
923 (setq spam-split-last-successful-check (car pair)))
925 (when (eq decision 'spam)
926 (if spam-split-symbolic-return
927 (setq decision spam-split-group)
930 (format "spam-split got %s but %s is nil"
931 (symbol-name decision)
932 (symbol-name spam-split-symbolic-return))))))))
934 (if spam-split-symbolic-return-positive 'ham nil)
937 (defun spam-find-spam ()
938 "This function will detect spam in the current newsgroup using spam-split."
941 (let* ((group gnus-newsgroup-name)
942 (autodetect (gnus-parameter-spam-autodetect group))
943 (methods (gnus-parameter-spam-autodetect-methods group))
944 (first-method (nth 0 methods)))
945 (when (and autodetect
946 (not (equal first-method 'none)))
949 (let ((id (spam-fetch-field-message-id-fast article))
950 (subject (spam-fetch-field-subject-fast article))
951 (sender (spam-fetch-field-from-fast article)))
952 (unless (and spam-log-to-registry
953 (spam-log-registered-p id 'incoming))
954 (let* ((spam-split-symbolic-return t)
955 (spam-split-symbolic-return-positive t)
958 (gnus-request-article-this-buffer
961 (if (or (null first-method)
962 (equal first-method 'default))
964 (apply 'spam-split methods)))))
965 (if (equal split-return 'spam)
966 (gnus-summary-mark-article article gnus-spam-mark))
968 (when (and split-return spam-log-to-registry)
969 (when (zerop (gnus-registry-group-count id))
970 (gnus-registry-add-group
971 id group subject sender))
973 (spam-log-processing-to-registry
977 spam-split-last-successful-check
979 (if spam-autodetect-recheck-messages
980 gnus-newsgroup-articles
981 gnus-newsgroup-unseen)))))
983 (defvar spam-registration-functions
984 ;; first the ham register, second the spam register function
985 ;; third the ham unregister, fourth the spam unregister function
986 '((spam-use-blacklist nil
987 spam-blacklist-register-routine
989 spam-blacklist-unregister-routine)
990 (spam-use-whitelist spam-whitelist-register-routine
992 spam-whitelist-unregister-routine
994 (spam-use-BBDB spam-BBDB-register-routine
996 spam-BBDB-unregister-routine
998 (spam-use-ifile spam-ifile-register-ham-routine
999 spam-ifile-register-spam-routine
1000 spam-ifile-unregister-ham-routine
1001 spam-ifile-unregister-spam-routine)
1002 (spam-use-spamoracle spam-spamoracle-learn-ham
1003 spam-spamoracle-learn-spam
1004 spam-spamoracle-unlearn-ham
1005 spam-spamoracle-unlearn-spam)
1006 (spam-use-stat spam-stat-register-ham-routine
1007 spam-stat-register-spam-routine
1008 spam-stat-unregister-ham-routine
1009 spam-stat-unregister-spam-routine)
1010 ;; note that spam-use-gmane is not a legitimate check
1012 spam-report-gmane-register-routine
1013 ;; does Gmane support unregistration?
1016 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1017 spam-bogofilter-register-spam-routine
1018 spam-bogofilter-unregister-ham-routine
1019 spam-bogofilter-unregister-spam-routine))
1020 "The spam-registration-functions list contains pairs
1021 associating a parameter variable with the ham and spam
1022 registration functions, and the ham and spam unregistration
1025 (defun spam-classification-valid-p (classification)
1026 (or (eq classification 'spam)
1027 (eq classification 'ham)))
1029 (defun spam-process-type-valid-p (process-type)
1030 (or (eq process-type 'incoming)
1031 (eq process-type 'process)))
1033 (defun spam-registration-check-valid-p (check)
1034 (assoc check spam-registration-functions))
1036 (defun spam-unregistration-check-valid-p (check)
1037 (assoc check spam-registration-functions))
1039 (defun spam-registration-function (classification check)
1040 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1041 (if (eq classification 'spam)
1045 (defun spam-unregistration-function (classification check)
1046 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1047 (if (eq classification 'spam)
1051 (defun spam-list-articles (articles classification)
1052 (let ((mark-check (if (eq classification 'spam)
1053 'spam-group-spam-mark-p
1054 'spam-group-ham-mark-p))
1055 list mark-cache-yes mark-cache-no)
1056 (dolist (article articles)
1057 (let ((mark (gnus-summary-article-mark article)))
1058 (unless (memq mark mark-cache-no)
1059 (if (memq mark mark-cache-yes)
1061 ;; else, we have to actually check the mark
1062 (if (funcall mark-check
1067 (push mark mark-cache-yes))
1068 (push mark mark-cache-no))))))
1071 (defun spam-register-routine (classification
1073 &optional unregister
1075 (when (and (spam-classification-valid-p classification)
1076 (spam-registration-check-valid-p check))
1077 (let* ((register-function
1078 (spam-registration-function classification check))
1079 (unregister-function
1080 (spam-unregistration-function classification check))
1081 (run-function (if unregister
1084 (log-function (if unregister
1085 'spam-log-undo-registration
1086 'spam-log-processing-to-registry))
1090 ;; make list of articles, using specific-articles if given
1091 (setq articles (or specific-articles
1093 gnus-newsgroup-articles
1096 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1097 (if unregister "Unregistering" "Registering")
1099 (if specific-articles "specific" "")
1100 (symbol-name classification)
1101 (symbol-name check))
1102 (funcall run-function articles)
1103 ;; now log all the registrations (or undo them, depending on unregister)
1104 (dolist (article articles)
1105 (funcall log-function
1106 (spam-fetch-field-message-id-fast article)
1110 gnus-newsgroup-name))))))
1112 ;;; log a ham- or spam-processor invocation to the registry
1113 (defun spam-log-processing-to-registry (id type classification check group)
1114 (when spam-log-to-registry
1115 (if (and (stringp id)
1117 (spam-process-type-valid-p type)
1118 (spam-classification-valid-p classification)
1119 (spam-registration-check-valid-p check))
1120 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1121 (cell (list classification check group)))
1122 (push cell cell-list)
1123 (gnus-registry-store-extra-entry
1128 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1129 "spam-log-processing-to-registry")))))
1131 ;;; check if a ham- or spam-processor registration has been done
1132 (defun spam-log-registered-p (id type)
1133 (when spam-log-to-registry
1134 (if (and (stringp id)
1135 (spam-process-type-valid-p type))
1136 (cdr-safe (gnus-registry-fetch-extra id type))
1138 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1139 "spam-log-registered-p"))
1142 ;;; check if a ham- or spam-processor registration needs to be undone
1143 (defun spam-log-unregistration-needed-p (id type classification check)
1144 (when spam-log-to-registry
1145 (if (and (stringp id)
1146 (spam-process-type-valid-p type)
1147 (spam-classification-valid-p classification)
1148 (spam-registration-check-valid-p check))
1149 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1151 (dolist (cell cell-list)
1153 (when (and (eq classification (nth 0 cell))
1154 (eq check (nth 1 cell)))
1158 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1159 "spam-log-unregistration-needed-p"))
1163 ;;; undo a ham- or spam-processor registration (the group is not used)
1164 (defun spam-log-undo-registration (id type classification check &optional group)
1165 (when (and spam-log-to-registry
1166 (spam-log-unregistration-needed-p id type classification check))
1167 (if (and (stringp id)
1168 (spam-process-type-valid-p type)
1169 (spam-classification-valid-p classification)
1170 (spam-registration-check-valid-p check))
1171 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1172 new-cell-list found)
1173 (dolist (cell cell-list)
1174 (unless (and (eq classification (nth 0 cell))
1175 (eq check (nth 1 cell)))
1176 (push cell new-cell-list)))
1177 (gnus-registry-store-extra-entry
1182 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1183 "spam-log-undo-registration"))
1186 ;;; set up IMAP widening if it's necessary
1187 (defun spam-setup-widening ()
1188 (dolist (check spam-list-of-statistical-checks)
1189 (when (symbol-value check)
1190 (setq nnimap-split-download-body-default t))))
1195 (defun spam-check-regex-body ()
1196 (let ((spam-regex-headers-ham spam-regex-body-ham)
1197 (spam-regex-headers-spam spam-regex-body-spam))
1198 (spam-check-regex-headers t)))
1203 (defun spam-check-regex-headers (&optional body)
1204 (let ((type (if body "body" "header"))
1205 (spam-split-group (if spam-split-symbolic-return
1209 (dolist (h-regex spam-regex-headers-ham)
1211 (goto-char (point-min))
1212 (when (re-search-forward h-regex nil t)
1213 (message "Ham regex %s search positive." type)
1215 (dolist (s-regex spam-regex-headers-spam)
1217 (goto-char (point-min))
1218 (when (re-search-forward s-regex nil t)
1219 (message "Spam regex %s search positive." type)
1221 (setq ret spam-split-group))))
1227 (defun spam-reverse-ip-string (ip)
1229 (mapconcat 'identity
1230 (nreverse (split-string ip "\\."))
1233 (defun spam-check-blackholes ()
1234 "Check the Received headers for blackholed relays."
1235 (let ((headers (nnmail-fetch-field "received"))
1236 (spam-split-group (if spam-split-symbolic-return
1243 (goto-char (point-min))
1244 (gnus-message 5 "Checking headers for relay addresses")
1245 (while (re-search-forward
1246 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1247 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1248 (push (spam-reverse-ip-string (match-string 1))
1250 (dolist (server spam-blackhole-servers)
1252 (unless (and spam-blackhole-good-server-regex
1253 ;; match the good-server-regex against the reversed (again) IP string
1255 spam-blackhole-good-server-regex
1256 (spam-reverse-ip-string ip)))
1258 (let ((query-string (concat ip "." server)))
1260 (let ((query-result (query-dig query-string)))
1262 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1264 (push (list ip server query-result)
1266 ;; else, if not using dig.el
1267 (when (query-dns query-string)
1268 (gnus-message 5 "positive blackhole check")
1269 (push (list ip server (query-dns query-string 'TXT))
1277 (autoload 'mail-check-payment "hashcash"))
1283 (defun spam-check-hashcash ()
1284 "Check the headers for hashcash payments."
1285 (mail-check-payment))) ;mail-check-payment returns a boolean
1291 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1292 ;;; <sacha@giotto.sj.ru>
1294 ;; all this is done inside a condition-case to trap errors
1297 (autoload 'bbdb-buffer "bbdb")
1298 (autoload 'bbdb-create-internal "bbdb")
1299 (autoload 'bbdb-search-simple "bbdb"))
1302 (when (condition-case nil
1305 (require 'bbdb-com))
1307 (defalias 'spam-BBDB-register-routine 'ignore)
1308 (defalias 'spam-enter-ham-BBDB 'ignore)
1311 (defun spam-enter-ham-BBDB (addresses &optional remove)
1312 "Enter an address into the BBDB; implies ham (non-spam) sender"
1313 (dolist (from addresses)
1314 (when (stringp from)
1315 (let* ((parsed-address (gnus-extract-address-components from))
1316 (name (or (nth 0 parsed-address) "Ham Sender"))
1317 (remove-function (if remove
1318 'bbdb-delete-record-internal
1320 (net-address (nth 1 parsed-address))
1321 (record (and net-address
1322 (bbdb-search-simple nil net-address))))
1324 (gnus-message 5 "%s address %s %s BBDB"
1325 (if remove "Deleting" "Adding")
1327 (if remove "from" "to"))
1329 (funcall remove-function record)
1330 (bbdb-create-internal name nil net-address nil nil
1331 "ham sender added by spam.el")))))))
1333 (defun spam-BBDB-register-routine (articles &optional unregister)
1335 (dolist (article articles)
1336 (when (stringp (spam-fetch-field-from-fast article))
1337 (push (spam-fetch-field-from-fast article) addresses)))
1338 ;; now do the register/unregister action
1339 (spam-enter-ham-BBDB addresses unregister)))
1341 (defun spam-BBDB-unregister-routine (articles)
1342 (spam-BBDB-register-routine articles t))
1344 (defun spam-check-BBDB ()
1345 "Mail from people in the BBDB is classified as ham or non-spam"
1346 (let ((who (nnmail-fetch-field "from"))
1347 (spam-split-group (if spam-split-symbolic-return
1351 (setq who (nth 1 (gnus-extract-address-components who)))
1352 (if (bbdb-search-simple nil who)
1354 (if spam-use-BBDB-exclusive
1361 ;;; check the ifile backend; return nil if the mail was NOT classified
1364 (defun spam-get-ifile-database-parameter ()
1365 "Get the command-line parameter for ifile's database from
1366 spam-ifile-database-path."
1367 (if spam-ifile-database-path
1368 (format "--db-file=%s" spam-ifile-database-path)
1371 (defun spam-check-ifile ()
1372 "Check the ifile backend for the classification of this message."
1373 (let ((article-buffer-name (buffer-name))
1374 (spam-split-group (if spam-split-symbolic-return
1379 (let ((temp-buffer-name (buffer-name))
1380 (db-param (spam-get-ifile-database-parameter)))
1382 (set-buffer article-buffer-name)
1383 (apply 'call-process-region
1384 (point-min) (point-max) spam-ifile-path
1385 nil temp-buffer-name nil "-c"
1386 (if db-param `(,db-param "-q") `("-q"))))
1387 ;; check the return now (we're back in the temp buffer)
1388 (goto-char (point-min))
1390 (setq category (buffer-substring (point) (spam-point-at-eol))))
1391 (when (not (zerop (length category))) ; we need a category here
1392 (if spam-ifile-all-categories
1393 (setq return category)
1394 ;; else, if spam-ifile-all-categories is not set...
1395 (when (string-equal spam-ifile-spam-category category)
1396 (setq return spam-split-group)))))) ; note return is nil otherwise
1399 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1400 "Register an article, given as a string, with a category.
1401 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1402 (let ((category (or category gnus-newsgroup-name))
1403 (add-or-delete-option (if unregister "-d" "-i"))
1404 (db (spam-get-ifile-database-parameter))
1407 (dolist (article articles)
1408 (let ((article-string (spam-get-article-as-string article)))
1409 (when (stringp article-string)
1410 (insert article-string))))
1411 (apply 'call-process-region
1412 (point-min) (point-max) spam-ifile-path
1414 add-or-delete-option category
1415 (if db `(,db "-h") `("-h"))))))
1417 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1418 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1420 (defun spam-ifile-unregister-spam-routine (articles)
1421 (spam-ifile-register-spam-routine articles t))
1423 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1424 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1426 (defun spam-ifile-unregister-ham-routine (articles)
1427 (spam-ifile-register-ham-routine articles t))
1433 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
1434 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
1435 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
1436 (autoload 'spam-stat-buffer-is-spam "spam-stat")
1437 (autoload 'spam-stat-load "spam-stat")
1438 (autoload 'spam-stat-save "spam-stat")
1439 (autoload 'spam-stat-split-fancy "spam-stat"))
1442 (when (condition-case nil
1443 (let ((spam-stat-install-hooks nil))
1444 (require 'spam-stat))
1446 (defalias 'spam-stat-register-ham-routine 'ignore)
1447 (defalias 'spam-stat-register-spam-routine 'ignore)
1450 (defun spam-check-stat ()
1451 "Check the spam-stat backend for the classification of this message"
1452 (let ((spam-split-group (if spam-split-symbolic-return
1455 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1456 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1458 (spam-stat-split-fancy)))
1460 (defun spam-stat-register-spam-routine (articles &optional unregister)
1461 (dolist (article articles)
1462 (let ((article-string (spam-get-article-as-string article)))
1464 (insert article-string)
1466 (spam-stat-buffer-change-to-non-spam)
1467 (spam-stat-buffer-is-spam))))))
1469 (defun spam-stat-unregister-spam-routine (articles)
1470 (spam-stat-register-spam-routine articles t))
1472 (defun spam-stat-register-ham-routine (articles &optional unregister)
1473 (dolist (article articles)
1474 (let ((article-string (spam-get-article-as-string article)))
1476 (insert article-string)
1478 (spam-stat-buffer-change-to-spam)
1479 (spam-stat-buffer-is-non-spam))))))
1481 (defun spam-stat-unregister-ham-routine (articles)
1482 (spam-stat-register-ham-routine articles t))
1484 (defun spam-maybe-spam-stat-load ()
1485 (when spam-use-stat (spam-stat-load)))
1487 (defun spam-maybe-spam-stat-save ()
1488 (when spam-use-stat (spam-stat-save)))))
1492 ;;;; Blacklists and whitelists.
1494 (defvar spam-whitelist-cache nil)
1495 (defvar spam-blacklist-cache nil)
1497 (defun spam-kill-whole-line ()
1499 (let ((kill-whole-line t))
1502 ;;; address can be a list, too
1503 (defun spam-enter-whitelist (address &optional remove)
1504 "Enter ADDRESS (list or single) into the whitelist.
1505 With a non-nil REMOVE, remove them."
1506 (interactive "sAddress: ")
1507 (spam-enter-list address spam-whitelist remove)
1508 (setq spam-whitelist-cache nil))
1510 ;;; address can be a list, too
1511 (defun spam-enter-blacklist (address &optional remove)
1512 "Enter ADDRESS (list or single) into the blacklist.
1513 With a non-nil REMOVE, remove them."
1514 (interactive "sAddress: ")
1515 (spam-enter-list address spam-blacklist remove)
1516 (setq spam-blacklist-cache nil))
1518 (defun spam-enter-list (addresses file &optional remove)
1519 "Enter ADDRESSES into the given FILE.
1520 Either the whitelist or the blacklist files can be used. With
1521 REMOVE not nil, remove the ADDRESSES."
1522 (if (stringp addresses)
1523 (spam-enter-list (list addresses) file remove)
1524 ;; else, we have a list of addresses here
1525 (unless (file-exists-p (file-name-directory file))
1526 (make-directory (file-name-directory file) t))
1529 (find-file-noselect file))
1530 (dolist (a addresses)
1532 (goto-char (point-min))
1533 (if (re-search-forward (regexp-quote a) nil t)
1534 ;; found the address
1536 (spam-kill-whole-line))
1537 ;; else, the address was not found
1539 (goto-char (point-max))
1545 ;;; returns t if the sender is in the whitelist, nil or
1546 ;;; spam-split-group otherwise
1547 (defun spam-check-whitelist ()
1548 ;; FIXME! Should it detect when file timestamps change?
1549 (let ((spam-split-group (if spam-split-symbolic-return
1552 (unless spam-whitelist-cache
1553 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1554 (if (spam-from-listed-p spam-whitelist-cache)
1556 (if spam-use-whitelist-exclusive
1560 (defun spam-check-blacklist ()
1561 ;; FIXME! Should it detect when file timestamps change?
1562 (let ((spam-split-group (if spam-split-symbolic-return
1565 (unless spam-blacklist-cache
1566 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1567 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1569 (defun spam-parse-list (file)
1570 (when (file-readable-p file)
1571 (let (contents address)
1573 (insert-file-contents file)
1575 (setq address (buffer-substring (point) (spam-point-at-eol)))
1577 ;; insert the e-mail address if detected, otherwise the raw data
1578 (unless (zerop (length address))
1579 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1580 (push (or pure-address address) contents)))))
1581 (nreverse contents))))
1583 (defun spam-from-listed-p (cache)
1584 (let ((from (nnmail-fetch-field "from"))
1587 (let ((address (pop cache)))
1588 (unless (zerop (length address)) ; 0 for a nil address too
1589 (setq address (regexp-quote address))
1590 ;; fix regexp-quote's treatment of user-intended regexes
1591 (while (string-match "\\\\\\*" address)
1592 (setq address (replace-match ".*" t t address))))
1593 (when (and address (string-match address from))
1598 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1599 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1600 (declassification (if blacklist 'ham 'spam))
1602 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1604 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1605 from addresses unregister-list)
1606 (dolist (article articles)
1607 (let ((from (spam-fetch-field-from-fast article))
1608 (id (spam-fetch-field-message-id-fast article))
1610 (when (stringp from)
1611 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1612 (when (and (not sender-ignored)
1613 (stringp ignore-regex)
1614 (string-match ignore-regex from))
1615 (setq sender-ignored t)))
1616 ;; remember the messages we need to unregister, unless remove is set
1619 (spam-log-unregistration-needed-p
1620 id 'process declassification de-symbol))
1621 (push from unregister-list))
1622 (unless sender-ignored
1623 (push from addresses)))))
1626 (funcall enter-function addresses t) ; unregister all these addresses
1627 ;; else, register normally and unregister what we need to
1628 (funcall remove-function unregister-list t)
1629 (dolist (article unregister-list)
1630 (spam-log-undo-registration
1631 (spam-fetch-field-message-id-fast article)
1635 (funcall enter-function addresses nil))))
1637 (defun spam-blacklist-unregister-routine (articles)
1638 (spam-blacklist-register-routine articles t))
1640 (defun spam-blacklist-register-routine (articles &optional unregister)
1641 (spam-filelist-register-routine articles t unregister))
1643 (defun spam-whitelist-unregister-routine (articles)
1644 (spam-whitelist-register-routine articles t))
1646 (defun spam-whitelist-register-routine (articles &optional unregister)
1647 (spam-filelist-register-routine articles nil unregister))
1650 ;;;; Spam-report glue
1651 (defun spam-report-gmane-register-routine (articles)
1653 (apply 'spam-report-gmane articles)))
1657 (defun spam-check-bogofilter-headers (&optional score)
1658 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1659 (spam-split-group (if spam-split-symbolic-return
1662 (when header ; return nil when no header
1663 (if score ; scoring mode
1664 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1665 (match-string 1 header)
1667 ;; spam detection mode
1668 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1670 spam-split-group)))))
1672 ;; return something sensible if the score can't be determined
1673 (defun spam-bogofilter-score ()
1674 "Get the Bogofilter spamicity score"
1676 (save-window-excursion
1677 (gnus-summary-show-article t)
1678 (set-buffer gnus-article-buffer)
1679 (let ((score (or (spam-check-bogofilter-headers t)
1680 (spam-check-bogofilter t))))
1681 (message "Spamicity score %s" score)
1683 (gnus-summary-show-article)))
1685 (defun spam-check-bogofilter (&optional score)
1686 "Check the Bogofilter backend for the classification of this message"
1687 (let ((article-buffer-name (buffer-name))
1688 (db spam-bogofilter-database-directory)
1691 (let ((temp-buffer-name (buffer-name)))
1693 (set-buffer article-buffer-name)
1694 (apply 'call-process-region
1695 (point-min) (point-max)
1696 spam-bogofilter-path
1697 nil temp-buffer-name nil
1698 (if db `("-d" ,db "-v") `("-v"))))
1699 (setq return (spam-check-bogofilter-headers score))))
1702 (defun spam-bogofilter-register-with-bogofilter (articles
1704 &optional unregister)
1705 "Register an article, given as a string, as spam or non-spam."
1706 (dolist (article articles)
1707 (let ((article-string (spam-get-article-as-string article))
1708 (db spam-bogofilter-database-directory)
1709 (switch (if unregister
1711 spam-bogofilter-spam-strong-switch
1712 spam-bogofilter-ham-strong-switch)
1714 spam-bogofilter-spam-switch
1715 spam-bogofilter-ham-switch))))
1716 (when (stringp article-string)
1718 (insert article-string)
1720 (apply 'call-process-region
1721 (point-min) (point-max)
1722 spam-bogofilter-path
1724 (if db `("-d" ,db "-v") `("-v"))))))))
1726 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1727 (spam-bogofilter-register-with-bogofilter articles t unregister))
1729 (defun spam-bogofilter-unregister-spam-routine (articles)
1730 (spam-bogofilter-register-spam-routine articles t))
1732 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1733 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1735 (defun spam-bogofilter-unregister-ham-routine (articles)
1736 (spam-bogofilter-register-ham-routine articles t))
1741 (defun spam-check-spamoracle ()
1742 "Run spamoracle on an article to determine whether it's spam."
1743 (let ((article-buffer-name (buffer-name))
1744 (spam-split-group (if spam-split-symbolic-return
1748 (let ((temp-buffer-name (buffer-name)))
1750 (set-buffer article-buffer-name)
1752 (apply 'call-process-region
1753 (point-min) (point-max)
1754 spam-spamoracle-binary
1755 nil temp-buffer-name nil
1756 (if spam-spamoracle-database
1757 `("-f" ,spam-spamoracle-database "mark")
1761 (set-buffer temp-buffer-name)
1762 (goto-char (point-min))
1763 (when (re-search-forward "^X-Spam: yes;" nil t)
1765 (error "Error running spamoracle: %s" status))))))))
1767 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1768 "Run spamoracle in training mode."
1770 (let ((temp-buffer-name (buffer-name)))
1772 (goto-char (point-min))
1773 (dolist (article articles)
1774 (insert (spam-get-article-as-string article)))
1775 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1779 (apply 'call-process-region
1780 (point-min) (point-max)
1781 spam-spamoracle-binary
1782 nil temp-buffer-name nil
1783 (if spam-spamoracle-database
1784 `("-f" ,spam-spamoracle-database
1787 (unless (eq 0 status)
1788 (error "Error running spamoracle: %s" status)))))))
1790 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1791 (spam-spamoracle-learn articles nil unregister))
1793 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1794 (spam-spamoracle-learn-ham articles t))
1796 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1797 (spam-spamoracle-learn articles t unregister))
1799 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1800 (spam-spamoracle-learn-spam articles t))
1806 (defun spam-initialize ()
1807 "Install the spam.el hooks and do other initialization"
1809 (setq spam-install-hooks t)
1810 ;; TODO: How do we redo this every time the `spam' face is customized?
1811 (push '((eq mark gnus-spam-mark) . spam)
1812 gnus-summary-highlight)
1813 ;; Add hooks for loading and saving the spam stats
1814 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1815 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1816 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1817 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1818 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1819 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1820 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1822 (defun spam-unload-hook ()
1823 "Uninstall the spam.el hooks"
1825 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1826 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1827 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1828 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1829 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1830 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1831 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1833 (add-hook 'spam-unload-hook 'spam-unload-hook)
1835 (when spam-install-hooks
1840 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1841 ;;; spam.el ends here