#include "ioloop.h"
#include "net.h"
#include "str.h"
+#include "base64.h"
#include "time-util.h"
#include "dlua-script-private.h"
#include "dict-lua.h"
#include "strescape.h"
#define LUA_SCRIPT_DOVECOT "dovecot"
+#define LUA_SCRIPT_DOVECOT_BASE64 "base64"
#define DLUA_EVENT_PASSTHROUGH "struct event_passthrough"
#define DLUA_EVENT "struct event"
return 1;
}
+static int dlua_base64_encode(lua_State *L)
+{
+ DLUA_REQUIRE_ARGS(L, 1);
+ size_t len;
+ const void *input = luaL_checklstring(L, 1, &len);
+
+ buffer_t *output = t_base64_encode(0, UINT_MAX, input, len);
+ lua_pushlstring(L, output->data, output->used);
+ return 1;
+}
+
+static int dlua_base64_decode(lua_State *L)
+{
+ DLUA_REQUIRE_ARGS(L, 1);
+ size_t len;
+ const char *input = luaL_checklstring(L, 1, &len);
+
+ buffer_t *output = t_base64_decode(0, input, len);
+ lua_pushlstring(L, output->data, output->used);
+ return 1;
+}
+
static luaL_Reg lua_dovecot_methods[] = {
{ "i_debug", dlua_i_debug },
{ "i_info", dlua_i_info },
{ NULL, NULL }
};
+static luaL_Reg lua_dovecot_base64_methods[] = {
+ { "encode", dlua_base64_encode },
+ { "decode", dlua_base64_decode },
+ { NULL, NULL }
+};
+
void dlua_get_dovecot(lua_State *L)
{
lua_getglobal(L, LUA_SCRIPT_DOVECOT);
/* set table's metatable, pops stack */
lua_setmetatable(script->L, -2);
+ /* create 'base64' */
+ lua_newtable(script->L);
+ luaL_setfuncs(script->L, lua_dovecot_base64_methods, 0);
+
+ /* add it to 'dovecot' */
+ lua_setfield(script->L, -2, LUA_SCRIPT_DOVECOT_BASE64);
+
/* register table as global */
lua_setglobal(script->L, LUA_SCRIPT_DOVECOT);
test_end();
}
+static void test_lua_base64(void)
+{
+ test_begin("lua base64");
+
+ struct dlua_script *script;
+ const char *error;
+
+ if (dlua_script_create_file("test-lua-base64.lua", &script, NULL, &error) < 0)
+ i_fatal("%s", error);
+
+ dlua_dovecot_register(script);
+ dlua_register(script, "test_assert", dlua_test_assert);
+
+ test_assert(dlua_script_init(script, &error) == 0);
+ test_assert(dlua_pcall(script->L, "test_base64", 0, 0, &error) == 0);
+
+ dlua_script_unref(&script);
+ test_end();
+}
+
int main(void) {
void (*tests[])(void) = {
test_lua,
test_tls,
test_compat_tointegerx_and_isinteger,
+ test_lua_base64,
NULL
};