diff --git a/db.py b/db.py index 72217ad..9ace8fd 100644 --- a/db.py +++ b/db.py @@ -6,15 +6,13 @@ from os import path, urandom from pylibscrypt import scrypt_mcf, scrypt_mcf_check import sqlite3 import pytoml as toml -import sendmail -import smtplib +from mastodon import Mastodon class DB(object): def __init__(self): dbfile = path.join(path.dirname(path.abspath(__file__)), 'ticketfrei.sqlite') - #dbfile = ':memory:' self.conn = sqlite3.connect(dbfile) self.cur = self.conn.cursor() self.cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user';") @@ -132,13 +130,6 @@ class DB(object): return None return User(self, row[0]) - def send_confirmation_mail(self, confirm_link, email): - m = sendmail.Mailer(self.config) - try: - m.send("Complete your registration here: " + confirm_link, email, "[Ticketfrei] Confirm your account") - except smtplib.SMTPRecipientsRefused: - return "Please enter a valid E-Mail address." - def close(self): self.conn.close() @@ -153,6 +144,44 @@ class User(object): def state(self): return dict(foo='bar') + def save_request_token(self, token): + self.db.cur.execute("INSERT INTO twitter_request_tokens(user_id, request_token) VALUES(?, ?);", + (self.uid, token)) + self.db.conn.commit() + + def get_request_token(self): + self.db.cur.execute("SELECT request_token FROM twitter_request_tokens WHERE user_id = ?;", (id,)) + request_token = self.db.cur.fetchone()[0] + self.db.cur.execute("DELETE FROM twitter_request_tokens WHERE user_id = ?;", (id,)) + self.db.conn.commit() + return request_token + + def save_twitter_token(self, access_token, access_token_secret): + self.db.cur.execute( + "INSERT INTO twitter_accounts(user_id, access_token_key, access_token_secret) VALUES(?, ?, ?);", + (id, access_token, access_token_secret)) + self.db.conn.commit() + + def get_mastodon_app_keys(self, instance): + self.db.cur.execute("SELECT client_id, client_secret FROM mastodon_instances WHERE instance = ?;", (instance, )) + try: + client_id, client_secret = self.db.cur.fetchone()[0] + return client_id, client_secret + except TypeError: + app_name = "ticketfrei" + str(self.db.secret)[0:4] + client_id, client_secret = Mastodon.create_app(app_name, api_base_url=instance) + self.db.cur.execute("INSERT INTO mastodon_instances(instance, client_id, client_secret) VALUES(?, ?, ?);", + (instance, client_id, client_secret)) + self.db.conn.commit() + return client_id, client_secret + + def save_masto_token(self, access_token, instance): + self.db.cur.execute("SELECT id FROM mastodon_instances WHERE instance = ?;", (instance, )) + instance_id = self.db.cur.fetchone()[0] + self.db.cur.execute("INSERT INTO mastodon_accounts(user_id, access_token, mastodon_instances_id, active) " + "VALUES(?, ?, ?, ?);", (self.uid, access_token, instance_id, 1)) + self.db.commit() + class DBPlugin(object): name = 'DBPlugin' diff --git a/template/settings.tpl b/template/settings.tpl index c77ec72..1d34116 100644 --- a/template/settings.tpl +++ b/template/settings.tpl @@ -16,6 +16,12 @@