void relay_weechat_alloc (struct t_relay_client *client) { struct t_relay_weechat_data *weechat_data; const char *password; password = weechat_config_string (relay_config_network_password); client->protocol_data = malloc (sizeof (*weechat_data)); if (client->protocol_data) { RELAY_WEECHAT_DATA(client, password_ok) = (password && password[0]) ? 0 : 1; #ifdef HAVE_ZLIB RELAY_WEECHAT_DATA(client, compression) = 1; #else RELAY_WEECHAT_DATA(client, compression) = 0; #endif RELAY_WEECHAT_DATA(client, buffers_sync) = weechat_hashtable_new (16, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_INTEGER, NULL, NULL); RELAY_WEECHAT_DATA(client, hook_signal_buffer) = NULL; RELAY_WEECHAT_DATA(client, hook_signal_nicklist) = NULL; RELAY_WEECHAT_DATA(client, buffers_nicklist) = weechat_hashtable_new (16, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); RELAY_WEECHAT_DATA(client, hook_timer_nicklist) = NULL; } }
int weechat_aspell_speller_init () { weechat_aspell_spellers = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!weechat_aspell_spellers) return 0; weechat_hashtable_set_pointer (weechat_aspell_spellers, "callback_free_value", &weechat_aspell_speller_free_value_cb); weechat_aspell_speller_buffer = weechat_hashtable_new (32, WEECHAT_HASHTABLE_POINTER, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!weechat_aspell_speller_buffer) { weechat_hashtable_free (weechat_aspell_spellers); return 0; } weechat_hashtable_set_pointer (weechat_aspell_speller_buffer, "callback_free_value", &weechat_aspell_speller_buffer_free_value_cb); return 1; }
void relay_weechat_alloc_with_infolist (struct t_relay_client *client, struct t_infolist *infolist) { struct t_relay_weechat_data *weechat_data; int index, value; char name[64]; const char *key; client->protocol_data = malloc (sizeof (*weechat_data)); if (client->protocol_data) { /* general stuff */ RELAY_WEECHAT_DATA(client, password_ok) = weechat_infolist_integer (infolist, "password_ok"); RELAY_WEECHAT_DATA(client, compression) = weechat_infolist_integer (infolist, "compression"); /* sync of buffers */ RELAY_WEECHAT_DATA(client, buffers_sync) = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_INTEGER, NULL, NULL); index = 0; while (1) { snprintf (name, sizeof (name), "buffers_sync_name_%05d", index); key = weechat_infolist_string (infolist, name); if (!key) break; snprintf (name, sizeof (name), "buffers_sync_value_%05d", index); value = weechat_infolist_integer (infolist, name); weechat_hashtable_set (RELAY_WEECHAT_DATA(client, buffers_sync), key, &value); index++; } RELAY_WEECHAT_DATA(client, hook_signal_buffer) = NULL; RELAY_WEECHAT_DATA(client, hook_hsignal_nicklist) = NULL; RELAY_WEECHAT_DATA(client, hook_signal_upgrade) = NULL; RELAY_WEECHAT_DATA(client, buffers_nicklist) = weechat_hashtable_new (32, WEECHAT_HASHTABLE_POINTER, WEECHAT_HASHTABLE_POINTER, NULL, NULL); weechat_hashtable_set_pointer (RELAY_WEECHAT_DATA(client, buffers_nicklist), "callback_free_value", &relay_weechat_free_buffers_nicklist); RELAY_WEECHAT_DATA(client, hook_timer_nicklist) = NULL; relay_weechat_hook_signals (client); } }
struct t_hashtable * weechat_lua_tohashtable (lua_State *interpreter, int index, int hashtable_size) { struct t_hashtable *hashtable; hashtable = weechat_hashtable_new (hashtable_size, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; lua_pushnil (interpreter); while (lua_next (interpreter, index - 1) != 0) { weechat_hashtable_set (hashtable, lua_tostring (interpreter, -2), lua_tostring (interpreter, -1)); /* remove value from stack (keep key for next iteration) */ lua_pop (interpreter, 1); } return hashtable; }
void relay_config_change_irc_backlog_tags (void *data, struct t_config_option *option) { char **items; int num_items, i; /* make C compiler happy */ (void) data; (void) option; if (!relay_config_hashtable_irc_backlog_tags) { relay_config_hashtable_irc_backlog_tags = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); } else weechat_hashtable_remove_all (relay_config_hashtable_irc_backlog_tags); items = weechat_string_split (weechat_config_string (relay_config_irc_backlog_tags), ";", 0, 0, &num_items); if (items) { for (i = 0; i < num_items; i++) { weechat_hashtable_set (relay_config_hashtable_irc_backlog_tags, items[i], NULL); } weechat_string_free_split (items); } }
struct t_hashtable * irc_message_parse_to_hashtable (struct t_irc_server *server, const char *message) { char *tags,*message_without_tags, *nick, *host, *command, *channel; char *arguments, *text, str_pos_text[32]; char empty_str[1] = { '\0' }; int pos_text; struct t_hashtable *hashtable; irc_message_parse (server, message, &tags, &message_without_tags, &nick, &host, &command, &channel, &arguments, &text, &pos_text); hashtable = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; weechat_hashtable_set (hashtable, "tags", (tags) ? tags : empty_str); weechat_hashtable_set (hashtable, "message_without_tags", (message_without_tags) ? message_without_tags : empty_str); weechat_hashtable_set (hashtable, "nick", (nick) ? nick : empty_str); weechat_hashtable_set (hashtable, "host", (host) ? host : empty_str); weechat_hashtable_set (hashtable, "command", (command) ? command : empty_str); weechat_hashtable_set (hashtable, "channel", (channel) ? channel : empty_str); weechat_hashtable_set (hashtable, "arguments", (arguments) ? arguments : empty_str); weechat_hashtable_set (hashtable, "text", (text) ? text : empty_str); snprintf (str_pos_text, sizeof (str_pos_text), "%d", pos_text); weechat_hashtable_set (hashtable, "pos_text", str_pos_text); if (tags) free (tags); if (message_without_tags) free (message_without_tags); if (nick) free (nick); if (host) free (host); if (command) free (command); if (channel) free (channel); if (arguments) free (arguments); if (text) free (text); return hashtable; }
struct t_hashtable * weechat_tcl_dict_to_hashtable (Tcl_Interp *interp, Tcl_Obj *dict, int hashtable_size) { struct t_hashtable *hashtable; Tcl_DictSearch search; Tcl_Obj *key, *value; int done; hashtable = weechat_hashtable_new (hashtable_size, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; if (Tcl_DictObjFirst (interp, dict, &search, &key, &value, &done) == TCL_OK) { for (; !done ; Tcl_DictObjNext(&search, &key, &value, &done)) { weechat_hashtable_set (hashtable, Tcl_GetString (key), Tcl_GetString (value)); } } Tcl_DictObjDone(&search); return hashtable; }
struct t_hashtable * weechat_guile_alist_to_hashtable (SCM alist, int hashtable_size) { struct t_hashtable *hashtable; int length, i; SCM pair; hashtable = weechat_hashtable_new (hashtable_size, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; length = scm_to_int (scm_length (alist)); for (i = 0; i < length; i++) { pair = scm_list_ref (alist, scm_from_int (i)); weechat_hashtable_set (hashtable, scm_i_string_chars (scm_list_ref (pair, scm_from_int (0))), scm_i_string_chars (scm_list_ref (pair, scm_from_int (1)))); } return hashtable; }
struct t_hashtable * weechat_lua_tohashtable (lua_State *interpreter, int index, int size, const char *type_keys, const char *type_values) { struct t_hashtable *hashtable; hashtable = weechat_hashtable_new (size, type_keys, type_values, NULL, NULL); if (!hashtable) return NULL; lua_pushnil (interpreter); while (lua_next (interpreter, index - 1) != 0) { if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0) { weechat_hashtable_set (hashtable, lua_tostring (interpreter, -2), lua_tostring (interpreter, -1)); } else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0) { weechat_hashtable_set (hashtable, lua_tostring (interpreter, -2), plugin_script_str2ptr ( weechat_lua_plugin, NULL, NULL, lua_tostring (interpreter, -1))); } /* remove value from stack (keep key for next iteration) */ lua_pop (interpreter, 1); } return hashtable; }
struct t_hashtable * weechat_python_dict_to_hashtable (PyObject *dict, int size, const char *type_keys, const char *type_values) { struct t_hashtable *hashtable; PyObject *key, *value; Py_ssize_t pos; char *str_key, *str_value; hashtable = weechat_hashtable_new (size, type_keys, type_values, NULL, NULL); if (!hashtable) return NULL; pos = 0; while (PyDict_Next (dict, &pos, &key, &value)) { str_key = NULL; str_value = NULL; if (PyBytes_Check (key)) { if (PyBytes_AsString (key)) str_key = strdup (PyBytes_AsString (key)); } else str_key = weechat_python_unicode_to_string (key); if (PyBytes_Check (value)) { if (PyBytes_AsString (value)) str_value = strdup (PyBytes_AsString (value)); } else str_value = weechat_python_unicode_to_string (value); if (str_key) { if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0) weechat_hashtable_set (hashtable, str_key, str_value); else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0) { weechat_hashtable_set (hashtable, str_key, plugin_script_str2ptr (weechat_python_plugin, NULL, NULL, str_value)); } } if (str_key) free (str_key); if (str_value) free (str_value); } return hashtable; }
void irc_redirect_stop (struct t_irc_redirect *redirect, const char *error) { struct t_hashtable *hashtable; char signal_name[1024], str_int[64]; redirect->current_count++; if (error || (redirect->current_count > redirect->count)) { /* * error or max count reached, then we run callback and remove * redirect */ hashtable = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (hashtable) { /* set error and output (main fields) */ weechat_hashtable_set (hashtable, "error", (error) ? (char *)error : ""); weechat_hashtable_set (hashtable, "output", (redirect->output) ? redirect->output : ""); snprintf (str_int, sizeof (str_int), "%d", redirect->output_size); weechat_hashtable_set (hashtable, "output_size", str_int); /* set some other fields with values from redirect */ weechat_hashtable_set (hashtable, "server", redirect->server->name); weechat_hashtable_set (hashtable, "pattern", redirect->pattern); weechat_hashtable_set (hashtable, "signal", redirect->signal); weechat_hashtable_set (hashtable, "command", redirect->command); } snprintf (signal_name, sizeof (signal_name), "irc_redirection_%s_%s", redirect->signal, redirect->pattern); (void) weechat_hook_hsignal_send (signal_name, hashtable); if (hashtable) weechat_hashtable_free (hashtable); irc_redirect_free (redirect); } else { /* * max count not yet reached, then we prepare redirect to continue * redirection */ redirect->cmd_start_received = 0; redirect->cmd_stop_received = 0; } }
void relay_weechat_alloc (struct t_relay_client *client) { struct t_relay_weechat_data *weechat_data; char *password; password = weechat_string_eval_expression (weechat_config_string (relay_config_network_password), NULL, NULL, NULL); client->protocol_data = malloc (sizeof (*weechat_data)); if (client->protocol_data) { RELAY_WEECHAT_DATA(client, password_ok) = (password && password[0]) ? 0 : 1; RELAY_WEECHAT_DATA(client, compression) = RELAY_WEECHAT_COMPRESSION_ZLIB; RELAY_WEECHAT_DATA(client, buffers_sync) = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_INTEGER, NULL, NULL); RELAY_WEECHAT_DATA(client, hook_signal_buffer) = NULL; RELAY_WEECHAT_DATA(client, hook_hsignal_nicklist) = NULL; RELAY_WEECHAT_DATA(client, hook_signal_upgrade) = NULL; RELAY_WEECHAT_DATA(client, buffers_nicklist) = weechat_hashtable_new (32, WEECHAT_HASHTABLE_POINTER, WEECHAT_HASHTABLE_POINTER, NULL, NULL); weechat_hashtable_set_pointer (RELAY_WEECHAT_DATA(client, buffers_nicklist), "callback_free_value", &relay_weechat_free_buffers_nicklist); RELAY_WEECHAT_DATA(client, hook_timer_nicklist) = NULL; relay_weechat_hook_signals (client); } if (password) free (password); }
void trigger_callback_init () { trigger_callback_hashtable_options_conditions = weechat_hashtable_new ( 32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (trigger_callback_hashtable_options_conditions) { weechat_hashtable_set (trigger_callback_hashtable_options_conditions, "type", "condition"); } trigger_callback_hashtable_options_regex = weechat_hashtable_new ( 32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); }
struct t_hashtable * weechat_ruby_hash_to_hashtable (VALUE hash, int size, const char *type_keys, const char *type_values) { struct t_hashtable *hashtable; hashtable = weechat_hashtable_new (size, type_keys, type_values, NULL, NULL); if (!hashtable) return NULL; rb_hash_foreach (hash, &weechat_ruby_hash_foreach_cb, (unsigned long)hashtable); return hashtable; }
struct t_hashtable * weechat_guile_alist_to_hashtable (SCM alist, int size, const char *type_keys, const char *type_values) { struct t_hashtable *hashtable; int length, i; SCM pair; char *str, *str2; hashtable = weechat_hashtable_new (size, type_keys, type_values, NULL, NULL); if (!hashtable) return NULL; length = scm_to_int (scm_length (alist)); for (i = 0; i < length; i++) { pair = scm_list_ref (alist, scm_from_int (i)); if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0) { str = scm_to_locale_string (scm_list_ref (pair, scm_from_int (0))); str2 = scm_to_locale_string (scm_list_ref (pair, scm_from_int (1))); weechat_hashtable_set (hashtable, str, str2); if (str) free (str); if (str2) free (str2); } else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0) { str = scm_to_locale_string (scm_list_ref (pair, scm_from_int (0))); str2 = scm_to_locale_string (scm_list_ref (pair, scm_from_int (1))); weechat_hashtable_set (hashtable, str, plugin_script_str2ptr (weechat_guile_plugin, NULL, NULL, str2)); if (str) free (str); if (str2) free (str2); } } return hashtable; }
void weechat_aspell_speller_remove_unused () { struct t_hashtable *used_spellers; struct t_infolist *infolist; if (weechat_aspell_plugin->debug) { weechat_printf (NULL, "%s: removing unused spellers", ASPELL_PLUGIN_NAME); } /* create a hashtable that will contain all used spellers */ used_spellers = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!used_spellers) return; /* collect used spellers and store them in hashtable "used_spellers" */ weechat_aspell_speller_add_dicts_to_hash (used_spellers, weechat_config_string (weechat_aspell_config_check_default_dict)); infolist = weechat_infolist_get ("option", NULL, "aspell.dict.*"); if (infolist) { while (weechat_infolist_next (infolist)) { weechat_aspell_speller_add_dicts_to_hash (used_spellers, weechat_infolist_string (infolist, "value")); } weechat_infolist_free (infolist); } /* * look at current spellers, and remove spellers that are not in hashtable * "used_spellers" */ weechat_hashtable_map (weechat_aspell_spellers, &weechat_aspell_speller_remove_unused_cb, used_spellers); weechat_hashtable_free (used_spellers); }
struct t_hashtable * weechat_ruby_hash_to_hashtable (VALUE hash, int hashtable_size) { struct t_hashtable *hashtable; hashtable = weechat_hashtable_new (hashtable_size, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; rb_hash_foreach (hash, &weechat_ruby_hash_foreach_cb, (unsigned long)hashtable); return hashtable; }
struct t_hashtable * weechat_tcl_dict_to_hashtable (Tcl_Interp *interp, Tcl_Obj *dict, int size, const char *type_keys, const char *type_values) { struct t_hashtable *hashtable; Tcl_DictSearch search; Tcl_Obj *key, *value; int done; hashtable = weechat_hashtable_new (size, type_keys, type_values, NULL, NULL); if (!hashtable) return NULL; if (Tcl_DictObjFirst (interp, dict, &search, &key, &value, &done) == TCL_OK) { for (; !done ; Tcl_DictObjNext(&search, &key, &value, &done)) { if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0) { weechat_hashtable_set (hashtable, Tcl_GetString (key), Tcl_GetString (value)); } else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0) { weechat_hashtable_set (hashtable, Tcl_GetString (key), plugin_script_str2ptr (weechat_tcl_plugin, NULL, NULL, Tcl_GetString (value))); } } } Tcl_DictObjDone(&search); return hashtable; }
struct t_hashtable * weechat_perl_hash_to_hashtable (SV *hash, int size, const char *type_keys, const char *type_values) { struct t_hashtable *hashtable; HV *hash2; SV *value; char *str_key; I32 retlen; hashtable = weechat_hashtable_new (size, type_keys, type_values, NULL, NULL); if (!hashtable) return NULL; if ((hash) && SvROK(hash) && SvRV(hash) && (SvTYPE(SvRV(hash)) == SVt_PVHV)) { hash2 = (HV *)SvRV(hash); hv_iterinit (hash2); while ((value = hv_iternextsv (hash2, &str_key, &retlen))) { if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0) { weechat_hashtable_set (hashtable, str_key, SvPV (value, PL_na)); } else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0) { weechat_hashtable_set (hashtable, str_key, plugin_script_str2ptr ( weechat_perl_plugin, NULL, NULL, SvPV (value, PL_na))); } } } return hashtable; }
struct t_hashtable * weechat_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 = weechat_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, WEECHAT_HASHTABLE_STRING) == 0) { weechat_hashtable_set (hashtable, *str_key, *str_value); } else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0) { weechat_hashtable_set (hashtable, *str_key, plugin_script_str2ptr (weechat_js_plugin, NULL, NULL, *str_value)); } } return hashtable; }
struct t_hashtable * trigger_callback_irc_message_parse (const char *irc_message, const char *irc_server) { struct t_hashtable *hashtable_in, *hashtable_out; hashtable_out = NULL; hashtable_in = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (hashtable_in) { weechat_hashtable_set (hashtable_in, "message", irc_message); weechat_hashtable_set (hashtable_in, "server", irc_server); hashtable_out = weechat_info_get_hashtable ("irc_message_parse", hashtable_in); weechat_hashtable_free (hashtable_in); } return hashtable_out; }
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)) { weechat_printf (NULL, _("%s%s: invalid options in option " "exec.command.default_options"), weechat_prefix ("error"), EXEC_PLUGIN_NAME); return WEECHAT_RC_ERROR; } if (!exec_command_parse_options (&cmd_options, argc, argv, start_arg, 1)) return WEECHAT_RC_ERROR; /* options "-bg" and "-o"/"-n" are incompatible */ if (cmd_options.detached && (cmd_options.output_to_buffer || cmd_options.new_buffer)) return WEECHAT_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 WEECHAT_RC_ERROR; /* command not found? */ if (cmd_options.command_index < 0) return WEECHAT_RC_ERROR; new_exec_cmd = exec_add (); if (!new_exec_cmd) return WEECHAT_RC_ERROR; /* create hashtable for weechat_hook_process_hashtable() */ process_options = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!process_options) { exec_free (new_exec_cmd); return WEECHAT_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..." */ weechat_hashtable_set (process_options, "arg1", "-c"); weechat_hashtable_set (process_options, "arg2", argv_eol[cmd_options.command_index]); } if (cmd_options.pipe_stdin) weechat_hashtable_set (process_options, "stdin", "1"); if (cmd_options.detached) weechat_hashtable_set (process_options, "detached", "1"); if (cmd_options.flush) weechat_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 (weechat_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 (weechat_buffer_get_string (ptr_new_buffer, "full_name")); } } else if (cmd_options.ptr_buffer) { new_exec_cmd->buffer_full_name = strdup (weechat_buffer_get_string (cmd_options.ptr_buffer, "full_name")); if (cmd_options.switch_to_buffer) weechat_buffer_set (cmd_options.ptr_buffer, "display", "1"); } if (cmd_options.ptr_buffer && (strcmp (weechat_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 (weechat_exec_plugin->debug >= 1) { weechat_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 = weechat_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 = weechat_infolist_get ("hook", new_exec_cmd->hook, NULL); if (ptr_infolist) { if (weechat_infolist_next (ptr_infolist)) { new_exec_cmd->pid = weechat_infolist_integer (ptr_infolist, "child_pid"); } weechat_infolist_free (ptr_infolist); } } else { exec_free (new_exec_cmd); weechat_printf (NULL, _("%s%s: failed to run command \"%s\""), weechat_prefix ("error"), EXEC_PLUGIN_NAME, argv_eol[cmd_options.command_index]); } weechat_hashtable_free (process_options); return WEECHAT_RC_OK; }
void relay_weechat_alloc_with_infolist (struct t_relay_client *client, struct t_infolist *infolist) { struct t_relay_weechat_data *weechat_data; int index, rc, value_int; char name[64]; const char *key, *value; client->protocol_data = malloc (sizeof (*weechat_data)); if (client->protocol_data) { /* general stuff */ RELAY_WEECHAT_DATA(client, password_ok) = weechat_infolist_integer (infolist, "password_ok"); RELAY_WEECHAT_DATA(client, compression) = weechat_infolist_integer (infolist, "compression"); /* sync of buffers */ RELAY_WEECHAT_DATA(client, buffers_sync) = weechat_hashtable_new (16, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); index = 0; while (1) { snprintf (name, sizeof (name), "buffers_sync_name_%05d", index); key = weechat_infolist_string (infolist, name); if (!key) break; snprintf (name, sizeof (name), "buffers_sync_value_%05d", index); value = weechat_infolist_string (infolist, name); rc = sscanf (value, "%d", &value_int); if ((rc == EOF) || (rc == 0)) value_int = 0; weechat_hashtable_set (RELAY_WEECHAT_DATA(client, buffers_sync), key, &value_int); index++; } RELAY_WEECHAT_DATA(client, hook_signal_buffer) = NULL; RELAY_WEECHAT_DATA(client, hook_signal_nicklist) = NULL; RELAY_WEECHAT_DATA(client, buffers_nicklist) = weechat_hashtable_new (16, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); index = 0; while (1) { snprintf (name, sizeof (name), "buffers_nicklist_name_%05d", index); key = weechat_infolist_string (infolist, name); if (!key) break; snprintf (name, sizeof (name), "buffers_nicklist_value_%05d", index); value = weechat_infolist_string (infolist, name); weechat_hashtable_set (RELAY_WEECHAT_DATA(client, buffers_sync), key, value); index++; } RELAY_WEECHAT_DATA(client, hook_timer_nicklist) = NULL; if (weechat_hashtable_get_integer (RELAY_WEECHAT_DATA(client, buffers_sync), "items_count") > 0) relay_weechat_hook_signals (client); if (weechat_hashtable_get_integer (RELAY_WEECHAT_DATA(client, buffers_sync), "items_count") > 0) relay_weechat_hook_timer_nicklist (client); } }
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 = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (hashtable) { weechat_hashtable_set (hashtable, "command", exec_cmd->command); snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number); weechat_hashtable_set (hashtable, "number", str_number); weechat_hashtable_set (hashtable, "name", exec_cmd->name); output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDOUT]); weechat_hashtable_set (hashtable, "out", output); if (output) free (output); output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDERR]); weechat_hashtable_set (hashtable, "err", output); if (output) free (output); snprintf (str_number, sizeof (str_number), "%d", return_code); weechat_hashtable_set (hashtable, "rc", str_number); weechat_hook_hsignal_send (exec_cmd->hsignal, hashtable); weechat_hashtable_free (hashtable); } } else { ptr_buffer = weechat_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 = weechat_buffer_get_integer (ptr_buffer, "type"); if (return_code >= 0) { if (buffer_type == 1) { weechat_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 { weechat_printf_date_tags ( ptr_buffer, 0, "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) { weechat_printf_y (ptr_buffer, -1, _("%s: unexpected end of command %d " "(\"%s\")"), EXEC_PLUGIN_NAME, exec_cmd->number, exec_cmd->command); } else { weechat_printf_date_tags ( ptr_buffer, 0, "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 (weechat_config_integer (exec_config_command_purge_delay) >= 0) { weechat_hook_timer (1 + (1000 * weechat_config_integer (exec_config_command_purge_delay)), 0, 1, &exec_timer_delete_cb, exec_cmd, NULL); } }
void trigger_callback_replace_regex (struct t_trigger *trigger, struct t_hashtable *pointers, struct t_hashtable *extra_vars, int display_monitor) { char *value; const char *ptr_key, *ptr_value; int i, pointers_allocated; pointers_allocated = 0; if (trigger->regex_count == 0) return; if (!pointers) { pointers = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!pointers) return; pointers_allocated = 1; } for (i = 0; i < trigger->regex_count; i++) { /* if regex is not set (invalid), skip it */ if (!trigger->regex[i].regex) continue; ptr_key = (trigger->regex[i].variable) ? trigger->regex[i].variable : trigger_hook_regex_default_var[weechat_config_integer (trigger->options[TRIGGER_OPTION_HOOK])]; if (!ptr_key || !ptr_key[0]) { if (trigger_buffer && display_monitor) { weechat_printf_tags (trigger_buffer, "no_trigger", "\t regex %d: %s", i + 1, _("no variable")); } continue; } ptr_value = weechat_hashtable_get (extra_vars, ptr_key); if (!ptr_value) { if (trigger_buffer && display_monitor) { weechat_printf_tags (trigger_buffer, "no_trigger", "\t regex %d (%s): %s", i + 1, ptr_key, _("empty variable")); } continue; } weechat_hashtable_set (pointers, "regex", trigger->regex[i].regex); weechat_hashtable_set (trigger_callback_hashtable_options_regex, "regex_replace", trigger->regex[i].replace_escaped); value = weechat_string_eval_expression ( ptr_value, pointers, extra_vars, trigger_callback_hashtable_options_regex); if (value) { /* display debug info on trigger buffer */ if (trigger_buffer && display_monitor) { weechat_printf_tags (trigger_buffer, "no_trigger", "\t regex %d %s(%s%s%s)%s: " "%s\"%s%s%s\"", i + 1, weechat_color ("chat_delimiters"), weechat_color ("reset"), ptr_key, weechat_color ("chat_delimiters"), weechat_color ("reset"), weechat_color ("chat_delimiters"), weechat_color ("reset"), value, weechat_color ("chat_delimiters")); } weechat_hashtable_set (extra_vars, ptr_key, value); free (value); } } if (pointers_allocated) weechat_hashtable_free (pointers); else weechat_hashtable_remove (pointers, "regex"); }
struct t_irc_redirect * irc_redirect_new_with_commands (struct t_irc_server *server, const char *pattern, const char *signal, int count, const char *string, int timeout, const char *cmd_start, const char *cmd_stop, const char *cmd_extra, const char *cmd_filter) { struct t_irc_redirect *new_redirect; char **items[4], *pos, *error; int i, j, num_items[4]; long value; struct t_hashtable *hash_cmd[4]; new_redirect = malloc (sizeof (*new_redirect)); if (!new_redirect) return NULL; /* create hashtables with commands */ for (i = 0; i < 4; i++) { hash_cmd[i] = NULL; items[i] = NULL; } if (cmd_start) items[0] = weechat_string_split (cmd_start, ",", 0, 0, &num_items[0]); if (cmd_stop) items[1] = weechat_string_split (cmd_stop, ",", 0, 0, &num_items[1]); if (cmd_extra) items[2] = weechat_string_split (cmd_extra, ",", 0, 0, &num_items[2]); if (cmd_filter) items[3] = weechat_string_split (cmd_filter, ",", 0, 0, &num_items[3]); for (i = 0; i < 4; i++) { if (items[i]) { hash_cmd[i] = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_INTEGER, NULL, NULL); for (j = 0; j < num_items[i]; j++) { if (i < 3) { value = -1; pos = strchr (items[i][j], ':'); if (pos) { pos[0] = '\0'; value = strtol (pos + 1, &error, 10); if (!error || error[0]) value = -1; } weechat_string_toupper (items[i][j]); weechat_hashtable_set (hash_cmd[i], items[i][j], &value); } else { weechat_hashtable_set (hash_cmd[i], items[i][j], NULL); } } weechat_string_free_split (items[i]); } } /* initialize new redirect */ new_redirect->server = server; new_redirect->pattern = strdup (pattern); new_redirect->signal = strdup (signal); new_redirect->count = (count >= 1) ? count : 1; new_redirect->current_count = 1; new_redirect->string = (string) ? strdup (string) : NULL; new_redirect->timeout = timeout; new_redirect->command = NULL; new_redirect->assigned_to_command = 0; new_redirect->start_time = 0; new_redirect->cmd_start = hash_cmd[0]; new_redirect->cmd_stop = hash_cmd[1]; new_redirect->cmd_extra = hash_cmd[2]; new_redirect->cmd_start_received = 0; new_redirect->cmd_stop_received = 0; new_redirect->cmd_filter = hash_cmd[3]; new_redirect->output = NULL; new_redirect->output_size = 0; /* add redirect to end of list */ new_redirect->prev_redirect = server->last_redirect; if (server->redirects) (server->last_redirect)->next_redirect = new_redirect; else server->redirects = new_redirect; server->last_redirect = new_redirect; new_redirect->next_redirect = NULL; return new_redirect; }
struct t_hashtable * irc_message_split (struct t_irc_server *server, const char *message) { struct t_hashtable *hashtable; char **argv, **argv_eol, *tags, *host, *command, *arguments, target[512]; char *pos; int split_ok, argc, index_args, max_length_nick, max_length_host; split_ok = 0; tags = NULL; host = NULL; command = NULL; arguments = NULL; index_args = 0; argv = NULL; argv_eol = NULL; /* debug message */ if (weechat_irc_plugin->debug >= 2) weechat_printf (NULL, "irc_message_split: message='%s'", message); hashtable = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; if (!message || !message[0]) goto end; if (message[0] == '@') { pos = strchr (message, ' '); if (pos) { tags = weechat_strndup (message, pos - message + 1); message = pos + 1; } } argv = weechat_string_split (message, " ", 0, 0, &argc); argv_eol = weechat_string_split (message, " ", 2, 0, NULL); if (argc < 2) goto end; if (argv[0][0] == ':') { if (argc < 3) goto end; host = argv[0]; command = argv[1]; arguments = argv_eol[2]; index_args = 2; } else { command = argv[0]; arguments = argv_eol[1]; index_args = 1; } max_length_nick = (server && (server->nick_max_length > 0)) ? server->nick_max_length : 16; max_length_host = 1 + /* ":" */ max_length_nick + /* nick */ 1 + /* "!" */ 63 + /* host */ 1; /* " " */ if ((weechat_strcasecmp (command, "ison") == 0) || (weechat_strcasecmp (command, "wallops") == 0)) { split_ok = irc_message_split_string (hashtable, tags, host, command, NULL, ":", (argv_eol[index_args][0] == ':') ? argv_eol[index_args] + 1 : argv_eol[index_args], NULL, ' ', max_length_host); } else if (weechat_strcasecmp (command, "join") == 0) { /* split join (if it's more than 510 bytes) */ if (strlen (message) > 510) { split_ok = irc_message_split_join (hashtable, tags, host, arguments); } } else if ((weechat_strcasecmp (command, "privmsg") == 0) || (weechat_strcasecmp (command, "notice") == 0)) { /* split privmsg/notice */ if (index_args + 1 <= argc - 1) { split_ok = irc_message_split_privmsg_notice (hashtable, tags, host, command, argv[index_args], (argv_eol[index_args + 1][0] == ':') ? argv_eol[index_args + 1] + 1 : argv_eol[index_args + 1], max_length_host); } } else if (weechat_strcasecmp (command, "005") == 0) { /* split 005 (isupport) */ if (index_args + 1 <= argc - 1) { split_ok = irc_message_split_005 (hashtable, tags, host, command, argv[index_args], (argv_eol[index_args + 1][0] == ':') ? argv_eol[index_args + 1] + 1 : argv_eol[index_args + 1]); } } else if (weechat_strcasecmp (command, "353") == 0) { /* * split 353 (list of users on channel): * :server 353 mynick = #channel :mynick nick1 @nick2 +nick3 */ if (index_args + 2 <= argc - 1) { if (irc_channel_is_channel (server, argv[index_args + 1])) { snprintf (target, sizeof (target), "%s %s", argv[index_args], argv[index_args + 1]); split_ok = irc_message_split_string (hashtable, tags, host, command, target, ":", (argv_eol[index_args + 2][0] == ':') ? argv_eol[index_args + 2] + 1 : argv_eol[index_args + 2], NULL, ' ', -1); } else { if (index_args + 3 <= argc - 1) { snprintf (target, sizeof (target), "%s %s %s", argv[index_args], argv[index_args + 1], argv[index_args + 2]); split_ok = irc_message_split_string (hashtable, tags, host, command, target, ":", (argv_eol[index_args + 3][0] == ':') ? argv_eol[index_args + 3] + 1 : argv_eol[index_args + 3], NULL, ' ', -1); } } } } end: if (!split_ok || (weechat_hashtable_get_integer (hashtable, "items_count") == 0)) { irc_message_split_add (hashtable, 1, tags, message, arguments); } if (tags) free (tags); if (argv) weechat_string_free_split (argv); if (argv_eol) weechat_string_free_split (argv_eol); return hashtable; }
struct t_hashtable * irc_message_split (struct t_irc_server *server, const char *message) { struct t_hashtable *hashtable; char **argv, **argv_eol, *tags, *host, *command, *arguments, target[512]; char *pos, monitor_action[3]; int split_ok, argc, index_args, max_length_nick, max_length_host; split_ok = 0; tags = NULL; host = NULL; command = NULL; arguments = NULL; index_args = 0; argv = NULL; argv_eol = NULL; /* debug message */ if (weechat_irc_plugin->debug >= 2) weechat_printf (NULL, "irc_message_split: message='%s'", message); hashtable = weechat_hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; if (!message || !message[0]) goto end; if (message[0] == '@') { pos = strchr (message, ' '); if (pos) { tags = weechat_strndup (message, pos - message + 1); message = pos + 1; } } argv = weechat_string_split (message, " ", 0, 0, &argc); argv_eol = weechat_string_split (message, " ", 2, 0, NULL); if (argc < 2) goto end; if (argv[0][0] == ':') { if (argc < 3) goto end; host = argv[0]; command = argv[1]; arguments = argv_eol[2]; index_args = 2; } else { command = argv[0]; arguments = argv_eol[1]; index_args = 1; } max_length_nick = (server && (server->nick_max_length > 0)) ? server->nick_max_length : 16; max_length_host = 1 + /* ":" */ max_length_nick + /* nick */ 1 + /* "!" */ 63 + /* host */ 1; /* " " */ if ((weechat_strcasecmp (command, "ison") == 0) || (weechat_strcasecmp (command, "wallops") == 0)) { /* * ISON :nick1 nick2 nick3 * WALLOPS :some text here */ split_ok = irc_message_split_string ( hashtable, tags, host, command, NULL, ":", (argv_eol[index_args][0] == ':') ? argv_eol[index_args] + 1 : argv_eol[index_args], NULL, ' ', max_length_host); } else if (weechat_strcasecmp (command, "monitor") == 0) { /* * MONITOR + nick1,nick2,nick3 * MONITOR - nick1,nick2,nick3 */ if (((argv_eol[index_args][0] == '+') || (argv_eol[index_args][0] == '-')) && (argv_eol[index_args][1] == ' ')) { snprintf (monitor_action, sizeof (monitor_action), "%c ", argv_eol[index_args][0]); split_ok = irc_message_split_string ( hashtable, tags, host, command, NULL, monitor_action, argv_eol[index_args] + 2, NULL, ',', max_length_host); } else { split_ok = irc_message_split_string ( hashtable, tags, host, command, NULL, ":", (argv_eol[index_args][0] == ':') ? argv_eol[index_args] + 1 : argv_eol[index_args], NULL, ',', max_length_host); } } else if (weechat_strcasecmp (command, "join") == 0) { /* JOIN #channel1,#channel2,#channel3 key1,key2 */ if (strlen (message) > 510) { /* split join if it's more than 510 bytes */ split_ok = irc_message_split_join (hashtable, tags, host, arguments); } } else if ((weechat_strcasecmp (command, "privmsg") == 0) || (weechat_strcasecmp (command, "notice") == 0)) { /* * PRIVMSG target :some text here * NOTICE target :some text here */ if (index_args + 1 <= argc - 1) { split_ok = irc_message_split_privmsg_notice ( hashtable, tags, host, command, argv[index_args], (argv_eol[index_args + 1][0] == ':') ? argv_eol[index_args + 1] + 1 : argv_eol[index_args + 1], max_length_host); } } else if (weechat_strcasecmp (command, "005") == 0) { /* :server 005 nick MODES=4 CHANLIMIT=#:20 NICKLEN=16 USERLEN=10 ... */ if (index_args + 1 <= argc - 1) { split_ok = irc_message_split_005 ( hashtable, tags, host, command, argv[index_args], (argv_eol[index_args + 1][0] == ':') ? argv_eol[index_args + 1] + 1 : argv_eol[index_args + 1]); } } else if (weechat_strcasecmp (command, "353") == 0) { /* * list of users on channel: * :server 353 mynick = #channel :mynick nick1 @nick2 +nick3 */ if (index_args + 2 <= argc - 1) { if (irc_channel_is_channel (server, argv[index_args + 1])) { snprintf (target, sizeof (target), "%s %s", argv[index_args], argv[index_args + 1]); split_ok = irc_message_split_string ( hashtable, tags, host, command, target, ":", (argv_eol[index_args + 2][0] == ':') ? argv_eol[index_args + 2] + 1 : argv_eol[index_args + 2], NULL, ' ', -1); } else { if (index_args + 3 <= argc - 1) { snprintf (target, sizeof (target), "%s %s %s", argv[index_args], argv[index_args + 1], argv[index_args + 2]); split_ok = irc_message_split_string ( hashtable, tags, host, command, target, ":", (argv_eol[index_args + 3][0] == ':') ? argv_eol[index_args + 3] + 1 : argv_eol[index_args + 3], NULL, ' ', -1); } } } } end: if (!split_ok || (weechat_hashtable_get_integer (hashtable, "items_count") == 0)) { irc_message_split_add (hashtable, 1, tags, message, arguments); } if (tags) free (tags); if (argv) weechat_string_free_split (argv); if (argv_eol) weechat_string_free_split (argv_eol); return hashtable; }