]> diplodocus.org Git - minc/blobdiff - minc
Document that HOME is used to find the maildir in the absence of
[minc] / minc
diff --git a/minc b/minc
index 8963d4e..346330f 100755 (executable)
--- a/minc
+++ b/minc
@@ -34,19 +34,6 @@ use Log::Dispatch;
 use Log::Dispatch::File;
 use POSIX qw(strftime WEXITSTATUS WIFEXITED);
 
-# debuglevels for the logger
-use constant LOG_DEBUGLEVEL     => 'info';
-use constant SCREEN_DEBUGLEVEL  => 'debug';
-
-# characters used to wrap around the id field in the log so I can more
-# easily parse the msg flow
-use constant DEBUGCHAR          => '%';
-use constant INFOCHAR           => '=';
-use constant INCOMINGCHAR       => '<';
-use constant SAVECHAR           => '>';
-use constant WARNCHAR           => 'W';
-use constant ERRORCHAR          => 'E';
-
 =head1 OPTIONS
 
 =over 4
@@ -106,7 +93,8 @@ if ($opt_s) {                   # ))){ # stupid cperl-mode
 
 =item HOME
 
-Where configuration files (.mincfilter) are found.
+Where configuration files (.mincfilter) are found.  Also,
+$HOME/Maildir is used for the maildir if MAILDIR is not set.
 
 =item MAILDIR
 
@@ -116,8 +104,7 @@ Where mail is delivered.
 
 =cut
 
-use Env qw(HOME);
-use Env qw(MAILDIR);
+use Env qw(HOME MAILDIR);
 
 if (not $HOME) {
     die("HOME environment variable must be set.\n");
@@ -164,6 +151,19 @@ if ($run) {
 ###############################################################################
 # Logging
 
+# debuglevels for the logger
+use constant LOG_DEBUGLEVEL     => 'info';
+use constant SCREEN_DEBUGLEVEL  => 'debug';
+
+# characters used to wrap around the id field in the log so I can more
+# easily parse the msg flow
+use constant DEBUGCHAR          => '%';
+use constant INFOCHAR           => '=';
+use constant INCOMINGCHAR       => '<';
+use constant SAVECHAR           => '>';
+use constant WARNCHAR           => 'W';
+use constant ERRORCHAR          => 'E';
+
 my $logger = new Log::Dispatch;
 $logger->add(new Log::Dispatch::File (name=>'logfile',
                                       filename=>$logfile,
@@ -201,10 +201,10 @@ sub logerr { mylog('error', ERRORCHAR, @_); }
 sub log_headers {
     my %headers = @_;
 
-    logincoming('From:       ', $headers{'Return-Path'});
-    logincoming('To:         ', $headers{'To'});
-    logincoming('Subject:    ', $headers{'Subject'});
-    logincoming('Message-Id: ', $headers{'Message-Id'});
+    logincoming('From:       ', $headers{'return-path'});
+    logincoming('To:         ', $headers{'to'});
+    logincoming('Subject:    ', $headers{'subject'});
+    logincoming('Message-Id: ', $headers{'message-id'});
 }
 
 \f
@@ -212,7 +212,7 @@ sub log_headers {
 # Utility procedures
 
 sub _errprint {
-    printf("%s:%s", __FILE__, __LINE__);
+    printf('%s:%s', __FILE__, __LINE__);
 
     if (@_) {
         print(': ');
@@ -275,7 +275,10 @@ sub store_message {
     my $mhfolder = shift;
     my $mhmsg;
     my $msgnum;
+    my $status;
 
+    # We must do this even in -n mode because later steps fail without
+    # it.  This should be harmless.
     mkfolder($mhfolder);
 
     $mhmsg = `mhpath +$mhfolder new`;
@@ -290,7 +293,10 @@ sub store_message {
         }
 
         if ($mhfolder ne 'SPAM') {
-            system("mark +$mhfolder $msgnum -sequence unseen -add");
+            $status = system("mark +$mhfolder $msgnum -sequence unseen -add");
+            if (not (WIFEXITED($status) and WEXITSTATUS($status) == 0)) {
+                errx(&EX_TEMPFAIL, "Failed to mark message unseen");
+            }
         }
     }
 
@@ -303,7 +309,8 @@ sub store_message {
 sub get_headers {
     my $msg = shift;
     my %headers;
-    my $current;            # current header, used for unfolding lines
+    my $current;                # current header, used for unfolding lines
+    my $fieldname;              # unmolested header name
 
     open(MSG, $msg);
     while (<MSG>) {
@@ -316,19 +323,15 @@ sub get_headers {
             # folded header continuation
 
             if (!defined($current)) {
-                # XXX: Malformed message; log to stderr
-                return undef;
+                print("Malformed message, cannot parse headers.\n");
+                return ();
             }
 
             $headers{$current} .= $_;
         } else {
-            ($current) = split(/:/);
-
-            # XXX: Is it correct simply to eat all whitespace between
-            # the colon and the first text for the header?
-            # Furthermore, is any space at all required?  Or is
-            # '^Subject:hey$' a perfectly valid header?
-            (undef, $headers{$current}) = split(/^$current:\s*/);
+            ($fieldname) = split(/:/);
+            $current = lc($fieldname);
+            (undef, $headers{$current}) = split(/^$fieldname:\s*/);
         }
     }
     close(MSG);
@@ -362,8 +365,10 @@ sub is_spam {
 
         logerr("$err: $message: $!");
 
-        if (unlink($err) != 1) {
-            err(&EX_TEMPFAIL, "Failed unlink($err)");
+        if ($run) {
+            if (unlink($err) != 1) {
+                err(&EX_TEMPFAIL, "Failed unlink($err)");
+            }
         }
     } elsif (-f $sig) {
         # This is supposed to be a signature created with razor-check
@@ -372,14 +377,22 @@ sub is_spam {
         # into razor-check.  For now, just unlink any sig files we
         # find and proceed with full razor-check mode.
 
-        if (unlink($sig) != 1) {
-            err(&EX_TEMPFAIL, "Failed unlink($sig)");
+        if ($run) {
+            if (unlink($sig) != 1) {
+                err(&EX_TEMPFAIL, "Failed unlink($sig)");
+            }
         }
     }
 
-    $status = system("razor-check < $msg");
-    if (WIFEXITED($status) and WEXITSTATUS($status) == 0) {
-        return 1;
+    if ($run) {
+        $status = system("razor-check < $msg");
+        if (not WIFEXITED($status)) {
+            err(&EX_TEMPFAIL, "Failed to run razor-check < $msg");
+        } elsif (WEXITSTATUS($status) == 0) {
+            return 1;
+        } else {
+            return 0;
+        }
     } else {
         return 0;
     }
@@ -389,16 +402,22 @@ sub kill_spam {
     my @msglist = @_;
     my @result;
     my $msg;
+    my $i;
 
     @result = ();
 
+    $i = 0;
     foreach $msg (@msglist) {
+        printf('%sChecking for spam... %6d/%d',
+               "\r", ++$i, scalar(@msglist));
         if (is_spam($msg)) {
+            print(" SPAM\n");
             store_message($msg, 'SPAM');
         } else {
             push(@result, $msg);
         }
     }
+    print("\nDone: ", scalar(@result), " survivors\n");
 
     return @result;
 }
@@ -417,11 +436,14 @@ sub find_mh_folder {
     my $try;
 
     %headers = get_headers($msg);
+    if (not %headers) {
+        return 'inbox';
+    }
 
     log_headers(%headers);
 
     foreach $header (keys(%FILTERS)) {
-        $contents = $headers{$header};
+        $contents = $headers{lc($header)};
 
         if (defined($contents)) {
             foreach $regex (keys(%{$FILTERS{$header}})) {