Compare commits

...

6 commits

Author SHA1 Message Date
2ac269d7f8 ignore mutations of rawm while iterating 2022-05-12 08:43:29 -06:00
bccf3c855a fix addspook not deleting itself 2022-05-12 08:38:29 -06:00
c42e5edcc1
put channel back, whoops 2022-04-28 13:50:14 -04:00
790aa9aff5
merge admin.py from upstream oirc 2022-04-28 13:47:47 -04:00
fd3174b1fe
merge bot.py from upstream oirc 2022-04-28 13:45:08 -04:00
f67b269b1b
build privmsg instead of using self.message
self.message will somewhat change behavior
2022-04-27 19:59:03 -04:00
3 changed files with 95 additions and 53 deletions

17
bot.py
View file

@ -19,9 +19,7 @@ def is_admin(func):
): ):
await func(self, channel, nick, msg) await func(self, channel, nick, msg)
else: else:
await message( await message(self, channel, "you do not have permission to do that")
self, "core", channel, "you do not have permission to do that"
)
return decorator return decorator
@ -54,7 +52,7 @@ def rawm(rname):
async def message(self, channel, msg): async def message(self, channel, msg):
modname = os.path.splittext(os.path.basename(inspect.stack()[1].filename))[0] modname = os.path.splitext(os.path.basename(inspect.stack()[:2][-1].filename))[0]
await self.send(build("PRIVMSG", [channel, f"[\x036{modname}\x0f] {msg}"])) await self.send(build("PRIVMSG", [channel, f"[\x036{modname}\x0f] {msg}"]))
@ -68,10 +66,10 @@ class Server(BaseServer):
if listener[0] == line.command: if listener[0] == line.command:
asyncio.create_task(listener[1](self, line)) asyncio.create_task(listener[1](self, line))
def line_preread(self, line: Line): async def line_preread(self, line: Line):
print(f"{self.name} < {line.format()}") print(f"{self.name} < {line.format()}")
def line_presend(self, line: Line): async def line_presend(self, line: Line):
print(f"{self.name} > {line.format()}") print(f"{self.name} > {line.format()}")
async def on_001(self, line): async def on_001(self, line):
@ -84,9 +82,9 @@ class Server(BaseServer):
asyncio.create_task(m.init(self)) asyncio.create_task(m.init(self))
shared.modules[i] = m shared.modules[i] = m
# depricated, to support old modules
async def message(self, channel, msg): async def message(self, channel, msg):
await self.send(build("PRIVMSG", [channel, msg])) modname = os.path.splitext(os.path.basename(inspect.stack()[:2][-1].filename))[0]
await self.send(build("PRIVMSG", [channel, f"[\x036{modname}\x0f] {msg}"]))
async def on_privmsg(self, line): async def on_privmsg(self, line):
if line.tags and "batch" in line.tags and line.tags["batch"] == "1": if line.tags and "batch" in line.tags and line.tags["batch"] == "1":
@ -105,7 +103,7 @@ class Server(BaseServer):
await self.handle_command(channel, nick, msg) await self.handle_command(channel, nick, msg)
async def handle_rawm(self, channel, nick, msg): async def handle_rawm(self, channel, nick, msg):
for i in shared.rawm: for i in list(shared.rawm):
await shared.rawm[i](self, channel, nick, msg) await shared.rawm[i](self, channel, nick, msg)
async def handle_command(self, channel, nick, msg): async def handle_command(self, channel, nick, msg):
@ -129,7 +127,6 @@ class Bot(BaseBot):
def create_server(self, name: str): def create_server(self, name: str):
return Server(self, name) return Server(self, name)
async def main(): async def main():
bot = Bot() bot = Bot()

View file

