]> diplodocus.org Git - minc/blobdiff - minc
Drop -h and use Getopt::Std automatic --help. New $VERSION global.
[minc] / minc
diff --git a/minc b/minc
index 2f85227..83acac4 100755 (executable)
--- a/minc
+++ b/minc
@@ -25,27 +25,28 @@ matching (m//) commands.
 use strict;
 use warnings;
 
-require 'sysexits.ph';
-
 use Data::Dumper;
 use Errno;
 use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
 use FileHandle;
 use File::Basename;
 use File::stat;
-use Getopt::Std;
+use Getopt::Std; $Getopt::Std::STANDARD_HELP_VERSION = 1;
 use Log::Dispatch;
 use Log::Dispatch::File;
 use POSIX qw(strftime WEXITSTATUS WIFEXITED);
 
-# Autoflush STDOUT for the benefit of the status reporting in kill_spam().
-STDOUT->autoflush(1);
+our $VERSION = 1;
 
 # If a filter set's header is $MAGIC_TO_TOKEN, that set is compared
 # against headers matching this regex (taken from procmail).
 my $MAGIC_TO_REGEX = '^((Original-)?(Resent-)?(To|Cc|Bcc)|(X-Envelope |Apparently(-Resent)?)-To)';
 my $MAGIC_TO_TOKEN = ' TO';
 
+# List of SPAM message numbers, scanned at the end so the user can
+# check for false positives.
+my @SPAM;
+
 =head1 OPTIONS
 
 =over 4
@@ -55,11 +56,7 @@ my $MAGIC_TO_TOKEN = ' TO';
 Dump (using Data::Dumper) the FILTERS list and exit.  This is useful
 for testing the syntax of .mincfilter.
 
-=item B<-f>
-
-Filter only, then exit.  This is useful after running B<minc -s>.
-
-=item B<-h>
+=item B<--help>
 
 Show help.
 
@@ -74,37 +71,24 @@ Print the filename of each message before checking it for spam.  This
 can be handy if a particular message is giving the spam checker a
 problem.
 
-=item B<-s>
-
-Process SPAM only, then exit, leaving all non-spam messages in the
-maildir.
-
 =back
 
 =cut
 
 my $dumpfilters = 0;
-my $filteronly = 0;
 our $run = 1;
 my $printfilenames = 0;
-my $spamonly = 0;
 
 our $opt_d;
-our $opt_f;
-our $opt_h;
 our $opt_n;
 our $opt_p;
-our $opt_s;                     # ;; # stupid cperl-mode
 
-if (not getopts('dfhnps')) {
-    exit(&EX_USAGE);
+if (not getopts('dnp')) {
+    exit(2);
 }
 
 if ($opt_d) {
     $dumpfilters = 1;
-} elsif ($opt_h) {
-    print("Sorry bub, no help.\n");
-    exit(&EX_OK);
 } elsif ($opt_n) {
     $run = 0;
 }
@@ -113,12 +97,6 @@ if ($opt_p) {
     $printfilenames = 1;
 }
 
-if ($opt_f) {
-    $filteronly = 1;
-} elsif ($opt_s) {              # ))){ # stupid cperl-mode
-    $spamonly = 1;
-}
-
 =head1 ENVIRONMENT
 
 =over 4
@@ -303,35 +281,6 @@ sub log_headers {
 ###############################################################################
 # Utility procedures
 
-sub _errprint {
-    printf(STDERR '%s:%s', __FILE__, __LINE__);
-
-    if (@_) {
-        print(STDERR ': ');
-        foreach (@_) {
-            print(STDERR);
-        }
-    }
-}
-
-sub err {
-    my $ex = shift;
-
-    _errprint(@_);
-    print(STDERR ": $!\n");
-
-    exit($ex);
-}
-
-sub errx {
-    my $ex = shift;
-
-    _errprint(@_);
-    print(STDERR "\n");
-
-    exit($ex);
-}
-
 sub mkfolder {
     my $mhfolder = shift;
     my $folder;
@@ -348,8 +297,7 @@ sub mkfolder {
                 if (-d $target or mkdir($target)) {
                     next;
                 } else {
-                    err(&EX_OSERR,
-                        "Failed to create +$mhfolder ($component)");
+                    die("Failed to create +$mhfolder ($component)");
                 }
             }
         }
@@ -361,7 +309,7 @@ sub getfiles {
     my @result;
 
     if (not opendir(DIR, $dir)) {
-        err(&EX_OSERR, "Failed opendir($dir)");
+        die("Failed opendir($dir)");
     }
 
     # Initialize $! to 0 (success) because stupid stupid Perl provides
@@ -376,7 +324,7 @@ sub getfiles {
       } readdir(DIR);
 
     if ($! != 0) {
-        err(&EX_OSERR, "Failed readdir($dir)");
+        die("Failed readdir($dir)");
     }
 
     if (scalar(@result) == 0) {
@@ -397,14 +345,14 @@ sub get_highest_msgnum {
 
     $dir = "$mh/$mhfolder";
     if (not opendir(DIR, $dir)) {
-        err(&EX_OSERR, "Failed opendir($dir)");
+        die("Failed opendir($dir)");
     }
 
     $! = 0;
     @list = readdir(DIR);
 
     if ($! != 0) {
-        err(&EX_OSERR, "Failed readdir($dir)");
+        die("Failed readdir($dir)");
     }
 
     closedir(DIR);
@@ -422,12 +370,6 @@ sub get_highest_msgnum {
     return $highest;
 }
 
-# We want to print the name of each list that has new mail only once,
-# so use this hash to store the lists that have already been printed.
-# Start the list out with SPAM already in it, since we don't care when
-# new messages are added to it.
-my %FOLDERS = ('SPAM'=>1);
-
 sub store_message {
     my $msg = shift;
     my $mhfolder = shift;
@@ -481,7 +423,7 @@ sub store_message {
         # This algorithm is different from the maildir one; let's make
         # 10 tries instead of 3.
         if ($try == 9) {
-            errx(&EX_TEMPFAIL, "Attempted filename $mhmsg exists.");
+            die("Attempted filename $mhmsg exists.");
         }
 
         # This algorithm is different; i don't think we need to sleep.
@@ -494,28 +436,27 @@ sub store_message {
 
     if ($run) {
         if (not rename($msg, $mhmsg)) {
-            err(&EX_OSERR, "Failed rename($msg, $mhmsg)");
+            die("Failed rename($msg, $mhmsg)");
         }
 
         # Mark each message as soon as we store it and bomb if that
         # fails.  While it is slow, it is not safe to store multiple
         # messages and then have a failure before marking some (or
         # all).
-        if ($mhfolder ne 'SPAM') {
+        if ($mhfolder eq 'SPAM') {
+            push(@SPAM, $msgnum);
+        } else {
             $status = system('mark', "+$mhfolder", "$msgnum", '-sequence',
                              'unseen', '-add');
             if (not WIFEXITED($status)) {
-                err(&EX_OSERR, "Failed to run mark");
+                die("Failed to run mark");
             } elsif (WEXITSTATUS($status) != 0) {
-                errx(&EX_SOFTWARE, "Failed to mark message unseen.");
+                die("Failed to mark message unseen.");
             }
         }
     }
 
-    if (not $FOLDERS{$mhfolder}) {
-        print("+$mhfolder\n");
-        $FOLDERS{$mhfolder} = 1;
-    }
+    return $msgnum;
 }
 
 # Parse a message file into a structure describing the headers.  The
@@ -567,47 +508,6 @@ sub get_headers {
     return %headers;
 }
 
-###############################################################################
-# Spam handling, sorting, etc.
-
-sub preprocess {
-    my @msglist = @_;
-    my @baton;
-    my $msg;
-    my $i;
-    my $st;
-    my @result;
-
-    @baton = spam_start_hook();
-
-    foreach $msg (@msglist) {
-        printf('%sChecking for spam... %6d/%d',
-               "\r", ++$i, scalar(@msglist));
-        if ($printfilenames) {
-            print($msg);
-        }
-        if (spam_check($msg, @baton)) {
-            print(" SPAM\n");
-            store_message($msg, 'SPAM');
-        } else {
-            push(@result, $msg);
-        }
-    }
-    print("\nDone: ", scalar(@result), " survivors\n");
-
-    spam_stop_hook(@baton);
-
-    return (map { $_->[1] }
-            sort { $a->[0] <=> $b->[0] }
-            map {
-                if (not ($st = stat($_))) {
-                    die("stat($_): $!");
-                }
-                [$st->mtime, $_]
-            }
-            @result);
-}
-
 \f
 ###############################################################################
 # Filtering
@@ -676,34 +576,70 @@ sub find_mh_folder {
 
 sub filter_mail {
     my @msglist = @_;
+    my $msgcount = scalar(@msglist);
+    my @baton;
     my $msg;
     my $mhfolder;
+    my $spam = 0;
+    my $saved = 0;
+    my %FOLDERS = ('SPAM'=>1);
+
+    @baton = spam_start_hook();
+
 
+    STDOUT->autoflush(1);
+    print("$msgcount messages...");
     foreach $msg (@msglist) {
-        $mhfolder = find_mh_folder($msg);
+        if (spam_check($msg, @baton)) {
+            $mhfolder = 'SPAM';
+            $spam = scalar(@SPAM)+ 1;
+        } else {
+            $mhfolder = find_mh_folder($msg);
+            $saved++;
+        }
+
         store_message($msg, $mhfolder);
+
+        print("\r");
+        if (not $FOLDERS{$mhfolder}) {
+            print(' ' x length($msgcount));
+            print("                                \r+$mhfolder\n");
+            $FOLDERS{$mhfolder} = 1;
+        }
+
+        printf('%6d SPAM %6d saved %6d/%1d',
+               $spam, $saved, $spam + $saved, $msgcount);
     }
+    print("\n");
+
+    spam_stop_hook(@baton);
 }
 
 \f
 MAIN: {
     my @msglist;
+    my $st;
 
     if ($dumpfilters) {
         $Data::Dumper::Indent = 1;
         print(Dumper(\@FILTERS));
-        exit(&EX_OK);
+        exit;
     }
 
-    @msglist = getfiles("$MAILDIR/new");
+    @msglist = (
+                map { $_->[1] }
+                sort { $a->[0] <=> $b->[0] }
+                map {
+                    if (not ($st = stat($_))) {
+                        die("stat($_): $!");
+                    }
+                    [$st->mtime, $_]
+                }
+                getfiles("$MAILDIR/new"));
 
-    if (not $filteronly) {
-        @msglist = preprocess(@msglist);
-    }
+    filter_mail(@msglist);
 
-    if (not $spamonly) {
-        filter_mail(@msglist);
-    }
+    @SPAM and (exec('scan', '+SPAM', @SPAM) or die);
 }
 
 \f