degesch: Lua: allow simulating user input

Also added UTF-8 validation to buffer:log() while I'm at it.
This commit is contained in:
Přemysl Eric Janouch 2016-01-14 03:34:29 +01:00
parent 331d1842b9
commit 56858a97dd
1 changed files with 23 additions and 1 deletions

View File

@ -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 }
};