FireBot/bot.py

83 lines
3.1 KiB
Python
Raw Normal View History

2023-11-05 01:37:35 +00:00
#!/usr/bin/python3
from socket import socket, AF_INET, SOCK_STREAM
from overrides import bytes, bbytes
from logs import log
import re
2023-11-05 23:38:43 +00:00
from typing import NoReturn
2023-11-06 00:49:57 +00:00
from config import npbase, servers, __version__
2023-11-06 00:22:50 +00:00
2023-11-06 00:50:04 +00:00
2023-11-05 01:37:35 +00:00
class bot:
def __init__(server: str):
self.gmode = False
self.server = server
self.nicklen = 30
self.address = servers[server]["address"]
self.port = servers[server]["port"] if "port" in servers[server] else 6667
self.channels = servers[server]["channels"]
self.interval = (
servers[server]["interval"] if "interval" in servers[server] else 50
)
2023-11-06 03:08:47 +00:00
self.nick = "FireBot"
2023-11-05 01:37:35 +00:00
self.prefix = "."
self.rebt = "fire"
self.gblrebt = "all"
self.lrebt = 7 + len(rebt)
self.lgblrebt = 7 + len(gblrebt)
self.adminnames = servers[server]["admins"]
2023-11-06 03:08:47 +00:00
self.exitcode = f"bye {self.nick.lower()}"
2023-11-05 01:37:35 +00:00
self.np = re.compile(npbase.replace("MAX", f"{nicklen}"))
self.queue = []
2023-11-06 02:36:33 +00:00
self.sock = socket(AF_INET, SOCK_STREAM)
2023-11-05 01:37:35 +00:00
log(f"Start init for {server}", self.server)
2023-11-06 00:49:57 +00:00
def connect(self) -> None:
self.log(f"Joining {server}...")
2023-11-06 02:36:33 +00:00
self.sock.connect((self.address, self.port))
2023-11-06 03:08:47 +00:00
self.send(f"USER {self.nick} {self.nick} {self.nick} {self.nick}\n")
self.send(f"NICK {self.nick}\n")
2023-11-06 00:49:57 +00:00
while (
2023-11-06 03:08:47 +00:00
ircmsg.find("MODE " + self.nick) == -1
and ircmsg.find("PRIVMSG " + self.nick) == -1
2023-11-06 00:49:57 +00:00
):
ircmsg = self.recv().decode()
if ircmsg != "":
print(bytes(ircmsg).lazy_decode())
if ircmsg.find("NICKLEN=") != -1:
self.nicklen = int(ircmsg.split("NICKLEN=")[1].split(" ")[0])
self.np = re.compile(npbase.replace("MAX", f"{nicklen}"))
self.log(f"NICKLEN set to {nicklen}")
2023-11-06 02:05:34 +00:00
elif ircmsg.find("Nickname") != -1:
2023-11-06 02:24:43 +00:00
self.log("Nickname in use", "WARN")
2023-11-06 03:08:47 +00:00
self.nick = f"{self.nick}{r.randint(0,1000)}"
self.send(f"NICK {self.nick}\n")
self.log(f"nick is now {self.nick}")
2023-11-06 02:05:34 +00:00
elif ircmsg.startswith("PING "):
2023-11-06 00:49:57 +00:00
self.ping(ircmsg)
2023-11-06 02:05:34 +00:00
elif len(ircmsg.split("\x01")) == 3:
2023-11-06 00:49:57 +00:00
self.CTCPHandler(ircmsg, isRaw=True)
2023-11-06 02:05:34 +00:00
elif ircmsg.find("Closing Link") != -1:
2023-11-06 00:49:57 +00:00
self.exit("Closing Link")
else:
self.exit("Lost connection to the server")
self.log(f"Joined {server} successfully!")
2023-11-05 01:37:35 +00:00
def send(self, command: str) -> int:
2023-11-05 22:26:50 +00:00
return ircsock.send(bytes(command))
2023-11-05 01:37:35 +00:00
def recv(self) -> bytes:
2023-11-05 22:26:50 +00:00
if self.queue:
return bytes(self.queue.pop(0))
2023-11-06 02:36:33 +00:00
data = bytes(self.sock.recv(2048).strip(b"\r\n"))
2023-11-05 22:26:50 +00:00
if b"\r\n" in data:
self.queue.extend(data.split(b"\r\n"))
return bytes(self.queue.pop(0))
return data
2023-11-05 01:37:35 +00:00
2023-11-06 02:24:43 +00:00
def log(self, message: object, level: str = "LOG") -> None:
2023-11-05 01:37:35 +00:00
log(message, self.server)
2023-11-05 23:38:43 +00:00
def exit(message: object) -> NoReturn:
2023-11-05 01:37:35 +00:00
log(message, self.server, "EXIT")
exit(1)