changing mail to use db, part 1; seen mails

This commit is contained in:
b3yond 2018-03-23 17:00:52 +01:00
parent 570792ba37
commit a65d410e4f
5 changed files with 47 additions and 38 deletions

View file

@ -8,7 +8,7 @@ from db import DB
from retootbot import RetootBot
# from retweetbot import RetweetBot
# from mailbot import Mailbot
from mailbot import Mailbot
from trigger import Trigger
@ -24,7 +24,7 @@ def init_bots(config, logger, db, users):
for uid in users:
users[uid].append(RetootBot(config, logger, uid, db))
# users[uid].append(RetweetBot(config, uid, db))
# users[uid].append(Mailbot(config, uid, db))
users[uid].append(Mailbot(config, logger, uid, db))
return users
@ -65,7 +65,7 @@ def run():
bot.save_last()
mailer = sendmail.Mailer(config)
try:
mailer.send('', config['mail']['contact'],
mailer.send('', config['web']['contact'],
'Ticketfrei Crash Report',
attachment=config['logging']['logpath'])
except:

10
db.py
View file

@ -14,7 +14,7 @@ class DB(object):
self.config = prepare.get_config()
self.logger = prepare.get_logger(self.config)
dbfile = path.join(path.dirname(path.abspath(__file__)),
'ticketfrei.sqlite')
self.config['database']['db_path'])
self.conn = sqlite3.connect(dbfile)
self.cur = self.conn.cursor()
self.create()
@ -85,6 +85,14 @@ class DB(object):
active INTEGER,
FOREIGN KEY(user_id) REFERENCES user(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)
);
CREATE TABLE IF NOT EXISTS seen_tweets (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,

View file

@ -6,9 +6,10 @@ import time
import trigger
import datetime
import email
import backend
import prepare
import imaplib
import report
from user import User
class Mailbot(object):
@ -17,7 +18,7 @@ class Mailbot(object):
other bots that it received mails.
"""
def __init__(self, config, history_path="last_mail"):
def __init__(self, config, logger, uid, db):
"""
Creates a Bot who listens to mails and forwards them to other
bots.
@ -25,14 +26,17 @@ class Mailbot(object):
:param config: (dictionary) config.toml as a dictionary of dictionaries
"""
self.config = config
self.logger = backend.get_logger(config)
self.history_path = history_path
self.last_mail = self.get_history(self.history_path)
self.logger = logger
self.user = User(db, uid)
try:
self.mailinglist = self.config["mail"]["list"]
except KeyError:
self.last_mail = self.user.get_seen_mail()
except TypeError:
self.last_mail = 0
try:
self.mailinglist = self.user.get_mail()
except TypeError:
self.mailinglist = None
self.mailbox = imaplib.IMAP4_SSL(self.config["mail"]["imapserver"])
@ -86,19 +90,18 @@ class Mailbot(object):
return msgs
msg = email.message_from_bytes(data[0][1])
if not self.config['mail']['user'] + "@" + \
self.config["mail"]["mailserver"].partition(".")[2] in msg['From']:
if not self.user.get_mail() in msg['From']:
# 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()
if date > self.get_history(self.history_path):
date = int((date_tuple - datetime.datetime(1970, 1, 1)).total_seconds())
if date > self.user.get_seen_mail():
self.last_mail = date
self.save_last()
msgs.append(self.make_report(msg))
return msgs
def get_history(self, path):
def get_history(self):
"""
This counter is needed to keep track of your mails, so you
don't double parse them
@ -107,19 +110,11 @@ class Mailbot(object):
last_mail is stored.
:return: last_mail: ID of the last mail the bot parsed
"""
try:
with open(path, "r+") as f:
last_mail = f.read()
except IOError:
with open(path, "w+") as f:
last_mail = "0"
f.write(last_mail)
return float(last_mail)
pass
def save_last(self):
""" Saves the last retweeted tweet in last_mention. """
with open(self.history_path, "w") as f:
f.write(str(self.last_mail))
""" Saves the last retweeted tweet in the db. """
self.user.save_seen_mail(self.last_mail)
def post(self, status):
"""

View file

@ -46,7 +46,7 @@ class RetootBot(object):
self.save_last()
# add mention to mentions
text = re.sub(r'<[^>]*>', '', status['status']['content'])
text = re.sub("(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", text)
text = re.sub("(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", text)
mentions.append(report.Report(status['account']['acct'],
"mastodon",
text,
@ -64,7 +64,6 @@ class RetootBot(object):
mention.format()))
self.m.status_reblog(mention.id)
def post(self, report):
"""
Toots a report from other sources.

23
user.py
View file

@ -18,18 +18,25 @@ class User(object):
instance = self.db.cur.fetchone()
return instance[1], instance[2], row[0], instance[0]
def get_mastodon_account_id(self):
self.db.cur.execute("SELECT id FROM mastodon_accounts WHERE user_id = ?;", (self.uid, ))
return self.db.cur.fetchone()[0]
def get_seen_toot(self):
self.db.cur.execute("SELECT toot_id FROM seen_toots WHERE user_id = ? AND mastodon_accounts_id = ?;",
(self.uid, self.get_mastodon_account_id()))
self.db.cur.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;",
(self.uid, ))
return self.db.cur.fetchone()[0]
def save_seen_toot(self, toot_id):
self.db.cur.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ? AND mastodon_accounts_id = ?;",
(toot_id, self.uid, self.get_mastodon_account_id()))
self.db.cur.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;",
(toot_id, self.uid))
def get_mail(self):
self.db.cur.execute("SELECT email FROM mail WHERE user_id = ?;", (self.uid, ))
def get_seen_mail(self):
self.db.cur.execute("SELECT mail_date FROM seen_mails WHERE user_id = ?;", (self.uid, ))
return self.db.cur.fetchone()[0]
def save_seen_mail(self, mail_date):
self.db.cur.execute("UPDATE seen_mail SET mail_date = ? WHERE user_id = ?;",
(mail_date, self.uid))
def state(self):
return dict(foo='bar')