Many small fixes. (MM Phase 6)

This commit is contained in:
Firepup Sixfifty 2023-11-07 07:53:14 -06:00
parent 8d36b4742e
commit 26e4bd0789
Signed by: Firepup650
GPG key ID: 7C92E2ABBBFAB9BA
4 changed files with 34 additions and 44 deletions

58
bot.py
View file

@ -4,9 +4,10 @@ from overrides import bytes, bbytes
from logs import log from logs import log
import re import re
from typing import NoReturn, Union from typing import NoReturn, Union
from config import npbase, servers, __version__ from config import npbase, servers, __version__, prefix
import commands as cmds import commands as cmds
from config import ESCAPE_SEQUENCE_RE, servers from config import decode_escapes, servers
from time import sleep
def mfind(message: str, find: list, usePrefix: bool = True) -> bool: def mfind(message: str, find: list, usePrefix: bool = True) -> bool:
if usePrefix: if usePrefix:
@ -14,12 +15,6 @@ def mfind(message: str, find: list, usePrefix: bool = True) -> bool:
else: else:
return any(message[: len(match)] == match for match in find) return any(message[: len(match)] == match for match in find)
def decode_escapes(s: str) -> str:
def decode_match(match):
return codecs.decode(match.group(0), "unicode-escape")
return ESCAPE_SEQUENCE_RE.sub(decode_match, s)
class bot: class bot:
def __init__(self, server: str): def __init__(self, server: str):
self.gmode = False self.gmode = False
@ -32,18 +27,16 @@ class bot:
servers[server]["interval"] if "interval" in servers[server] else 50 servers[server]["interval"] if "interval" in servers[server] else 50
) )
self.nick = "FireBot" self.nick = "FireBot"
self.prefix = "."
self.rebt = "fire" self.rebt = "fire"
self.gblrebt = "all" self.gblrebt = "all"
self.adminnames = servers[server]["admins"] self.adminnames = servers[server]["admins"]
self.exitcode = f"bye {self.nick.lower()}" self.exitcode = f"bye {self.nick.lower()}"
self.np = re.compile(npbase.replace("MAX", f"{nicklen}"))
self.queue = [] self.queue = []
self.sock = socket(AF_INET, SOCK_STREAM) self.sock = socket(AF_INET, SOCK_STREAM)
self.log(f"Start init for {server}") self.log(f"Start init for {self.server}")
def connect(self) -> None: def connect(self) -> None:
self.log(f"Joining {server}...") self.log(f"Joining {self.server}...")
self.sock.connect((self.address, self.port)) self.sock.connect((self.address, self.port))
self.send(f"USER {self.nick} {self.nick} {self.nick} {self.nick}\n") self.send(f"USER {self.nick} {self.nick} {self.nick} {self.nick}\n")
self.send(f"NICK {self.nick}\n") self.send(f"NICK {self.nick}\n")
@ -57,8 +50,7 @@ class bot:
print(bytes(ircmsg).lazy_decode()) print(bytes(ircmsg).lazy_decode())
if ircmsg.find("NICKLEN=") != -1: if ircmsg.find("NICKLEN=") != -1:
self.nicklen = int(ircmsg.split("NICKLEN=")[1].split(" ")[0]) self.nicklen = int(ircmsg.split("NICKLEN=")[1].split(" ")[0])
self.np = re.compile(npbase.replace("MAX", f"{self.nicklen}")) self.log(f"NICKLEN set to {self.nicklen}")
self.log(f"NICKLEN set to {nicklen}")
elif ircmsg.find("Nickname") != -1: elif ircmsg.find("Nickname") != -1:
self.log("Nickname in use", "WARN") self.log("Nickname in use", "WARN")
self.nick = f"{self.nick}{r.randint(0,1000)}" self.nick = f"{self.nick}{r.randint(0,1000)}"
@ -72,7 +64,7 @@ class bot:
self.exit("Closing Link") self.exit("Closing Link")
else: else:
self.exit("Lost connection to the server") self.exit("Lost connection to the server")
self.log(f"Joined {server} successfully!") self.log(f"Joined {self.server} successfully!")
def join(self, chan: str, origin: str, lock: bool = True) -> None: def join(self, chan: str, origin: str, lock: bool = True) -> None:
self.log(f"Joining {chan}...") self.log(f"Joining {chan}...")
@ -86,7 +78,7 @@ class bot:
if origin != "null": if origin != "null":
self.sendmsg(f"Refusing to join channel {chan} (protected)", origin) self.sendmsg(f"Refusing to join channel {chan} (protected)", origin)
return return
if chan in channels and lock: if chan in self.channels and lock:
if origin != "null": if origin != "null":
self.sendmsg(f"I'm already in {chan}.", origin) self.sendmsg(f"I'm already in {chan}.", origin)
return return
@ -119,7 +111,7 @@ class bot:
if origin != "null": if origin != "null":
self.sendmsg(f"{chan} is +O, and I'm not an operator.", origin) self.sendmsg(f"{chan} is +O, and I'm not an operator.", origin)
elif code == 366: elif code == 366:
log(f"Joining {chan} succeeded", server) self.log(f"Joining {chan} succeeded")
if origin != "null": if origin != "null":
self.sendmsg(f"Joined {chan}", origin) self.sendmsg(f"Joined {chan}", origin)
self.channels[chan] = 0 self.channels[chan] = 0
@ -247,45 +239,35 @@ class bot:
self.log( self.log(
f'Got "{bytes(message).lazy_decode()}" from "{name}" in "{chan}"', f'Got "{bytes(message).lazy_decode()}" from "{name}" in "{chan}"',
) )
if len(name) > nicklen: if len(name) > self.nicklen:
self.log(f"Name too long ({len(name)} > {nicklen})") self.log(f"Name too long ({len(name)} > {self.nicklen})")
continue continue
elif chan not in channels: elif chan not in self.channels:
self.log( self.log(
f"Channel not in channels ({chan} not in {channels})", f"Channel not in channels ({chan} not in {self.channels})",
"WARN", "WARN",
) )
else: else:
channels[chan] += 1 self.channels[chan] += 1
if "goat" in name.lower() and self.gmode == True: if "goat" in name.lower() and self.gmode == True:
cmds.goat(self, chan) cmds.goat(self, chan)
handled = False handled = False
for cmd in cmds.data: for cmd in cmds.data:
triggers = [cmd]
triggers.extend(cmds.data[cmd]["aliases"])
triggers = list(call.replace("$BOTNICK", self.nick) for call in triggers)
if mfind( if mfind(
message, message,
cmd.replace("$BOTNICK", self.nick), triggers,
cmds.data[cmd]["prefix"], cmds.data[cmd]["prefix"],
): ):
cmds.call[cmd](self, chan, name) cmds.call[cmd](self, chan, name, message)
handled = True handled = True
break break
if not handled:
for cmd in cmds.data:
for alias in cmds.data[cmd]["aliases"]:
if mfind(
message,
alias.replace("$BOTNICK", self.nick),
cmds.data[cmd]["prefix"],
):
cmds.call[cmd](self, chan, name, message)
handled = True
break
if handled:
break
if not handled: if not handled:
for check in cmds.checks: for check in cmds.checks:
if re.search( if re.search(
check.replace("$MAX", self.nicklen).replace( check.replace("$MAX", str(self.nicklen)).replace(
"$BOTNICK", self.nick "$BOTNICK", self.nick
), ),
message, message,

View file

@ -1,5 +1,6 @@
from subprocess import run, PIPE from subprocess import run, PIPE
from config import npbase, su from config import npbase, su, decode_escapes
import random as r
def goat(bot, chan: str, name: str, message: str) -> None: def goat(bot, chan: str, name: str, message: str) -> None:
bot.log("GOAT DETECTED") bot.log("GOAT DETECTED")
@ -112,9 +113,9 @@ def eball(bot, chan: str, name: str, message: str) -> None:
def debug(bot, chan: str, name: str, message: str) -> None: def debug(bot, chan: str, name: str, message: str) -> None:
if name.lower() in bot.adminnames: if name.lower() in bot.adminnames:
bot.msg(f"[DEBUG] NICKLEN={nicklen}", chan) bot.msg(f"[DEBUG] NICKLEN={bot.nicklen}", chan)
bot.msg(f"[DEBUG] ADMINS={adminnames}", chan) bot.msg(f"[DEBUG] ADMINS={bot.adminnames}", chan)
bot.msg(f"[DEBUG] CHANNELS={channels}", chan) bot.msg(f"[DEBUG] CHANNELS={bot.channels}", chan)
def raw(bot, chan: str, name: str, message: str) -> None: def raw(bot, chan: str, name: str, message: str) -> None:
@ -156,7 +157,7 @@ data = {
"debug": {"prefix": True, "aliases": ["dbg"]}, "debug": {"prefix": True, "aliases": ["dbg"]},
"8ball": {"prefix": True, "aliases": ["eightball", "8b"]}, "8ball": {"prefix": True, "aliases": ["eightball", "8b"]},
"join ": {"prefix": True, "aliases": []}, "join ": {"prefix": True, "aliases": []},
"quote": {"prefix": True, "aliases": []}, "quote": {"prefix": True, "aliases": ["q"]},
"goat.mode.activate": {"prefix": True, "aliases": []}, "goat.mode.activate": {"prefix": True, "aliases": []},
"goat.mode.deactivate": {"prefix": True, "aliases": []}, "goat.mode.deactivate": {"prefix": True, "aliases": []},
"help": {"prefix": True, "aliases": []}, "help": {"prefix": True, "aliases": []},

View file

@ -38,3 +38,9 @@ ESCAPE_SEQUENCE_RE = re.compile(
)""", )""",
re.UNICODE | re.VERBOSE, re.UNICODE | re.VERBOSE,
) )
prefix = "."
def decode_escapes(s: str) -> str:
def decode_match(match):
return codecs.decode(match.group(0), "unicode-escape")
return ESCAPE_SEQUENCE_RE.sub(decode_match, s)

View file

@ -1,9 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
from bot import bot from bot import bot
from sys import argv as args
server = args[1] if args else "UNSTABLE BOT MODE" server = args[1] if args else "UNSTABLE BOT MODE"
if __name__ == "__main__": if __name__ == "__main__":
instance = bot.bot(server) instance = bot(server)
instance.mainloop() instance.mainloop()