bam/bam.py

92 lines
2.5 KiB
Python
Raw Normal View History

2022-02-14 17:34:03 +00:00
import asyncio, time
from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams
2022-02-14 18:49:30 +00:00
from secrets import OPER, NICK, NETWORK, HOST, SECONDS, MAXMSGS, JOIN, BADLINE, KILL, LOG
2022-02-14 17:34:03 +00:00
class Server(BaseServer):
def __init__(self, bot, name):
super().__init__(bot, name)
self.log={}
2022-02-14 18:49:30 +00:00
self.isoper=[]
2022-02-14 17:34:03 +00:00
async def line_read(self, line):
print(f"{self.name} < {line.format()}")
if "on_" + line.command.lower() in dir(self):
asyncio.create_task(
self.__getattribute__("on_" + line.command.lower())(line)
)
2022-02-15 00:36:44 +00:00
# disable automatic WHOing
async def _next_who(self):
pass
2022-02-14 17:34:03 +00:00
async def line_send(self, line):
print(f"{self.name} > {line.format()}")
async def on_001(self, line):
await self.send_raw(OPER)
2022-02-14 18:49:30 +00:00
async def on_313(self, line):
self.isoper.append(line.params[1])
async def on_378(self, line):
if line.params[1] in self.isoper:
self.isoper.remove(line.params[1])
return
await self.send_raw(KILL.format(line.params[1]))
await self.send_raw(BADLINE.format(line.params[2].split()[3]))
await self.send_raw(BADLINE.format(line.params[2].split()[4]))
2022-02-14 18:49:30 +00:00
2022-02-14 17:34:03 +00:00
async def on_privmsg(self, line):
nick = line.hostmask.nickname
if nick not in self.log:
self.log[nick] = []
2022-02-14 18:49:30 +00:00
self.log[nick].append([time.time()] + line.params)
if len(self.log[nick]) > MAXMSGS:
self.log[nick].pop(0)
elif len(self.log[nick]) < MAXMSGS:
return
if self.log[nick][0][0] + SECONDS <= time.time():
2022-02-14 17:34:03 +00:00
self.log[nick].pop(0)
2022-02-14 18:49:30 +00:00
return
channels = []
2022-02-14 18:49:30 +00:00
for msg in self.log[nick][:-1]:
channels.append(msg[1])
2022-02-14 18:49:30 +00:00
if msg[2] != line.params[1]:
return
if len(set(channels)) < 2:
return
2022-02-14 18:49:30 +00:00
await self.send(build("WHOIS",[nick,nick]))
await self.send_raw(LOG.format(nick,','.join(set([ln[1] for ln in self.log[nick]]))))
2022-02-14 17:34:03 +00:00
2022-02-14 18:49:30 +00:00
del self.log[nick]
2022-02-14 17:34:03 +00:00
2022-02-14 20:29:00 +00:00
async def on_invite(self, line):
await self.send(build("JOIN",[line.params[1]]))
2022-02-14 17:34:03 +00:00
class Bot(BaseBot):
def create_server(self, name: str):
return Server(self, name)
async def main():
bot = Bot()
params = ConnectionParams(NICK, host=HOST, port=6697, autojoin=JOIN)
await bot.add_server(NETWORK, params)
await bot.run()
if __name__ == "__main__":
asyncio.run(main())