]> diplodocus.org Git - minc/blobdiff - minc
undocumented lightly tested support for CODE ref filters
[minc] / minc
diff --git a/minc b/minc
index b219f83..224e56a 100755 (executable)
--- a/minc
+++ b/minc
@@ -1,6 +1,4 @@
-#! /usr/bin/env perl
-
-# $Id$
+#!/usr/bin/perl
 
 =head1 NAME
 
@@ -12,7 +10,7 @@ B<minc> [B<-m> I<MAX>] [B<-n>]
 
 B<minc> B<-d>
 
-B<minc> B<-h>
+B<minc> B<-r>
 
 =head1 DESCRIPTION
 
@@ -37,6 +35,8 @@ rest of the line.
 use strict;
 use warnings;
 
+$SIG{'PIPE'} = 'IGNORE';
+
 use Data::Dumper;
 use Errno;
 use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
@@ -44,7 +44,6 @@ use FileHandle;
 use File::Temp qw(tempfile);
 use File::stat;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
-use POSIX qw(strftime WEXITSTATUS WIFEXITED);
 use Pod::Usage;
 
 our $VERSION = 1;
@@ -54,9 +53,11 @@ our $VERSION = 1;
 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;
+# Mapping of message numbers to array references.  The first element is set by
+# filter_mail to a reference to a header hash for the message; the second is
+# set by maildier_spam to the name of the message file in the spam maildir.
+# scan_spam scans this at the end so the user can check for false positives.
+my %SPAM;
 
 =head1 OPTIONS
 
@@ -80,6 +81,10 @@ Stop processing after I<MAX> messages.
 Dry run; do not actually incorporate the mail, but log and report to
 stdout/stderr as normal.
 
+=item B<-r>
+
+Rebuild `mhpath +`/.folders from scratch, processing no mail.
+
 =back
 
 =cut
@@ -88,15 +93,19 @@ my $dumpfilters;
 my $help;
 my $maxmsgs;
 my $norun;
+my $rebuild_dot_folders;
 
+if (!caller()) {
 GetOptions(
            'd' => \$dumpfilters,
            'h|help' => \$help,
            'm=i' => \$maxmsgs,
            'n' => \$norun,
+           'r' => \$rebuild_dot_folders,
           ) or pod2usage();
 $help and pod2usage(-exitstatus=>0, -verbose=>1);
 @ARGV == 0 or pod2usage();
+}
 
 our $run = !$norun;
 
@@ -148,14 +157,6 @@ may define the FILTERS list, @start_hooks, @filter_hooks,
 B<minc> adds all folders it filters into to this file, which is used
 by lukem's B<new(1)> (XXX need a link).
 
-=item `mhpath +`/logs/minc.log
-
-Where B<minc> logs what it does, unless in -n mode.
-
-=item `mhpath +`/logs/dryrun.log
-
-Where B<minc> logs what it would do; used in -n mode.
-
 =item `mhpath +`/.minc.context
 
 B<minc> uses this file for context (i.e. current folder) instead of
@@ -176,69 +177,40 @@ our (@start_hooks, @stop_hooks, @filter_hooks, @post_store_hooks);
 our $SCAN_P_FOLDER = 0.1;
 our $SCAN_P_MESSAGE = 0.0625;
 our $SCAN_P_FROM = 0.175;
+our @folder_sort_list = (qr/^inbox$/);
+our $folder_sorter = sub { sort_by_list(@_, @folder_sort_list) };
 
 my $mh;
-my $logfile;
 
 $mh = `mhpath +`;
 chomp($mh);
 
-if ($run) {
-    $logfile = $mh . '/logs/minc.log';
-} else {
-    $logfile = $mh . '/logs/dryrun.log';
-}
-
 $ENV{"MHCONTEXT"} = $mh . '/.minc.context';
 
 \f
 ###############################################################################
-# Logging
-
-sub mylog {
-    my $timestamp = strftime('%b %e %H:%M:%S', localtime);
-    my $msg;
-    foreach my $part (@_) {
-        if (defined($part)) {
-            $msg .= $part;
-        }
-    }
-    # no newlines in the log message, thanks
-    $msg =~ s/\n/ /gm;
-
-    open(LOG, ">>$logfile") or die("open(>>$logfile): $!");
-    print(LOG "$timestamp $msg\n") or die("print(>>$logfile): $!");
-    close(LOG) or die("close($logfile): $!");
-}
+# Utility procedures
 
