#!/usr/bin/python """Script to create a blosxom entry from an email message. Takes a message from stdin. Concatenates all text/html sections and writes those to blog entry titled for the unix timestamp at which the message was received. If there are no HTML sections, concatenates all text/plain sections and posts those, after adding paragraphs. Posts all attached image/* files at the bottom. Subject of email becomes title of blog entry. $Id$ (c) 2003 Joseph Barillari Released under the terms of the X11 license.""" ## Configuration # require this email address ONLY_FROM="youremail@example.com" # blosxom $DATADIR DATADIR="/home/YOURHOMEDIR/entries" # $DATADIR from the internet WEBDIR="/entries/" # command to run after message is written to datadir # blank to disable #POST_PROCESS="perl /usr/lib/cgi-bin/blosxom.cgi -password='PASSWORD' -all=1" POST_PROCESS="" # reply with acknowledgments from this address FROM_ADDR="Blosxom Posting Script " # script will chgrp the 'entries' folder to this (blank to disable) CHGRP_TO="www-data" # cosmetics SEPARATOR="

" import sys, time, email, smtplib, traceback, os, re, string def reply(msg, subj, content): message = """From: Blosxom Posting Script\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n\r\nA copy of the original email is attached below:\r\n\r\n%s""" % (ONLY_FROM, subj, content, msg) # http://www.python.org/doc/current/lib/SMTP-example.html server = smtplib.SMTP('localhost') server.sendmail(FROM_ADDR, ONLY_FROM, message) server.quit() def main(): message_str = sys.stdin.read() msg = email.message_from_string(message_str) #reply(msg, "report", str(msg.items())) subject = msg.get("Subject", None) if not subject: reply(msg, "Error: No subject", "You must include a subject line.") sys.exit(1) fromline = msg.get("From", None) if not fromline: reply(msg, "Error: No from line", "You must include a from line.") sys.exit(1) if string.find(fromline, ONLY_FROM) == -1: reply(msg, "Error: Posting attempt from wrong address.", "You must post from the address specified in the ONLY_FROM variable.") sys.exit(1) ALL_TEXT = [] ALL_HTML = [] attachments = [] for part in msg.walk(): if part.get_content_type() == "text/plain": ALL_TEXT.append(part.get_payload(decode=1)) elif part.get_content_type() == "text/html": ALL_HTML.append(part.get_payload(decode=1)) elif part.get_content_type()[0:6] == "image/": attachments.append(part) rSbody=re.compile(".*]*>", re.IGNORECASE|re.MULTILINE) rShtml=re.compile(".*]*>", re.IGNORECASE|re.MULTILINE) rEhead=re.compile(".*]*>", re.IGNORECASE|re.MULTILINE) rEbody=re.compile("]*>.*", re.IGNORECASE|re.MULTILINE) rEhtml=re.compile("]*>.*", re.IGNORECASE|re.MULTILINE) p_bk = re.compile("\n\W*\n") l_bk = re.compile("\n") if len(ALL_HTML) == 0: body = "

" + string.join(ALL_TEXT, "\n\n") + "

" body = p_bk.sub("

",body) body = l_bk.sub("
",body) # I should use textile, or just require valid html else: body = "" for snippet in ALL_HTML: #try to remove all starting and ending HTML snippet=rShtml.sub("",snippet) snippet=rEhead.sub("",snippet) snippet=rSbody.sub("",snippet) snippet=rEhtml.sub("",snippet) snippet=rEbody.sub("",snippet) body = body+snippet+SEPARATOR body = subject+"\n\n"+body prefix = time.strftime("%Y_%m_%d_%H_%M_%S") anum = 0 for a in attachments: ct = a.get_content_type() extension = ct[string.index(ct,"/")+1:] filename = prefix + "-" + str(anum) + "." + extension open(DATADIR + "/" + filename, "w").write(a.get_payload(decode=1)) anum += 1 body += "

" f = open(DATADIR + "/" + prefix + ".txt", "w") f.write(body) f.close() time.sleep(3) if not POST_PROCESS=="": os.system(POST_PROCESS) if not CHGRP_TO=="": os.system("chgrp -R %s %s/*" % (CHGRP_TO, DATADIR)) os.system("chmod -R g+rwx %s" % (DATADIR)) reply(msg,"Successful post", "Posting succeeded. Message follows:") try: main() except: reply("", "Exception thrown", "blosxom-via-email failed. The following exception was thrown:\n\n" + string.join(traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2])))