diff --git a/bot.py b/bot.py index dfffad3..24fe11f 100644 --- a/bot.py +++ b/bot.py @@ -53,7 +53,7 @@ class bot(bare.bot): else "FireBot" ) self.queue: list[bbytes] = [] # pyright: ignore [reportInvalidTypeForm] - self.statuses = {'firepup': {}} + self.statuses = {"firepup": {}} self.ops = {} self.sock = socket(AF_INET, SOCK_STREAM) self.current = "user" @@ -318,38 +318,41 @@ class bot(bare.bot): if action in handlers.handles: res, chan = handlers.handles[action](self, ircmsg) if res == "reload" and type(chan) == str: - try: - reload(conf) - self.adminnames = ( - conf.servers[self.server]["admins"] - if "admins" in conf.servers[self.server] - else [] - ) - self.ignores = ( - conf.servers[self.server]["ignores"] - if "ignores" in conf.servers[self.server] - else [] - ) - self.__version__ = conf.__version__ - self.npallowed = conf.npallowed - self.interval = ( - conf.servers[self.server]["interval"] - if "interval" in conf.servers[self.server] - else 50 - ) - conf.prefix = ( - conf.servers[self.server]["prefix"] - if "prefix" in conf.servers[self.server] - else "." - ) - reload(cmds) - reload(handlers) - self.msg("Reloaded successfully", chan) - except Exception: - Err = format_exc() - for line in Err.split("\n"): - self.log(line, "ERROR") - self.msg("Reload failed, likely partially reloaded. Please check error logs.", chan) + try: + reload(conf) + self.adminnames = ( + conf.servers[self.server]["admins"] + if "admins" in conf.servers[self.server] + else [] + ) + self.ignores = ( + conf.servers[self.server]["ignores"] + if "ignores" in conf.servers[self.server] + else [] + ) + self.__version__ = conf.__version__ + self.npallowed = conf.npallowed + self.interval = ( + conf.servers[self.server]["interval"] + if "interval" in conf.servers[self.server] + else 50 + ) + conf.prefix = ( + conf.servers[self.server]["prefix"] + if "prefix" in conf.servers[self.server] + else "." + ) + reload(cmds) + reload(handlers) + self.msg("Reloaded successfully", chan) + except Exception: + Err = format_exc() + for line in Err.split("\n"): + self.log(line, "ERROR") + self.msg( + "Reload failed, likely partially reloaded. Please check error logs.", + chan, + ) else: if ircmsg.startswith("PING "): self.ping(ircmsg) diff --git a/commands.py b/commands.py index 7627e86..e2c6afe 100644 --- a/commands.py +++ b/commands.py @@ -155,19 +155,19 @@ def debug(bot: bare.bot, chan: str, name: str, message: str) -> None: def debugInternal(bot: bare.bot, chan: str, name: str, message: str) -> None: things = dir(bot) try: - thing = message.split(' ', 1)[1] + 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) + 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) + bot.msg(str(eval(message.split(" ", 1)[1])), chan) except Exception as E: bot.msg(f"Exception: {E}", chan) @@ -234,17 +234,23 @@ def markov(bot: bare.bot, chan: str, name: str, message: str) -> None: def setStatus(bot: bare.bot, chan: str, name: str, message: str) -> None: - user, stat, reas = ('', 0, '') + user, stat, reas = ("", 0, "") try: - if message.split(' ')[1] == "help": - bot.msg("Assuming you want help with status codes. 1 is Available, 2 is Busy, 3 is Unavailable, anything else is Unknown.", chan) + if message.split(" ")[1] == "help": + bot.msg( + "Assuming you want help with status codes. 1 is Available, 2 is Busy, 3 is Unavailable, anything else is Unknown.", + chan, + ) return - message = message.split(' ', 1)[1] - user = message.split(' ')[0].lower() - stat = int(message.split(' ')[1]) - reas = message.split(' ', 2)[2] + message = message.split(" ", 1)[1] + user = message.split(" ")[0].lower() + stat = int(message.split(" ")[1]) + reas = message.split(" ", 2)[2] except IndexError: - 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) + 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) @@ -259,28 +265,31 @@ def setStatus(bot: bare.bot, chan: str, name: str, message: str) -> None: case _: stat = "Unknown" if user in ["me", "my", "I"]: - user = 'firepup' - bot.statuses[user] = {'status': stat, 'reason': reas} + user = "firepup" + bot.statuses[user] = {"status": stat, "reason": reas} bot.msg(f"Status set for '{user}'. Raw data: {bot.statuses[user]}", chan) def getStatus(bot: bare.bot, chan: str, name: str, message: str) -> None: - user = '' + user = "" try: - user = message.split(' ')[1] + user = message.split(" ")[1] except IndexError: - user = 'firepup' + user = "firepup" 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) + 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] + msg = message.split(" ", 1)[1] + nick = msg.split("!")[0] + host = msg.split("@", 1)[1] dnsbl, raws = conf.dnsblHandler(bot, nick, host, chan) bot.msg(f"Blacklist check: {dnsbl if dnsbl else 'Safe.'} ({raws})", chan) except Exception as E: @@ -301,7 +310,11 @@ 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}, + "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": ["j"], "check": checks.admin}, diff --git a/config.py b/config.py index 6fa3300..11dd767 100644 --- a/config.py +++ b/config.py @@ -6,25 +6,27 @@ from typing import Optional, Any, Union import bare, pylast from pydnsbl import DNSBLIpChecker, DNSBLDomainChecker, providers as BL + class droneBL(BL.Provider): def process_response(self, response): reasons = set() for result in response: reason = result.host - if reason in ['127.0.0.3']: - reasons.add('IRC Spambot') - elif reason in ['127.0.0.19']: - reasons.add('Abused VPN') - elif reason in ['127.0.0.9', '127.0.0.8']: - reasons.add('Open Proxy') - elif reason in ['127.0.0.13']: - reasons.add('Automated Attacks') + if reason in ["127.0.0.3"]: + reasons.add("IRC Spambot") + elif reason in ["127.0.0.19"]: + reasons.add("Abused VPN") + elif reason in ["127.0.0.9", "127.0.0.8"]: + reasons.add("Open Proxy") + elif reason in ["127.0.0.13"]: + reasons.add("Automated Attacks") else: - print('Unknown dnsbl reason: ' + reason, flush=True) - reasons.add('unknown') + print("Unknown dnsbl reason: " + reason, flush=True) + reasons.add("unknown") return reasons -providers = BL.BASE_PROVIDERS + [droneBL('dnsbl.dronebl.org')] + +providers = BL.BASE_PROVIDERS + [droneBL("dnsbl.dronebl.org")] ipbl = DNSBLIpChecker(providers=providers) hsbl = DNSBLDomainChecker(providers=providers) @@ -44,7 +46,7 @@ servers: dict[str, dict[str, Any]] = { "channels": {"#random": 0, "#dice": 0, "#offtopic": 0, "#main/replirc": 0}, "ignores": ["#main/replirc"], "hosts": ["9pfs.repl.co"], - "dnsblMode": "kickban" + "dnsblMode": "kickban", }, "efnet": { "address": "irc.underworld.no", @@ -126,8 +128,9 @@ def decode_escapes(s: str) -> str: def cmdFind(message: str, find: list, usePrefix: bool = True) -> bool: cmd = None try: - cmd = message.split(' ', 1)[0] - except IndexError: ... + cmd = message.split(" ", 1)[0] + except IndexError: + ... if not cmd: return False if usePrefix: @@ -136,7 +139,6 @@ def cmdFind(message: str, find: list, usePrefix: bool = True) -> bool: 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) @@ -172,34 +174,53 @@ def dnsbl(hostname: str) -> tuple[str, dict[str, list[str]]]: if hstDT[host] != ["unknown"]: hosts.append(host) if not hosts: - return "", hstDT + return "", hstDT hostStr = None if len(hosts) >= 3: - hostStr = ', and '.join((', '.join(hosts)).rsplit(", ", 1)) + hostStr = ", and ".join((", ".join(hosts)).rsplit(", ", 1)) else: - hostStr = ' and '.join(hosts) + hostStr = " and ".join(hosts) return hostStr, hstDT -def dnsblHandler(bot: bare.bot, nick: str, hostname: str, chan: str) -> tuple[str, dict[str, list[str]]]: - dnsblStatus = 'Not enabled' + +def dnsblHandler( + bot: bare.bot, nick: str, hostname: str, chan: str +) -> tuple[str, dict[str, list[str]]]: + dnsblStatus = "Not enabled" dnsblResps = {} if bot.dnsblMode != "none": dnsblStatus, dnsblResps = 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"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} blacklist(s).") + bot.sendraw( + f"OS AKILL ADD *@{hostname} !P Sorry, but you're on the {dnsblStatus} blacklist(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).") + 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).") + 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, dnsblResps diff --git a/handlers.py b/handlers.py index 2406433..85e915b 100644 --- a/handlers.py +++ b/handlers.py @@ -117,15 +117,21 @@ def PRIVMSG(bot: bare.bot, msg: str) -> Union[tuple[None, None], tuple[str, str] Err = format_exc() for line in Err.split("\n"): bot.log(line, "ERROR") - bot.msg("Sorry, I had an error trying to execute that command. Please check error logs.", chan) + bot.msg( + "Sorry, I had an error trying to execute that command. Please check error logs.", + chan, + ) else: - try: - cmds.call[cmd](bot, chan, name, message) - except Exception: - Err = format_exc() - for line in Err.split("\n"): - bot.log(line, "ERROR") - bot.msg("Sorry, I had an error trying to execute that command. Please check error logs.", chan) + try: + cmds.call[cmd](bot, chan, name, message) + except Exception: + Err = format_exc() + for line in Err.split("\n"): + bot.log(line, "ERROR") + bot.msg( + "Sorry, I had an error trying to execute that command. Please check error logs.", + chan, + ) handled = True break if not handled: @@ -213,7 +219,8 @@ def MODE(bot: bare.bot, msg: str) -> tuple[None, None]: users = "" try: users = msg.split("#", 1)[1].split(" ", 2)[2].split() - except IndexError: ... + except IndexError: + ... if len(modes) != len(users): bot.log("Refusing to handle modes that do not have corresponding users.") return None, None