#!/usr/bin/python # -*- coding: utf-8 -*- # Inject one or more mbox of email, or a single email into the system. # # ./inject-mbox.py mbox [mbox ...] # ./inject-mbox.py < single_email # # Note that if you inject the same message or message threads twice, # then they are processed twice. patchq does not deduplicate. # # This script will inject anything that looks similar enough to an # email, even non-patches, cover letters, etc. The ‘threader.py’ # script filters out non-patches. import email import mailbox import pika import sys import config connection = pika.BlockingConnection(pika.ConnectionParameters( host = config.mq_server)) channel = connection.channel() processed = 0 def inject(m): global processed # Decode the subject line and store it back in the email as UTF-8. # This saves a lot of effort later on, even though it's not # strictly RFC822 compliant. # https://stackoverflow.com/questions/7331351/python-email-header-decoding-utf-8/7331577#7331577 subj = m['Subject'] subj = email.header.decode_header(subj) subj = ''.join([ unicode(t[0], t[1] or 'ASCII') for t in subj ]) m['Subject'] = subj print("Injecting %s" % m['Subject']) channel.basic_publish(exchange = 'patchq_input', routing_key = '', body = m.as_string()) processed = processed+1 # Read from mboxes passed on the command line, or read a single # email from stdin. if len(sys.argv) > 1: for arg in sys.argv[1:]: mbox = mailbox.mbox(arg) for m in mbox: inject(m) else: m = email.message_from_file(sys.stdin) inject(m) print ("Processed %d email(s)." % processed)