bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#include "lib.h"
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#include "dlua-script-private.h"
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define LUA_SCRIPT_DOVECOT "dovecot"
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistatic int dlua_i_debug(lua_State *L)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi{
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi struct dlua_script *script = dlua_script_from_state(L);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *msg = luaL_checkstring(script->L, 1);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi i_debug("%s", msg);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi return 0;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi}
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistatic int dlua_i_info(lua_State *L)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi{
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi struct dlua_script *script = dlua_script_from_state(L);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *msg = luaL_checkstring(script->L, 1);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi i_info("%s", msg);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi return 0;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi}
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistatic int dlua_i_warning(lua_State *L)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi{
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi struct dlua_script *script = dlua_script_from_state(L);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *msg = luaL_checkstring(script->L, 1);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi i_warning("%s", msg);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi return 0;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi}
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistatic int dlua_i_error(lua_State *L)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi{
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi struct dlua_script *script = dlua_script_from_state(L);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *msg = luaL_checkstring(script->L, 1);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi i_error("%s", msg);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi return 0;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi}
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistatic luaL_Reg lua_dovecot_methods[] = {
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi { "i_debug", dlua_i_debug },
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi { "i_info", dlua_i_info },
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi { "i_warning", dlua_i_warning },
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi { "i_error", dlua_i_error },
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi { NULL, NULL }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi};
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid dlua_getdovecot(struct dlua_script *script)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi{
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_getglobal(script->L, LUA_SCRIPT_DOVECOT);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi}
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid dlua_dovecot_register(struct dlua_script *script)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi{
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi /* Create table for holding values */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_newtable(script->L);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi /* push new metatable to stack */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi luaL_newmetatable(script->L, LUA_SCRIPT_DOVECOT);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi /* this will register functions to the metatable itself */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi luaL_setfuncs(script->L, lua_dovecot_methods, 0);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi /* point __index to self */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_pushvalue(script->L, -1);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_setfield(script->L, -1, "__index");
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi /* set table's metatable, pops stack */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_setmetatable(script->L, -2);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi /* register table as global */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_setglobal(script->L, LUA_SCRIPT_DOVECOT);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi}