]> code.delx.au - gnu-emacs/blobdiff - lisp/mh-e/mh-junk.el
(mh-alias-read-address-map): Define within defvar.
[gnu-emacs] / lisp / mh-e / mh-junk.el
index 5d2bf87581ef9775d5ecae6d3e74b1dd4dd2721f..e3ca8ad24e72859596b433f18808ec6a5ea843f2 100644 (file)
@@ -1,6 +1,6 @@
-;;; mh-junk.el --- Interface to anti-spam measures
+;;; mh-junk.el --- MH-E interface to anti-spam measures
 
-;; Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
+;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Satyaki Das <satyaki@theforce.stanford.edu>,
 ;;         Bill Wohler <wohler@newt.com>
@@ -9,10 +9,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,9 +20,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 
 ;;; Code:
 
-(eval-when-compile (require 'mh-acros))
-(mh-require-cl)
 (require 'mh-e)
+(require 'mh-scan)
+(mh-require-cl)
 
-;; Interactive functions callable from the folder buffer
 ;;;###mh-autoload
 (defun mh-junk-blacklist (range)
   "Blacklist RANGE as spam.
@@ -105,14 +102,18 @@ RANGE is read in interactive use."
 (defvar mh-spamassassin-executable (executable-find "spamassassin"))
 (defvar mh-sa-learn-executable (executable-find "sa-learn"))
 
+;;;###mh-autoload
 (defun mh-spamassassin-blacklist (msg)
   "Blacklist MSG with SpamAssassin.
 
-SpamAssassin is one of the more popular spam filtering programs. Get
-it from your local distribution or from http://spamassassin.org/.
+SpamAssassin is one of the more popular spam filtering programs.
+Get it from your local distribution or from
+http://spamassassin.org/.
 
-To use SpamAssassin, add the following recipes to \".procmailrc\":
+To use SpamAssassin, add the following recipes to
+\".procmailrc\":
 
+    PATH=$PATH:/usr/bin/mh
     MAILDIR=$HOME/`mhparam Path`
 
     # Fight spam with SpamAssassin.
@@ -130,88 +131,95 @@ To use SpamAssassin, add the following recipes to \".procmailrc\":
 
 If you don't use \"spamc\", use \"spamassassin -P -a\".
 
-Note that one of the recipes above throws away messages with a score
-greater than or equal to 10. Here's how you can determine a value that
-works best for you.
+Note that one of the recipes above throws away messages with a
+score greater than or equal to 10. Here's how you can determine a
+value that works best for you.
 
-First, run \"spamassassin -t\" on every mail message in your archive and
-use Gnumeric to verify that the average plus the standard deviation of
-good mail is under 5, the SpamAssassin default for \"spam\".
+First, run \"spamassassin -t\" on every mail message in your
+archive and use Gnumeric to verify that the average plus the
+standard deviation of good mail is under 5, the SpamAssassin
+default for \"spam\".
 
-Using Gnumeric, sort the messages by score and view the messages with
-the highest score. Determine the score which encompasses all of your
-interesting messages and add a couple of points to be conservative.
-Add that many dots to the \"X-Spam-Level:\" header field above to send
-messages with that score down the drain.
+Using Gnumeric, sort the messages by score and view the messages
+with the highest score. Determine the score which encompasses all
+of your interesting messages and add a couple of points to be
+conservative. Add that many dots to the \"X-Spam-Level:\" header
+field above to send messages with that score down the drain.
 
-In the example above, messages with a score of 5-9 are set aside in
-the \"+spam\" folder for later review. The major weakness of rules-based
-filters is a plethora of false positives so it is worthwhile to check.
+In the example above, messages with a score of 5-9 are set aside
+in the \"+spam\" folder for later review. The major weakness of
+rules-based filters is a plethora of false positives so it is
+worthwhile to check.
 
-If SpamAssassin classifies a message incorrectly, or is unsure, you
-can use the MH-E commands \\[mh-junk-blacklist] and
+If SpamAssassin classifies a message incorrectly, or is unsure,
+you can use the MH-E commands \\[mh-junk-blacklist] and
 \\[mh-junk-whitelist].
 
-The \\[mh-junk-blacklist] command adds a \"blacklist_from\" entry to
-\"~/spamassassin/user_prefs\", deletes the message, and sends the
-message to the Razor, so that others might not see this spam. If the
-\"sa-learn\" command is available, the message is also recategorized as
-spam.
+The command \\[mh-junk-blacklist] adds a \"blacklist_from\" entry
+to \"~/spamassassin/user_prefs\", deletes the message, and sends
+the message to the Razor, so that others might not see this spam.
+If the \"sa-learn\" command is available, the message is also
+recategorized as spam.
 
-The \\[mh-junk-whitelist] command adds a \"whitelist_from\" rule to the
-\"~/.spamassassin/user_prefs\" file. If the \"sa-learn\" command is
-available, the message is also recategorized as ham.
+The command \\[mh-junk-whitelist] adds a \"whitelist_from\" rule
+to the \"~/.spamassassin/user_prefs\" file. If the \"sa-learn\"
+command is available, the message is also recategorized as ham.
 
 Over time, you'll observe that the same host or domain occurs
-repeatedly in the \"blacklist_from\" entries, so you might think that
-you could avoid future spam by blacklisting all mail from a particular
-domain. The utility function `mh-spamassassin-identify-spammers' helps
-you do precisely that. This function displays a frequency count of the
-hosts and domains in the \"blacklist_from\" entries from the last blank
-line in \"~/.spamassassin/user_prefs\" to the end of the file. This
+repeatedly in the \"blacklist_from\" entries, so you might think
+that you could avoid future spam by blacklisting all mail from a
+particular domain. The utility function
+`mh-spamassassin-identify-spammers' helps you do precisely that.
+This function displays a frequency count of the hosts and domains
+in the \"blacklist_from\" entries from the last blank line in
+\"~/.spamassassin/user_prefs\" to the end of the file. This
 information can be used so that you can replace multiple
 \"blacklist_from\" entries with a single wildcard entry such as:
 
     blacklist_from *@*amazingoffersdirect2u.com
 
 In versions of SpamAssassin (2.50 and on) that support a Bayesian
-classifier, \\[mh-junk-blacklist] uses the \"sa-learn\" program to
-recategorize the message as spam. Neither MH-E, nor SpamAssassin,
-rebuilds the database after adding words, so you will need to run
-\"sa-learn --rebuild\" periodically. This can be done by adding the
-following to your crontab:
+classifier, \\[mh-junk-blacklist] uses the program \"sa-learn\"
+to recategorize the message as spam. Neither MH-E, nor
+SpamAssassin, rebuilds the database after adding words, so you
+will need to run \"sa-learn --rebuild\" periodically. This can be
+done by adding the following to your crontab:
 
-    0 * * * *  sa-learn --rebuild > /dev/null 2>&1"
+    0 * * * *   sa-learn --rebuild > /dev/null 2>&1"
   (unless mh-spamassassin-executable
     (error "Unable to find the spamassassin executable"))
   (let ((current-folder mh-current-folder)
         (msg-file (mh-msg-filename msg mh-current-folder))
         (sender))
-    (save-excursion
-      (message "Reporting message %d..." msg)
-      (mh-truncate-log-buffer)
-      (call-process mh-spamassassin-executable msg-file mh-log-buffer nil
+    (message "Reporting message %d..." msg)
+    (mh-truncate-log-buffer)
+    ;; Put call-process output in log buffer if we are saving it
+    ;; (this happens if mh-junk-background is t).
+    (with-current-buffer mh-log-buffer
+      (call-process mh-spamassassin-executable msg-file mh-junk-background nil
                     ;;"--report" "--remove-from-whitelist"
                     "-r" "-R")          ; spamassassin V2.20
-      (when mh-sa-learn-executable
-          (message "Recategorizing this message as spam...")
-          (call-process mh-sa-learn-executable msg-file mh-log-buffer nil
-                        "--single" "--spam" "--local" "--no-rebuild"))
-      (message "Blacklisting message %d..." msg)
-      (set-buffer (get-buffer-create mh-temp-buffer))
+    (when mh-sa-learn-executable
+      (message "Recategorizing message %d as spam..." msg)
+      (mh-truncate-log-buffer)
+      (call-process mh-sa-learn-executable msg-file mh-junk-background nil
+                    "--single" "--spam" "--local" "--no-rebuild")))
+    (message "Blacklisting sender of message %d..." msg)
+    (with-current-buffer (get-buffer-create mh-temp-buffer)
       (erase-buffer)
       (call-process (expand-file-name mh-scan-prog mh-progs)
-                    nil mh-junk-background nil
-                    (format "%s" msg) current-folder
+                    nil t nil
+                    (format "%d" msg) current-folder
                     "-format" "%<(mymbox{from})%|%(addr{from})%>")
       (goto-char (point-min))
       (if (search-forward-regexp "^\\(.+\\)$" nil t)
           (progn
             (setq sender (match-string 0))
             (mh-spamassassin-add-rule "blacklist_from" sender)
-            (message "Blacklisting message %d...done" msg))
-        (message "Blacklisting message %d...not done (from my address)" msg)))))
+            (message "Blacklisting sender of message %d...done" msg))
+        (message "Blacklisting sender of message %d...not done (from my address)" msg)))))
 
+;;;###mh-autoload
 (defun mh-spamassassin-whitelist (msg)
   "Whitelist MSG with SpamAssassin.
 
@@ -225,28 +233,31 @@ See `mh-spamassassin-blacklist' for more information."
   (let ((msg-file (mh-msg-filename msg mh-current-folder))
         (show-buffer (get-buffer mh-show-buffer))
         from)
-    (save-excursion
-      (set-buffer (get-buffer-create mh-temp-buffer))
+    (with-current-buffer (get-buffer-create mh-temp-buffer)
       (erase-buffer)
-      (message "Removing spamassassin markup from message...")
-      (call-process mh-spamassassin-executable msg-file mh-temp-buffer nil
+      (message "Removing spamassassin markup from message %d..." msg)
+      (call-process mh-spamassassin-executable msg-file t nil
                     ;; "--remove-markup"
                     "-d")               ; spamassassin V2.20
       (if show-buffer
           (kill-buffer show-buffer))
       (write-file msg-file)
       (when mh-sa-learn-executable
-        (message "Recategorizing this message as ham...")
-        (call-process mh-sa-learn-executable msg-file mh-temp-buffer nil
-                      "--single" "--ham" "--local --no-rebuild"))
-      (message "Whitelisting message %d..." msg)
+        (message "Recategorizing message %d as ham..." msg)
+        (mh-truncate-log-buffer)
+        ;; Put call-process output in log buffer if we are saving it
+        ;; (this happens if mh-junk-background is t).
+        (with-current-buffer mh-log-buffer
+          (call-process mh-sa-learn-executable msg-file mh-junk-background nil
+                        "--single" "--ham" "--local" "--no-rebuild")))
+      (message "Whitelisting sender of message %d..." msg)
       (setq from
             (car (mh-funcall-if-exists
                   ietf-drums-parse-address (mh-get-header-field "From:"))))
       (kill-buffer nil)
       (unless (or (null from) (equal from ""))
         (mh-spamassassin-add-rule "whitelist_from" from))
-      (message "Whitelisting message %d...done" msg))))
+      (message "Whitelisting sender of message %d...done" msg))))
 
 (defun mh-spamassassin-add-rule (rule body)
   "Add a new rule to \"~/.spamassassin/user_prefs\".
@@ -265,6 +276,7 @@ The name of the rule is RULE and its body is BODY."
       (if (not buffer-exists)
           (kill-buffer nil)))))
 
+;;;###mh-autoload
 (defun mh-spamassassin-identify-spammers ()
   "Identify spammers who are repeat offenders.
 
@@ -314,6 +326,7 @@ information can be used so that you can replace multiple
 
 (defvar mh-bogofilter-executable (executable-find "bogofilter"))
 
+;;;###mh-autoload
 (defun mh-bogofilter-blacklist (msg)
   "Blacklist MSG with bogofilter.
 
@@ -335,6 +348,7 @@ type of message to start doing a good job.
 
 To use bogofilter, add the following recipes to \".procmailrc\":
 
+    PATH=$PATH:/usr/bin/mh
     MAILDIR=$HOME/`mhparam Path`
 
     # Fight spam with bogofilter.
@@ -364,9 +378,14 @@ The \"Bogofilter tuning HOWTO\" describes how you can fine-tune Bogofilter."
   (unless mh-bogofilter-executable
     (error "Unable to find the bogofilter executable"))
   (let ((msg-file (mh-msg-filename msg mh-current-folder)))
-    (call-process mh-bogofilter-executable msg-file mh-junk-background
-                  nil "-s")))
+    (mh-truncate-log-buffer)
+    ;; Put call-process output in log buffer if we are saving it
+    ;; (this happens if mh-junk-background is t).
+    (with-current-buffer mh-log-buffer
+      (call-process mh-bogofilter-executable msg-file mh-junk-background
+                    nil "-s"))))
 
+;;;###mh-autoload
 (defun mh-bogofilter-whitelist (msg)
   "Whitelist MSG with bogofilter.
 
@@ -374,8 +393,12 @@ See `mh-bogofilter-blacklist' for more information."
   (unless mh-bogofilter-executable
     (error "Unable to find the bogofilter executable"))
   (let ((msg-file (mh-msg-filename msg mh-current-folder)))
-    (call-process mh-bogofilter-executable msg-file mh-junk-background
-                  nil "-n")))
+    (mh-truncate-log-buffer)
+    ;; Put call-process output in log buffer if we are saving it
+    ;; (this happens if mh-junk-background is t).
+    (with-current-buffer mh-log-buffer
+      (call-process mh-bogofilter-executable msg-file mh-junk-background
+                    nil "-n"))))
 
 \f
 
