2018-01-05 13:16:24 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sendmail
|
2018-01-05 16:13:41 +00:00
|
|
|
import ssl
|
2018-01-05 13:16:24 +00:00
|
|
|
import datetime
|
|
|
|
import email
|
|
|
|
import imaplib
|
2018-01-18 11:42:37 +00:00
|
|
|
import report
|
2018-03-23 16:00:52 +00:00
|
|
|
from user import User
|
2018-01-07 19:22:32 +00:00
|
|
|
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-01-05 13:16:24 +00:00
|
|
|
class Mailbot(object):
|
|
|
|
"""
|
|
|
|
Bot which sends Mails if mentioned via twitter/mastodon, and tells
|
|
|
|
other bots that it received mails.
|
|
|
|
"""
|
|
|
|
|
2018-03-23 16:00:52 +00:00
|
|
|
def __init__(self, config, logger, uid, db):
|
2018-01-05 13:16:24 +00:00
|
|
|
"""
|
|
|
|
Creates a Bot who listens to mails and forwards them to other
|
|
|
|
bots.
|
|
|
|
|
|
|
|
:param config: (dictionary) config.toml as a dictionary of dictionaries
|
|
|
|
"""
|
|
|
|
self.config = config
|
2018-03-23 16:00:52 +00:00
|
|
|
self.logger = logger
|
|
|
|
self.user = User(db, uid)
|
2018-01-05 13:16:24 +00:00
|
|
|
|
2018-03-23 16:00:52 +00:00
|
|
|
try:
|
|
|
|
self.last_mail = self.user.get_seen_mail()
|
|
|
|
except TypeError:
|
|
|
|
self.last_mail = 0
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-01-05 13:16:24 +00:00
|
|
|
try:
|
2018-03-23 16:00:52 +00:00
|
|
|
self.mailinglist = self.user.get_mail()
|
|
|
|
except TypeError:
|
2018-01-05 13:16:24 +00:00
|
|
|
self.mailinglist = None
|
|
|
|
|
|
|
|
self.mailbox = imaplib.IMAP4_SSL(self.config["mail"]["imapserver"])
|
2018-01-05 16:13:41 +00:00
|
|
|
context = ssl.create_default_context()
|
|
|
|
try:
|
|
|
|
self.mailbox.starttls(ssl_context=context)
|
|
|
|
except:
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.error('StartTLS failed', exc_info=True)
|
2018-01-05 13:16:24 +00:00
|
|
|
try:
|
2018-01-05 16:13:41 +00:00
|
|
|
self.mailbox.login(self.config["mail"]["user"], self.config["mail"]["passphrase"])
|
2018-01-05 13:16:24 +00:00
|
|
|
except imaplib.IMAP4.error:
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.error("Login to mail server failed", exc_info=True)
|
2018-01-07 19:22:32 +00:00
|
|
|
try:
|
|
|
|
mailer = sendmail.Mailer(config)
|
|
|
|
mailer.send('', config['mail']['contact'],
|
|
|
|
'Ticketfrei Crash Report',
|
|
|
|
attachment=config['logging']['logpath'])
|
|
|
|
except:
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.error('Mail sending failed', exc_info=True)
|
2018-01-05 13:16:24 +00:00
|
|
|
|
2018-01-18 12:19:11 +00:00
|
|
|
def repost(self, status):
|
2018-01-05 13:16:24 +00:00
|
|
|
"""
|
2018-01-18 11:42:37 +00:00
|
|
|
E-Mails don't have to be reposted - they already reached everyone on the mailing list.
|
2018-03-23 12:19:39 +00:00
|
|
|
The function still needs to be here because backend.py assumes it, and take the
|
2018-01-18 12:19:11 +00:00
|
|
|
report object they want to give us.
|
|
|
|
|
|
|
|
:param status: (report.Report object)
|
2018-01-18 11:42:37 +00:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def crawl(self):
|
|
|
|
"""
|
|
|
|
crawl for new mails.
|
|
|
|
:return: msgs: (list of report.Report objects)
|
2018-01-05 13:16:24 +00:00
|
|
|
"""
|
2018-01-23 08:17:26 +00:00
|
|
|
try:
|
|
|
|
rv, data = self.mailbox.select("Inbox")
|
|
|
|
except imaplib.IMAP4.abort:
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.error("Crawling Mail failed", exc_info=True)
|
2018-01-23 08:17:26 +00:00
|
|
|
rv = False
|
2018-01-05 16:13:41 +00:00
|
|
|
msgs = []
|
2018-01-05 13:16:24 +00:00
|
|
|
if rv == 'OK':
|
|
|
|
rv, data = self.mailbox.search(None, "ALL")
|
|
|
|
if rv != 'OK':
|
2018-01-05 16:13:41 +00:00
|
|
|
return msgs
|
2018-01-05 13:16:24 +00:00
|
|
|
|
|
|
|
for num in data[0].split():
|
|
|
|
rv, data = self.mailbox.fetch(num, '(RFC822)')
|
|
|
|
if rv != 'OK':
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.error("Couldn't fetch mail %s %s" % (rv, str(data)))
|
2018-01-05 16:13:41 +00:00
|
|
|
return msgs
|
2018-01-05 13:16:24 +00:00
|
|
|
msg = email.message_from_bytes(data[0][1])
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-03-23 16:00:52 +00:00
|
|
|
if not self.user.get_mail() in msg['From']:
|
2018-01-18 13:23:11 +00:00
|
|
|
# get a comparable date out of the email
|
|
|
|
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
|
|
|
date_tuple = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
|
2018-03-23 16:00:52 +00:00
|
|
|
date = int((date_tuple - datetime.datetime(1970, 1, 1)).total_seconds())
|
|
|
|
if date > self.user.get_seen_mail():
|
2018-01-18 13:23:11 +00:00
|
|
|
self.last_mail = date
|
|
|
|
self.save_last()
|
|
|
|
msgs.append(self.make_report(msg))
|
2018-01-05 16:13:41 +00:00
|
|
|
return msgs
|
|
|
|
|
2018-01-18 12:06:53 +00:00
|
|
|
def save_last(self):
|
2018-03-23 16:00:52 +00:00
|
|
|
""" Saves the last retweeted tweet in the db. """
|
|
|
|
self.user.save_seen_mail(self.last_mail)
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-01-18 12:42:23 +00:00
|
|
|
def post(self, status):
|
2018-01-05 13:16:24 +00:00
|
|
|
"""
|
2018-01-18 11:42:37 +00:00
|
|
|
sends reports by other sources to a mailing list.
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-01-18 12:42:23 +00:00
|
|
|
:param status: (report.Report object)
|
2018-01-05 13:16:24 +00:00
|
|
|
"""
|
2018-01-18 12:42:23 +00:00
|
|
|
mailer = sendmail.Mailer(self.config)
|
|
|
|
mailer.send(status.format(), self.mailinglist, "Warnung: Kontrolleure gesehen")
|
2018-01-05 13:16:24 +00:00
|
|
|
|
2018-01-18 11:42:37 +00:00
|
|
|
def make_report(self, msg):
|
2018-01-05 16:13:41 +00:00
|
|
|
"""
|
2018-01-18 11:42:37 +00:00
|
|
|
generates a report out of a mail
|
|
|
|
|
2018-01-05 16:13:41 +00:00
|
|
|
:param msg: email.parser.Message object
|
2018-01-18 11:42:37 +00:00
|
|
|
:return: post: report.Report object
|
2018-01-05 16:13:41 +00:00
|
|
|
"""
|
|
|
|
# get a comparable date out of the email
|
|
|
|
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
|
|
|
date_tuple = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
|
|
|
|
date = (date_tuple-datetime.datetime(1970,1,1)).total_seconds()
|
|
|
|
|
|
|
|
author = msg.get("From") # get mail author from email header
|
|
|
|
# :todo take only the part before the @
|
|
|
|
|
|
|
|
text = msg.get_payload()
|
2018-01-18 11:42:37 +00:00
|
|
|
post = report.Report(author, "mail", text, None, date)
|
2018-01-05 16:13:41 +00:00
|
|
|
self.last_mail = date
|
2018-01-18 12:06:53 +00:00
|
|
|
self.save_last()
|
2018-01-05 16:13:41 +00:00
|
|
|
return post
|
|
|
|
|
2018-01-18 11:42:37 +00:00
|
|
|
def flow(self, trigger, statuses):
|
2018-01-05 16:13:41 +00:00
|
|
|
"""
|
2018-01-18 11:42:37 +00:00
|
|
|
to be iterated. uses trigger to separate the sheep from the goats
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-01-18 11:42:37 +00:00
|
|
|
:param statuses: (list of report.Report objects)
|
|
|
|
:return: statuses: (list of report.Report objects)
|
2018-01-05 16:13:41 +00:00
|
|
|
"""
|
2018-01-18 13:23:11 +00:00
|
|
|
for status in statuses:
|
|
|
|
self.post(status)
|
2018-01-05 16:13:41 +00:00
|
|
|
|
2018-01-18 11:42:37 +00:00
|
|
|
msgs = self.crawl()
|
2018-01-05 16:13:41 +00:00
|
|
|
|
|
|
|
statuses = []
|
|
|
|
for msg in msgs:
|
2018-01-18 11:42:37 +00:00
|
|
|
if trigger.is_ok(msg.get_payload()):
|
|
|
|
statuses.append(msg)
|
2018-01-05 16:13:41 +00:00
|
|
|
return statuses
|
|
|
|
|
2018-03-23 16:07:00 +00:00
|
|
|
"""
|
2018-01-05 13:16:24 +00:00
|
|
|
if __name__ == "__main__":
|
2018-03-23 16:07:00 +00:00
|
|
|
config = prepare.get_config()
|
2018-01-05 13:16:24 +00:00
|
|
|
|
2018-01-18 11:42:37 +00:00
|
|
|
# initialise trigger
|
2018-01-07 19:22:32 +00:00
|
|
|
trigger = trigger.Trigger(config)
|
2018-01-18 11:42:37 +00:00
|
|
|
|
|
|
|
# initialise mail bot
|
|
|
|
m = Mailbot(config)
|
|
|
|
|
2018-01-05 16:13:41 +00:00
|
|
|
statuses = []
|
|
|
|
try:
|
|
|
|
while 1:
|
2018-01-18 11:42:37 +00:00
|
|
|
print("Received Reports: " + str(m.flow(trigger, statuses)))
|
2018-01-05 16:13:41 +00:00
|
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Good bye. Remember to restart the bot!")
|
|
|
|
except:
|
2018-03-23 10:18:00 +00:00
|
|
|
m.logger.error('Shutdown', exc_info=True)
|
2018-01-18 12:06:53 +00:00
|
|
|
m.save_last()
|
2018-01-07 19:22:32 +00:00
|
|
|
try:
|
|
|
|
mailer = sendmail.Mailer(config)
|
|
|
|
mailer.send('', config['mail']['contact'],
|
|
|
|
'Ticketfrei Crash Report',
|
|
|
|
attachment=config['logging']['logpath'])
|
|
|
|
except:
|
2018-03-23 10:18:00 +00:00
|
|
|
m.logger.error('Mail sending failed', exc_info=True)
|
2018-03-23 16:07:00 +00:00
|
|
|
"""
|