diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2007-02-05 22:18:33 +0000 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2007-02-05 22:18:33 +0000 |
commit | 8e436c58cd2d2d914b93521263007d31e171ec18 (patch) | |
tree | 4241dd53994121d8cf029e89cd60d43aaa876128 /src/plugins | |
parent | 4713f94602236a6c534d55e81a093e72f3e6c47f (diff) | |
download | weechat-8e436c58cd2d2d914b93521263007d31e171ec18.zip |
Added event handler to plugin API
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/plugins-interface.c | 25 | ||||
-rw-r--r-- | src/plugins/plugins.c | 113 | ||||
-rw-r--r-- | src/plugins/plugins.h | 4 | ||||
-rw-r--r-- | src/plugins/scripts/lua/weechat-lua.c | 186 | ||||
-rw-r--r-- | src/plugins/scripts/perl/weechat-perl.c | 106 | ||||
-rw-r--r-- | src/plugins/scripts/python/weechat-python.c | 108 | ||||
-rw-r--r-- | src/plugins/scripts/ruby/weechat-ruby.c | 118 | ||||
-rw-r--r-- | src/plugins/scripts/weechat-script.c | 28 | ||||
-rw-r--r-- | src/plugins/scripts/weechat-script.h | 3 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 12 |
10 files changed, 668 insertions, 35 deletions
diff --git a/src/plugins/plugins-interface.c b/src/plugins/plugins-interface.c index 64eed838b..f96c0e66e 100644 --- a/src/plugins/plugins-interface.c +++ b/src/plugins/plugins-interface.c @@ -357,6 +357,22 @@ weechat_plugin_keyboard_handler_add (t_weechat_plugin *plugin, } /* + * weechat_plugin_event_handler_add: add an event handler + */ + +t_plugin_handler * +weechat_plugin_event_handler_add (t_weechat_plugin *plugin, char *event, + t_plugin_handler_func *handler_func, + char *handler_args, void *handler_pointer) +{ + if (plugin && event && handler_func) + return plugin_event_handler_add (plugin, event, handler_func, + handler_args, handler_pointer); + + return NULL; +} + +/* * weechat_plugin_handler_remove: remove a WeeChat handler */ @@ -571,6 +587,15 @@ weechat_plugin_get_info (t_weechat_plugin *plugin, char *info, char *server) if (ptr_server && ptr_server->is_connected && ptr_server->name) return strdup (ptr_server->name); } + else if (ascii_strcasecmp (info, "type") == 0) + { + return_str = (char *) malloc (32); + if (!return_str) + return NULL; + snprintf (return_str, 32, "%d", + gui_current_window->buffer->type); + return return_str; + } else if (ascii_strcasecmp (info, "away") == 0) { if (ptr_server && ptr_server->is_connected && ptr_server->is_away) diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 0c5d58148..d63bbf256 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -221,6 +221,7 @@ plugin_msg_handler_add (t_weechat_plugin *plugin, char *irc_command, new_handler->completion_template = NULL; new_handler->interval = 0; new_handler->remaining = 0; + new_handler->event = NULL; new_handler->handler = handler_func; new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; new_handler->handler_pointer = handler_pointer; @@ -304,6 +305,7 @@ plugin_cmd_handler_add (t_weechat_plugin *plugin, char *command, new_handler->completion_template = (completion_template) ? strdup (completion_template) : strdup (""); new_handler->interval = 0; new_handler->remaining = 0; + new_handler->event = NULL; new_handler->handler = handler_func; new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; new_handler->handler_pointer = handler_pointer; @@ -364,6 +366,7 @@ plugin_timer_handler_add (t_weechat_plugin *plugin, int interval, new_handler->completion_template = NULL; new_handler->interval = interval; new_handler->remaining = interval; + new_handler->event = NULL; new_handler->handler = handler_func; new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; new_handler->handler_pointer = handler_pointer; @@ -390,14 +393,13 @@ plugin_timer_handler_add (t_weechat_plugin *plugin, int interval, } /* - * plugin_keyboard_handler_add: add a timer handler + * plugin_keyboard_handler_add: add a keyboard handler * arguments: * 1. the plugin pointer - * 2. the interval between two calls - * 3. the handler function - * 4. handler args: a string given to + * 2. the handler function + * 3. handler args: a string given to * handler when called (used by scripts) - * 5. handler pointer: a pointer given to + * 4. handler pointer: a pointer given to * handler when called (used by scripts) */ @@ -420,6 +422,7 @@ plugin_keyboard_handler_add (t_weechat_plugin *plugin, new_handler->completion_template = NULL; new_handler->interval = 0; new_handler->remaining = 0; + new_handler->event = NULL; new_handler->handler = handler_func; new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; new_handler->handler_pointer = handler_pointer; @@ -446,6 +449,63 @@ plugin_keyboard_handler_add (t_weechat_plugin *plugin, } /* + * plugin_event_handler_add: add an event handler + * arguments: + * 1. the plugin pointer + * 2. the event to catch + * 3. the handler function + * 4. handler args: a string given to + * handler when called (used by scripts) + * 5. handler pointer: a pointer given to + * handler when called (used by scripts) + */ + +t_plugin_handler * +plugin_event_handler_add (t_weechat_plugin *plugin, char *event, + t_plugin_handler_func *handler_func, + char *handler_args, void *handler_pointer) +{ + t_plugin_handler *new_handler; + + new_handler = (t_plugin_handler *)malloc (sizeof (t_plugin_handler)); + if (new_handler) + { + new_handler->type = PLUGIN_HANDLER_EVENT; + new_handler->irc_command = NULL; + new_handler->command = NULL; + new_handler->description = NULL; + new_handler->arguments = NULL; + new_handler->arguments_description = NULL; + new_handler->completion_template = NULL; + new_handler->interval = 0; + new_handler->remaining = 0; + new_handler->event = strdup (event); + new_handler->handler = handler_func; + new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; + new_handler->handler_pointer = handler_pointer; + new_handler->running = 0; + + /* add new handler to list */ + new_handler->prev_handler = plugin->last_handler; + new_handler->next_handler = NULL; + if (plugin->handlers) + (plugin->last_handler)->next_handler = new_handler; + else + plugin->handlers = new_handler; + plugin->last_handler = new_handler; + } + else + { + irc_display_prefix (NULL, NULL, PREFIX_ERROR); + gui_printf (NULL, + _("%s plugin %s: unable to add event handler (not enough memory)\n"), + WEECHAT_ERROR, plugin->name); + return NULL; + } + return new_handler; +} + +/* * plugin_msg_handler_exec: execute a message handler * return: code for informing WeeChat whether message * should be ignored or not @@ -628,6 +688,46 @@ plugin_keyboard_handler_exec (char *key, char *input_before, char *input_after) } /* + * plugin_event_handler_exec: execute an event handler + * return: PLUGIN_RC_OK if all ok + * PLUGIN_RC_KO if at least one handler failed + */ + +int +plugin_event_handler_exec (char *event, char *data) +{ + t_weechat_plugin *ptr_plugin; + t_plugin_handler *ptr_handler; + int return_code, final_return_code; + char *argv[1] = { NULL }; + + argv[0] = data; + + final_return_code = PLUGIN_RC_OK; + + for (ptr_plugin = weechat_plugins; ptr_plugin; + ptr_plugin = ptr_plugin->next_plugin) + { + for (ptr_handler = ptr_plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == PLUGIN_HANDLER_EVENT) + && (ascii_strcasecmp (ptr_handler->event, event) == 0)) + { + return_code = ((int) (ptr_handler->handler) (ptr_plugin, + 1, argv, + ptr_handler->handler_args, + ptr_handler->handler_pointer)); + if (return_code == PLUGIN_RC_KO) + final_return_code = PLUGIN_RC_KO; + } + } + } + + return final_return_code; +} + +/* * plugin_handler_remove: remove a handler for a plugin */ @@ -667,6 +767,8 @@ plugin_handler_remove (t_weechat_plugin *plugin, free (handler->arguments); if (handler->arguments_description) free (handler->arguments_description); + if (handler->event) + free (handler->event); if (handler->handler_args) free (handler->handler_args); @@ -1055,6 +1157,7 @@ plugin_load (char *filename) new_plugin->cmd_handler_add = &weechat_plugin_cmd_handler_add; new_plugin->timer_handler_add = &weechat_plugin_timer_handler_add; new_plugin->keyboard_handler_add = &weechat_plugin_keyboard_handler_add; + new_plugin->event_handler_add = &weechat_plugin_event_handler_add; new_plugin->handler_remove = &weechat_plugin_handler_remove; new_plugin->handler_remove_all = &weechat_plugin_handler_remove_all; new_plugin->modifier_add = &weechat_plugin_modifier_add; diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index 035333c34..d1a421f1b 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -61,10 +61,14 @@ extern t_plugin_handler *plugin_timer_handler_add (t_weechat_plugin *, int, extern t_plugin_handler *plugin_keyboard_handler_add (t_weechat_plugin *, t_plugin_handler_func *, char *, void *); +extern t_plugin_handler *plugin_event_handler_add (t_weechat_plugin *, char *, + t_plugin_handler_func *, + char *, void *); extern int plugin_msg_handler_exec (char *, char *, char *); extern int plugin_cmd_handler_exec (char *, char *, char *); extern int plugin_timer_handler_exec (); extern int plugin_keyboard_handler_exec (char *, char *, char *); +extern int plugin_event_handler_exec (char *, char *); extern void plugin_handler_remove (t_weechat_plugin *, t_plugin_handler *); extern void plugin_handler_remove_all (t_weechat_plugin *); diff --git a/src/plugins/scripts/lua/weechat-lua.c b/src/plugins/scripts/lua/weechat-lua.c index 31ea9b122..056d620ce 100644 --- a/src/plugins/scripts/lua/weechat-lua.c +++ b/src/plugins/scripts/lua/weechat-lua.c @@ -201,6 +201,36 @@ weechat_lua_keyboard_handler (t_weechat_plugin *plugin, } /* + * weechat_lua_event_handler: general event handler for Lua + */ + +int +weechat_lua_event_handler (t_weechat_plugin *plugin, + int argc, char **argv, + char *handler_args, void *handler_pointer) +{ + int *r; + int ret; + + if (argc >= 1) + { + r = (int *) weechat_lua_exec (plugin, (t_plugin_script *)handler_pointer, + SCRIPT_EXEC_INT, + handler_args, argv[0], NULL, NULL); + if (r == NULL) + ret = PLUGIN_RC_KO; + else + { + ret = *r; + free (r); + } + return ret; + } + else + return PLUGIN_RC_KO; +} + +/* * weechat_lua_modifier: general modifier for Lua */ @@ -310,7 +340,7 @@ weechat_lua_register (lua_State *L) */ static int -weechat_lua_set_charset (lua_State *L) +weechat_lua_set_charset (lua_State *L) { const char *charset; int n; @@ -355,7 +385,7 @@ weechat_lua_set_charset (lua_State *L) */ static int -weechat_lua_print (lua_State *L) +weechat_lua_print (lua_State *L) { const char *message, *channel_name, *server_name; int n; @@ -414,7 +444,7 @@ weechat_lua_print (lua_State *L) */ static int -weechat_lua_print_server (lua_State *L) +weechat_lua_print_server (lua_State *L) { const char *message; int n; @@ -457,7 +487,7 @@ weechat_lua_print_server (lua_State *L) */ static int -weechat_lua_print_infobar (lua_State *L) +weechat_lua_print_infobar (lua_State *L) { const char *message; int delay, n; @@ -502,7 +532,7 @@ weechat_lua_print_infobar (lua_State *L) */ static int -weechat_lua_remove_infobar (lua_State *L) +weechat_lua_remove_infobar (lua_State *L) { int n, how_many; @@ -536,7 +566,7 @@ weechat_lua_remove_infobar (lua_State *L) */ static int -weechat_lua_log (lua_State *L) +weechat_lua_log (lua_State *L) { const char *message, *channel_name, *server_name; int n; @@ -595,7 +625,7 @@ weechat_lua_log (lua_State *L) */ static int -weechat_lua_command (lua_State *L) +weechat_lua_command (lua_State *L) { const char *command, *channel_name, *server_name; int n; @@ -654,7 +684,7 @@ weechat_lua_command (lua_State *L) */ static int -weechat_lua_add_message_handler (lua_State *L) +weechat_lua_add_message_handler (lua_State *L) { const char *irc_command, *function; int n; @@ -706,7 +736,7 @@ weechat_lua_add_message_handler (lua_State *L) */ static int -weechat_lua_add_command_handler (lua_State *L) +weechat_lua_add_command_handler (lua_State *L) { const char *command, *function, *description, *arguments, *arguments_description; const char *completion_template; @@ -778,7 +808,7 @@ weechat_lua_add_command_handler (lua_State *L) */ static int -weechat_lua_add_timer_handler (lua_State *L) +weechat_lua_add_timer_handler (lua_State *L) { int interval; const char *function; @@ -831,7 +861,7 @@ weechat_lua_add_timer_handler (lua_State *L) */ static int -weechat_lua_add_keyboard_handler (lua_State *L) +weechat_lua_add_keyboard_handler (lua_State *L) { const char *function; int n; @@ -877,6 +907,58 @@ weechat_lua_add_keyboard_handler (lua_State *L) } /* + * weechat_lua_add_event_handler: add handler for events + */ + +static int +weechat_lua_add_event_handler (lua_State *L) +{ + const char *event, *function; + int n; + + /* make gcc happy */ + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to add event handler, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + event = NULL; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 2) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"add_event_handler\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + event = lua_tostring (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + if (!lua_plugin->event_handler_add (lua_plugin, (char *) event, + weechat_lua_event_handler, + (char *) function, + (void *)lua_current_script)) + { + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + lua_pushnumber (lua_current_interpreter, 1); + return 1; +} + +/* * weechat_lua_remove_handler: remove a command/message handler */ @@ -1011,6 +1093,50 @@ weechat_lua_remove_keyboard_handler (lua_State *L) } /* + * weechat_lua_remove_event_handler: remove an event handler + */ + +static int +weechat_lua_remove_event_handler (lua_State *L) +{ + const char *function; + int n; + + /* make gcc happy */ + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to remove event handler, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 1) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"remove_event_handler\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + function = lua_tostring (lua_current_interpreter, -1); + + weechat_script_remove_event_handler (lua_plugin, lua_current_script, + (char *) function); + + lua_pushnumber (lua_current_interpreter, 1); + return 1; +} + +/* * weechat_lua_add_modifier: add a modifier */ @@ -1118,7 +1244,7 @@ weechat_lua_remove_modifier (lua_State *L) */ static int -weechat_lua_get_info (lua_State *L) +weechat_lua_get_info (lua_State *L) { const char *arg, *server_name; char *info; @@ -1172,7 +1298,7 @@ weechat_lua_get_info (lua_State *L) */ static int -weechat_lua_get_dcc_info (lua_State *L) +weechat_lua_get_dcc_info (lua_State *L) { t_plugin_dcc_info *dcc_info, *ptr_dcc; char timebuffer1[64]; @@ -1289,7 +1415,7 @@ weechat_lua_get_dcc_info (lua_State *L) */ static int -weechat_lua_get_config (lua_State *L) +weechat_lua_get_config (lua_State *L) { const char *option; char *return_value; @@ -1336,7 +1462,7 @@ weechat_lua_get_config (lua_State *L) */ static int -weechat_lua_set_config (lua_State *L) +weechat_lua_set_config (lua_State *L) { const char *option, *value; int n; @@ -1383,7 +1509,7 @@ weechat_lua_set_config (lua_State *L) */ static int -weechat_lua_get_plugin_config (lua_State *L) +weechat_lua_get_plugin_config (lua_State *L) { const char *option; char *return_value; @@ -1432,7 +1558,7 @@ weechat_lua_get_plugin_config (lua_State *L) */ static int -weechat_lua_set_plugin_config (lua_State *L) +weechat_lua_set_plugin_config (lua_State *L) { const char *option, *value; int n; @@ -1481,7 +1607,7 @@ weechat_lua_set_plugin_config (lua_State *L) */ static int -weechat_lua_get_server_info (lua_State *L) +weechat_lua_get_server_info (lua_State *L) { t_plugin_server_info *server_info, *ptr_server; char timebuffer[64]; @@ -1627,7 +1753,7 @@ weechat_lua_get_server_info (lua_State *L) */ static int -weechat_lua_get_channel_info (lua_State *L) +weechat_lua_get_channel_info (lua_State *L) { t_plugin_channel_info *channel_info, *ptr_channel; const char *server; @@ -1711,7 +1837,7 @@ weechat_lua_get_channel_info (lua_State *L) */ static int -weechat_lua_get_nick_info (lua_State *L) +weechat_lua_get_nick_info (lua_State *L) { t_plugin_nick_info *nick_info, *ptr_nick; const char *server, *channel; @@ -1825,7 +1951,7 @@ weechat_lua_get_irc_color (lua_State *L) */ static int -weechat_lua_get_window_info (lua_State *L) +weechat_lua_get_window_info (lua_State *L) { t_plugin_window_info *window_info, *ptr_window; int i; @@ -1897,7 +2023,7 @@ weechat_lua_get_window_info (lua_State *L) */ static int -weechat_lua_get_buffer_info (lua_State *L) +weechat_lua_get_buffer_info (lua_State *L) { t_plugin_buffer_info *buffer_info, *ptr_buffer; @@ -1929,7 +2055,7 @@ weechat_lua_get_buffer_info (lua_State *L) lua_pushstring (lua_current_interpreter, "type"); lua_pushnumber (lua_current_interpreter, ptr_buffer->type); lua_rawset (lua_current_interpreter, -3); - + lua_pushstring (lua_current_interpreter, "num_displayed"); lua_pushnumber (lua_current_interpreter, ptr_buffer->num_displayed); lua_rawset (lua_current_interpreter, -3); @@ -1966,7 +2092,7 @@ weechat_lua_get_buffer_info (lua_State *L) */ static int -weechat_lua_get_buffer_data (lua_State *L) +weechat_lua_get_buffer_data (lua_State *L) { t_plugin_buffer_line *buffer_data, *ptr_data; const char *server, *channel; @@ -2053,7 +2179,7 @@ weechat_lua_get_buffer_data (lua_State *L) */ static int -weechat_lua_constant_plugin_rc_ok (lua_State *L) +weechat_lua_constant_plugin_rc_ok (lua_State *L) { /* make gcc happy */ (void) L; @@ -2063,7 +2189,7 @@ weechat_lua_constant_plugin_rc_ok (lua_State *L) } static int -weechat_lua_constant_plugin_rc_ko (lua_State *L) +weechat_lua_constant_plugin_rc_ko (lua_State *L) { /* make gcc happy */ (void) L; @@ -2073,7 +2199,7 @@ weechat_lua_constant_plugin_rc_ko (lua_State *L) } static int -weechat_lua_constant_plugin_rc_ok_ignore_weechat (lua_State *L) +weechat_lua_constant_plugin_rc_ok_ignore_weechat (lua_State *L) { /* make gcc happy */ (void) L; @@ -2083,7 +2209,7 @@ weechat_lua_constant_plugin_rc_ok_ignore_weechat (lua_State *L) } static int -weechat_lua_constant_plugin_rc_ok_ignore_plugins (lua_State *L) +weechat_lua_constant_plugin_rc_ok_ignore_plugins (lua_State *L) { /* make gcc happy */ (void) L; @@ -2093,7 +2219,7 @@ weechat_lua_constant_plugin_rc_ok_ignore_plugins (lua_State *L) } static int -weechat_lua_constant_plugin_rc_ok_ignore_all (lua_State *L) +weechat_lua_constant_plugin_rc_ok_ignore_all (lua_State *L) { /* make gcc happy */ (void) L; @@ -2120,9 +2246,11 @@ const struct luaL_reg weechat_lua_funcs[] = { { "add_command_handler", weechat_lua_add_command_handler }, { "add_timer_handler", weechat_lua_add_timer_handler }, { "add_keyboard_handler", weechat_lua_add_keyboard_handler }, + { "add_event_handler", weechat_lua_add_event_handler }, { "remove_handler", weechat_lua_remove_handler }, { "remove_timer_handler", weechat_lua_remove_timer_handler }, { "remove_keyboard_handler", weechat_lua_remove_keyboard_handler }, + { "remove_event_handler", weechat_lua_remove_event_handler }, { "add_modifier", weechat_lua_add_modifier }, { "remove_modifier", weechat_lua_remove_modifier }, { "get_info", weechat_lua_get_info }, diff --git a/src/plugins/scripts/perl/weechat-perl.c b/src/plugins/scripts/perl/weechat-perl.c index 4c96bb923..8827e4553 100644 --- a/src/plugins/scripts/perl/weechat-perl.c +++ b/src/plugins/scripts/perl/weechat-perl.c @@ -321,6 +321,35 @@ weechat_perl_keyboard_handler (t_weechat_plugin *plugin, } /* + * weechat_perl_event_handler: general event handler for Perl + */ + +int +weechat_perl_event_handler (t_weechat_plugin *plugin, + int argc, char **argv, + char *handler_args, void *handler_pointer) +{ + int *r, ret; + + if (argc >= 1) + { + r = (int *) weechat_perl_exec (plugin, (t_plugin_script *)handler_pointer, + SCRIPT_EXEC_INT, + handler_args, argv[0], NULL, NULL); + if (r == NULL) + ret = PLUGIN_RC_KO; + else + { + ret = *r; + free (r); + } + return ret; + } + else + return PLUGIN_RC_KO; +} + +/* * weechat_perl_modifier: general modifier for Perl */ @@ -836,6 +865,45 @@ static XS (XS_weechat_add_keyboard_handler) } /* + * weechat::add_event_handler: add an event handler + */ + +static XS (XS_weechat_add_event_handler) +{ + char *event, *function; + dXSARGS; + + /* make gcc happy */ + (void) cv; + + if (!perl_current_script) + { + perl_plugin->print_server (perl_plugin, + "Perl error: unable to add event handler, " + "script not initialized"); + XSRETURN_NO; + } + + if (items < 2) + { + perl_plugin->print_server (perl_plugin, + "Perl error: wrong parameters for " + "\"add_event_handler\" function"); + XSRETURN_NO; + } + + event = SvPV (ST (0), PL_na); + function = SvPV (ST (1), PL_na); + + if (perl_plugin->event_handler_add (perl_plugin, event, + weechat_perl_event_handler, function, + (void *)perl_current_script)) + XSRETURN_YES; + + XSRETURN_NO; +} + +/* * weechat::remove_handler: remove a message/command handler */ @@ -945,6 +1013,42 @@ static XS (XS_weechat_remove_keyboard_handler) } /* + * weechat::remove_event_handler: remove an event handler + */ + +static XS (XS_weechat_remove_event_handler) +{ + char *function; + dXSARGS; + + /* make gcc happy */ + (void) cv; + + if (!perl_current_script) + { + perl_plugin->print_server (perl_plugin, + "Perl error: unable to remove event handler, " + "script not initialized"); + XSRETURN_NO; + } + + if (items < 1) + { + perl_plugin->print_server (perl_plugin, + "Perl error: wrong parameters for " + "\"remove_event_handler\" function"); + XSRETURN_NO; + } + + function = SvPV (ST (0), PL_na); + + weechat_script_remove_event_handler (perl_plugin, perl_current_script, + function); + + XSRETURN_YES; +} + +/* * weechat::add_modifier: add a modifier */ @@ -1798,9 +1902,11 @@ weechat_perl_xs_init (pTHX) newXS ("weechat::add_command_handler", XS_weechat_add_command_handler, "weechat"); newXS ("weechat::add_timer_handler", XS_weechat_add_timer_handler, "weechat"); newXS ("weechat::add_keyboard_handler", XS_weechat_add_keyboard_handler, "weechat"); + newXS ("weechat::add_event_handler", XS_weechat_add_event_handler, "weechat"); newXS ("weechat::remove_handler", XS_weechat_remove_handler, "weechat"); newXS ("weechat::remove_timer_handler", XS_weechat_remove_timer_handler, "weechat"); newXS ("weechat::remove_keyboard_handler", XS_weechat_remove_keyboard_handler, "weechat"); + newXS ("weechat::remove_event_handler", XS_weechat_remove_event_handler, "weechat"); newXS ("weechat::add_modifier", XS_weechat_add_modifier, "weechat"); newXS ("weechat::remove_modifier", XS_weechat_remove_modifier, "weechat"); newXS ("weechat::get_info", XS_weechat_get_info, "weechat"); diff --git a/src/plugins/scripts/python/weechat-python.c b/src/plugins/scripts/python/weechat-python.c index 79bf6e139..fba55d097 100644 --- a/src/plugins/scripts/python/weechat-python.c +++ b/src/plugins/scripts/python/weechat-python.c @@ -243,6 +243,36 @@ weechat_python_keyboard_handler (t_weechat_plugin *plugin, } /* + * weechat_python_event_handler: general event handler for Python + */ + +int +weechat_python_event_handler (t_weechat_plugin *plugin, + int argc, char **argv, + char *handler_args, void *handler_pointer) +{ + int *r; + int ret; + + if (argc >= 1) + { + r = (int *) weechat_python_exec (plugin, (t_plugin_script *)handler_pointer, + SCRIPT_EXEC_INT, + handler_args, argv[0], NULL, NULL); + if (r == NULL) + ret = PLUGIN_RC_KO; + else + { + ret = *r; + free (r); + } + return ret; + } + else + return PLUGIN_RC_KO; +} + +/* * weechat_python_modifier: general modifier for Python */ @@ -756,6 +786,46 @@ weechat_python_add_keyboard_handler (PyObject *self, PyObject *args) } /* + * weechat_python_add_event_handler: add handler for events + */ + +static PyObject * +weechat_python_add_event_handler (PyObject *self, PyObject *args) +{ + char *event, *function; + + /* make gcc happy */ + (void) self; + + if (!python_current_script) + { + python_plugin->print_server (python_plugin, + "Python error: unable to add event handler, " + "script not initialized"); + return Py_BuildValue ("i", 0); + } + + event = NULL; + function = NULL; + + if (!PyArg_ParseTuple (args, "ss", &event, &function)) + { + python_plugin->print_server (python_plugin, + "Python error: wrong parameters for " + "\"add_event_handler\" function"); + return Py_BuildValue ("i", 0); + } + + if (python_plugin->event_handler_add (python_plugin, event, + weechat_python_event_handler, + function, + (void *)python_current_script)) + return Py_BuildValue ("i", 1); + + return Py_BuildValue ("i", 0); +} + +/* * weechat_python_remove_handler: remove a handler */ @@ -865,6 +935,42 @@ weechat_python_remove_keyboard_handler (PyObject *self, PyObject *args) } /* + * weechat_python_remove_event_handler: remove an event handler + */ + +static PyObject * +weechat_python_remove_event_handler (PyObject *self, PyObject *args) +{ + char *function; + + /* make gcc happy */ + (void) self; + + if (!python_current_script) + { + python_plugin->print_server (python_plugin, + "Python error: unable to remove event handler, " + "script not initialized"); + return Py_BuildValue ("i", 0); + } + + function = NULL; + + if (!PyArg_ParseTuple (args, "s", &function)) + { + python_plugin->print_server (python_plugin, + "Python error: wrong parameters for " + "\"remove_event_handler\" function"); + return Py_BuildValue ("i", 0); + } + + weechat_script_remove_event_handler (python_plugin, python_current_script, + function); + + return Py_BuildValue ("i", 1); +} + +/* * weechat_python_add_modifier: add a modifier */ @@ -1761,9 +1867,11 @@ PyMethodDef weechat_python_funcs[] = { { "add_command_handler", weechat_python_add_command_handler, METH_VARARGS, "" }, { "add_timer_handler", weechat_python_add_timer_handler, METH_VARARGS, "" }, { "add_keyboard_handler", weechat_python_add_keyboard_handler, METH_VARARGS, "" }, + { "add_event_handler", weechat_python_add_event_handler, METH_VARARGS, "" }, { "remove_handler", weechat_python_remove_handler, METH_VARARGS, "" }, { "remove_timer_handler", weechat_python_remove_timer_handler, METH_VARARGS, "" }, { "remove_keyboard_handler", weechat_python_remove_keyboard_handler, METH_VARARGS, "" }, + { "remove_event_handler", weechat_python_remove_event_handler, METH_VARARGS, "" }, { "add_modifier", weechat_python_add_modifier, METH_VARARGS, "" }, { "remove_modifier", weechat_python_remove_modifier, METH_VARARGS, "" }, { "get_info", weechat_python_get_info, METH_VARARGS, "" }, diff --git a/src/plugins/scripts/ruby/weechat-ruby.c b/src/plugins/scripts/ruby/weechat-ruby.c index d59ff7360..ec00ff9e3 100644 --- a/src/plugins/scripts/ruby/weechat-ruby.c +++ b/src/plugins/scripts/ruby/weechat-ruby.c @@ -282,6 +282,36 @@ weechat_ruby_keyboard_handler (t_weechat_plugin *plugin, } /* + * weechat_ruby_event_handler: general event handler for Ruby + */ + +int +weechat_ruby_event_handler (t_weechat_plugin *plugin, + int argc, char **argv, + char *handler_args, void *handler_pointer) +{ + int *r; + int ret; + + if (argc >= 1) + { + r = (int *) weechat_ruby_exec (plugin, (t_plugin_script *)handler_pointer, + SCRIPT_EXEC_INT, + handler_args, argv[0], NULL, NULL); + if (r == NULL) + ret = PLUGIN_RC_KO; + else + { + ret = *r; + free (r); + } + return ret; + } + else + return PLUGIN_RC_KO; +} + +/* * weechat_ruby_modifier: general modifier for Ruby */ @@ -948,6 +978,52 @@ weechat_ruby_add_keyboard_handler (VALUE class, VALUE function) } /* + * weechat_ruby_add_event_handler: add a handler for events + */ + +static VALUE +weechat_ruby_add_event_handler (VALUE class, VALUE event, VALUE function) +{ + char *c_event, *c_function; + + /* make gcc happy */ + (void) class; + + if (!ruby_current_script) + { + ruby_plugin->print_server (ruby_plugin, + "Ruby error: unable to add event handler, " + "script not initialized"); + return INT2FIX (0); + } + + c_event = NULL; + c_function = NULL; + + if (NIL_P (event) || NIL_P (function)) + { + ruby_plugin->print_server (ruby_plugin, + "Ruby error: wrong parameters for " + "\"add_event_handler\" function"); + return INT2FIX (0); + } + + Check_Type (event, T_STRING); + Check_Type (function, T_STRING); + + c_event = STR2CSTR (event); + c_function = STR2CSTR (function); + + if (ruby_plugin->event_handler_add (ruby_plugin, c_event, + weechat_ruby_event_handler, + c_function, + (void *)ruby_current_script)) + return INT2FIX (1); + + return INT2FIX (0); +} + +/* * weechat_ruby_remove_handler: remove a handler */ @@ -1071,6 +1147,46 @@ weechat_ruby_remove_keyboard_handler (VALUE class, VALUE function) } /* + * weechat_ruby_remove_event_handler: remove an event handler + */ + +static VALUE +weechat_ruby_remove_event_handler (VALUE class, VALUE function) +{ + char *c_function; + + /* make gcc happy */ + (void) class; + + if (!ruby_current_script) + { + ruby_plugin->print_server (ruby_plugin, + "Ruby error: unable to remove event handler, " + "script not initialized"); + return INT2FIX (0); + } + + c_function = NULL; + + if (NIL_P (function)) + { + ruby_plugin->print_server (ruby_plugin, + "Ruby error: wrong parameters for " + "\"remove_event_handler\" function"); + return INT2FIX (0); + } + + Check_Type (function, T_STRING); + + c_function = STR2CSTR (function); + + weechat_script_remove_event_handler (ruby_plugin, ruby_current_script, + c_function); + + return INT2FIX (1); +} + +/* * weechat_ruby_add_modifier: add a modifier */ @@ -2486,9 +2602,11 @@ weechat_plugin_init (t_weechat_plugin *plugin) rb_define_module_function (ruby_mWeechat, "add_command_handler", weechat_ruby_add_command_handler, -1); rb_define_module_function (ruby_mWeechat, "add_timer_handler", weechat_ruby_add_timer_handler, 2); rb_define_module_function (ruby_mWeechat, "add_keyboard_handler", weechat_ruby_add_keyboard_handler, 1); + rb_define_module_function (ruby_mWeechat, "add_event_handler", weechat_ruby_add_event_handler, 2); rb_define_module_function (ruby_mWeechat, "remove_handler", weechat_ruby_remove_handler, 2); rb_define_module_function (ruby_mWeechat, "remove_timer_handler", weechat_ruby_remove_timer_handler, 1); rb_define_module_function (ruby_mWeechat, "remove_keyboard_handler", weechat_ruby_remove_keyboard_handler, 1); + rb_define_module_function (ruby_mWeechat, "remove_event_handler", weechat_ruby_remove_event_handler, 1); rb_define_module_function (ruby_mWeechat, "add_modifier", weechat_ruby_add_modifier, 3); rb_define_module_function (ruby_mWeechat, "remove_modifier", weechat_ruby_remove_modifier, 3); rb_define_module_function (ruby_mWeechat, "get_info", weechat_ruby_get_info, -1); diff --git a/src/plugins/scripts/weechat-script.c b/src/plugins/scripts/weechat-script.c index f10bcbd40..54c76c4ed 100644 --- a/src/plugins/scripts/weechat-script.c +++ b/src/plugins/scripts/weechat-script.c @@ -517,6 +517,34 @@ weechat_script_remove_keyboard_handler (t_weechat_plugin *plugin, } /* + * weechat_script_remove_event_handler: remove an event handler for a script + */ + +void +weechat_script_remove_event_handler (t_weechat_plugin *plugin, + t_plugin_script *script, + char *function) +{ + t_plugin_handler *ptr_handler, *next_handler; + + /* search and remove timer handlers */ + ptr_handler = plugin->handlers; + while (ptr_handler) + { + if ((ptr_handler->type == PLUGIN_HANDLER_EVENT) + && ((t_plugin_script *)ptr_handler->handler_pointer == script) + && (plugin->ascii_strcasecmp (plugin, ptr_handler->handler_args, function) == 0)) + { + next_handler = ptr_handler->next_handler; + plugin->handler_remove (plugin, ptr_handler); + ptr_handler = next_handler; + } + else + ptr_handler = ptr_handler->next_handler; + } +} + +/* * weechat_script_remove_modifier: remove a modifier * arg1=type, arg2=command, arg3=function */ diff --git a/src/plugins/scripts/weechat-script.h b/src/plugins/scripts/weechat-script.h index 0f5ef956d..74af518f6 100644 --- a/src/plugins/scripts/weechat-script.h +++ b/src/plugins/scripts/weechat-script.h @@ -79,6 +79,9 @@ extern void weechat_script_remove_timer_handler (t_weechat_plugin *, extern void weechat_script_remove_keyboard_handler (t_weechat_plugin *, t_plugin_script *, char *); +extern void weechat_script_remove_event_handler (t_weechat_plugin *, + t_plugin_script *, + char *); extern void weechat_script_remove_modifier (t_weechat_plugin *, t_plugin_script *, char *, char *, char *); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 24f32e3ca..9e0e50684 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -191,7 +191,8 @@ enum t_plugin_handler_type PLUGIN_HANDLER_MESSAGE = 0, /* IRC message handler */ PLUGIN_HANDLER_COMMAND, /* command handler */ PLUGIN_HANDLER_TIMER, /* timer handler */ - PLUGIN_HANDLER_KEYBOARD /* keyboard handler */ + PLUGIN_HANDLER_KEYBOARD, /* keyboard handler */ + PLUGIN_HANDLER_EVENT /* event handler */ }; typedef struct t_plugin_handler t_plugin_handler; @@ -213,6 +214,9 @@ struct t_plugin_handler /* data for timer handler */ int interval; /* interval between two calls to fct */ int remaining; /* seconds remaining before next call */ + + /* data for event handler */ + char *event; /* event to catch */ /* data common to all handlers */ t_plugin_handler_func *handler; /* pointer to handler */ @@ -323,6 +327,9 @@ struct t_weechat_plugin t_plugin_handler *(*keyboard_handler_add) (t_weechat_plugin *, t_plugin_handler_func *, char *, void *); + t_plugin_handler *(*event_handler_add) (t_weechat_plugin *, char *, + t_plugin_handler_func *, + char *, void *); void (*handler_remove) (t_weechat_plugin *, t_plugin_handler *); void (*handler_remove_all) (t_weechat_plugin *); @@ -400,6 +407,9 @@ extern t_plugin_handler *weechat_plugin_timer_handler_add (t_weechat_plugin *, i extern t_plugin_handler *weechat_plugin_keyboard_handler_add (t_weechat_plugin *, t_plugin_handler_func *, char *, void *); +extern t_plugin_handler *weechat_plugin_event_handler_add (t_weechat_plugin *, char *, + t_plugin_handler_func *, + char *, void *); extern void weechat_plugin_handler_remove (t_weechat_plugin *, t_plugin_handler *); extern void weechat_plugin_handler_remove_all (t_weechat_plugin *); |