From 4665807d0963bb20fa6253df082847f0434f0b0d Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Wed, 20 Apr 2016 23:23:53 +0200
Subject: degesch: expose message parsing to Lua
---
degesch.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/degesch.c b/degesch.c
index 9f5ad7b..c21dba7 100644
--- a/degesch.c
+++ b/degesch.c
@@ -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 },
--
cgit v1.2.3-70-g09d2