def process_new_email(path, threads_index): with open(path, "r") as fd: parser = email.parser.HeaderParser() email_headers = parser.parse(fd) subject = email_headers["subject"] from_field = {} from_field["name"], from_field["address"] = email.utils.parseaddr( email_headers["From"]) to_field = {} to_field["addresses"] = email.utils.getaddresses(email_headers["to"]) if subject != None: subject = headers.cleanup_subject(subject) thread = None for index, thr in enumerate(threads_index): if thr["subject"] == subject: thread = threads_index.pop(index) break if not thread: # create a new thread thread = threads.create_thread_structure() thread["subject"] = subject thread["creator"] = from_field msg_id = os.path.basename(path) thread["messages"].append(msg_id) thread["date"] = datetime.datetime.utcnow() thread["unread"] = True if from_field["address"] != thread["creator"]["address"]: thread["lastreplyfrom"] = from_field threads_index.insert(0, thread)
def process_new_email(path, threads_index): with open(path, "r") as fd: parser = email.parser.HeaderParser() email_headers = parser.parse(fd) subject = email_headers["subject"] from_field = {} from_field["name"], from_field["address"] = email.utils.parseaddr(email_headers["From"]) to_field = {} to_field["addresses"] = email.utils.getaddresses(email_headers["to"]) if subject != None: subject = headers.cleanup_subject(subject) thread = None for index, thr in enumerate(threads_index): if thr["subject"] == subject: thread = threads_index.pop(index) break if not thread: # create a new thread thread = threads.create_thread_structure() thread["subject"] = subject thread["creator"] = from_field msg_id = os.path.basename(path) thread["messages"].append(msg_id) thread["date"] = datetime.datetime.utcnow() thread["unread"] = True if from_field["address"] != thread["creator"]["address"]: thread["lastreplyfrom"] = from_field threads_index.insert(0, thread)
def run(self): while True: while len(events_queue) != 0: event = events_queue.pop(0) if event["type"] == "create": try: print event["path"] with open(event["path"], "r") as fd: parser = email.parser.HeaderParser() email_headers = parser.parse(fd) subject = email_headers.get("Subject") print "Subject: %s" % subject if subject != None: subject = headers.cleanup_subject(subject) if subject in threads_index.data: threads_index.data[subject].append(event["path"]) else: threads_index.data[subject] = [event["path"]] except IOError: # Postfix/Dovecot creates temporary files. Ignore them pass time.sleep(EVENTS_QUEUE_PROCESSING_DELAY)