#!/usr/bin/perl use warnings; use strict; use File::Copy; use File::Slurp; chdir or die "cannot chdir to home directory: $!"; # Just hope there are no magic chars in these filenames. my $todo = 'mail/new_mail'; my $todo_filtered = 'mail/new_mail_not_obvious_spam'; my $tagged = 'mail/new_mail_tagged'; #my $notspam = 'mail/new_mail_notspam'; #my $spam = 'mail/new_mail_spam'; my $inbox = 'mail/inbox'; #my $inbox_spam = 'mail/inbox_spam'; my $delivery = '.email'; my $spam_to_learn = 'mail/spam'; my $spam_learned = 'mail/spam_learned'; my $ham_to_learn = 'mail/ham'; my $ham_learned = 'mail/ham_learned'; START: foreach ($todo, $tagged) { die "$_ already exists" if -e } sub append( $$ ) { my ($from, $to) = @_; my $got = read_file $from; local *APPEND; open APPEND, '>>', $to or die "cannot open $to for appending: $!"; print APPEND $got or die "cannot write to $to: $!"; close APPEND or die "cannot close $to: $!"; } if (0) { my $sa_update = 'sa-update'; print "running $sa_update\n"; if (system($sa_update)) { # Copied from perlfunc(1). Is there a CPAN module to do this? if ($? == -1) { die "failed to run $sa_update: $!\n"; } elsif ($? & 127) { die(sprintf("$sa_update died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without')); } else { my $exit_status = $? >> 8; if ($exit_status == 1) { print "no updates\n"; } else { die "$sa_update failed with exit status $exit_status\n"; } } } else { print "downloaded updates ok\n"; } } my @learn = ([ $spam_to_learn, '--report', $spam_learned ], [ $ham_to_learn, '--revoke', $ham_learned ]); foreach (@learn) { my ($in, $flag, $out) = @$_; next if not -e $in; print "learning from $in\n"; system('spamassassin', '--progress', $flag, '--mbox', $in) && die "spamassassin $flag failed"; print "done $in, appending it to $out\n"; append $in, $out; print "unlinking $in\n"; unlink $in or die "cannot unlink $in: $!"; } if (not -e $delivery) { print "$delivery not there, no new mail to process\n"; exit; } print "moving $delivery to $todo\n"; move $delivery, $todo or die "cannot move $delivery to $todo: $!"; my $grepmail_not_spam = "grepmail -vh 'X-Spam-Status: Yes'"; my $grepmail_spam = "grepmail -h 'X-Spam-Status: Yes'"; print "filtering out messages in $todo already marked as spam and writing to $todo_filtered\n"; system("$grepmail_not_spam $todo >$todo_filtered") && die 'grepmail failed'; print "spam-checking $todo_filtered and writing to $tagged\n"; system("spamassassin --progress --mbox $todo_filtered >$tagged") && die 'spamassassin failed'; #print "filtering non-spam messages from $tagged to $notspam\n"; #system("$grepmail_not_spam $tagged >$notspam") # && die 'grepmail failed'; #print "filtering spam messages from $tagged to $spam\n"; #system("$grepmail_spam $tagged >$spam") # && die 'grepmail failed'; #die 'size mismatch' if (-s $notspam) + (-s $spam) != -s $tagged; #print "appending $notspam to $inbox\n"; #append $notspam, $inbox; #print "appending $spam to $inbox_spam\n"; #append $spam, $inbox_spam; #my @to_unlink = ($todo, $todo_filtered, $tagged, $notspam, $spam); print "appending $tagged to $inbox\n"; append $tagged, $inbox; my @to_unlink = ($todo, $todo_filtered, $tagged); print "unlinking @to_unlink\n"; unlink @to_unlink or die "cannot unlink @to_unlink: $!"; goto START;