mirror of
https://github.com/weechat/weechat.git
synced 2026-06-26 21:06:38 +02:00
Added timer handler for plugins
This commit is contained in:
@@ -509,6 +509,57 @@ weechat_lua_add_command_handler (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
@@ -554,6 +605,49 @@ weechat_lua_remove_handler (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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},
|
||||
@@ -1538,6 +1634,25 @@ weechat_lua_cmd (t_weechat_plugin *plugin,
|
||||
ptr_handler->handler_args);
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -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)
|
||||
@@ -559,6 +602,43 @@ static XS (XS_weechat_remove_handler)
|
||||
XSRETURN_YES;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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");
|
||||
@@ -1368,6 +1450,25 @@ weechat_perl_cmd (t_weechat_plugin *plugin,
|
||||
ptr_handler->handler_args);
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -416,6 +416,46 @@ weechat_python_add_command_handler (PyObject *self, PyObject *args)
|
||||
return Py_BuildValue ("i", 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
@@ -453,6 +493,42 @@ weechat_python_remove_handler (PyObject *self, PyObject *args)
|
||||
return Py_BuildValue ("i", 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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, "" },
|
||||
@@ -1326,6 +1404,25 @@ weechat_python_cmd (t_weechat_plugin *plugin,
|
||||
ptr_handler->handler_args);
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -574,6 +574,52 @@ weechat_ruby_add_command_handler (int argc, VALUE *argv, VALUE class)
|
||||
return INT2FIX (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
@@ -617,6 +663,46 @@ weechat_ruby_remove_handler (VALUE class, VALUE command, VALUE function)
|
||||
return INT2FIX (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
@@ -1476,6 +1562,25 @@ weechat_ruby_cmd (t_weechat_plugin *plugin,
|
||||
ptr_handler->handler_args);
|
||||
}
|
||||
}
|
||||
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;
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
@@ -208,6 +208,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
|
||||
|
||||
@@ -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 *);
|
||||
|
||||
Reference in New Issue
Block a user