2016-04-03 17:28:11 +03:00
|
|
|
/*
|
2016-04-04 04:38:22 +03:00
|
|
|
* Copyright (c) 2015-2016 mniip
|
2016-04-03 17:28:11 +03:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
|
|
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
* so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
|
|
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <gmodule.h>
|
|
|
|
|
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <hexchat-plugin.h>
|
|
|
|
|
2020-09-21 21:22:50 +03:00
|
|
|
#define WORD_ARRAY_LEN 32
|
2020-09-18 01:50:28 +03:00
|
|
|
|
2016-04-05 20:01:15 +03:00
|
|
|
static char plugin_name[] = "Lua";
|
2016-04-03 17:28:11 +03:00
|
|
|
static char plugin_description[] = "Lua scripting interface";
|
2016-04-05 20:01:15 +03:00
|
|
|
static char plugin_version[16] = "1.3";
|
2016-04-03 17:28:11 +03:00
|
|
|
|
|
|
|
static char console_tab[] = ">>lua<<";
|
|
|
|
static char command_help[] =
|
|
|
|
"Usage: /lua load <filename>\n"
|
|
|
|
" unload <filename>\n"
|
|
|
|
" reload <filename>\n"
|
|
|
|
" exec <code>\n"
|
|
|
|
" inject <filename> <code>\n"
|
|
|
|
" reset\n"
|
|
|
|
" list\n"
|
|
|
|
" console";
|
|
|
|
|
|
|
|
static char registry_field[] = "plugin";
|
|
|
|
|
|
|
|
static hexchat_plugin *ph;
|
|
|
|
|
|
|
|
#if LUA_VERSION_NUM < 502
|
|
|
|
#define lua_rawlen lua_objlen
|
|
|
|
#define luaL_setfuncs(L, r, n) luaL_register(L, NULL, r)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
hexchat_hook *hook;
|
|
|
|
lua_State *state;
|
|
|
|
int ref;
|
|
|
|
}
|
|
|
|
hook_info;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *description;
|
|
|
|
char *version;
|
|
|
|
hexchat_plugin *handle;
|
|
|
|
char *filename;
|
|
|
|
lua_State *state;
|
2016-04-04 15:51:03 +03:00
|
|
|
GPtrArray *hooks;
|
|
|
|
GPtrArray *unload_hooks;
|
2016-04-03 17:28:11 +03:00
|
|
|
int traceback;
|
|
|
|
int status;
|
|
|
|
}
|
|
|
|
script_info;
|
|
|
|
|
|
|
|
#define STATUS_ACTIVE 1
|
|
|
|
#define STATUS_DEFERRED_UNLOAD 2
|
|
|
|
#define STATUS_DEFERRED_RELOAD 4
|
|
|
|
|
|
|
|
static void check_deferred(script_info *info);
|
|
|
|
|
|
|
|
static inline script_info *get_info(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
script_info *info;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, registry_field);
|
2016-04-03 18:04:48 +03:00
|
|
|
info = lua_touserdata(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 1);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_register(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
char const *name, *version, *description;
|
2016-04-03 17:28:11 +03:00
|
|
|
script_info *info = get_info(L);
|
|
|
|
if(info->name)
|
|
|
|
return luaL_error(L, "script is already registered as '%s'", info->name);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
|
|
|
name = luaL_checkstring(L, 1);
|
|
|
|
version = luaL_checkstring(L, 2);
|
|
|
|
description = luaL_checkstring(L, 3);
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
info->name = g_strdup(name);
|
|
|
|
info->description = g_strdup(description);
|
|
|
|
info->version = g_strdup(version);
|
|
|
|
info->handle = hexchat_plugingui_add(ph, info->filename, info->name, info->description, info->version, NULL);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_command(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_command(ph, luaL_checkstring(L, 1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tostring(lua_State *L, int n)
|
|
|
|
{
|
|
|
|
luaL_checkany(L, n);
|
|
|
|
switch (lua_type(L, n))
|
|
|
|
{
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
lua_pushstring(L, lua_tostring(L, n));
|
|
|
|
break;
|
|
|
|
case LUA_TSTRING:
|
|
|
|
lua_pushvalue(L, n);
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
lua_pushstring(L, (lua_toboolean(L, n) ? "true" : "false"));
|
|
|
|
break;
|
|
|
|
case LUA_TNIL:
|
|
|
|
lua_pushliteral(L, "nil");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_pushfstring(L, "%s: %p", luaL_typename(L, n), lua_topointer(L, n));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_print(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
int i, args = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
for(i = 1; i <= args; i++)
|
|
|
|
{
|
|
|
|
if(i != 1)
|
|
|
|
luaL_addstring(&b, " ");
|
|
|
|
tostring(L, i);
|
|
|
|
luaL_addvalue(&b);
|
|
|
|
}
|
|
|
|
luaL_pushresult(&b);
|
|
|
|
hexchat_print(ph, lua_tostring(L, -1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_emit_print(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_emit_print(ph, luaL_checkstring(L, 1), luaL_optstring(L, 2, NULL), luaL_optstring(L, 3, NULL), luaL_optstring(L, 4, NULL), luaL_optstring(L, 5, NULL), luaL_optstring(L, 6, NULL), NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_emit_print_attrs(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_event_attrs *attrs = *(hexchat_event_attrs **)luaL_checkudata(L, 1, "attrs");
|
|
|
|
hexchat_emit_print_attrs(ph, attrs, luaL_checkstring(L, 2), luaL_optstring(L, 3, NULL), luaL_optstring(L, 4, NULL), luaL_optstring(L, 5, NULL), luaL_optstring(L, 6, NULL), luaL_optstring(L, 7, NULL), NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_send_modes(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
size_t i, n;
|
|
|
|
int modes;
|
|
|
|
char const *mode;
|
|
|
|
char const **targets;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
2016-04-03 18:04:48 +03:00
|
|
|
n = lua_rawlen(L, 1);
|
|
|
|
mode = luaL_checkstring(L, 2);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(strlen(mode) != 2)
|
|
|
|
return luaL_argerror(L, 2, "expected sign followed by a mode letter");
|
2016-04-03 18:04:48 +03:00
|
|
|
modes = luaL_optinteger(L, 3, 0);
|
2016-04-04 05:53:29 +03:00
|
|
|
targets = g_new(char const *, n);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
lua_rawgeti(L, 1, i + 1);
|
|
|
|
if(lua_type(L, -1) != LUA_TSTRING)
|
|
|
|
{
|
2016-04-04 05:53:29 +03:00
|
|
|
g_free(targets);
|
2016-04-03 17:28:11 +03:00
|
|
|
return luaL_argerror(L, 1, "expected an array of strings");
|
|
|
|
}
|
|
|
|
targets[i] = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
hexchat_send_modes(ph, targets, n, modes, mode[0], mode[1]);
|
2016-04-04 05:53:29 +03:00
|
|
|
g_free(targets);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_nickcmp(lua_State *L)
|
|
|
|
{
|
2016-04-04 04:53:32 +03:00
|
|
|
lua_pushinteger(L, hexchat_nickcmp(ph, luaL_checkstring(L, 1), luaL_checkstring(L, 2)));
|
2016-04-03 17:28:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_strip(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
2016-04-03 18:04:48 +03:00
|
|
|
char const *text;
|
|
|
|
gboolean leave_colors, leave_attrs;
|
|
|
|
char *result;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
luaL_checktype(L, 1, LUA_TSTRING);
|
2016-04-03 18:04:48 +03:00
|
|
|
text = lua_tolstring(L, 1, &len);
|
|
|
|
leave_colors = lua_toboolean(L, 2);
|
|
|
|
leave_attrs = lua_toboolean(L, 3);
|
|
|
|
result = hexchat_strip(ph, text, len, (leave_colors ? 0 : 1) | (leave_attrs ? 0 : 2));
|
2016-04-03 17:28:11 +03:00
|
|
|
if(result)
|
|
|
|
{
|
|
|
|
lua_pushstring(L, result);
|
|
|
|
hexchat_free(ph, result);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_hook(hook_info *hook)
|
|
|
|
{
|
|
|
|
script_info *info = get_info(hook->state);
|
2016-04-04 15:51:03 +03:00
|
|
|
g_ptr_array_add(info->hooks, hook);
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_hook(hook_info *hook)
|
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
if(hook->state)
|
2016-04-04 15:51:03 +03:00
|
|
|
luaL_unref(hook->state, LUA_REGISTRYINDEX, hook->ref);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(hook->hook)
|
|
|
|
hexchat_unhook(ph, hook->hook);
|
2016-04-04 05:53:29 +03:00
|
|
|
g_free(hook);
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unregister_hook(hook_info *hook)
|
|
|
|
{
|
|
|
|
script_info *info = get_info(hook->state);
|
2016-04-04 15:51:03 +03:00
|
|
|
|
2016-04-04 23:40:45 +03:00
|
|
|
if(g_ptr_array_remove_fast(info->hooks, hook))
|
2016-04-04 15:51:03 +03:00
|
|
|
return 1;
|
|
|
|
|
2016-04-04 23:40:45 +03:00
|
|
|
if(g_ptr_array_remove_fast(info->unload_hooks, hook))
|
2016-04-04 15:51:03 +03:00
|
|
|
return 1;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_command_closure(char *word[], char *word_eol[], void *udata)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
int base, i, ret;
|
2016-04-03 17:28:11 +03:00
|
|
|
hook_info *info = udata;
|
|
|
|
lua_State *L = info->state;
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, script->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref);
|
|
|
|
lua_newtable(L);
|
2020-09-18 01:50:28 +03:00
|
|
|
for(i = 1; i < WORD_ARRAY_LEN && *word_eol[i]; i++)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
lua_pushstring(L, word[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
|
|
|
lua_newtable(L);
|
2020-09-18 01:50:28 +03:00
|
|
|
for(i = 1; i < WORD_ARRAY_LEN && *word_eol[i]; i++)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
lua_pushstring(L, word_eol[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
|
|
|
script->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 2, 1, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error in command hook: %s", error ? error : "(non-string error)");
|
|
|
|
check_deferred(script);
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
ret = lua_tointeger(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 2);
|
|
|
|
check_deferred(script);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_command(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
hook_info *info, **u;
|
|
|
|
char const *command, *help;
|
|
|
|
int ref, pri;
|
|
|
|
|
|
|
|
command = luaL_optstring(L, 1, "");
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 2);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
help = luaL_optstring(L, 3, NULL);
|
|
|
|
pri = luaL_optinteger(L, 4, HEXCHAT_PRI_NORM);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = hexchat_hook_command(ph, command, pri, api_command_closure, help, info);
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
register_hook(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_print_closure(char *word[], void *udata)
|
|
|
|
{
|
|
|
|
hook_info *info = udata;
|
|
|
|
lua_State *L = info->state;
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
int i, j, base, ret;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, script->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
for(j = 31; j >= 1; j--)
|
2016-04-05 20:05:54 +03:00
|
|
|
{
|
2016-04-03 17:28:11 +03:00
|
|
|
if(*word[j])
|
|
|
|
break;
|
2016-04-05 20:05:54 +03:00
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_newtable(L);
|
|
|
|
for(i = 1; i <= j; i++)
|
|
|
|
{
|
|
|
|
lua_pushstring(L, word[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
|
|
|
script->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 1, 1, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error in print hook: %s", error ? error : "(non-string error)");
|
|
|
|
check_deferred(script);
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
ret = lua_tointeger(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 2);
|
|
|
|
check_deferred(script);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_print(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *event = luaL_checkstring(L, 1);
|
2016-04-03 18:04:48 +03:00
|
|
|
hook_info *info, **u;
|
|
|
|
int ref, pri;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 2);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
pri = luaL_optinteger(L, 3, HEXCHAT_PRI_NORM);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = hexchat_hook_print(ph, event, pri, api_print_closure, info);
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
register_hook(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hexchat_event_attrs *event_attrs_copy(const hexchat_event_attrs *attrs)
|
|
|
|
{
|
|
|
|
hexchat_event_attrs *copy = hexchat_event_attrs_create(ph);
|
|
|
|
copy->server_time_utc = attrs->server_time_utc;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_print_attrs_closure(char *word[], hexchat_event_attrs *attrs, void *udata)
|
|
|
|
{
|
|
|
|
hook_info *info = udata;
|
|
|
|
lua_State *L = info->state;
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
int base, i, j, ret;
|
|
|
|
hexchat_event_attrs **u;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, script->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref);
|
|
|
|
for(j = 31; j >= 1; j--)
|
2016-04-05 20:05:54 +03:00
|
|
|
{
|
2016-04-03 17:28:11 +03:00
|
|
|
if(*word[j])
|
|
|
|
break;
|
2016-04-05 20:05:54 +03:00
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_newtable(L);
|
|
|
|
for(i = 1; i <= j; i++)
|
|
|
|
{
|
|
|
|
lua_pushstring(L, word[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hexchat_event_attrs *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = event_attrs_copy(attrs);
|
|
|
|
luaL_newmetatable(L, "attrs");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
script->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 2, 1, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error in print_attrs hook: %s", error ? error : "(non-string error)");
|
|
|
|
check_deferred(script);
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
ret = lua_tointeger(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 2);
|
|
|
|
check_deferred(script);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_print_attrs(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
hook_info *info, **u;
|
|
|
|
int ref, pri;
|
2016-04-03 17:28:11 +03:00
|
|
|
char const *event = luaL_checkstring(L, 1);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 2);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
pri = luaL_optinteger(L, 3, HEXCHAT_PRI_NORM);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = hexchat_hook_print_attrs(ph, event, pri, api_print_attrs_closure, info);
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
register_hook(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_server_closure(char *word[], char *word_eol[], void *udata)
|
|
|
|
{
|
|
|
|
hook_info *info = udata;
|
|
|
|
lua_State *L = info->state;
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
int base, i, ret;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, script->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref);
|
|
|
|
lua_newtable(L);
|
2020-09-18 01:50:28 +03:00
|
|
|
for(i = 1; i < WORD_ARRAY_LEN && *word_eol[i]; i++)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
lua_pushstring(L, word[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
|
|
|
lua_newtable(L);
|
2020-09-18 01:50:28 +03:00
|
|
|
for(i = 1; i < WORD_ARRAY_LEN && *word_eol[i]; i++)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
lua_pushstring(L, word_eol[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
|
|
|
script->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 2, 1, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error in server hook: %s", error ? error : "(non-string error)");
|
|
|
|
check_deferred(script);
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
ret = lua_tointeger(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 2);
|
|
|
|
check_deferred(script);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_server(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *command = luaL_optstring(L, 1, "RAW LINE");
|
2016-04-03 18:04:48 +03:00
|
|
|
hook_info *info, **u;
|
|
|
|
int ref, pri;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 2);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
pri = luaL_optinteger(L, 3, HEXCHAT_PRI_NORM);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = hexchat_hook_server(ph, command, pri, api_server_closure, info);
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
register_hook(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_server_attrs_closure(char *word[], char *word_eol[], hexchat_event_attrs *attrs, void *udata)
|
|
|
|
{
|
|
|
|
hook_info *info = udata;
|
|
|
|
lua_State *L = info->state;
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
int base, i, ret;
|
|
|
|
hexchat_event_attrs **u;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, script->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref);
|
|
|
|
lua_newtable(L);
|
2020-09-18 01:50:28 +03:00
|
|
|
for(i = 1; i < WORD_ARRAY_LEN && *word_eol[i]; i++)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
lua_pushstring(L, word[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
|
|
|
lua_newtable(L);
|
2020-09-18 01:50:28 +03:00
|
|
|
for(i = 1; i < WORD_ARRAY_LEN && *word_eol[i]; i++)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
lua_pushstring(L, word_eol[i]);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
|
|
|
|
u = lua_newuserdata(L, sizeof(hexchat_event_attrs *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = event_attrs_copy(attrs);
|
|
|
|
luaL_newmetatable(L, "attrs");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
script->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 3, 1, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error in server_attrs hook: %s", error ? error : "(non-string error)");
|
|
|
|
check_deferred(script);
|
2016-04-03 18:04:48 +03:00
|
|
|
return HEXCHAT_EAT_NONE;
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
ret = lua_tointeger(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 2);
|
|
|
|
check_deferred(script);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_server_attrs(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *command = luaL_optstring(L, 1, "RAW LINE");
|
2016-04-03 18:04:48 +03:00
|
|
|
int ref, pri;
|
|
|
|
hook_info *info, **u;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 2);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
pri = luaL_optinteger(L, 3, HEXCHAT_PRI_NORM);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = hexchat_hook_server_attrs(ph, command, pri, api_server_attrs_closure, info);
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
register_hook(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_timer_closure(void *udata)
|
|
|
|
{
|
|
|
|
hook_info *info = udata;
|
|
|
|
lua_State *L = info->state;
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
int base, ret;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, script->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref);
|
|
|
|
script->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 0, 1, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error in timer hook: %s", error ? error : "(non-string error)");
|
|
|
|
check_deferred(script);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
ret = lua_toboolean(L, -1);
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pop(L, 2);
|
|
|
|
check_deferred(script);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_timer(lua_State *L)
|
|
|
|
{
|
2016-04-27 21:53:36 +03:00
|
|
|
int ref, timeout = luaL_checkinteger (L, 1);
|
2016-04-03 18:04:48 +03:00
|
|
|
hook_info *info, **u;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 2);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = hexchat_hook_timer(ph, timeout, api_timer_closure, info);
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
register_hook(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_hook_unload(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
script_info *script;
|
|
|
|
hook_info *info, **u;
|
|
|
|
int ref;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, 1);
|
2016-04-03 18:04:48 +03:00
|
|
|
ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
2016-04-04 05:53:29 +03:00
|
|
|
info = g_new(hook_info, 1);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
info->ref = ref;
|
|
|
|
info->hook = NULL;
|
2016-04-03 18:04:48 +03:00
|
|
|
u = lua_newuserdata(L, sizeof(hook_info *));
|
2016-04-03 17:28:11 +03:00
|
|
|
*u = info;
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_setmetatable(L, -2);
|
2016-04-03 18:04:48 +03:00
|
|
|
script = get_info(info->state);
|
2016-04-04 15:51:03 +03:00
|
|
|
|
|
|
|
g_ptr_array_add(script->unload_hooks, info);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_unhook(lua_State *L)
|
|
|
|
{
|
|
|
|
hook_info **info = (hook_info **)luaL_checkudata(L, 1, "hook");
|
|
|
|
if(*info)
|
|
|
|
{
|
|
|
|
unregister_hook(*info);
|
|
|
|
*info = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tostring(L, 1);
|
|
|
|
return luaL_error(L, "hook %s is already unhooked", lua_tostring(L, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_find_context(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *server = luaL_optstring(L, 1, NULL);
|
|
|
|
char const *channel = luaL_optstring(L, 2, NULL);
|
|
|
|
hexchat_context *context = hexchat_find_context(ph, server, channel);
|
|
|
|
if(context)
|
|
|
|
{
|
|
|
|
hexchat_context **u = lua_newuserdata(L, sizeof(hexchat_context *));
|
|
|
|
*u = context;
|
|
|
|
luaL_newmetatable(L, "context");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_get_context(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_context *context = hexchat_get_context(ph);
|
|
|
|
hexchat_context **u = lua_newuserdata(L, sizeof(hexchat_context *));
|
|
|
|
*u = context;
|
|
|
|
luaL_newmetatable(L, "context");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_set_context(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_context *context = *(hexchat_context **)luaL_checkudata(L, 1, "context");
|
|
|
|
int success = hexchat_set_context(ph, context);
|
|
|
|
lua_pushboolean(L, success);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wrap_context_closure(lua_State *L)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
hexchat_context *old, *context = *(hexchat_context **)luaL_checkudata(L, 1, "context");
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_replace(L, 1);
|
2016-04-03 18:04:48 +03:00
|
|
|
old = hexchat_get_context(ph);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(!hexchat_set_context(ph, context))
|
|
|
|
return luaL_error(L, "could not switch into context");
|
|
|
|
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
|
|
|
|
hexchat_set_context(ph, old);
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void wrap_context(lua_State *L, char const *field, lua_CFunction func)
|
|
|
|
{
|
|
|
|
lua_pushcfunction(L, func);
|
|
|
|
lua_pushcclosure(L, wrap_context_closure, 1);
|
|
|
|
lua_setfield(L, -2, field);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_context_meta_eq(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_context *this = *(hexchat_context **)luaL_checkudata(L, 1, "context");
|
|
|
|
hexchat_context *that = *(hexchat_context **)luaL_checkudata(L, 2, "context");
|
|
|
|
lua_pushboolean(L, this == that);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_get_info(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *key = luaL_checkstring(L, 1);
|
|
|
|
char const *data = hexchat_get_info(ph, key);
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "gtkwin_ptr") || !strcmp(key, "win_ptr"))
|
|
|
|
lua_pushlightuserdata(L, (void *)data);
|
|
|
|
else
|
|
|
|
lua_pushstring(L, data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_attrs(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_event_attrs *attrs = hexchat_event_attrs_create(ph);
|
|
|
|
hexchat_event_attrs **u = lua_newuserdata(L, sizeof(hexchat_event_attrs *));
|
|
|
|
*u = attrs;
|
|
|
|
luaL_newmetatable(L, "attrs");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_iterate_closure(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_list *list = *(hexchat_list **)luaL_checkudata(L, lua_upvalueindex(1), "list");
|
|
|
|
if(hexchat_list_next(ph, list))
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_iterate(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *name = luaL_checkstring(L, 1);
|
|
|
|
hexchat_list *list = hexchat_list_get(ph, name);
|
|
|
|
if(list)
|
|
|
|
{
|
|
|
|
hexchat_list **u = lua_newuserdata(L, sizeof(hexchat_list *));
|
|
|
|
*u = list;
|
|
|
|
luaL_newmetatable(L, "list");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_pushcclosure(L, api_iterate_closure, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return luaL_argerror(L, 1, "invalid list name");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_prefs_meta_index(lua_State *L)
|
|
|
|
{
|
|
|
|
char const *key = luaL_checkstring(L, 2);
|
|
|
|
char const *string;
|
|
|
|
int number;
|
|
|
|
int ret = hexchat_get_prefs(ph, key, &string, &number);
|
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
case 1:
|
|
|
|
lua_pushstring(L, string);
|
|
|
|
return 1;
|
|
|
|
case 2:
|
|
|
|
lua_pushnumber(L, number);
|
|
|
|
return 1;
|
|
|
|
case 3:
|
|
|
|
lua_pushboolean(L, number);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_prefs_meta_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
return luaL_error(L, "hexchat.prefs is read-only");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int list_marshal(lua_State *L, const char *key, hexchat_list *list)
|
|
|
|
{
|
|
|
|
char const *str = hexchat_list_str(ph, list, key);
|
2016-04-03 18:04:48 +03:00
|
|
|
int number;
|
2016-04-03 17:28:11 +03:00
|
|
|
if(str)
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "context"))
|
|
|
|
{
|
|
|
|
hexchat_context **u = lua_newuserdata(L, sizeof(hexchat_context *));
|
|
|
|
*u = (hexchat_context *)str;
|
|
|
|
luaL_newmetatable(L, "context");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pushstring(L, str);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
number = hexchat_list_int(ph, list, key);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(number != -1)
|
|
|
|
{
|
|
|
|
lua_pushinteger(L, number);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (list != NULL)
|
|
|
|
{
|
2016-07-13 13:02:40 +03:00
|
|
|
time_t tm = hexchat_list_time(ph, list, key);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(tm != -1)
|
|
|
|
{
|
|
|
|
lua_pushinteger(L, tm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_props_meta_index(lua_State *L)
|
|
|
|
{
|
2016-07-13 13:02:40 +03:00
|
|
|
char const *key = luaL_checkstring(L, 2);
|
2016-04-03 17:28:11 +03:00
|
|
|
return list_marshal(L, key, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_props_meta_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
return luaL_error(L, "hexchat.props is read-only");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_pluginprefs_meta_index(lua_State *L)
|
|
|
|
{
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
const char *key;
|
|
|
|
hexchat_plugin *h;
|
|
|
|
char str[512];
|
|
|
|
int r;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
if(!script->name)
|
|
|
|
return luaL_error(L, "cannot use hexchat.pluginprefs before registering with hexchat.register");
|
2016-04-03 18:04:48 +03:00
|
|
|
|
|
|
|
key = luaL_checkstring(L, 2);
|
|
|
|
h = script->handle;
|
|
|
|
r = hexchat_pluginpref_get_int(h, key);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(r != -1)
|
|
|
|
{
|
2016-04-04 04:53:32 +03:00
|
|
|
lua_pushinteger(L, r);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(hexchat_pluginpref_get_str(h, key, str))
|
|
|
|
{
|
2016-08-06 04:15:26 +03:00
|
|
|
/* Wasn't actually a failure */
|
|
|
|
if (!strcmp(str, "-1"))
|
|
|
|
lua_pushinteger(L, r);
|
|
|
|
else
|
|
|
|
lua_pushstring(L, str);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_pluginprefs_meta_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
const char *key;
|
|
|
|
hexchat_plugin *h;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
if(!script->name)
|
|
|
|
return luaL_error(L, "cannot use hexchat.pluginprefs before registering with hexchat.register");
|
2016-04-03 18:04:48 +03:00
|
|
|
|
|
|
|
key = luaL_checkstring(L, 2);
|
|
|
|
h = script->handle;
|
2016-04-03 17:28:11 +03:00
|
|
|
switch(lua_type(L, 3))
|
|
|
|
{
|
|
|
|
case LUA_TSTRING:
|
|
|
|
hexchat_pluginpref_set_str(h, key, lua_tostring(L, 3));
|
|
|
|
return 0;
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
hexchat_pluginpref_set_int(h, key, lua_tointeger(L, 3));
|
|
|
|
return 0;
|
|
|
|
case LUA_TNIL: case LUA_TNONE:
|
|
|
|
hexchat_pluginpref_delete(h, key);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return luaL_argerror(L, 3, "expected string, number, or nil");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_pluginprefs_meta_pairs_closure(lua_State *L)
|
|
|
|
{
|
|
|
|
char *dest = lua_touserdata(L, lua_upvalueindex(1));
|
|
|
|
hexchat_plugin *h = get_info(L)->handle;
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
if(dest && *dest)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
int r;
|
|
|
|
char str[512];
|
2016-04-03 17:28:11 +03:00
|
|
|
char *key = dest;
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
dest = strchr(dest, ',');
|
|
|
|
if(dest)
|
|
|
|
*(dest++) = 0;
|
|
|
|
lua_pushlightuserdata(L, dest);
|
|
|
|
lua_replace(L, lua_upvalueindex(1));
|
|
|
|
lua_pushstring(L, key);
|
2016-04-03 18:04:48 +03:00
|
|
|
r = hexchat_pluginpref_get_int(h, key);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(r != -1)
|
|
|
|
{
|
2016-04-04 04:53:32 +03:00
|
|
|
lua_pushinteger(L, r);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if(hexchat_pluginpref_get_str(h, key, str))
|
|
|
|
{
|
|
|
|
lua_pushstring(L, str);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_hexchat_pluginprefs_meta_pairs(lua_State *L)
|
|
|
|
{
|
|
|
|
script_info *script = get_info(L);
|
2016-04-03 18:04:48 +03:00
|
|
|
char *dest;
|
|
|
|
hexchat_plugin *h;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
if(!script->name)
|
|
|
|
return luaL_error(L, "cannot use hexchat.pluginprefs before registering with hexchat.register");
|
2016-04-03 18:04:48 +03:00
|
|
|
|
|
|
|
dest = lua_newuserdata(L, 4096);
|
|
|
|
h = script->handle;
|
2016-04-03 17:28:11 +03:00
|
|
|
if(!hexchat_pluginpref_list(h, dest))
|
|
|
|
strcpy(dest, "");
|
|
|
|
lua_pushlightuserdata(L, dest);
|
|
|
|
lua_pushlightuserdata(L, dest);
|
|
|
|
lua_pushcclosure(L, api_hexchat_pluginprefs_meta_pairs_closure, 2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_attrs_meta_index(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_event_attrs *attrs = *(hexchat_event_attrs **)luaL_checkudata(L, 1, "attrs");
|
|
|
|
char const *key = luaL_checkstring(L, 2);
|
|
|
|
if(!strcmp(key, "server_time_utc"))
|
|
|
|
{
|
2016-04-04 04:53:32 +03:00
|
|
|
lua_pushinteger(L, attrs->server_time_utc);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_attrs_meta_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_event_attrs *attrs = *(hexchat_event_attrs **)luaL_checkudata(L, 1, "attrs");
|
|
|
|
char const *key = luaL_checkstring(L, 2);
|
|
|
|
if(!strcmp(key, "server_time_utc"))
|
|
|
|
{
|
2016-04-27 21:53:36 +03:00
|
|
|
attrs->server_time_utc = luaL_checkinteger(L, 3);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_attrs_meta_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_event_attrs *attrs = *(hexchat_event_attrs **)luaL_checkudata(L, 1, "attrs");
|
|
|
|
hexchat_event_attrs_free(ph, attrs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_list_meta_index(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_list *list = *(hexchat_list **)luaL_checkudata(L, 1, "list");
|
|
|
|
char const *key = luaL_checkstring(L, 2);
|
|
|
|
return list_marshal(L, key, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_list_meta_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
return luaL_error(L, "hexchat.iterate list is read-only");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int api_list_meta_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
hexchat_list *list = *(hexchat_list **)luaL_checkudata(L, 1, "list");
|
|
|
|
hexchat_list_free(ph, list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static luaL_Reg api_hexchat[] = {
|
|
|
|
{"register", api_hexchat_register},
|
|
|
|
{"command", api_hexchat_command},
|
|
|
|
{"print", api_hexchat_print},
|
|
|
|
{"emit_print", api_hexchat_emit_print},
|
|
|
|
{"emit_print_attrs", api_hexchat_emit_print_attrs},
|
|
|
|
{"send_modes", api_hexchat_send_modes},
|
|
|
|
{"nickcmp", api_hexchat_nickcmp},
|
|
|
|
{"strip", api_hexchat_strip},
|
|
|
|
{"get_info", api_hexchat_get_info},
|
|
|
|
{"hook_command", api_hexchat_hook_command},
|
|
|
|
{"hook_print", api_hexchat_hook_print},
|
|
|
|
{"hook_print_attrs", api_hexchat_hook_print_attrs},
|
|
|
|
{"hook_server", api_hexchat_hook_server},
|
|
|
|
{"hook_server_attrs", api_hexchat_hook_server_attrs},
|
|
|
|
{"hook_timer", api_hexchat_hook_timer},
|
|
|
|
{"hook_unload", api_hexchat_hook_unload},
|
|
|
|
{"unhook", api_hexchat_unhook},
|
|
|
|
{"get_context", api_hexchat_get_context},
|
|
|
|
{"find_context", api_hexchat_find_context},
|
|
|
|
{"set_context", api_hexchat_set_context},
|
|
|
|
{"attrs", api_hexchat_attrs},
|
|
|
|
{"iterate", api_hexchat_iterate},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static luaL_Reg api_hexchat_props_meta[] = {
|
|
|
|
{"__index", api_hexchat_props_meta_index},
|
|
|
|
{"__newindex", api_hexchat_props_meta_newindex},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static luaL_Reg api_hexchat_prefs_meta[] = {
|
|
|
|
{"__index", api_hexchat_prefs_meta_index},
|
|
|
|
{"__newindex", api_hexchat_prefs_meta_newindex},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static luaL_Reg api_hexchat_pluginprefs_meta[] = {
|
|
|
|
{"__index", api_hexchat_pluginprefs_meta_index},
|
|
|
|
{"__newindex", api_hexchat_pluginprefs_meta_newindex},
|
|
|
|
{"__pairs", api_hexchat_pluginprefs_meta_pairs},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static luaL_Reg api_hook_meta_index[] = {
|
|
|
|
{"unhook", api_hexchat_unhook},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static luaL_Reg api_attrs_meta[] = {
|
|
|
|
{"__index", api_attrs_meta_index},
|
|
|
|
{"__newindex", api_attrs_meta_newindex},
|
|
|
|
{"__gc", api_attrs_meta_gc},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static luaL_Reg api_list_meta[] = {
|
|
|
|
{"__index", api_list_meta_index},
|
|
|
|
{"__newindex", api_list_meta_newindex},
|
|
|
|
{"__gc", api_list_meta_gc},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int luaopen_hexchat(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_setfuncs(L, api_hexchat, 0);
|
|
|
|
|
2016-04-04 04:53:32 +03:00
|
|
|
lua_pushinteger(L, HEXCHAT_PRI_HIGHEST); lua_setfield(L, -2, "PRI_HIGHEST");
|
|
|
|
lua_pushinteger(L, HEXCHAT_PRI_HIGH); lua_setfield(L, -2, "PRI_HIGH");
|
|
|
|
lua_pushinteger(L, HEXCHAT_PRI_NORM); lua_setfield(L, -2, "PRI_NORM");
|
|
|
|
lua_pushinteger(L, HEXCHAT_PRI_LOW); lua_setfield(L, -2, "PRI_LOW");
|
|
|
|
lua_pushinteger(L, HEXCHAT_PRI_LOWEST); lua_setfield(L, -2, "PRI_LOWEST");
|
|
|
|
lua_pushinteger(L, HEXCHAT_EAT_NONE); lua_setfield(L, -2, "EAT_NONE");
|
|
|
|
lua_pushinteger(L, HEXCHAT_EAT_HEXCHAT); lua_setfield(L, -2, "EAT_HEXCHAT");
|
|
|
|
lua_pushinteger(L, HEXCHAT_EAT_PLUGIN); lua_setfield(L, -2, "EAT_PLUGIN");
|
|
|
|
lua_pushinteger(L, HEXCHAT_EAT_ALL); lua_setfield(L, -2, "EAT_ALL");
|
2016-04-03 17:28:11 +03:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_setfuncs(L, api_hexchat_prefs_meta, 0);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_setfield(L, -2, "prefs");
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_setfuncs(L, api_hexchat_props_meta, 0);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_setfield(L, -2, "props");
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_setfuncs(L, api_hexchat_pluginprefs_meta, 0);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_setfield(L, -2, "pluginprefs");
|
|
|
|
|
|
|
|
luaL_newmetatable(L, "hook");
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_setfuncs(L, api_hook_meta_index, 0);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
luaL_newmetatable(L, "context");
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushcfunction(L, api_hexchat_set_context);
|
|
|
|
lua_setfield(L, -2, "set");
|
|
|
|
wrap_context(L, "find_context", api_hexchat_find_context);
|
|
|
|
wrap_context(L, "print", api_hexchat_print);
|
|
|
|
wrap_context(L, "emit_print", api_hexchat_emit_print);
|
|
|
|
wrap_context(L, "emit_print_attrs", api_hexchat_emit_print_attrs);
|
|
|
|
wrap_context(L, "command", api_hexchat_command);
|
|
|
|
wrap_context(L, "nickcmp", api_hexchat_nickcmp);
|
|
|
|
wrap_context(L, "get_info", api_hexchat_get_info);
|
|
|
|
wrap_context(L, "iterate", api_hexchat_iterate);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
lua_pushcfunction(L, api_hexchat_context_meta_eq);
|
|
|
|
lua_setfield(L, -2, "__eq");
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
|
|
|
|
luaL_newmetatable(L, "attrs");
|
|
|
|
luaL_setfuncs(L, api_attrs_meta, 0);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
luaL_newmetatable(L, "list");
|
|
|
|
luaL_setfuncs(L, api_list_meta, 0);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pairs_closure(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_settop(L, 1);
|
|
|
|
if(luaL_getmetafield(L, 1, "__pairs"))
|
|
|
|
{
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_call(L, 1, LUA_MULTRET);
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_call(L, 1, LUA_MULTRET);
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patch_pairs(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_getglobal(L, "pairs");
|
|
|
|
lua_pushcclosure(L, pairs_closure, 1);
|
|
|
|
lua_setglobal(L, "pairs");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patch_clibs(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, LUA_REGISTRYINDEX))
|
|
|
|
{
|
|
|
|
if(lua_type(L, -2) == LUA_TLIGHTUSERDATA && lua_type(L, -1) == LUA_TTABLE)
|
|
|
|
{
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, "_CLIBS");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:51:03 +03:00
|
|
|
static GPtrArray *scripts;
|
2016-04-03 17:28:11 +03:00
|
|
|
|
|
|
|
static char *expand_buffer = NULL;
|
|
|
|
static char const *expand_path(char const *path)
|
|
|
|
{
|
|
|
|
if(g_path_is_absolute(path))
|
|
|
|
return path;
|
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
if(path[0] == '~')
|
|
|
|
{
|
|
|
|
if(!path[1] || path[1] == '/')
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
g_free(expand_buffer);
|
2016-04-03 17:28:11 +03:00
|
|
|
expand_buffer = g_build_filename(g_get_home_dir(), path + 1, NULL);
|
|
|
|
return expand_buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *user = g_strdup(path + 1);
|
|
|
|
char *slash_pos = strchr(user, '/');
|
2016-04-03 18:04:48 +03:00
|
|
|
struct passwd *pw;
|
2016-04-03 17:28:11 +03:00
|
|
|
if(slash_pos)
|
|
|
|
*slash_pos = 0;
|
2016-04-03 18:04:48 +03:00
|
|
|
pw = getpwnam(user);
|
2016-04-03 17:28:11 +03:00
|
|
|
g_free(user);
|
|
|
|
if(pw)
|
|
|
|
{
|
|
|
|
slash_pos = strchr(path, '/');
|
|
|
|
if(!slash_pos)
|
|
|
|
return pw->pw_dir;
|
2016-04-04 15:51:03 +03:00
|
|
|
|
|
|
|
g_free(expand_buffer);
|
2016-04-03 17:28:11 +03:00
|
|
|
expand_buffer = g_strconcat(pw->pw_dir, slash_pos, NULL);
|
|
|
|
return expand_buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
g_free(expand_buffer);
|
2016-04-03 17:28:11 +03:00
|
|
|
expand_buffer = g_build_filename(hexchat_get_info(ph, "configdir"), "addons", path, NULL);
|
|
|
|
return expand_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:51:03 +03:00
|
|
|
static inline int is_lua_file(char const *file)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
return g_str_has_suffix(file, ".lua") || g_str_has_suffix(file, ".luac");
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void prepare_state(lua_State *L, script_info *info)
|
|
|
|
{
|
|
|
|
luaL_openlibs(L);
|
|
|
|
if(LUA_VERSION_NUM < 502)
|
|
|
|
patch_pairs(L);
|
|
|
|
if(LUA_VERSION_NUM > 502)
|
|
|
|
patch_clibs(L);
|
|
|
|
lua_getglobal(L, "debug");
|
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
info->traceback = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_pushlightuserdata(L, info);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, registry_field);
|
|
|
|
luaopen_hexchat(L);
|
|
|
|
lua_setglobal(L, "hexchat");
|
|
|
|
lua_getglobal(L, "hexchat");
|
|
|
|
lua_getfield(L, -1, "print");
|
|
|
|
lua_setglobal(L, "print");
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2016-04-04 23:40:45 +03:00
|
|
|
static void run_unload_hook(hook_info *hook, lua_State *L)
|
2016-04-04 15:51:03 +03:00
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
int base = lua_gettop(L);
|
2016-04-04 15:51:03 +03:00
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, hook->ref);
|
|
|
|
if(lua_pcall(L, 0, 0, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
hexchat_printf(ph, "Lua error in unload hook: %s", error ? error : "(non-string error)");
|
|
|
|
}
|
2016-04-04 23:40:45 +03:00
|
|
|
lua_settop(L, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void run_unload_hooks(script_info *info, void *unused)
|
|
|
|
{
|
|
|
|
lua_State *L = info->state;
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->traceback);
|
|
|
|
g_ptr_array_foreach(info->unload_hooks, (GFunc)run_unload_hook, L);
|
|
|
|
lua_pop(L, 1);
|
2016-04-04 15:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_script(script_info *info)
|
|
|
|
{
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
g_clear_pointer(&info->hooks, g_ptr_array_unref);
|
|
|
|
g_clear_pointer(&info->unload_hooks, g_ptr_array_unref);
|
|
|
|
g_clear_pointer(&info->state, lua_close);
|
|
|
|
if (info->handle)
|
|
|
|
hexchat_plugingui_remove(ph, info->handle);
|
|
|
|
g_free(info->filename);
|
|
|
|
g_free(info->name);
|
|
|
|
g_free(info->description);
|
|
|
|
g_free(info->version);
|
|
|
|
g_free(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
static script_info *create_script(char const *file)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
int base;
|
|
|
|
char *filename_fs;
|
|
|
|
lua_State *L;
|
2016-04-04 05:53:29 +03:00
|
|
|
script_info *info = g_new0(script_info, 1);
|
2016-04-04 15:51:03 +03:00
|
|
|
info->hooks = g_ptr_array_new_with_free_func((GDestroyNotify)free_hook);
|
|
|
|
info->unload_hooks = g_ptr_array_new_with_free_func((GDestroyNotify)free_hook);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->filename = g_strdup(expand_path(file));
|
2016-04-03 18:04:48 +03:00
|
|
|
L = luaL_newstate();
|
2016-04-03 17:28:11 +03:00
|
|
|
info->state = L;
|
|
|
|
if(!L)
|
|
|
|
{
|
2016-04-04 04:28:02 +03:00
|
|
|
hexchat_print(ph, "\00304Could not allocate memory for the script");
|
2016-04-04 15:51:03 +03:00
|
|
|
destroy_script(info);
|
2016-04-03 17:28:11 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
prepare_state(L, info);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
|
|
|
filename_fs = g_filename_from_utf8(info->filename, -1, NULL, NULL, NULL);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(!filename_fs)
|
|
|
|
{
|
|
|
|
hexchat_printf(ph, "Invalid filename: %s", info->filename);
|
2016-04-04 15:51:03 +03:00
|
|
|
destroy_script(info);
|
2016-04-03 17:28:11 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(luaL_loadfile(L, filename_fs))
|
|
|
|
{
|
|
|
|
g_free(filename_fs);
|
|
|
|
hexchat_printf(ph, "Lua syntax error: %s", luaL_optstring(L, -1, ""));
|
2016-04-04 15:51:03 +03:00
|
|
|
destroy_script(info);
|
2016-04-03 17:28:11 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
g_free(filename_fs);
|
|
|
|
info->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 0, 0, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
2016-04-03 18:04:48 +03:00
|
|
|
hexchat_printf(ph, "Lua error: %s", error ? error : "(non-string error)");
|
2016-04-04 15:51:03 +03:00
|
|
|
destroy_script(info);
|
|
|
|
return NULL;
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
if(!info->name)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
hexchat_printf(ph, "Lua script didn't register with hexchat.register");
|
2016-04-04 15:51:03 +03:00
|
|
|
destroy_script(info);
|
|
|
|
return NULL;
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:51:03 +03:00
|
|
|
static script_info *get_script_by_file(char const *filename)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
char const *expanded = expand_path(filename);
|
2016-04-04 15:51:03 +03:00
|
|
|
guint i;
|
|
|
|
for(i = 0; i < scripts->len; i++)
|
|
|
|
{
|
|
|
|
script_info *script = scripts->pdata[i];
|
|
|
|
if(!strcmp(script->filename, expanded))
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
return script;
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
2016-04-04 15:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-28 02:58:22 +03:00
|
|
|
static int load_script(char const *file)
|
|
|
|
{
|
|
|
|
script_info *info = get_script_by_file(file);
|
|
|
|
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
hexchat_print(ph, "Lua script is already loaded");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = create_script(file);
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
g_ptr_array_add(scripts, info);
|
|
|
|
check_deferred(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:51:03 +03:00
|
|
|
static int unload_script(char const *filename)
|
|
|
|
{
|
|
|
|
script_info *script = get_script_by_file(filename);
|
|
|
|
|
|
|
|
if (!script)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(script->status & STATUS_ACTIVE)
|
|
|
|
script->status |= STATUS_DEFERRED_UNLOAD;
|
|
|
|
else
|
2016-04-04 23:40:45 +03:00
|
|
|
{
|
|
|
|
run_unload_hooks(script, NULL);
|
2016-04-04 15:51:03 +03:00
|
|
|
g_ptr_array_remove_fast(scripts, script);
|
2016-04-04 23:40:45 +03:00
|
|
|
}
|
2016-04-04 15:51:03 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int reload_script(char const *filename)
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
script_info *script = get_script_by_file(filename);
|
|
|
|
|
|
|
|
if (!script)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(script->status & STATUS_ACTIVE)
|
2016-04-05 20:05:54 +03:00
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
script->status |= STATUS_DEFERRED_RELOAD;
|
2016-04-05 20:05:54 +03:00
|
|
|
}
|
2016-04-04 15:51:03 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *filename = g_strdup(script->filename);
|
2016-04-04 23:40:45 +03:00
|
|
|
run_unload_hooks(script, NULL);
|
2016-04-04 15:51:03 +03:00
|
|
|
g_ptr_array_remove_fast(scripts, script);
|
|
|
|
load_script(filename);
|
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void autoload_scripts(void)
|
|
|
|
{
|
|
|
|
char *path = g_build_filename(hexchat_get_info(ph, "configdir"), "addons", NULL);
|
|
|
|
GDir *dir = g_dir_open(path, 0, NULL);
|
|
|
|
if(dir)
|
|
|
|
{
|
|
|
|
char const *filename;
|
|
|
|
while((filename = g_dir_read_name(dir)))
|
2016-04-05 20:05:54 +03:00
|
|
|
{
|
2016-04-03 17:28:11 +03:00
|
|
|
if(is_lua_file(filename))
|
|
|
|
load_script(filename);
|
2016-04-05 20:05:54 +03:00
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
g_dir_close(dir);
|
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:11:23 +03:00
|
|
|
static script_info *interp = NULL;
|
2016-04-03 17:28:11 +03:00
|
|
|
static void create_interpreter(void)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
lua_State *L;
|
2016-04-04 05:53:29 +03:00
|
|
|
interp = g_new0(script_info, 1);
|
2016-04-04 23:40:45 +03:00
|
|
|
interp->hooks = g_ptr_array_new_with_free_func((GDestroyNotify)free_hook);
|
|
|
|
interp->unload_hooks = g_ptr_array_new_with_free_func((GDestroyNotify)free_hook);
|
2016-04-03 17:28:11 +03:00
|
|
|
interp->name = "lua interpreter";
|
|
|
|
interp->description = "";
|
|
|
|
interp->version = "";
|
|
|
|
interp->handle = ph;
|
|
|
|
interp->filename = "";
|
2016-04-03 18:04:48 +03:00
|
|
|
L = luaL_newstate();
|
2016-04-03 17:28:11 +03:00
|
|
|
interp->state = L;
|
|
|
|
if(!L)
|
|
|
|
{
|
2016-04-04 04:28:02 +03:00
|
|
|
hexchat_print(ph, "\00304Could not allocate memory for the interpreter");
|
2016-04-04 05:53:29 +03:00
|
|
|
g_free(interp);
|
2016-04-03 17:28:11 +03:00
|
|
|
interp = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prepare_state(L, interp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_interpreter(void)
|
|
|
|
{
|
|
|
|
if(interp)
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
g_clear_pointer(&interp->hooks, g_ptr_array_unref);
|
|
|
|
g_clear_pointer(&interp->unload_hooks, g_ptr_array_unref);
|
|
|
|
g_clear_pointer(&interp->state, lua_close);
|
|
|
|
g_clear_pointer(&interp, g_free);
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inject_string(script_info *info, char const *line)
|
|
|
|
{
|
|
|
|
lua_State *L = info->state;
|
2016-04-03 18:04:48 +03:00
|
|
|
int base, top;
|
2016-07-13 13:02:40 +03:00
|
|
|
char *ret_line;
|
2016-07-14 17:04:58 +03:00
|
|
|
gboolean force_ret = FALSE;
|
2016-07-13 13:02:40 +03:00
|
|
|
|
|
|
|
if(line[0] == '=')
|
2016-07-14 17:04:58 +03:00
|
|
|
{
|
2016-07-13 13:02:40 +03:00
|
|
|
line++;
|
2016-07-14 17:04:58 +03:00
|
|
|
force_ret = TRUE;
|
|
|
|
}
|
2016-07-13 13:02:40 +03:00
|
|
|
ret_line = g_strconcat("return ", line, NULL);
|
2016-04-03 18:04:48 +03:00
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, info->traceback);
|
2016-04-03 18:04:48 +03:00
|
|
|
base = lua_gettop(L);
|
2016-07-13 13:02:40 +03:00
|
|
|
if(luaL_loadbuffer(L, ret_line, strlen(ret_line), "@interpreter"))
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
2016-07-14 17:04:58 +03:00
|
|
|
if(!force_ret)
|
|
|
|
lua_pop(L, 1);
|
|
|
|
if(force_ret || luaL_loadbuffer(L, line, strlen(line), "@interpreter"))
|
2016-07-13 13:02:40 +03:00
|
|
|
{
|
|
|
|
hexchat_printf(ph, "Lua syntax error: %s", luaL_optstring(L, -1, ""));
|
|
|
|
lua_pop(L, 2);
|
|
|
|
g_free(ret_line);
|
|
|
|
return;
|
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
2016-07-13 13:02:40 +03:00
|
|
|
g_free(ret_line);
|
2016-04-03 17:28:11 +03:00
|
|
|
info->status |= STATUS_ACTIVE;
|
|
|
|
if(lua_pcall(L, 0, LUA_MULTRET, base))
|
|
|
|
{
|
|
|
|
char const *error = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
hexchat_printf(ph, "Lua error: %s", error ? error : "(non-string error)");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-03 18:04:48 +03:00
|
|
|
top = lua_gettop(L);
|
2016-04-03 17:28:11 +03:00
|
|
|
if(top > base)
|
|
|
|
{
|
2016-04-03 18:04:48 +03:00
|
|
|
int i;
|
2016-04-03 17:28:11 +03:00
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
for(i = base + 1; i <= top; i++)
|
|
|
|
{
|
|
|
|
if(i != base + 1)
|
|
|
|
luaL_addstring(&b, " ");
|
|
|
|
tostring(L, i);
|
|
|
|
luaL_addvalue(&b);
|
|
|
|
}
|
|
|
|
luaL_pushresult(&b);
|
|
|
|
hexchat_print(ph, lua_tostring(L, -1));
|
|
|
|
lua_pop(L, top - base + 1);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
check_deferred(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_load(char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
if(is_lua_file(word[2]))
|
|
|
|
{
|
|
|
|
load_script(word[2]);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_unload(char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
if(unload_script(word[2]))
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
else
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_reload(char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
if(reload_script(word[2]))
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
else
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_console_exec(char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
char const *channel = hexchat_get_info(ph, "channel");
|
|
|
|
if(channel && !strcmp(channel, console_tab))
|
|
|
|
{
|
|
|
|
if(interp)
|
|
|
|
{
|
|
|
|
hexchat_printf(ph, "> %s", word_eol[1]);
|
|
|
|
inject_string(interp, word_eol[1]);
|
|
|
|
}
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:11:23 +03:00
|
|
|
static void check_deferred(script_info *info)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
info->status &= ~STATUS_ACTIVE;
|
|
|
|
if(info->status & STATUS_DEFERRED_UNLOAD)
|
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
run_unload_hooks(info, NULL);
|
2016-04-04 15:51:03 +03:00
|
|
|
g_ptr_array_remove_fast(scripts, info);
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
else if(info->status & STATUS_DEFERRED_RELOAD)
|
|
|
|
{
|
|
|
|
if(info == interp)
|
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
run_unload_hooks(interp, NULL);
|
2016-04-03 17:28:11 +03:00
|
|
|
destroy_interpreter();
|
|
|
|
create_interpreter();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
char *filename = g_strdup(info->filename);
|
2016-04-04 23:40:45 +03:00
|
|
|
run_unload_hooks(info, NULL);
|
2016-04-04 15:51:03 +03:00
|
|
|
g_ptr_array_remove_fast(scripts, info);
|
|
|
|
load_script(filename);
|
|
|
|
g_free(filename);
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_lua(char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
if(!strcmp(word[2], "load"))
|
|
|
|
{
|
|
|
|
load_script(word[3]);
|
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "unload"))
|
|
|
|
{
|
|
|
|
if(!unload_script(word[3]))
|
|
|
|
hexchat_printf(ph, "Could not find a script by the name '%s'", word[3]);
|
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "reload"))
|
|
|
|
{
|
|
|
|
if(!reload_script(word[3]))
|
|
|
|
hexchat_printf(ph, "Could not find a script by the name '%s'", word[3]);
|
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "exec"))
|
|
|
|
{
|
|
|
|
if(interp)
|
|
|
|
inject_string(interp, word_eol[3]);
|
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "inject"))
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
script_info *script = get_script_by_file(word[3]);
|
|
|
|
if (script)
|
|
|
|
{
|
|
|
|
inject_string(script, word_eol[4]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-03 17:28:11 +03:00
|
|
|
hexchat_printf(ph, "Could not find a script by the name '%s'", word[3]);
|
2016-04-04 15:51:03 +03:00
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "reset"))
|
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
if(interp)
|
2016-04-08 02:02:38 +03:00
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
if(interp->status & STATUS_ACTIVE)
|
2016-04-05 20:05:54 +03:00
|
|
|
{
|
2016-04-04 23:40:45 +03:00
|
|
|
interp->status |= STATUS_DEFERRED_RELOAD;
|
2016-04-05 20:05:54 +03:00
|
|
|
}
|
2016-04-04 23:40:45 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
run_unload_hooks(interp, NULL);
|
|
|
|
destroy_interpreter();
|
|
|
|
create_interpreter();
|
|
|
|
}
|
2016-04-08 02:02:38 +03:00
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "list"))
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
guint i;
|
2016-04-05 20:01:15 +03:00
|
|
|
hexchat_print(ph,
|
|
|
|
"Name Version Filename Description\n"
|
|
|
|
"---- ------- -------- -----------\n");
|
2016-04-04 15:51:03 +03:00
|
|
|
for(i = 0; i < scripts->len; i++)
|
|
|
|
{
|
|
|
|
script_info *info = scripts->pdata[i];
|
2016-04-05 20:01:15 +03:00
|
|
|
char *basename = g_path_get_basename(info->filename);
|
|
|
|
hexchat_printf(ph, "%-16s %-8s %-20s %-10s\n", info->name, info->version,
|
|
|
|
basename, info->description);
|
|
|
|
g_free(basename);
|
2016-04-04 15:51:03 +03:00
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
if(interp)
|
2016-04-05 20:01:15 +03:00
|
|
|
hexchat_printf(ph, "%-16s %-8s", interp->name, plugin_version);
|
2016-04-03 17:28:11 +03:00
|
|
|
}
|
|
|
|
else if(!strcmp(word[2], "console"))
|
|
|
|
{
|
|
|
|
hexchat_commandf(ph, "query %s", console_tab);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hexchat_command(ph, "help lua");
|
|
|
|
}
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:14:36 +03:00
|
|
|
/* Reinitialization safegaurd */
|
|
|
|
static int initialized = 0;
|
|
|
|
|
2016-04-03 17:28:11 +03:00
|
|
|
G_MODULE_EXPORT int hexchat_plugin_init(hexchat_plugin *plugin_handle, char **name, char **description, char **version, char *arg)
|
|
|
|
{
|
2017-02-27 20:14:36 +03:00
|
|
|
if(initialized != 0)
|
|
|
|
{
|
|
|
|
hexchat_print(plugin_handle, "Lua interface already loaded\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-05 20:01:15 +03:00
|
|
|
if (g_str_has_prefix(LUA_VERSION, "Lua "))
|
|
|
|
{
|
|
|
|
strcat(plugin_version, "/");
|
|
|
|
g_strlcat(plugin_version, LUA_VERSION + 4, sizeof(plugin_version));
|
|
|
|
}
|
2016-04-03 17:28:11 +03:00
|
|
|
|
|
|
|
*name = plugin_name;
|
|
|
|
*description = plugin_description;
|
|
|
|
*version = plugin_version;
|
|
|
|
|
|
|
|
ph = plugin_handle;
|
2017-02-27 20:14:36 +03:00
|
|
|
initialized = 1;
|
2016-04-03 17:28:11 +03:00
|
|
|
|
|
|
|
hexchat_hook_command(ph, "", HEXCHAT_PRI_NORM, command_console_exec, NULL, NULL);
|
|
|
|
hexchat_hook_command(ph, "LOAD", HEXCHAT_PRI_NORM, command_load, NULL, NULL);
|
|
|
|
hexchat_hook_command(ph, "UNLOAD", HEXCHAT_PRI_NORM, command_unload, NULL, NULL);
|
|
|
|
hexchat_hook_command(ph, "RELOAD", HEXCHAT_PRI_NORM, command_reload, NULL, NULL);
|
|
|
|
hexchat_hook_command(ph, "lua", HEXCHAT_PRI_NORM, command_lua, command_help, NULL);
|
|
|
|
|
|
|
|
hexchat_printf(ph, "%s version %s loaded.\n", plugin_name, plugin_version);
|
|
|
|
|
2016-04-04 15:51:03 +03:00
|
|
|
scripts = g_ptr_array_new_with_free_func((GDestroyNotify)destroy_script);
|
2016-04-03 17:28:11 +03:00
|
|
|
create_interpreter();
|
|
|
|
|
|
|
|
if(!arg)
|
|
|
|
autoload_scripts();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT int hexchat_plugin_deinit(hexchat_plugin *plugin_handle)
|
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
guint i;
|
|
|
|
gboolean active = FALSE;
|
|
|
|
for(i = 0; i < scripts->len; i++)
|
|
|
|
{
|
|
|
|
if(((script_info*)scripts->pdata[i])->status & STATUS_ACTIVE)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
2016-04-04 15:51:03 +03:00
|
|
|
active = TRUE;
|
2016-04-03 17:28:11 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-04-04 15:51:03 +03:00
|
|
|
}
|
2016-04-04 23:40:45 +03:00
|
|
|
if(interp && interp->status & STATUS_ACTIVE)
|
2016-04-04 15:51:03 +03:00
|
|
|
active = TRUE;
|
|
|
|
if(active)
|
2016-04-03 17:28:11 +03:00
|
|
|
{
|
|
|
|
hexchat_print(ph, "\00304Cannot unload the lua plugin while there are active states");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-04 23:40:45 +03:00
|
|
|
if(interp)
|
|
|
|
run_unload_hooks(interp, NULL);
|
2016-04-03 17:28:11 +03:00
|
|
|
destroy_interpreter();
|
2016-04-04 23:40:45 +03:00
|
|
|
g_ptr_array_foreach(scripts, (GFunc)run_unload_hooks, NULL);
|
2016-04-04 15:51:03 +03:00
|
|
|
g_clear_pointer(&scripts, g_ptr_array_unref);
|
|
|
|
g_clear_pointer(&expand_buffer, g_free);
|
2016-04-03 17:28:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|