2018-08-08 12:04:31 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sendmail
|
|
|
|
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-09-25 09:06:08 +00:00
|
|
|
reports.append(make_report(msg.get_payload(), 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-08-08 12:04:31 +00:00
|
|
|
if report.author != rec:
|
|
|
|
try:
|
|
|
|
sendmail.sendmail(rec, "Ticketfrei " + user.get_city() +
|
2018-08-08 15:09:26 +00:00
|
|
|
" Report", 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
|
|
|
|
# :todo take only the part in between the < >
|
2018-08-08 12:04:31 +00:00
|
|
|
|
|
|
|
text = msg.get_payload()
|
|
|
|
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()
|