示例#1
0
struct t_hashtable *
dogechat_js_object_to_hashtable (v8::Handle<v8::Object> obj,
                                int size,
                                const char *type_keys,
                                const char *type_values)
{
    struct t_hashtable *hashtable;
    unsigned int i;
    v8::Handle<v8::Array> keys;
    v8::Handle<v8::Value> key, value;

    hashtable = dogechat_hashtable_new (size, type_keys, type_values,
                                       NULL, NULL);

    if (!hashtable)
        return NULL;

    keys = obj->GetPropertyNames();
    for (i = 0; i < keys->Length(); i++)
    {
        key = keys->Get(i);
        value = obj->Get(key);
        v8::String::Utf8Value str_key(key);
        v8::String::Utf8Value str_value(value);
        if (strcmp (type_values, DOGECHAT_HASHTABLE_STRING) == 0)
        {
            dogechat_hashtable_set (hashtable, *str_key, *str_value);
        }
        else if (strcmp (type_values, DOGECHAT_HASHTABLE_POINTER) == 0)
        {
            dogechat_hashtable_set (hashtable, *str_key,
                                   plugin_script_str2ptr (dogechat_js_plugin,
                                                          NULL, NULL,
                                                          *str_value));
        }
    }

    return hashtable;
}
示例#2
0
void
script_action_install (int quiet)
{
    struct t_script_repo *ptr_script_to_install;
    char *filename, *url;
    struct t_hashtable *options;

    while (1)
    {
        ptr_script_to_install = script_action_get_next_script_to_install ();

        /* no more script to install? just exit function */
        if (!ptr_script_to_install)
            return;

        /*
         * script to install and plugin is loaded: exit loop and go on with
         * install
         */
        if (script_plugin_loaded[ptr_script_to_install->language])
            break;

        /* plugin not loaded for language of script: display error */
        dogechat_printf (NULL,
                        _("%s: script \"%s\" can not be installed because "
                          "plugin \"%s\" is not loaded"),
                        SCRIPT_PLUGIN_NAME,
                        ptr_script_to_install->name_with_extension,
                        script_language[ptr_script_to_install->language]);
    }

    filename = script_config_get_script_download_filename (ptr_script_to_install,
                                                           NULL);
    if (filename)
    {
        options = dogechat_hashtable_new (32,
                                         DOGECHAT_HASHTABLE_STRING,
                                         DOGECHAT_HASHTABLE_STRING,
                                         NULL,
                                         NULL);
        if (options)
        {
            url = script_build_download_url (ptr_script_to_install->url);
            if (url)
            {
                if (!dogechat_config_boolean (script_config_look_quiet_actions))
                {
                    dogechat_printf (NULL,
                                    _("%s: downloading script \"%s\"..."),
                                    SCRIPT_PLUGIN_NAME,
                                    ptr_script_to_install->name_with_extension);
                }
                dogechat_hashtable_set (options, "file_out", filename);
                dogechat_hook_process_hashtable (
                    url,
                    options,
                    dogechat_config_integer (script_config_scripts_download_timeout) * 1000,
                    &script_action_install_process_cb,
                    (quiet) ? (void *)1 : (void *)0);
                free (url);
            }
            dogechat_hashtable_free (options);
        }
        free (filename);
    }
}
示例#3
0
void
script_action_show (const char *name, int quiet)
{
    struct t_script_repo *ptr_script;
    char *filename, *url;
    struct t_hashtable *options;

    if (name)
    {
        ptr_script = script_repo_search_by_name_ext (name);
        if (ptr_script)
        {
            script_buffer_show_detail_script (ptr_script);
            if (dogechat_config_boolean (script_config_look_display_source))
            {
                dogechat_printf_y (script_buffer,
                                  script_buffer_detail_script_last_line++,
                                  _("Source code:"));
                dogechat_printf_y (script_buffer,
                                  script_buffer_detail_script_last_line++,
                                  "%s----------------------------------------"
                                  "----------------------------------------",
                                  dogechat_color ("lightcyan"));
                dogechat_printf_y (script_buffer,
                                  script_buffer_detail_script_last_line,
                                  _("Downloading script..."));
                dogechat_printf_y (script_buffer,
                                  script_buffer_detail_script_last_line + 1,
                                  "%s----------------------------------------"
                                  "----------------------------------------",
                                  dogechat_color ("lightcyan"));
                filename = script_config_get_script_download_filename (ptr_script,
                                                                       ".repository");
                if (filename)
                {
                    options = dogechat_hashtable_new (32,
                                                     DOGECHAT_HASHTABLE_STRING,
                                                     DOGECHAT_HASHTABLE_STRING,
                                                     NULL,
                                                     NULL);
                    if (options)
                    {
                        url = script_build_download_url (ptr_script->url);
                        if (url)
                        {
                            dogechat_hashtable_set (options, "file_out", filename);
                            dogechat_hook_process_hashtable (
                                url,
                                options,
                                dogechat_config_integer (script_config_scripts_download_timeout) * 1000,
                                &script_action_show_source_process_cb,
                                NULL);
                            free (url);
                        }
                        dogechat_hashtable_free (options);
                    }
                    free (filename);
                }
            }
        }
        else
        {
            if (!quiet)
            {
                dogechat_printf (NULL,
                                _("%s: script \"%s\" not found"),
                                SCRIPT_PLUGIN_NAME, name);
            }
        }
    }
    else
        script_buffer_show_detail_script (NULL);
}
示例#4
0
int
exec_command_run (struct t_gui_buffer *buffer,
                  int argc, char **argv, char **argv_eol, int start_arg)
{
    char str_buffer[512];
    struct t_exec_cmd *new_exec_cmd;
    struct t_exec_cmd_options cmd_options;
    struct t_hashtable *process_options;
    struct t_infolist *ptr_infolist;
    struct t_gui_buffer *ptr_new_buffer;

    /* parse command options */
    cmd_options.command_index = -1;
    cmd_options.use_shell = 0;
    cmd_options.detached = 0;
    cmd_options.pipe_stdin = 0;
    cmd_options.timeout = 0;
    cmd_options.ptr_buffer_name = NULL;
    cmd_options.ptr_buffer = buffer;
    cmd_options.output_to_buffer = 0;
    cmd_options.new_buffer = 0;
    cmd_options.new_buffer_clear = 0;
    cmd_options.switch_to_buffer = 1;
    cmd_options.line_numbers = -1;
    cmd_options.flush = 1;
    cmd_options.color = EXEC_COLOR_AUTO;
    cmd_options.display_rc = 1;
    cmd_options.ptr_command_name = NULL;
    cmd_options.pipe_command = NULL;
    cmd_options.hsignal = NULL;

    /* parse default options */
    if (!exec_command_parse_options (&cmd_options,
                                     exec_config_cmd_num_options,
                                     exec_config_cmd_options,
                                     0, 0))
    {
        dogechat_printf (NULL,
                        _("%s%s: invalid options in option "
                          "exec.command.default_options"),
                        dogechat_prefix ("error"), EXEC_PLUGIN_NAME);
        return DOGECHAT_RC_ERROR;
    }
    if (!exec_command_parse_options (&cmd_options, argc, argv, start_arg, 1))
        return DOGECHAT_RC_ERROR;

    /* options "-bg" and "-o"/"-n" are incompatible */
    if (cmd_options.detached
        && (cmd_options.output_to_buffer || cmd_options.new_buffer))
        return DOGECHAT_RC_ERROR;

    /* options "-pipe" and "-bg"/"-o"/"-n" are incompatible */
    if (cmd_options.pipe_command
        && (cmd_options.detached || cmd_options.output_to_buffer
            || cmd_options.new_buffer))
        return DOGECHAT_RC_ERROR;

    /* command not found? */
    if (cmd_options.command_index < 0)
        return DOGECHAT_RC_ERROR;

    new_exec_cmd = exec_add ();
    if (!new_exec_cmd)
        return DOGECHAT_RC_ERROR;

    /* create hashtable for dogechat_hook_process_hashtable() */
    process_options = dogechat_hashtable_new (32,
                                             DOGECHAT_HASHTABLE_STRING,
                                             DOGECHAT_HASHTABLE_STRING,
                                             NULL,
                                             NULL);
    if (!process_options)
    {
        exec_free (new_exec_cmd);
        return DOGECHAT_RC_ERROR;
    }
    /* automatically disable shell if we are downloading an URL */
    if (strncmp (argv_eol[cmd_options.command_index], "url:", 4) == 0)
        cmd_options.use_shell = 0;
    if (cmd_options.use_shell)
    {
        /* command will be: sh -c "command arguments..." */
        dogechat_hashtable_set (process_options, "arg1", "-c");
        dogechat_hashtable_set (process_options, "arg2",
                               argv_eol[cmd_options.command_index]);
    }
    if (cmd_options.pipe_stdin)
        dogechat_hashtable_set (process_options, "stdin", "1");
    if (cmd_options.detached)
        dogechat_hashtable_set (process_options, "detached", "1");
    if (cmd_options.flush)
        dogechat_hashtable_set (process_options, "buffer_flush", "1");

    /* set variables in new command (before running the command) */
    new_exec_cmd->name = (cmd_options.ptr_command_name) ?
        strdup (cmd_options.ptr_command_name) : NULL;
    new_exec_cmd->command = strdup (argv_eol[cmd_options.command_index]);
    new_exec_cmd->detached = cmd_options.detached;

    if (!cmd_options.detached && !cmd_options.pipe_command
        && !cmd_options.hsignal)
    {
        if (cmd_options.ptr_buffer_name && !cmd_options.ptr_buffer)
        {
            /* output in a new buffer using given name */
            new_exec_cmd->output_to_buffer = 0;
            snprintf (str_buffer, sizeof (str_buffer),
                      "exec.%s", cmd_options.ptr_buffer_name);
            ptr_new_buffer = exec_buffer_new (str_buffer,
                                              (cmd_options.new_buffer == 2),
                                              cmd_options.new_buffer_clear,
                                              cmd_options.switch_to_buffer);
            if (ptr_new_buffer)
            {
                new_exec_cmd->buffer_full_name =
                    strdup (dogechat_buffer_get_string (ptr_new_buffer,
                                                       "full_name"));
            }
        }
        else if (cmd_options.new_buffer)
        {
            /* output in a new buffer using automatic name */
            if (new_exec_cmd->name)
            {
                snprintf (str_buffer, sizeof (str_buffer),
                          "exec.%s", new_exec_cmd->name);
            }
            else
            {
                snprintf (str_buffer, sizeof (str_buffer),
                          "exec.%d", new_exec_cmd->number);
            }
            ptr_new_buffer = exec_buffer_new (str_buffer,
                                              (cmd_options.new_buffer == 2),
                                              cmd_options.new_buffer_clear,
                                              cmd_options.switch_to_buffer);
            if (ptr_new_buffer)
            {
                new_exec_cmd->buffer_full_name =
                    strdup (dogechat_buffer_get_string (ptr_new_buffer,
                                                       "full_name"));
            }
        }
        else if (cmd_options.ptr_buffer)
        {
            new_exec_cmd->buffer_full_name =
                strdup (dogechat_buffer_get_string (cmd_options.ptr_buffer,
                                                   "full_name"));
            if (cmd_options.switch_to_buffer)
                dogechat_buffer_set (cmd_options.ptr_buffer, "display", "1");
        }
        if (cmd_options.ptr_buffer
            && (strcmp (dogechat_buffer_get_string (cmd_options.ptr_buffer, "plugin"),
                        EXEC_PLUGIN_NAME) == 0))
        {
            cmd_options.output_to_buffer = 0;
            cmd_options.new_buffer = 1;
        }
    }
    new_exec_cmd->output_to_buffer = cmd_options.output_to_buffer;
    new_exec_cmd->line_numbers = (cmd_options.line_numbers < 0) ?
        cmd_options.new_buffer : cmd_options.line_numbers;
    new_exec_cmd->color = cmd_options.color;
    new_exec_cmd->display_rc = cmd_options.display_rc;
    new_exec_cmd->pipe_command = cmd_options.pipe_command;
    new_exec_cmd->hsignal = cmd_options.hsignal;

    /* execute the command */
    if (dogechat_exec_plugin->debug >= 1)
    {
        dogechat_printf (NULL, "%s: executing command: \"%s%s%s\"",
                        EXEC_PLUGIN_NAME,
                        (cmd_options.use_shell) ? "" : "sh -c '",
                        argv_eol[cmd_options.command_index],
                        (cmd_options.use_shell) ? "" : "'");
    }
    new_exec_cmd->hook = dogechat_hook_process_hashtable (
        (cmd_options.use_shell) ? "sh" : argv_eol[cmd_options.command_index],
        process_options,
        cmd_options.timeout * 1000,
        &exec_process_cb,
        new_exec_cmd);

    if (new_exec_cmd->hook)
    {
        /* get PID of command */
        ptr_infolist = dogechat_infolist_get ("hook", new_exec_cmd->hook, NULL);
        if (ptr_infolist)
        {
            if (dogechat_infolist_next (ptr_infolist))
            {
                new_exec_cmd->pid = dogechat_infolist_integer (ptr_infolist,
                                                              "child_pid");
            }
            dogechat_infolist_free (ptr_infolist);
        }
    }
    else
    {
        exec_free (new_exec_cmd);
        dogechat_printf (NULL,
                        _("%s%s: failed to run command \"%s\""),
                        dogechat_prefix ("error"), EXEC_PLUGIN_NAME,
                        argv_eol[cmd_options.command_index]);
    }

    dogechat_hashtable_free (process_options);

    return DOGECHAT_RC_OK;
}
示例#5
0
void
exec_end_command (struct t_exec_cmd *exec_cmd, int return_code)
{
    struct t_gui_buffer *ptr_buffer;
    struct t_hashtable *hashtable;
    char str_number[32], *output;
    int i, buffer_type;

    if (exec_cmd->hsignal)
    {
        hashtable = dogechat_hashtable_new (32,
                                           DOGECHAT_HASHTABLE_STRING,
                                           DOGECHAT_HASHTABLE_STRING,
                                           NULL,
                                           NULL);
        if (hashtable)
        {
            dogechat_hashtable_set (hashtable, "command", exec_cmd->command);
            snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number);
            dogechat_hashtable_set (hashtable, "number", str_number);
            dogechat_hashtable_set (hashtable, "name", exec_cmd->name);
            output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDOUT]);
            dogechat_hashtable_set (hashtable, "out", output);
            if (output)
                free (output);
            output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDERR]);
            dogechat_hashtable_set (hashtable, "err", output);
            if (output)
                free (output);
            snprintf (str_number, sizeof (str_number), "%d", return_code);
            dogechat_hashtable_set (hashtable, "rc", str_number);
            dogechat_hook_hsignal_send (exec_cmd->hsignal, hashtable);
            dogechat_hashtable_free (hashtable);
        }
    }
    else
    {
        ptr_buffer = dogechat_buffer_search ("==", exec_cmd->buffer_full_name);

        /* display the last line of output (if not ending with '\n') */
        exec_display_line (exec_cmd, ptr_buffer, EXEC_STDOUT,
                           exec_cmd->output[EXEC_STDOUT]);
        exec_display_line (exec_cmd, ptr_buffer, EXEC_STDERR,
                           exec_cmd->output[EXEC_STDERR]);

        /*
         * display return code (only if command is not detached, if output is
         * NOT sent to buffer, and if command is not piped)
         */
        if (exec_cmd->display_rc
            && !exec_cmd->detached && !exec_cmd->output_to_buffer
            && !exec_cmd->pipe_command)
        {
            buffer_type = dogechat_buffer_get_integer (ptr_buffer, "type");
            if (return_code >= 0)
            {
                if (buffer_type == 1)
                {
                    dogechat_printf_y (ptr_buffer, -1,
                                      ("%s: end of command %d (\"%s\"), "
                                       "return code: %d"),
                                      EXEC_PLUGIN_NAME, exec_cmd->number,
                                      exec_cmd->command, return_code);
                }
                else
                {
                    dogechat_printf_tags (ptr_buffer, "exec_rc",
                                         _("%s: end of command %d (\"%s\"), "
                                           "return code: %d"),
                                         EXEC_PLUGIN_NAME, exec_cmd->number,
                                         exec_cmd->command, return_code);
                }
            }
            else
            {
                if (buffer_type == 1)
                {
                    dogechat_printf_y (ptr_buffer, -1,
                                      _("%s: unexpected end of command %d "
                                        "(\"%s\")"),
                                      EXEC_PLUGIN_NAME, exec_cmd->number,
                                      exec_cmd->command);
                }
                else
                {
                    dogechat_printf_tags (ptr_buffer, "exec_rc",
                                         _("%s: unexpected end of command %d "
                                           "(\"%s\")"),
                                         EXEC_PLUGIN_NAME, exec_cmd->number,
                                         exec_cmd->command);
                }
            }
        }
    }

    /* (re)set some variables after the end of command */
    exec_cmd->hook = NULL;
    exec_cmd->pid = 0;
    exec_cmd->end_time = time (NULL);
    exec_cmd->return_code = return_code;
    for (i = 0; i < 2; i++)
    {
        if (exec_cmd->output[i])
        {
            free (exec_cmd->output[i]);
            exec_cmd->output[i] = NULL;
        }
        exec_cmd->output_size[i] = 0;
    }

    /* schedule a timer to remove the executed command */
    if (dogechat_config_integer (exec_config_command_purge_delay) >= 0)
    {
        dogechat_hook_timer (1 + (1000 * dogechat_config_integer (exec_config_command_purge_delay)),
                            0, 1,
                            &exec_timer_delete_cb, exec_cmd);
    }
}