From fdd490518ca39e961e98f8422d7aec33e772d5fc Mon Sep 17 00:00:00 2001 From: Firepup Sixfifty Date: Thu, 23 May 2024 08:16:29 -0500 Subject: [PATCH] Allow admins to premptively check DNSBLs. Also remove some Debug lines and fix minor issues. --- commands.py | 17 +++++++++++++++-- config.py | 30 ++++++++++++++++++++++++++---- handlers.py | 25 ++++++------------------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/commands.py b/commands.py index 9154ad7..9592784 100644 --- a/commands.py +++ b/commands.py @@ -224,7 +224,7 @@ def setStatus(bot: bare.bot, chan: str, name: str, message: str) -> None: stat = int(message.split(' ')[1]) reas = message.split(' ', 2)[2] except IndexError: - bot.msg(f"Insufficent information to set a status. Expected 3 args. Only got {len(message.split(' ', 1)) - 1} args.", chan) + bot.msg(f"Insufficent information to set a status. Only got {len(message.split(' ')) - (1 if '.sS' in message else 0)}/3 expected args.", chan) return except ValueError: bot.msg("Status parameter must be an int.", chan) @@ -250,13 +250,24 @@ def getStatus(bot: bare.bot, chan: str, name: str, message: str) -> None: user = message.split(' ')[1] except IndexError: user = 'firepup' - bot.msg(f"[DEBUG] {user} - {bot.statuses.get(user)} (msg in was {message.split(' ')})", chan) if bot.statuses.get(user) is None: bot.msg("You've gotta provide a nick I actually recognize.", chan) return bot.msg(f"{user}'s status: {'Unknown' if not bot.statuses[user].get('status') else bot.statuses[user]['status']} - {'Reason unset' if not bot.statuses[user].get('reason') else bot.statuses[user]['reason']}", chan) +def check(bot: bare.bot, chan: str, name: str, message: str) -> None: + try: + msg = message.split(' ', 1)[1] + nick = msg.split('!')[0] + host = msg.split('@', 1)[1] + dnsbl = conf.dnsblHandler(bot, nick, host, chan) + bot.msg("Blacklist check: " + (dnsbl if dnsbl else "Safe."), chan) + except Exception as E: + bot.msg("Blacklist lookup failed. Error recorded to bot logs.", chan) + bot.log(str(E), "FATAL") + + data: dict[str, dict[str, Any]] = { "!botlist": {"prefix": False, "aliases": []}, "bugs bugs bugs": {"prefix": False, "aliases": []}, @@ -290,6 +301,7 @@ data: dict[str, dict[str, Any]] = { "markov": {"prefix": True, "aliases": ["m"]}, "setStatus": {"prefix": True, "aliases": ["sS"], "check": checks.admin}, "getStatus": {"prefix": True, "aliases": ["gS"]}, + "check": {"prefix": True, "aliases": [], "check": checks.admin}, } regexes: list[str] = [conf.npbase, conf.su] call: dict[str, Callable[[bare.bot, str, str, str], None]] = { @@ -318,4 +330,5 @@ call: dict[str, Callable[[bare.bot, str, str, str], None]] = { "markov": markov, "setStatus": setStatus, "getStatus": getStatus, + "check": check, } diff --git a/config.py b/config.py index a297899..fd177bf 100644 --- a/config.py +++ b/config.py @@ -10,7 +10,7 @@ ipbl = DNSBLIpChecker() hsbl = DNSBLDomainChecker() load_dotenv() -__version__ = "v3.0.14" +__version__ = "v3.0.15" npbase: str = ( "\[\x0303last\.fm\x03\] [A-Za-z0-9_[\]{}\\|\-^]{1,$MAX} (is listening|last listened) to: \x02.+ - .*\x02( \([0-9]+ plays\)( \[.*\])?)?" # pyright: ignore [reportInvalidStringEscapeSequence] ) @@ -123,7 +123,7 @@ def sub( return result -def dnsbl(hostname: str) -> Union[str, None]: +def dnsbl(hostname: str) -> str: hosts = [] hstDT = None try: @@ -136,12 +136,34 @@ def dnsbl(hostname: str) -> Union[str, None]: for host in hstDT: if hstDT[host] != ["unknown"]: hosts.append(host) - print(f'DEBUG: {host} - {hstDT[host]}') if not hosts: - return + return "" hostStr = None if len(hosts) >= 3: hostStr = ', and '.join((', '.join(hosts)).rsplit(", ", 1)) else: hostStr = ' and '.join(hosts) return hostStr + +def dnsblHandler(bot: bare.bot, nick: str, hostname: str, chan: str) -> str: + dnsblStatus = 'Not enabled' + if bot.dnsblMode != "none": + dnsblStatus = dnsbl(hostname) + if dnsblStatus: + match bot.dnsblMode: + case "kickban": + bot.sendraw(f"KICK #{chan} {nick} :Sorry, but you're on the {dnsblStatus} blacklist(s).") + bot.sendraw(f"MODE #{chan} +b *!*@{hostname}") + case "akill": + bot.sendraw(f"OS AKILL ADD *@{hostname} !P Sorry, but you're on the {dnsblStatus} blacklists(s).") + case "kline": + bot.sendraw(f"KILL {nick} :Sorry, but you're on the {dnsblStatus} blacklist(s).") + bot.sendraw(f"KLINE 524160 *@{hostname} :Sorry, but you're on the {dnsblStatus} blacklist(s).") + bot.sendraw(f"KLINE *@{hostname} :Sorry, but you're on the {dnsblStatus} blacklist(s).") + case "gline": + bot.sendraw(f"KILL {nick} :Sorry, but you're on the {dnsblStatus} blacklist(s).") + bot.sendraw(f"GLINE *@{hostname} 524160 :Sorry, but you're on the {dnsblStatus} blacklist(s).") + bot.sendraw(f"GLINE *@{hostname} :Sorry, but you're on the {dnsblStatus} blacklist(s).") + case _: + bot.log(f'Unknown dnsbl Mode "{bot.dnsblMode}"!', "WARN") + return dnsblStatus diff --git a/handlers.py b/handlers.py index 877171c..9be12ad 100644 --- a/handlers.py +++ b/handlers.py @@ -189,27 +189,14 @@ def JOIN(bot: bare.bot, msg: str) -> tuple[None, None]: nick = msg.split("!", 1)[0][1:] hostname = msg.split("@", 1)[1].split(" ", 1)[0].strip() chan = msg.split("#")[-1].strip() - if bot.dnsblMode != "none": - dnsblStatus = conf.dnsbl(hostname) - if dnsblStatus: - match bot.dnsblMode: - case "kickban": - bot.sendraw(f"KICK #{chan} {nick} :Sorry, but you're on the {dnsblStatus} blacklist(s).") - bot.sendraw(f"MODE #{chan} +b *!*@{hostname}") - case "akill": - bot.sendraw(f"OS AKILL ADD *@{hostname} !P Sorry, but you're on the {dnsblStatus} blacklists(s).") - case "kline": - bot.sendraw(f"KILL {nick} :Sorry, but you're on the {dnsblStatus} blacklist(s).") - bot.sendraw(f"KLINE 524160 *@{hostname} :Sorry, but you're on the {dnsblStatus} blacklist(s).") - bot.sendraw(f"KLINE *@{hostname} :Sorry, but you're on the {dnsblStatus} blacklist(s).") - case "gline": - bot.sendraw(f"KILL {nick} :Sorry, but you're on the {dnsblStatus} blacklist(s).") - bot.sendraw(f"GLINE *@{hostname} 524160 :Sorry, but you're on the {dnsblStatus} blacklist(s).") - bot.sendraw(f"GLINE *@{hostname} :Sorry, but you're on the {dnsblStatus} blacklist(s).") - case _: - bot.log(f'Unknown dnsbl Mode "{bot.dnsblMode}"!', "WARN") + conf.dnsblHandler(bot, nick, hostname, chan) return None, None + +def MODE(bot: bare.bot, msg: str) -> tuple[None, None]: + pass + + def NULL(bot: bare.bot, msg: str) -> tuple[None, None]: return None, None