sort of works. but not for production

This commit is contained in:
Hedy Li 2021-10-13 19:57:00 +08:00
parent 83c4ad16e7
commit 8e117177b9
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
2 changed files with 43 additions and 18 deletions

View File

@ -1,22 +1,23 @@
# for relay.py
NICKNAME = 'hedyrelay'
SERVERS = {
"libera": {
# host port ssl
"connection": ("irc.libera.chat", 6697, True),
"chans": ["#hedy"],
"hidechan": True,
},
"tilde": {
"connection": ("irc.tilde.chat", 6697, True),
"chans": ["##hedy"]
"chans": ["##hedy"],
"hidechan": True,
},
"foxies": {
"connection": ("foxes.are.allowed.org", 6697, True),
"chans": ["#hedy!"]
"connection": ("foxes.are.allowed.org", 6697, True, NICKNAME, NICKNAME, "tilde.cafe"),
"chans": ["#hedy"],
"hidechan": True
}
}
NICKNAME = 'hedyrelay'
ADMINS=['hedy', 'julian']

46
main.py
View File

@ -44,13 +44,21 @@ class Server(BaseServer):
if line.command == "001":
print(f"connected to {self.name}")
self.chans = SERVERS[self.name]["chans"]
self.chans_actual = []
for c in self.chans:
await self.send(build("JOIN", [c]))
# for details on the '!' see config
await self.send(build("JOIN", [c.strip('!')]))
self.chans_actual.append(c.strip('!'))
if line.command == "PRIVMSG" and line.params[0] in self.chans:
if line.command == "PRIVMSG" and line.params[0] in self.chans_actual:
chan = line.params.pop(0)
text = line.params[0].replace("\1ACTION","*").replace("\1","")
me = False
if "\1ACTION" in line.params[0]:
me = True
text = line.params[0].replace("\1ACTION","").replace("\1","")
nick = line.source.split('!')[0]
if me:
text = "* " + nick + text
if nick == self.nickname or (line.tags and "batch" in line.tags) or "\x0f\x0f\x0f\x0f" in text:
return
@ -59,7 +67,8 @@ class Server(BaseServer):
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])]))
for c in self.chans_actual:
await self.send(build("PRIVMSG",[c,"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))
@ -71,25 +80,40 @@ class Server(BaseServer):
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))
for server in self.bot.servers:
hide = False
for c in SERVERS[server]["chans"]:
if c.endswith(chan) and SERVERS[server].get("hidechan") == True:
hide = True
break
asyncio.create_task(self.bot.servers[server].bc(self.name, chan,nick,text, hide))
if line.command == "INVITE":
await self.send(build("JOIN",[line.params[1]]))
# 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 bc(self,name,chan,nick,msg):
async def bc(self,name,chan,nick,msg, hide):
if self.disconnected or "chans" not in list(dir(self)):
return
if name == self.name and len(SERVERS[name]["chans"]) == 1:
print("NAME", name, self.name)
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}"]))
print(self.name)
for c in self.chans_actual:
# if c != chan:
print("RELAYING", c)
# await self.send(build("PRIVMSG",[c,f"\x0f\x0f\x0f\x0f<{nick[:1]}\u200c{nick[1:]}@{name}{chan}> {msg}"]))
s = f"\x0f\x0f\x0f\x0f<{nick[:1]}\u200c{nick[1:]}/{name}"
if not hide:
s += f"{chan}"
s += f"> {msg}"
await self.send(build("PRIVMSG", [c, s]))
async def ac(self,name,args):
if self.disconnected or "chans" not in list(dir(self)):
@ -107,7 +131,7 @@ class Server(BaseServer):
else:
nargs.append(arg)
print("nargs:", nargs)
await self.send(build(nargs[0],[self.chans[0]]+nargs[1:])) # TODO: loop over chans
await self.send(build(nargs[0],[self.chans_actual[0]]+nargs[1:])) # TODO: loop over chans
class Bot(BaseBot):
def create_server(self, name: str):