From 58060a52496f5a0a933594fc88537f22e122e307 Mon Sep 17 00:00:00 2001 From: Firepup Sixfifty Date: Thu, 23 May 2024 09:04:41 -0500 Subject: [PATCH] Improve cmd checking, add two new debug commands, and watch for gaining/losing ops --- bare.py | 2 ++ bot.py | 1 + commands.py | 24 ++++++++++++++++++++++++ config.py | 16 +++++++++++++++- handlers.py | 23 +++++++++++++++++++---- 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/bare.py b/bare.py index 4d6a0eb..bab4f8d 100644 --- a/bare.py +++ b/bare.py @@ -43,6 +43,8 @@ class bot: markov: MarkovBot autoMethod: str dnsblMode: str + statuses: dict[str, dict[str, str]] + ops: dict[str, bool] def __init__(self, server: str): ... diff --git a/bot.py b/bot.py index 2fde3cb..b457f53 100644 --- a/bot.py +++ b/bot.py @@ -53,6 +53,7 @@ class bot(bare.bot): ) self.queue: list[bbytes] = [] # pyright: ignore [reportInvalidTypeForm] self.statuses = {'firepup': {}} + self.ops = {} self.sock = socket(AF_INET, SOCK_STREAM) self.current = "user" self.threads = ( diff --git a/commands.py b/commands.py index 9592784..a9c759d 100644 --- a/commands.py +++ b/commands.py @@ -152,6 +152,26 @@ def debug(bot: bare.bot, chan: str, name: str, message: str) -> None: bot.msg(f"[DEBUG] {dbg_out}", chan) +def debugInternal(bot: bare.bot, chan: str, name: str, message: str) -> None: + things = dir(bot) + try: + thing = message.split(' ', 1)[1] + except IndexError: + bot.msg("You can't just ask me to lookup nothing.", chan) + return + if thing in things: + bot.msg(f"self.{thing} = {getattr(bot, thing)}", chan) + else: + bot.msg(f"I have nothing called \"{thing}\"", chan) + + +def debugEval(bot: bare.bot, chan: str, name: str, message: str) -> None: + try: + bot.msg(str(eval(message.split(' ', 1)[1])), chan) + except Exception as E: + bot.msg(f"Exception: {E}", chan) + + def raw(bot: bare.bot, chan: str, name: str, message: str) -> None: bot.sendraw(message.split(" ", 1)[1]) @@ -281,6 +301,8 @@ data: dict[str, dict[str, Any]] = { "uptime": {"prefix": True, "aliases": []}, "raw ": {"prefix": True, "aliases": ["cmd "], "check": checks.admin}, "debug": {"prefix": True, "aliases": ["dbg", "d"], "check": checks.admin}, + "debugInternal": {"prefix": True, "aliases": ["dbgInt", "dI"], "check": checks.admin}, + "debugEval": {"prefix": True, "aliases": ["dbgEval", "dE"], "check": checks.admin}, "8ball": {"prefix": True, "aliases": ["eightball", "8b"]}, "join ": {"prefix": True, "aliases": [], "check": checks.admin}, "quote": {"prefix": True, "aliases": ["q"]}, @@ -314,6 +336,8 @@ call: dict[str, Callable[[bare.bot, str, str, str], None]] = { "uptime": uptime, "raw ": raw, "debug": debug, + "debugInternal": debugInternal, + "debugEval": debugEval, "8ball": eball, "join ": join, "quote": quote, diff --git a/config.py b/config.py index fd177bf..ba2b5f2 100644 --- a/config.py +++ b/config.py @@ -10,7 +10,7 @@ ipbl = DNSBLIpChecker() hsbl = DNSBLDomainChecker() load_dotenv() -__version__ = "v3.0.15" +__version__ = "v3.0.16" npbase: str = ( "\[\x0303last\.fm\x03\] [A-Za-z0-9_[\]{}\\|\-^]{1,$MAX} (is listening|last listened) to: \x02.+ - .*\x02( \([0-9]+ plays\)( \[.*\])?)?" # pyright: ignore [reportInvalidStringEscapeSequence] ) @@ -102,6 +102,20 @@ def decode_escapes(s: str) -> str: return ESCAPE_SEQUENCE_RE.sub(decode_match, s) +def cmdFind(message: str, find: list, usePrefix: bool = True) -> bool: + cmd = None + try: + cmd = message.split(' ', 1)[0] + except IndexError: ... + if not cmd: + return False + if usePrefix: + return any(cmd == prefix + match for match in find) + else: + return any(cmd == match for match in find) + + + def mfind(message: str, find: list, usePrefix: bool = True) -> bool: if usePrefix: return any(message[: len(match) + 1] == prefix + match for match in find) diff --git a/handlers.py b/handlers.py index 9be12ad..be82b38 100644 --- a/handlers.py +++ b/handlers.py @@ -103,7 +103,7 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] triggers = [cmd] triggers.extend(cmds.data[cmd]["aliases"]) triggers = list(conf.sub(call, bot, chan, name).lower() for call in triggers) - if conf.mfind( + if conf.cmdFind( conf.sub(message, bot, chan, name).lower(), triggers, cmds.data[cmd]["prefix"], @@ -124,7 +124,7 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] cmds.call[check](bot, chan, name, message) handled = True break - if not handled and conf.mfind(message, ["reload", "r"]): + if not handled and conf.cmdFind(message, ["reload", "r"]): if checks.admin(bot, name, host, chan, "reload"): return "reload", chan handled = True @@ -194,7 +194,22 @@ def JOIN(bot: bare.bot, msg: str) -> tuple[None, None]: def MODE(bot: bare.bot, msg: str) -> tuple[None, None]: - pass + chan = msg.split("#", 1)[1].split(" ", 1)[0] + add = True if msg.split("#", 1)[1].split(" ", 2)[1][0] == "+" else False + modes = msg.split("#", 1)[1].split(" ", 2)[1][1:] + users = "" + try: + users = msg.split("#", 1)[1].split(" ", 2)[2].split() + except IndexError: ... + if len(modes) != len(users): + bot.log("Refusing to handle modes that do not have corresponding users.") + return None, None + for i in range(len(modes)): + if users[i] == bot.nick: + if modes[i] == "o": + bot.ops[chan] = add + bot.log(f"{'Got' if add else 'Lost'} ops in {chan}") + return None, None def NULL(bot: bare.bot, msg: str) -> tuple[None, None]: @@ -208,7 +223,7 @@ handles: dict[ "NICK": NICK, "KICK": KICK, "PART": PART, - "MODE": NULL, + "MODE": MODE, "TOPIC": NULL, "QUIT": QUIT, "JOIN": JOIN,