IRCat/modules/ban_engine.py

42 lines
2 KiB
Python
Raw Permalink Normal View History

2025-01-09 22:39:20 -08:00
import os, sys, sqlite3
2025-01-09 17:30:46 -08:00
__ircat_type__ = "allsocket"
2025-01-09 22:39:20 -08:00
__ircat_requires__ = ["ban-provider", "host"]
2025-01-10 18:44:48 -08:00
__ircat_giveme__ = ["sql"] # Only command and allsocket have these.
2025-01-09 17:30:46 -08:00
class IRCatModule:
memory = {} # {ip: [content]}
useSQLengine = False
2025-01-10 18:35:50 -08:00
def __init__(self, ban_provider, host, sql):
2025-01-09 17:30:46 -08:00
self.ban_provider = ban_provider
2025-01-10 18:53:04 -08:00
self.host = host
2025-01-10 18:34:03 -08:00
if ban_provider == "sql":
2025-01-09 17:30:46 -08:00
self.useSQLengine = True
2025-01-10 18:35:50 -08:00
self.SQLengine = sql
2025-01-09 17:30:46 -08:00
def onValidate(self, socket, ip):
2025-01-10 18:54:51 -08:00
bans = open(self.ban_provider).read().split("\n")
for i in bans:
if ip in i.split(" ")[0]:
print("IP is banned, killing connection now...")
reason = " ".join(i.split(" ")[1:])
host = self.host
socket.sendall(bytes(f":{host} 465 * :You are banned from this server\r\n","UTF-8"))
socket.sendall(bytes(f"ERROR :Closing Link: {ip} (K-Lined: {reason})\r\n","UTF-8"))
raise Exception("K-Lined: " + " ".join(i.split(" ")[1:]))
def onSocket(self, socket, value, ip, cachedNick=None, validated=False):
if validated:
if self.useSQLengine:
pass
else:
bans = open(self.ban_provider).read().split("\n")
for i in bans:
if ip in i.split(" ")[0]:
print("IP is banned, killing connection now...")
reason = " ".join(i.split(" ")[1:])
host = self.host
socket.sendall(bytes(f":{host} 465 * :You are banned from this server\r\n","UTF-8"))
socket.sendall(bytes(f"ERROR :Closing Link: {ip} (K-Lined: {reason})\r\n","UTF-8"))
raise Exception("Banned: " + " ".join(i.split(" ")[1:]))
2025-01-09 22:39:20 -08:00
def ban(self, target_mask, reason="The ban() hammer has spoken!"):
if self.useSQLengine:
cur = self.SQLengine.conn.cursor()
else:
open(self.ban_provider, "a").write(f"\n{target_mask} {reason}")