use of net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner in project aiode by robinfriedli.
the class ScriptCommandInterceptor method performChained.
@Override
public void performChained(Command command) {
if (command instanceof AbstractCommand) {
AbstractCommand abstractCommand = (AbstractCommand) command;
if (abstractCommand.getArgumentController().argumentSet("skipInterceptors") || abstractCommand.getCommandContribution().isDisableScriptInterceptors()) {
return;
}
} else {
return;
}
CommandContext context = command.getContext();
Session session = context.getSession();
GuildSpecification specification = context.getGuildContext().getSpecification(session);
boolean enableScripting = guildPropertyManager.getPropertyValueOptional("enableScripting", Boolean.class, specification).orElse(true);
if (!enableScripting) {
return;
}
String usageId = getUsageId();
List<StoredScript> scriptInterceptors = queryBuilderFactory.find(StoredScript.class).where((cb, root, subQueryFactory) -> cb.and(cb.isTrue(root.get("active")), cb.equal(root.get("scriptUsage"), subQueryFactory.createUncorrelatedSubQuery(StoredScript.ScriptUsage.class, "pk").where((cb1, root1) -> cb1.equal(root1.get("uniqueId"), usageId)).build(session)))).build(session).getResultList();
if (scriptInterceptors.isEmpty()) {
return;
}
Aiode aiode = Aiode.get();
SafeGroovyScriptRunner scriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, aiode.getGroovyVariableManager(), aiode.getSecurityManager(), false);
AtomicReference<StoredScript> currentScriptReference = new AtomicReference<>();
try {
scriptRunner.runScripts(scriptInterceptors, currentScriptReference, 5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
Throwable error = e.getCause() != null ? e.getCause() : e;
if (error instanceof CommandFailure) {
messageService.sendError(String.format("Executing command %1$ss failed due to an error in %1$s '%2$s'", usageId, currentScriptReference.get().getIdentifier()), context.getChannel());
} else {
EmbedBuilder embedBuilder = ExceptionUtils.buildErrorEmbed(error);
StoredScript currentScript = currentScriptReference.get();
embedBuilder.setTitle(String.format("Error occurred while executing custom command %s%s", usageId, currentScript.getIdentifier() != null ? ": " + currentScript.getIdentifier() : ""));
messageService.sendTemporary(embedBuilder.build(), context.getChannel());
}
} catch (TimeoutException e) {
StoredScript currentScript = currentScriptReference.get();
messageService.sendError(String.format("Execution of script command %ss stopped because script%s has run into a timeout", usageId, currentScript != null ? String.format(" '%s'", currentScript.getIdentifier()) : ""), context.getChannel());
}
}
use of net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner in project aiode by robinfriedli.
the class ScriptCommand method executeScript.
private void executeScript(Aiode aiode, CommandContext context, Session session) {
GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, aiode.getSecurityManager(), argumentSet("privileged"));
StoredScript script;
if (this.script != null) {
script = this.script;
} else {
String identifier = getArgumentValue("invoke");
Optional<StoredScript> storedScript = SearchEngine.searchScript(identifier, "script", session);
if (storedScript.isPresent()) {
script = storedScript.get();
} else {
throw new InvalidCommandException(String.format("No such script '%s'", identifier));
}
}
if (!script.isActive()) {
throw new InvalidCommandException(String.format("Script '%s' has been deactivated. Use the active argument to reactivate the script.", script.getIdentifier()));
}
groovyScriptRunner.runAndSendResult(script.getScript(), 10, TimeUnit.SECONDS);
}
use of net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner in project aiode by robinfriedli.
the class EvalCommand method doRun.
@Override
public void doRun() {
CommandContext context = getContext();
Aiode aiode = Aiode.get();
SecurityManager securityManager = aiode.getSecurityManager();
GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, securityManager, argumentSet("privileged"));
groovyScriptRunner.runAndSendResult(getCommandInput(), 10, TimeUnit.SECONDS);
}
Aggregations