]> diplodocus.org Git - minc/blobdiff - minc
Collapse the spam-checking and filtering passes into a single pass and
[minc] / minc
diff --git a/minc b/minc
index 87a7813..8d8b6a3 100755 (executable)
--- a/minc
+++ b/minc
@@ -8,15 +8,14 @@ B<minc> - Incorporate mail from a maildir into mh folders.
 
 =head1 SYNOPSIS
 
-B<minc> [-B<dhns>]
+B<minc> [-B<dfhnps>]
 
 =head1 DESCRIPTION
 
-B<minc> is a program for incorporating mail from a maildir to a mh
-folder hierarchy.  It takes mail from a maildir folder (not a maildir
-folder hierarchy), optionally checks for spam with a user-defined
-spam-checking function, and optionally filters mail into separate mh
-folders.
+B<minc> incorporates mail from a maildir to a mh folder hierarchy.  It
+takes mail from a maildir folder (not a maildir folder hierarchy),
+optionally checks for spam with a user-defined spam-checking function,
+and optionally filters mail into separate mh folders.
 
 The filtering is quite sophisticated, as it is done using real Perl
 matching (m//) commands.
@@ -26,8 +25,6 @@ matching (m//) commands.
 use strict;
 use warnings;
 
-require 'sysexits.ph';
-
 use Data::Dumper;
 use Errno;
 use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
@@ -98,14 +95,14 @@ our $opt_p;
 our $opt_s;                     # ;; # stupid cperl-mode
 
 if (not getopts('dfhnps')) {
-    exit(&EX_USAGE);
+    exit(2);
 }
 
 if ($opt_d) {
     $dumpfilters = 1;
 } elsif ($opt_h) {
     print("Sorry bub, no help.\n");
-    exit(&EX_OK);
+    exit;
 } elsif ($opt_n) {
     $run = 0;
 }
@@ -304,35 +301,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;
@@ -349,8 +317,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)");
                 }
             }
         }
@@ -362,7 +329,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
@@ -377,7 +344,7 @@ sub getfiles {
       } readdir(DIR);
 
     if ($! != 0) {
-        err(&EX_OSERR, "Failed readdir($dir)");
+        die("Failed readdir($dir)");
     }
 
     if (scalar(@result) == 0) {
@@ -398,14 +365,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);
@@ -423,12 +390,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;
@@ -466,7 +427,7 @@ sub store_message {
                 # up duplicated.  The advantage of creating an empty
                 # file followed by rename(2) is that an extra empty
                 # file is left behind as opposed to a duplicate
-                # message.  This is more easiliy detected by the user.
+                # message.  This is more easily detected by the user.
                 if ($run) {
                     if (sysopen(MSG, "$mhmsg",
                                 O_WRONLY | O_EXCL | O_CREAT, 0600)) {
@@ -482,7 +443,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.
@@ -495,7 +456,7 @@ 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
@@ -506,17 +467,12 @@ sub store_message {
             $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;
-    }
 }
 
 # Parse a message file into a structure describing the headers.  The
@@ -568,47 +524,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
@@ -677,34 +592,68 @@ 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();
 
+    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