quick shunt
This script (qmail-quickshunt.pl) is written so that mail can be moved quickly before any sort of filtering takes place. This move is determined on a CDB lookup. CDB lookups are very fast and take very little time, they do not need any sort of DB connection over network (unless your file system requires such) and thus return results very quickly.
I started work on this one Sunday when I was about to create some maildrop rules for people where I wanted no filtering to take place, rather than maintain a list of addresses in maildrop format I thought it would be easier to have a text list, and this program was the result of that thought - or brain fart, however you wish to think of it!
The script itself was written to operate around my existing system, and some work has had to take place in order to make it self contained and be readable here.
This relies on the CDB_File library, which is available at cpan.
dot qmail
As with other delivery processes we must put a command into the .qmail file so that we can execute the script upon delivery. This is simple enough, like so:
| /var/qmail/bin/preline /usr/local/bin/qmail-quickshunt.pl
Make sure that the qmail-quickshunt.pl is user/group/other executable, mode 755, whichever you prefer.
cdb files
The script make_headers_cdb.pl takes care of this for us. It reads data on standard input and writes the file to it's first argument, so first create the keys file:
| envelope_from_ed@example.test*/Maildir/ |
| envelope_to_ed-test-this*/Maildir/.Trash/ |
| envelope_from_edn@example.test*/Maildir/.jokes/ |
| Subject: Test*/Maildir/.important/ |
My initial choice of separator was a colon (:), but as this is used in headers I decided to use an asterisk (*) instead, so if your field requires and asterisk please escape it with a backslash (\). When fed into the make_headers_cdb.pl it will produce the .cdb file. Ideally you might wish to run it like this:
cat header_keys.txt | perl make_headers.pl ~/Maildir/headers.cdb
how i use it
Well, this was extracted from my own delivery script which calls a variety of scanners and what-not which wouldn't make any sense in this context since it's partly a huge mess, and partly just because of the quirks of my system.
If you're an ISP you might want to use a qmail-queue-scanner patch to insert this functionality, but that might be a bit over kill. You could look into maintaining the .qmail file of your users also, this could allow you to introduce user specific filtering too.
Anyway, this is too much waffle, if you're a single user this is probably more use.
The script exits with return value of 99 if mail has been moved. This is useful if you wish to run this script from within a dot-qmail file since qmail-local will class the delivery to be a success on this event.
My personal dot-qmail file looks like this:
| | /var/qmail/bin/preline /home/ed/code/perl/qmail/qmail-quickshunt.pl |
| | /var/qmail/bin/preline /home/ed/code/perl/qmail/headers2.pl |
Using this setup allows my main script to continue it's work (filtering for spam later on.
Remember the main purpose of this script is to shunt mail out of the way to somewhere relevant if the mail matches some trusted headers. Maybe one day the same can be performed based on GPG signing!
Info