mailbot uses reports now, and doesn't need to own trigger
This commit is contained in:
parent
ff73c5dc21
commit
cde5494de3
63
mailbot.py
63
mailbot.py
|
@ -9,6 +9,7 @@ import email
|
||||||
import logging
|
import logging
|
||||||
import pytoml as toml
|
import pytoml as toml
|
||||||
import imaplib
|
import imaplib
|
||||||
|
import report
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ class Mailbot(object):
|
||||||
other bots that it received mails.
|
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
|
Creates a Bot who listens to mails and forwards them to other
|
||||||
bots.
|
bots.
|
||||||
|
@ -27,7 +28,6 @@ class Mailbot(object):
|
||||||
:param config: (dictionary) config.toml as a dictionary of dictionaries
|
:param config: (dictionary) config.toml as a dictionary of dictionaries
|
||||||
"""
|
"""
|
||||||
self.config = config
|
self.config = config
|
||||||
self.trigger = trigger
|
|
||||||
|
|
||||||
self.history_path = history_path
|
self.history_path = history_path
|
||||||
self.last_mail = self.get_history(self.history_path)
|
self.last_mail = self.get_history(self.history_path)
|
||||||
|
@ -55,10 +55,17 @@ class Mailbot(object):
|
||||||
except:
|
except:
|
||||||
logger.error('Mail sending failed', exc_info=True)
|
logger.error('Mail sending failed', exc_info=True)
|
||||||
|
|
||||||
def listen(self):
|
def repost(self):
|
||||||
"""
|
"""
|
||||||
listen for mails which contain goodwords but no badwords.
|
E-Mails don't have to be reposted - they already reached everyone on the mailing list.
|
||||||
:return:
|
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")
|
rv, data = self.mailbox.select("Inbox")
|
||||||
msgs = []
|
msgs = []
|
||||||
|
@ -81,11 +88,12 @@ class Mailbot(object):
|
||||||
if date > self.get_history(self.history_path):
|
if date > self.get_history(self.history_path):
|
||||||
self.last_mail = date
|
self.last_mail = date
|
||||||
self.save_last_mail()
|
self.save_last_mail()
|
||||||
msgs.append(msg)
|
msgs.append(self.make_report(msg))
|
||||||
return msgs
|
return msgs
|
||||||
|
|
||||||
def get_history(self, path):
|
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
|
don't double parse them
|
||||||
|
|
||||||
:param path: string: contains path to the file where the ID of the
|
: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:
|
with open(self.history_path, "w") as f:
|
||||||
f.write(str(self.last_mail))
|
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:
|
for status in statuses:
|
||||||
|
status = status.format()
|
||||||
mailer = sendmail.Mailer(self.config)
|
mailer = sendmail.Mailer(self.config)
|
||||||
mailer.send(status, self.mailinglist, "Warnung: Kontrolleure gesehen")
|
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
|
: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
|
# get a comparable date out of the email
|
||||||
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
||||||
|
@ -131,26 +141,26 @@ class Mailbot(object):
|
||||||
# :todo take only the part before the @
|
# :todo take only the part before the @
|
||||||
|
|
||||||
text = msg.get_payload()
|
text = msg.get_payload()
|
||||||
post = author + ": " + text
|
post = report.Report(author, "mail", text, None, date)
|
||||||
self.last_mail = date
|
self.last_mail = date
|
||||||
self.save_last_mail()
|
self.save_last_mail()
|
||||||
return post
|
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
|
:param statuses: (list of report.Report objects)
|
||||||
:return: list of statuses to post in mastodon & twitter
|
:return: statuses: (list of report.Report objects)
|
||||||
"""
|
"""
|
||||||
self.send_report(statuses)
|
self.post(statuses)
|
||||||
|
|
||||||
msgs = self.listen()
|
msgs = self.crawl()
|
||||||
|
|
||||||
statuses = []
|
statuses = []
|
||||||
for msg in msgs:
|
for msg in msgs:
|
||||||
if self.trigger.is_ok(msg.get_payload()):
|
if trigger.is_ok(msg.get_payload()):
|
||||||
statuses.append(self.to_social(msg))
|
statuses.append(msg)
|
||||||
return statuses
|
return statuses
|
||||||
|
|
||||||
|
|
||||||
|
@ -159,16 +169,21 @@ if __name__ == "__main__":
|
||||||
with open('config.toml') as configfile:
|
with open('config.toml') as configfile:
|
||||||
config = toml.load(configfile)
|
config = toml.load(configfile)
|
||||||
|
|
||||||
|
# set log file
|
||||||
fh = logging.FileHandler(config['logging']['logpath'])
|
fh = logging.FileHandler(config['logging']['logpath'])
|
||||||
fh.setLevel(logging.DEBUG)
|
fh.setLevel(logging.DEBUG)
|
||||||
logger.addHandler(fh)
|
logger.addHandler(fh)
|
||||||
|
|
||||||
|
# initialise trigger
|
||||||
trigger = trigger.Trigger(config)
|
trigger = trigger.Trigger(config)
|
||||||
m = Mailbot(config, trigger)
|
|
||||||
|
# initialise mail bot
|
||||||
|
m = Mailbot(config)
|
||||||
|
|
||||||
statuses = []
|
statuses = []
|
||||||
try:
|
try:
|
||||||
while 1:
|
while 1:
|
||||||
print("Received Reports: " + str(m.flow(statuses)))
|
print("Received Reports: " + str(m.flow(trigger, statuses)))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Good bye. Remember to restart the bot!")
|
print("Good bye. Remember to restart the bot!")
|
||||||
|
|
Loading…
Reference in a new issue