]> diplodocus.org Git - minc/blobdiff - minc
New option -p. Using this option causes minc to print the filename
[minc] / minc
diff --git a/minc b/minc
index a143457..b74ecd4 100755 (executable)
--- a/minc
+++ b/minc
@@ -31,12 +31,21 @@ require 'sysexits.ph';
 use Data::Dumper;
 use Errno;
 use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
+use FileHandle;
 use File::Basename;
 use Getopt::Std;
 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);
+
+# 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';
+
 =head1 OPTIONS
 
 =over 4
@@ -59,6 +68,12 @@ Show help.
 Dry run; do not actually incorporate the mail, but log and report to
 stdout/stderr as normal.
 
+=item B<-p>
+
+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
@@ -71,15 +86,17 @@ maildir.
 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('dfhns')) {
+if (not getopts('dfhnps')) {
     exit(&EX_USAGE);
 }
 
@@ -92,6 +109,10 @@ if ($opt_d) {
     $run = 0;
 }
 
+if ($opt_p) {
+    $printfilenames = 1;
+}
+
 if ($opt_f) {
     $filteronly = 1;
 } elsif ($opt_s) {              # ))){ # stupid cperl-mode
@@ -135,16 +156,23 @@ expected to define the FILTERS list.
 
 =item $HOME/.mincspam
 
-If this file exists, B<minc> will include it with the expectation that it
-will define a B<is_spam> function.  This function takes a message
-filename as an argument and returns 1 if the message is spam, else 0.
-If this file does not exist, B<minc> will define a simple function that
-always returns 0.
+If this file exists, B<minc> will include it with the expectation that
+it will define a B<spam_check> function.  This function takes a
+message filename as an argument and returns 1 if the message is spam,
+else 0.  If this file does not exist, B<minc> will define a simple
+function that always returns 0.
 
 One of B<minc>'s global variables is available to the user-defined
-is_spam function: $run.  This boolean should be honored; is_spam
-should only take real action (i.e. removing or creating files, running
-external programs, etc.) if $run is non-zero.
+B<spam_check> function: $run.  This boolean should be honored;
+B<spam_check> should only take real action (i.e. removing or creating
+files, running external programs, etc.) if $run is non-zero.
+
+This file may also declare two other functions: B<spam_start_hook> and
+B<spam_stop_hook>.  The former is passed no arguments and is expected
+to return a list.  This list is a "baton" that will also be passed to
+B<spam_check> and B<spam_stop_hook>.  It can hold anything
+B<spam_check> will need to do its job, whether network sockets, pipes,
+or whatever.
 
 XXX: need more details about the spam-handling process; for now read
 the code.
@@ -173,14 +201,14 @@ marked unseen.
 =cut
 
 our @FILTERS;
-require "$HOME/.mincfilter2";
+require "$HOME/.mincfilter";
 
 if (-f "$HOME/.mincspam") {
     require "$HOME/.mincspam";
 } else {
-    sub is_spam {
-        return 0;
-    }
+    eval "sub spam_start_hook { return (); }";
+    eval "sub spam_stop_hook { }";
+    eval "sub spam_check { return 0; }";
 }
 
 my $mh;
@@ -241,22 +269,34 @@ sub mylog {
     }
 }
 
-sub logincoming { mylog('info', INCOMINGCHAR, @_); }
 sub logsave { mylog('notice', SAVECHAR, @_); }
 sub loginfo { mylog('info', INFOCHAR, @_); }
 sub logdebug { mylog('debug', DEBUGCHAR, @_); }
 sub logwarn { mylog('warning', WARNCHAR, @_); }
 sub logerr { mylog('error', ERRORCHAR, @_); }
 
+sub logincoming {
+    my ($text, @contents) = @_;
+    my $last;
+
+    if (@contents) {
+        $last = $contents[-1];
+    } else {
+        $last = '';
+    }
+
+    mylog('info', INCOMINGCHAR, $text, $last);
+}
+
 sub log_headers {
     my %headers = @_;
 
     # For an explanation of the %headers structure, see the
     # get_headers function below.
-    logincoming('From:       ', @{$headers{'return-path'}}[-1]);
-    logincoming('To:         ', @{$headers{'to'}}[-1]);
-    logincoming('Subject:    ', @{$headers{'subject'}}[-1]);
-    logincoming('Message-Id: ', @{$headers{'message-id'}}[-1]);
+    logincoming('From:       ', @{$headers{'return-path'}});
+    logincoming('To:         ', @{$headers{'to'}});
+    logincoming('Subject:    ', @{$headers{'subject'}});
+    logincoming('Message-Id: ', @{$headers{'message-id'}});
 }
 
 \f
@@ -411,6 +451,17 @@ sub store_message {
                 # So attempt to create a file with this name.  If it
                 # succeeds, in just a bit here we'll knowingly clobber
                 # this file with the rename(2) call.
+
+                # Another way to do this is not to use rename(2), but
+                # use link(2) + unlink(2) instead.  That's how the
+                # standard maildir algorithm does it.  Each method has
+                # a disadvantage: the program may crash between the
+                # link(2) and unlink(2) calls.  With the standard
+                # maildir algorithm, that means the message will end
+                # 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.
                 if ($run) {
                     if (sysopen(MSG, "$mhmsg",
                                 O_WRONLY | O_EXCL | O_CREAT, 0600)) {
@@ -433,7 +484,9 @@ sub store_message {
         #sleep(2);
     }
 
-    logsave("+$mhfolder");
+    if ($mhfolder ne 'SPAM') {
+        logsave("+$mhfolder");
+    }
 
     if ($run) {
         if (not rename($msg, $mhmsg)) {
@@ -514,18 +567,23 @@ sub get_headers {
 # Spam handling
 
 sub kill_spam {
+    my @baton;
     my @msglist = @_;
     my @result;
     my $msg;
     my $i;
 
-    @result = ();
+    @baton = spam_start_hook();
 
+    @result = ();
     $i = 0;
     foreach $msg (@msglist) {
         printf('%sChecking for spam... %6d/%d',
                "\r", ++$i, scalar(@msglist));
-        if (is_spam($msg)) {
+        if ($printfilenames) {
+            print($msg);
+        }
+        if (spam_check($msg, @baton)) {
             print(" SPAM\n");
             store_message($msg, 'SPAM');
         } else {
@@ -534,6 +592,8 @@ sub kill_spam {
     }
     print("\nDone: ", scalar(@result), " survivors\n");
 
+    spam_stop_hook(@baton);
+
     return @result;
 }
 
@@ -566,13 +626,32 @@ sub find_mh_folder {
         @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.
+            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/) {
+                if ($contents =~ /$match/i) {
                     if (eval "\$result = \"$expression\"") {
                         return $result;
                     }
@@ -646,6 +725,11 @@ are traversed I<in order>.  This means the filters can be arranged so
 that some have priority over others.  XXX: get Doug to write an
 example of having the same header matched more than once.
 
+Lastly, B<minc> supports a magic ' TO' header.  Filters using this
+header are matched against a collection of headers related to (and
+including) the To.  Which headers to use is determined by a regular
+expression borrowed from procmail.
+
 =head1 EXAMPLES
 
 @FILTERS = (
@@ -681,7 +765,7 @@ apache.org subdomain, with mailing lists hosted there.  So, given a
 list such as dev@httpd.apache.org, this filter will create the folder
 name l/apache/httpd/dev.
 
-For an example is_spam function, see
+For an example B<spam_check> function, see
 L<http:E<047>E<047>pretzelnet.orgE<047>cvsE<047>dotfilesE<047>.mincspam>
 
 =head1 AUTHORS