diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2006-02-19 10:43:47 +0000 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2006-02-19 10:43:47 +0000 |
commit | 3a213f38eca02f158169afd2f140050930f5cdc3 (patch) | |
tree | d46434e51141bb415e90afbdd9a76e65a4249549 /src | |
parent | 484274d65f27d21e8da1823f36fefd67e8bed67b (diff) | |
download | weechat-3a213f38eca02f158169afd2f140050930f5cdc3.zip |
Added timer handler for plugins
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/curses/gui-input.c | 4 | ||||
-rw-r--r-- | src/plugins/plugins-interface.c | 16 | ||||
-rw-r--r-- | src/plugins/plugins.c | 105 | ||||
-rw-r--r-- | src/plugins/plugins.h | 4 | ||||
-rw-r--r-- | src/plugins/scripts/lua/weechat-lua.c | 115 | ||||
-rw-r--r-- | src/plugins/scripts/perl/weechat-perl.c | 103 | ||||
-rw-r--r-- | src/plugins/scripts/python/weechat-python.c | 97 | ||||
-rw-r--r-- | src/plugins/scripts/ruby/weechat-ruby.c | 107 | ||||
-rw-r--r-- | src/plugins/scripts/weechat-script.c | 29 | ||||
-rw-r--r-- | src/plugins/scripts/weechat-script.h | 3 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 15 |
11 files changed, 594 insertions, 4 deletions
diff --git a/src/gui/curses/gui-input.c b/src/gui/curses/gui-input.c index 689407811..696ce9e1c 100644 --- a/src/gui/curses/gui-input.c +++ b/src/gui/curses/gui-input.c @@ -46,6 +46,7 @@ #include "../../common/fifo.h" #include "../../common/utf8.h" #include "../../irc/irc.h" +#include "../../plugins/plugins.h" /* @@ -379,6 +380,9 @@ gui_main_loop () server_check_away (); } } + + /* call timer handlers */ + plugin_timer_handler_exec (); } FD_ZERO (&read_fd); diff --git a/src/plugins/plugins-interface.c b/src/plugins/plugins-interface.c index bd7470eb2..e7f88712f 100644 --- a/src/plugins/plugins-interface.c +++ b/src/plugins/plugins-interface.c @@ -278,6 +278,22 @@ weechat_plugin_cmd_handler_add (t_weechat_plugin *plugin, char *command, } /* + * weechat_plugin_timer_handler_add: add a timer handler + */ + +t_plugin_handler * +weechat_plugin_timer_handler_add (t_weechat_plugin *plugin, int interval, + t_plugin_handler_func *handler_func, + char *handler_args, void *handler_pointer) +{ + if (plugin && (interval >= 1) && handler_func) + return plugin_timer_handler_add (plugin, interval, handler_func, + handler_args, handler_pointer); + + return NULL; +} + +/* * weechat_plugin_handler_remove: remove a WeeChat handler */ diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 8b3d31ec7..440034650 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -199,6 +199,9 @@ plugin_msg_handler_add (t_weechat_plugin *plugin, char *irc_command, 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->handler = handler_func; new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; new_handler->handler_pointer = handler_pointer; @@ -270,6 +273,8 @@ plugin_cmd_handler_add (t_weechat_plugin *plugin, char *command, new_handler->arguments = (arguments) ? strdup (arguments) : NULL; new_handler->arguments_description = (arguments_description) ? strdup (arguments_description) : NULL; new_handler->completion_template = (completion_template) ? strdup (completion_template) : NULL; + new_handler->interval = 0; + new_handler->remaining = 0; new_handler->handler = handler_func; new_handler->handler_args = (handler_args) ? strdup (handler_args) : NULL; new_handler->handler_pointer = handler_pointer; @@ -300,6 +305,62 @@ plugin_cmd_handler_add (t_weechat_plugin *plugin, char *command, } /* + * plugin_timer_handler_add: add a timer handler + * arguments: + * 1. the plugin pointer + * 2. the interval between two calls + * 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_timer_handler_add (t_weechat_plugin *plugin, int interval, + 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 = HANDLER_TIMER; + 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 = interval; + new_handler->remaining = interval; + 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 timer 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 @@ -391,6 +452,49 @@ plugin_cmd_handler_exec (char *server, char *command, char *arguments) } /* + * plugin_timer_handler_exec: check timer handlers and execute functions if needed + * return: PLUGIN_RC_OK if all ok + * PLUGIN_RC_KO if at least one handler failed + */ + +int +plugin_timer_handler_exec () +{ + t_weechat_plugin *ptr_plugin; + t_plugin_handler *ptr_handler; + int return_code, final_return_code; + + 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 == HANDLER_TIMER) + { + ptr_handler->remaining--; + if (ptr_handler->remaining <= 0) + { + return_code = ((int) (ptr_handler->handler) (ptr_plugin, + "", + "", + "", + ptr_handler->handler_args, + ptr_handler->handler_pointer)); + ptr_handler->remaining = ptr_handler->interval; + 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 */ @@ -621,6 +725,7 @@ plugin_load (char *filename) new_plugin->exec_on_files = &weechat_plugin_exec_on_files; new_plugin->msg_handler_add = &weechat_plugin_msg_handler_add; new_plugin->cmd_handler_add = &weechat_plugin_cmd_handler_add; + new_plugin->timer_handler_add = &weechat_plugin_timer_handler_add; new_plugin->handler_remove = &weechat_plugin_handler_remove; new_plugin->handler_remove_all = &weechat_plugin_handler_remove_all; new_plugin->print = &weechat_plugin_print; diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index 78d1ae95a..44e2e4359 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -44,8 +44,12 @@ extern t_plugin_handler *plugin_cmd_handler_add (t_weechat_plugin *, char *, char *, t_plugin_handler_func *, char *, void *); +extern t_plugin_handler *plugin_timer_handler_add (t_weechat_plugin *, int, + 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 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 f4e2751df..311c0a012 100644 --- a/src/plugins/scripts/lua/weechat-lua.c +++ b/src/plugins/scripts/lua/weechat-lua.c @@ -510,6 +510,57 @@ weechat_lua_add_command_handler (lua_State *L) } /* + * weechat_lua_add_timer_handler: add a timer handler + */ + +static int +weechat_lua_add_timer_handler (lua_State *L) +{ + int interval; + const char *function; + int n; + /* make gcc happy */ + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to add timer handler, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + interval = 10; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 2) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"add_timer_handler\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + interval = lua_tonumber (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + if (!lua_plugin->timer_handler_add (lua_plugin, interval, + weechat_lua_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 handler */ @@ -555,6 +606,49 @@ weechat_lua_remove_handler (lua_State *L) } /* + * weechat_lua_remove_timer_handler: remove a timer handler + */ + +static int +weechat_lua_remove_timer_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 timer 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_timer_handler\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + function = lua_tostring (lua_current_interpreter, -1); + + weechat_script_remove_timer_handler (lua_plugin, lua_current_script, + (char *) function); + + lua_pushnumber (lua_current_interpreter, 1); + return 1; +} + +/* * weechat_lua_get_info: get various infos */ @@ -1275,7 +1369,9 @@ const struct luaL_reg weechat_lua_funcs[] = { { "command", weechat_lua_command}, { "add_message_handler", weechat_lua_add_message_handler}, { "add_command_handler", weechat_lua_add_command_handler}, + { "add_timer_handler", weechat_lua_add_timer_handler}, { "remove_handler", weechat_lua_remove_handler}, + { "remove_timer_handler", weechat_lua_remove_timer_handler}, { "get_info", weechat_lua_get_info}, { "get_dcc_info", weechat_lua_get_dcc_info}, { "get_config", weechat_lua_get_config}, @@ -1540,6 +1636,25 @@ weechat_lua_cmd (t_weechat_plugin *plugin, } if (!handler_found) plugin->print_server (plugin, " (none)"); + + /* list Lua timer handlers */ + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Lua timer handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == HANDLER_MESSAGE) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " %d seconds => Lua(%s)", + ptr_handler->interval, + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); break; case 1: if (plugin->ascii_strcasecmp (plugin, argv[0], "autoload") == 0) diff --git a/src/plugins/scripts/perl/weechat-perl.c b/src/plugins/scripts/perl/weechat-perl.c index 5b329a506..d6eaa27f4 100644 --- a/src/plugins/scripts/perl/weechat-perl.c +++ b/src/plugins/scripts/perl/weechat-perl.c @@ -522,7 +522,50 @@ static XS (XS_weechat_add_command_handler) } /* - * weechat::remove_handler: remove a handler + * weechat::add_timer_handler: add timer handler + */ + +static XS (XS_weechat_add_timer_handler) +{ + int interval; + char *function; + unsigned int integer; + dXSARGS; + + /* make gcc happy */ + (void) cv; + + if (!perl_current_script) + { + perl_plugin->print_server (perl_plugin, + "Perl error: unable to add timer handler, " + "script not initialized"); + XSRETURN_NO; + } + + if (items < 2) + { + perl_plugin->print_server (perl_plugin, + "Perl error: wrong parameters for " + "\"add_timer_handler\" function"); + XSRETURN_NO; + } + + interval = SvIV (ST (0)); + function = SvPV (ST (1), integer); + + perl_plugin->print_server (perl_plugin, + "Perl add timer: interval = %d", interval); + if (perl_plugin->timer_handler_add (perl_plugin, interval, + weechat_perl_handler, function, + (void *)perl_current_script)) + XSRETURN_YES; + + XSRETURN_NO; +} + +/* + * weechat::remove_handler: remove a message/command handler */ static XS (XS_weechat_remove_handler) @@ -560,6 +603,43 @@ static XS (XS_weechat_remove_handler) } /* + * weechat::remove_timer_handler: remove a timer handler + */ + +static XS (XS_weechat_remove_timer_handler) +{ + char *function; + unsigned int integer; + dXSARGS; + + /* make gcc happy */ + (void) cv; + + if (!perl_current_script) + { + perl_plugin->print_server (perl_plugin, + "Perl error: unable to remove timer handler, " + "script not initialized"); + XSRETURN_NO; + } + + if (items < 1) + { + perl_plugin->print_server (perl_plugin, + "Perl error: wrong parameters for " + "\"remove_timer_handler\" function"); + XSRETURN_NO; + } + + function = SvPV (ST (0), integer); + + weechat_script_remove_timer_handler (perl_plugin, perl_current_script, + function); + + XSRETURN_YES; +} + +/* * weechat::get_info: get various infos */ @@ -1084,7 +1164,9 @@ weechat_perl_xs_init (pTHX) newXS ("weechat::command", XS_weechat_command, "weechat"); newXS ("weechat::add_message_handler", XS_weechat_add_message_handler, "weechat"); newXS ("weechat::add_command_handler", XS_weechat_add_command_handler, "weechat"); + newXS ("weechat::add_timer_handler", XS_weechat_add_timer_handler, "weechat"); newXS ("weechat::remove_handler", XS_weechat_remove_handler, "weechat"); + newXS ("weechat::remove_timer_handler", XS_weechat_remove_timer_handler, "weechat"); newXS ("weechat::get_info", XS_weechat_get_info, "weechat"); newXS ("weechat::get_dcc_info", XS_weechat_get_dcc_info, "weechat"); newXS ("weechat::get_config", XS_weechat_get_config, "weechat"); @@ -1370,6 +1452,25 @@ weechat_perl_cmd (t_weechat_plugin *plugin, } if (!handler_found) plugin->print_server (plugin, " (none)"); + + /* list Perl timer handlers */ + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Perl timer handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == HANDLER_TIMER) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " %d seconds => Perl(%s)", + ptr_handler->interval, + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); break; case 1: if (plugin->ascii_strcasecmp (plugin, argv[0], "autoload") == 0) diff --git a/src/plugins/scripts/python/weechat-python.c b/src/plugins/scripts/python/weechat-python.c index e3faa6274..07576f1a0 100644 --- a/src/plugins/scripts/python/weechat-python.c +++ b/src/plugins/scripts/python/weechat-python.c @@ -417,6 +417,46 @@ weechat_python_add_command_handler (PyObject *self, PyObject *args) } /* + * weechat_python_add_timer_handler: add a timer handler + */ + +static PyObject * +weechat_python_add_timer_handler (PyObject *self, PyObject *args) +{ + int interval; + char *function; + + /* make gcc happy */ + (void) self; + + if (!python_current_script) + { + python_plugin->print_server (python_plugin, + "Python error: unable to add timer handler, " + "script not initialized"); + return Py_BuildValue ("i", 0); + } + + interval = 10; + function = NULL; + + if (!PyArg_ParseTuple (args, "is", &interval, &function)) + { + python_plugin->print_server (python_plugin, + "Python error: wrong parameters for " + "\"add_timer_handler\" function"); + return Py_BuildValue ("i", 0); + } + + if (python_plugin->timer_handler_add (python_plugin, interval, + weechat_python_handler, function, + (void *)python_current_script)) + return Py_BuildValue ("i", 1); + + return Py_BuildValue ("i", 0); +} + +/* * weechat_python_remove_handler: remove a handler */ @@ -454,6 +494,42 @@ weechat_python_remove_handler (PyObject *self, PyObject *args) } /* + * weechat_python_remove_timer_handler: remove a timer handler + */ + +static PyObject * +weechat_python_remove_timer_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 timer 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_timer_handler\" function"); + return Py_BuildValue ("i", 0); + } + + weechat_script_remove_timer_handler (python_plugin, python_current_script, + function); + + return Py_BuildValue ("i", 1); +} + +/* * weechat_python_get_info: get various infos */ @@ -1019,7 +1095,9 @@ PyMethodDef weechat_python_funcs[] = { { "command", weechat_python_command, METH_VARARGS, "" }, { "add_message_handler", weechat_python_add_message_handler, METH_VARARGS, "" }, { "add_command_handler", weechat_python_add_command_handler, METH_VARARGS, "" }, + { "add_timer_handler", weechat_python_add_timer_handler, METH_VARARGS, "" }, { "remove_handler", weechat_python_remove_handler, METH_VARARGS, "" }, + { "remove_timer_handler", weechat_python_remove_timer_handler, METH_VARARGS, "" }, { "get_info", weechat_python_get_info, METH_VARARGS, "" }, { "get_dcc_info", weechat_python_get_dcc_info, METH_VARARGS, "" }, { "get_config", weechat_python_get_config, METH_VARARGS, "" }, @@ -1328,6 +1406,25 @@ weechat_python_cmd (t_weechat_plugin *plugin, } if (!handler_found) plugin->print_server (plugin, " (none)"); + + /* list Python timer handlers */ + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Python timer handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == HANDLER_TIMER) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " %d seconds => Python(%s)", + ptr_handler->interval, + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); break; case 1: if (plugin->ascii_strcasecmp (plugin, argv[0], "autoload") == 0) diff --git a/src/plugins/scripts/ruby/weechat-ruby.c b/src/plugins/scripts/ruby/weechat-ruby.c index 397a98ade..11b3f8efd 100644 --- a/src/plugins/scripts/ruby/weechat-ruby.c +++ b/src/plugins/scripts/ruby/weechat-ruby.c @@ -575,6 +575,52 @@ weechat_ruby_add_command_handler (int argc, VALUE *argv, VALUE class) } /* + * weechat_ruby_add_timer_handler: add a timer handler + */ + +static VALUE +weechat_ruby_add_timer_handler (VALUE class, VALUE interval, VALUE function) +{ + int c_interval; + char *c_function; + + /* make gcc happy */ + (void) class; + + if (!ruby_current_script) + { + ruby_plugin->print_server (ruby_plugin, + "Ruby error: unable to add timer handler, " + "script not initialized"); + return INT2FIX (0); + } + + c_interval = 10; + c_function = NULL; + + if (NIL_P (interval) || NIL_P (function)) + { + ruby_plugin->print_server (ruby_plugin, + "Ruby error: wrong parameters for " + "\"add_timer_handler\" function"); + return INT2FIX (0); + } + + Check_Type (interval, T_FIXNUM); + Check_Type (function, T_STRING); + + c_interval = FIX2INT (interval); + c_function = STR2CSTR (function); + + if (ruby_plugin->timer_handler_add (ruby_plugin, c_interval, + weechat_ruby_handler, c_function, + (void *)ruby_current_script)) + return INT2FIX (1); + + return INT2FIX (0); +} + +/* * weechat_ruby_remove_handler: remove a handler */ @@ -618,6 +664,46 @@ weechat_ruby_remove_handler (VALUE class, VALUE command, VALUE function) } /* + * weechat_ruby_remove_timer_handler: remove a timer handler + */ + +static VALUE +weechat_ruby_remove_timer_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 timer 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_timer_handler\" function"); + return INT2FIX (0); + } + + Check_Type (function, T_STRING); + + c_function = STR2CSTR (function); + + weechat_script_remove_timer_handler (ruby_plugin, ruby_current_script, + c_function); + + return INT2FIX (1); +} + +/* * weechat_ruby_get_info: get various infos */ @@ -1478,6 +1564,25 @@ weechat_ruby_cmd (t_weechat_plugin *plugin, } if (!handler_found) plugin->print_server (plugin, " (none)"); + + /* list Ruby timer handlers */ + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Ruby timer handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == HANDLER_TIMER) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " %d seconds => Ruby(%s)", + ptr_handler->interval, + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); break; case 1: if (plugin->ascii_strcasecmp (plugin, argv[0], "autoload") == 0) @@ -1612,7 +1717,9 @@ weechat_plugin_init (t_weechat_plugin *plugin) rb_define_module_function (mWeechat, "command", weechat_ruby_command, -1); rb_define_module_function (mWeechat, "add_message_handler", weechat_ruby_add_message_handler, 2); rb_define_module_function (mWeechat, "add_command_handler", weechat_ruby_add_command_handler, -1); + rb_define_module_function (mWeechat, "add_timer_handler", weechat_ruby_add_timer_handler, 2); rb_define_module_function (mWeechat, "remove_handler", weechat_ruby_remove_handler, 2); + rb_define_module_function (mWeechat, "remove_timer_handler", weechat_ruby_remove_timer_handler, 1); rb_define_module_function (mWeechat, "get_info", weechat_ruby_get_info, -1); rb_define_module_function (mWeechat, "get_dcc_info", weechat_ruby_get_dcc_info, 0); rb_define_module_function (mWeechat, "get_config", weechat_ruby_get_config, 1); diff --git a/src/plugins/scripts/weechat-script.c b/src/plugins/scripts/weechat-script.c index 688ba6fce..636d121e3 100644 --- a/src/plugins/scripts/weechat-script.c +++ b/src/plugins/scripts/weechat-script.c @@ -184,7 +184,7 @@ weechat_script_remove_handler (t_weechat_plugin *plugin, t_plugin_handler *ptr_handler, *next_handler; char *ptr_arg1; - /* search and remove message handlers */ + /* search and remove handlers */ ptr_handler = plugin->handlers; while (ptr_handler) { @@ -209,6 +209,33 @@ weechat_script_remove_handler (t_weechat_plugin *plugin, } /* + * weechat_script_remove_timer_handler: remove a timer handler for a script + */ + +void +weechat_script_remove_timer_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 (((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_get_plugin_config: get a value of a script option * format in file is: plugin.script.option=value */ diff --git a/src/plugins/scripts/weechat-script.h b/src/plugins/scripts/weechat-script.h index 49a44a3eb..d60c03c97 100644 --- a/src/plugins/scripts/weechat-script.h +++ b/src/plugins/scripts/weechat-script.h @@ -51,6 +51,9 @@ extern void weechat_script_remove (t_weechat_plugin *, extern void weechat_script_remove_handler (t_weechat_plugin *, t_plugin_script *, char *, char *); +extern void weechat_script_remove_timer_handler (t_weechat_plugin *, + t_plugin_script *, + char *); extern char *weechat_script_get_plugin_config (t_weechat_plugin *, t_plugin_script *, char *); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 727d57215..5d980675f 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -133,8 +133,9 @@ typedef enum t_handler_type t_handler_type; enum t_handler_type { - HANDLER_MESSAGE, - HANDLER_COMMAND + HANDLER_MESSAGE = 0, /* IRC message handler */ + HANDLER_COMMAND, /* command handler */ + HANDLER_TIMER /* timer handler */ }; typedef struct t_plugin_handler t_plugin_handler; @@ -153,6 +154,10 @@ struct t_plugin_handler char *arguments_description; /* (for /help) args long description */ char *completion_template; /* template for completion */ + /* data for timer handler */ + int interval; /* interval between two calls to fct */ + int remaining; /* seconds remaining before next call */ + /* data common to all handlers */ t_plugin_handler_func *handler; /* pointer to handler */ char *handler_args; /* arguments sent to handler */ @@ -210,6 +215,9 @@ struct t_weechat_plugin char *, t_plugin_handler_func *, char *, void *); + t_plugin_handler *(*timer_handler_add) (t_weechat_plugin *, int, + t_plugin_handler_func *, + char *, void *); void (*handler_remove) (t_weechat_plugin *, t_plugin_handler *); void (*handler_remove_all) (t_weechat_plugin *); @@ -259,6 +267,9 @@ extern t_plugin_handler *weechat_plugin_cmd_handler_add (t_weechat_plugin *, cha char *, t_plugin_handler_func *, char *, void *); +extern t_plugin_handler *weechat_plugin_timer_handler_add (t_weechat_plugin *, int, + 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 *); |