2018-08-08 12:04:31 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
2018-10-06 08:20:37 +00:00
|
|
|
from sendmail import sendmail
|
2018-08-08 12:04:31 +00:00
|
|
|
import datetime
|
2018-08-08 15:09:26 +00:00
|
|
|
import mailbox
|
2018-08-08 12:04:31 +00:00
|
|
|
import email
|
|
|
|
import report
|
|
|
|
from bot import Bot
|
2018-08-09 13:01:51 +00:00
|
|
|
from config import config
|
|
|
|
from db import db
|
2018-08-08 12:04:31 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Mailbot(Bot):
|
|
|
|
|
|
|
|
# returns a list of Report objects
|
|
|
|
def crawl(self, user):
|
|
|
|
reports = []
|
2018-09-14 10:45:49 +00:00
|
|
|
# todo: adjust to actual mailbox
|
2018-09-23 16:53:46 +00:00
|
|
|
mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user'])
|
2018-08-08 12:04:31 +00:00
|
|
|
for msg in mails:
|
2018-08-08 15:09:26 +00:00
|
|
|
if get_date_from_header(msg['Date']) > user.get_seen_mail():
|
2018-09-24 20:40:29 +00:00
|
|
|
if user.get_city().lower() in msg['To'].lower():
|
2018-10-05 08:56:34 +00:00
|
|
|
reports.append(make_report(msg, user))
|
2018-08-08 12:04:31 +00:00
|
|
|
return reports
|
|
|
|
|
|
|
|
# post/boost Report object
|
|
|
|
def post(self, user, report):
|
|
|
|
recipients = user.get_mailinglist()
|
|
|
|
for rec in recipients:
|
|
|
|
rec = rec[0]
|
2018-08-09 13:01:51 +00:00
|
|
|
unsubscribe_text = "\n_______\nYou don't want to receive those messages? Unsubscribe with this link: "
|
|
|
|
body = report.text + unsubscribe_text + config['web']['host'] + "/city/mail/unsubscribe/" \
|
2018-09-15 16:47:43 +00:00
|
|
|
+ db.mail_subscription_token(rec, user.get_city())
|
2018-10-08 13:09:18 +00:00
|
|
|
if rec not in report.author:
|
2018-08-08 12:04:31 +00:00
|
|
|
try:
|
2018-10-06 00:46:54 +00:00
|
|
|
city = user.get_city()
|
2018-10-06 08:20:37 +00:00
|
|
|
sendmail(rec, "Ticketfrei " + city + " Report",
|
2018-10-06 00:46:54 +00:00
|
|
|
city=city, body=body)
|
2018-08-08 12:04:31 +00:00
|
|
|
except Exception:
|
|
|
|
logger.error("Sending Mail failed.", exc_info=True)
|
|
|
|
|
|
|
|
|
|
|
|
def make_report(msg, user):
|
|
|
|
"""
|
|
|
|
generates a report out of a mail
|
|
|
|
|
|
|
|
:param msg: email.parser.Message object
|
|
|
|
:return: post: report.Report object
|
|
|
|
"""
|
|
|
|
# get a comparable date out of the email
|
2018-08-08 15:09:26 +00:00
|
|
|
date = get_date_from_header(msg['Date'])
|
2018-08-08 12:04:31 +00:00
|
|
|
|
2018-08-08 15:09:26 +00:00
|
|
|
author = msg['From'] # get mail author from email header
|
2018-08-08 12:04:31 +00:00
|
|
|
|
2018-10-07 16:47:37 +00:00
|
|
|
if msg.is_multipart():
|
|
|
|
text = []
|
|
|
|
for part in msg.get_payload():
|
|
|
|
if part.get_content_type() == "text":
|
|
|
|
text.append(part.get_payload())
|
2018-10-08 13:02:15 +00:00
|
|
|
elif part.get_content_type() == "application/pgp-signature":
|
|
|
|
pass # ignore PGP signatures
|
2018-10-07 16:47:37 +00:00
|
|
|
elif part.get_content_type() == "multipart/mixed":
|
|
|
|
for p in part:
|
2018-10-07 21:27:06 +00:00
|
|
|
if isinstance(p, str):
|
|
|
|
text.append(p)
|
2018-10-07 21:28:29 +00:00
|
|
|
elif p.get_content_type() == "text":
|
2018-10-07 16:47:37 +00:00
|
|
|
text.append(part.get_payload())
|
|
|
|
else:
|
|
|
|
logger.error("unknown MIMEtype: " +
|
|
|
|
p.get_content_type())
|
|
|
|
else:
|
|
|
|
logger.error("unknown MIMEtype: " +
|
|
|
|
part.get_content_type())
|
|
|
|
text = '\n'.join(text)
|
|
|
|
else:
|
|
|
|
text = msg.get_payload()
|
2018-08-08 12:04:31 +00:00
|
|
|
post = report.Report(author, "mail", text, None, date)
|
|
|
|
user.save_seen_mail(date)
|
|
|
|
return post
|
2018-08-08 15:09:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_date_from_header(header):
|
|
|
|
"""
|
|
|
|
:param header: msg['Date']
|
|
|
|
:return: float: total seconds
|
|
|
|
"""
|
|
|
|
date_tuple = email.utils.parsedate_tz(header)
|
|
|
|
date_tuple = datetime.datetime.fromtimestamp(
|
|
|
|
email.utils.mktime_tz(date_tuple)
|
|
|
|
)
|
|
|
|
return (date_tuple - datetime.datetime(1970, 1, 1)).total_seconds()
|