IRCat/modules/sqlite_local.py

39 lines
2 KiB
Python
Raw Normal View History

2025-01-08 21:28:58 -08:00
# IRCat module for local SQLite database (default)
2025-01-21 18:38:37 -08:00
import sqlite3, os, traceback
2025-01-21 19:01:09 -08:00
from cryptography.fernet import Fernet
2025-01-08 21:32:41 -08:00
__ircat_type__ = "sql.provider" # The type of module
2025-01-21 18:38:37 -08:00
__ircat_requires__ = ["data-path", "fernet-key"] # The required config.yml entries.
2025-01-08 22:13:43 -08:00
class broker:
2025-01-21 18:38:37 -08:00
def __init__(self, data_path, fernet_key):
2025-01-08 21:28:58 -08:00
if not os.path.isfile(data_path):
print("Creating database file...")
open(data_path, "w").write("")
2025-01-21 19:57:54 -08:00
self.conn = sqlite3.connect(data_path, check_same_thread=False)
2025-01-21 18:38:37 -08:00
self.fnet = Fernet(fernet_key)
2025-01-08 21:28:58 -08:00
db = self.conn.cursor()
2025-01-21 19:03:21 -08:00
db.execute("""CREATE table IF NOT EXISTS bans (ip varchar(255), reason varchar(255))""")
2025-01-22 19:13:36 -08:00
db.execute("""CREATE table IF NOT EXISTS nickserv (user varchar(255), modes varchar(255), hash varchar(255), email varchar(255))""")
2025-01-08 21:28:58 -08:00
db.execute("""CREATE table IF NOT EXISTS groups (name varchar(255), owner varchar(255))""")
db.execute("""CREATE table IF NOT EXISTS chanserv (name varchar(255), modes varchar(255), params varchar(255), owner varchar(255), usermodes varchar(255), optimodes varchar(255))""")
2025-01-21 18:38:37 -08:00
def nickserv_identify(self, nick, password:str):
2025-01-08 21:28:58 -08:00
db = self.conn.cursor()
db.execute("SELECT * FROM nickserv WHERE user=?;", [nick])
e = db.fetchall()
if e == []:
2025-01-21 18:38:37 -08:00
return False
2025-01-08 21:28:58 -08:00
else:
2025-01-21 18:38:37 -08:00
try:
2025-01-21 20:53:15 -08:00
return e[0] if self.fnet.decrypt(bytes(e[0][2], "UTF-8")).decode() == password else False
2025-01-21 18:38:37 -08:00
except:
print(traceback.format_exc())
2025-01-21 20:16:48 -08:00
return False
def nickserv_register(self, nick, password, email):
2025-01-21 20:46:41 -08:00
hashed = self.fnet.encrypt(bytes(password, "UTF-8")).decode()
2025-01-21 20:16:48 -08:00
db = self.conn.cursor()
2025-01-22 20:21:06 -08:00
db.execute("INSERT INTO nickserv values(?, 'iw', ?, ?);", [nick, hashed, email])
2025-01-22 20:23:57 -08:00
self.conn.commit()
def nickserv_isexist(self, nick):
db = self.conn.cursor()
db.execute("SELECT * FROM nickserv WHERE user=?;", [nick])
e = db.fetchall()
return e != []