#!/usr/bin/python import email, os, re, sys, time from xml.sax.saxutils import escape blosxom = '/home/rubys/web/intertwingly.net/blosxom' msg = email.message_from_file(sys.stdin) # determine the target blog entry. If this fails, python will throw # an exception, and procmail will resume with target = re.findall("(^|\W)blog\W(\d+)(\W|$)",msg['To'])[0][1] title = escape(msg['Subject']) name = escape(msg['From'].split(' <')[0]).replace('"','') addr = escape(msg.get_unixfrom().split(' ')[1]).replace('"','"') path = escape(re.sub('\s+', ' ', msg['Received'])).replace('"','"') if title.find('*****SPAM*****')==0: sys.exit(99) # extract and escape the text portion of the payload body = msg.get_payload() if isinstance(body,list): body=body[0].get_payload() body = escape(body.strip()) body=re.compile("(SPAM.*SPAM[^\n]*)",re.S).sub("
\\1",body) if body.startswith('
<html><body bgcolor=')==0: sys.exit(99)
# default title to the title of the blog entry
entry=blosxom+'/'+target+'.txt'
if not title: title=open(entry).readline().strip()
# only allow email for 30 days
age=(time.mktime(time.localtime())-os.stat(entry).st_mtime)/86400
if age>=31: sys.exit(99)
# write the comment out where blosxom will find it
file=blosxom+'/'+target+'-'+str(int(time.mktime(time.gmtime())))+'.cmt'
out=open(file, 'w')
out.write("""%s
%s
Emailed by %s
""" % (title, body, addr, path, name))
out.close()
# make the file readable
os.chmod(file, 0664)