<commit_before>#!/usr/bin/env python3

import logging
import sendmail
import datetime
import mailbox
import email
import report
from bot import Bot
from config import config
from db import db

logger = logging.getLogger(__name__)


class Mailbot(Bot):

    # returns a list of Report objects
    def crawl(self, user):
        reports = []
        # todo: adjust to actual mailbox
        mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user'])
        for msg in mails:
            if get_date_from_header(msg['Date']) > user.get_seen_mail():
                if user.get_city().lower() in msg['To'].lower():
                    reports.append(make_report(msg.get_payload(), user))
        return reports

    # post/boost Report object
    def post(self, user, report):
        recipients = user.get_mailinglist()
        for rec in recipients:
            rec = rec[0]
            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/" \
                   + db.mail_subscription_token(rec, user.get_city())
            if report.author != rec:
                try:
                    sendmail.sendmail(rec, "Ticketfrei " + user.get_city() +
                                      " Report", body=body)
                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
    date = get_date_from_header(msg['Date'])

    author = msg['From']  # get mail author from email header<commit_msg>payload is extracted later, we need the message object here<commit_after>#!/usr/bin/env python3

import logging
import sendmail
import datetime
import mailbox
import email
import report
from bot import Bot
from config import config
from db import db

logger = logging.getLogger(__name__)


class Mailbot(Bot):

    # returns a list of Report objects
    def crawl(self, user):
        reports = []
        # todo: adjust to actual mailbox
        mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user'])
        for msg in mails:
            if get_date_from_header(msg['Date']) > user.get_seen_mail():
                if user.get_city().lower() in msg['To'].lower():
                    reports.append(make_report(msg, user))
        return reports

    # post/boost Report object
    def post(self, user, report):
        recipients = user.get_mailinglist()
        for rec in recipients:
            rec = rec[0]
            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/" \
                   + db.mail_subscription_token(rec, user.get_city())
            if report.author != rec:
                try:
                    sendmail.sendmail(rec, "Ticketfrei " + user.get_city() +
                                      " Report", body=body)
                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
    date = get_date_from_header(msg['Date'])

    author = msg['From']  # get mail author from email header
{'fraction_non_alphanumeric': 0.10396039603960396, 'fraction_numerical': 0.0011001100110011, 'mean_word_length': 2.2736273627362737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '475450', 'n_tokens_mistral': 1013, 'n_tokens_neox': 978, 'n_words': 304}