From dadb977ea1b5123ce06d58058e5b950697484b20 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Mon, 22 Mar 2021 06:59:51 +0000 Subject: [PATCH] hi! --- config.py | 19 +++++++ main.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 157 insertions(+) create mode 100644 config.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/config.py b/config.py new file mode 100644 index 0000000..4d3199b --- /dev/null +++ b/config.py @@ -0,0 +1,19 @@ +# for relay.py + + +SERVERS = { + "freenode": { + "connection": ("chat.freenode.net", 6697, True), + "chans": ["#hedy", "#hedy2"], + }, + "tilde": { + "connection": ("irc.tilde.chat", 6697, True), + "chans": ["##hedy"] + }, +} + +NICKNAME = 'hbot' + +ADMINS=['hedy'] + +NOPING=[] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e5c468e --- /dev/null +++ b/main.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 + +import asyncio, 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... +from ircrobots.server import * + +from config import * + + +class Server(BaseServer): + + # overwrite connect so i can put try except blocks there + async def connect(self, + transport: ITCPTransport, + params: ConnectionParams): + try: + await sts_transmute(params) + await resume_transmute(params) + + reader, writer = await transport.connect( + params.host, + params.port, + tls =params.tls, + tls_verify=params.tls_verify, + bindhost =params.bindhost) + + self._reader = reader + self._writer = writer + + self.params = params + await self.handshake() + except: + print('connection with {} failed, disconnecting'.format(self.name)) + self.disconnected = True + + async def line_read(self, line: Line): + print(f"{self.name} < {line.format()}") + + if line.command == "001": + print(f"connected to {self.name}") + self.chans = SERVERS[self.name]["chans"] + for c in self.chans: + await self.send(build("JOIN", [c])) + + if line.command == "PRIVMSG" and line.params[0] in self.chans: + chan = line.params.pop(0) + text = line.params[0].replace("\1ACTION","*").replace("\1","") + nick = line.source.split('!')[0] + if nick == self.nickname or (line.tags and "batch" in line.tags) or "\x0f\x0f\x0f\x0f" in text: + return + + # if nick.lower() in self.users and self.users[nick.lower()].account in ADMINS: + # if text[:len(self.nickname)+2].lower() == f'{self.nickname}: '.lower(): + # args = text[len(self.nickname)+2:].split(' ') + # if args[0] == 'connect' and len(args) > 4: + # await self.bot.add_server(args[1],ConnectionParams(NICKNAME,args[2],args[3],bool(int(args[4])))) + # await self.send(build("PRIVMSG",[self.chans,"Connected to {} :3".format(args[1])])) + # return + # for i in random.sample(list(self.bot.servers),len(self.bot.servers)): + # asyncio.create_task(self.bot.servers[i].ac(self.name,args)) + # return + for npn in NOPING: + offset = 1 + for loc in find_all_indexes(text.lower(), npn.lower()): + text = text[:loc+offset]+"\u200c"+text[loc+offset:] + offset += 1 + + for i in self.bot.servers: + asyncio.create_task(self.bot.servers[i].bc(self.name, chan,nick,text)) + + if line.command == "INVITE": + await self.send(build("JOIN",[line.params[1]])) + self.chans.append(line.params[1]) + + async def line_send(self, line: Line): + print(f"{self.name} > {line.format()}") + + async def bc(self,name,chan,nick,msg): + if self.disconnected or "chans" not in list(dir(self)): + return + if name == self.name and len(SERVERS[name]["chans"]) == 1: + return + for c in self.chans: + if c != chan: + await self.send(build("PRIVMSG",[c,f"\x0f\x0f\x0f\x0f<{nick[:1]}\u200c{nick[1:]}@{name}{chan}> {msg}"])) + + # async def ac(self,name,args): + # if self.disconnected or "chan" not in list(dir(self)): + # return + # nargs = [] + # isComb = False + # for arg in args: + # if arg[0] == ':': + # isComb = True + # nargs.append(arg[1:]) + # continue + # if isComb: + # nargs[-1] += ' '+arg + # else: + # nargs.append(arg) + # await self.send(build(nargs[0],[self.chan]+nargs[1:])) + +class Bot(BaseBot): + 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 SERVERS.items(): + params = ConnectionParams(NICKNAME, *s["connection"]) + await bot.add_server(name, params) + + await bot.run() + +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fd4ab91 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +ircrobots