]> diplodocus.org Git - minc/blobdiff - minc
Move the declaration of the hooks lists near the @FILTERS declaration.
[minc] / minc
diff --git a/minc b/minc
index 387651466c41039b653e3e4120af69861cbdd3b1..6837e19b7e7bd0065200c0d965b56759129205e8 100755 (executable)
--- a/minc
+++ b/minc
@@ -1,4 +1,4 @@
-#! /usr/local/bin/perl
+#! /usr/bin/env perl
 
 # $Id$
 
@@ -8,9 +8,11 @@ B<minc> - incorporate mail from a maildir into mh folders
 
 =head1 SYNOPSIS
 
+B<minc> [B<-m> I<MAX>] [B<-n>] [B<-p>]
+
 B<minc> B<-d>
 
-B<minc> [B<-m>] [B<-n>] [B<-p>]
+B<minc> B<-h>
 
 =head1 DESCRIPTION
 
@@ -56,7 +58,7 @@ my @SPAM;
 Dump (using Data::Dumper) the FILTERS list and exit.  This is useful
 for testing the syntax of .mincfilter.
 
-=item B<--help>
+=item B<-h>
 
 Show help.
 
@@ -93,7 +95,7 @@ GetOptions(
            'p' => \$printfilenames,
           ) or pod2usage();
 $help and pod2usage(-exitstatus=>0, -verbose=>1);
-scalar(@ARGV) == 0 or pod2usage();
+@ARGV == 0 or pod2usage();
 
 our $run = !$norun;
 
@@ -114,7 +116,9 @@ Where mail is delivered.
 
 =cut
 
