diff --git a/remove-spam-high-confidence-maildir.plx b/remove-spam-high-confidence-maildir.plx index d3e61e8..e13f9bc 100644 --- a/remove-spam-high-confidence-maildir.plx +++ b/remove-spam-high-confidence-maildir.plx @@ -21,17 +21,21 @@ use strict; use warnings; use Mail::Header; +use Date::Manip; + #use File::Copy; if (@ARGV < 3 or @ARGV > 4) { - print STDERR "usage: $0 []\n"; + print STDERR "usage: $0 []\n"; exit 1; } -my($MAILDIR_FOLDER, $DSPAM_PROB_MIN, $DSPAM_CONF_MIN, $COUNT_ONLY) = @ARGV; +my($MAILDIR_FOLDER, $DSPAM_PROB_MIN, $DSPAM_CONF_MIN, $DAYS, $COUNT_ONLY) = @ARGV; my($total, $countDeleted) = (0, 0); +my $nDaysAgo = ParseDate("$DAYS days ago"); + my @msgDirs = ("$MAILDIR_FOLDER/cur", "$MAILDIR_FOLDER/new"); foreach my $dir (@msgDirs) { @@ -52,6 +56,26 @@ MAIL: foreach my $dir (@msgDirs) { my $header = new Mail::Header(\*MAIL_MESSAGE); my $fields = $header->header_hashref; + my $mailDate; + foreach my $dt (@{$fields->{"Date"}}) { + if (not defined $mailDate) { + $mailDate = $dt; + } else { + $mailDate = $dt if $dt lt $maileDate; + } + } + if (not defined $mailDate) { + print STDERR "File $file has no Date: header. Skipping.\n"; + next MAIL; + } + $parsedDate = ParseDate($mailDate); + unless (defined $parseDate) { + print STDERR "File $file has Unparsable Date header $mailDate"; + next MAIL; + } + + next MAIL if ($parseDate gt $nDaysAgo); + my %dspamVal; foreach my $val ('Confidence', 'Probability') { foreach my $dv (@{$fields->{"X-Dspam-$val"}}) {