diff --git a/bot.py b/bot.py index 7140d56..7cef93c 100644 --- a/bot.py +++ b/bot.py @@ -41,6 +41,7 @@ class bot(bare.bot): self.queue: list[bbytes] = [] self.sock = socket(AF_INET, SOCK_STREAM) self.npallowed = ["FireBitBot"] + self.current = "user" self.log(f"Start init for {self.server}") def connect(self) -> None: diff --git a/checks.py b/checks.py new file mode 100644 index 0000000..c936977 --- /dev/null +++ b/checks.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 +import config as conf +import random as r +from typing import Any, Callable, Optional +import bare, re + +def admin(bot: bare.bot, name: str, host: Optional[str] = "nul", chan: Optional[str] = "nul", cmd: Optional[str] = "nul") -> bool: + if ( + name.lower() in conf.servers[bot.server]["admins"] + or host in conf.admin_hosts + or host in conf.servers[bot.server]["hosts"] + ): + if bot.current != "bridge": + return True + else: + if type(chan) != str or chan == "nul": + return False + else: + bot.msg(f"Sorry {name}, bridged users can't use admin commands.", chan) + return False + else: + if type(chan) != str or chan == "nul": + return False + else: + bot.msg(f"Sorry {name}, {cmd} is an admin only command.", chan) + return False diff --git a/commands.py b/commands.py index 144de08..5cf7137 100644 --- a/commands.py +++ b/commands.py @@ -1,8 +1,9 @@ +#!/usr/bin/python3 from subprocess import run, PIPE import config as conf import random as r from typing import Any, Callable -import bare, re +import bare, re, checks def goat(bot: bare.bot, chan: str, name: str, message: str) -> None: @@ -50,19 +51,14 @@ def uptime(bot: bare.bot, chan: str, name: str, message: str) -> None: def isAdmin(bot: bare.bot, chan: str, name: str, message: str) -> None: bot.msg( - f"{conf.adminCheck(bot, name)} (hostname is not checked)", + f"{checks.admin(bot, name)} (hostname is not checked)", chan, ) def help(bot: bare.bot, chan: str, name: str, message: str) -> None: helpErr = False - if (name.startswith("saxjax") and bot.server == "efnet") or ( - name == "ReplIRC" and bot.server == "replirc" - ): - if "<" in message and "<" in message: - helpErr = True - elif name.endswith("dsc"): + if bot.current == "bridge": helpErr = True if not helpErr: bot.msg("Command list needs rework", name) @@ -102,10 +98,11 @@ def quote(bot: bare.bot, chan: str, name: str, message: str) -> None: query = "null" if " " in message: query = message.split(" ", 1)[1] - qfilter = f".*{query}.*" + qfilter = f".*{query}.*".replace(" ", "\s") r.seed() mm = open("mastermessages.txt", "r") - q = list(filter(lambda x: re.match(qfilter, x), mm.readlines())) + quotes = mm.readlines() + q = list(filter(lambda x: re.match(qfilter, x), quotes)) if q == []: q = [f'No results for "{query}" '] sel = conf.decode_escapes( @@ -153,7 +150,7 @@ def reboot(bot: bare.bot, chan: str, name: str, message: str) -> None: def sudo(bot: bare.bot, chan: str, name: str, message: str) -> None: - if conf.adminCheck(bot, name): + if checks.admin(bot, name): bot.msg("Error - system failure, contact system operator", chan) elif "bot" in name.lower(): bot.log("lol, no.") @@ -179,22 +176,22 @@ data: dict[str, dict[str, Any]] = { "bugs bugs bugs": {"prefix": False, "aliases": []}, "hi $BOTNICK": {"prefix": False, "aliases": ["hello $BOTNICK"]}, # [npbase, su] - "restart": {"prefix": True, "aliases": ["reboot"], "admin": True}, + "restart": {"prefix": True, "aliases": ["reboot"], "check": checks.admin}, "uptime": {"prefix": True, "aliases": []}, - "raw ": {"prefix": True, "aliases": ["cmd "], "admin": True}, - "debug": {"prefix": True, "aliases": ["dbg"], "admin": True}, + "raw ": {"prefix": True, "aliases": ["cmd "], "check": checks.admin}, + "debug": {"prefix": True, "aliases": ["dbg"], "check": checks.admin}, "8ball": {"prefix": True, "aliases": ["eightball", "8b"]}, - "join ": {"prefix": True, "aliases": [], "admin": True}, + "join ": {"prefix": True, "aliases": [], "check": checks.admin}, "quote": {"prefix": True, "aliases": ["q"]}, - "goat.mode.activate": {"prefix": True, "aliases": [], "admin": True}, - "goat.mode.deactivate": {"prefix": True, "aliases": [], "admin": True}, + "goat.mode.activate": {"prefix": True, "aliases": [], "check": checks.admin}, + "goat.mode.deactivate": {"prefix": True, "aliases": [], "check": checks.admin}, "help": {"prefix": True, "aliases": []}, "amiadmin": {"prefix": True, "aliases": []}, "ping": {"prefix": True, "aliases": []}, - "op me": {"prefix": False, "aliases": [], "admin": True}, + "op me": {"prefix": False, "aliases": [], "check": checks.admin}, "whoami": {"prefix": True, "aliases": []}, } -checks: list[str] = [conf.npbase, conf.su] +regexes: list[str] = [conf.npbase, conf.su] call: dict[str, Callable[[bare.bot, str, str, str], None]] = { "!botlist": botlist, "bugs bugs bugs": bugs, diff --git a/config.py b/config.py index 5ca2c0c..37b6039 100644 --- a/config.py +++ b/config.py @@ -66,11 +66,3 @@ def mfind(message: str, find: list, usePrefix: bool = True) -> bool: return any(message[: len(match) + 1] == prefix + match for match in find) else: return any(message[: len(match)] == match for match in find) - - -def adminCheck(bot: bare.bot, name: str, host: Optional[str] = "nul") -> bool: - return ( - name.lower() in servers[bot.server]["admins"] - or host in admin_hosts - or host in servers[bot.server]["hosts"] - ) diff --git a/handlers.py b/handlers.py index ac66783..b1e4c37 100644 --- a/handlers.py +++ b/handlers.py @@ -1,11 +1,11 @@ +#!/usr/bin/python3 import random as r import config as conf -import re import commands as cmds from typing import Union, Callable from overrides import bytes, bbytes from importlib import reload -import bare +import bare, re, checks def CTCP(bot: bare.bot, msg: str) -> bool: @@ -46,14 +46,15 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] name = msg.split("!", 1)[0][1:] host = msg.split("@", 1)[1].split(" ", 1)[0] bridge = False + bot.current = "user" if ( (name.startswith("saxjax") and bot.server == "efnet") or (name == "ReplIRC" and bot.server == "replirc") - or (name == "FirePyLink_" and bot.server == "ircnow") - or (name == "FirePyLink" and bot.server == "backupbox") + or (name in ["FirePyLink_", "FirePyLink"] and bot.server in ["ircnow", "backupbox"]) ): if "<" in msg and ">" in msg: bridge = True + bot.current = "bridge" Nname = msg.split("<", 1)[1].split(">", 1)[0].strip() if name == "ReplIRC": name = Nname[4:] @@ -70,7 +71,7 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] message = msg.split("PRIVMSG", 1)[1].split(":", 1)[1].strip() chan = msg.split("PRIVMSG", 1)[1].split(":", 1)[0].strip() bot.log( - f'Got "{bytes(message).lazy_decode()}" from "{name}" in "{chan}"', + f'Got "{bytes(message).lazy_decode()}" from "{name}" in "{chan}" ({bot.current})', ) if len(name) > bot.nicklen: bot.log(f"Name too long ({len(name)} > {bot.nicklen})") @@ -101,18 +102,16 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] cmds.data[cmd]["prefix"], ): if ( - "admin" in cmds.data[cmd] and cmds.data[cmd]["admin"] - ) and not conf.adminCheck(bot, name, host): - bot.msg( - f"Sorry {name}, you don't have permission to use {cmd.strip()}.", - chan, - ) + "check" in cmds.data[cmd] and cmds.data[cmd]["check"] + ): + if cmds.data[cmd]["check"](bot, name, host, chan, cmd): + cmds.call[cmd](bot, chan, name, message) else: cmds.call[cmd](bot, chan, name, message) handled = True break if not handled: - for check in cmds.checks: + for check in cmds.regexes: if re.search( check.replace("$MAX", str(bot.nicklen)).replace("$BOTNICK", bot.nick), message, @@ -121,13 +120,8 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] handled = True break if not handled and conf.mfind(message, ["reload"]): - if conf.adminCheck(bot, name, host): + if checks.admin(bot, name, host, chan, "reload"): return "reload", chan - else: - bot.msg( - f"Sorry {name}, you don't have permission to use reload.", - chan, - ) handled = True if not handled and len(message.split("\x01")) == 3: if not CTCP(bot, message):