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
=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
=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");
###############################################################################
# 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,
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
# Utility procedures
sub _errprint {
- printf("%s:%s", __FILE__, __LINE__);
+ printf('%s:%s', __FILE__, __LINE__);
if (@_) {
print(': ');
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`;
}
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");
+ }
}
}
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>) {
# 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);
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
# 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;
}
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;
}
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}})) {