I spent the past few days looking for a good way to implement spamassassin server wide. Most solutions involve creating a .qmail files for all virtual users, but I don't like that. There's qmail-scanner that can use spamassassin to tag messages, but does not stop spam delivery. Local users benefit from /etc/procmailrc, but virtual users cannot.
To make things even worse, I don't want to stop spam, but deliver it to a mailbox that I can review to see if my spamassassin settings are correct.
After hours of searching and not finding anything that did what I want, I hacked up this little script that does exactly what I want. Perl's just the glue that holds things together, but I couldn't find strong glue like this elsewhere.
#!/usr/bin/perl
# Server wide spam stopper for qmail, second version.
# Meant to be used with qmail-qfilter and spamassassin.
# Does not require or use procmail.
# Use at your own risk. Sysadmin skills are required.
# Made by Juerd Waalboer.
# In tcpserver configuration or init.d/qmail
# QMAILQUEUE=/path/to/somefilter
#
# #!/bin/sh
# # This is /path/to/somefilter
# exec /path/to/qmail-qfilter \
# /path/to/spamc -- \
# /path/to/thisscript -- \ # This Perl script
# /path/to/qmail-inject -n
use strict;
use Sys::Hostname qw(hostname);
use Time::HiRes qw(gettimeofday);
use Fatal qw(open close link unlink);
my $maildir = '/var/spam'; # Maildir to store spam in.
my $spam = 0;
my $headers = '';
while (<>) {
/^$/ and do {
$/ = \65536;
if ($spam > 0) {
my $tph = join '.',
time(), "$$\_".(gettimeofday)[1], hostname();
open my $fh, ">$maildir/tmp/$tph";
print $fh $headers, $_;
print $fh $_ while <>;
close $fh;
chmod 0660, "$maildir/tmp/$tph";
link "$maildir/tmp/$tph", "$maildir/new/$tph";
unlink "$maildir/tmp/$tph";
exit 99; # "Succesful", but don't deliver
} else {
print $headers, $_;
print while <>;
exit 0; # Deliver it
}
};
/^X-Spam-Flag: YES/ and $spam++;
$headers .= $_;
}
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.