@@ -383,6 +406,7 @@ See `mh-bogofilter-blacklist' for more information."
 
 (defvar mh-spamprobe-executable (executable-find "spamprobe"))
 
+;;;###mh-autoload
 (defun mh-spamprobe-blacklist (msg)
   "Blacklist MSG with SpamProbe.
 
@@ -391,6 +415,7 @@ distribution or from http://spamprobe.sourceforge.net.
 
 To use SpamProbe, add the following recipes to \".procmailrc\":
 
+    PATH=$PATH:/usr/bin/mh
     MAILDIR=$HOME/`mhparam Path`
 
     # Fight spam with SpamProbe.
@@ -410,9 +435,14 @@ update SpamProbe's training."
   (unless mh-spamprobe-executable
     (error "Unable to find the spamprobe executable"))
   (let ((msg-file (mh-msg-filename msg mh-current-folder)))
-    (call-process mh-spamprobe-executable msg-file mh-junk-background
-                  nil "spam")))
+    (mh-truncate-log-buffer)
+    ;; Put call-process output in log buffer if we are saving it
+    ;; (this happens if mh-junk-background is t).
+    (with-current-buffer mh-log-buffer
+      (call-process mh-spamprobe-executable msg-file mh-junk-background
+                    nil "spam"))))
 
+;;;###mh-autoload
 (defun mh-spamprobe-whitelist (msg)
   "Whitelist MSG with SpamProbe.
 
@@ -420,8 +450,12 @@ See `mh-spamprobe-blacklist' for more information."
   (unless mh-spamprobe-executable
     (error "Unable to find the spamprobe executable"))
   (let ((msg-file (mh-msg-filename msg mh-current-folder)))
-    (call-process mh-spamprobe-executable msg-file mh-junk-background
-                  nil "good")))
+    (mh-truncate-log-buffer)
+    ;; Put call-process output in log buffer if we are saving it
+    ;; (this happens if mh-junk-background is t).
+    (with-current-buffer mh-log-buffer
+      (call-process mh-spamprobe-executable msg-file mh-junk-background
+                    nil "good"))))
 
 (provide 'mh-junk)