From 56858a97dd32f2e45a8ef023ca7dfe1e5f5d7460 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Thu, 14 Jan 2016 03:34:29 +0100
Subject: degesch: Lua: allow simulating user input
Also added UTF-8 validation to buffer:log() while I'm at it.
---
degesch.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/degesch.c b/degesch.c
index d736a9c..fe503aa 100644
--- a/degesch.c
+++ b/degesch.c
@@ -7432,6 +7432,15 @@ lua_plugin_handle_string_filter_result (struct lua_plugin *self,
return true;
}
+static const char *
+lua_plugin_check_utf8 (lua_State *L, int arg)
+{
+ size_t len;
+ const char *s = luaL_checklstring (L, arg, &len);
+ luaL_argcheck (L, utf8_validate (s, len), 1, "must be valid UTF-8");
+ return s;
+}
+
static void
lua_plugin_log_error
(struct lua_plugin *self, const char *where, struct error *error)
@@ -7548,7 +7557,7 @@ lua_buffer_log (lua_State *L)
{
struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
luaL_argcheck (L, wrapper->buffer, 1, "dead reference used");
- const char *message = luaL_checkstring (L, 2);
+ const char *message = lua_plugin_check_utf8 (L, 2);
struct buffer *buffer = wrapper->buffer;
log_full (wrapper->plugin->ctx, buffer->server, buffer,
@@ -7556,12 +7565,25 @@ lua_buffer_log (lua_State *L)
return 0;
}
+static int
+lua_buffer_execute (lua_State *L)
+{
+ struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
+ luaL_argcheck (L, wrapper->buffer, 1, "dead reference used");
+ const char *line = lua_plugin_check_utf8 (L, 2);
+
+ struct buffer *buffer = wrapper->buffer;
+ process_input_utf8 (wrapper->plugin->ctx, buffer, line, 0);
+ return 0;
+}
+
static luaL_Reg lua_buffer_table[] =
{
// TODO: some useful methods or values
{ "__gc", lua_buffer_gc },
{ "get_server", lua_buffer_get_server },
{ "log", lua_buffer_log },
+ { "execute", lua_buffer_execute },
{ NULL, NULL }
};
--
cgit v1.2.3-70-g09d2