Some good fun this morning. Decided to make my procmail handling of mailing lists a little more automated. Using python, which I don't know at all.
So, this is what I came up with. my .procmailrc loads from various files, most notably:
INCLUDERC=$PM/lists.rc INCLUDERC=$PM/automation.rc
so lists are matched before automation, which is important. automation.rc contains
:0 hc: * ^List-ID.* | $PM/listid.py
So that any lists which haven't been matched already, get matched here. The fun sauce, is listid.py;
import sys import os from email.parser import Parser from email.header import Header headers = Parser().parse(sys.stdin) if 'list-id' in headers: listid = headers['list-id'] destination = listid.split('<')[1].split('.')[0] rc = open('/home/soneil/.procmail/lists.rc', 'a') rc.write(':0:\n') rc.write('* ^List-ID.*%s\n' % listid.split('<')[1].split('>')[0]) rc.write('.Lists.%s/\n' % destination) rc.write('\n') os.mkdir('/home/soneil/Maildir/.Lists.%s' % destination)
There's a bunch of 'todo' here. hardcoded paths are naughty. There's no checking to see if a filter (or directory) already exists. Neither should happen, but neither are handled well. And lastly, once I trust it's working properly, I want to pipe the mail back into procmail so that it's run again, but this time hitting the new filter and ending up in the new directory. Some fun potential for endless loops there :)