#!/usr/bin/python import re class Exim: def __init__(self, file): msgs = dict() msg_from = re.compile("^\d+-\d+-\d+ \d+:\d+:\d+ (?P[\w-]+) <= (?P[\w.-]+@[\w.-]+)") msg_to = re.compile("^\d+-\d+-\d+ \d+:\d+:\d+ (?P[\w-]+) => (?P[\w.-]+@[\w.-]+)") f = open(file, "r") for l in f.readlines(): # From field r = msg_from.search(l) if r: if msgs.has_key(r.group("msgid")): msgs[r.group("msgid")][0] = r.group("from") else: msgs[r.group("msgid")] = [r.group("from"), None] # To field r = msg_to.search(l) if r: if msgs.has_key(r.group("msgid")): msgs[r.group("msgid")][1] = r.group("to") else: msgs[r.group("msgid")] = [None, r.group("to")] f.close() # Remove incomplete values for k in msgs.keys(): if msgs[k][0] == None or msgs[k][1] == None: del msgs[k] self.msgs = msgs def links(self): res = [] for k in self.msgs: res.append((self.msgs[k][0], self.msgs[k][1])) return res if __name__ == "__main__": feed = Exim("data/mainlog") print feed.links()