Automatic sorting of mailing lists with maildrop
I'm lazy. I used to sort my mails with procmail and I was hesitant of subscribing to yet another mailing list because that meant finding the appropriate X-Ben-There: (Or List-Id:) header, logging into my server, adding a procmail rule for this list and logging out again, then moving all the mails that arrived before the rule into the folder. This seems precisely like the kind of dull task a computer is good at. :-)
When I started to become involved in the OpenMoko project the number of mailing lists I subscribed to just exploded.
So I read up on the MATCH operator in procmail and came up with the following snippet:
:0
* ^X-BeenThere: \/[^@]*
.ML.$MATCH/
This takes the part matching the regular expression right of the '\/' and assigns that to the variable MATCH.
The problem here is that this only grabs the part before the domain, so announce@foo and announce@bar are both sorted into ML/announce (Folders can't contain @ in their name). Unfortunately there is only one MATCH variable in procmail so there's no way around that as long as you're using procmail. So I had a look at and maildrop.
I find the syntax maildrop uses much more concise (if you know C style syntax anyway). However it seems that you can't define and use functions yourself, which is a little annoying. Especially since you will have to create a new folder yourself if it doesn't already exist.
But anyway here's the snippet, that sorts your mailinglists into folders of the format ML/listname-at-listdomain (replacing the dots in the domain with a hyphen):
if ( /^X-BeenThere:\s*(.*)/ )
{
ADDR=getaddr($MATCH)
if ($ADDR =~ /^(.*)@(.*)/)
{
LISTNAME=${MATCH1}
LISTDOMAIN=""
foreach ($MATCH2) =~ /[^.]+/
{
LISTDOMAIN="${LISTDOMAIN}-${MATCH}"
}
MDFOLDER="${MLBASE}.${LISTNAME}-at${LISTDOMAIN}"
include "${MAILDROPFOLDER}/deliver2folder"
}
}
Since I can't define any functions I am including files with the required functionality and pass arguments through variables (ugly, but it works). Here's the content of deliver2folder and createfolder (used by deliver2folder):
deliver2folder:
include "${MAILDROPFOLDER}/createfolder"
to "${MDBASE}/.${MDFOLDER}/"
createfolder:
# Check if a maildir folder exists and create it if necessary
# The folder must be in MDFOLDER, the maildir base folder in MDBASE
`test -d ${MDBASE}/.${MDFOLDER}`
if ( $RETURNCODE == 1 )
{
`/usr/bin/maildirmake -f ${MDFOLDER} ${MDBASE}`
}
Now if only I could convince claws-mail to automatically scan for new folders every time...
Trackbacks
Use the following link to trackback from your own site:
http://totalueberwachung.de/blog/trackbacks?article_id=259