-sub logheader {
-    my ($text, @contents) = @_;
-    my $last;
+sub sort_by_list {
+    my $a = shift;
+    my $b = shift;
 
-    if (@contents) {
-        $last = $contents[-1];
-    } else {
-        $last = '';
+    for my $i (@_) {
+        my $am = $a =~ $i;
+        my $bm = $b =~ $i;
+        if ($am) {
+            if ($bm) {
+                last;
+            }
+            return -1;
+        } elsif ($bm) {
+            return 1;
+        }
     }
 
-    mylog('<< ', $text, $last);
+    return $a cmp $b;
 }
 
-sub log_headers {
-    my %headers = @_;
-
-    # For an explanation of the %headers structure, see the
-    # get_headers function below.
-    logheader('From:       ', @{$headers{'return-path'}});
-    logheader('To:         ', @{$headers{'to'}});
-    logheader('Subject:    ', @{$headers{'subject'}});
-    logheader('Message-Id: ', @{$headers{'message-id'}});
-}
-
-\f
-###############################################################################
-# Utility procedures
-
 sub mkfolder {
     my $folder = shift;
     my $target;
@@ -271,17 +243,22 @@ sub getfiles {
     }
 
     if (@result <= 2) {
-        exit(0);
+        return ();
     }
 
     STDOUT->autoflush(1);
-    print(@result - 2, " messages...");
+    if (@result == 3) {
+        print('1 message...');
+    } else {
+        print(@result - 2, ' messages...');
+    }
 
     closedir(DIR);
 
     return @result;
 }
 
+my %msgnum_cache;
 sub get_highest_msgnum {
     my $mhfolder = shift;
     my $dir;
@@ -289,6 +266,10 @@ sub get_highest_msgnum {
     my $highest;
     my $msgnum;
 
+    if (defined($msgnum_cache{$mhfolder})) {
+        return $msgnum_cache{$mhfolder}++;
+    }
+
     $dir = "$mh/$mhfolder";
     if (not opendir(DIR, $dir)) {
         die("opendir($dir): $!");
@@ -314,7 +295,71 @@ sub get_highest_msgnum {
         }
     }
 
-    return $highest;
+    $msgnum_cache{$mhfolder} = $highest;
+    return $msgnum_cache{$mhfolder}++;
+}
+
+sub mark {
+    my $folder = shift;
+    my $msgnum = shift;
+    my $seq = shift;
+    my $fn = "$mh/$folder/.mh_sequences";
+    my $fh;
+    my @sequences;
+
+    if (not open($fh, $fn)) {
+        $!{ENOENT} or die("open($fn): $!");
+    } else {
+        @sequences = <$fh>;
+        chomp(@sequences);
+    }
+
+    my $marked = 0;
+    open($fh, '>', $fn) or die("open(>$fn): $!");
+    for $_ (@sequences) {
+        if (/^$seq: (.*)/) {
+            my @parts;
+            my @result;
+            my $done = 0;
+            for my $part (split(' ', $1)) {
+                if (not $done) {
+                    my ($st, $en) = split('-', $part);
+                    if ((defined($en) and ($msgnum >= $st and $msgnum <= $en))
+                        or $msgnum == $st) {
+                        # It's already there.
+                        $done = 1;
+                    }
+                    if (defined($en)) {
+                        if ($st - 1 == $msgnum) {
+                            $part = "$msgnum-$en";
+                            $done = 1;
+                        } elsif ($en + 1 == $msgnum) {
+                            $part = "$st-$msgnum";
+                            $done = 1;
+                        }
+                    } else {
+                        if ($part - 1 == $msgnum) {
+                            $part = "$msgnum-$part";
+                            $done = 1;
+                        } elsif ($part + 1 == $msgnum) {
+                            $part = "$part-$msgnum";
+                            $done = 1;
+                        }
+                    }
+                }
+                push(@result, $part);
+            }
+            if (not $done) {
+                push(@result, $msgnum);
+            }
+            print($fh "$seq: ", join(' ', @result), "\n");
+            $marked = 1;
+        } else {
+            print($fh "$_\n");
+        }
+    }
+    $marked or print($fh "$seq: $msgnum\n");
+    close($fh) or die("close(>$fn): $!");
 }
 
 sub store_message {
@@ -323,7 +368,6 @@ sub store_message {
     my $msgnum;
     my $try;
     my $mhmsg;
-    my $status;
 
     # We must do this even in -n mode because later steps fail without
     # it.  This should be harmless.
@@ -377,8 +421,6 @@ sub store_message {
         #sleep(2);
     }
 
-    mylog('+', $mhfolder);
-
     if ($run) {
         if (not rename($msg, $mhmsg)) {
             die("rename($msg, $mhmsg): $!");
@@ -388,18 +430,8 @@ sub store_message {
         # 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 eq 'SPAM') {
-            push(@SPAM, $msgnum);
-        } 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) {
-                die("Failed to mark message unseen.");
-            }
+        if ($mhfolder ne 'SPAM') {
+            mark($mhfolder, $msgnum, 'unseen');
         }
     }
 
@@ -452,7 +484,7 @@ sub get_headers {
     }
     close(MSG);
 
-    return %headers;
+    return \%headers;
 }
 
 \f
@@ -461,64 +493,88 @@ sub get_headers {
 
 sub find_mh_folder {
     my $msg = shift;
-    my %headers = @_;
-    my $filterref;
-    my @filter;
-    my $header;
-    my $contents;
-    my $pair;
-    my $match;
-    my $expression;
-    my $result;
-
-    if (not %headers) {
+    my $header = shift;
+
+    if (not %$header) {
         return 'malformed';
     }
 
     # Walk the list of filters.  This structure is documented in
     # pod at the end of the program.
-    foreach $filterref (@FILTERS) {
-        @filter = @$filterref;
-        $header = shift(@filter);
-
-        # Handle filters using the magic TO header specially.
-        if ($header eq $MAGIC_TO_TOKEN) {
-            foreach $header (keys(%headers)) {
-                if ($header =~ /$MAGIC_TO_REGEX/i) {
-                    foreach $contents (@{$headers{lc($header)}}) {
-                        foreach $pair (@filter) {
-                            ($match, $expression) = @$pair;
-                            if ($contents =~ /$match/) {
-                                return $expression;
-                            }
-                        }
-                    }
-                }
-            }
-
-            # Now that it's been processed specially, skip normal handling.
+    for my $filterref (@FILTERS) {
+        if (ref($filterref) eq 'CODE') {
+            my $m = $filterref->($header, $msg);
+            $m && return $m;
             next;
         }
 
-        # Walk the list of message headers matching the filter's
-        # specified header.
-        foreach $contents (@{$headers{lc($header)}}) {
-            # Walk the filter's list of match/expression pairs.
-            foreach $pair (@filter) {
-                ($match, $expression) = @$pair;
-                if ($contents =~ /$match/i) {
-                    if (eval "\$result = \"$expression\"") {
-                        return $result;
-                    }
+        my $m = match($header, @$filterref);
+        $m && return $m;
+    }
+
+    return 'inbox';
+}
+
+# Test all the header fields against each [regexp, folder-expression] pair.
+sub match {
+    my $header = shift;
+    my $filter_field = shift;
+    my @filters = @_;
+
+    # Handle filters using the magic TO header specially.
+    if ($filter_field eq $MAGIC_TO_TOKEN) {
+        for my $field_name (keys(%$header)) {
+            if ($field_name =~ /$MAGIC_TO_REGEX/i) {
+                my $m = match_one_field($header->{$field_name}, \@filters);
+                $m && return $m;
+            }
+        }
+        # Now that it's been processed specially, skip normal handling.
+        return;
+    }
+
+    # Walk the list of header fields matching the filter's specified header.
+    my $m = match_one_field($header->{lc($filter_field)}, \@filters);
+    $m && return $m;
+}
+
+# Test all the values of one header field against each [regexp,
+# folder-expression] pair.
+sub match_one_field {
+    my $values = shift;
+    my $filters = shift;
+    for my $value (@$values) {
+        for my $pair (@$filters) {
+            my ($regexp, $expression) = @$pair;
+            if ($value =~ $regexp) {
+                my $result;
+                if (eval "\$result = \"$expression\"") {
+                    return $result;
                 }
             }
         }
     }
+}
 
-    return 'inbox';
+sub scan_line {
+    my ($headers, $mhfolder, $msgnum, $nf, $nm, $nF, $ns) = @_;
+    my $from = '';
+    my $subject = '';
+    # Sometimes these headers are missing...
+    eval { $from = [@{$headers->{'from'}}]->[-1] };
+    eval { $subject = [@{$headers->{'subject'}}]->[-1] };
+    # Replace garbage characters.
+    for ($from, $subject) {
+        tr/\x00-\x1f\x80-\xff/?/;
+    }
+    return sprintf("\%-${nf}s \%${nm}d \%-${nF}s \%s",
+                   substr($mhfolder, 0, $nf), substr($msgnum, 0, $nm),
+                   substr($from, 0, $nF),
+                   substr($subject, 0, $ns));
 }
 
 sub filter_mail {
+    @_ or return ();
     my $msgcount = @_ - 2; # don't count . and ..
     my $len = length($msgcount);
     my @baton;
@@ -547,49 +603,49 @@ sub filter_mail {
         }
     }
 
+    my $tty = -t STDOUT;
+    if (not $tty) {
+        # Print a newline after the incomplete "N messages..." line.
+        print("\n");
+    }
+
     for $msg (@_) {
         ($msg eq '.' or $msg eq '..') and next;
 
-        my %headers = get_headers($msg);
-        log_headers(%headers);
+        my $headers = get_headers($msg);
 
         undef($mhfolder);
         for my $hook (@filter_hooks) {
-            my $result = $hook->(\%batons, \%headers, $msg);
+            my $result = $hook->(\%batons, $headers, $msg);
             defined($result) and ($mhfolder = $result);
         }
 
-        defined($mhfolder) or ($mhfolder = find_mh_folder($msg, %headers));
+        defined($mhfolder) or ($mhfolder = find_mh_folder($msg, $headers));
 
         $msgnum = store_message($msg, $mhfolder);
         $folders{$mhfolder}++;
 
+        if ($tty) {
+            print("\r");
+        }
+
         if ($mhfolder eq 'SPAM') {
             $spam++;
+            $SPAM{$msgnum} = [$headers, undef];
         } else {
             $saved++;
-            my $from = [@{$headers{'from'}}]->[-1];
-            my $subject = [@{$headers{'subject'}}]->[-1];
-            for ($from, $subject) {
-                tr/\x00-\x1f\x80-\xff/ /;
-            }
-            print("\r");
-            print(' ' x $COLUMNS);
-            printf("\r\%-${nf}s \%${nm}d \%-${nF}s \%s\n",
-                   substr($mhfolder, 0, $nf), substr($msgnum, 0, $nm),
-                   # XXX shouldn't pop, as these are about to be
-                   # passed to post_store_hooks
-                   substr($from, 0, $nF),
-                   substr($subject, 0, $ns));
+            print(scan_line($headers, $mhfolder, $msgnum, $nf, $nm, $nF, $ns),
+                  "\n");
         }
 
         for my $hook (@post_store_hooks) {
-            $hook->(\%batons, \%headers, $mhfolder, $msgnum);
+            $hook->(\%batons, $headers, $mhfolder, $msgnum);
         }
 
-        print("\r");
-        printf(" \%${len}d SPAM \%${len}d saved \%${len}d/%1d",
-               $spam, $saved, $spam + $saved, $msgcount);
+        if ($tty) {
+            printf(" \%${len}d SPAM \%${len}d saved \%${len}d/%1d",
+                   $spam, $saved, $spam + $saved, $msgcount);
+        }
 
         defined($maxmsgs) and ($spam + $saved < $maxmsgs or last);
     }
@@ -602,18 +658,16 @@ sub filter_mail {
     return %folders;
 }
 
-sub update_folders {
+sub build_dot_folders {
     my $folders = shift;
-    my ($fh, $fn);
+    my $fh = shift;
+    my $fn;
 
-    if (not open($fh, "$mh/.folders")) {
-        warn("open($mh/.folders): $!");
-        return;
-    }
-
-    while (<$fh>) {
-        chomp;
-        $folders->{$_}++;
+    if (defined($fh)) {
+        while (<$fh>) {
+            chomp;
+            $folders->{$_}++;
+        }
     }
 
     eval { ($fh, $fn) = tempfile("$mh/.folders.XXXXX") };
@@ -622,9 +676,7 @@ sub update_folders {
         return;
     }
 
-    print($fh "inbox\n");
-    for my $folder (sort(keys(%$folders))) {
-        $folder eq 'inbox' and next;
+    for my $folder (sort { $folder_sorter->($a,$b) } keys(%$folders)) {
         print($fh "$folder\n");
     }
 
@@ -637,8 +689,106 @@ sub update_folders {
     rename($fn, "$mh/.folders") or warn("rename($fn, $mh/.folders): $!");
 }
 
+sub create_dot_folders {
+    if (-f "$HOME/.minc") {
+        require "$HOME/.minc";
+    }
+
+    my %folders;
+    open(my $fh, '-|', 'folders', '-fast', '-recur')
+      or die("open(folders|): $!");
+    build_dot_folders(\%folders, $fh);
+    return 0;
+}
+
+sub update_dot_folders {
+    my $folders = shift;
+    my $fh;
+
+    if (not open($fh, "$mh/.folders") and not $!{ENOENT}) {
+        # For ENOENT, we go ahead and create it, else we error and
+        # don't clobber it.
+        warn("open($mh/.folders): $!");
+        return;
+    }
+
+    build_dot_folders($folders, $fh);
+}
+
+# XXX Could use some unification with getfiles...
+sub maildir_spam {
+    my $dir = "$MAILDIR/spam/new";
+
+    if (not chdir($dir)) {
+        $!{ENOENT} or print(STDERR "skipping maildir spam: chdir($dir): $!\n");
+        return;
+    }
+
+    if (not opendir(DIR, '.')) {
+        print(STDERR "skipping maildir spam: opendir($dir): $!\n");
+        return;
+    }
+
+    $! = 0;
+    my @spams = readdir(DIR);
+    if ($! != 0) {
+        print(STDERR "skipping maildir spam: readdir($dir): $!\n");
+        return;
+    }
+
+    closedir(DIR);
+
+    for my $msg (@spams) {
+        ($msg eq '.' or $msg eq '..') and next;
+        my $msgnum = store_message($msg, 'SPAM');
+        # Store the original file name for scan_spam in -n mode.
+        $SPAM{$msgnum} = [undef, $msg];
+    }
+}
+
+sub scan_spam {
+    my ($msgnum, $header, $tuple, $msg);
+
+    # Unlike filter_mail, we don't need to print the folder name.
+    # Calculate how many columns would be allocated to it...
+    my $nf = int($COLUMNS * $SCAN_P_FOLDER);
+    # ...and add that amount to COLUMNS to calculate the number of columns to
+    # allocate to msgnum and from snippet, thus filling the line without
+    # printing the folder name.
+    my $nm = int(($COLUMNS + $nf) * $SCAN_P_MESSAGE);
+    my $nF = int(($COLUMNS + $nf) * $SCAN_P_FROM);
+    my $ns = $COLUMNS - $nm - $nF - 3;
+
+    for $msgnum (sort(keys(%SPAM))) {
+        $tuple = $SPAM{$msgnum};
+        if (defined($tuple->[0])) {
+            # Filed by filter_mail, so we have the header.
+            $header = $tuple->[0];
+        } elsif (defined($tuple->[1])) {
+            # Filed by maildir_spam, so we don't have the header.
+            if ($run) {
+                # The message has been filed, load it from $mh.
+                $msg = "$mh/SPAM/$msgnum";
+            } else {
+                # The message has not been filed, load it from the maildir.
+                # $tuple->[1] is just a basename, not a path; this works
+                # because maildir_spam did chdir(Maildir/spam/new).
+                $msg = $tuple->[1];
+            }
+            $header = get_headers($msg);
+        } else {
+            print(STDERR
+                  "BUG: corrupt SPAM tuple, neither element defined",
+                  " for message $msgnum\n");
+            next;
+        }
+        print(scan_line($header, '', $msgnum, 0, $nm, $nF, $ns),
+              "\n");
+    }
+}
+
 \f
-MAIN: {
+if (!caller()) {
     my $st;
 
     if ($dumpfilters) {
@@ -648,6 +798,8 @@ MAIN: {
         exit;
     }
 
+    $rebuild_dot_folders and exit(create_dot_folders);
+
     chdir("$MAILDIR/new") or die("chdir($MAILDIR/new): $!");
     my %folders = filter_mail(map { $_->[1] }
                 sort { $a->[0] <=> $b->[0] }
@@ -659,11 +811,14 @@ MAIN: {
                 }
                 getfiles('.'));
 
-    update_folders(\%folders);
+    $run and %folders and update_dot_folders(\%folders);
 
-    @SPAM and (exec('scan', '+SPAM', @SPAM) or die);
+    maildir_spam();
+    scan_spam();
 }
 
+1;
+
 \f
 __END__