merge admin.py from upstream oirc

This commit is contained in:
vulpine 2022-04-28 13:47:47 -04:00 committed by GitHub
parent fd3174b1fe
commit 790aa9aff5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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[al] = ms
shared.rawm[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[al] = ms
shared.rawm[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))) asyncio.create_task(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)",