relay/main.py

228 lines
7.3 KiB
Python
Raw Permalink Normal View History

2021-03-22 06:59:51 +00:00
#!/usr/bin/env python3
import asyncio
import random
2021-03-22 06:59:51 +00:00
# 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
2021-10-13 11:57:13 +00:00
import config as cfg
2021-03-22 06:59:51 +00:00
2024-04-06 04:34:09 +00:00
## LOGGING ##
2024-04-06 07:41:17 +00:00
f_log = None
2024-04-06 04:13:40 +00:00
2024-04-06 07:41:17 +00:00
def log(type_: str, msg: str, also_print: bool = False):
"""Log a message, optionally echoed to stdout, with a type prefix."""
f_log.write(f"[{type_}] {msg}\n")
2024-04-06 04:13:40 +00:00
if also_print:
print(msg)
2024-04-06 07:41:17 +00:00
def manual_log(type_: str, msg: str, *args, **kwargs):
"""Manually log a message with given type."""
log("manual", f"({type_}) {msg}", *args, **kwargs)
2024-04-06 04:13:40 +00:00
2024-04-06 04:34:09 +00:00
## UTILS ##
# Coming soon
## BOT ##
class Server(Ir.Server):
2021-03-22 06:59:51 +00:00
# overwrite connect so i can put try except blocks there
async def connect(self, transport: S.ITCPTransport, params: Ir.ConnectionParams):
2021-03-22 06:59:51 +00:00
try:
await S.sts_transmute(params)
await S.resume_transmute(params)
2021-03-22 06:59:51 +00:00
reader, writer = await transport.connect(
2021-10-13 11:57:13 +00:00
params.host,
params.port,
tls=params.tls,
bindhost=params.bindhost,
)
2021-03-22 06:59:51 +00:00
self._reader = reader
self._writer = writer
self.params = params
await self.handshake()
except:
2024-04-06 07:41:17 +00:00
manual_log("ERROR", "connection with {} failed, disconnecting".format(self.name), also_print=True)
2021-03-22 06:59:51 +00:00
self.disconnected = True
2024-04-06 07:41:17 +00:00
def auto_log(self, msg: str, *args, **kwargs):
"""Log an automated message with server name as info."""
log(self.name, msg, *args, **kwargs)
async def line_read(self, line: It.line):
2024-04-06 04:13:40 +00:00
self.auto_log(f"< {line.format()}")
2021-03-22 06:59:51 +00:00
if line.command == "001":
2024-04-06 07:41:17 +00:00
manual_log("status", f"connected to {self.name} :D", True)
self.chans = cfg.SERVERS[self.name]["chans"]
2021-10-13 11:57:00 +00:00
self.chans_actual = []
2021-03-22 06:59:51 +00:00
for c in self.chans:
await self.send(It.build("JOIN", [c]))
2024-04-06 07:41:17 +00:00
manual_log("status", f"joined {self.name} {c} through config", True)
self.chans_actual.append(c)
2021-03-22 06:59:51 +00:00
2024-04-06 04:13:40 +00:00
elif line.command == "PRIVMSG" and line.params[0] == self.nickname:
self.auto_log(f"< {line.format()}")
2022-03-30 01:19:19 +00:00
line.params.pop(0)
nick = line.source.split("!")[0]
text = line.params[0]
if text.startswith("!") and nick in cfg.ADMINS:
2022-03-30 01:19:19 +00:00
args = text[1:].split(" ")
asyncio.create_task(self.ac(self.name, args))
return
2024-04-06 04:13:40 +00:00
elif line.command == "PRIVMSG" and line.params[0] in self.chans_actual:
2021-03-22 06:59:51 +00:00
chan = line.params.pop(0)
2021-10-13 11:57:00 +00:00
me = False
if "\1ACTION" in line.params[0]:
me = True
2021-10-13 11:57:13 +00:00
text = line.params[0].replace("\1ACTION", "").replace("\1", "")
nick = line.source.split("!")[0]
2021-10-13 11:57:00 +00:00
if me:
text = "* " + nick + text
2021-10-13 11:57:13 +00:00
if (
nick == self.nickname
or (line.tags and "batch" in line.tags)
or "\x0f\x0f\x0f\x0f" in text
):
2021-03-22 06:59:51 +00:00
return
2021-10-13 11:57:13 +00:00
if (
nick.lower() in self.users
and self.users[nick.lower()].account in cfg.ADMINS
2021-10-13 11:57:13 +00:00
):
if (
text[: len(self.nickname) + 3].lower()
== f"{self.nickname}: !".lower()
2021-10-13 11:57:13 +00:00
):
args = text[len(self.nickname) + 3 :].split(" ")
2021-10-13 11:57:13 +00:00
if args[0] == "connect" and len(args) > 4:
await self.bot.add_server(
args[1],
Ir.ConnectionParams(cfg.NICKNAME, args[2], args[3]),
2021-10-13 11:57:13 +00:00
)
2021-10-13 11:57:00 +00:00
for c in self.chans_actual:
2021-10-13 11:57:13 +00:00
await self.send(
It.build(
2021-10-13 11:57:13 +00:00
"PRIVMSG", [c, "Connected to {} :3".format(args[1])]
)
)
2021-03-23 02:20:39 +00:00
return
asyncio.create_task(self.ac(self.name, args))
2021-03-23 02:20:39 +00:00
return
for npn in cfg.NOPING:
2021-03-22 06:59:51 +00:00
offset = 1
for loc in find_all_indexes(text.lower(), npn.lower()):
text = text[: loc + offset] + "\u200b" + text[loc + offset :]
2021-03-22 06:59:51 +00:00
offset += 1
2021-10-13 11:57:00 +00:00
for server in self.bot.servers:
hide = False
if len(cfg.SERVERS[self.name]["chans"]) == 0:
2021-10-13 12:38:09 +00:00
hide = True
else:
for c in cfg.SERVERS[self.name]["chans"]:
if c.endswith(chan) and cfg.SERVERS[server].get("hidechan") == True:
2021-10-13 12:38:09 +00:00
hide = True
break
2021-10-13 11:57:13 +00:00
asyncio.create_task(
self.bot.servers[server].bc(self.name, chan, nick, text, hide)
)
2021-03-22 06:59:51 +00:00
if line.command == "INVITE":
await self.send(It.build("JOIN", [line.params[1]]))
2024-04-06 07:41:17 +00:00
manual_log("status", f"joined {self.name} {line.params[1]} through invite", True)
2021-03-29 13:03:29 +00:00
# TODO: add to relay chans if needed
2021-03-22 06:59:51 +00:00
self.chans.append(line.params[1])
2021-10-13 11:57:00 +00:00
self.chans_actual.append(line.params[1])
2021-03-22 06:59:51 +00:00
async def line_send(self, line: It.Line):
2024-04-06 04:13:40 +00:00
self.auto_log(f"> {line.format()}")
2021-03-22 06:59:51 +00:00
2021-10-13 11:57:13 +00:00
async def bc(self, name, chan, nick, msg, hide):
2024-04-06 07:41:17 +00:00
"""Handle a relay operation."""
manual_log("relay", f"{self.name}{chan}@{nick}{'(hidechan)' if hide else ''}: {msg}", True)
2021-03-22 06:59:51 +00:00
if self.disconnected or "chans" not in list(dir(self)):
return
if name == self.name and len(cfg.SERVERS[name]["chans"]) == 1:
2021-03-22 06:59:51 +00:00
return
2024-04-06 07:41:17 +00:00
2021-10-13 11:57:00 +00:00
for c in self.chans_actual:
s = f"\x0f\x0f\x0f\x0f<{nick[:1]}\u200b{nick[1:]}/{name}"
2024-04-06 07:41:17 +00:00
2021-10-13 11:57:00 +00:00
if not hide:
s += f"{chan}"
s += f"> {msg}"
2024-04-06 07:41:17 +00:00
await self.send(It.build("PRIVMSG", [c, s]))
2021-10-13 11:57:00 +00:00
2021-10-13 11:57:13 +00:00
async def ac(self, name, args):
2024-04-06 07:41:17 +00:00
"""Handle an admin command"""
manual_log("admin-command", f"{self.name}: {args}", True)
2021-10-13 11:57:13 +00:00
if self.disconnected or "chans" not in list(dir(self)):
2021-03-23 02:20:39 +00:00
return
2024-04-06 07:41:17 +00:00
2021-03-23 02:20:39 +00:00
nargs = []
isComb = False
for arg in args:
2021-10-13 11:57:13 +00:00
if arg[0] == ":":
2021-03-23 02:20:39 +00:00
isComb = True
nargs.append(arg[1:])
continue
if isComb:
2021-10-13 11:57:13 +00:00
nargs[-1] += " " + arg
2021-03-23 02:20:39 +00:00
else:
nargs.append(arg)
2024-04-06 07:41:17 +00:00
await self.send(It.build(nargs[0], nargs[1:])) # TODO: loop over chans
2021-10-13 11:57:13 +00:00
2021-03-22 06:59:51 +00:00
class Bot(Ir.Bot):
2021-03-22 06:59:51 +00:00
def create_server(self, name: str):
return Server(self, name)
def find_all_indexes(input_str, search_str):
l1 = []
length = len(input_str)
index = 0
while index < length:
i = input_str.find(search_str, index)
if i == -1:
return l1
l1.append(i)
index = i + 1
return l1
async def main():
bot = Bot()
for name, s in cfg.SERVERS.items():
params = Ir.ConnectionParams(cfg.NICKNAME, **s["connection"])
2021-03-22 06:59:51 +00:00
await bot.add_server(name, params)
await bot.run()
2021-10-13 11:57:13 +00:00
2021-03-22 06:59:51 +00:00
if __name__ == "__main__":
2024-04-06 07:41:17 +00:00
f_log = open(cfg.log_file, "w+")
2021-03-23 02:20:39 +00:00
asyncio.run(main())