degesch: expose message parsing to Lua

This commit is contained in:
Přemysl Eric Janouch 2016-04-20 23:23:53 +02:00
parent 1180255e7b
commit 4665807d09
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 46 additions and 0 deletions

View File

@ -8277,6 +8277,51 @@ lua_plugin_log_error
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
lua_plugin_kv (lua_State *L, const char *key, const char *value)
{
lua_pushstring (L, value);
lua_setfield (L, -2, key);
}
static void
lua_plugin_push_message (lua_State *L, const struct irc_message *msg)
{
lua_createtable (L, 0, 4);
lua_createtable (L, msg->tags.len, 0);
struct str_map_iter iter;
str_map_iter_init (&iter, &msg->tags);
const char *value;
while ((value = str_map_iter_next (&iter)))
lua_plugin_kv (L, iter.link->key, value);
lua_setfield (L, -2, "tags");
// TODO: parse the prefix further?
if (msg->prefix) lua_plugin_kv (L, "prefix", msg->prefix);
if (msg->command) lua_plugin_kv (L, "command", msg->command);
lua_createtable (L, msg->params.len, 0);
for (size_t i = 0; i < msg->params.len; i++)
{
lua_pushstring (L, msg->params.vector[i]);
lua_rawseti (L, -2, i + 1);
}
lua_setfield (L, -2, "params");
}
static int
lua_plugin_parse (lua_State *L)
{
struct irc_message msg;
irc_parse_message (&msg, luaL_checkstring (L, 1));
lua_plugin_push_message (L, &msg);
irc_free_message (&msg);
return 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define XLUA_BUFFER_METATABLE "buffer" ///< Identifier for the Lua metatable
struct lua_buffer
@ -9451,6 +9496,7 @@ lua_plugin_connect (lua_State *L)
static luaL_Reg lua_plugin_library[] =
{
{ "parse", lua_plugin_parse },
{ "hook_input", lua_plugin_hook_input },
{ "hook_irc", lua_plugin_hook_irc },
{ "hook_completion", lua_plugin_hook_completion },