Add logging and remove star-import (FINALLY)

This commit is contained in:
hedy 2024-04-05 05:33:19 +00:00
parent c556a43d99
commit 745c6ed116
3 changed files with 46 additions and 38 deletions

View File

@ -24,3 +24,5 @@ SERVERS = {
ADMINS = ["hedy"]
NOPING = ["hedy", "jmjl"]
log_file = "devlog.txt"

View File

@ -24,3 +24,5 @@ SERVERS = {
ADMINS = ["hedy"]
NOPING = ["hedy", "jmjl"]
log_file = "log.txt"

80
main.py
View File

@ -1,25 +1,26 @@
#!/usr/bin/env python3
import asyncio, random
import asyncio
import random
from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams
# xfnw was too lazy to import any more, so he went with a star-import
# :<
# well, I'm also lazy, but I am IN NO WAY letting my LSP scream at me for
# all the unknown symbols, I just use an alias instead.
import irctokens as It
import ircrobots as Ir
import ircrobots.server as S
# xfnw was too lazy to import any more, so...
from ircrobots.server import *
from config import *
import config as cfg
class Server(BaseServer):
class Server(Ir.Server):
# overwrite connect so i can put try except blocks there
async def connect(self, transport: ITCPTransport, params: ConnectionParams):
async def connect(self, transport: S.ITCPTransport, params: Ir.ConnectionParams):
try:
await sts_transmute(params)
await resume_transmute(params)
await S.sts_transmute(params)
await S.resume_transmute(params)
reader, writer = await transport.connect(
params.host,
@ -37,23 +38,25 @@ class Server(BaseServer):
print("connection with {} failed, disconnecting".format(self.name))
self.disconnected = True
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
self.f_log = open(cfg.log_file, "w+")
async def line_read(self, line: It.line):
self.f_log.write(f"{self.name} < {line.format()}\n")
if line.command == "001":
print(f"connected to {self.name}")
self.chans = SERVERS[self.name]["chans"]
self.chans = cfg.SERVERS[self.name]["chans"]
self.chans_actual = []
for c in self.chans:
# for details on the '!' see config
await self.send(build("JOIN", [c.strip("!")]))
self.chans_actual.append(c.strip("!"))
await self.send(It.build("JOIN", [c]))
print(f"joined {self.name} {c} through config")
self.chans_actual.append(c)
if line.command == "PRIVMSG" and line.params[0] == self.nickname:
line.params.pop(0)
nick = line.source.split("!")[0]
text = line.params[0]
if text.startswith("!") and nick in ADMINS:
if text.startswith("!") and nick in cfg.ADMINS:
args = text[1:].split(" ")
asyncio.create_task(self.ac(self.name, args))
return
@ -76,7 +79,7 @@ class Server(BaseServer):
if (
nick.lower() in self.users
and self.users[nick.lower()].account in ADMINS
and self.users[nick.lower()].account in cfg.ADMINS
):
if (
text[: len(self.nickname) + 3].lower()
@ -86,11 +89,11 @@ class Server(BaseServer):
if args[0] == "connect" and len(args) > 4:
await self.bot.add_server(
args[1],
ConnectionParams(NICKNAME, args[2], args[3]),
Ir.ConnectionParams(cfg.NICKNAME, args[2], args[3]),
)
for c in self.chans_actual:
await self.send(
build(
It.build(
"PRIVMSG", [c, "Connected to {} :3".format(args[1])]
)
)
@ -98,7 +101,7 @@ class Server(BaseServer):
asyncio.create_task(self.ac(self.name, args))
return
for npn in NOPING:
for npn in cfg.NOPING:
offset = 1
for loc in find_all_indexes(text.lower(), npn.lower()):
text = text[: loc + offset] + "\u200b" + text[loc + offset :]
@ -106,11 +109,11 @@ class Server(BaseServer):
for server in self.bot.servers:
hide = False
if len(SERVERS[self.name]["chans"]) == 0:
if len(cfg.SERVERS[self.name]["chans"]) == 0:
hide = True
else:
for c in SERVERS[self.name]["chans"]:
if c.endswith(chan) and SERVERS[server].get("hidechan") == True:
for c in cfg.SERVERS[self.name]["chans"]:
if c.endswith(chan) and cfg.SERVERS[server].get("hidechan") == True:
hide = True
break
asyncio.create_task(
@ -118,28 +121,30 @@ class Server(BaseServer):
)
if line.command == "INVITE":
await self.send(build("JOIN", [line.params[1]]))
await self.send(It.build("JOIN", [line.params[1]]))
print(f"joined {self.name} {line.params[1]} through invite")
# TODO: add to relay chans if needed
self.chans.append(line.params[1])
self.chans_actual.append(line.params[1])
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
async def line_send(self, line: It.Line):
self.f_log.write(f"{self.name} > {line.format()}\n")
async def bc(self, name, chan, nick, msg, hide):
print(f"<RELAY> {self.name}{chan}@{nick}{'(hidechan)' if hide else ''}: {msg}")
if self.disconnected or "chans" not in list(dir(self)):
return
if name == self.name and len(SERVERS[name]["chans"]) == 1:
if name == self.name and len(cfg.SERVERS[name]["chans"]) == 1:
return
for c in self.chans_actual:
# if c != chan:
s = f"\x0f\x0f\x0f\x0f<{nick[:1]}\u200b{nick[1:]}/{name}"
if not hide:
s += f"{chan}"
s += f"> {msg}"
await self.send(build("PRIVMSG", [c, s]))
await self.send(It.build("PRIVMSG", [c, s]))
async def ac(self, name, args):
print(f"<ADMIN_CMD> {self.name}: {args}")
if self.disconnected or "chans" not in list(dir(self)):
return
nargs = []
@ -153,11 +158,10 @@ class Server(BaseServer):
nargs[-1] += " " + arg
else:
nargs.append(arg)
print("nargs:", nargs)
await self.send(build(nargs[0], nargs[1:])) # TODO: loop over chans
await self.send(It.build(nargs[0], nargs[1:])) # TODO: loop over chans
class Bot(BaseBot):
class Bot(Ir.Bot):
def create_server(self, name: str):
return Server(self, name)
@ -177,8 +181,8 @@ def find_all_indexes(input_str, search_str):
async def main():
bot = Bot()
for name, s in SERVERS.items():
params = ConnectionParams(NICKNAME, **s["connection"])
for name, s in cfg.SERVERS.items():
params = Ir.ConnectionParams(cfg.NICKNAME, **s["connection"])
await bot.add_server(name, params)
await bot.run()