From 68f723888798a792480f4113663cc4e33a4b2d64 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sun, 13 Jan 2008 23:29:43 +0100 Subject: [PATCH] Migration of Lua plugin to new API --- src/plugins/scripts/lua/Makefile.am | 5 +- src/plugins/scripts/lua/lua.c | 2734 --------------- src/plugins/scripts/lua/weechat-lua-api.c | 3062 +++++++++++++++++ src/plugins/scripts/lua/weechat-lua-api.h | 25 + src/plugins/scripts/lua/weechat-lua.c | 598 ++++ src/plugins/scripts/lua/weechat-lua.h | 35 + src/plugins/scripts/perl/weechat-perl-api.c | 12 +- src/plugins/scripts/perl/weechat-perl.c | 3 +- .../scripts/python/weechat-python-api.c | 16 +- src/plugins/scripts/python/weechat-python.c | 3 +- src/plugins/scripts/ruby/weechat-ruby-api.c | 20 +- src/plugins/scripts/ruby/weechat-ruby.c | 3 +- 12 files changed, 3754 insertions(+), 2762 deletions(-) delete mode 100644 src/plugins/scripts/lua/lua.c create mode 100644 src/plugins/scripts/lua/weechat-lua-api.c create mode 100644 src/plugins/scripts/lua/weechat-lua-api.h create mode 100644 src/plugins/scripts/lua/weechat-lua.c create mode 100644 src/plugins/scripts/lua/weechat-lua.h diff --git a/src/plugins/scripts/lua/Makefile.am b/src/plugins/scripts/lua/Makefile.am index 178a22c24..2cd2afd69 100644 --- a/src/plugins/scripts/lua/Makefile.am +++ b/src/plugins/scripts/lua/Makefile.am @@ -20,6 +20,9 @@ libdir = ${weechat_libdir}/plugins lib_LTLIBRARIES = lua.la -lua_la_SOURCES = lua.c +lua_la_SOURCES = weechat-lua.c \ + weechat-lua.h \ + weechat-lua-api.c \ + weechat-lua-api.h lua_la_LDFLAGS = -module lua_la_LIBADD = ../lib_weechat_plugins_scripts.la $(LUA_LFLAGS) diff --git a/src/plugins/scripts/lua/lua.c b/src/plugins/scripts/lua/lua.c deleted file mode 100644 index 36357ad02..000000000 --- a/src/plugins/scripts/lua/lua.c +++ /dev/null @@ -1,2734 +0,0 @@ -/* - * Copyright (c) 2003-2008 by FlashCode - * See README for License detail, AUTHORS for developers list. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* weechat-lua.c: Lua plugin support for WeeChat */ - -#undef _ - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "../../weechat-plugin.h" -#include "../weechat-script.h" - - -WEECHAT_PLUGIN_NAME("lua"); -WEECHAT_PLUGIN_DESCRIPTION("Lua plugin for WeeChat"); -WEECHAT_PLUGIN_AUTHOR("FlashCode "); -WEECHAT_PLUGIN_VERSION("0.1"); -WEECHAT_PLUGIN_LICENSE("GPL"); - -t_weechat_plugin *lua_plugin; - -t_plugin_script *lua_scripts = NULL; -t_plugin_script *lua_current_script = NULL; -char *lua_current_script_filename = NULL; -lua_State *lua_current_interpreter = NULL; - - -/* - * weechat_lua_exec: execute a Lua script - */ - -void * -weechat_lua_exec (t_weechat_plugin *plugin, - t_plugin_script *script, - int ret_type, - char *function, char *arg1, char *arg2, char *arg3) -{ - void *ret_value; - int *ret_i; - - lua_current_interpreter = script->interpreter; - - lua_getglobal (lua_current_interpreter, function); - lua_current_script = script; - - if (arg1) - { - lua_pushstring (lua_current_interpreter, (arg1) ? arg1 : ""); - if (arg2) - { - lua_pushstring (lua_current_interpreter, (arg2) ? arg2 : ""); - if (arg3) - lua_pushstring (lua_current_interpreter, (arg3) ? arg3 : ""); - } - } - - if (lua_pcall (lua_current_interpreter, - (arg1) ? ((arg2) ? ((arg3) ? 3 : 2) : 1) : 0, 1, 0) != 0) - { - plugin->print_server (plugin, - "Lua error: unable to run function \"%s\"", - function); - plugin->print_server (plugin, - "Lua error: %s", - lua_tostring (lua_current_interpreter, -1)); - return NULL; - } - - if (ret_type == SCRIPT_EXEC_STRING) - ret_value = strdup ((char *) lua_tostring (lua_current_interpreter, -1)); - else if (ret_type == SCRIPT_EXEC_INT) - { - ret_i = (int *)malloc (sizeof(int)); - if (ret_i) - *ret_i = lua_tonumber (lua_current_interpreter, -1); - ret_value = ret_i; - } - else - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for function \"%s\"", - function); - return NULL; - } - - return ret_value; -} - -/* - * weechat_lua_cmd_msg_handler: general command/message handler for Lua - */ - -int -weechat_lua_cmd_msg_handler (t_weechat_plugin *plugin, - int argc, char **argv, - char *handler_args, void *handler_pointer) -{ - int *r; - int ret; - - if (argc >= 3) - { - r = (int *) weechat_lua_exec (plugin, (t_plugin_script *)handler_pointer, - SCRIPT_EXEC_INT, - handler_args, argv[0], argv[2], NULL); - if (r == NULL) - ret = PLUGIN_RC_KO; - else - { - ret = *r; - free (r); - } - return ret; - } - else - return PLUGIN_RC_KO; -} - -/* - * weechat_lua_timer_handler: general timer handler for Lua - */ - -int -weechat_lua_timer_handler (t_weechat_plugin *plugin, - int argc, char **argv, - char *handler_args, void *handler_pointer) -{ - /* make C compiler happy */ - (void) argc; - (void) argv; - int *r; - int ret; - - r = (int *) weechat_lua_exec (plugin, (t_plugin_script *)handler_pointer, - SCRIPT_EXEC_INT, - handler_args, NULL, NULL, NULL); - if (r == NULL) - ret = PLUGIN_RC_KO; - else - { - ret = *r; - free (r); - } - return ret; -} - -/* - * weechat_lua_keyboard_handler: general keyboard handler for Lua - */ - -int -weechat_lua_keyboard_handler (t_weechat_plugin *plugin, - int argc, char **argv, - char *handler_args, void *handler_pointer) -{ - int *r; - int ret; - - if (argc >= 3) - { - r = (int *) weechat_lua_exec (plugin, (t_plugin_script *)handler_pointer, - SCRIPT_EXEC_INT, - handler_args, argv[0], argv[1], argv[2]); - if (r == NULL) - ret = PLUGIN_RC_KO; - else - { - ret = *r; - free (r); - } - return ret; - } - else - return PLUGIN_RC_KO; -} - -/* - * 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], - (argc >= 2) ? argv[1] : NULL, - (argc >= 3) ? argv[2] : 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 - */ - -char * -weechat_lua_modifier (t_weechat_plugin *plugin, - int argc, char **argv, - char *modifier_args, void *modifier_pointer) -{ - if (argc >= 2) - return (char *) weechat_lua_exec (plugin, (t_plugin_script *)modifier_pointer, - SCRIPT_EXEC_STRING, - modifier_args, argv[0], argv[1], NULL); - else - return NULL; -} - -/* - * weechat_lua_register: startup function for all WeeChat Lua scripts - */ - -static int -weechat_lua_register (lua_State *L) -{ - const char *name, *version, *shutdown_func, *description, *charset; - int n; - - /* make C compiler happy */ - (void) L; - - lua_current_script = NULL; - - name = NULL; - version = NULL; - shutdown_func = NULL; - description = NULL; - charset = NULL; - - n = lua_gettop (lua_current_interpreter); - - if ((n < 4) || (n > 5)) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"register\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - switch (n) - { - case 4: - name = lua_tostring (lua_current_interpreter, -4); - version = lua_tostring (lua_current_interpreter, -3); - shutdown_func = lua_tostring (lua_current_interpreter, -2); - description = lua_tostring (lua_current_interpreter, -1); - break; - case 5: - name = lua_tostring (lua_current_interpreter, -5); - version = lua_tostring (lua_current_interpreter, -4); - shutdown_func = lua_tostring (lua_current_interpreter, -3); - description = lua_tostring (lua_current_interpreter, -2); - charset = lua_tostring (lua_current_interpreter, -1); - break; - } - - if (weechat_script_search (lua_plugin, &lua_scripts, (char *) name)) - { - /* error: another scripts already exists with this name! */ - lua_plugin->print_server (lua_plugin, - "Lua error: unable to register " - "\"%s\" script (another script " - "already exists with this name)", - name); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - /* register script */ - lua_current_script = weechat_script_add (lua_plugin, - &lua_scripts, - (lua_current_script_filename) ? - lua_current_script_filename : "", - (char *) name, - (char *) version, - (char *) shutdown_func, - (char *) description, - (char *) charset); - if (lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua: registered script \"%s\", " - "version %s (%s)", - name, version, description); - } - else - { - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_set_charset: set script charset - */ - -static int -weechat_lua_set_charset (lua_State *L) -{ - const char *charset; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to set charset, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - charset = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 1) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"set_charset\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - charset = lua_tostring (lua_current_interpreter, -1); - - weechat_script_set_charset (lua_plugin, - lua_current_script, - (char *) charset); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_print: print message into a buffer (current or specified one) - */ - -static int -weechat_lua_print (lua_State *L) -{ - const char *message, *channel_name, *server_name; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_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->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"print\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - lua_plugin->print (lua_plugin, - (char *) server_name, - (char *) channel_name, - "%s", (char *) message); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_print_server: print message into a buffer server - */ - -static int -weechat_lua_print_server (lua_State *L) -{ - const char *message; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to print message, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - message = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n == 1) - message = lua_tostring (lua_current_interpreter, -1); - else - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"print_server\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - lua_plugin->print_server (lua_plugin, "%s", (char *) message); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_print_infobar: print message to infobar - */ - -static int -weechat_lua_print_infobar (lua_State *L) -{ - const char *message; - int delay, n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to print infobar message, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - delay = 1; - message = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"print_infobar\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - delay = lua_tonumber (lua_current_interpreter, -2); - message = lua_tostring (lua_current_interpreter, -1); - - lua_plugin->print_infobar (lua_plugin, delay, "%s", (char *) message); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_remove_infobar: remove message(s) in infobar - */ - -static int -weechat_lua_remove_infobar (lua_State *L) -{ - int n, how_many; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to remove infobar message(s), " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - how_many = 0; - - n = lua_gettop (lua_current_interpreter); - - if (n == 1) - how_many = lua_tonumber (lua_current_interpreter, -1); - - lua_plugin->infobar_remove (lua_plugin, how_many); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_log: 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 C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_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->print_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 - */ - -static int -weechat_lua_command (lua_State *L) -{ - const char *command, *channel_name, *server_name; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to run command, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - command = NULL; - channel_name = NULL; - server_name = NULL; - - n = lua_gettop (lua_current_interpreter); - - switch (n) - { - case 1: - command = lua_tostring (lua_current_interpreter, -1); - break; - case 2: - channel_name = lua_tostring (lua_current_interpreter, -2); - command = 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); - command = lua_tostring (lua_current_interpreter, -1); - break; - default: - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"command\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - lua_plugin->exec_command (lua_plugin, - (char *) server_name, - (char *) channel_name, - (char *) command); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_add_message_handler: add handler for messages - */ - -static int -weechat_lua_add_message_handler (lua_State *L) -{ - const char *irc_command, *function; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to add message handler, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - irc_command = NULL; - function = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"add_message_handler\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - irc_command = lua_tostring (lua_current_interpreter, -2); - function = lua_tostring (lua_current_interpreter, -1); - - if (!lua_plugin->msg_handler_add (lua_plugin, (char *) irc_command, - weechat_lua_cmd_msg_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_add_command_handler: define/redefines commands - */ - -static int -weechat_lua_add_command_handler (lua_State *L) -{ - const char *command, *function, *description, *arguments, *arguments_description; - const char *completion_template; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to add command handler, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - command = NULL; - function = NULL; - description = NULL; - arguments = NULL; - arguments_description = NULL; - completion_template = NULL; - - n = lua_gettop (lua_current_interpreter); - - switch (n) - { - case 2: - command = lua_tostring (lua_current_interpreter, -2); - function = lua_tostring (lua_current_interpreter, -1); - break; - case 3: - command = lua_tostring (lua_current_interpreter, -3); - function = lua_tostring (lua_current_interpreter, -2); - description = lua_tostring (lua_current_interpreter, -1); - break; - case 4: - command = lua_tostring (lua_current_interpreter, -4); - function = lua_tostring (lua_current_interpreter, -3); - description = lua_tostring (lua_current_interpreter, -2); - arguments = lua_tostring (lua_current_interpreter, -1); - break; - case 5: - command = lua_tostring (lua_current_interpreter, -5); - function = lua_tostring (lua_current_interpreter, -4); - description = lua_tostring (lua_current_interpreter, -3); - arguments = lua_tostring (lua_current_interpreter, -2); - arguments_description = lua_tostring (lua_current_interpreter, -1); - break; - case 6: - command = lua_tostring (lua_current_interpreter, -6); - function = lua_tostring (lua_current_interpreter, -5); - description = lua_tostring (lua_current_interpreter, -4); - arguments = lua_tostring (lua_current_interpreter, -3); - arguments_description = lua_tostring (lua_current_interpreter, -2); - completion_template = lua_tostring (lua_current_interpreter, -1); - break; - default: - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"add_command_handler\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - if (!lua_plugin->cmd_handler_add (lua_plugin, - (char *) command, - (char *) description, - (char *) arguments, - (char *) arguments_description, - (char *) completion_template, - weechat_lua_cmd_msg_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_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 C compiler 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_timer_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_add_keyboard_handler: add a keyboard handler - */ - -static int -weechat_lua_add_keyboard_handler (lua_State *L) -{ - const char *function; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to add keyboard 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 " - "\"add_keyboard_handler\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - function = lua_tostring (lua_current_interpreter, -1); - - if (!lua_plugin->keyboard_handler_add (lua_plugin, - weechat_lua_keyboard_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_add_event_handler: add handler for events - */ - -static int -weechat_lua_add_event_handler (lua_State *L) -{ - const char *event, *function; - int n; - - /* make C compiler 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 - */ - -static int -weechat_lua_remove_handler (lua_State *L) -{ - const char *command, *function; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to remove handler, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - command = NULL; - function = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"remove_handler\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - command = lua_tostring (lua_current_interpreter, -2); - function = lua_tostring (lua_current_interpreter, -1); - - weechat_script_remove_handler (lua_plugin, lua_current_script, - (char *) command, (char *) function); - - lua_pushnumber (lua_current_interpreter, 1); - 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 C compiler 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_remove_keyboard_handler: remove a keyboard handler - */ - -static int -weechat_lua_remove_keyboard_handler (lua_State *L) -{ - const char *function; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to remove keyboard 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_keyboard_handler\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - function = lua_tostring (lua_current_interpreter, -1); - - weechat_script_remove_keyboard_handler (lua_plugin, lua_current_script, - (char *) function); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * 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 C compiler 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 - */ - -static int -weechat_lua_add_modifier (lua_State *L) -{ - const char *type, *command, *function; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to add modifier, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - type = NULL; - command = NULL; - function = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 3) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"add_modifier\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - type = lua_tostring (lua_current_interpreter, -3); - command = lua_tostring (lua_current_interpreter, -2); - function = lua_tostring (lua_current_interpreter, -1); - - if (!lua_plugin->modifier_add (lua_plugin, (char *)type, (char *)command, - weechat_lua_modifier, - (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_modifier: remove a modifier - */ - -static int -weechat_lua_remove_modifier (lua_State *L) -{ - const char *type, *command, *function; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to remove modifier, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - type = NULL; - command = NULL; - function = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 3) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"remove_modifier\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - type = lua_tostring (lua_current_interpreter, -3); - command = lua_tostring (lua_current_interpreter, -2); - function = lua_tostring (lua_current_interpreter, -1); - - weechat_script_remove_modifier (lua_plugin, lua_current_script, - (char *)type, (char *)command, - (char *)function); - - lua_pushnumber (lua_current_interpreter, 1); - return 1; -} - -/* - * weechat_lua_get_info: get various infos - */ - -static int -weechat_lua_get_info (lua_State *L) -{ - const char *arg, *server_name; - char *info; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get info, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - arg = NULL; - server_name = NULL; - - n = lua_gettop (lua_current_interpreter); - - switch (n) - { - case 1: - arg = lua_tostring (lua_current_interpreter, -1); - break; - case 2: - arg = lua_tostring (lua_current_interpreter, -2); - server_name = lua_tostring (lua_current_interpreter, -1); - break; - default: - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_info\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - info = lua_plugin->get_info (lua_plugin, (char *) arg, (char *) server_name); - if (info) - lua_pushstring (lua_current_interpreter, info); - else - lua_pushstring (lua_current_interpreter, ""); - - return 1; -} - -/* - * weechat_lua_get_dcc_info: get infos about DCC - */ - -static int -weechat_lua_get_dcc_info (lua_State *L) -{ - t_plugin_dcc_info *dcc_info, *ptr_dcc; - char timebuffer1[64]; - char timebuffer2[64]; - struct in_addr in; - int i; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get DCC info, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - dcc_info = lua_plugin->get_dcc_info (lua_plugin); - if (!dcc_info) - { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - for (i = 0, ptr_dcc = dcc_info; ptr_dcc; ptr_dcc = ptr_dcc->next_dcc, i++) - { - strftime(timebuffer1, sizeof(timebuffer1), "%F %T", - localtime(&ptr_dcc->start_time)); - strftime(timebuffer2, sizeof(timebuffer2), "%F %T", - localtime(&ptr_dcc->start_transfer)); - in.s_addr = htonl(ptr_dcc->addr); - - lua_pushnumber (lua_current_interpreter, i); - lua_newtable (lua_current_interpreter); - - lua_pushstring (lua_current_interpreter, "server"); - lua_pushstring (lua_current_interpreter, ptr_dcc->server); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "channel"); - lua_pushstring (lua_current_interpreter, ptr_dcc->channel); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "type"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->type); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "status"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->status); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "start_time"); - lua_pushstring (lua_current_interpreter, timebuffer1); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "start_transfer"); - lua_pushstring (lua_current_interpreter, timebuffer2); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "address"); - lua_pushstring (lua_current_interpreter, inet_ntoa(in)); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "port"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->port); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick"); - lua_pushstring (lua_current_interpreter, ptr_dcc->nick); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "remote_file"); - lua_pushstring (lua_current_interpreter, ptr_dcc->filename); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "local_file"); - lua_pushstring (lua_current_interpreter, ptr_dcc->local_filename); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "filename_suffix"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->filename_suffix); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "size"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->size); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "pos"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->pos); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "start_resume"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->start_resume); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "cps"); - lua_pushnumber (lua_current_interpreter, ptr_dcc->bytes_per_sec); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - } - - lua_plugin->free_dcc_info (lua_plugin, dcc_info); - - return 1; -} - -/* - * weechat_lua_get_config: get value of a WeeChat config option - */ - -static int -weechat_lua_get_config (lua_State *L) -{ - const char *option; - char *return_value; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get config option, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 1) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_config\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = lua_tostring (lua_current_interpreter, -1); - - return_value = lua_plugin->get_config (lua_plugin, (char *) option); - if (return_value) - lua_pushstring (lua_current_interpreter, return_value); - else - lua_pushstring (lua_current_interpreter, ""); - - return 1; -} - -/* - * weechat_lua_set_config: set value of a WeeChat config option - */ - -static int -weechat_lua_set_config (lua_State *L) -{ - const char *option, *value; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to set config option, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = NULL; - value = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"set_config\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = lua_tostring (lua_current_interpreter, -2); - value = lua_tostring (lua_current_interpreter, -1); - - if (lua_plugin->set_config (lua_plugin, (char *) option, (char *) value)) - lua_pushnumber (lua_current_interpreter, 1); - else - lua_pushnumber (lua_current_interpreter, 0); - - return 1; -} - -/* - * weechat_lua_get_plugin_config: get value of a plugin config option - */ - -static int -weechat_lua_get_plugin_config (lua_State *L) -{ - const char *option; - char *return_value; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get plugin config option, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 1) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_plugin_config\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = lua_tostring (lua_current_interpreter, -1); - - return_value = weechat_script_get_plugin_config (lua_plugin, - lua_current_script, - (char *) option); - if (return_value) - lua_pushstring (lua_current_interpreter, return_value); - else - lua_pushstring (lua_current_interpreter, ""); - - return 1; -} - -/* - * weechat_lua_set_plugin_config: set value of a plugin config option - */ - -static int -weechat_lua_set_plugin_config (lua_State *L) -{ - const char *option, *value; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to set plugin config option, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = NULL; - value = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"set_plugin_config\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - option = lua_tostring (lua_current_interpreter, -2); - value = lua_tostring (lua_current_interpreter, -1); - - if (weechat_script_set_plugin_config (lua_plugin, - lua_current_script, - (char *) option, (char *) value)) - lua_pushnumber (lua_current_interpreter, 1); - else - lua_pushnumber (lua_current_interpreter, 0); - - return 1; -} - -/* - * weechat_lua_get_server_info: get infos about servers - */ - -static int -weechat_lua_get_server_info (lua_State *L) -{ - t_plugin_server_info *server_info, *ptr_server; - char timebuffer[64]; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get server infos, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - server_info = lua_plugin->get_server_info (lua_plugin); - if (!server_info) { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - for (ptr_server = server_info; ptr_server; ptr_server = ptr_server->next_server) - { - strftime(timebuffer, sizeof(timebuffer), "%F %T", - localtime(&ptr_server->away_time)); - - lua_pushstring (lua_current_interpreter, ptr_server->name); - lua_newtable (lua_current_interpreter); - - lua_pushstring (lua_current_interpreter, "autoconnect"); - lua_pushnumber (lua_current_interpreter, ptr_server->autoconnect); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "autoreconnect"); - lua_pushnumber (lua_current_interpreter, ptr_server->autoreconnect); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "autoreconnect_delay"); - lua_pushnumber (lua_current_interpreter, ptr_server->autoreconnect_delay); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "temp_server"); - lua_pushnumber (lua_current_interpreter, ptr_server->temp_server); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "address"); - lua_pushstring (lua_current_interpreter, ptr_server->address); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "port"); - lua_pushnumber (lua_current_interpreter, ptr_server->port); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "ipv6"); - lua_pushnumber (lua_current_interpreter, ptr_server->ipv6); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "ssl"); - lua_pushnumber (lua_current_interpreter, ptr_server->ssl); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "password"); - lua_pushstring (lua_current_interpreter, ptr_server->password); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick1"); - lua_pushstring (lua_current_interpreter, ptr_server->nick1); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick2"); - lua_pushstring (lua_current_interpreter, ptr_server->nick2); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick3"); - lua_pushstring (lua_current_interpreter, ptr_server->nick3); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "username"); - lua_pushstring (lua_current_interpreter, ptr_server->username); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "realname"); - lua_pushstring (lua_current_interpreter, ptr_server->realname); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "command"); - lua_pushstring (lua_current_interpreter, ptr_server->command); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "command_delay"); - lua_pushnumber (lua_current_interpreter, ptr_server->command_delay); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "autojoin"); - lua_pushstring (lua_current_interpreter, ptr_server->autojoin); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "autorejoin"); - lua_pushnumber (lua_current_interpreter, ptr_server->autorejoin); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "notify_levels"); - lua_pushstring (lua_current_interpreter, ptr_server->notify_levels); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "is_connected"); - lua_pushnumber (lua_current_interpreter, ptr_server->is_connected); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "ssl_connected"); - lua_pushnumber (lua_current_interpreter, ptr_server->ssl_connected); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick"); - lua_pushstring (lua_current_interpreter, ptr_server->nick); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick_modes"); - lua_pushstring (lua_current_interpreter, ptr_server->nick_modes); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "away_time"); - lua_pushstring (lua_current_interpreter, timebuffer); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "lag"); - lua_pushnumber (lua_current_interpreter, ptr_server->lag); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - } - - lua_plugin->free_server_info(lua_plugin, server_info); - - return 1; -} - -/* - * weechat_lua_get_channel_info: get infos about channels - */ - -static int -weechat_lua_get_channel_info (lua_State *L) -{ - t_plugin_channel_info *channel_info, *ptr_channel; - const char *server; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get channel infos, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - server = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 1) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_channel_info\" function"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - server = lua_tostring (lua_current_interpreter, -1); - - channel_info = lua_plugin->get_channel_info (lua_plugin, (char *) server); - if (!channel_info) - { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - for (ptr_channel = channel_info; ptr_channel; ptr_channel = ptr_channel->next_channel) - { - lua_pushstring (lua_current_interpreter, ptr_channel->name); - lua_newtable (lua_current_interpreter); - - lua_pushstring (lua_current_interpreter, "type"); - lua_pushnumber (lua_current_interpreter, ptr_channel->type); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "topic"); - lua_pushstring (lua_current_interpreter, ptr_channel->topic); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "modes"); - lua_pushstring (lua_current_interpreter, ptr_channel->modes); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "limit"); - lua_pushnumber (lua_current_interpreter, ptr_channel->limit); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "key"); - lua_pushstring (lua_current_interpreter, ptr_channel->key); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nicks_count"); - lua_pushnumber (lua_current_interpreter, ptr_channel->nicks_count); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - } - - lua_plugin->free_channel_info(lua_plugin, channel_info); - - return 1; -} - -/* - * weechat_lua_get_nick_info: get infos about nicks - */ - -static int -weechat_lua_get_nick_info (lua_State *L) -{ - t_plugin_nick_info *nick_info, *ptr_nick; - const char *server, *channel; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get nick infos, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - server = NULL; - channel = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_nick_info\" function"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - server = lua_tostring (lua_current_interpreter, -2); - channel = lua_tostring (lua_current_interpreter, -1); - - nick_info = lua_plugin->get_nick_info (lua_plugin, (char *) server, (char *) channel); - if (!nick_info) - { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - for(ptr_nick = nick_info; ptr_nick; ptr_nick = ptr_nick->next_nick) - { - lua_pushstring (lua_current_interpreter, ptr_nick->nick); - lua_newtable (lua_current_interpreter); - - lua_pushstring (lua_current_interpreter, "flags"); - lua_pushnumber (lua_current_interpreter, ptr_nick->flags); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "host"); - lua_pushstring (lua_current_interpreter, - ptr_nick->host ? ptr_nick->host : ""); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - } - - lua_plugin->free_nick_info(lua_plugin, nick_info); - - return 1; -} - -/* - * weechat_lua_get_irc_color: - * get the numeric value which identify an irc color by its name - */ - -static int -weechat_lua_get_irc_color (lua_State *L) -{ - const char *color; - int n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get irc color, " - "script not initialized"); - lua_pushnumber (lua_current_interpreter, -1); - return 1; - } - - color = NULL; - - n = lua_gettop (lua_current_interpreter); - - if (n != 1) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_irc_color\" function"); - lua_pushnumber (lua_current_interpreter, -1); - return 1; - } - - color = lua_tostring (lua_current_interpreter, -1); - - lua_pushnumber (lua_current_interpreter, - lua_plugin->get_irc_color (lua_plugin, (char *) color)); - return 1; -} - -/* - * weechat_lua_get_window_info: get infos about windows - */ - -static int -weechat_lua_get_window_info (lua_State *L) -{ - t_plugin_window_info *window_info, *ptr_win; - int i; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get window info, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - window_info = lua_plugin->get_window_info (lua_plugin); - if (!window_info) - { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - i = 0; - for (ptr_win = window_info; ptr_win; ptr_win = ptr_win->next_window) - { - lua_pushnumber (lua_current_interpreter, i); - lua_newtable (lua_current_interpreter); - - lua_pushstring (lua_current_interpreter, "num_buffer"); - lua_pushnumber (lua_current_interpreter, ptr_win->num_buffer); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "win_x"); - lua_pushnumber (lua_current_interpreter, ptr_win->win_x); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "win_y"); - lua_pushnumber (lua_current_interpreter, ptr_win->win_y); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "win_width"); - lua_pushnumber (lua_current_interpreter, ptr_win->win_width); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "win_height"); - lua_pushnumber (lua_current_interpreter, ptr_win->win_height); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "win_width_pct"); - lua_pushnumber (lua_current_interpreter, ptr_win->win_width_pct); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "win_height_pct"); - lua_pushnumber (lua_current_interpreter, ptr_win->win_height_pct); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - - i++; - } - - lua_plugin->free_window_info (lua_plugin, window_info); - - return 1; -} - -/* - * weechat_lua_get_buffer_info: get infos about buffers - */ - -static int -weechat_lua_get_buffer_info (lua_State *L) -{ - t_plugin_buffer_info *buffer_info, *ptr_buffer; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get buffer info, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - buffer_info = lua_plugin->get_buffer_info (lua_plugin); - if (!buffer_info) { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - for (ptr_buffer = buffer_info; ptr_buffer; ptr_buffer = ptr_buffer->next_buffer) - { - lua_pushnumber (lua_current_interpreter, ptr_buffer->number); - lua_newtable (lua_current_interpreter); - - 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); - - lua_pushstring (lua_current_interpreter, "server"); - lua_pushstring (lua_current_interpreter, - ptr_buffer->server_name == NULL ? "" : ptr_buffer->server_name); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "channel"); - lua_pushstring (lua_current_interpreter, - ptr_buffer->channel_name == NULL ? "" : ptr_buffer->channel_name); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "notify_level"); - lua_pushnumber (lua_current_interpreter, ptr_buffer->notify_level); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "log_filename"); - lua_pushstring (lua_current_interpreter, - ptr_buffer->log_filename == NULL ? "" : ptr_buffer->log_filename); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - } - - lua_plugin->free_buffer_info(lua_plugin, buffer_info); - - return 1; -} - -/* - * weechat_lua_get_buffer_data: get buffer content - */ - -static int -weechat_lua_get_buffer_data (lua_State *L) -{ - t_plugin_buffer_line *buffer_data, *ptr_data; - const char *server, *channel; - char timebuffer[64]; - int i, n; - - /* make C compiler happy */ - (void) L; - - if (!lua_current_script) - { - lua_plugin->print_server (lua_plugin, - "Lua error: unable to get buffer data, " - "script not initialized"); - lua_pushnil (lua_current_interpreter); - return 1; - } - - server = NULL; - channel = NULL; - - n = lua_gettop (lua_current_interpreter); - if (n != 2) - { - lua_plugin->print_server (lua_plugin, - "Lua error: wrong parameters for " - "\"get_buffer_data\" function"); - lua_pushnumber (lua_current_interpreter, 0); - return 1; - } - - server = lua_tostring (lua_current_interpreter, -2); - channel = lua_tostring (lua_current_interpreter, -1); - - buffer_data = lua_plugin->get_buffer_data (lua_plugin, (char *) server, (char *) channel); - if (!buffer_data) - { - lua_pushboolean (lua_current_interpreter, 0); - return 1; - } - - lua_newtable (lua_current_interpreter); - - for (i = 0, ptr_data = buffer_data; ptr_data; ptr_data = ptr_data->next_line, i++) - { - lua_pushnumber (lua_current_interpreter, i); - lua_newtable (lua_current_interpreter); - - strftime(timebuffer, sizeof(timebuffer), "%F %T", - localtime(&ptr_data->date)); - - lua_pushstring (lua_current_interpreter, "date"); - lua_pushstring (lua_current_interpreter, timebuffer); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "nick"); - lua_pushstring (lua_current_interpreter, - ptr_data->nick == NULL ? "" : ptr_data->nick); - lua_rawset (lua_current_interpreter, -3); - - lua_pushstring (lua_current_interpreter, "data"); - lua_pushstring (lua_current_interpreter, - ptr_data->data == NULL ? "" : ptr_data->data); - lua_rawset (lua_current_interpreter, -3); - - lua_rawset (lua_current_interpreter, -3); - } - - lua_plugin->free_buffer_data (lua_plugin, buffer_data); - - return 1; -} - -/* - * Lua constant as functions - */ - -static int -weechat_lua_constant_plugin_rc_ok (lua_State *L) -{ - /* make C compiler happy */ - (void) L; - - lua_pushnumber (lua_current_interpreter, PLUGIN_RC_OK); - return 1; -} - -static int -weechat_lua_constant_plugin_rc_ko (lua_State *L) -{ - /* make C compiler happy */ - (void) L; - - lua_pushnumber (lua_current_interpreter, PLUGIN_RC_KO); - return 1; -} - -static int -weechat_lua_constant_plugin_rc_ok_ignore_weechat (lua_State *L) -{ - /* make C compiler happy */ - (void) L; - - lua_pushnumber (lua_current_interpreter, PLUGIN_RC_OK_IGNORE_WEECHAT); - return 1; -} - -static int -weechat_lua_constant_plugin_rc_ok_ignore_plugins (lua_State *L) -{ - /* make C compiler happy */ - (void) L; - - lua_pushnumber (lua_current_interpreter, PLUGIN_RC_OK_IGNORE_PLUGINS); - return 1; -} - -static int -weechat_lua_constant_plugin_rc_ok_ignore_all (lua_State *L) -{ - /* make C compiler happy */ - (void) L; - - lua_pushnumber (lua_current_interpreter, PLUGIN_RC_OK_IGNORE_ALL); - return 1; -} - -static int -weechat_lua_constant_plugin_rc_ok_with_highlight (lua_State *L) -{ - /* make C compiler happy */ - (void) L; - - lua_pushnumber (lua_current_interpreter, PLUGIN_RC_OK_WITH_HIGHLIGHT); - return 1; -} - -/* - * Lua subroutines - */ - -static -const struct luaL_reg weechat_lua_funcs[] = { - { "register", weechat_lua_register }, - { "set_charset", weechat_lua_set_charset }, - { "print", weechat_lua_print }, - { "print_server", weechat_lua_print_server }, - { "print_infobar", weechat_lua_print_infobar }, - { "remove_infobar", weechat_lua_remove_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 }, - { "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 }, - { "get_dcc_info", weechat_lua_get_dcc_info }, - { "get_config", weechat_lua_get_config }, - { "set_config", weechat_lua_set_config }, - { "get_plugin_config", weechat_lua_get_plugin_config }, - { "set_plugin_config", weechat_lua_set_plugin_config }, - { "get_server_info", weechat_lua_get_server_info }, - { "get_channel_info", weechat_lua_get_channel_info }, - { "get_nick_info", weechat_lua_get_nick_info }, - { "get_irc_color", weechat_lua_get_irc_color }, - { "get_window_info", weechat_lua_get_window_info }, - { "get_buffer_info", weechat_lua_get_buffer_info }, - { "get_buffer_data", weechat_lua_get_buffer_data }, - /* define constants as function which returns values */ - { "PLUGIN_RC_OK", weechat_lua_constant_plugin_rc_ok }, - { "PLUGIN_RC_KO", weechat_lua_constant_plugin_rc_ko }, - { "PLUGIN_RC_OK_IGNORE_WEECHAT", weechat_lua_constant_plugin_rc_ok_ignore_weechat }, - { "PLUGIN_RC_OK_IGNORE_PLUGINS", weechat_lua_constant_plugin_rc_ok_ignore_plugins }, - { "PLUGIN_RC_OK_IGNORE_ALL", weechat_lua_constant_plugin_rc_ok_ignore_all }, - { "PLUGIN_RC_OK_WITH_HIGHLIGHT", weechat_lua_constant_plugin_rc_ok_with_highlight }, - { NULL, NULL } -}; - -int -weechat_lua_load (t_weechat_plugin *plugin, char *filename) -{ - FILE *fp; - char *weechat_lua_code = { - "weechat_outputs = {\n" - " write = function (self, str)\n" - " weechat.print(\"Lua stdout/stderr : \" .. str)\n" - " end\n" - "}\n" - "io.stdout = weechat_outputs\n" - "io.stderr = weechat_outputs\n" - }; - - plugin->print_server (plugin, "Loading Lua script \"%s\"", filename); - - if ((fp = fopen (filename, "r")) == NULL) - { - plugin->print_server (plugin, "Lua error: script \"%s\" not found", - filename); - return 0; - } - - lua_current_script = NULL; - - lua_current_interpreter = lua_open (); - - if (lua_current_interpreter == NULL) - { - plugin->print_server (plugin, - "Lua error: unable to create new sub-interpreter"); - fclose (fp); - return 0; - } - -#ifdef LUA_VERSION_NUM /* LUA_VERSION_NUM is defined only in lua >= 5.1.0 */ - luaL_openlibs (lua_current_interpreter); -#else - luaopen_base (lua_current_interpreter); - luaopen_string (lua_current_interpreter); - luaopen_table (lua_current_interpreter); - luaopen_math (lua_current_interpreter); - luaopen_io (lua_current_interpreter); - luaopen_debug (lua_current_interpreter); -#endif - - luaL_openlib (lua_current_interpreter, "weechat", weechat_lua_funcs, 0); - -#ifdef LUA_VERSION_NUM - if (luaL_dostring (lua_current_interpreter, weechat_lua_code) != 0) -#else - if (lua_dostring (lua_current_interpreter, weechat_lua_code) != 0) -#endif - plugin->print_server (plugin, - "Lua warning: unable to redirect stdout and stderr"); - - lua_current_script_filename = filename; - - if (luaL_loadfile (lua_current_interpreter, filename) != 0) - { - plugin->print_server (plugin, - "Lua error: unable to load file \"%s\"", - filename); - plugin->print_server (plugin, - "Lua error: %s", - lua_tostring (lua_current_interpreter, -1)); - lua_close (lua_current_interpreter); - fclose (fp); - return 0; - } - - if (lua_pcall (lua_current_interpreter, 0, 0, 0) != 0) - { - plugin->print_server (plugin, - "Lua error: unable to execute file \"%s\"", - filename); - plugin->print_server (plugin, - "Lua error: %s", - lua_tostring (lua_current_interpreter, -1)); - lua_close (lua_current_interpreter); - fclose (fp); - /* if script was registered, removing from list */ - if (lua_current_script != NULL) - weechat_script_remove (plugin, &lua_scripts, lua_current_script); - return 0; - } - fclose (fp); - - if (lua_current_script == NULL) - { - plugin->print_server (plugin, - "Lua error: function \"register\" not found " - "(or failed) in file \"%s\"", - filename); - lua_close (lua_current_interpreter); - return 0; - } - - lua_current_script->interpreter = (lua_State *) lua_current_interpreter; - - return 1; -} - -/* - * weechat_lua_unload: unload a Lua script - */ - -void -weechat_lua_unload (t_weechat_plugin *plugin, t_plugin_script *script) -{ - int *r; - - plugin->print_server (plugin, - "Unloading Lua script \"%s\"", - script->name); - - if (script->shutdow_func && script->shutdown_func[0]) - { - r = weechat_lua_exec (plugin, script, SCRIPT_EXEC_INT, - script->shutdown_func, NULL, NULL, NULL); - if (r) - free (r); - } - - lua_close (script->interpreter); - - weechat_script_remove (plugin, &lua_scripts, script); -} - -/* - * weechat_lua_unload_name: unload a Lua script by name - */ - -void -weechat_lua_unload_name (t_weechat_plugin *plugin, char *name) -{ - t_plugin_script *ptr_script; - - ptr_script = weechat_script_search (plugin, &lua_scripts, name); - if (ptr_script) - { - weechat_lua_unload (plugin, ptr_script); - plugin->print_server (plugin, - "Lua script \"%s\" unloaded", - name); - } - else - { - plugin->print_server (plugin, - "Lua error: script \"%s\" not loaded", - name); - } -} - -/* - * weechat_lua_unload_all: unload all Lua scripts - */ - -void -weechat_lua_unload_all (t_weechat_plugin *plugin) -{ - plugin->print_server (plugin, - "Unloading all Lua scripts"); - while (lua_scripts) - weechat_lua_unload (plugin, lua_scripts); - - plugin->print_server (plugin, - "Lua scripts unloaded"); -} - -/* - * weechat_lua_cmd: /lua command handler - */ - -int -weechat_lua_cmd (t_weechat_plugin *plugin, - int cmd_argc, char **cmd_argv, - char *handler_args, void *handler_pointer) -{ - int argc, handler_found, modifier_found; - char **argv, *path_script; - t_plugin_script *ptr_script; - t_plugin_handler *ptr_handler; - t_plugin_modifier *ptr_modifier; - - /* make C compiler happy */ - (void) handler_args; - (void) handler_pointer; - - if (cmd_argc < 3) - return PLUGIN_RC_KO; - - if (cmd_argv[2]) - argv = plugin->explode_string (plugin, cmd_argv[2], " ", 0, &argc); - else - { - argv = NULL; - argc = 0; - } - - switch (argc) - { - case 0: - /* list registered Lua scripts */ - plugin->print_server (plugin, ""); - plugin->print_server (plugin, "Registered Lua scripts:"); - if (lua_scripts) - { - for (ptr_script = lua_scripts; - ptr_script; ptr_script = ptr_script->next_script) - { - plugin->print_server (plugin, " %s v%s%s%s", - ptr_script->name, - ptr_script->version, - (ptr_script->description[0]) ? " - " : "", - ptr_script->description); - } - } - else - plugin->print_server (plugin, " (none)"); - - /* list Lua message handlers */ - plugin->print_server (plugin, ""); - plugin->print_server (plugin, "Lua message handlers:"); - handler_found = 0; - for (ptr_handler = plugin->handlers; - ptr_handler; ptr_handler = ptr_handler->next_handler) - { - if ((ptr_handler->type == PLUGIN_HANDLER_MESSAGE) - && (ptr_handler->handler_args)) - { - handler_found = 1; - plugin->print_server (plugin, " IRC(%s) => Lua(%s)", - ptr_handler->irc_command, - ptr_handler->handler_args); - } - } - if (!handler_found) - plugin->print_server (plugin, " (none)"); - - /* list Lua command handlers */ - plugin->print_server (plugin, ""); - plugin->print_server (plugin, "Lua command handlers:"); - handler_found = 0; - for (ptr_handler = plugin->handlers; - ptr_handler; ptr_handler = ptr_handler->next_handler) - { - if ((ptr_handler->type == PLUGIN_HANDLER_COMMAND) - && (ptr_handler->handler_args)) - { - handler_found = 1; - plugin->print_server (plugin, " /%s => Lua(%s)", - ptr_handler->command, - 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 == PLUGIN_HANDLER_TIMER) - && (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)"); - - /* list Lua keyboard handlers */ - plugin->print_server (plugin, ""); - plugin->print_server (plugin, "Lua keyboard handlers:"); - handler_found = 0; - for (ptr_handler = plugin->handlers; - ptr_handler; ptr_handler = ptr_handler->next_handler) - { - if ((ptr_handler->type == PLUGIN_HANDLER_KEYBOARD) - && (ptr_handler->handler_args)) - { - handler_found = 1; - plugin->print_server (plugin, " Lua(%s)", - ptr_handler->handler_args); - } - } - if (!handler_found) - plugin->print_server (plugin, " (none)"); - - /* list Lua event handlers */ - plugin->print_server (plugin, ""); - plugin->print_server (plugin, "Lua event handlers:"); - handler_found = 0; - for (ptr_handler = plugin->handlers; - ptr_handler; ptr_handler = ptr_handler->next_handler) - { - if ((ptr_handler->type == PLUGIN_HANDLER_EVENT) - && (ptr_handler->handler_args)) - { - handler_found = 1; - plugin->print_server (plugin, " %s => Lua(%s)", - ptr_handler->event, - ptr_handler->handler_args); - } - } - if (!handler_found) - plugin->print_server (plugin, " (none)"); - - /* list Lua modifiers */ - plugin->print_server (plugin, ""); - plugin->print_server (plugin, "Lua modifiers:"); - modifier_found = 0; - for (ptr_modifier = plugin->modifiers; - ptr_modifier; ptr_modifier = ptr_modifier->next_modifier) - { - modifier_found = 1; - if (ptr_modifier->type == PLUGIN_MODIFIER_IRC_IN) - plugin->print_server (plugin, " IRC(%s, %s) => Lua(%s)", - ptr_modifier->command, - PLUGIN_MODIFIER_IRC_IN_STR, - ptr_modifier->modifier_args); - else if (ptr_modifier->type == PLUGIN_MODIFIER_IRC_USER) - plugin->print_server (plugin, " IRC(%s, %s) => Lua(%s)", - ptr_modifier->command, - PLUGIN_MODIFIER_IRC_USER_STR, - ptr_modifier->modifier_args); - else if (ptr_modifier->type == PLUGIN_MODIFIER_IRC_OUT) - plugin->print_server (plugin, " IRC(%s, %s) => Lua(%s)", - ptr_modifier->command, - PLUGIN_MODIFIER_IRC_OUT_STR, - ptr_modifier->modifier_args); - } - if (!modifier_found) - plugin->print_server (plugin, " (none)"); - break; - case 1: - if (plugin->strcasecmp (plugin, argv[0], "autoload") == 0) - weechat_script_auto_load (plugin, "lua", weechat_lua_load); - else if (plugin->strcasecmp (plugin, argv[0], "reload") == 0) - { - weechat_lua_unload_all (plugin); - weechat_script_auto_load (plugin, "lua", weechat_lua_load); - } - else if (plugin->strcasecmp (plugin, argv[0], "unload") == 0) - weechat_lua_unload_all (plugin); - break; - case 2: - if (plugin->strcasecmp (plugin, argv[0], "load") == 0) - { - /* load Lua script */ - path_script = weechat_script_search_full_name (plugin, "lua", argv[1]); - weechat_lua_load (plugin, (path_script) ? path_script : argv[1]); - if (path_script) - free (path_script); - } - else if (plugin->strcasecmp (plugin, argv[0], "unload") == 0) - { - /* unload Lua script */ - weechat_lua_unload_name (plugin, argv[1]); - } - else - { - plugin->print_server (plugin, - "Lua error: unknown option for " - "\"lua\" command"); - } - break; - default: - plugin->print_server (plugin, - "Lua error: wrong argument count for \"lua\" command"); - } - - if (argv) - plugin->free_exploded_string (plugin, argv); - - return PLUGIN_RC_OK; -} - -/* - * weechat_plugin_init: initialize Lua plugin - */ - -int -weechat_plugin_init (t_weechat_plugin *plugin) -{ - - lua_plugin = plugin; - - plugin->print_server (plugin, "Loading Lua module \"weechat\""); - - plugin->cmd_handler_add (plugin, "lua", - "list/load/unload Lua scripts", - "[load filename] | [autoload] | [reload] | [unload [script]]", - "filename: Lua script (file) to load\n" - "script: script name to unload\n\n" - "Without argument, /lua command lists all loaded Lua scripts.", - "load|autoload|reload|unload %f", - weechat_lua_cmd, NULL, NULL); - - plugin->mkdir_home (plugin, "lua"); - plugin->mkdir_home (plugin, "lua/autoload"); - - script_init (weechat_lua_plugin); - weechat_script_auto_load (plugin, "lua", weechat_lua_load); - - /* init ok */ - return PLUGIN_RC_OK; -} - -/* - * weechat_plugin_end: shutdown Lua interface - */ - -void -weechat_plugin_end (t_weechat_plugin *plugin) -{ - /* unload all scripts */ - weechat_lua_unload_all (plugin); - - lua_plugin->print_server (lua_plugin, - "Lua plugin ended"); -} diff --git a/src/plugins/scripts/lua/weechat-lua-api.c b/src/plugins/scripts/lua/weechat-lua-api.c new file mode 100644 index 000000000..a3373c9f0 --- /dev/null +++ b/src/plugins/scripts/lua/weechat-lua-api.c @@ -0,0 +1,3062 @@ +/* + * Copyright (c) 2003-2008 by FlashCode + * See README for License detail, AUTHORS for developers list. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* weechat-lua-api.c: Lua API functions */ + +#undef _ + +#include +#include +#include +#include +#include + +#include "../../weechat-plugin.h" +#include "../script.h" +#include "../script-api.h" +#include "../script-callback.h" +#include "weechat-lua.h" + + +#define LUA_RETURN_OK return 1 +#define LUA_RETURN_ERROR return 0 +#define LUA_RETURN_EMPTY \ + lua_pushstring (lua_current_interpreter, ""); \ + return 0 +#define LUA_RETURN_STRING(__string) \ + lua_pushstring (lua_current_interpreter, \ + (__string) ? __string : ""); \ + return 1; +#define LUA_RETURN_STRING_FREE(__string) \ + lua_pushstring (lua_current_interpreter, \ + (__string) ? __string : ""); \ + if (__string) \ + free (__string); \ + return 1; + + +/* + * weechat_lua_api_register: startup function for all WeeChat Lua scripts + */ + +static int +weechat_lua_api_register (lua_State *L) +{ + const char *name, *author, *version, *license, *description; + const char *shutdown_func, *charset; + int n; + + /* make C compiler happy */ + (void) L; + + lua_current_script = NULL; + + name = NULL; + author = NULL; + version = NULL; + license = NULL; + description = NULL; + shutdown_func = NULL; + charset = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 7) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("register"); + LUA_RETURN_ERROR; + } + + name = lua_tostring (lua_current_interpreter, -7); + author = lua_tostring (lua_current_interpreter, -6); + version = lua_tostring (lua_current_interpreter, -5); + license = lua_tostring (lua_current_interpreter, -4); + description = lua_tostring (lua_current_interpreter, -3); + shutdown_func = lua_tostring (lua_current_interpreter, -2); + charset = lua_tostring (lua_current_interpreter, -1); + + if (script_search (weechat_lua_plugin, lua_scripts, (char *)name)) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unable to register script " + "\"%s\" (another script already " + "exists with this name)"), + weechat_prefix ("error"), "lua", name); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + /* register script */ + lua_current_script = script_add (weechat_lua_plugin, + &lua_scripts, + (lua_current_script_filename) ? + lua_current_script_filename : "", + (char *)name, + (char *)author, + (char *)version, + (char *)license, + (char *)description, + (char *)shutdown_func, + (char *)charset); + if (lua_current_script) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: registered script \"%s\", " + "version %s (%s)"), + weechat_prefix ("info"), "lua", + name, version, description); + } + else + { + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + lua_pushnumber (lua_current_interpreter, 1); + return 1; +} + +/* + * weechat_lua_api_charset_set: set script charset + */ + +static int +weechat_lua_api_charset_set (lua_State *L) +{ + const char *charset; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("charset_set"); + LUA_RETURN_ERROR; + } + + charset = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("charset_set"); + LUA_RETURN_ERROR; + } + + charset = lua_tostring (lua_current_interpreter, -1); + + script_api_charset_set (lua_current_script, + (char *) charset); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_iconv_to_internal: convert string to internal WeeChat charset + */ + +static int +weechat_lua_api_iconv_to_internal (lua_State *L) +{ + const char *charset, *string; + char *result; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("iconv_to_internal"); + LUA_RETURN_EMPTY; + } + + charset = NULL; + string = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("iconv_to_internal"); + LUA_RETURN_ERROR; + } + + charset = lua_tostring (lua_current_interpreter, -2); + string = lua_tostring (lua_current_interpreter, -1); + + result = weechat_iconv_to_internal ((char *)charset, (char *)string); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_iconv_from_internal: convert string from WeeChat internal + * charset to another one + */ + +static int +weechat_lua_api_iconv_from_internal (lua_State *L) +{ + const char *charset, *string; + char *result; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("iconv_from_internal"); + LUA_RETURN_EMPTY; + } + + charset = NULL; + string = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("iconv_from_internal"); + LUA_RETURN_ERROR; + } + + charset = lua_tostring (lua_current_interpreter, -2); + string = lua_tostring (lua_current_interpreter, -1); + + result = weechat_iconv_from_internal ((char *)charset, (char *)string); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_mkdir_home: create a directory in WeeChat home + */ + +static int +weechat_lua_api_mkdir_home (lua_State *L) +{ + const char *directory; + int mode, n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("mkdir_home"); + LUA_RETURN_ERROR; + } + + directory = NULL; + mode = 0; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("mkdir_home"); + LUA_RETURN_ERROR; + } + + directory = lua_tostring (lua_current_interpreter, -2); + mode = lua_tonumber (lua_current_interpreter, -1); + + if (weechat_mkdir_home ((char *)directory, mode)) + LUA_RETURN_OK; + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_mkdir: create a directory + */ + +static int +weechat_lua_api_mkdir (lua_State *L) +{ + const char *directory; + int mode, n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("mkdir"); + LUA_RETURN_ERROR; + } + + directory = NULL; + mode = 0; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("mkdir"); + LUA_RETURN_ERROR; + } + + directory = lua_tostring (lua_current_interpreter, -2); + mode = lua_tonumber (lua_current_interpreter, -1); + + if (weechat_mkdir ((char *)directory, mode)) + LUA_RETURN_OK; + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_prefix: get a prefix, used for display + */ + +static int +weechat_lua_api_prefix (lua_State *L) +{ + const char *prefix; + char *result; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("prefix"); + LUA_RETURN_EMPTY; + } + + prefix = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("prefix"); + LUA_RETURN_ERROR; + } + + prefix = lua_tostring (lua_current_interpreter, -1); + + result = weechat_prefix ((char *)prefix); + LUA_RETURN_STRING(result); +} + +/* + * weechat_lua_api_color: get a color code, used for display + */ + +static int +weechat_lua_api_color (lua_State *L) +{ + const char *color; + char *result; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("color"); + LUA_RETURN_EMPTY; + } + + color = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("color"); + LUA_RETURN_ERROR; + } + + color = lua_tostring (lua_current_interpreter, -1); + + result = weechat_prefix ((char *)color); + LUA_RETURN_STRING(result); +} + +/* + * weechat_lua_api_print: print message in a buffer + */ + +static int +weechat_lua_api_print (lua_State *L) +{ + const char *buffer, *message; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("print"); + LUA_RETURN_ERROR; + } + + buffer = NULL; + message = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("print"); + LUA_RETURN_ERROR; + } + + buffer = lua_tostring (lua_current_interpreter, -2); + message = lua_tostring (lua_current_interpreter, -1); + + script_api_printf (weechat_lua_plugin, + lua_current_script, + script_string_to_pointer ((char *)buffer), + "%s", (char *)message); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_infobar_print: print message to infobar + */ + +static int +weechat_lua_api_infobar_print (lua_State *L) +{ + const char *color, *message; + int delay, n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("infobar_print"); + LUA_RETURN_ERROR; + } + + delay = 1; + color = NULL; + message = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("infobar_print"); + LUA_RETURN_ERROR; + } + + delay = lua_tonumber (lua_current_interpreter, -3); + color = lua_tostring (lua_current_interpreter, -2); + message = lua_tostring (lua_current_interpreter, -1); + + script_api_infobar_printf (weechat_lua_plugin, + lua_current_script, + delay, + (char *)color, + "%s", + (char *)message); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_infobar_remove: remove message(s) in infobar + */ + +static int +weechat_lua_api_infobar_remove (lua_State *L) +{ + int n, how_many; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("infobar_remove"); + LUA_RETURN_ERROR; + } + + how_many = 0; + + n = lua_gettop (lua_current_interpreter); + + if (n == 1) + how_many = lua_tonumber (lua_current_interpreter, -1); + + weechat_infobar_remove (how_many); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_log_print: print message in WeeChat log file + */ + +static int +weechat_lua_api_log_print (lua_State *L) +{ + const char *message; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("log_print"); + LUA_RETURN_ERROR; + } + + message = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("log_print"); + LUA_RETURN_ERROR; + } + + message = lua_tostring (lua_current_interpreter, -1); + + script_api_log_printf (weechat_lua_plugin, + lua_current_script, + "%s", (char *) message); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_hook_command_cb: callback for command hooked + */ + +int +weechat_lua_api_hook_command_cb (void *data, struct t_gui_buffer *buffer, + int argc, char **argv, char **argv_eol) +{ + struct t_script_callback *script_callback; + char *lua_argv[3], empty_arg[1] = { '\0' }; + int *rc, ret; + + /* make C compiler happy */ + (void) argv; + + script_callback = (struct t_script_callback *)data; + + lua_argv[0] = script_pointer_to_string (buffer); + lua_argv[1] = (argc > 1) ? argv_eol[1] : empty_arg; + lua_argv[2] = NULL; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + if (lua_argv[0]) + free (lua_argv[0]); + + return ret; +} + +/* + * weechat_lua_api_hook_command: hook a command + */ + +static int +weechat_lua_api_hook_command (lua_State *L) +{ + const char *command, *description, *args, *args_description, *completion; + const char *function; + char *result; + int n; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_command"); + LUA_RETURN_EMPTY; + } + + command = NULL; + description = NULL; + args = NULL; + args_description = NULL; + completion = NULL; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 6) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_command"); + LUA_RETURN_EMPTY; + } + + command = lua_tostring (lua_current_interpreter, -6); + description = lua_tostring (lua_current_interpreter, -5); + args = lua_tostring (lua_current_interpreter, -4); + args_description = lua_tostring (lua_current_interpreter, -3); + completion = lua_tostring (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_command (weechat_lua_plugin, + lua_current_script, + (char *)command, + (char *)description, + (char *)args, + (char *)args_description, + (char *)completion, + &weechat_lua_api_hook_command_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_hook_timer_cb: callback for timer hooked + */ + +int +weechat_lua_api_hook_timer_cb (void *data) +{ + struct t_script_callback *script_callback; + char *lua_argv[1] = { NULL }; + int *rc, ret; + + script_callback = (struct t_script_callback *)data; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + + return ret; +} + +/* + * weechat_lua_api_hook_timer: hook a timer + */ + +static int +weechat_lua_api_hook_timer (lua_State *L) +{ + int n, interval, align_second, max_calls; + const char *function; + char *result; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_timer"); + LUA_RETURN_EMPTY; + } + + interval = 10; + align_second = 0; + max_calls = 0; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 4) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_timer"); + LUA_RETURN_EMPTY; + } + + interval = lua_tonumber (lua_current_interpreter, -4); + align_second = lua_tonumber (lua_current_interpreter, -3); + max_calls = lua_tonumber (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_timer (weechat_lua_plugin, + lua_current_script, + interval, + align_second, + max_calls, + &weechat_lua_api_hook_timer_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_hook_fd_cb: callback for fd hooked + */ + +int +weechat_lua_api_hook_fd_cb (void *data) +{ + struct t_script_callback *script_callback; + char *lua_argv[1] = { NULL }; + int *rc, ret; + + script_callback = (struct t_script_callback *)data; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + + return ret; +} + +/* + * weechat_lua_api_hook_fd: hook a fd + */ + +static int +weechat_lua_api_hook_fd (lua_State *L) +{ + int n, fd, read, write, exception; + const char *function; + char *result; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_fd"); + LUA_RETURN_EMPTY; + } + + fd = 0; + read = 0; + write = 0; + exception = 0; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 5) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_fd"); + LUA_RETURN_EMPTY; + } + + fd = lua_tonumber (lua_current_interpreter, -5); + read = lua_tonumber (lua_current_interpreter, -4); + write = lua_tonumber (lua_current_interpreter, -3); + exception = lua_tonumber (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_fd (weechat_lua_plugin, + lua_current_script, + fd, + read, + write, + exception, + &weechat_lua_api_hook_fd_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_hook_print_cb: callback for print hooked + */ + +int +weechat_lua_api_hook_print_cb (void *data, struct t_gui_buffer *buffer, + time_t date, char *prefix, char *message) +{ + struct t_script_callback *script_callback; + char *lua_argv[5]; + static char timebuffer[64]; + int *rc, ret; + + script_callback = (struct t_script_callback *)data; + + snprintf (timebuffer, sizeof (timebuffer) - 1, "%ld", date); + + lua_argv[0] = script_pointer_to_string (buffer); + lua_argv[1] = timebuffer; + lua_argv[2] = prefix; + lua_argv[3] = message; + lua_argv[4] = NULL; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + + return ret; +} + +/* + * weechat_lua_api_hook_print: hook a print + */ + +static int +weechat_lua_api_hook_print (lua_State *L) +{ + const char *buffer, *message, *function; + char *result; + int n, strip_colors; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_print"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + message = NULL; + strip_colors = 0; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 4) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_print"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -4); + message = lua_tostring (lua_current_interpreter, -3); + strip_colors = lua_tonumber (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_print (weechat_lua_plugin, + lua_current_script, + script_string_to_pointer ((char *)buffer), + (char *)message, + strip_colors, + &weechat_lua_api_hook_print_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_hook_signal_cb: callback for signal hooked + */ + +int +weechat_lua_api_hook_signal_cb (void *data, char *signal, char *type_data, + void *signal_data) +{ + struct t_script_callback *script_callback; + char *lua_argv[3]; + static char value_str[64]; + int *rc, ret, free_needed; + + script_callback = (struct t_script_callback *)data; + + lua_argv[0] = signal; + free_needed = 0; + if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0) + { + lua_argv[1] = (char *)signal_data; + } + else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_INT) == 0) + { + snprintf (value_str, sizeof (value_str) - 1, + "%d", *((int *)signal_data)); + lua_argv[1] = value_str; + } + else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_POINTER) == 0) + { + lua_argv[1] = script_pointer_to_string (signal_data); + free_needed = 1; + } + else + lua_argv[1] = NULL; + lua_argv[2] = NULL; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + + return ret; +} + +/* + * weechat_lua_api_hook_signal: hook a signal + */ + +static int +weechat_lua_api_hook_signal (lua_State *L) +{ + const char *signal, *function; + char *result; + int n; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_signal"); + LUA_RETURN_EMPTY; + } + + signal = NULL; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_signal"); + LUA_RETURN_EMPTY; + } + + signal = lua_tostring (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_signal (weechat_lua_plugin, + lua_current_script, + (char *)signal, + &weechat_lua_api_hook_signal_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_hook_signal_send: send a signal + */ + +static int +weechat_lua_api_hook_signal_send (lua_State *L) +{ + const char *signal, *type_data, *signal_data; + int n, number; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_signal"); + LUA_RETURN_EMPTY; + } + + signal = NULL; + type_data = NULL; + signal_data = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_signal"); + LUA_RETURN_EMPTY; + } + + signal = lua_tostring (lua_current_interpreter, -3); + type_data = lua_tostring (lua_current_interpreter, -2); + + if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0) + { + signal_data = lua_tostring (lua_current_interpreter, -1); + weechat_hook_signal_send ((char *)signal, (char *)type_data, + (char *)signal_data); + LUA_RETURN_OK; + } + else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_INT) == 0) + { + number = lua_tonumber (lua_current_interpreter, -1); + weechat_hook_signal_send ((char *)signal, (char *)type_data, + &number); + LUA_RETURN_OK; + } + else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_POINTER) == 0) + { + signal_data = lua_tostring (lua_current_interpreter, -1); + weechat_hook_signal_send ((char *)signal, (char *)type_data, + script_string_to_pointer ((char *)signal_data)); + LUA_RETURN_OK; + } + + LUA_RETURN_ERROR; +} + +/* + * weechat_lua_api_hook_config_cb: callback for config option hooked + */ + +int +weechat_lua_api_hook_config_cb (void *data, char *type, char *option, + char *value) +{ + struct t_script_callback *script_callback; + char *lua_argv[4]; + int *rc, ret; + + script_callback = (struct t_script_callback *)data; + + lua_argv[0] = type; + lua_argv[1] = option; + lua_argv[2] = value; + lua_argv[3] = NULL; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + + return ret; +} + +/* + * weechat_lua_api_hook_config: hook a config option + */ + +static int +weechat_lua_api_hook_config (lua_State *L) +{ + const char *type, *option, *function; + char *result; + int n; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_config"); + LUA_RETURN_EMPTY; + } + + type = NULL; + option = NULL; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_config"); + LUA_RETURN_EMPTY; + } + + type = lua_tostring (lua_current_interpreter, -3); + option = lua_tostring (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_config (weechat_lua_plugin, + lua_current_script, + (char *)type, + (char *)option, + &weechat_lua_api_hook_config_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_hook_completion_cb: callback for completion option hooked + */ + +int +weechat_lua_api_hook_completion_cb (void *data, char *completion, + struct t_gui_buffer *buffer, + struct t_weelist *list) +{ + struct t_script_callback *script_callback; + char *lua_argv[4]; + int *rc, ret; + + script_callback = (struct t_script_callback *)data; + + lua_argv[0] = completion; + lua_argv[1] = script_pointer_to_string (buffer); + lua_argv[2] = script_pointer_to_string (list); + lua_argv[3] = NULL; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + if (lua_argv[1]) + free (lua_argv[1]); + if (lua_argv[2]) + free (lua_argv[2]); + + return ret; +} + +/* + * weechat_lua_api_hook_completion: hook a completion + */ + +static int +weechat_lua_api_hook_completion (lua_State *L) +{ + const char *completion, *function; + char *result; + int n; + struct t_hook *new_hook; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("hook_completion"); + LUA_RETURN_EMPTY; + } + + completion = NULL; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_completion"); + LUA_RETURN_EMPTY; + } + + completion = lua_tostring (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_hook = script_api_hook_completion (weechat_lua_plugin, + lua_current_script, + (char *)completion, + &weechat_lua_api_hook_completion_cb, + (char *)function); + + result = script_pointer_to_string (new_hook); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_unhook: unhook something + */ + +static int +weechat_lua_api_unhook (lua_State *L) +{ + const char *hook; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("unhook"); + LUA_RETURN_ERROR; + } + + hook = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("unhook"); + LUA_RETURN_ERROR; + } + + hook = lua_tostring (lua_current_interpreter, -1); + + if (script_api_unhook (weechat_lua_plugin, + lua_current_script, + script_string_to_pointer ((char *)hook))) + LUA_RETURN_OK; + + LUA_RETURN_ERROR; +} + +/* + * weechat_lua_api_unhook_all: unhook all for script + */ + +static int +weechat_lua_api_unhook_all (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("unhook_all"); + LUA_RETURN_ERROR; + } + + script_api_unhook_all (weechat_lua_plugin, + lua_current_script); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_input_data_cb: callback for input data in a buffer + */ + +int +weechat_lua_api_input_data_cb (void *data, struct t_gui_buffer *buffer, + char *input_data) +{ + struct t_script_callback *script_callback; + char *lua_argv[3]; + int *rc, ret; + + script_callback = (struct t_script_callback *)data; + + lua_argv[0] = script_pointer_to_string (buffer); + lua_argv[2] = input_data; + lua_argv[3] = NULL; + + rc = (int *) weechat_lua_exec (script_callback->script, + WEECHAT_SCRIPT_EXEC_INT, + script_callback->function, + lua_argv); + + if (!rc) + ret = WEECHAT_RC_ERROR; + else + { + ret = *rc; + free (rc); + } + if (lua_argv[0]) + free (lua_argv[0]); + + return ret; +} + +/* + * weechat_lua_api_buffer_new: create a new buffer + */ + +static int +weechat_lua_api_buffer_new (lua_State *L) +{ + const char *category, *name, *function; + char *result; + int n; + struct t_gui_buffer *new_buffer; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("buffer_new"); + LUA_RETURN_EMPTY; + } + + category = NULL; + name = NULL; + function = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("buffer_new"); + LUA_RETURN_EMPTY; + } + + category = lua_tostring (lua_current_interpreter, -3); + name = lua_tostring (lua_current_interpreter, -2); + function = lua_tostring (lua_current_interpreter, -1); + + new_buffer = script_api_buffer_new (weechat_lua_plugin, + lua_current_script, + (char *)category, + (char *)name, + &weechat_lua_api_input_data_cb, + (char *)function); + + result = script_pointer_to_string (new_buffer); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_buffer_search: search a buffer + */ + +static int +weechat_lua_api_buffer_search (lua_State *L) +{ + const char *category, *name; + char *result; + int n; + struct t_gui_buffer *ptr_buffer; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("buffer_search"); + LUA_RETURN_EMPTY; + } + + category = NULL; + name = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("buffer_search"); + LUA_RETURN_EMPTY; + } + + category = lua_tostring (lua_current_interpreter, -2); + name = lua_tostring (lua_current_interpreter, -1); + + ptr_buffer = weechat_buffer_search ((char *)category, + (char *)name); + + result = script_pointer_to_string (ptr_buffer); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_buffer_close: close a buffer + */ + +static int +weechat_lua_api_buffer_close (lua_State *L) +{ + const char *buffer; + int n, switch_to_another; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("buffer_close"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + switch_to_another = 0; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("buffer_close"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -2); + switch_to_another = lua_tonumber (lua_current_interpreter, -1); + + script_api_buffer_close (weechat_lua_plugin, + lua_current_script, + script_string_to_pointer ((char *)buffer), + switch_to_another); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_buffer_get: get a buffer property + */ + +static int +weechat_lua_api_buffer_get (lua_State *L) +{ + const char *buffer, *property; + char *value; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("buffer_get"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + property = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("buffer_get"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -2); + property = lua_tostring (lua_current_interpreter, -1); + + value = weechat_buffer_get (script_string_to_pointer ((char *)buffer), + (char *)property); + LUA_RETURN_STRING(value); +} + +/* + * weechat_lua_api_buffer_set: set a buffer property + */ + +static int +weechat_lua_api_buffer_set (lua_State *L) +{ + const char *buffer, *property, *value; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("buffer_set"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + property = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("buffer_set"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -3); + property = lua_tostring (lua_current_interpreter, -2); + value = lua_tostring (lua_current_interpreter, -1); + + weechat_buffer_set (script_string_to_pointer ((char *)buffer), + (char *)property, + (char *)value); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_nicklist_add_group: add a group in nicklist + */ + +static int +weechat_lua_api_nicklist_add_group (lua_State *L) +{ + const char *buffer, *parent_group, *name, *color; + char *result; + int n, visible; + struct t_gui_nick_group *new_group; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_add_group"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + parent_group = NULL; + name = NULL; + color = NULL; + visible = 0; + + n = lua_gettop (lua_current_interpreter); + + if (n < 5) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_add_group"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -5); + parent_group = lua_tostring (lua_current_interpreter, -4); + name = lua_tostring (lua_current_interpreter, -3); + color = lua_tostring (lua_current_interpreter, -2); + visible = lua_tonumber (lua_current_interpreter, -1); + + new_group = weechat_nicklist_add_group (script_string_to_pointer ((char *)buffer), + script_string_to_pointer ((char *)parent_group), + (char *)name, + (char *)color, + visible); + + result = script_pointer_to_string (new_group); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_nicklist_search_group: search a group in nicklist + */ + +static int +weechat_lua_api_nicklist_search_group (lua_State *L) +{ + const char *buffer, *from_group, *name; + char *result; + int n; + struct t_gui_nick_group *ptr_group; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_search_group"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + from_group = NULL; + name = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_search_group"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -3); + from_group = lua_tostring (lua_current_interpreter, -2); + name = lua_tostring (lua_current_interpreter, -1); + + ptr_group = weechat_nicklist_search_group (script_string_to_pointer ((char *)buffer), + script_string_to_pointer ((char *)from_group), + (char *)name); + + result = script_pointer_to_string (ptr_group); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_nicklist_add_nick: add a nick in nicklist + */ + +static int +weechat_lua_api_nicklist_add_nick (lua_State *L) +{ + const char *buffer, *group, *name, *color, *prefix, *prefix_color; + char char_prefix, *result; + int n, visible; + struct t_gui_nick *new_nick; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_add_nick"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + group = NULL; + name = NULL; + color = NULL; + prefix = NULL; + prefix_color = NULL; + visible = 0; + + n = lua_gettop (lua_current_interpreter); + + if (n < 7) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_add_nick"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -7); + group = lua_tostring (lua_current_interpreter, -6); + name = lua_tostring (lua_current_interpreter, -5); + color = lua_tostring (lua_current_interpreter, -4); + prefix = lua_tostring (lua_current_interpreter, -3); + prefix_color = lua_tostring (lua_current_interpreter, -2); + visible = lua_tonumber (lua_current_interpreter, -1); + + if (prefix && prefix[0]) + char_prefix = prefix[0]; + else + char_prefix = ' '; + + new_nick = weechat_nicklist_add_nick (script_string_to_pointer ((char *)buffer), + script_string_to_pointer ((char *)group), + (char *)name, + (char *)color, + char_prefix, + (char *)prefix_color, + visible); + + result = script_pointer_to_string (new_nick); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_nicklist_search_nick: search a nick in nicklist + */ + +static int +weechat_lua_api_nicklist_search_nick (lua_State *L) +{ + const char *buffer, *from_group, *name; + char *result; + int n; + struct t_gui_nick *ptr_nick; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_search_nick"); + LUA_RETURN_EMPTY; + } + + buffer = NULL; + from_group = NULL; + name = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 3) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_search_nick"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -3); + from_group = lua_tostring (lua_current_interpreter, -2); + name = lua_tostring (lua_current_interpreter, -1); + + ptr_nick = weechat_nicklist_search_nick (script_string_to_pointer ((char *)buffer), + script_string_to_pointer ((char *)from_group), + (char *)name); + + result = script_pointer_to_string (ptr_nick); + LUA_RETURN_STRING_FREE(result); +} + +/* + * weechat_lua_api_nicklist_remove_group: remove a group from nicklist + */ + +static int +weechat_lua_api_nicklist_remove_group (lua_State *L) +{ + const char *buffer, *group; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_remove_group"); + LUA_RETURN_ERROR; + } + + buffer = NULL; + group = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_remove_group"); + LUA_RETURN_ERROR; + } + + buffer = lua_tostring (lua_current_interpreter, -3); + group = lua_tostring (lua_current_interpreter, -2); + + weechat_nicklist_remove_group (script_string_to_pointer ((char *)buffer), + script_string_to_pointer ((char *)group)); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_nicklist_remove_nick: remove a nick from nicklist + */ + +static int +weechat_lua_api_nicklist_remove_nick (lua_State *L) +{ + const char *buffer, *nick; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_remove_nick"); + LUA_RETURN_ERROR; + } + + buffer = NULL; + nick = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_remove_nick"); + LUA_RETURN_ERROR; + } + + buffer = lua_tostring (lua_current_interpreter, -3); + nick = lua_tostring (lua_current_interpreter, -2); + + weechat_nicklist_remove_nick (script_string_to_pointer ((char *)buffer), + script_string_to_pointer ((char *)nick)); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_nicklist_remove_all: remove all groups/nicks from nicklist + */ + +static int +weechat_lua_api_nicklist_remove_all (lua_State *L) +{ + const char *buffer; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("nicklist_remove_all"); + LUA_RETURN_ERROR; + } + + buffer = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("nicklist_remove_all"); + LUA_RETURN_ERROR; + } + + buffer = lua_tostring (lua_current_interpreter, -3); + + weechat_nicklist_remove_all (script_string_to_pointer ((char *)buffer)); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_command: send command to server + */ + +static int +weechat_lua_api_command (lua_State *L) +{ + const char *buffer, *command; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("command"); + LUA_RETURN_EMPTY; + } + + command = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 2) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("command"); + LUA_RETURN_EMPTY; + } + + buffer = lua_tostring (lua_current_interpreter, -2); + command = lua_tostring (lua_current_interpreter, -1); + + script_api_command (weechat_lua_plugin, + lua_current_script, + script_string_to_pointer ((char *)buffer), + (char *)command); + + LUA_RETURN_OK; +} + +/* + * weechat_lua_api_info_get: get info about WeeChat + */ + +static int +weechat_lua_api_info_get (lua_State *L) +{ + const char *info; + char *value; + int n; + + /* make C compiler happy */ + (void) L; + + if (!lua_current_script) + { + WEECHAT_SCRIPT_MSG_NOT_INITIALIZED("info_get"); + LUA_RETURN_EMPTY; + } + + info = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n < 1) + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("info_get"); + LUA_RETURN_EMPTY; + } + + info = lua_tostring (lua_current_interpreter, -1); + + value = weechat_info_get ((char *)info); + LUA_RETURN_STRING(value); +} + +/* + * weechat_lua_api_get_dcc_info: get infos about DCC + */ + +/* +static int +weechat_lua_api_get_dcc_info (lua_State *L) +{ + t_plugin_dcc_info *dcc_info, *ptr_dcc; + char timebuffer1[64]; + char timebuffer2[64]; + struct in_addr in; + int i; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get DCC info, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + dcc_info = lua_plugin->get_dcc_info (lua_plugin); + if (!dcc_info) + { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + for (i = 0, ptr_dcc = dcc_info; ptr_dcc; ptr_dcc = ptr_dcc->next_dcc, i++) + { + strftime(timebuffer1, sizeof(timebuffer1), "%F %T", + localtime(&ptr_dcc->start_time)); + strftime(timebuffer2, sizeof(timebuffer2), "%F %T", + localtime(&ptr_dcc->start_transfer)); + in.s_addr = htonl(ptr_dcc->addr); + + lua_pushnumber (lua_current_interpreter, i); + lua_newtable (lua_current_interpreter); + + lua_pushstring (lua_current_interpreter, "server"); + lua_pushstring (lua_current_interpreter, ptr_dcc->server); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "channel"); + lua_pushstring (lua_current_interpreter, ptr_dcc->channel); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "type"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->type); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "status"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->status); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "start_time"); + lua_pushstring (lua_current_interpreter, timebuffer1); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "start_transfer"); + lua_pushstring (lua_current_interpreter, timebuffer2); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "address"); + lua_pushstring (lua_current_interpreter, inet_ntoa(in)); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "port"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->port); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick"); + lua_pushstring (lua_current_interpreter, ptr_dcc->nick); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "remote_file"); + lua_pushstring (lua_current_interpreter, ptr_dcc->filename); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "local_file"); + lua_pushstring (lua_current_interpreter, ptr_dcc->local_filename); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "filename_suffix"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->filename_suffix); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "size"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->size); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "pos"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->pos); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "start_resume"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->start_resume); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "cps"); + lua_pushnumber (lua_current_interpreter, ptr_dcc->bytes_per_sec); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + } + + lua_plugin->free_dcc_info (lua_plugin, dcc_info); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_config: get value of a WeeChat config option + */ + +/* +static int +weechat_lua_api_get_config (lua_State *L) +{ + const char *option; + char *return_value; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get config option, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 1) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"get_config\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = lua_tostring (lua_current_interpreter, -1); + + return_value = lua_plugin->get_config (lua_plugin, (char *) option); + if (return_value) + lua_pushstring (lua_current_interpreter, return_value); + else + lua_pushstring (lua_current_interpreter, ""); + + return 1; +} +*/ + +/* + * weechat_lua_api_set_config: set value of a WeeChat config option + */ + +/* +static int +weechat_lua_api_set_config (lua_State *L) +{ + const char *option, *value; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to set config option, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = NULL; + value = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 2) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"set_config\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = lua_tostring (lua_current_interpreter, -2); + value = lua_tostring (lua_current_interpreter, -1); + + if (lua_plugin->set_config (lua_plugin, (char *) option, (char *) value)) + lua_pushnumber (lua_current_interpreter, 1); + else + lua_pushnumber (lua_current_interpreter, 0); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_plugin_config: get value of a plugin config option + */ + +/* +static int +weechat_lua_api_get_plugin_config (lua_State *L) +{ + const char *option; + char *return_value; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get plugin config option, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 1) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"get_plugin_config\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = lua_tostring (lua_current_interpreter, -1); + + return_value = weechat_script_get_plugin_config (lua_plugin, + lua_current_script, + (char *) option); + if (return_value) + lua_pushstring (lua_current_interpreter, return_value); + else + lua_pushstring (lua_current_interpreter, ""); + + return 1; +} +*/ + +/* + * weechat_lua_api_set_plugin_config: set value of a plugin config option + */ + +/* +static int +weechat_lua_api_set_plugin_config (lua_State *L) +{ + const char *option, *value; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to set plugin config option, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = NULL; + value = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 2) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"set_plugin_config\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + option = lua_tostring (lua_current_interpreter, -2); + value = lua_tostring (lua_current_interpreter, -1); + + if (weechat_script_set_plugin_config (lua_plugin, + lua_current_script, + (char *) option, (char *) value)) + lua_pushnumber (lua_current_interpreter, 1); + else + lua_pushnumber (lua_current_interpreter, 0); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_server_info: get infos about servers + */ + +/* +static int +weechat_lua_api_get_server_info (lua_State *L) +{ + t_plugin_server_info *server_info, *ptr_server; + char timebuffer[64]; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get server infos, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + server_info = lua_plugin->get_server_info (lua_plugin); + if (!server_info) { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + for (ptr_server = server_info; ptr_server; ptr_server = ptr_server->next_server) + { + strftime(timebuffer, sizeof(timebuffer), "%F %T", + localtime(&ptr_server->away_time)); + + lua_pushstring (lua_current_interpreter, ptr_server->name); + lua_newtable (lua_current_interpreter); + + lua_pushstring (lua_current_interpreter, "autoconnect"); + lua_pushnumber (lua_current_interpreter, ptr_server->autoconnect); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "autoreconnect"); + lua_pushnumber (lua_current_interpreter, ptr_server->autoreconnect); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "autoreconnect_delay"); + lua_pushnumber (lua_current_interpreter, ptr_server->autoreconnect_delay); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "temp_server"); + lua_pushnumber (lua_current_interpreter, ptr_server->temp_server); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "address"); + lua_pushstring (lua_current_interpreter, ptr_server->address); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "port"); + lua_pushnumber (lua_current_interpreter, ptr_server->port); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "ipv6"); + lua_pushnumber (lua_current_interpreter, ptr_server->ipv6); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "ssl"); + lua_pushnumber (lua_current_interpreter, ptr_server->ssl); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "password"); + lua_pushstring (lua_current_interpreter, ptr_server->password); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick1"); + lua_pushstring (lua_current_interpreter, ptr_server->nick1); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick2"); + lua_pushstring (lua_current_interpreter, ptr_server->nick2); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick3"); + lua_pushstring (lua_current_interpreter, ptr_server->nick3); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "username"); + lua_pushstring (lua_current_interpreter, ptr_server->username); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "realname"); + lua_pushstring (lua_current_interpreter, ptr_server->realname); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "command"); + lua_pushstring (lua_current_interpreter, ptr_server->command); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "command_delay"); + lua_pushnumber (lua_current_interpreter, ptr_server->command_delay); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "autojoin"); + lua_pushstring (lua_current_interpreter, ptr_server->autojoin); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "autorejoin"); + lua_pushnumber (lua_current_interpreter, ptr_server->autorejoin); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "notify_levels"); + lua_pushstring (lua_current_interpreter, ptr_server->notify_levels); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "is_connected"); + lua_pushnumber (lua_current_interpreter, ptr_server->is_connected); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "ssl_connected"); + lua_pushnumber (lua_current_interpreter, ptr_server->ssl_connected); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick"); + lua_pushstring (lua_current_interpreter, ptr_server->nick); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick_modes"); + lua_pushstring (lua_current_interpreter, ptr_server->nick_modes); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "away_time"); + lua_pushstring (lua_current_interpreter, timebuffer); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "lag"); + lua_pushnumber (lua_current_interpreter, ptr_server->lag); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + } + + lua_plugin->free_server_info(lua_plugin, server_info); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_channel_info: get infos about channels + */ + +/* +static int +weechat_lua_api_get_channel_info (lua_State *L) +{ + t_plugin_channel_info *channel_info, *ptr_channel; + const char *server; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get channel infos, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + server = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 1) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"get_channel_info\" function"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + server = lua_tostring (lua_current_interpreter, -1); + + channel_info = lua_plugin->get_channel_info (lua_plugin, (char *) server); + if (!channel_info) + { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + for (ptr_channel = channel_info; ptr_channel; ptr_channel = ptr_channel->next_channel) + { + lua_pushstring (lua_current_interpreter, ptr_channel->name); + lua_newtable (lua_current_interpreter); + + lua_pushstring (lua_current_interpreter, "type"); + lua_pushnumber (lua_current_interpreter, ptr_channel->type); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "topic"); + lua_pushstring (lua_current_interpreter, ptr_channel->topic); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "modes"); + lua_pushstring (lua_current_interpreter, ptr_channel->modes); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "limit"); + lua_pushnumber (lua_current_interpreter, ptr_channel->limit); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "key"); + lua_pushstring (lua_current_interpreter, ptr_channel->key); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nicks_count"); + lua_pushnumber (lua_current_interpreter, ptr_channel->nicks_count); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + } + + lua_plugin->free_channel_info(lua_plugin, channel_info); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_nick_info: get infos about nicks + */ + +/* +static int +weechat_lua_api_get_nick_info (lua_State *L) +{ + t_plugin_nick_info *nick_info, *ptr_nick; + const char *server, *channel; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get nick infos, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + server = NULL; + channel = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 2) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"get_nick_info\" function"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + server = lua_tostring (lua_current_interpreter, -2); + channel = lua_tostring (lua_current_interpreter, -1); + + nick_info = lua_plugin->get_nick_info (lua_plugin, (char *) server, (char *) channel); + if (!nick_info) + { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + for(ptr_nick = nick_info; ptr_nick; ptr_nick = ptr_nick->next_nick) + { + lua_pushstring (lua_current_interpreter, ptr_nick->nick); + lua_newtable (lua_current_interpreter); + + lua_pushstring (lua_current_interpreter, "flags"); + lua_pushnumber (lua_current_interpreter, ptr_nick->flags); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "host"); + lua_pushstring (lua_current_interpreter, + ptr_nick->host ? ptr_nick->host : ""); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + } + + lua_plugin->free_nick_info(lua_plugin, nick_info); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_irc_color: + * get the numeric value which identify an irc color by its name + */ + +/* +static int +weechat_lua_api_get_irc_color (lua_State *L) +{ + const char *color; + int n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get irc color, " + "script not initialized"); + lua_pushnumber (lua_current_interpreter, -1); + return 1; + } + + color = NULL; + + n = lua_gettop (lua_current_interpreter); + + if (n != 1) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"get_irc_color\" function"); + lua_pushnumber (lua_current_interpreter, -1); + return 1; + } + + color = lua_tostring (lua_current_interpreter, -1); + + lua_pushnumber (lua_current_interpreter, + lua_plugin->get_irc_color (lua_plugin, (char *) color)); + return 1; +} +*/ + +/* + * weechat_lua_api_get_window_info: get infos about windows + */ + +/* +static int +weechat_lua_api_get_window_info (lua_State *L) +{ + t_plugin_window_info *window_info, *ptr_win; + int i; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get window info, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + window_info = lua_plugin->get_window_info (lua_plugin); + if (!window_info) + { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + i = 0; + for (ptr_win = window_info; ptr_win; ptr_win = ptr_win->next_window) + { + lua_pushnumber (lua_current_interpreter, i); + lua_newtable (lua_current_interpreter); + + lua_pushstring (lua_current_interpreter, "num_buffer"); + lua_pushnumber (lua_current_interpreter, ptr_win->num_buffer); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "win_x"); + lua_pushnumber (lua_current_interpreter, ptr_win->win_x); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "win_y"); + lua_pushnumber (lua_current_interpreter, ptr_win->win_y); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "win_width"); + lua_pushnumber (lua_current_interpreter, ptr_win->win_width); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "win_height"); + lua_pushnumber (lua_current_interpreter, ptr_win->win_height); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "win_width_pct"); + lua_pushnumber (lua_current_interpreter, ptr_win->win_width_pct); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "win_height_pct"); + lua_pushnumber (lua_current_interpreter, ptr_win->win_height_pct); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + + i++; + } + + lua_plugin->free_window_info (lua_plugin, window_info); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_buffer_info: get infos about buffers + */ + +/* +static int +weechat_lua_api_get_buffer_info (lua_State *L) +{ + t_plugin_buffer_info *buffer_info, *ptr_buffer; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get buffer info, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + buffer_info = lua_plugin->get_buffer_info (lua_plugin); + if (!buffer_info) { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + for (ptr_buffer = buffer_info; ptr_buffer; ptr_buffer = ptr_buffer->next_buffer) + { + lua_pushnumber (lua_current_interpreter, ptr_buffer->number); + lua_newtable (lua_current_interpreter); + + 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); + + lua_pushstring (lua_current_interpreter, "server"); + lua_pushstring (lua_current_interpreter, + ptr_buffer->server_name == NULL ? "" : ptr_buffer->server_name); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "channel"); + lua_pushstring (lua_current_interpreter, + ptr_buffer->channel_name == NULL ? "" : ptr_buffer->channel_name); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "notify_level"); + lua_pushnumber (lua_current_interpreter, ptr_buffer->notify_level); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "log_filename"); + lua_pushstring (lua_current_interpreter, + ptr_buffer->log_filename == NULL ? "" : ptr_buffer->log_filename); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + } + + lua_plugin->free_buffer_info(lua_plugin, buffer_info); + + return 1; +} +*/ + +/* + * weechat_lua_api_get_buffer_data: get buffer content + */ + +/* +static int +weechat_lua_api_get_buffer_data (lua_State *L) +{ + t_plugin_buffer_line *buffer_data, *ptr_data; + const char *server, *channel; + char timebuffer[64]; + int i, n; + + // make C compiler happy + (void) L; + + if (!lua_current_script) + { + lua_plugin->print_server (lua_plugin, + "Lua error: unable to get buffer data, " + "script not initialized"); + lua_pushnil (lua_current_interpreter); + return 1; + } + + server = NULL; + channel = NULL; + + n = lua_gettop (lua_current_interpreter); + if (n != 2) + { + lua_plugin->print_server (lua_plugin, + "Lua error: wrong parameters for " + "\"get_buffer_data\" function"); + lua_pushnumber (lua_current_interpreter, 0); + return 1; + } + + server = lua_tostring (lua_current_interpreter, -2); + channel = lua_tostring (lua_current_interpreter, -1); + + buffer_data = lua_plugin->get_buffer_data (lua_plugin, (char *) server, (char *) channel); + if (!buffer_data) + { + lua_pushboolean (lua_current_interpreter, 0); + return 1; + } + + lua_newtable (lua_current_interpreter); + + for (i = 0, ptr_data = buffer_data; ptr_data; ptr_data = ptr_data->next_line, i++) + { + lua_pushnumber (lua_current_interpreter, i); + lua_newtable (lua_current_interpreter); + + strftime(timebuffer, sizeof(timebuffer), "%F %T", + localtime(&ptr_data->date)); + + lua_pushstring (lua_current_interpreter, "date"); + lua_pushstring (lua_current_interpreter, timebuffer); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "nick"); + lua_pushstring (lua_current_interpreter, + ptr_data->nick == NULL ? "" : ptr_data->nick); + lua_rawset (lua_current_interpreter, -3); + + lua_pushstring (lua_current_interpreter, "data"); + lua_pushstring (lua_current_interpreter, + ptr_data->data == NULL ? "" : ptr_data->data); + lua_rawset (lua_current_interpreter, -3); + + lua_rawset (lua_current_interpreter, -3); + } + + lua_plugin->free_buffer_data (lua_plugin, buffer_data); + + return 1; +} +*/ + +/* + * Lua constant as functions + */ + +static int +weechat_lua_api_constant_weechat_rc_ok (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushnumber (lua_current_interpreter, WEECHAT_RC_OK); + return 1; +} + +static int +weechat_lua_api_constant_weechat_rc_error (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushnumber (lua_current_interpreter, WEECHAT_RC_ERROR); + return 1; +} + +static int +weechat_lua_api_constant_weechat_rc_ok_ignore_weechat (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushnumber (lua_current_interpreter, WEECHAT_RC_OK_IGNORE_WEECHAT); + return 1; +} + +static int +weechat_lua_api_constant_weechat_rc_ok_ignore_plugins (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushnumber (lua_current_interpreter, WEECHAT_RC_OK_IGNORE_PLUGINS); + return 1; +} + +static int +weechat_lua_api_constant_weechat_rc_ok_ignore_all (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushnumber (lua_current_interpreter, WEECHAT_RC_OK_IGNORE_ALL); + return 1; +} + +static int +weechat_lua_api_constant_weechat_rc_ok_with_highlight (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushnumber (lua_current_interpreter, WEECHAT_RC_OK_WITH_HIGHLIGHT); + return 1; +} + +static int +weechat_lua_api_constant_weechat_list_pos_sort (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_LIST_POS_SORT); + return 1; +} + +static int +weechat_lua_api_constant_weechat_list_pos_beginning (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_LIST_POS_BEGINNING); + return 1; +} + +static int +weechat_lua_api_constant_weechat_list_pos_end (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_LIST_POS_END); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hotlist_low (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOTLIST_LOW); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hotlist_message (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOTLIST_MESSAGE); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hotlist_private (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOTLIST_PRIVATE); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hotlist_highlight (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOTLIST_HIGHLIGHT); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hook_signal_string (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOOK_SIGNAL_STRING); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hook_signal_int (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOOK_SIGNAL_INT); + return 1; +} + +static int +weechat_lua_api_constant_weechat_hook_signal_pointer (lua_State *L) +{ + /* make C compiler happy */ + (void) L; + + lua_pushstring (lua_current_interpreter, WEECHAT_HOOK_SIGNAL_POINTER); + return 1; +} + +/* + * Lua subroutines + */ + +const struct luaL_reg weechat_lua_api_funcs[] = { + { "register", &weechat_lua_api_register }, + { "charset_set", &weechat_lua_api_charset_set }, + { "iconv_to_internal", &weechat_lua_api_iconv_to_internal }, + { "iconv_from_internal", &weechat_lua_api_iconv_from_internal }, + { "mkdir_home", &weechat_lua_api_mkdir_home }, + { "mkdir", &weechat_lua_api_mkdir }, + { "prefix", &weechat_lua_api_prefix }, + { "color", &weechat_lua_api_color }, + { "print", &weechat_lua_api_print }, + { "infobar_print", &weechat_lua_api_infobar_print }, + { "infobar_remove", &weechat_lua_api_infobar_remove }, + { "log_print", &weechat_lua_api_log_print }, + { "hook_command", &weechat_lua_api_hook_command }, + { "hook_timer", &weechat_lua_api_hook_timer }, + { "hook_fd", &weechat_lua_api_hook_fd }, + { "hook_print", &weechat_lua_api_hook_print }, + { "hook_signal", &weechat_lua_api_hook_signal }, + { "hook_signal_send", &weechat_lua_api_hook_signal_send }, + { "hook_config", &weechat_lua_api_hook_config }, + { "hook_completion", &weechat_lua_api_hook_completion }, + { "unhook", &weechat_lua_api_unhook }, + { "unhook_all", &weechat_lua_api_unhook_all }, + { "buffer_new", &weechat_lua_api_buffer_new }, + { "buffer_search", &weechat_lua_api_buffer_search }, + { "buffer_close", &weechat_lua_api_buffer_close }, + { "buffer_get", &weechat_lua_api_buffer_get }, + { "buffer_set", &weechat_lua_api_buffer_set }, + { "nicklist_add_group", &weechat_lua_api_nicklist_add_group }, + { "nicklist_search_group", &weechat_lua_api_nicklist_search_group }, + { "nicklist_add_nick", &weechat_lua_api_nicklist_add_nick }, + { "nicklist_search_nick", &weechat_lua_api_nicklist_search_nick }, + { "nicklist_remove_group", &weechat_lua_api_nicklist_remove_group }, + { "nicklist_remove_nick", &weechat_lua_api_nicklist_remove_nick }, + { "nicklist_remove_all", &weechat_lua_api_nicklist_remove_all }, + { "command", &weechat_lua_api_command }, + { "info_get", &weechat_lua_api_info_get }, + //{ "get_dcc_info", &weechat_lua_api_get_dcc_info }, + //{ "get_config", &weechat_lua_api_get_config }, + //{ "set_config", &weechat_lua_api_set_config }, + //{ "get_plugin_config", &weechat_lua_api_get_plugin_config }, + //{ "set_plugin_config", &weechat_lua_api_set_plugin_config }, + //{ "get_server_info", &weechat_lua_api_get_server_info }, + //{ "get_channel_info", &weechat_lua_api_get_channel_info }, + //{ "get_nick_info", &weechat_lua_api_get_nick_info }, + //{ "get_irc_color", &weechat_lua_api_get_irc_color }, + //{ "get_window_info", &weechat_lua_api_get_window_info }, + //{ "get_buffer_info", &weechat_lua_api_get_buffer_info }, + //{ "get_buffer_data", &weechat_lua_api_get_buffer_data }, + /* define constants as function which returns values */ + { "WEECHAT_RC_OK", &weechat_lua_api_constant_weechat_rc_ok }, + { "WEECHAT_RC_ERROR", &weechat_lua_api_constant_weechat_rc_error }, + { "WEECHAT_RC_OK_IGNORE_WEECHAT", &weechat_lua_api_constant_weechat_rc_ok_ignore_weechat }, + { "WEECHAT_RC_OK_IGNORE_PLUGINS", &weechat_lua_api_constant_weechat_rc_ok_ignore_plugins }, + { "WEECHAT_RC_OK_IGNORE_ALL", &weechat_lua_api_constant_weechat_rc_ok_ignore_all }, + { "WEECHAT_RC_OK_WITH_HIGHLIGHT", &weechat_lua_api_constant_weechat_rc_ok_with_highlight }, + { "WEECHAT_LIST_POS_SORT", &weechat_lua_api_constant_weechat_list_pos_sort }, + { "WEECHAT_LIST_POS_BEGINNING", &weechat_lua_api_constant_weechat_list_pos_beginning }, + { "WEECHAT_LIST_POS_END", &weechat_lua_api_constant_weechat_list_pos_end }, + { "WEECHAT_HOTLIST_LOW", &weechat_lua_api_constant_weechat_hotlist_low }, + { "WEECHAT_HOTLIST_MESSAGE", &weechat_lua_api_constant_weechat_hotlist_message }, + { "WEECHAT_HOTLIST_PRIVATE", &weechat_lua_api_constant_weechat_hotlist_private }, + { "WEECHAT_HOTLIST_HIGHLIGHT", &weechat_lua_api_constant_weechat_hotlist_highlight }, + { "WEECHAT_HOOK_SIGNAL_STRING", &weechat_lua_api_constant_weechat_hook_signal_string }, + { "WEECHAT_HOOK_SIGNAL_INT", &weechat_lua_api_constant_weechat_hook_signal_int }, + { "WEECHAT_HOOK_SIGNAL_POINTER", &weechat_lua_api_constant_weechat_hook_signal_pointer }, + { NULL, NULL } +}; diff --git a/src/plugins/scripts/lua/weechat-lua-api.h b/src/plugins/scripts/lua/weechat-lua-api.h new file mode 100644 index 000000000..f8b366a58 --- /dev/null +++ b/src/plugins/scripts/lua/weechat-lua-api.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2003-2008 by FlashCode + * See README for License detail, AUTHORS for developers list. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef __WEECHAT_LUA_API_H +#define __WEECHAT_LUA_API_H 1 + +extern struct luaL_reg weechat_lua_api_funcs[]; + +#endif /* weechat-lua.h */ diff --git a/src/plugins/scripts/lua/weechat-lua.c b/src/plugins/scripts/lua/weechat-lua.c new file mode 100644 index 000000000..17f0a1471 --- /dev/null +++ b/src/plugins/scripts/lua/weechat-lua.c @@ -0,0 +1,598 @@ +/* + * Copyright (c) 2003-2008 by FlashCode + * See README for License detail, AUTHORS for developers list. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* weechat-lua.c: Lua plugin for WeeChat */ + +#undef _ + +#include +#include +#include + +//#include +#include +#include +//#include +//#include +//#include +//#include +//#include + +#include "../../weechat-plugin.h" +#include "../script.h" +#include "weechat-lua.h" +#include "weechat-lua-api.h" + + +WEECHAT_PLUGIN_NAME("lua"); +WEECHAT_PLUGIN_DESCRIPTION("Lua plugin for WeeChat"); +WEECHAT_PLUGIN_AUTHOR("FlashCode "); +WEECHAT_PLUGIN_VERSION("0.1"); +WEECHAT_PLUGIN_LICENSE("GPL"); + +struct t_weechat_plugin *weechat_lua_plugin; + +struct t_plugin_script *lua_scripts = NULL; +struct t_plugin_script *lua_current_script = NULL; +char *lua_current_script_filename = NULL; +lua_State *lua_current_interpreter = NULL; + + +/* + * weechat_lua_exec: execute a Lua script + */ + +void * +weechat_lua_exec (struct t_plugin_script *script, + int ret_type, char *function, char **argv) +{ + void *ret_value; + int argc, *ret_i; + + lua_current_interpreter = script->interpreter; + + lua_getglobal (lua_current_interpreter, function); + lua_current_script = script; + + if (argv && argv[0]) + { + lua_pushstring (lua_current_interpreter, argv[0]); + argc = 1; + if (argv[1]) + { + argc = 2; + lua_pushstring (lua_current_interpreter, argv[1]); + if (argv[2]) + { + argc = 3; + lua_pushstring (lua_current_interpreter, argv[2]); + if (argv[3]) + { + argc = 4; + lua_pushstring (lua_current_interpreter, argv[3]); + if (argv[4]) + { + argc = 5; + lua_pushstring (lua_current_interpreter, argv[4]); + } + } + } + } + } + + if (lua_pcall (lua_current_interpreter, argc, 1, 0) != 0) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unable to run function \"%s\""), + weechat_prefix ("error"), "lua", function); + weechat_printf (NULL, + weechat_gettext ("%s%s: error: %s"), + weechat_prefix ("error"), "lua", + lua_tostring (lua_current_interpreter, -1)); + return NULL; + } + + if (ret_type == WEECHAT_SCRIPT_EXEC_STRING) + ret_value = strdup ((char *) lua_tostring (lua_current_interpreter, -1)); + else if (ret_type == WEECHAT_SCRIPT_EXEC_INT) + { + ret_i = (int *)malloc (sizeof(int)); + if (ret_i) + *ret_i = lua_tonumber (lua_current_interpreter, -1); + ret_value = ret_i; + } + else + { + WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS(function); + return NULL; + } + + return ret_value; +} + +int +weechat_lua_load (char *filename) +{ + FILE *fp; + char *weechat_lua_code = { + "weechat_outputs = {\n" + " write = function (self, str)\n" + " weechat.print(\"Lua stdout/stderr : \" .. str)\n" + " end\n" + "}\n" + "io.stdout = weechat_outputs\n" + "io.stderr = weechat_outputs\n" + }; + + if ((fp = fopen (filename, "r")) == NULL) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: script \"%s\" not found"), + weechat_prefix ("error"), "lua", filename); + return 0; + } + + weechat_printf (NULL, + weechat_gettext ("%s%s: loading script \"%s\""), + weechat_prefix ("info"), "lua", filename); + + lua_current_script = NULL; + + lua_current_interpreter = lua_open (); + + if (lua_current_interpreter == NULL) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unable to create new " + "sub-interpreter"), + weechat_prefix ("error"), "lua"); + fclose (fp); + return 0; + } + +#ifdef LUA_VERSION_NUM /* LUA_VERSION_NUM is defined only in lua >= 5.1.0 */ + luaL_openlibs (lua_current_interpreter); +#else + luaopen_base (lua_current_interpreter); + luaopen_string (lua_current_interpreter); + luaopen_table (lua_current_interpreter); + luaopen_math (lua_current_interpreter); + luaopen_io (lua_current_interpreter); + luaopen_debug (lua_current_interpreter); +#endif + + luaL_openlib (lua_current_interpreter, "weechat", weechat_lua_api_funcs, 0); + +#ifdef LUA_VERSION_NUM + if (luaL_dostring (lua_current_interpreter, weechat_lua_code) != 0) +#else + if (lua_dostring (lua_current_interpreter, weechat_lua_code) != 0) +#endif + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unable to redirect stdout " + "and stderr"), + weechat_prefix ("error"), "lua"); + } + + lua_current_script_filename = filename; + + if (luaL_loadfile (lua_current_interpreter, filename) != 0) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unable to load file \"%s\""), + weechat_prefix ("error"), "lua", filename); + weechat_printf (NULL, + weechat_gettext ("%s%s: error: %s"), + weechat_prefix ("error"), "lua", + lua_tostring (lua_current_interpreter, -1)); + lua_close (lua_current_interpreter); + fclose (fp); + return 0; + } + + if (lua_pcall (lua_current_interpreter, 0, 0, 0) != 0) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unable to execute file " + "\"%s\""), + weechat_prefix ("error"), "lua", filename); + weechat_printf (NULL, + weechat_gettext ("%s%s: error: %s"), + weechat_prefix ("error"), "lua", + lua_tostring (lua_current_interpreter, -1)); + lua_close (lua_current_interpreter); + fclose (fp); + /* if script was registered, removing from list */ + if (lua_current_script) + script_remove (weechat_lua_plugin, &lua_scripts, lua_current_script); + return 0; + } + fclose (fp); + + if (lua_current_script == NULL) + { + weechat_printf (NULL, + weechat_gettext ("%s%s: function \"register\" not " + "found (or failed) in file \"%s\""), + weechat_prefix ("error"), "lua", filename); + lua_close (lua_current_interpreter); + return 0; + } + + lua_current_script->interpreter = (lua_State *) lua_current_interpreter; + + return 1; +} + +/* + * weechat_lua_load_cb: callback for weechat_script_auto_load() function + */ + +int +weechat_lua_load_cb (void *data, char *filename) +{ + /* make C compiler happy */ + (void) data; + + return weechat_lua_load (filename); +} + +/* + * weechat_lua_unload: unload a Lua script + */ + +void +weechat_lua_unload (struct t_plugin_script *script) +{ + int *r; + char *lua_argv[1] = { NULL }; + + weechat_printf (NULL, + weechat_gettext ("%s%s: unloading script \"%s\""), + weechat_prefix ("info"), "lua", script->name); + + if (script->shutdown_func && script->shutdown_func[0]) + { + r = weechat_lua_exec (script, + WEECHAT_SCRIPT_EXEC_INT, + script->shutdown_func, + lua_argv); + if (r) + free (r); + } + + lua_close (script->interpreter); + + script_remove (weechat_lua_plugin, &lua_scripts, script); +} + +/* + * weechat_lua_unload_name: unload a Lua script by name + */ + +void +weechat_lua_unload_name (char *name) +{ + struct t_plugin_script *ptr_script; + + ptr_script = script_search (weechat_lua_plugin, lua_scripts, name); + if (ptr_script) + { + weechat_lua_unload (ptr_script); + weechat_printf (NULL, + weechat_gettext ("%s%s: script \"%s\" unloaded"), + weechat_prefix ("info"), "lua", name); + } + else + { + weechat_printf (NULL, + weechat_gettext ("%s%s: script \"%s\" not loaded"), + weechat_prefix ("error"), "lua", name); + } +} + +/* + * weechat_lua_unload_all: unload all Lua scripts + */ + +void +weechat_lua_unload_all () +{ + while (lua_scripts) + { + weechat_lua_unload (lua_scripts); + } +} + +/* + * weechat_lua_command_cb: callback for "/lua" command + */ + +int +weechat_lua_command_cb (void *data, struct t_gui_buffer *buffer, + int argc, char **argv, char **argv_eol) +{ + //int handler_found, modifier_found; + char *path_script; + struct t_plugin_script *ptr_script; + //t_plugin_handler *ptr_handler; + //t_plugin_modifier *ptr_modifier; + + /* make C compiler happy */ + (void) data; + (void) buffer; + + if (argc == 1) + { + /* list registered Lua scripts */ + weechat_printf (NULL, ""); + weechat_printf (NULL, + weechat_gettext ("Registered %s scripts:"), + "lua"); + if (lua_scripts) + { + for (ptr_script = lua_scripts; ptr_script; + ptr_script = ptr_script->next_script) + { + weechat_printf (NULL, + weechat_gettext (" %s v%s (%s), by %s, " + "license %s"), + ptr_script->name, + ptr_script->version, + ptr_script->description, + ptr_script->author, + ptr_script->license); + } + } + else + weechat_printf (NULL, weechat_gettext (" (none)")); + + /* + // list Lua message handlers + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Lua message handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == PLUGIN_HANDLER_MESSAGE) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " IRC(%s) => Lua(%s)", + ptr_handler->irc_command, + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); + + // list Lua command handlers + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Lua command handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == PLUGIN_HANDLER_COMMAND) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " /%s => Lua(%s)", + ptr_handler->command, + 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 == PLUGIN_HANDLER_TIMER) + && (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)"); + + // list Lua keyboard handlers + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Lua keyboard handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == PLUGIN_HANDLER_KEYBOARD) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " Lua(%s)", + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); + + // list Lua event handlers + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Lua event handlers:"); + handler_found = 0; + for (ptr_handler = plugin->handlers; + ptr_handler; ptr_handler = ptr_handler->next_handler) + { + if ((ptr_handler->type == PLUGIN_HANDLER_EVENT) + && (ptr_handler->handler_args)) + { + handler_found = 1; + plugin->print_server (plugin, " %s => Lua(%s)", + ptr_handler->event, + ptr_handler->handler_args); + } + } + if (!handler_found) + plugin->print_server (plugin, " (none)"); + + // list Lua modifiers + plugin->print_server (plugin, ""); + plugin->print_server (plugin, "Lua modifiers:"); + modifier_found = 0; + for (ptr_modifier = plugin->modifiers; + ptr_modifier; ptr_modifier = ptr_modifier->next_modifier) + { + modifier_found = 1; + if (ptr_modifier->type == PLUGIN_MODIFIER_IRC_IN) + plugin->print_server (plugin, " IRC(%s, %s) => Lua(%s)", + ptr_modifier->command, + PLUGIN_MODIFIER_IRC_IN_STR, + ptr_modifier->modifier_args); + else if (ptr_modifier->type == PLUGIN_MODIFIER_IRC_USER) + plugin->print_server (plugin, " IRC(%s, %s) => Lua(%s)", + ptr_modifier->command, + PLUGIN_MODIFIER_IRC_USER_STR, + ptr_modifier->modifier_args); + else if (ptr_modifier->type == PLUGIN_MODIFIER_IRC_OUT) + plugin->print_server (plugin, " IRC(%s, %s) => Lua(%s)", + ptr_modifier->command, + PLUGIN_MODIFIER_IRC_OUT_STR, + ptr_modifier->modifier_args); + } + if (!modifier_found) + plugin->print_server (plugin, " (none)"); + */ + } + else if (argc == 2) + { + if (weechat_strcasecmp (argv[1], "autoload") == 0) + script_auto_load (weechat_lua_plugin, + "lua", &weechat_lua_load_cb); + else if (weechat_strcasecmp (argv[1], "reload") == 0) + { + weechat_lua_unload_all (); + script_auto_load (weechat_lua_plugin, + "lua", &weechat_lua_load_cb); + } + else if (weechat_strcasecmp (argv[1], "unload") == 0) + { + weechat_lua_unload_all (); + } + } + else + { + if (weechat_strcasecmp (argv[1], "load") == 0) + { + /* load Lua script */ + path_script = script_search_full_name (weechat_lua_plugin, + "lua", argv_eol[2]); + weechat_lua_load ((path_script) ? path_script : argv_eol[2]); + if (path_script) + free (path_script); + } + else if (weechat_strcasecmp (argv[1], "unload") == 0) + { + /* unload Lua script */ + weechat_lua_unload_name (argv_eol[2]); + } + else + { + weechat_printf (NULL, + weechat_gettext ("%s%s: unknown option for " + "command \"%s\""), + weechat_prefix ("error"), "lua", "lua"); + } + } + + return WEECHAT_RC_OK; +} + +/* + * weechat_lua_dump_data_cb: dump Lua plugin data in WeeChat log file + */ + +int +weechat_lua_dump_data_cb (void *data, char *signal, char *type_data, + void *signal_data) +{ + /* make C compiler happy */ + (void) data; + (void) signal; + (void) type_data; + (void) signal_data; + + script_print_log (weechat_lua_plugin, lua_scripts); + + return WEECHAT_RC_OK; +} + +/* + * weechat_plugin_init: initialize Lua plugin + */ + +int +weechat_plugin_init (struct t_weechat_plugin *plugin) +{ + + weechat_lua_plugin = plugin; + + weechat_hook_command ("lua", + weechat_gettext ("list/load/unload Lua scripts"), + weechat_gettext ("[load filename] | [autoload] | " + "[reload] | [unload [script]]"), + weechat_gettext ("filename: Lua script (file) to " + "load\n" + "script: script name to unload\n\n" + "Without argument, /lua command " + "lists all loaded Lua scripts."), + "load|autoload|reload|unload %f", + &weechat_lua_command_cb, NULL); + + weechat_mkdir_home ("lua", 0644); + weechat_mkdir_home ("lua/autoload", 0644); + + weechat_hook_signal ("dump_data", &weechat_lua_dump_data_cb, NULL); + + script_init (weechat_lua_plugin); + script_auto_load (weechat_lua_plugin, "lua", &weechat_lua_load_cb); + + /* init ok */ + return WEECHAT_RC_OK; +} + +/* + * weechat_plugin_end: shutdown Lua interface + */ + +void +weechat_plugin_end (struct t_weechat_plugin *plugin) +{ + /* make C compiler happy */ + (void) plugin; + + /* unload all scripts */ + weechat_lua_unload_all (); +} diff --git a/src/plugins/scripts/lua/weechat-lua.h b/src/plugins/scripts/lua/weechat-lua.h new file mode 100644 index 000000000..2630fa99d --- /dev/null +++ b/src/plugins/scripts/lua/weechat-lua.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2003-2008 by FlashCode + * See README for License detail, AUTHORS for developers list. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef __WEECHAT_LUA_H +#define __WEECHAT_LUA_H 1 + +#define weechat_plugin weechat_lua_plugin + +extern struct t_weechat_plugin *weechat_lua_plugin; + +extern struct t_plugin_script *lua_scripts; +extern struct t_plugin_script *lua_current_script; +extern char *lua_current_script_filename; +extern lua_State *lua_current_interpreter; + +extern void * weechat_lua_exec (struct t_plugin_script *script, + int ret_type, char *function, char **argv); + +#endif /* weechat-lua.h */ diff --git a/src/plugins/scripts/perl/weechat-perl-api.c b/src/plugins/scripts/perl/weechat-perl-api.c index 749f272e3..27c50e5a7 100644 --- a/src/plugins/scripts/perl/weechat-perl-api.c +++ b/src/plugins/scripts/perl/weechat-perl-api.c @@ -45,7 +45,7 @@ XSRETURN (1); \ } \ XST_mPV (0, ""); \ - XSRETURN (1); + XSRETURN (1) #define PERL_RETURN_STRING_FREE(__string) \ if (__string) \ { \ @@ -54,7 +54,7 @@ XSRETURN (1); \ } \ XST_mPV (0, ""); \ - XSRETURN (1); + XSRETURN (1) extern void boot_DynaLoader (pTHX_ CV* cv); @@ -328,7 +328,7 @@ static XS (XS_weechat_color) } /* - * weechat::print: print message into a buffer (current or specified one) + * weechat::print: print message in a buffer */ static XS (XS_weechat_print) @@ -828,7 +828,7 @@ static XS (XS_weechat_hook_signal) static XS (XS_weechat_hook_signal_send) { char *type_data; - int int_value; + int number; dXSARGS; /* make C compiler happy */ @@ -856,10 +856,10 @@ static XS (XS_weechat_hook_signal_send) } else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_INT) == 0) { - int_value = SvIV(ST (2)); + number = SvIV(ST (2)); weechat_hook_signal_send (SvPV (ST (0), PL_na), /* signal */ type_data, - &int_value); /* signal_data */ + &number); /* signal_data */ PERL_RETURN_OK; } else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_POINTER) == 0) diff --git a/src/plugins/scripts/perl/weechat-perl.c b/src/plugins/scripts/perl/weechat-perl.c index b3474de81..c34a80223 100644 --- a/src/plugins/scripts/perl/weechat-perl.c +++ b/src/plugins/scripts/perl/weechat-perl.c @@ -730,8 +730,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) weechat_hook_signal ("dump_data", &weechat_perl_dump_data_cb, NULL); script_init (weechat_perl_plugin); - script_auto_load (weechat_perl_plugin, - "perl", &weechat_perl_load_cb); + script_auto_load (weechat_perl_plugin, "perl", &weechat_perl_load_cb); /* init ok */ return WEECHAT_RC_OK; diff --git a/src/plugins/scripts/python/weechat-python-api.c b/src/plugins/scripts/python/weechat-python-api.c index c4ba6b21a..8a7ac6bf3 100644 --- a/src/plugins/scripts/python/weechat-python-api.c +++ b/src/plugins/scripts/python/weechat-python-api.c @@ -41,7 +41,7 @@ #define PYTHON_RETURN_STRING(__string) \ if (__string) \ return Py_BuildValue ("s", __string); \ - return Py_BuildValue ("s", ""); + return Py_BuildValue ("s", "") #define PYTHON_RETURN_STRING_FREE(__string) \ if (__string) \ { \ @@ -49,7 +49,7 @@ free (__string); \ return object; \ } \ - return Py_BuildValue ("s", ""); + return Py_BuildValue ("s", "") /* @@ -342,7 +342,7 @@ weechat_python_api_color (PyObject *self, PyObject *args) } /* - * weechat_python_api_prnt: print message into a buffer (current or specified one) + * weechat_python_api_prnt: print message in a buffer */ static PyObject * @@ -900,7 +900,7 @@ static PyObject * weechat_python_api_hook_signal_send (PyObject *self, PyObject *args) { char *signal, *type_data, *signal_data, *error; - long number; + int number; /* make C compiler happy */ (void) self; @@ -920,10 +920,11 @@ weechat_python_api_hook_signal_send (PyObject *self, PyObject *args) WEECHAT_SCRIPT_MSG_WRONG_ARGUMENTS("hook_signal_send"); PYTHON_RETURN_ERROR; } - + if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0) { weechat_hook_signal_send (signal, type_data, signal_data); + PYTHON_RETURN_OK; } else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_INT) == 0) { @@ -933,13 +934,16 @@ weechat_python_api_hook_signal_send (PyObject *self, PyObject *args) { weechat_hook_signal_send (signal, type_data, &number); } + PYTHON_RETURN_OK; } else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_POINTER) == 0) { weechat_hook_signal_send (signal, type_data, script_string_to_pointer (signal_data)); + PYTHON_RETURN_OK; } - PYTHON_RETURN_OK; + + PYTHON_RETURN_ERROR; } /* diff --git a/src/plugins/scripts/python/weechat-python.c b/src/plugins/scripts/python/weechat-python.c index f8779da03..81db742d4 100644 --- a/src/plugins/scripts/python/weechat-python.c +++ b/src/plugins/scripts/python/weechat-python.c @@ -767,8 +767,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) weechat_hook_signal ("dump_data", &weechat_python_dump_data_cb, NULL); script_init (weechat_python_plugin); - script_auto_load (weechat_python_plugin, - "python", &weechat_python_load_cb); + script_auto_load (weechat_python_plugin, "python", &weechat_python_load_cb); /* init ok */ return WEECHAT_RC_OK; diff --git a/src/plugins/scripts/ruby/weechat-ruby-api.c b/src/plugins/scripts/ruby/weechat-ruby-api.c index 1f244f59e..03bd324a5 100644 --- a/src/plugins/scripts/ruby/weechat-ruby-api.c +++ b/src/plugins/scripts/ruby/weechat-ruby-api.c @@ -45,7 +45,7 @@ #define RUBY_RETURN_STRING(__string) \ if (__string) \ return rb_str_new2 (__string); \ - return rb_str_new2 (""); + return rb_str_new2 ("") #define RUBY_RETURN_STRING_FREE(__string) \ if (__string) \ { \ @@ -53,7 +53,7 @@ free (__string); \ return return_value; \ } \ - return rb_str_new2 (""); + return rb_str_new2 ("") /* @@ -402,7 +402,7 @@ weechat_ruby_api_color (VALUE class, VALUE color) } /* - * weechat_ruby_api_print: print message into a buffer (current or specified one) + * weechat_ruby_api_print: print message in a buffer */ static VALUE @@ -1043,7 +1043,7 @@ weechat_ruby_api_hook_signal_send (VALUE class, VALUE signal, VALUE type_data, VALUE signal_data) { char *c_signal, *c_type_data, *c_signal_data; - int int_value; + int number; /* make C compiler happy */ (void) class; @@ -1080,8 +1080,8 @@ weechat_ruby_api_hook_signal_send (VALUE class, VALUE signal, VALUE type_data, else if (strcmp (c_type_data, WEECHAT_HOOK_SIGNAL_INT) == 0) { Check_Type (signal_data, T_STRING); - int_value = FIX2INT (signal_data); - weechat_hook_signal_send (c_signal, c_type_data, &int_value); + number = FIX2INT (signal_data); + weechat_hook_signal_send (c_signal, c_type_data, &number); RUBY_RETURN_OK; } else if (strcmp (c_type_data, WEECHAT_HOOK_SIGNAL_POINTER) == 0) @@ -1101,7 +1101,8 @@ weechat_ruby_api_hook_signal_send (VALUE class, VALUE signal, VALUE type_data, */ int -weechat_ruby_api_hook_config_cb (void *data, char *type, char *option, char *value) +weechat_ruby_api_hook_config_cb (void *data, char *type, char *option, + char *value) { struct t_script_callback *script_callback; char *ruby_argv[4]; @@ -1184,7 +1185,8 @@ weechat_ruby_api_hook_config (VALUE class, VALUE type, VALUE option, VALUE funct */ int -weechat_ruby_api_hook_completion_cb (void *data, char *completion, struct t_gui_buffer *buffer, +weechat_ruby_api_hook_completion_cb (void *data, char *completion, + struct t_gui_buffer *buffer, struct t_weelist *list) { struct t_script_callback *script_callback; @@ -1734,7 +1736,7 @@ weechat_ruby_api_nicklist_add_nick (VALUE class, VALUE buffer, VALUE group, script_string_to_pointer (c_group), c_name, c_color, - prefix, + char_prefix, c_prefix_color, c_visible); diff --git a/src/plugins/scripts/ruby/weechat-ruby.c b/src/plugins/scripts/ruby/weechat-ruby.c index 6e307e7b6..167cc4091 100644 --- a/src/plugins/scripts/ruby/weechat-ruby.c +++ b/src/plugins/scripts/ruby/weechat-ruby.c @@ -821,8 +821,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) weechat_hook_signal ("dump_data", &weechat_ruby_dump_data_cb, NULL); script_init (weechat_ruby_plugin); - script_auto_load (weechat_ruby_plugin, - "ruby", &weechat_ruby_load_cb); + script_auto_load (weechat_ruby_plugin, "ruby", &weechat_ruby_load_cb); /* init ok */ return WEECHAT_RC_OK;