1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;;; This module addresses a few aspects of spam control under Gnus. Page
28 ;;; breaks are used for grouping declarations and documentation relating to
29 ;;; each particular aspect.
31 ;;; The integration with Gnus is not yet complete. See various `FIXME'
32 ;;; comments, below, for supplementary explanations or discussions.
34 ;;; Several TODO items are marked as such
36 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting,
37 ;; remote processing, training through files
41 (eval-when-compile (require 'cl))
45 (require 'gnus-uu) ; because of key prefix issues
46 ;;; for the definitions of group content classification and spam processors
48 (require 'message) ;for the message-fetch-field functions
50 ;; for nnimap-split-download-body-default
51 (eval-when-compile (require 'nnimap))
53 ;; autoload executable-find
55 ;; executable-find is not autoloaded in Emacs 20
56 (autoload 'executable-find "executable"))
60 (autoload 'query-dig "dig"))
62 ;; autoload spam-report
64 (autoload 'spam-report-gmane "spam-report"))
66 ;; autoload gnus-registry
68 (autoload 'gnus-registry-group-count "gnus-registry")
69 (autoload 'gnus-registry-add-group "gnus-registry")
70 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
71 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
75 (autoload 'query-dns "dns"))
85 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
86 "Directory for spam whitelists and blacklists."
90 (defcustom spam-move-spam-nonspam-groups-only t
91 "Whether spam should be moved in non-spam groups only.
92 When t, only ham and unclassified groups will have their spam moved
93 to the spam-process-destination. When nil, spam will also be moved from
98 (defcustom spam-process-ham-in-nonham-groups nil
99 "Whether ham should be processed in non-ham groups."
103 (defcustom spam-log-to-registry nil
104 "Whether spam/ham processing should be logged in the registry."
108 (defcustom spam-split-symbolic-return nil
109 "Whether `spam-split' should work with symbols or group names."
113 (defcustom spam-split-symbolic-return-positive nil
114 "Whether `spam-split' should ALWAYS work with symbols or group names.
115 Do not set this if you use `spam-split' in a fancy split
120 (defcustom spam-process-ham-in-spam-groups nil
121 "Whether ham should be processed in spam groups."
125 (defcustom spam-mark-only-unseen-as-spam t
126 "Whether only unseen articles should be marked as spam in spam groups.
127 When nil, all unread articles in a spam group are marked as
128 spam. Set this if you want to leave an article unread in a spam group
129 without losing it to the automatic spam-marking process."
133 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
134 "Whether ham should be marked unread before it's moved.
135 The article is moved out of a spam group according to ham-process-destination.
136 This variable is an official entry in the international Longest Variable Name
141 (defcustom spam-disable-spam-split-during-ham-respool nil
142 "Whether `spam-split' should be ignored while resplitting ham in a process
143 destination. This is useful to prevent ham from ending up in the same spam
144 group after the resplit. Don't set this to t if you have spam-split as the
145 last rule in your split configuration."
149 (defcustom spam-autodetect-recheck-messages nil
150 "Should spam.el recheck all meessages when autodetecting?
151 Normally this is nil, so only unseen messages will be checked."
155 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
156 "The location of the whitelist.
157 The file format is one regular expression per line.
158 The regular expression is matched against the address."
162 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
163 "The location of the blacklist.
164 The file format is one regular expression per line.
165 The regular expression is matched against the address."
169 (defcustom spam-use-dig t
170 "Whether `query-dig' should be used instead of `query-dns'."
174 (defcustom spam-use-blacklist nil
175 "Whether the blacklist should be used by `spam-split'."
179 (defcustom spam-blacklist-ignored-regexes nil
180 "Regular expressions that the blacklist should ignore."
181 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
184 (defcustom spam-use-whitelist nil
185 "Whether the whitelist should be used by `spam-split'."
189 (defcustom spam-use-whitelist-exclusive nil
190 "Whether whitelist-exclusive should be used by `spam-split'.
191 Exclusive whitelisting means that all messages from senders not in the whitelist
192 are considered spam."
196 (defcustom spam-use-blackholes nil
197 "Whether blackholes should be used by `spam-split'."
201 (defcustom spam-use-hashcash nil
202 "Whether hashcash payments should be detected by `spam-split'."
206 (defcustom spam-use-regex-headers nil
207 "Whether a header regular expression match should be used by `spam-split'.
208 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
212 (defcustom spam-use-regex-body nil
213 "Whether a body regular expression match should be used by `spam-split'.
214 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
218 (defcustom spam-use-bogofilter-headers nil
219 "Whether bogofilter headers should be used by `spam-split'.
220 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
224 (defcustom spam-use-bogofilter nil
225 "Whether bogofilter should be invoked by `spam-split'.
226 Enable this if you want Gnus to invoke Bogofilter on new messages."
230 (defcustom spam-use-BBDB nil
231 "Whether BBDB should be used by `spam-split'."
235 (defcustom spam-use-BBDB-exclusive nil
236 "Whether BBDB-exclusive should be used by `spam-split'.
237 Exclusive BBDB means that all messages from senders not in the BBDB are
242 (defcustom spam-use-ifile nil
243 "Whether ifile should be used by `spam-split'."
247 (defcustom spam-use-stat nil
248 "Whether `spam-stat' should be used by `spam-split'."
252 (defcustom spam-use-spamoracle nil
253 "Whether spamoracle should be used by `spam-split'."
257 (defcustom spam-install-hooks (or
261 spam-use-whitelist-exclusive
264 spam-use-regex-headers
266 spam-use-bogofilter-headers
269 spam-use-BBDB-exclusive
273 "Whether the spam hooks should be installed.
274 Default to t if one of the spam-use-* variables is set."
278 (defcustom spam-split-group "spam"
279 "Group name where incoming spam should be put by `spam-split'."
283 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
284 ;;; not regular expressions
285 (defcustom spam-junk-mailgroups (cons
287 '("mail.junk" "poste.pourriel"))
288 "Mailgroups with spam contents.
289 All unmarked article in such group receive the spam mark on group entry."
290 :type '(repeat (string :tag "Group"))
293 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
294 "dev.null.dk" "relays.visi.com")
295 "List of blackhole servers."
296 :type '(repeat (string :tag "Server"))
299 (defcustom spam-blackhole-good-server-regex nil
300 "String matching IP addresses that should not be checked in the blackholes."
301 :type '(radio (const nil) regexp)
305 '((((class color) (type tty) (background dark))
306 (:foreground "gray80" :background "gray50"))
307 (((class color) (type tty) (background light))
308 (:foreground "gray50" :background "gray80"))
309 (((class color) (background dark))
310 (:foreground "ivory2"))
311 (((class color) (background light))
312 (:foreground "ivory4"))
313 (t :inverse-video t))
314 "Face for spam-marked articles."
316 ;; backward-compatibility alias
317 (put 'spam-face 'face-alias 'spam)
319 (defcustom spam-face 'spam
320 "Face for spam-marked articles."
324 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
325 "Regular expression for positive header spam matches."
326 :type '(repeat (regexp :tag "Regular expression to match spam header"))
329 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
330 "Regular expression for positive header ham matches."
331 :type '(repeat (regexp :tag "Regular expression to match ham header"))
334 (defcustom spam-regex-body-spam '()
335 "Regular expression for positive body spam matches."
336 :type '(repeat (regexp :tag "Regular expression to match spam body"))
339 (defcustom spam-regex-body-ham '()
340 "Regular expression for positive body ham matches."
341 :type '(repeat (regexp :tag "Regular expression to match ham body"))
344 (defgroup spam-ifile nil
345 "Spam ifile configuration."
348 (defcustom spam-ifile-path (executable-find "ifile")
349 "File path of the ifile executable program."
350 :type '(choice (file :tag "Location of ifile")
351 (const :tag "ifile is not installed"))
354 (defcustom spam-ifile-database-path nil
355 "File path of the ifile database."
356 :type '(choice (file :tag "Location of the ifile database")
357 (const :tag "Use the default"))
360 (defcustom spam-ifile-spam-category "spam"
361 "Name of the spam ifile category."
365 (defcustom spam-ifile-ham-category nil
366 "Name of the ham ifile category.
367 If nil, the current group name will be used."
368 :type '(choice (string :tag "Use a fixed category")
369 (const :tag "Use the current group name"))
372 (defcustom spam-ifile-all-categories nil
373 "Whether the ifile check will return all categories, or just spam.
374 Set this to t if you want to use the `spam-split' invocation of ifile as
375 your main source of newsgroup names."
379 (defgroup spam-bogofilter nil
380 "Spam bogofilter configuration."
383 (defcustom spam-bogofilter-path (executable-find "bogofilter")
384 "File path of the Bogofilter executable program."
385 :type '(choice (file :tag "Location of bogofilter")
386 (const :tag "Bogofilter is not installed"))
387 :group 'spam-bogofilter)
389 (defcustom spam-bogofilter-header "X-Bogosity"
390 "The header that Bogofilter inserts in messages."
392 :group 'spam-bogofilter)
394 (defcustom spam-bogofilter-spam-switch "-s"
395 "The switch that Bogofilter uses to register spam messages."
397 :group 'spam-bogofilter)
399 (defcustom spam-bogofilter-ham-switch "-n"
400 "The switch that Bogofilter uses to register ham messages."
402 :group 'spam-bogofilter)
404 (defcustom spam-bogofilter-spam-strong-switch "-S"
405 "The switch that Bogofilter uses to unregister ham messages."
407 :group 'spam-bogofilter)
409 (defcustom spam-bogofilter-ham-strong-switch "-N"
410 "The switch that Bogofilter uses to unregister spam messages."
412 :group 'spam-bogofilter)
414 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
415 "The regex on `spam-bogofilter-header' for positive spam identification."
417 :group 'spam-bogofilter)
419 (defcustom spam-bogofilter-database-directory nil
420 "Directory path of the Bogofilter databases."
421 :type '(choice (directory
422 :tag "Location of the Bogofilter database directory")
423 (const :tag "Use the default"))
424 :group 'spam-bogofilter)
426 (defgroup spam-spamoracle nil
427 "Spam spamoracle configuration."
430 (defcustom spam-spamoracle-database nil
431 "Location of spamoracle database file. When nil, use the default
432 spamoracle database."
433 :type '(choice (directory :tag "Location of spamoracle database file.")
434 (const :tag "Use the default"))
435 :group 'spam-spamoracle)
437 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
438 "Location of the spamoracle binary."
439 :type '(choice (directory :tag "Location of the spamoracle binary")
440 (const :tag "Use the default"))
441 :group 'spam-spamoracle)
443 ;;; Key bindings for spam control.
445 (gnus-define-keys gnus-summary-mode-map
446 "St" spam-bogofilter-score
447 "Sx" gnus-summary-mark-as-spam
448 "Mst" spam-bogofilter-score
449 "Msx" gnus-summary-mark-as-spam
450 "\M-d" gnus-summary-mark-as-spam)
452 (defvar spam-old-ham-articles nil
453 "List of old ham articles, generated when a group is entered.")
455 (defvar spam-old-spam-articles nil
456 "List of old spam articles, generated when a group is entered.")
458 (defvar spam-split-disabled nil
459 "If non-nil, `spam-split' is disabled, and always returns nil.")
461 (defvar spam-split-last-successful-check nil
462 "`spam-split' will set this to nil or a spam-use-XYZ check if it
465 ;; convenience functions
466 (defun spam-xor (a b)
467 "Logical exclusive `or'."
468 (and (or a b) (not (and a b))))
470 (defun spam-group-ham-marks (group &optional spam)
471 (when (stringp group)
472 (let ((marks (car (if spam
473 (gnus-parameter-spam-marks group)
474 (gnus-parameter-ham-marks group)))))
475 (if (listp (car marks))
479 (defun spam-group-spam-contents-p (group)
481 (or (member group spam-junk-mailgroups)
482 (memq 'gnus-group-spam-classification-spam
483 (gnus-parameter-spam-contents group)))
486 (defun spam-group-ham-contents-p (group)
488 (memq 'gnus-group-spam-classification-ham
489 (gnus-parameter-spam-contents group))
492 (defvar spam-list-of-processors
493 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
494 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
495 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
496 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
497 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
498 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
499 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
500 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
501 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
502 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
503 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
504 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
505 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
506 "The spam-list-of-processors list contains pairs associating a
507 ham/spam exit processor variable with a classification and a
508 spam-use-* variable.")
510 (defun spam-group-processor-p (group processor)
511 (if (and (stringp group)
513 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
514 (spam-group-processor-multiple-p
516 (cdr-safe (assoc processor spam-list-of-processors))))
519 (defun spam-group-processor-multiple-p (group processor-info)
520 (let* ((classification (nth 0 processor-info))
521 (check (nth 1 processor-info))
522 (parameters (nth 0 (gnus-parameter-spam-process group)))
524 (dolist (parameter parameters)
525 (when (and (null found)
527 (eq classification (nth 0 parameter))
528 (eq check (nth 1 parameter)))
532 (defun spam-group-spam-processor-report-gmane-p (group)
533 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
535 (defun spam-group-spam-processor-bogofilter-p (group)
536 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
538 (defun spam-group-spam-processor-blacklist-p (group)
539 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
541 (defun spam-group-spam-processor-ifile-p (group)
542 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
544 (defun spam-group-ham-processor-ifile-p (group)
545 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
547 (defun spam-group-spam-processor-spamoracle-p (group)
548 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
550 (defun spam-group-ham-processor-bogofilter-p (group)
551 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
553 (defun spam-group-spam-processor-stat-p (group)
554 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
556 (defun spam-group-ham-processor-stat-p (group)
557 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
559 (defun spam-group-ham-processor-whitelist-p (group)
560 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
562 (defun spam-group-ham-processor-BBDB-p (group)
563 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
565 (defun spam-group-ham-processor-copy-p (group)
566 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
568 (defun spam-group-ham-processor-spamoracle-p (group)
569 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
571 ;;; Summary entry and exit processing.
573 (defun spam-summary-prepare ()
574 (setq spam-old-ham-articles
575 (spam-list-articles gnus-newsgroup-articles 'ham))
576 (setq spam-old-spam-articles
577 (spam-list-articles gnus-newsgroup-articles 'spam))
578 (spam-mark-junk-as-spam-routine))
580 ;; The spam processors are invoked for any group, spam or ham or neither
581 (defun spam-summary-prepare-exit ()
582 (unless gnus-group-is-exiting-without-update-p
583 (gnus-message 6 "Exiting summary buffer and applying spam rules")
585 ;; first of all, unregister any articles that are no longer ham or spam
586 ;; we have to iterate over the processors, or else we'll be too slow
587 (dolist (classification '(spam ham))
588 (let* ((old-articles (if (eq classification 'spam)
589 spam-old-spam-articles
590 spam-old-ham-articles))
591 (new-articles (spam-list-articles
592 gnus-newsgroup-articles
594 (changed-articles (gnus-set-difference old-articles new-articles)))
595 ;; now that we have the changed articles, we go through the processors
596 (dolist (processor-param spam-list-of-processors)
597 (let ((processor (nth 0 processor-param))
598 (processor-classification (nth 1 processor-param))
599 (check (nth 2 processor-param))
601 (dolist (article changed-articles)
602 (let ((id (spam-fetch-field-message-id-fast article)))
603 (when (spam-log-unregistration-needed-p
604 id 'process classification check)
605 (push article unregister-list))))
606 ;; call spam-register-routine with specific articles to unregister,
607 ;; when there are articles to unregister and the check is enabled
608 (when (and unregister-list (symbol-value check))
609 (spam-register-routine classification check t unregister-list))))))
611 ;; find all the spam processors applicable to this group
612 (dolist (processor-param spam-list-of-processors)
613 (let ((processor (nth 0 processor-param))
614 (classification (nth 1 processor-param))
615 (check (nth 2 processor-param)))
616 (when (and (eq 'spam classification)
617 (spam-group-processor-p gnus-newsgroup-name processor))
618 (spam-register-routine classification check))))
620 (if spam-move-spam-nonspam-groups-only
621 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
622 (spam-mark-spam-as-expired-and-move-routine
623 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
624 (gnus-message 5 "Marking spam as expired and moving it to %s"
626 (spam-mark-spam-as-expired-and-move-routine
627 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
629 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
630 ;; expire spam, in case the above did not expire them
631 (gnus-message 5 "Marking spam as expired without moving it")
632 (spam-mark-spam-as-expired-and-move-routine nil)
634 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
635 (and (spam-group-spam-contents-p gnus-newsgroup-name)
636 spam-process-ham-in-spam-groups)
637 spam-process-ham-in-nonham-groups)
638 ;; find all the ham processors applicable to this group
639 (dolist (processor-param spam-list-of-processors)
640 (let ((processor (nth 0 processor-param))
641 (classification (nth 1 processor-param))
642 (check (nth 2 processor-param)))
643 (when (and (eq 'ham classification)
644 (spam-group-processor-p gnus-newsgroup-name processor))
645 (spam-register-routine classification check)))))
647 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
648 (gnus-message 5 "Copying ham")
649 (spam-ham-copy-routine
650 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
652 ;; now move all ham articles out of spam groups
653 (when (spam-group-spam-contents-p gnus-newsgroup-name)
654 (gnus-message 5 "Moving ham messages from spam group")
655 (spam-ham-move-routine
656 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
658 (setq spam-old-ham-articles nil)
659 (setq spam-old-spam-articles nil))
661 (defun spam-mark-junk-as-spam-routine ()
662 ;; check the global list of group names spam-junk-mailgroups and the
664 (when (spam-group-spam-contents-p gnus-newsgroup-name)
665 (gnus-message 5 "Marking %s articles as spam"
666 (if spam-mark-only-unseen-as-spam
669 (let ((articles (if spam-mark-only-unseen-as-spam
670 gnus-newsgroup-unseen
671 gnus-newsgroup-unreads)))
672 (dolist (article articles)
673 (gnus-summary-mark-article article gnus-spam-mark)))))
675 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
676 (if (and (car-safe groups) (listp (car-safe groups)))
677 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
678 (gnus-summary-kill-process-mark)
679 (let ((articles gnus-newsgroup-articles)
680 (backend-supports-deletions
681 (gnus-check-backend-function
682 'request-move-article gnus-newsgroup-name))
683 article tomove deletep)
684 (dolist (article articles)
685 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
686 (gnus-summary-mark-article article gnus-expirable-mark)
687 (push article tomove)))
689 ;; now do the actual copies
690 (dolist (group groups)
693 (dolist (article tomove)
694 (gnus-summary-set-process-mark article))
696 (if (or (not backend-supports-deletions)
697 (> (length groups) 1))
699 (gnus-summary-copy-article nil group)
701 (gnus-summary-move-article nil group)))))
703 ;; now delete the articles, if there was a copy done, and the
705 (when (and deletep backend-supports-deletions)
706 (dolist (article tomove)
707 (gnus-summary-set-process-mark article))
709 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
710 (gnus-summary-delete-article nil))))
712 (gnus-summary-yank-process-mark))))
714 (defun spam-ham-copy-or-move-routine (copy groups)
715 (gnus-summary-kill-process-mark)
716 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
717 (backend-supports-deletions
718 (gnus-check-backend-function
719 'request-move-article gnus-newsgroup-name))
720 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
721 article mark todo deletep respool)
723 (when (member 'respool groups)
724 (setq respool t) ; boolean for later
725 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
727 ;; now do the actual move
728 (dolist (group groups)
729 (when (and todo (stringp group))
730 (dolist (article todo)
731 (when spam-mark-ham-unread-before-move-from-spam-group
732 (gnus-summary-mark-article article gnus-unread-mark))
733 (gnus-summary-set-process-mark article))
735 (if respool ; respooling is with a "fake" group
736 (let ((spam-split-disabled
737 (or spam-split-disabled
738 spam-disable-spam-split-during-ham-respool)))
739 (gnus-summary-respool-article nil respool-method))
740 (if (or (not backend-supports-deletions) ; else, we are not respooling
741 (> (length groups) 1))
742 (progn ; if copying, copy and set deletep
743 (gnus-summary-copy-article nil group)
745 (gnus-summary-move-article nil group))))) ; else move articles
747 ;; now delete the articles, unless a) copy is t, and there was a copy done
748 ;; b) a move was done to a single group
749 ;; c) backend-supports-deletions is nil
751 (when (and deletep backend-supports-deletions)
752 (dolist (article todo)
753 (gnus-summary-set-process-mark article))
755 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
756 (gnus-summary-delete-article nil))))))
758 (gnus-summary-yank-process-mark))
760 (defun spam-ham-copy-routine (&rest groups)
761 (if (and (car-safe groups) (listp (car-safe groups)))
762 (apply 'spam-ham-copy-routine (car groups))
763 (spam-ham-copy-or-move-routine t groups)))
765 (defun spam-ham-move-routine (&rest groups)
766 (if (and (car-safe groups) (listp (car-safe groups)))
767 (apply 'spam-ham-move-routine (car groups))
768 (spam-ham-copy-or-move-routine nil groups)))
771 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
773 'line-end-position)))
775 (defun spam-get-article-as-string (article)
776 (let ((article-buffer (spam-get-article-as-buffer article))
779 (save-window-excursion
780 (set-buffer article-buffer)
781 (setq article-string (buffer-string))))
784 (defun spam-get-article-as-buffer (article)
785 (let ((article-buffer))
786 (when (numberp article)
787 (save-window-excursion
788 (gnus-summary-goto-subject article)
789 (gnus-summary-show-article t)
790 (setq article-buffer (get-buffer gnus-article-buffer))))
794 ;; (defun spam-get-article-as-filename (article)
795 ;; (let ((article-filename))
796 ;; (when (numberp article)
797 ;; (nnml-possibly-change-directory
798 ;; (gnus-group-real-name gnus-newsgroup-name))
799 ;; (setq article-filename (expand-file-name
800 ;; (int-to-string article) nnml-current-directory)))
801 ;; (if (file-exists-p article-filename)
805 (defun spam-fetch-field-from-fast (article)
806 "Fetch the `from' field quickly, using the internal gnus-data-list function"
807 (if (and (numberp article)
808 (assoc article (gnus-data-list nil)))
810 (gnus-data-header (assoc article (gnus-data-list nil))))
813 (defun spam-fetch-field-subject-fast (article)
814 "Fetch the `subject' field quickly, using the internal
815 gnus-data-list function"
816 (if (and (numberp article)
817 (assoc article (gnus-data-list nil)))
819 (gnus-data-header (assoc article (gnus-data-list nil))))
822 (defun spam-fetch-field-message-id-fast (article)
823 "Fetch the `Message-ID' field quickly, using the internal
824 gnus-data-list function"
825 (if (and (numberp article)
826 (assoc article (gnus-data-list nil)))
827 (mail-header-message-id
828 (gnus-data-header (assoc article (gnus-data-list nil))))
832 ;;;; Spam determination.
834 (defvar spam-list-of-checks
835 '((spam-use-blacklist . spam-check-blacklist)
836 (spam-use-regex-headers . spam-check-regex-headers)
837 (spam-use-regex-body . spam-check-regex-body)
838 (spam-use-whitelist . spam-check-whitelist)
839 (spam-use-BBDB . spam-check-BBDB)
840 (spam-use-ifile . spam-check-ifile)
841 (spam-use-spamoracle . spam-check-spamoracle)
842 (spam-use-stat . spam-check-stat)
843 (spam-use-blackholes . spam-check-blackholes)
844 (spam-use-hashcash . spam-check-hashcash)
845 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
846 (spam-use-bogofilter . spam-check-bogofilter))
847 "The spam-list-of-checks list contains pairs associating a
848 parameter variable with a spam checking function. If the
849 parameter variable is true, then the checking function is called,
850 and its value decides what happens. Each individual check may
851 return nil, t, or a mailgroup name. The value nil means that the
852 check does not yield a decision, and so, that further checks are
853 needed. The value t means that the message is definitely not
854 spam, and that further spam checks should be inhibited.
855 Otherwise, a mailgroup name or the symbol 'spam (depending on
856 spam-split-symbolic-return) is returned where the mail should go,
857 and further checks are also inhibited. The usual mailgroup name
858 is the value of `spam-split-group', meaning that the message is
861 (defvar spam-list-of-statistical-checks
867 "The spam-list-of-statistical-checks list contains all the mail
868 splitters that need to have the full message body available.")
870 ;;;TODO: modify to invoke self with each check if invoked without specifics
871 (defun spam-split (&rest specific-checks)
872 "Split this message into the `spam' group if it is spam.
873 This function can be used as an entry in the variable `nnmail-split-fancy',
874 for example like this: (: spam-split). It can take checks as
875 parameters. A string as a parameter will set the
876 spam-split-group to that string.
878 See the Info node `(gnus)Fancy Mail Splitting' for more details."
880 (setq spam-split-last-successful-check nil)
881 (unless spam-split-disabled
882 (let ((spam-split-group-choice spam-split-group))
883 (dolist (check specific-checks)
884 (when (stringp check)
885 (setq spam-split-group-choice check)
886 (setq specific-checks (delq check specific-checks))))
888 (let ((spam-split-group spam-split-group-choice))
891 (dolist (check spam-list-of-statistical-checks)
892 (when (and (symbolp check) (symbol-value check))
894 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
897 ;; (progn (widen) (debug (buffer-string)))
898 (let ((list-of-checks spam-list-of-checks)
900 (while (and list-of-checks (not decision))
901 (let ((pair (pop list-of-checks)))
902 (when (and (symbol-value (car pair))
903 (or (null specific-checks)
904 (memq (car pair) specific-checks)))
905 (gnus-message 5 "spam-split: calling the %s function"
906 (symbol-name (cdr pair)))
907 (setq decision (funcall (cdr pair)))
908 ;; if we got a decision at all, save the current check
910 (setq spam-split-last-successful-check (car pair)))
912 (when (eq decision 'spam)
913 (if spam-split-symbolic-return
914 (setq decision spam-split-group)
917 (format "spam-split got %s but %s is nil"
918 (symbol-name decision)
919 (symbol-name spam-split-symbolic-return))))))))
921 (if spam-split-symbolic-return-positive 'ham nil)
924 (defun spam-find-spam ()
925 "This function will detect spam in the current newsgroup using spam-split."
928 (let* ((group gnus-newsgroup-name)
929 (autodetect (gnus-parameter-spam-autodetect group))
930 (methods (gnus-parameter-spam-autodetect-methods group))
931 (first-method (nth 0 methods)))
932 (when (and autodetect
933 (not (equal first-method 'none)))
936 (let ((id (spam-fetch-field-message-id-fast article))
937 (subject (spam-fetch-field-subject-fast article))
938 (sender (spam-fetch-field-from-fast article)))
939 (unless (and spam-log-to-registry
940 (spam-log-registered-p id 'incoming))
941 (let* ((spam-split-symbolic-return t)
942 (spam-split-symbolic-return-positive t)
945 (gnus-request-article-this-buffer
948 (if (or (null first-method)
949 (equal first-method 'default))
951 (apply 'spam-split methods)))))
952 (if (equal split-return 'spam)
953 (gnus-summary-mark-article article gnus-spam-mark))
955 (when (and split-return spam-log-to-registry)
956 (when (zerop (gnus-registry-group-count id))
957 (gnus-registry-add-group
958 id group subject sender))
960 (spam-log-processing-to-registry
964 spam-split-last-successful-check
966 (if spam-autodetect-recheck-messages
967 gnus-newsgroup-articles
968 gnus-newsgroup-unseen)))))
970 (defvar spam-registration-functions
971 ;; first the ham register, second the spam register function
972 ;; third the ham unregister, fourth the spam unregister function
973 '((spam-use-blacklist nil
974 spam-blacklist-register-routine
976 spam-blacklist-unregister-routine)
977 (spam-use-whitelist spam-whitelist-register-routine
979 spam-whitelist-unregister-routine
981 (spam-use-BBDB spam-BBDB-register-routine
983 spam-BBDB-unregister-routine
985 (spam-use-ifile spam-ifile-register-ham-routine
986 spam-ifile-register-spam-routine
987 spam-ifile-unregister-ham-routine
988 spam-ifile-unregister-spam-routine)
989 (spam-use-spamoracle spam-spamoracle-learn-ham
990 spam-spamoracle-learn-spam
991 spam-spamoracle-unlearn-ham
992 spam-spamoracle-unlearn-spam)
993 (spam-use-stat spam-stat-register-ham-routine
994 spam-stat-register-spam-routine
995 spam-stat-unregister-ham-routine
996 spam-stat-unregister-spam-routine)
997 ;; note that spam-use-gmane is not a legitimate check
999 spam-report-gmane-register-routine
1000 ;; does Gmane support unregistration?
1003 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1004 spam-bogofilter-register-spam-routine
1005 spam-bogofilter-unregister-ham-routine
1006 spam-bogofilter-unregister-spam-routine))
1007 "The spam-registration-functions list contains pairs
1008 associating a parameter variable with the ham and spam
1009 registration functions, and the ham and spam unregistration
1012 (defun spam-classification-valid-p (classification)
1013 (or (eq classification 'spam)
1014 (eq classification 'ham)))
1016 (defun spam-process-type-valid-p (process-type)
1017 (or (eq process-type 'incoming)
1018 (eq process-type 'process)))
1020 (defun spam-registration-check-valid-p (check)
1021 (assoc check spam-registration-functions))
1023 (defun spam-unregistration-check-valid-p (check)
1024 (assoc check spam-registration-functions))
1026 (defun spam-registration-function (classification check)
1027 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1028 (if (eq classification 'spam)
1032 (defun spam-unregistration-function (classification check)
1033 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1034 (if (eq classification 'spam)
1038 (defun spam-list-articles (articles classification)
1039 (let ((marks (mapcar 'eval (spam-group-ham-marks gnus-newsgroup-name
1040 (eq classification 'spam))))
1042 (dolist (article articles)
1043 (if (memq (gnus-summary-article-mark article) marks)
1044 (push article list)))
1047 (defun spam-register-routine (classification
1049 &optional unregister
1051 (when (and (spam-classification-valid-p classification)
1052 (spam-registration-check-valid-p check))
1053 (let* ((register-function
1054 (spam-registration-function classification check))
1055 (unregister-function
1056 (spam-unregistration-function classification check))
1057 (run-function (if unregister
1060 (log-function (if unregister
1061 'spam-log-undo-registration
1062 'spam-log-processing-to-registry))
1066 ;; make list of articles, using specific-articles if given
1067 (setq articles (or specific-articles
1069 gnus-newsgroup-articles
1072 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1073 (if unregister "Unregistering" "Registering")
1075 (if specific-articles "specific" "")
1076 (symbol-name classification)
1077 (symbol-name check))
1078 (funcall run-function articles)
1079 ;; now log all the registrations (or undo them, depending on unregister)
1080 (dolist (article articles)
1081 (funcall log-function
1082 (spam-fetch-field-message-id-fast article)
1086 gnus-newsgroup-name))))))
1088 ;;; log a ham- or spam-processor invocation to the registry
1089 (defun spam-log-processing-to-registry (id type classification check group)
1090 (when spam-log-to-registry
1091 (if (and (stringp id)
1093 (spam-process-type-valid-p type)
1094 (spam-classification-valid-p classification)
1095 (spam-registration-check-valid-p check))
1096 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1097 (cell (list classification check group)))
1098 (push cell cell-list)
1099 (gnus-registry-store-extra-entry
1104 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1105 "spam-log-processing-to-registry")))))
1107 ;;; check if a ham- or spam-processor registration has been done
1108 (defun spam-log-registered-p (id type)
1109 (when spam-log-to-registry
1110 (if (and (stringp id)
1111 (spam-process-type-valid-p type))
1112 (cdr-safe (gnus-registry-fetch-extra id type))
1114 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1115 "spam-log-registered-p"))
1118 ;;; check if a ham- or spam-processor registration needs to be undone
1119 (defun spam-log-unregistration-needed-p (id type classification check)
1120 (when spam-log-to-registry
1121 (if (and (stringp id)
1122 (spam-process-type-valid-p type)
1123 (spam-classification-valid-p classification)
1124 (spam-registration-check-valid-p check))
1125 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1127 (dolist (cell cell-list)
1129 (when (and (eq classification (nth 0 cell))
1130 (eq check (nth 1 cell)))
1134 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1135 "spam-log-unregistration-needed-p"))
1139 ;;; undo a ham- or spam-processor registration (the group is not used)
1140 (defun spam-log-undo-registration (id type classification check &optional group)
1141 (when (and spam-log-to-registry
1142 (spam-log-unregistration-needed-p id type classification check))
1143 (if (and (stringp id)
1144 (spam-process-type-valid-p type)
1145 (spam-classification-valid-p classification)
1146 (spam-registration-check-valid-p check))
1147 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1148 new-cell-list found)
1149 (dolist (cell cell-list)
1150 (unless (and (eq classification (nth 0 cell))
1151 (eq check (nth 1 cell)))
1152 (push cell new-cell-list)))
1153 (gnus-registry-store-extra-entry
1158 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1159 "spam-log-undo-registration"))
1162 ;;; set up IMAP widening if it's necessary
1163 (defun spam-setup-widening ()
1164 (dolist (check spam-list-of-statistical-checks)
1165 (when (symbol-value check)
1166 (setq nnimap-split-download-body-default t))))
1171 (defun spam-check-regex-body ()
1172 (let ((spam-regex-headers-ham spam-regex-body-ham)
1173 (spam-regex-headers-spam spam-regex-body-spam))
1174 (spam-check-regex-headers t)))
1179 (defun spam-check-regex-headers (&optional body)
1180 (let ((type (if body "body" "header"))
1181 (spam-split-group (if spam-split-symbolic-return
1185 (dolist (h-regex spam-regex-headers-ham)
1187 (goto-char (point-min))
1188 (when (re-search-forward h-regex nil t)
1189 (message "Ham regex %s search positive." type)
1191 (dolist (s-regex spam-regex-headers-spam)
1193 (goto-char (point-min))
1194 (when (re-search-forward s-regex nil t)
1195 (message "Spam regex %s search positive." type)
1197 (setq ret spam-split-group))))
1203 (defun spam-reverse-ip-string (ip)
1205 (mapconcat 'identity
1206 (nreverse (split-string ip "\\."))
1209 (defun spam-check-blackholes ()
1210 "Check the Received headers for blackholed relays."
1211 (let ((headers (nnmail-fetch-field "received"))
1212 (spam-split-group (if spam-split-symbolic-return
1219 (goto-char (point-min))
1220 (gnus-message 5 "Checking headers for relay addresses")
1221 (while (re-search-forward
1222 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1223 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1224 (push (spam-reverse-ip-string (match-string 1))
1226 (dolist (server spam-blackhole-servers)
1228 (unless (and spam-blackhole-good-server-regex
1229 ;; match the good-server-regex against the reversed (again) IP string
1231 spam-blackhole-good-server-regex
1232 (spam-reverse-ip-string ip)))
1234 (let ((query-string (concat ip "." server)))
1236 (let ((query-result (query-dig query-string)))
1238 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1240 (push (list ip server query-result)
1242 ;; else, if not using dig.el
1243 (when (query-dns query-string)
1244 (gnus-message 5 "positive blackhole check")
1245 (push (list ip server (query-dns query-string 'TXT))
1253 (autoload 'mail-check-payment "hashcash"))
1259 (defun spam-check-hashcash ()
1260 "Check the headers for hashcash payments."
1261 (mail-check-payment))) ;mail-check-payment returns a boolean
1267 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1268 ;;; <sacha@giotto.sj.ru>
1270 ;; all this is done inside a condition-case to trap errors
1273 (autoload 'bbdb-buffer "bbdb")
1274 (autoload 'bbdb-create-internal "bbdb")
1275 (autoload 'bbdb-search-simple "bbdb"))
1278 (when (condition-case nil
1281 (require 'bbdb-com))
1283 (defalias 'spam-BBDB-register-routine 'ignore)
1284 (defalias 'spam-enter-ham-BBDB 'ignore)
1287 (defun spam-enter-ham-BBDB (addresses &optional remove)
1288 "Enter an address into the BBDB; implies ham (non-spam) sender"
1289 (dolist (from addresses)
1290 (when (stringp from)
1291 (let* ((parsed-address (gnus-extract-address-components from))
1292 (name (or (nth 0 parsed-address) "Ham Sender"))
1293 (remove-function (if remove
1294 'bbdb-delete-record-internal
1296 (net-address (nth 1 parsed-address))
1297 (record (and net-address
1298 (bbdb-search-simple nil net-address))))
1300 (gnus-message 5 "%s address %s %s BBDB"
1301 (if remove "Deleting" "Adding")
1303 (if remove "from" "to"))
1305 (funcall remove-function record)
1306 (bbdb-create-internal name nil net-address nil nil
1307 "ham sender added by spam.el")))))))
1309 (defun spam-BBDB-register-routine (articles &optional unregister)
1311 (dolist (article articles)
1312 (when (stringp (spam-fetch-field-from-fast article))
1313 (push (spam-fetch-field-from-fast article) addresses)))
1314 ;; now do the register/unregister action
1315 (spam-enter-ham-BBDB addresses unregister)))
1317 (defun spam-BBDB-unregister-routine (articles)
1318 (spam-BBDB-register-routine articles t))
1320 (defun spam-check-BBDB ()
1321 "Mail from people in the BBDB is classified as ham or non-spam"
1322 (let ((who (nnmail-fetch-field "from"))
1323 (spam-split-group (if spam-split-symbolic-return
1327 (setq who (nth 1 (gnus-extract-address-components who)))
1328 (if (bbdb-search-simple nil who)
1330 (if spam-use-BBDB-exclusive
1337 ;;; check the ifile backend; return nil if the mail was NOT classified
1340 (defun spam-get-ifile-database-parameter ()
1341 "Get the command-line parameter for ifile's database from
1342 spam-ifile-database-path."
1343 (if spam-ifile-database-path
1344 (format "--db-file=%s" spam-ifile-database-path)
1347 (defun spam-check-ifile ()
1348 "Check the ifile backend for the classification of this message."
1349 (let ((article-buffer-name (buffer-name))
1350 (spam-split-group (if spam-split-symbolic-return
1355 (let ((temp-buffer-name (buffer-name))
1356 (db-param (spam-get-ifile-database-parameter)))
1358 (set-buffer article-buffer-name)
1359 (apply 'call-process-region
1360 (point-min) (point-max) spam-ifile-path
1361 nil temp-buffer-name nil "-c"
1362 (if db-param `(,db-param "-q") `("-q"))))
1363 ;; check the return now (we're back in the temp buffer)
1364 (goto-char (point-min))
1366 (setq category (buffer-substring (point) (spam-point-at-eol))))
1367 (when (not (zerop (length category))) ; we need a category here
1368 (if spam-ifile-all-categories
1369 (setq return category)
1370 ;; else, if spam-ifile-all-categories is not set...
1371 (when (string-equal spam-ifile-spam-category category)
1372 (setq return spam-split-group)))))) ; note return is nil otherwise
1375 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1376 "Register an article, given as a string, with a category.
1377 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1378 (let ((category (or category gnus-newsgroup-name))
1379 (add-or-delete-option (if unregister "-d" "-i"))
1380 (db (spam-get-ifile-database-parameter))
1383 (dolist (article articles)
1384 (let ((article-string (spam-get-article-as-string article)))
1385 (when (stringp article-string)
1386 (insert article-string))))
1387 (apply 'call-process-region
1388 (point-min) (point-max) spam-ifile-path
1390 add-or-delete-option category
1391 (if db `(,db "-h") `("-h"))))))
1393 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1394 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1396 (defun spam-ifile-unregister-spam-routine (articles)
1397 (spam-ifile-register-spam-routine articles t))
1399 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1400 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1402 (defun spam-ifile-unregister-ham-routine (articles)
1403 (spam-ifile-register-ham-routine articles t))
1409 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
1410 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
1411 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
1412 (autoload 'spam-stat-buffer-is-spam "spam-stat")
1413 (autoload 'spam-stat-load "spam-stat")
1414 (autoload 'spam-stat-save "spam-stat")
1415 (autoload 'spam-stat-split-fancy "spam-stat"))
1418 (when (condition-case nil
1419 (let ((spam-stat-install-hooks nil))
1420 (require 'spam-stat))
1422 (defalias 'spam-stat-register-ham-routine 'ignore)
1423 (defalias 'spam-stat-register-spam-routine 'ignore)
1426 (defun spam-check-stat ()
1427 "Check the spam-stat backend for the classification of this message"
1428 (let ((spam-split-group (if spam-split-symbolic-return
1431 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1432 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1434 (spam-stat-split-fancy)))
1436 (defun spam-stat-register-spam-routine (articles &optional unregister)
1437 (dolist (article articles)
1438 (let ((article-string (spam-get-article-as-string article)))
1440 (insert article-string)
1442 (spam-stat-buffer-change-to-non-spam)
1443 (spam-stat-buffer-is-spam))))))
1445 (defun spam-stat-unregister-spam-routine (articles)
1446 (spam-stat-register-spam-routine articles t))
1448 (defun spam-stat-register-ham-routine (articles &optional unregister)
1449 (dolist (article articles)
1450 (let ((article-string (spam-get-article-as-string article)))
1452 (insert article-string)
1454 (spam-stat-buffer-change-to-spam)
1455 (spam-stat-buffer-is-non-spam))))))
1457 (defun spam-stat-unregister-ham-routine (articles)
1458 (spam-stat-register-ham-routine articles t))
1460 (defun spam-maybe-spam-stat-load ()
1461 (when spam-use-stat (spam-stat-load)))
1463 (defun spam-maybe-spam-stat-save ()
1464 (when spam-use-stat (spam-stat-save)))))
1468 ;;;; Blacklists and whitelists.
1470 (defvar spam-whitelist-cache nil)
1471 (defvar spam-blacklist-cache nil)
1473 (defun spam-kill-whole-line ()
1475 (let ((kill-whole-line t))
1478 ;;; address can be a list, too
1479 (defun spam-enter-whitelist (address &optional remove)
1480 "Enter ADDRESS (list or single) into the whitelist.
1481 With a non-nil REMOVE, remove them."
1482 (interactive "sAddress: ")
1483 (spam-enter-list address spam-whitelist remove)
1484 (setq spam-whitelist-cache nil))
1486 ;;; address can be a list, too
1487 (defun spam-enter-blacklist (address &optional remove)
1488 "Enter ADDRESS (list or single) into the blacklist.
1489 With a non-nil REMOVE, remove them."
1490 (interactive "sAddress: ")
1491 (spam-enter-list address spam-blacklist remove)
1492 (setq spam-blacklist-cache nil))
1494 (defun spam-enter-list (addresses file &optional remove)
1495 "Enter ADDRESSES into the given FILE.
1496 Either the whitelist or the blacklist files can be used. With
1497 REMOVE not nil, remove the ADDRESSES."
1498 (if (stringp addresses)
1499 (spam-enter-list (list addresses) file remove)
1500 ;; else, we have a list of addresses here
1501 (unless (file-exists-p (file-name-directory file))
1502 (make-directory (file-name-directory file) t))
1505 (find-file-noselect file))
1506 (dolist (a addresses)
1508 (goto-char (point-min))
1509 (if (re-search-forward (regexp-quote a) nil t)
1510 ;; found the address
1512 (spam-kill-whole-line))
1513 ;; else, the address was not found
1515 (goto-char (point-max))
1521 ;;; returns t if the sender is in the whitelist, nil or
1522 ;;; spam-split-group otherwise
1523 (defun spam-check-whitelist ()
1524 ;; FIXME! Should it detect when file timestamps change?
1525 (let ((spam-split-group (if spam-split-symbolic-return
1528 (unless spam-whitelist-cache
1529 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1530 (if (spam-from-listed-p spam-whitelist-cache)
1532 (if spam-use-whitelist-exclusive
1536 (defun spam-check-blacklist ()
1537 ;; FIXME! Should it detect when file timestamps change?
1538 (let ((spam-split-group (if spam-split-symbolic-return
1541 (unless spam-blacklist-cache
1542 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1543 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1545 (defun spam-parse-list (file)
1546 (when (file-readable-p file)
1547 (let (contents address)
1549 (insert-file-contents file)
1551 (setq address (buffer-substring (point) (spam-point-at-eol)))
1553 ;; insert the e-mail address if detected, otherwise the raw data
1554 (unless (zerop (length address))
1555 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1556 (push (or pure-address address) contents)))))
1557 (nreverse contents))))
1559 (defun spam-from-listed-p (cache)
1560 (let ((from (nnmail-fetch-field "from"))
1563 (let ((address (pop cache)))
1564 (unless (zerop (length address)) ; 0 for a nil address too
1565 (setq address (regexp-quote address))
1566 ;; fix regexp-quote's treatment of user-intended regexes
1567 (while (string-match "\\\\\\*" address)
1568 (setq address (replace-match ".*" t t address))))
1569 (when (and address (string-match address from))
1574 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1575 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1576 (declassification (if blacklist 'ham 'spam))
1578 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1580 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1581 from addresses unregister-list)
1582 (dolist (article articles)
1583 (let ((from (spam-fetch-field-from-fast article))
1584 (id (spam-fetch-field-message-id-fast article))
1586 (when (stringp from)
1587 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1588 (when (and (not sender-ignored)
1589 (stringp ignore-regex)
1590 (string-match ignore-regex from))
1591 (setq sender-ignored t)))
1592 ;; remember the messages we need to unregister, unless remove is set
1595 (spam-log-unregistration-needed-p
1596 id 'process declassification de-symbol))
1597 (push from unregister-list))
1598 (unless sender-ignored
1599 (push from addresses)))))
1602 (funcall enter-function addresses t) ; unregister all these addresses
1603 ;; else, register normally and unregister what we need to
1604 (funcall remove-function unregister-list t)
1605 (dolist (article unregister-list)
1606 (spam-log-undo-registration
1607 (spam-fetch-field-message-id-fast article)
1611 (funcall enter-function addresses nil))))
1613 (defun spam-blacklist-unregister-routine (articles)
1614 (spam-blacklist-register-routine articles t))
1616 (defun spam-blacklist-register-routine (articles &optional unregister)
1617 (spam-filelist-register-routine articles t unregister))
1619 (defun spam-whitelist-unregister-routine (articles)
1620 (spam-whitelist-register-routine articles t))
1622 (defun spam-whitelist-register-routine (articles &optional unregister)
1623 (spam-filelist-register-routine articles nil unregister))
1626 ;;;; Spam-report glue
1627 (defun spam-report-gmane-register-routine (articles)
1629 (apply 'spam-report-gmane articles)))
1633 (defun spam-check-bogofilter-headers (&optional score)
1634 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1635 (spam-split-group (if spam-split-symbolic-return
1638 (when header ; return nil when no header
1639 (if score ; scoring mode
1640 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1641 (match-string 1 header)
1643 ;; spam detection mode
1644 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1646 spam-split-group)))))
1648 ;; return something sensible if the score can't be determined
1649 (defun spam-bogofilter-score ()
1650 "Get the Bogofilter spamicity score"
1652 (save-window-excursion
1653 (gnus-summary-show-article t)
1654 (set-buffer gnus-article-buffer)
1655 (let ((score (or (spam-check-bogofilter-headers t)
1656 (spam-check-bogofilter t))))
1657 (message "Spamicity score %s" score)
1659 (gnus-summary-show-article)))
1661 (defun spam-check-bogofilter (&optional score)
1662 "Check the Bogofilter backend for the classification of this message"
1663 (let ((article-buffer-name (buffer-name))
1664 (db spam-bogofilter-database-directory)
1667 (let ((temp-buffer-name (buffer-name)))
1669 (set-buffer article-buffer-name)
1670 (apply 'call-process-region
1671 (point-min) (point-max)
1672 spam-bogofilter-path
1673 nil temp-buffer-name nil
1674 (if db `("-d" ,db "-v") `("-v"))))
1675 (setq return (spam-check-bogofilter-headers score))))
1678 (defun spam-bogofilter-register-with-bogofilter (articles
1680 &optional unregister)
1681 "Register an article, given as a string, as spam or non-spam."
1682 (dolist (article articles)
1683 (let ((article-string (spam-get-article-as-string article))
1684 (db spam-bogofilter-database-directory)
1685 (switch (if unregister
1687 spam-bogofilter-spam-strong-switch
1688 spam-bogofilter-ham-strong-switch)
1690 spam-bogofilter-spam-switch
1691 spam-bogofilter-ham-switch))))
1692 (when (stringp article-string)
1694 (insert article-string)
1696 (apply 'call-process-region
1697 (point-min) (point-max)
1698 spam-bogofilter-path
1700 (if db `("-d" ,db "-v") `("-v"))))))))
1702 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1703 (spam-bogofilter-register-with-bogofilter articles t unregister))
1705 (defun spam-bogofilter-unregister-spam-routine (articles)
1706 (spam-bogofilter-register-spam-routine articles t))
1708 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1709 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1711 (defun spam-bogofilter-unregister-ham-routine (articles)
1712 (spam-bogofilter-register-ham-routine articles t))
1717 (defun spam-check-spamoracle ()
1718 "Run spamoracle on an article to determine whether it's spam."
1719 (let ((article-buffer-name (buffer-name))
1720 (spam-split-group (if spam-split-symbolic-return
1724 (let ((temp-buffer-name (buffer-name)))
1726 (set-buffer article-buffer-name)
1728 (apply 'call-process-region
1729 (point-min) (point-max)
1730 spam-spamoracle-binary
1731 nil temp-buffer-name nil
1732 (if spam-spamoracle-database
1733 `("-f" ,spam-spamoracle-database "mark")
1737 (set-buffer temp-buffer-name)
1738 (goto-char (point-min))
1739 (when (re-search-forward "^X-Spam: yes;" nil t)
1741 (error "Error running spamoracle: %s" status))))))))
1743 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1744 "Run spamoracle in training mode."
1746 (let ((temp-buffer-name (buffer-name)))
1748 (goto-char (point-min))
1749 (dolist (article articles)
1750 (insert (spam-get-article-as-string article)))
1751 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1755 (apply 'call-process-region
1756 (point-min) (point-max)
1757 spam-spamoracle-binary
1758 nil temp-buffer-name nil
1759 (if spam-spamoracle-database
1760 `("-f" ,spam-spamoracle-database
1763 (unless (eq 0 status)
1764 (error "Error running spamoracle: %s" status)))))))
1766 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1767 (spam-spamoracle-learn articles nil unregister))
1769 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1770 (spam-spamoracle-learn-ham articles t))
1772 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1773 (spam-spamoracle-learn articles t unregister))
1775 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1776 (spam-spamoracle-learn-spam articles t))
1782 (defun spam-initialize ()
1783 "Install the spam.el hooks and do other initialization"
1785 (setq spam-install-hooks t)
1786 ;; TODO: How do we redo this every time the `spam' face is customized?
1787 (push '((eq mark gnus-spam-mark) . spam)
1788 gnus-summary-highlight)
1789 ;; Add hooks for loading and saving the spam stats
1790 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1791 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1792 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1793 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1794 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1795 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1796 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1798 (defun spam-unload-hook ()
1799 "Uninstall the spam.el hooks"
1801 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1802 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1803 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1804 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1805 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1806 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1807 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1809 (add-hook 'spam-unload-hook 'spam-unload-hook)
1811 (when spam-install-hooks
1816 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1817 ;;; spam.el ends here