diff --git a/src/plugins/plugins-interface.c b/src/plugins/plugins-interface.c index cb7a0cfdf..9c682324d 100644 --- a/src/plugins/plugins-interface.c +++ b/src/plugins/plugins-interface.c @@ -35,6 +35,7 @@ #include "plugins.h" #include "plugins-config.h" #include "../common/command.h" +#include "../common/log.h" #include "../common/weeconfig.h" #include "../irc/irc.h" #include "../gui/gui.h" @@ -213,6 +214,31 @@ weechat_plugin_infobar_printf (t_weechat_plugin *plugin, int time_displayed, cha gui_infobar_printf (time_displayed, COLOR_WIN_INFOBAR, "%s", buf); } +/* + * weechat_plugin_log: add a message on logs + */ + +void +weechat_plugin_log (t_weechat_plugin *plugin, + char *server, char *channel, char *message, ...) +{ + t_gui_buffer *ptr_buffer; + va_list argptr; + static char buf[8192]; + + if (!plugin || !message) + return; + + ptr_buffer = gui_buffer_search (server, channel); + if (ptr_buffer) + { + va_start (argptr, message); + vsnprintf (buf, sizeof (buf) - 1, message, argptr); + va_end (argptr); + log_write_line (ptr_buffer, buf); + } +} + /* * weechat_plugin_msg_handler_add: add a message handler */ diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index bd09f23bd..db60c69e5 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -625,6 +625,7 @@ plugin_load (char *filename) new_plugin->printf = &weechat_plugin_printf; new_plugin->printf_server = &weechat_plugin_printf_server; new_plugin->infobar_printf = &weechat_plugin_infobar_printf; + new_plugin->log = &weechat_plugin_log; new_plugin->exec_command = &weechat_plugin_exec_command; new_plugin->get_info = &weechat_plugin_get_info; new_plugin->get_dcc_info = &weechat_plugin_get_dcc_info; diff --git a/src/plugins/scripts/lua/weechat-lua.c b/src/plugins/scripts/lua/weechat-lua.c index 7a19dd5db..422908cb3 100644 --- a/src/plugins/scripts/lua/weechat-lua.c +++ b/src/plugins/scripts/lua/weechat-lua.c @@ -272,6 +272,64 @@ weechat_lua_print_infobar (lua_State *L) return 1; } +/* + * weechat_lua_print: log message in server/channel (current or specified ones) + */ + +static int +weechat_lua_log (lua_State *L) +{ + const char *message, *channel_name, *server_name; + int n; + /* make gcc happy */ + (void) L; + + if (!lua_current_script) + { + lua_plugin->printf_server (lua_plugin, + "Lua error: unable to print message, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + message = NULL; + channel_name = NULL; + server_name = NULL; + + n = lua_gettop (lua_current_interpreter); + + switch (n) + { + case 1: + message = lua_tostring (lua_current_interpreter, -1); + break; + case 2: + channel_name = lua_tostring (lua_current_interpreter, -2); + message = lua_tostring (lua_current_interpreter, -1); + break; + case 3: + server_name = lua_tostring (lua_current_interpreter, -3); + channel_name = lua_tostring (lua_current_interpreter, -2); + message = lua_tostring (lua_current_interpreter, -1); + break; + default: + lua_plugin->printf_server (lua_plugin, + "Lua error: wrong parameters for " + "\"log\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + lua_plugin->log (lua_plugin, + (char *) server_name, + (char *) channel_name, + "%s", (char *) message); + + lua_pushnumber (lua_current_interpreter, 1); + return 1; +} + /* * weechat_lua_command: send command to server */ @@ -1213,6 +1271,7 @@ const struct luaL_reg weechat_lua_funcs[] = { { "register", weechat_lua_register}, { "print", weechat_lua_print}, { "print_infobar", weechat_lua_print_infobar}, + { "log", weechat_lua_log}, { "command", weechat_lua_command}, { "add_message_handler", weechat_lua_add_message_handler}, { "add_command_handler", weechat_lua_add_command_handler}, diff --git a/src/plugins/scripts/perl/weechat-perl.c b/src/plugins/scripts/perl/weechat-perl.c index ca20365d2..d50bf1d8f 100644 --- a/src/plugins/scripts/perl/weechat-perl.c +++ b/src/plugins/scripts/perl/weechat-perl.c @@ -336,6 +336,54 @@ static XS (XS_weechat_print_infobar) XSRETURN_YES; } +/* + * weechat::log: log message in server/channel (current or specified ones) + */ + +static XS (XS_weechat_log) +{ + unsigned int integer; + char *message, *channel_name, *server_name; + dXSARGS; + + /* make gcc happy */ + (void) cv; + + if (!perl_current_script) + { + perl_plugin->printf_server (perl_plugin, + "Perl error: unable to print message, " + "script not initialized"); + XSRETURN_NO; + } + + if (items < 1) + { + perl_plugin->printf_server (perl_plugin, + "Perl error: wrong parameters for " + "\"log\" function"); + XSRETURN_NO; + } + + message = SvPV (ST (0), integer); + + channel_name = NULL; + server_name = NULL; + + if (items > 1) + { + channel_name = SvPV (ST (1), integer); + if (items > 2) + server_name = SvPV (ST (2), integer); + } + + perl_plugin->log (perl_plugin, + server_name, channel_name, + "%s", message); + + XSRETURN_YES; +} + /* * weechat::command: send command to server */ @@ -1032,6 +1080,7 @@ weechat_perl_xs_init (pTHX) newXS ("weechat::register", XS_weechat_register, "weechat"); newXS ("weechat::print", XS_weechat_print, "weechat"); newXS ("weechat::print_infobar", XS_weechat_print_infobar, "weechat"); + newXS ("weechat::log", XS_weechat_log, "weechat"); 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"); diff --git a/src/plugins/scripts/python/weechat-python.c b/src/plugins/scripts/python/weechat-python.c index b33a61444..007ed3edc 100644 --- a/src/plugins/scripts/python/weechat-python.c +++ b/src/plugins/scripts/python/weechat-python.c @@ -247,6 +247,45 @@ weechat_python_print_infobar (PyObject *self, PyObject *args) return Py_BuildValue ("i", 1); } +/* + * weechat_python_log: log message in server/channel (current or specified ones) + */ + +static PyObject * +weechat_python_log (PyObject *self, PyObject *args) +{ + char *message, *channel_name, *server_name; + + /* make gcc happy */ + (void) self; + + if (!python_current_script) + { + python_plugin->printf_server (python_plugin, + "Python error: unable to log message, " + "script not initialized"); + return Py_BuildValue ("i", 0); + } + + message = NULL; + channel_name = NULL; + server_name = NULL; + + if (!PyArg_ParseTuple (args, "s|ss", &message, &channel_name, &server_name)) + { + python_plugin->printf_server (python_plugin, + "Python error: wrong parameters for " + "\"log\" function"); + return Py_BuildValue ("i", 0); + } + + python_plugin->log (python_plugin, + server_name, channel_name, + "%s", message); + + return Py_BuildValue ("i", 1); +} + /* * weechat_python_command: send command to server */ @@ -976,6 +1015,7 @@ PyMethodDef weechat_python_funcs[] = { { "register", weechat_python_register, METH_VARARGS, "" }, { "prnt", weechat_python_print, METH_VARARGS, "" }, { "print_infobar", weechat_python_print_infobar, METH_VARARGS, "" }, + { "log", weechat_python_log, METH_VARARGS, "" }, { "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, "" }, diff --git a/src/plugins/scripts/ruby/weechat-ruby.c b/src/plugins/scripts/ruby/weechat-ruby.c index dc90544a5..72b60a564 100644 --- a/src/plugins/scripts/ruby/weechat-ruby.c +++ b/src/plugins/scripts/ruby/weechat-ruby.c @@ -319,6 +319,66 @@ weechat_ruby_print_infobar (VALUE class, VALUE delay, VALUE message) return INT2FIX (1); } +/* + * weechat_ruby_log: log message in server/channel (current or specified ones) + */ + +static VALUE +weechat_ruby_log (int argc, VALUE *argv, VALUE class) +{ + VALUE message, channel_name, server_name; + char *c_message, *c_channel_name, *c_server_name; + + /* make gcc happy */ + (void) class; + + if (!ruby_current_script) + { + ruby_plugin->printf_server (ruby_plugin, + "Ruby error: unable to log message, " + "script not initialized"); + return INT2FIX (0); + } + + message = Qnil; + channel_name = Qnil; + server_name = Qnil; + c_message = NULL; + c_channel_name = NULL; + c_server_name = NULL; + + rb_scan_args (argc, argv, "12", &message, &channel_name, &server_name); + + if (NIL_P (message)) + { + ruby_plugin->printf_server (ruby_plugin, + "Ruby error: wrong parameters for " + "\"log\" function"); + return INT2FIX (0); + } + + Check_Type (message, T_STRING); + c_message = STR2CSTR (message); + + if (!NIL_P (channel_name)) + { + Check_Type (channel_name, T_STRING); + c_channel_name = STR2CSTR (channel_name); + } + + if (!NIL_P (server_name)) + { + Check_Type (server_name, T_STRING); + c_server_name = STR2CSTR (server_name); + } + + ruby_plugin->log (ruby_plugin, + c_server_name, c_channel_name, + "%s", c_message); + + return INT2FIX (1); +} + /* * weechat_ruby_command: send command to server */ @@ -1548,6 +1608,7 @@ weechat_plugin_init (t_weechat_plugin *plugin) rb_define_module_function (mWeechat, "register", weechat_ruby_register, 4); rb_define_module_function (mWeechat, "print", weechat_ruby_print, -1); rb_define_module_function (mWeechat, "print_infobar", weechat_ruby_print_infobar, 2); + rb_define_module_function (mWeechat, "log", weechat_ruby_log, 2); 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); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index cc39abcc3..b4523f5d1 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -201,6 +201,8 @@ struct t_weechat_plugin void (*printf) (t_weechat_plugin *, char *, char *, char *, ...); void (*printf_server) (t_weechat_plugin *, char *, ...); void (*infobar_printf) (t_weechat_plugin *, int, char *, ...); + + void (*log) (t_weechat_plugin *, char *, char *, char *, ...); t_plugin_handler *(*msg_handler_add) (t_weechat_plugin *, char *, t_plugin_handler_func *, @@ -245,6 +247,9 @@ extern void weechat_plugin_printf (t_weechat_plugin *, char *, char *, char *, . extern void weechat_plugin_printf_server (t_weechat_plugin *, char *, ...); extern void weechat_plugin_infobar_printf (t_weechat_plugin *, int, char *, ...); +/* log functions */ +extern void weechat_plugin_log (t_weechat_plugin *, char *, char *, char *, ...); + /* handler functions */ extern t_plugin_handler *weechat_plugin_msg_handler_add (t_weechat_plugin *, char *, t_plugin_handler_func *, diff --git a/weechat/src/plugins/plugins-interface.c b/weechat/src/plugins/plugins-interface.c index cb7a0cfdf..9c682324d 100644 --- a/weechat/src/plugins/plugins-interface.c +++ b/weechat/src/plugins/plugins-interface.c @@ -35,6 +35,7 @@ #include "plugins.h" #include "plugins-config.h" #include "../common/command.h" +#include "../common/log.h" #include "../common/weeconfig.h" #include "../irc/irc.h" #include "../gui/gui.h" @@ -213,6 +214,31 @@ weechat_plugin_infobar_printf (t_weechat_plugin *plugin, int time_displayed, cha gui_infobar_printf (time_displayed, COLOR_WIN_INFOBAR, "%s", buf); } +/* + * weechat_plugin_log: add a message on logs + */ + +void +weechat_plugin_log (t_weechat_plugin *plugin, + char *server, char *channel, char *message, ...) +{ + t_gui_buffer *ptr_buffer; + va_list argptr; + static char buf[8192]; + + if (!plugin || !message) + return; + + ptr_buffer = gui_buffer_search (server, channel); + if (ptr_buffer) + { + va_start (argptr, message); + vsnprintf (buf, sizeof (buf) - 1, message, argptr); + va_end (argptr); + log_write_line (ptr_buffer, buf); + } +} + /* * weechat_plugin_msg_handler_add: add a message handler */ diff --git a/weechat/src/plugins/plugins.c b/weechat/src/plugins/plugins.c index bd09f23bd..db60c69e5 100644 --- a/weechat/src/plugins/plugins.c +++ b/weechat/src/plugins/plugins.c @@ -625,6 +625,7 @@ plugin_load (char *filename) new_plugin->printf = &weechat_plugin_printf; new_plugin->printf_server = &weechat_plugin_printf_server; new_plugin->infobar_printf = &weechat_plugin_infobar_printf; + new_plugin->log = &weechat_plugin_log; new_plugin->exec_command = &weechat_plugin_exec_command; new_plugin->get_info = &weechat_plugin_get_info; new_plugin->get_dcc_info = &weechat_plugin_get_dcc_info; diff --git a/weechat/src/plugins/scripts/lua/weechat-lua.c b/weechat/src/plugins/scripts/lua/weechat-lua.c index 7a19dd5db..422908cb3 100644 --- a/weechat/src/plugins/scripts/lua/weechat-lua.c +++ b/weechat/src/plugins/scripts/lua/weechat-lua.c @@ -272,6 +272,64 @@ weechat_lua_print_infobar (lua_State *L) return 1; } +/* + * weechat_lua_print: log message in server/channel (current or specified ones) + */ + +static int +weechat_lua_log (lua_State *L) +{ + const char *message, *channel_name, *server_name; + int n; + /* make gcc happy */ + (void) L; + + if (!lua_current_script) + { + lua_plugin->printf_server (lua_plugin, + "Lua error: unable to print message, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + message = NULL; + channel_name = NULL; + server_name = NULL; + + n = lua_gettop (lua_current_interpreter); + + switch (n) + { + case 1: + message = lua_tostring (lua_current_interpreter, -1); + break; + case 2: + channel_name = lua_tostring (lua_current_interpreter, -2); + message = lua_tostring (lua_current_interpreter, -1); + break; + case 3: + server_name = lua_tostring (lua_current_interpreter, -3); + channel_name = lua_tostring (lua_current_interpreter, -2); + message = lua_tostring (lua_current_interpreter, -1); + break; + default: + lua_plugin->printf_server (lua_plugin, + "Lua error: wrong parameters for " + "\"log\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + lua_plugin->log (lua_plugin, + (char *) server_name, + (char *) channel_name, + "%s", (char *) message); + + lua_pushnumber (lua_current_interpreter, 1); + return 1; +} + /* * weechat_lua_command: send command to server */ @@ -1213,6 +1271,7 @@ const struct luaL_reg weechat_lua_funcs[] = { { "register", weechat_lua_register}, { "print", weechat_lua_print}, { "print_infobar", weechat_lua_print_infobar}, + { "log", weechat_lua_log}, { "command", weechat_lua_command}, { "add_message_handler", weechat_lua_add_message_handler}, { "add_command_handler", weechat_lua_add_command_handler}, diff --git a/weechat/src/plugins/scripts/perl/weechat-perl.c b/weechat/src/plugins/scripts/perl/weechat-perl.c index ca20365d2..d50bf1d8f 100644 --- a/weechat/src/plugins/scripts/perl/weechat-perl.c +++ b/weechat/src/plugins/scripts/perl/weechat-perl.c @@ -336,6 +336,54 @@ static XS (XS_weechat_print_infobar) XSRETURN_YES; } +/* + * weechat::log: log message in server/channel (current or specified ones) + */ + +static XS (XS_weechat_log) +{ + unsigned int integer; + char *message, *channel_name, *server_name; + dXSARGS; + + /* make gcc happy */ + (void) cv; + + if (!perl_current_script) + { + perl_plugin->printf_server (perl_plugin, + "Perl error: unable to print message, " + "script not initialized"); + XSRETURN_NO; + } + + if (items < 1) + { + perl_plugin->printf_server (perl_plugin, + "Perl error: wrong parameters for " + "\"log\" function"); + XSRETURN_NO; + } + + message = SvPV (ST (0), integer); + + channel_name = NULL; + server_name = NULL; + + if (items > 1) + { + channel_name = SvPV (ST (1), integer); + if (items > 2) + server_name = SvPV (ST (2), integer); + } + + perl_plugin->log (perl_plugin, + server_name, channel_name, + "%s", message); + + XSRETURN_YES; +} + /* * weechat::command: send command to server */ @@ -1032,6 +1080,7 @@ weechat_perl_xs_init (pTHX) newXS ("weechat::register", XS_weechat_register, "weechat"); newXS ("weechat::print", XS_weechat_print, "weechat"); newXS ("weechat::print_infobar", XS_weechat_print_infobar, "weechat"); + newXS ("weechat::log", XS_weechat_log, "weechat"); 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"); diff --git a/weechat/src/plugins/scripts/python/weechat-python.c b/weechat/src/plugins/scripts/python/weechat-python.c index b33a61444..007ed3edc 100644 --- a/weechat/src/plugins/scripts/python/weechat-python.c +++ b/weechat/src/plugins/scripts/python/weechat-python.c @@ -247,6 +247,45 @@ weechat_python_print_infobar (PyObject *self, PyObject *args) return Py_BuildValue ("i", 1); } +/* + * weechat_python_log: log message in server/channel (current or specified ones) + */ + +static PyObject * +weechat_python_log (PyObject *self, PyObject *args) +{ + char *message, *channel_name, *server_name; + + /* make gcc happy */ + (void) self; + + if (!python_current_script) + { + python_plugin->printf_server (python_plugin, + "Python error: unable to log message, " + "script not initialized"); + return Py_BuildValue ("i", 0); + } + + message = NULL; + channel_name = NULL; + server_name = NULL; + + if (!PyArg_ParseTuple (args, "s|ss", &message, &channel_name, &server_name)) + { + python_plugin->printf_server (python_plugin, + "Python error: wrong parameters for " + "\"log\" function"); + return Py_BuildValue ("i", 0); + } + + python_plugin->log (python_plugin, + server_name, channel_name, + "%s", message); + + return Py_BuildValue ("i", 1); +} + /* * weechat_python_command: send command to server */ @@ -976,6 +1015,7 @@ PyMethodDef weechat_python_funcs[] = { { "register", weechat_python_register, METH_VARARGS, "" }, { "prnt", weechat_python_print, METH_VARARGS, "" }, { "print_infobar", weechat_python_print_infobar, METH_VARARGS, "" }, + { "log", weechat_python_log, METH_VARARGS, "" }, { "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, "" }, diff --git a/weechat/src/plugins/scripts/ruby/weechat-ruby.c b/weechat/src/plugins/scripts/ruby/weechat-ruby.c index dc90544a5..72b60a564 100644 --- a/weechat/src/plugins/scripts/ruby/weechat-ruby.c +++ b/weechat/src/plugins/scripts/ruby/weechat-ruby.c @@ -319,6 +319,66 @@ weechat_ruby_print_infobar (VALUE class, VALUE delay, VALUE message) return INT2FIX (1); } +/* + * weechat_ruby_log: log message in server/channel (current or specified ones) + */ + +static VALUE +weechat_ruby_log (int argc, VALUE *argv, VALUE class) +{ + VALUE message, channel_name, server_name; + char *c_message, *c_channel_name, *c_server_name; + + /* make gcc happy */ + (void) class; + + if (!ruby_current_script) + { + ruby_plugin->printf_server (ruby_plugin, + "Ruby error: unable to log message, " + "script not initialized"); + return INT2FIX (0); + } + + message = Qnil; + channel_name = Qnil; + server_name = Qnil; + c_message = NULL; + c_channel_name = NULL; + c_server_name = NULL; + + rb_scan_args (argc, argv, "12", &message, &channel_name, &server_name); + + if (NIL_P (message)) + { + ruby_plugin->printf_server (ruby_plugin, + "Ruby error: wrong parameters for " + "\"log\" function"); + return INT2FIX (0); + } + + Check_Type (message, T_STRING); + c_message = STR2CSTR (message); + + if (!NIL_P (channel_name)) + { + Check_Type (channel_name, T_STRING); + c_channel_name = STR2CSTR (channel_name); + } + + if (!NIL_P (server_name)) + { + Check_Type (server_name, T_STRING); + c_server_name = STR2CSTR (server_name); + } + + ruby_plugin->log (ruby_plugin, + c_server_name, c_channel_name, + "%s", c_message); + + return INT2FIX (1); +} + /* * weechat_ruby_command: send command to server */ @@ -1548,6 +1608,7 @@ weechat_plugin_init (t_weechat_plugin *plugin) rb_define_module_function (mWeechat, "register", weechat_ruby_register, 4); rb_define_module_function (mWeechat, "print", weechat_ruby_print, -1); rb_define_module_function (mWeechat, "print_infobar", weechat_ruby_print_infobar, 2); + rb_define_module_function (mWeechat, "log", weechat_ruby_log, 2); 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); diff --git a/weechat/src/plugins/weechat-plugin.h b/weechat/src/plugins/weechat-plugin.h index cc39abcc3..b4523f5d1 100644 --- a/weechat/src/plugins/weechat-plugin.h +++ b/weechat/src/plugins/weechat-plugin.h @@ -201,6 +201,8 @@ struct t_weechat_plugin void (*printf) (t_weechat_plugin *, char *, char *, char *, ...); void (*printf_server) (t_weechat_plugin *, char *, ...); void (*infobar_printf) (t_weechat_plugin *, int, char *, ...); + + void (*log) (t_weechat_plugin *, char *, char *, char *, ...); t_plugin_handler *(*msg_handler_add) (t_weechat_plugin *, char *, t_plugin_handler_func *, @@ -245,6 +247,9 @@ extern void weechat_plugin_printf (t_weechat_plugin *, char *, char *, char *, . extern void weechat_plugin_printf_server (t_weechat_plugin *, char *, ...); extern void weechat_plugin_infobar_printf (t_weechat_plugin *, int, char *, ...); +/* log functions */ +extern void weechat_plugin_log (t_weechat_plugin *, char *, char *, char *, ...); + /* handler functions */ extern t_plugin_handler *weechat_plugin_msg_handler_add (t_weechat_plugin *, char *, t_plugin_handler_func *,