def UserIsBanned(chatid: int) -> tuple: sql = "SELECT state, reason FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) result = cursor.fetchone() if result is not None: return bool(result[0]), result[1] else: return False, "User not found"
def GetAttempts(chatid: int) -> int: sql = "SELECT attempts FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) result = cursor.fetchone() if result is not None: return int(result[0]) else: return 5
def VerifyToken(token: str) -> tuple: sql = "SELECT name FROM tokens WHERE token=?" hsh = TokenToHash(token) cursor.execute(sql, (hsh, )) result = cursor.fetchone() if result is not None: return True, result[0] else: return False, "Unknown"
def TokenContainsPermission(token_name: str, permission: str) -> bool: sql = "SELECT permissions FROM tokens WHERE name=?" cursor.execute(sql, (token_name, )) result = cursor.fetchone() # Check if token exists if not result: return False # Check root perms if result[0] == "*": return True # Check other perms else: return permission in loads(result[0])
def TokenDelete(name: str) -> bool: # Check if token exists sql = "SELECT id FROM tokens WHERE name=?" cursor.execute(sql, (name, )) result = cursor.fetchone() # Delete token if result is not None: lock.acquire(True) sql = "DELETE FROM tokens WHERE id=?" cursor.execute(sql, (result[0], )) connection.commit() lock.release() return True else: return False
def EnumerateBannedUsers() -> str: result = "Banned users list\n" sql = "SELECT chatid, username, reason FROM banned WHERE state=1 OR attempts=0" cursor.execute(sql) users = cursor.fetchall() # No banned users if len(users) == 0: return "There is no banned users" # Enum for user in users: chatid = user[0] username = user[1] reason = user[2] result += f"CHATID: {chatid},\nREASON: {reason},\nUSERNAME: {username}\n\n" return result
def SetAttempts(chatid: int, username: str = "Unknown", attempts: int = 5) -> str: lock.acquire(True) sql = "SELECT id FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) # Change attempts count to user result = cursor.fetchone() if result is not None: sql = "UPDATE banned SET attempts=?, username=? WHERE chatid=?" else: sql = "INSERT INTO banned (attempts, username, chatid) VALUES (?, ?, ?)" # Execute sql & commit changes cursor.execute(sql, (attempts, username, chatid)) connection.commit() lock.release() return sql[:6]
def TokenCreate(name: str, permissions: list) -> str: lock.acquire(True) token = uuid4().urn[9:] # Create new token sql = "INSERT INTO tokens (token, name, permissions) VALUES (?, ?, ?)" hsh = TokenToHash(token) # Get permissions if "*" in permissions: perms = "*" else: perms = dumps(permissions) # Execute sql & commit changes cursor.execute(sql, (hsh, name, perms)) # Done connection.commit() lock.release() return token
def BanUser(chatid: int, username: str = "Unknown", state: bool = True, reason: str = "") -> None: lock.acquire(True) sql = "SELECT id FROM banned WHERE chatid=?" cursor.execute(sql, (chatid, )) # Change ban state to user result = cursor.fetchone() if result is not None: sql = "UPDATE banned SET state=?, username=?, reason=? WHERE chatid=?" else: sql = "INSERT INTO banned (state, username, reason, chatid) VALUES (?, ?, ?, ?)" # Set user is banned cursor.execute(sql, (int(state), username, reason, chatid)) # Remove row from authorized users if state is True: sql = "DELETE FROM authorized WHERE chatid=?" cursor.execute(sql, (chatid, )) connection.commit() lock.release()