]> diplodocus.org Git - minc/blobdiff - minc
(get_headers): Check errors on open.
[minc] / minc
diff --git a/minc b/minc
index 8bf698b..d008d66 100755 (executable)
--- a/minc
+++ b/minc
@@ -4,11 +4,15 @@
 
 =head1 NAME
 
-B<minc> - Incorporate mail from a maildir into mh folders.
+B<minc> - incorporate mail from a maildir into mh folders
 
 =head1 SYNOPSIS
 
-B<minc> [-B<dmnp>]
+B<minc> [B<-m> I<MAX>] [B<-n>] [B<-p>]
+
+B<minc> B<-d>
+
+B<minc> B<-h>
 
 =head1 DESCRIPTION
 
@@ -30,8 +34,9 @@ use Errno;
 use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
 use FileHandle;
 use File::stat;
-use Getopt::Std; $Getopt::Std::STANDARD_HELP_VERSION = 1;
+use Getopt::Long qw(:config gnu_getopt no_ignore_case);
 use POSIX qw(strftime WEXITSTATUS WIFEXITED);
+use Pod::Usage;
 
 our $VERSION = 1;
 
@@ -53,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.
 
@@ -76,30 +81,23 @@ problem.
 
 =cut
 
-my $dumpfilters = 0;
+my $dumpfilters;
+my $help;
 my $maxmsgs;
-our $run = 1;
-my $printfilenames = 0;
+my $norun;
+my $printfilenames;
 
-my %opts;
+GetOptions(
+           'd' => \$dumpfilters,
+           'h|help' => \$help,
+           'm=i' => \$maxmsgs,
+           'n' => \$norun,
+           'p' => \$printfilenames,
+          ) or pod2usage();
+$help and pod2usage(-exitstatus=>0, -verbose=>1);
+@ARGV == 0 or pod2usage();
 
-if (not getopts('dm:np', \%opts)) {
-    exit(2);
-}
-
-if ($opts{'d'}) {
-    $dumpfilters = 1;
-} elsif ($opts{'n'}) {
-    $run = 0;
-}
-
-if (defined($opts{'m'})) {
-    $maxmsgs = $opts{'m'};
-}
-
-if ($opts{'p'}) {
-    $printfilenames = 1;
-}
+our $run = !$norun;
 
 =head1 ENVIRONMENT
 
@@ -248,25 +246,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): $!");
     }
 }
 
@@ -275,7 +262,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
@@ -290,10 +277,10 @@ sub getfiles {
       } readdir(DIR);
 
     if ($! != 0) {
-        die("Failed readdir($dir)");
+        die("readdir($dir): $!");
     }
 
-    if (scalar(@result) == 0) {
+    if (@result == 0) {
         exit(0);
     }
 
@@ -314,14 +301,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);
@@ -405,7 +393,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
@@ -417,6 +405,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) {
@@ -442,7 +432,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) {
@@ -545,7 +535,7 @@ sub find_mh_folder {
 
 sub filter_mail {
     my @msglist = @_;
-    my $msgcount = scalar(@msglist);
+    my $msgcount = @msglist;
     my $msgcountlen = length($msgcount);
     my @baton;
     my $msg;
@@ -565,9 +555,13 @@ sub filter_mail {
     @baton = spam_start_hook();
 
     foreach $msg (@msglist) {
+        if ($printfilenames) {
+            print("$msg\n");
+        }
+
         if (spam_check($msg, @baton)) {
             $mhfolder = 'SPAM';
-            $spam = scalar(@SPAM)+ 1;
+            $spam = @SPAM + 1;
         } else {
             $mhfolder = find_mh_folder($msg);
             $saved++;
@@ -690,7 +684,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