Search in sources :

Example 1 with ScriptProvider

use of com.biglybt.pif.utils.ScriptProvider in project BiglyBT by BiglySoftware.

the class CoreImpl method executeScript.

void executeScript(String script, String action, boolean download_trigger) {
    String script_type = "";
    if (script.length() >= 10 && script.substring(0, 10).toLowerCase(Locale.US).startsWith("javascript")) {
        int p1 = script.indexOf('(');
        int p2 = script.lastIndexOf(')');
        if (p1 != -1 && p2 != -1) {
            script = script.substring(p1 + 1, p2).trim();
            if (script.startsWith("\"") && script.endsWith("\"")) {
                script = script.substring(1, script.length() - 1);
            }
            // allow people to escape " if it makes them feel better
            script = script.replaceAll("\\\\\"", "\"");
            script_type = ScriptProvider.ST_JAVASCRIPT;
        }
    }
    File script_file = null;
    if (script_type == "") {
        script_file = new File(script.trim());
        if (!script_file.isFile()) {
            Logger.log(new LogEvent(LOGID, "Script failed to run - '" + script_file + "' isn't a valid script file"));
            Debug.out("Invalid script: " + script_file);
            return;
        }
    }
    try {
        boolean close_vuze = action.equals("RunScriptAndClose");
        if (!close_vuze) {
            // assume script might implement a sleep
            announceAll(true);
        }
        if (script_file != null) {
            getPluginManager().getDefaultPluginInterface().getUtilities().createProcess(script_file.getAbsolutePath());
        } else {
            boolean provider_found = false;
            List<ScriptProvider> providers = CoreFactory.getSingleton().getPluginManager().getDefaultPluginInterface().getUtilities().getScriptProviders();
            for (ScriptProvider p : providers) {
                if (p.getScriptType() == script_type) {
                    provider_found = true;
                    Map<String, Object> bindings = new HashMap<>();
                    String intent = "shutdown" + "(\"" + action + "\")";
                    bindings.put("intent", intent);
                    bindings.put("is_downloading_complete", download_trigger);
                    p.eval(script, bindings);
                }
            }
            if (!provider_found) {
                if (!js_plugin_install_tried) {
                    js_plugin_install_tried = true;
                    PluginUtils.installJavaScriptPlugin();
                }
            }
        }
        if (close_vuze) {
            requestStop();
        }
    } catch (Throwable e) {
        Logger.log(new LogAlert(true, LogAlert.AT_ERROR, "Script failed to run - '" + script + "'", e));
        Debug.out("Invalid script: " + script, e);
    }
}
Also used : ScriptProvider(com.biglybt.pif.utils.ScriptProvider) LogEvent(com.biglybt.core.logging.LogEvent) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) LogAlert(com.biglybt.core.logging.LogAlert)

Example 2 with ScriptProvider

use of com.biglybt.pif.utils.ScriptProvider in project BiglyBT by BiglySoftware.

the class TagManagerImpl method evalScript.

protected Object evalScript(Tag tag, String script, DownloadManager dm, String intent_key) {
    String script_type = "";
    if (script.length() >= 10 && script.substring(0, 10).toLowerCase(Locale.US).startsWith("javascript")) {
        int p1 = script.indexOf('(');
        int p2 = script.lastIndexOf(')');
        if (p1 != -1 && p2 != -1) {
            script = script.substring(p1 + 1, p2).trim();
            if (script.startsWith("\"") && script.endsWith("\"")) {
                script = script.substring(1, script.length() - 1);
            }
            // allow people to escape " if it makes them feel better
            script = script.replaceAll("\\\\\"", "\"");
            script_type = ScriptProvider.ST_JAVASCRIPT;
        }
    }
    if (script_type == "") {
        Debug.out("Unrecognised script type: " + script);
        return (null);
    }
    boolean provider_found = false;
    List<ScriptProvider> providers = CoreFactory.getSingleton().getPluginManager().getDefaultPluginInterface().getUtilities().getScriptProviders();
    for (ScriptProvider p : providers) {
        if (p.getScriptType() == script_type) {
            provider_found = true;
            Download plugin_dm = PluginCoreUtils.wrap(dm);
            if (plugin_dm == null) {
                // deleted in the meantime
                return (null);
            }
            Map<String, Object> bindings = new HashMap<>();
            String dm_name = dm.getDisplayName();
            if (dm_name.length() > 32) {
                dm_name = dm_name.substring(0, 29) + "...";
            }
            String intent = intent_key + "(\"" + tag.getTagName() + "\",\"" + dm_name + "\")";
            bindings.put("intent", intent);
            bindings.put("download", plugin_dm);
            bindings.put("tag", tag);
            try {
                Object result = p.eval(script, bindings);
                return (result);
            } catch (Throwable e) {
                Debug.out(e);
                return (null);
            }
        }
    }
    if (!provider_found) {
        if (!js_plugin_install_tried) {
            js_plugin_install_tried = true;
            PluginUtils.installJavaScriptPlugin();
        }
    }
    return (null);
}
Also used : ScriptProvider(com.biglybt.pif.utils.ScriptProvider) Download(com.biglybt.pif.download.Download)

Aggregations

ScriptProvider (com.biglybt.pif.utils.ScriptProvider)2 LogAlert (com.biglybt.core.logging.LogAlert)1 LogEvent (com.biglybt.core.logging.LogEvent)1 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)1 Download (com.biglybt.pif.download.Download)1 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1