From 74bc044fece827cf2a7f05915bd0b0f24f4fac5c Mon Sep 17 00:00:00 2001 From: Firepup Sixfifty Date: Sat, 18 May 2024 17:38:49 -0500 Subject: [PATCH] DNSBL support --- bare.py | 1 + bot.py | 5 +++++ config.py | 26 +++++++++++++++++++++++++- handlers.py | 23 +++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/bare.py b/bare.py index 180242f..4d6a0eb 100644 --- a/bare.py +++ b/bare.py @@ -42,6 +42,7 @@ class bot: onStrtCmds: list[str] markov: MarkovBot autoMethod: str + dnsblMode: str def __init__(self, server: str): ... diff --git a/bot.py b/bot.py index c1df37f..c540c8f 100644 --- a/bot.py +++ b/bot.py @@ -77,6 +77,11 @@ class bot(bare.bot): if "autoMethod" in conf.servers[server] else "QUOTE" ) + self.dnsblMode = ( + conf.servers[server]["dnsblMode"] + if "dnsblMode" in conf.servers[server] + else "none" + ) self.lastfmLink = conf.lastfmLink with open("mastermessages.txt") as f: TMFeed = [] diff --git a/config.py b/config.py index 0de2cdf..00aa930 100644 --- a/config.py +++ b/config.py @@ -4,9 +4,13 @@ from dotenv import load_dotenv # type: ignore import re, codecs from typing import Optional, Any import bare, pylast +from pydnsbl import DNSBLIpChecker, DNSBLDomainChecker + +ipbl = DNSBLIpChecker() +hsbl = DNSBLDomainChecker() load_dotenv() -__version__ = "v3.0.12" +__version__ = "v3.0.13" npbase: str = ( "\[\x0303last\.fm\x03\] [A-Za-z0-9_[\]{}\\|\-^]{1,$MAX} (is listening|last listened) to: \x02.+ - .*\x02( \([0-9]+ plays\)( \[.*\])?)?" # pyright: ignore [reportInvalidStringEscapeSequence] ) @@ -20,11 +24,13 @@ 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" }, "efnet": { "address": "irc.underworld.no", "channels": {"#random": 0, "#dice": 0}, "hosts": ["154.sub-174-251-241.myvzw.com"], + "dnsblMode": "kickban", }, "replirc": { "address": "127.0.0.1", @@ -46,6 +52,7 @@ servers: dict[str, dict[str, Any]] = { "hosts": ["owner.firepi"], "threads": ["radio"], "autoMethod": "MARKOV", + "dnsblMode": "akill", }, "backupbox": { "address": "127.0.0.1", @@ -58,6 +65,7 @@ servers: dict[str, dict[str, Any]] = { "2600-6c5a-637f-1a85-0000-0000-0000-6667.inf6.spectrum.com", ], "onIdntCmds": ["OPER e e"], + "dnsbl-mode": "gline", }, "twitch": { "nick": "fireschatbot", @@ -113,3 +121,19 @@ def sub( if name: result = result.replace("$SENDER", name).replace("$NAME", name) return result + + +def dnsbl(hostname: str) -> Union[str, None]: + hosts = None + try: + hosts = ipbl.check(hostname).detected_by.keys() + except ValueError: + hosts = hsbl.check(hostname).detected_by.keys() + if not hosts: + return + hostStr = None + if len(hosts) >= 3: + hostStr = ', and '.join((', '.join(hosts)).rsplit(", ", 1)) + else: + hostStr = ' and '.join(hosts) + return hostStr diff --git a/handlers.py b/handlers.py index 6888ee3..028cda2 100644 --- a/handlers.py +++ b/handlers.py @@ -185,6 +185,28 @@ def QUIT(bot: bare.bot, msg: str) -> tuple[None, None]: return None, None +def JOIN(bot: bare.bot, msg: str) -> tuple[None, None]: + if bot.dnsblMode != "none": + dnsblStatus = conf.dnsbl(hostname) + if dnsblStatus: + match bot.dnsblMode: + case "kickban": + bot.sendraw(f"KICK {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 None, None + def NULL(bot: bare.bot, msg: str) -> tuple[None, None]: return None, None @@ -199,4 +221,5 @@ handles: dict[ "MODE": NULL, "TOPIC": NULL, "QUIT": QUIT, + "JOIN": JOIN, }