+
+def parse_and_deliver(maildir, url, statedir):
+ md = mailbox.Maildir(maildir)
+ fp = feedparser.parse(url)
+ for item in fp["items"]:
+ # things that we need in the message
+ msg = email.message_from_string("")
+ msg.add_header("Subject", item["title"])
+ msg.set_unixfrom("Brett Parker <iDunno@sommitrealweird.co.uk>")
+ msg.add_header("Date", datetime.datetime(*item["created_parsed"][0:6]).strftime("%a, %e %b %Y %T -0000"))
+ msg.add_header("To", url)
+ msg.set_payload(item["content"][0]["value"])
+ msg.set_charset("utf8")
+ msg.set_default_type("text/plain")
+
+ # start by working out the filename we should be writting to, we do
+ # this following the normal maildir style rules
+ fname = str(os.getpid()) + "".join([random.choice(string.ascii_letters + string.digits) for a in range(0,10)]) + datetime.datetime.now().strftime('%s')
+ fn = os.path.join(maildir, "tmp", fname)
+ fh = open(fn, "w")
+ fh.write(msg.as_string())
+ fh.close()
+ # now move it in to the new directory
+ newfn = os.path.join(maildir, "new", fname)
+ os.link(fn, newfn)
+ os.unlink(fn)
+