-use Env qw(HOME MAILDIR);
+use Env qw(COLUMNS HOME MAILDIR);
+
+$COLUMNS ||= 80;
 
 if (not $HOME) {
     die("HOME environment variable must be set.\n");
@@ -179,7 +183,7 @@ marked unseen.
 =cut
 
 our @FILTERS;
-require "$HOME/.mincfilter";
+our (@start_hooks, @stop_hooks, @filter_hooks, @post_store_hooks);
 
 my $mh;
 my $logfile;
@@ -244,25 +248,14 @@ sub log_headers {
 # Utility procedures
 
 sub mkfolder {
-    my $mhfolder = shift;
-    my $folder;
+    my $folder = shift;
     my $target;
     my $component;
 
-    $folder = $mh . '/' . $mhfolder;
-    $target = '';
-
-    if (not -d $folder) {
-        foreach $component (split('/', $folder)) {
-            if (defined($component) and length($component) >= 1) {
-                $target = $target . '/' . $component;
-                if (-d $target or mkdir($target)) {
-                    next;
-                } else {
-                    die("Failed to create +$mhfolder ($component)");
-                }
-            }
-        }
+    $target = $mh;
+    foreach $component (split('/', $folder)) {
+        $target = join('/', $target, $component);
+        -d $target or mkdir($target) or die("mkdir($target): $!");
     }
 }
 
@@ -271,7 +264,7 @@ sub getfiles {
     my @result;
 
     if (not opendir(DIR, $dir)) {
-        die("Failed opendir($dir)");
+        die("opendir($dir): $!");
     }
 
     # Initialize $! to 0 (success) because stupid stupid Perl provides
@@ -280,21 +273,17 @@ sub getfiles {
     # to detect an error.  Real Programmers don't handle errors,
     # right?  >sigh<
     $! = 0;
-    @result = grep {
-        ($_ ne '.' and $_ ne '..')
-          and $_ = "$MAILDIR/new/$_"
-      } readdir(DIR);
-
+    @result = readdir(DIR);
     if ($! != 0) {
-        die("Failed readdir($dir)");
+        die("readdir($dir): $!");
     }
 
-    if (scalar(@result) == 0) {
+    if (@result <= 2) {
         exit(0);
     }
 
     STDOUT->autoflush(1);
-    print(scalar(@result), " messages...");
+    print(@result - 2, " messages...");
 
     closedir(DIR);
 
@@ -310,14 +299,15 @@ sub get_highest_msgnum {
 
     $dir = "$mh/$mhfolder";
     if (not opendir(DIR, $dir)) {
-        die("Failed opendir($dir)");
+        die("opendir($dir): $!");
     }
 
+    # Insert rant from getfiles here.
     $! = 0;
     @list = readdir(DIR);
 
     if ($! != 0) {
-        die("Failed readdir($dir)");
+        die("readdir($dir): $!");
     }
 
     closedir(DIR);
@@ -374,7 +364,7 @@ sub store_message {
                 # file is left behind as opposed to a duplicate
                 # message.  This is more easily detected by the user.
                 if ($run) {
-                    if (sysopen(MSG, "$mhmsg",
+                    if (sysopen(MSG, $mhmsg,
                                 O_WRONLY | O_EXCL | O_CREAT, 0600)) {
                         close(MSG);
                         last;
@@ -401,7 +391,7 @@ sub store_message {
 
     if ($run) {
         if (not rename($msg, $mhmsg)) {
-            die("Failed rename($msg, $mhmsg)");
+            die("rename($msg, $mhmsg): $!");
         }
 
         # Mark each message as soon as we store it and bomb if that
@@ -413,6 +403,8 @@ sub store_message {
         } else {
             $status = system('mark', "+$mhfolder", "$msgnum", '-sequence',
                              'unseen', '-add');
+            # XXX need to handle signalled and stopped, and print
+            # the exit code or signal number.
             if (not WIFEXITED($status)) {
                 die("Failed to run mark");
             } elsif (WEXITSTATUS($status) != 0) {
@@ -438,7 +430,7 @@ sub get_headers {
     my $fieldname;              # unmolested header name
     my $contents;               # contents of header
 
-    open(MSG, $msg);
+    open(MSG, $msg) or die("open(MSG, $msg): $!");
     while (<MSG>) {
         chomp;
         if (length == 0) {
@@ -449,8 +441,8 @@ sub get_headers {
             # folded header continuation
 
             if (not defined($current)) {
-                print(STDERR "Malformed message, cannot parse headers.\n");
-                return ();
+                warn('Malformed message, cannot parse headers.');
+                next;
             }
 
             @{$headers{$current}}[-1] .= $_;
@@ -479,7 +471,7 @@ sub get_headers {
 
 sub find_mh_folder {
     my $msg = shift;
-    my %headers;
+    my %headers = @_;
     my $filterref;
     my @filter;
     my $header;
@@ -489,13 +481,10 @@ sub find_mh_folder {
     my $expression;
     my $result;
 
-    %headers = get_headers($msg);
     if (not %headers) {
         return 'malformed';
     }
 
-    log_headers(%headers);
-
     # Walk the list of filters.  This structure is documented in
     # pod at the end of the program.
     foreach $filterref (@FILTERS) {
@@ -541,66 +530,95 @@ sub find_mh_folder {
 
 sub filter_mail {
     my @msglist = @_;
-    my $msgcount = scalar(@msglist);
-    my $msgcountlen = length($msgcount);
+    my $msgcount = @msglist - 2; # don't count . and ..
+    my $len = length($msgcount);
     my @baton;
     my $msg;
     my $mhfolder;
     my $spam = 0;
     my $saved = 0;
+    my $msgnum;
     my %FOLDERS = ('SPAM'=>1);
 
-    if (-f "$HOME/.mincspam") {
-        require "$HOME/.mincspam";
-    } else {
-        eval "sub spam_start_hook { return (); }";
-        eval "sub spam_stop_hook { }";
-        eval "sub spam_check { return 0; }";
+    # XXX lame names and hard-coded proportions.
+    my $nf = $COLUMNS * 0.1;
+    my $nm = $COLUMNS * 0.0625;
+    my $nF = $COLUMNS * 0.175;
+    my $ns = $COLUMNS - $nf - $nm - $nF - 3;
+
+    if (-f "$HOME/.minc") {
+        require "$HOME/.minc";
     }
 
-    @baton = spam_start_hook();
+    my %batons;
+    for my $hook (@start_hooks) {
+        my ($handle, @baton) = $hook->();
+        if (defined($handle)) {
+            $batons{$handle} = [@baton];
+        }
+    }
 
     foreach $msg (@msglist) {
-        if (spam_check($msg, @baton)) {
-            $mhfolder = 'SPAM';
-            $spam = scalar(@SPAM)+ 1;
+        ($msg eq '.' or $msg eq '..') and next;
+
+        if ($printfilenames) {
+            print("$msg\n");
+        }
+
+        my %headers = get_headers($msg);
+        log_headers(%headers);
+
+        undef($mhfolder);
+        for my $hook (@filter_hooks) {
+            my $result = $hook->(\%batons, \%headers, $msg);
+            defined($result) and ($mhfolder = $result);
+        }
+
+        defined($mhfolder) or ($mhfolder = find_mh_folder($msg, %headers));
+
+        $msgnum = store_message($msg, $mhfolder);
+
+        if ($mhfolder eq 'SPAM') {
+            $spam++;
         } else {
-            $mhfolder = find_mh_folder($msg);
             $saved++;
+            print("\r");
+            print(' ' x $COLUMNS);
+            printf("\r\%-${nf}s \%${nm}d \%-${nF}s \%s\n",
+                   substr($mhfolder, 0, $nf), substr($msgnum, 0, $nm),
+                   substr(pop(@{$headers{'from'}}), 0, $nF),
+                   substr(pop(@{$headers{'subject'}}), 0, $ns))
         }
 
-        store_message($msg, $mhfolder);
-
-        print("\r");
-        if (not $FOLDERS{$mhfolder}) {
-            print(' ' x $msgcountlen);
-            print("                                \r+$mhfolder\n");
-            $FOLDERS{$mhfolder} = 1;
+        for my $hook (@post_store_hooks) {
+            $hook->(\%batons, \%headers, $mhfolder, $msgnum);
         }
 
-        printf('%6d SPAM %6d saved %6d/%1d',
+        printf(" \%${len}d SPAM \%${len}d saved \%${len}d/%1d",
                $spam, $saved, $spam + $saved, $msgcount);
 
         defined($maxmsgs) and ($spam + $saved < $maxmsgs or last);
     }
     print("\n");
 
-    spam_stop_hook(@baton);
+    for my $hook (@stop_hooks) {
+        $hook->(\%batons);
+    }
 }
 
 \f
 MAIN: {
-    my @msglist;
     my $st;
 
     if ($dumpfilters) {
+        require "$HOME/.minc";
         $Data::Dumper::Indent = 1;
         print(Dumper(\@FILTERS));
         exit;
     }
 
-    @msglist = (
-                map { $_->[1] }
+    chdir("$MAILDIR/new") or die("chdir($MAILDIR/new): $!");
+    filter_mail(map { $_->[1] }
                 sort { $a->[0] <=> $b->[0] }
                 map {
                     if (not ($st = stat($_))) {
@@ -608,9 +626,7 @@ MAIN: {
                     }
                     [$st->mtime, $_]
                 }
-                getfiles("$MAILDIR/new"));
-
-    filter_mail(@msglist);
+                getfiles('.'));
 
     @SPAM and (exec('scan', '+SPAM', @SPAM) or die);
 }
@@ -686,7 +702,7 @@ list such as dev@httpd.apache.org, this filter will create the folder
 name l/apache/httpd/dev.
 
 For an example B<spam_check> function, see
-L<http:E<047>E<047>pretzelnet.orgE<047>cvsE<047>dotfilesE<047>.mincspam>
+L<http://pretzelnet.org/cvs/dotfiles/.mincspam>
 
 =head1 AUTHORS