Search in sources :

Example 1 with SafeGroovyScriptRunner

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());
    }
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) AtomicReference(java.util.concurrent.atomic.AtomicReference) Aiode(net.robinfriedli.aiode.Aiode) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) SafeGroovyScriptRunner(net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner) StoredScript(net.robinfriedli.aiode.entities.StoredScript) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) CommandFailure(net.robinfriedli.aiode.exceptions.CommandFailure) ExecutionException(java.util.concurrent.ExecutionException) Session(org.hibernate.Session) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with SafeGroovyScriptRunner

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);
}
Also used : SafeGroovyScriptRunner(net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovySandboxComponent(net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)

Example 3 with SafeGroovyScriptRunner

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);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) SafeGroovyScriptRunner(net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner) Aiode(net.robinfriedli.aiode.Aiode) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovySandboxComponent(net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)

Aggregations

SafeGroovyScriptRunner (net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner)3 Aiode (net.robinfriedli.aiode.Aiode)2 GroovySandboxComponent (net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 StoredScript (net.robinfriedli.aiode.entities.StoredScript)2 GroovyVariableManager (net.robinfriedli.aiode.scripting.GroovyVariableManager)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)1 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)1 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)1 CommandFailure (net.robinfriedli.aiode.exceptions.CommandFailure)1 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)1 Session (org.hibernate.Session)1