@ -1,4 +1,4 @@
import importlib, time, asyncio, random import importlib, time, asyncio, random, sys
from bot import * from bot import *
quitmessages = [ quitmessages = [
@ -11,15 +11,17 @@ quitmessages = [
async def commit(self, chan, source, msg): async def commit(self, chan, source, msg):
await self.quit("{} told me to commit {}".format(source, msg)) await self.send(build("QUIT", [f"{source} told me to commit {msg}!"]))
sys.exit()
async def quit(self, chan, source, msg): async def quit(self, chan, source, msg):
await self.send(build("QUIT", [random.choice(quitmessages)])) await self.send(build("QUIT", [random.choice(quitmessages)]))
sys.exit()
async def reloadmods(self, chan, source, msg): async def reloadmods(self, chan, source, msg):
await self.message(chan, "[\x036admin\x0f] reloading modules...") await self.message(chan, "reloading modules...")
shared.oldcmd = shared.commands shared.oldcmd = shared.commands
shared.commands = {} shared.commands = {}
shared.rawm = {} shared.rawm = {}
@ -29,14 +31,13 @@ async def reloadmods(self, chan, source, msg):
for i in shared.modules: for i in shared.modules:
importlib.reload(shared.modules[i]) importlib.reload(shared.modules[i])
await shared.modules[i].init(self) await shared.modules[i].init(self)
# await self.message(chan, '[\x036admin\x0f] load {} sucess!'.format(i)) # await self.message(chan, 'load {} sucess!'.format(i))
await self.message( await self.message(
chan, chan, "done! {} modules reloaded!".format(len(shared.modules))
"[\x036admin\x0f] done! {} modules reloaded!".format(len(shared.modules)),
) )
except: except:
await self.message( await self.message(
chan, "[\x036admin\x0f] reload failed... attempting to recover..." chan, "reload failed... {}...".format(repr(sys.exc_info()[1]))
) )
shared.commands = shared.oldcmd shared.commands = shared.oldcmd
@ -46,104 +47,114 @@ async def rawcmd(self, chan, source, msg):
async def joins(self, chan, source, msg): async def joins(self, chan, source, msg):
await self.message(chan, "[\x036admin\x0f] joining slowly as to not flood...") await self.message(chan, "joining slowly as to not flood...")
for i in self.chandb.all(): for i in self.chandb.all():
await self.send(build("JOIN", [i["name"]])) await self.send(build("JOIN", [i["name"]]))
await asyncio.sleep(1) await asyncio.sleep(1)
print("joined {}".format(i["name"])) print("joined {}".format(i["name"]))
await self.message( await self.message(
chan, chan,
"[\x036admin\x0f] Sucess! i may be laggy for a bit while i sort through all these channels...", "Sucess! i may be laggy for a bit while i sort through all these channels...",
) )
async def aexec(self, code): async def aexec(self, code, chan=None, source=None, msg=None):
# Make an async function with the code and `exec` it # Make an async function with the code and `exec` it
exec(f"async def __ex(self): " + "".join(f"\n {l}" for l in code.split("\n"))) exec(
f"async def __ex(self, chan, source, msg): "
+ "".join(f"\n {l}" for l in code.split("\\n"))
)
# Get `__ex` from local variables, call it and return the result # Get `__ex` from local variables, call it and return the result
return await locals()["__ex"](self) return await locals()["__ex"](self, chan, source, msg)
async def ev(self, chan, source, msg): async def ev(self, chan, source, msg):
msg = msg.split(" ") msg = msg.split(" ")
try: try:
await self.message( await self.message(
chan, chan, "ok, output: {}".format(str(await aexec(self, " ".join(msg)))[:400])
"[\x036admin\x0f] ok, output: {}".format(
str(await aexec(self, " ".join(msg)))[:400]
),
) )
except: except:
await self.message(chan, "[\x036admin\x0f] exception in eval!") await self.message(chan, "ut oh! {}".format(repr(sys.exc_info()[1])))
async def send(self, c, n, m): async def send(self, c, n, m):
msg = m.split(" ") msg = m.split(" ")
await self.message(msg.pop(0), " ".join(msg)) await self.message(msg.pop(0), " ".join(msg))
await self.message(c, "[\x036admin\x0f] sent") await self.message(c, "sent")
async def shut(self, c, n, m): async def shut(self, c, n, m):
shared.qtime[c] = time.time() + (60 * 10) shared.qtime[c] = time.time() + (60 * 10)
await self.message(c, "[\x036admin\x0f] Ok, il be back in 10 minutes") await self.message(c, "Ok, il be back in 10 minutes")
async def schans(self, c, n, m): async def schans(self, c, n, m):
self.chandb.delete() self.chandb.delete()
for i in self.channels: for i in self.channels:
self.chandb.insert(dict(name=i)) self.chandb.insert(dict(name=i))
await self.message(c, "[\x036admin\x0f] Ok") await self.message(c, "Ok")
async def addalias(self, c, n, m): async def addalias(self, c, n, m):
al = m.split(" ")[0] al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in self.cmd: Alias(al, m)
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy")
return
self.cmd[al] = Alias(m).alias
await self.message(c, '[\x036admin\x0f] added "{}" alias for "{}"'.format(al, m)) await self.message(c, 'added "{}" alias for "{}"'.format(al, m))
async def addcommand(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
Command(al, m)
await self.message(c, 'added "{}" alias for "{}"'.format(al, m))
async def addot(self, c, n, m): async def addot(self, c, n, m):
al = m.split(" ")[0] al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in shared.rawm: if al in shared.rawm:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy") await self.message(c, "no dont overwrite a command dummy")
return return
shared.rawm[al] = Ot(m, al).ot Ot(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" trigger for "{}"'.format(al, m)) await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
async def addspook(self, c, n, m): async def addspook(self, c, n, m):
al = m.split(" ")[0] al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in shared.rawm: if al in shared.rawm:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy") await self.message(c, "no dont overwrite a command dummy")
return return
shared.rawm[al] = Spook(m, al).spook Spook(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" trigger for "{}"'.format(al, m)) await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
async def addtrigger(self, c, n, m): async def addtrigger(self, c, n, m):
al = m.split(" ")[0] al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in shared.rawm: if al in shared.rawm:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy") await self.message(c, "no dont overwrite a command dummy")
return return
shared.rawm[al] = Trigger(m, al).trigger Trigger(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" trigger for "{}"'.format(al, m)) await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
class Ot: class Ot:
def __init__(self, ms, al):
ots = {}
def __init__(self, al, ms):
self.ms = str(ms) self.ms = str(ms)
self.al = str(al) self.al = str(al)
self.__class__.ots[self.al] = ms
shared.rawm[self.al] = self.ot
async def ot(alself, self, c, n, m): async def ot(alself, self, c, n, m):
if alself.al in m and n != self.nickname: if alself.al in m and n != self.nickname:
@ -156,20 +167,30 @@ class Ot:
class Spook: class Spook:
def __init__(self, ms, al):
spooks = {}
def __init__(self, al, ms):
self.ms = str(ms) self.ms = str(ms)
self.al = str(al) self.al = str(al)
self.__class__.spooks[self.al] = ms
shared.rawm[self.al] = self.spook
async def spook(alself, self, c, n, m): async def spook(alself, self, c, n, m):
if alself.al in m and n != self.nickname: if alself.al in m and n != self.nickname:
asyncio.create_task(self.message(c, alself.ms.format(m))) self.send(build("PRIVMSG", [c, alself.ms.format(m)]))
shared.rawm.pop(alself.al) shared.rawm.pop(alself.al)
class Trigger: class Trigger:
def __init__(self, ms, al):
triggers = {}
def __init__(self, al, ms):
self.ms = str(ms) self.ms = str(ms)
self.al = str(al) self.al = str(al)
self.__class__.triggers[al] = ms
shared.rawm[al] = self.trigger
async def trigger(alself, self, c, n, m): async def trigger(alself, self, c, n, m):
if alself.al in m: if alself.al in m:
@ -180,9 +201,33 @@ class Trigger:
) )
class Alias: class Command:
def __init__(self, ms):
commands = {}
def __init__(self, cmd, ms):
self.ms = str(ms) self.ms = str(ms)
self.__class__.commands[cmd] = ms
shared.commands[cmd] = self.command
async def command(alself, self, chan, source, msg):
try:
out = await aexec(self, alself.ms, chan, source, msg)
except:
await self.message(chan, "ut oh! {}".format(repr(sys.exc_info()[1])))
else:
if out is not None and len(out) > 0:
await self.message(chan, str(out))
class Alias:
aliases = {}
def __init__(self, cmd, ms):
self.ms = str(ms)
self.__class__.aliases[cmd] = ms
shared.commands[cmd] = self.alias
async def alias(alself, self, c, n, m): async def alias(alself, self, c, n, m):
asyncio.create_task( asyncio.create_task(
@ -203,6 +248,7 @@ commands = {
"shut": shut, "shut": shut,
"schans": schans, "schans": schans,
"addalias": addalias, "addalias": addalias,
"addcommand": addcommand,
"addtrigger": addtrigger, "addtrigger": addtrigger,
"addot": addot, "addot": addot,
"addspook": addspook, "addspook": addspook,
@ -214,7 +260,7 @@ commands = {
async def adminHandle(self, chan, source, msg): async def adminHandle(self, chan, source, msg):
msg = msg.split(" ") msg = msg.split(" ")
if len(msg) < 1 or not msg[0] in commands: if len(msg) < 1 or not msg[0] in commands:
await self.message(chan, "[\x036admin\x0f] Invalid command") await self.message(chan, "Invalid command")
return return
print("[ADMIN MODULE] {} told me to {}!!!".format(source, msg[0])) print("[ADMIN MODULE] {} told me to {}!!!".format(source, msg[0]))
asyncio.create_task(commands[msg.pop(0)](self, chan, source, " ".join(msg))) asyncio.create_task(commands[msg.pop(0)](self, chan, source, " ".join(msg)))
@ -225,7 +271,6 @@ async def init(self):
self.admins = ["xfnw"] self.admins = ["xfnw"]
return return
self.cmd["admin"] = adminHandle
self.help["admin"] = [ self.help["admin"] = [
"admin - various bot owner commands (more for subcommands)", "admin - various bot owner commands (more for subcommands)",

View file

@ -160,7 +160,7 @@ async def go(self, c, n, m):
) )
if msg[-1] == "\x01" and msg[0] != "\x01": if msg[-1] == "\x01" and msg[0] != "\x01":
msg = msg[:-1] msg = msg[:-1]
await self.message(c, msg) await self.send(build("PRIVMSG", [c, msg]))
async def init(self): async def init(self):