2018-09-29 08:45:08 +00:00
|
|
|
|
2018-09-29 14:05:50 +00:00
|
|
|
import glob, json, os, subprocess
|
2018-10-12 17:07:23 +00:00
|
|
|
from src import IRCObject, ModuleManager, utils
|
2018-09-29 08:45:08 +00:00
|
|
|
|
2018-10-12 17:07:23 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def on_load(self):
|
|
|
|
self._directory = os.path.join(self.bot.directory, "modules", "scripts")
|
2018-09-29 14:05:50 +00:00
|
|
|
self._hooks = []
|
|
|
|
self._load_scripts()
|
2018-09-29 08:45:08 +00:00
|
|
|
|
2018-09-29 14:05:50 +00:00
|
|
|
def _load_scripts(self):
|
2018-09-29 08:45:08 +00:00
|
|
|
for filename in glob.glob(os.path.join(self._directory, "*")):
|
|
|
|
name = os.path.basename(filename)
|
2018-10-03 12:22:37 +00:00
|
|
|
for hashflag, value in utils.get_hashflags(filename):
|
2018-09-29 08:45:08 +00:00
|
|
|
if hashflag == "name" and value:
|
|
|
|
name = value
|
|
|
|
elif hashflag == "hook" and value:
|
2018-10-06 15:18:59 +00:00
|
|
|
hook_fn = self._make_hook(filename, name)
|
|
|
|
hook = self.events.on(value).hook(hook_fn)
|
2018-09-29 14:05:50 +00:00
|
|
|
self._hooks.append([value, hook])
|
|
|
|
|
2018-10-06 15:18:59 +00:00
|
|
|
def _make_hook(self, filename, name):
|
|
|
|
return lambda event: self.call(event, filename, name)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.reloadscripts", permission="reloadscripts")
|
2018-09-29 14:05:50 +00:00
|
|
|
def reload(self, event):
|
|
|
|
for event_name, hook in self._hooks:
|
|
|
|
self.events.on(event_name).unhook(hook)
|
|
|
|
self._load_scripts()
|
|
|
|
event["stdout"].write("Reloaded all scripts")
|
2018-09-29 08:45:08 +00:00
|
|
|
|
|
|
|
def call(self, event, filename, name):
|
2018-10-07 10:24:00 +00:00
|
|
|
env = {
|
|
|
|
"HOME": os.environ["HOME"],
|
|
|
|
"PATH": os.environ["PATH"]
|
|
|
|
}
|
|
|
|
|
2018-09-29 08:45:08 +00:00
|
|
|
env["EVENT"] = event.name
|
|
|
|
for key, value in event.kwargs.items():
|
2018-09-29 14:05:50 +00:00
|
|
|
if isinstance(value, (str,)):
|
|
|
|
env[key.upper()] = value
|
|
|
|
elif isinstance(value, (bool,)):
|
|
|
|
env[key.upper()] = str(int(value))
|
|
|
|
elif isinstance(value, (list, dict)):
|
|
|
|
env[key.upper()] = json.dumps(value)
|
2018-10-01 12:48:55 +00:00
|
|
|
elif isinstance(value, (IRCObject.Object,)):
|
|
|
|
env[key.upper()] = str(value)
|
2018-09-29 08:45:08 +00:00
|
|
|
|
2018-10-07 07:22:46 +00:00
|
|
|
proc = subprocess.Popen([filename], env=env,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2018-09-29 08:45:08 +00:00
|
|
|
try:
|
|
|
|
proc.wait(5)
|
2018-10-08 22:35:37 +00:00
|
|
|
except subprocess.TimeoutExpired as e:
|
|
|
|
proc.kill()
|
2018-09-29 08:45:08 +00:00
|
|
|
return
|
|
|
|
|
2018-09-29 08:46:47 +00:00
|
|
|
out = proc.stdout.read().decode("utf8").strip("\n")
|
2018-09-29 08:45:08 +00:00
|
|
|
if out:
|
|
|
|
if proc.returncode == 0:
|
|
|
|
if "stdout" in event:
|
|
|
|
event["stdout"].set_prefix(name)
|
|
|
|
event["stdout"].write(out)
|
|
|
|
else:
|
|
|
|
if "stderr" in event:
|
|
|
|
event["stderr"].set_prefix(name)
|
|
|
|
event["stderr"].write(out)
|