2018-03-28 15:36:35 +00:00
|
|
|
from config import config
|
2018-03-22 01:23:31 +00:00
|
|
|
import jwt
|
2018-03-24 15:26:35 +00:00
|
|
|
import logging
|
2018-03-24 14:02:11 +00:00
|
|
|
from os import urandom
|
2018-03-28 15:36:35 +00:00
|
|
|
from pylibscrypt import scrypt_mcf
|
2018-03-22 01:23:31 +00:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
2018-03-24 15:26:35 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-03-22 01:23:31 +00:00
|
|
|
class DB(object):
|
2018-03-24 14:02:11 +00:00
|
|
|
def __init__(self, dbfile):
|
2018-03-22 01:23:31 +00:00
|
|
|
self.conn = sqlite3.connect(dbfile)
|
|
|
|
self.cur = self.conn.cursor()
|
2018-03-22 20:15:15 +00:00
|
|
|
self.create()
|
2018-03-22 01:23:31 +00:00
|
|
|
self.secret = urandom(32)
|
|
|
|
|
2018-03-28 15:36:35 +00:00
|
|
|
def execute(self, *args, **kwargs):
|
|
|
|
return self.cur.execute(*args, **kwargs)
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
2018-03-22 01:23:31 +00:00
|
|
|
def create(self):
|
|
|
|
# init db
|
2018-03-22 20:15:15 +00:00
|
|
|
self.cur.executescript('''
|
|
|
|
CREATE TABLE IF NOT EXISTS user (
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
passhash TEXT,
|
|
|
|
enabled INTEGER DEFAULT 1
|
|
|
|
);
|
2018-03-28 15:36:35 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS email (
|
2018-03-22 20:15:15 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
2018-03-28 15:36:35 +00:00
|
|
|
user_id INTEGER,
|
|
|
|
email TEXT,
|
2018-03-22 20:15:15 +00:00
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
|
|
|
);
|
2018-03-28 15:36:35 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS triggerpatterns (
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
|
|
|
pattern TEXT,
|
2018-03-22 20:15:15 +00:00
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
|
|
|
);
|
2018-03-28 15:36:35 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS badwords (
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
|
|
|
word TEXT,
|
2018-03-22 20:15:15 +00:00
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS mastodon_instances (
|
2018-03-28 15:36:35 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
2018-03-22 20:15:15 +00:00
|
|
|
instance TEXT,
|
|
|
|
client_id TEXT,
|
|
|
|
client_secret TEXT
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS mastodon_accounts (
|
2018-03-28 15:36:35 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
|
|
|
access_token TEXT,
|
|
|
|
instance_id INTEGER,
|
2018-03-22 20:15:15 +00:00
|
|
|
active INTEGER,
|
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id),
|
|
|
|
FOREIGN KEY(instance_id) REFERENCES mastodon_instances(id)
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS seen_toots (
|
2018-03-28 15:36:35 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
2018-03-22 20:15:15 +00:00
|
|
|
mastodon_accounts_id INTEGER,
|
2018-03-28 15:36:35 +00:00
|
|
|
toot_id TEXT,
|
2018-03-22 20:15:15 +00:00
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id),
|
|
|
|
FOREIGN KEY(mastodon_accounts_id)
|
|
|
|
REFERENCES mastodon_accounts(id)
|
|
|
|
);
|
2018-03-28 15:36:35 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS twitter_request_tokens (
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
|
|
|
request_token TEXT,
|
2018-03-22 20:15:15 +00:00
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
|
|
|
);
|
2018-03-28 15:36:35 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS twitter_accounts (
|
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
|
|
|
client_id TEXT,
|
|
|
|
client_secret TEXT,
|
|
|
|
active INTEGER,
|
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
2018-03-23 16:00:52 +00:00
|
|
|
);
|
2018-03-22 20:15:15 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS seen_tweets (
|
2018-03-28 15:36:35 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
2018-03-22 20:15:15 +00:00
|
|
|
twitter_accounts_id INTEGER,
|
|
|
|
tweet_id TEXT,
|
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
2018-03-28 22:12:19 +00:00
|
|
|
FOREIGN KEY(twitter_accounts_id)
|
|
|
|
REFERENCES twitter_accounts(id)
|
2018-03-28 15:36:35 +00:00
|
|
|
);
|
2018-03-28 22:13:00 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS mailinglist (
|
2018-03-28 15:36:35 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
|
|
user_id INTEGER,
|
|
|
|
email TEXT,
|
|
|
|
active INTEGER,
|
|
|
|
FOREIGN KEY(user_id) REFERENCES user(id)
|
2018-03-22 20:15:15 +00:00
|
|
|
);
|
|
|
|
''')
|
2018-03-22 01:23:31 +00:00
|
|
|
|
2018-03-28 15:36:35 +00:00
|
|
|
def user_token(self, email, password):
|
2018-03-22 01:23:31 +00:00
|
|
|
return jwt.encode({
|
2018-03-28 15:36:35 +00:00
|
|
|
'email': email,
|
|
|
|
'passhash': scrypt_mcf(
|
|
|
|
password.encode('utf-8')
|
|
|
|
).decode('ascii')
|
2018-03-22 01:23:31 +00:00
|
|
|
}, self.secret).decode('ascii')
|
|
|
|
|
2018-03-28 15:36:35 +00:00
|
|
|
def confirm(self, token):
|
2018-03-28 22:12:19 +00:00
|
|
|
from user import User
|
2018-03-28 15:36:35 +00:00
|
|
|
try:
|
|
|
|
json = jwt.decode(token, self.secret)
|
|
|
|
except jwt.DecodeError:
|
|
|
|
return None # invalid token
|
|
|
|
if 'passhash' in json.keys():
|
|
|
|
# create user
|
|
|
|
self.execute("INSERT INTO user (passhash) VALUES(?, ?);",
|
|
|
|
(json['passhash'], ))
|
|
|
|
uid = self.cur.lastrowid
|
|
|
|
else:
|
|
|
|
uid = json['uid']
|
|
|
|
self.execute("INSERT INTO email (user_id, email) VALUES(?, ?);",
|
|
|
|
(uid, json['email']))
|
|
|
|
self.commit()
|
|
|
|
return User(uid)
|
2018-03-22 01:23:31 +00:00
|
|
|
|
|
|
|
def by_email(self, email):
|
2018-03-28 22:12:19 +00:00
|
|
|
from user import User
|
2018-03-28 15:36:35 +00:00
|
|
|
self.execute("SELECT user_id FROM email WHERE email=?;", (email, ))
|
|
|
|
try:
|
|
|
|
uid, = self.cur.fetchone()
|
|
|
|
except TypeError:
|
2018-03-22 01:23:31 +00:00
|
|
|
return None
|
2018-03-28 15:36:35 +00:00
|
|
|
return User(uid)
|
2018-03-22 01:23:31 +00:00
|
|
|
|
2018-03-28 15:36:35 +00:00
|
|
|
@property
|
|
|
|
def active_users(self):
|
2018-03-28 22:12:19 +00:00
|
|
|
from user import User
|
2018-03-28 15:36:35 +00:00
|
|
|
self.execute("SELECT id FROM user WHERE enabled=1;")
|
|
|
|
return [User(uid) for uid, in self.cur.fetchall()]
|
2018-03-22 01:23:31 +00:00
|
|
|
|
|
|
|
|
2018-03-28 15:36:35 +00:00
|
|
|
db = DB(config['database']['db_path'])
|