From d8b2b811dab497c4f7c516ef39c17861e71db129 Mon Sep 17 00:00:00 2001 From: b3yond Date: Thu, 29 Mar 2018 00:13:00 +0200 Subject: [PATCH] started mail rewrite --- active_bots/mailbot.py | 17 ++++++----------- db.py | 10 +--------- user.py | 5 +++-- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/active_bots/mailbot.py b/active_bots/mailbot.py index a11231f..b5a57ed 100644 --- a/active_bots/mailbot.py +++ b/active_bots/mailbot.py @@ -9,12 +9,13 @@ import email import imaplib import report from user import User +from bot import Bot logger = logging.getLogger(__name__) -class Mailbot(object): +class Mailbot(Bot): """ Bot which sends Mails if mentioned via twitter/mastodon, and tells other bots that it received mails. @@ -26,15 +27,8 @@ class Mailbot(object): bots. """ - self.user = User(uid) - try: - self.last_mail = self.user.get_seen_mail() - except TypeError: - self.last_mail = 0 - - try: - self.mailinglist = self.user.get_mail() + self.mailinglist = self.user.get_mailinglist() except TypeError: self.mailinglist = None @@ -67,11 +61,12 @@ class Mailbot(object): """ pass - def crawl(self): + def crawl(self, user): """ crawl for new mails. :return: msgs: (list of report.Report objects) """ + mailinglist = user.get_mailinglist() try: rv, data = self.mailbox.select("Inbox") except imaplib.IMAP4.abort: @@ -90,7 +85,7 @@ class Mailbot(object): return msgs msg = email.message_from_bytes(data[0][1]) - if not self.user.get_mail() in msg['From']: + if not self.user.get_mailinglist() in msg['From']: # get a comparable date out of the email date_tuple = email.utils.parsedate_tz(msg['Date']) date_tuple = datetime.datetime.fromtimestamp( diff --git a/db.py b/db.py index 4b62586..5a090f8 100644 --- a/db.py +++ b/db.py @@ -97,7 +97,7 @@ class DB(object): tweet_id TEXT, FOREIGN KEY(user_id) REFERENCES user(id) ); - CREATE TABLE IF NOT EXISTS mail ( + CREATE TABLE IF NOT EXISTS mailinglist ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, email TEXT, @@ -106,14 +106,6 @@ class DB(object): FOREIGN KEY(twitter_accounts_id) REFERENCES twitter_accounts(id) ); - CREATE TABLE IF NOT EXISTS seen_mails ( - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - user_id INTEGER, - mail_id INTEGER, - mail_date INTEGER, - FOREIGN KEY(user_id) REFERENCES user(id) - FOREIGN KEY(mail_id) REFERENCES mail(id) - ); ''') def user_token(self, email, password): diff --git a/user.py b/user.py index e4edf10..703f9ab 100644 --- a/user.py +++ b/user.py @@ -108,8 +108,9 @@ class User(object): db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;", (tweet_id, self.uid)) - def get_mail(self): - db.execute("SELECT email FROM mail WHERE user_id = ?;", (self.uid, )) + def get_mailinglist(self): + db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, )) + return db.cur.fetchone()[0] def get_seen_mail(self): db.execute("SELECT mail_date FROM seen_mails WHERE user_id = ?;", (self.uid, ))