use of net.robinfriedli.aiode.scripting.GroovyVariableManager in project aiode by robinfriedli.
the class AbstractScriptCommand method saveNewScript.
private void saveNewScript(QueryBuilderFactory queryBuilderFactory, Session session, CommandContext context) {
String identifier = getArgumentValue("identifier");
String script = getCommandInput();
Optional<StoredScript> existingScript = SearchEngine.searchScript(identifier, scriptUsageId, session);
if (existingScript.isPresent()) {
throw new InvalidCommandException(String.format("%s with identifier '%s' already exists", scriptUsageId, identifier));
}
Aiode aiode = Aiode.get();
GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
GroovyShell groovyShell;
if (argumentSet("privileged")) {
groovyShell = new GroovyShell();
} else {
groovyShell = new GroovyShell(aiode.getGroovySandboxComponent().getCompilerConfiguration());
}
groovyVariableManager.prepareShell(groovyShell);
try {
groovyShell.parse(script);
} catch (MultipleCompilationErrorsException e) {
throw new InvalidCommandException(String.format("Could not compile provided script:%s%s", System.lineSeparator(), ExceptionUtils.formatScriptCompilationError(e)));
}
StoredScript.ScriptUsage scriptUsage = queryBuilderFactory.find(StoredScript.ScriptUsage.class).where((cb, root) -> cb.equal(root.get("uniqueId"), scriptUsageId)).build(session).uniqueResult();
StoredScript storedScript = new StoredScript();
storedScript.setGuildId(context.getGuild().getIdLong());
storedScript.setIdentifier(identifier);
storedScript.setScript(script);
storedScript.setScriptUsage(scriptUsage);
storedScript.setActive(!argumentSet("deactivate"));
invoke(() -> session.persist(storedScript));
}
use of net.robinfriedli.aiode.scripting.GroovyVariableManager 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.GroovyVariableManager 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