Format with black

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

View File

@ -1,4 +1,4 @@
NICKNAME = 'hedyrelay'
NICKNAME = "hedyrelay"
SERVERS = {
"libera": {
# host port ssl
@ -12,13 +12,20 @@ SERVERS = {
"hidechan": True,
},
"foxies": {
"connection": ("foxes.are.allowed.org", 6697, True, NICKNAME, NICKNAME, "tilde.cafe"),
"connection": (
"foxes.are.allowed.org",
6697,
True,
NICKNAME,
NICKNAME,
"tilde.cafe",
),
"chans": ["#hedy"],
"hidechan": True
}
"hidechan": True,
},
}
ADMINS=['hedy', 'julian']
ADMINS = ["hedy", "julian"]
NOPING=['hedy']
NOPING = ["hedy"]

92
main.py
View File

@ -6,6 +6,7 @@ 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 *
@ -15,19 +16,18 @@ from config import *
class Server(BaseServer):
# overwrite connect so i can put try except blocks there
async def connect(self,
transport: ITCPTransport,
params: ConnectionParams):
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)
params.host,
params.port,
tls=params.tls,
tls_verify=params.tls_verify,
bindhost=params.bindhost,
)
self._reader = reader
self._writer = writer
@ -35,7 +35,7 @@ class Server(BaseServer):
self.params = params
await self.handshake()
except:
print('connection with {} failed, disconnecting'.format(self.name))
print("connection with {} failed, disconnecting".format(self.name))
self.disconnected = True
async def line_read(self, line: Line):
@ -47,37 +47,58 @@ class Server(BaseServer):
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(build("JOIN", [c.strip("!")]))
self.chans_actual.append(c.strip("!"))
if line.command == "PRIVMSG" and line.params[0] in self.chans_actual:
chan = line.params.pop(0)
me = False
if "\1ACTION" in line.params[0]:
me = True
text = line.params[0].replace("\1ACTION","").replace("\1","")
nick = line.source.split('!')[0]
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:
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]))))
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]))
),
)
for c in self.chans_actual:
await self.send(build("PRIVMSG",[c,"Connected to {} :3".format(args[1])]))
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))
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:]
text = text[: loc + offset] + "\u200c" + text[loc + offset :]
offset += 1
for server in self.bot.servers:
@ -86,10 +107,12 @@ class Server(BaseServer):
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))
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]]))
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])
@ -97,7 +120,7 @@ class Server(BaseServer):
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
async def bc(self,name,chan,nick,msg, hide):
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:
@ -114,31 +137,32 @@ class Server(BaseServer):
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)):
async def ac(self, name, args):
if self.disconnected or "chans" not in list(dir(self)):
return
nargs = []
isComb = False
for arg in args:
print("a", arg)
if arg[0] == ':':
if arg[0] == ":":
isComb = True
nargs.append(arg[1:])
continue
if isComb:
nargs[-1] += ' '+arg
nargs[-1] += " " + arg
else:
nargs.append(arg)
print("nargs:", nargs)
await self.send(build(nargs[0],[self.chans_actual[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):
return Server(self, name)
def find_all_indexes(input_str, search_str):
l1 = []
length = len(input_str)
@ -152,7 +176,6 @@ def find_all_indexes(input_str, search_str):
return l1
async def main():
bot = Bot()
for name, s in SERVERS.items():
@ -161,5 +184,6 @@ async def main():
await bot.run()
if __name__ == "__main__":
asyncio.run(main())