From cde5494de3242f5bd703f7a6b7b86a002e30b21e Mon Sep 17 00:00:00 2001 From: b3yond Date: Thu, 18 Jan 2018 12:42:37 +0100 Subject: [PATCH] mailbot uses reports now, and doesn't need to own trigger --- mailbot.py | 63 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/mailbot.py b/mailbot.py index b53cf14..39c1eb9 100644 --- a/mailbot.py +++ b/mailbot.py @@ -9,6 +9,7 @@ import email import logging import pytoml as toml import imaplib +import report logger = logging.getLogger(__name__) @@ -19,7 +20,7 @@ class Mailbot(object): other bots that it received mails. """ - def __init__(self, config, trigger, history_path="last_mail"): + def __init__(self, config, history_path="last_mail"): """ Creates a Bot who listens to mails and forwards them to other bots. @@ -27,7 +28,6 @@ class Mailbot(object): :param config: (dictionary) config.toml as a dictionary of dictionaries """ self.config = config - self.trigger = trigger self.history_path = history_path self.last_mail = self.get_history(self.history_path) @@ -55,10 +55,17 @@ class Mailbot(object): except: logger.error('Mail sending failed', exc_info=True) - def listen(self): + def repost(self): """ - listen for mails which contain goodwords but no badwords. - :return: + E-Mails don't have to be reposted - they already reached everyone on the mailing list. + The function still needs to be here because ticketfrei.py assumes it. + """ + pass + + def crawl(self): + """ + crawl for new mails. + :return: msgs: (list of report.Report objects) """ rv, data = self.mailbox.select("Inbox") msgs = [] @@ -81,11 +88,12 @@ class Mailbot(object): if date > self.get_history(self.history_path): self.last_mail = date self.save_last_mail() - msgs.append(msg) + msgs.append(self.make_report(msg)) return msgs def get_history(self, path): - """ This counter is needed to keep track of your mails, so you + """ + This counter is needed to keep track of your mails, so you don't double parse them :param path: string: contains path to the file where the ID of the @@ -106,21 +114,23 @@ class Mailbot(object): with open(self.history_path, "w") as f: f.write(str(self.last_mail)) - def send_report(self, statuses): + def post(self, statuses): """ - sends reports by twitter & mastodon to a mailing list. + sends reports by other sources to a mailing list. - :param statuses: (list) of status strings + :param statuses: (list of report.Report objects) """ for status in statuses: + status = status.format() mailer = sendmail.Mailer(self.config) mailer.send(status, self.mailinglist, "Warnung: Kontrolleure gesehen") - def to_social(self, msg): + def make_report(self, msg): """ - sends a report from the mailing list to social + generates a report out of a mail + :param msg: email.parser.Message object - :return: post: (string) of author + text + :return: post: report.Report object """ # get a comparable date out of the email date_tuple = email.utils.parsedate_tz(msg['Date']) @@ -131,26 +141,26 @@ class Mailbot(object): # :todo take only the part before the @ text = msg.get_payload() - post = author + ": " + text + post = report.Report(author, "mail", text, None, date) self.last_mail = date self.save_last_mail() return post - def flow(self, statuses): + def flow(self, trigger, statuses): """ - to be iterated + to be iterated. uses trigger to separate the sheep from the goats - :param statuses: (list) of statuses to send to mailinglist - :return: list of statuses to post in mastodon & twitter + :param statuses: (list of report.Report objects) + :return: statuses: (list of report.Report objects) """ - self.send_report(statuses) + self.post(statuses) - msgs = self.listen() + msgs = self.crawl() statuses = [] for msg in msgs: - if self.trigger.is_ok(msg.get_payload()): - statuses.append(self.to_social(msg)) + if trigger.is_ok(msg.get_payload()): + statuses.append(msg) return statuses @@ -159,16 +169,21 @@ if __name__ == "__main__": with open('config.toml') as configfile: config = toml.load(configfile) + # set log file fh = logging.FileHandler(config['logging']['logpath']) fh.setLevel(logging.DEBUG) logger.addHandler(fh) + # initialise trigger trigger = trigger.Trigger(config) - m = Mailbot(config, trigger) + + # initialise mail bot + m = Mailbot(config) + statuses = [] try: while 1: - print("Received Reports: " + str(m.flow(statuses))) + print("Received Reports: " + str(m.flow(trigger, statuses))) time.sleep(1) except KeyboardInterrupt: print("Good bye. Remember to restart the bot!")