Eric Shubert wrote:
Jon Ernster wrote:
Figured I'd share something I wrote that others here might use since
many of you have shared or helped me out as well.
I wrote this because when I go on vacation I usually shut down the
laptop that has my mail rules which sends all my spam to the spam
folder. By the time I get back the shell script that I have that runs
on a daily basis (courtesy of Jake Vickers) gives this error because
there are too many spam files:
/root/learn-spam: /usr/bin/sa-learn: /usr/bin/perl: bad interpreter:
Argument list too long
/root/learn-spam: line 10: /bin/rm: Argument list too long
So I just wrote this to process the files individually. Not the fastest
script in the world (because of SpamAssassin, not becaues of my code,
obviously) ;), but it works.
J.
------------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
#~ $Id: learn-spam.pl 15 2008-07-03 17:27:09Z jernster $
my %opts = ();
getopts( 'd:u:', \%opts );
my ( $domain, $user ) = @opts{ qw( d u ) };
my $usage =<<EOF;
Usage:
$0 -d example.com -u user
EOF
die "$usage" unless ( $domain && $user );
my $dir = "/home/vpopmail/domains/$domain/$user/Maildir/cur";
my $starttime = time;
my $count = 0;
opendir(DIR, $dir);
my @files = readdir(DIR);
close(DIR);
foreach my $file ( @files )
{
if ( $file =~ /^\./ )
{
next;
}
else
{
my $fpfile = "$dir/$file";
$count++;
print "Learning SPAM - $file\n";
system("/usr/bin/sa-learn --spam $fpfile");
print "Deleting $file\n";
unlink($fpfile);
}
}
print "Syncing databases...\n";
system("/usr/bin/sa-learn --sync");
print "De-linting files...\n";
system("/usr/bin/spamassassin --lint");
system("chown vpopmail:vchkpw /home/vpopmail/.spamassassin/*");
system("/usr/bin/qmail-spam restart");
print "Done!\n";
my $duration = time - $starttime;
print "\nTotal duration: $duration seconds\n";
print "Processed $count SPAM files.\n";
Thanks, Jon. I wish we had a little more of this.
Observations:
.) I like the way you've handled parameters
.) Is this learning everything in the user's cur directory as spam? Doesn't
seem appropriate to me
.) all sa-learn and spamassassin commands need to be run as user vpopmail.
How is that happening?
.) qmail-spam is usually in /usr/sbin, not /usr/bin
Eric,
I create a user named spam and I forward everything to that
user/directory. You're right that this wouldn't be ideal for someone to
run this against their personal mail box. Alternatively this could
easily be modified to learn email as HAM instead of SPAM.
I just run the script as root - seems to work.
Location is pretty much irrelevant as long as it's some where in the
PATH as to not have to reference it WITH the full path if necessary.
Appreciate the comments!
Jon