diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2016-04-20 23:23:53 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2016-04-21 22:09:35 +0200 |
commit | 4665807d0963bb20fa6253df082847f0434f0b0d (patch) | |
tree | a325b2419da8a4ab02132addd76592c9ec35b682 /degesch.c | |
parent | 1180255e7b0e9e70a6d8b7665ac8a77886ef16a5 (diff) | |
download | xK-4665807d0963bb20fa6253df082847f0434f0b0d.tar.gz xK-4665807d0963bb20fa6253df082847f0434f0b0d.tar.xz xK-4665807d0963bb20fa6253df082847f0434f0b0d.zip |
degesch: expose message parsing to Lua
Diffstat (limited to 'degesch.c')
-rw-r--r-- | degesch.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -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 }, |