From a25ebb709ded9081769018e8b7dbcfee846a06c6 Mon Sep 17 00:00:00 2001 From: Bruce Guenter Date: Wed, 6 Apr 2011 16:23:34 -0600 Subject: [PATCH] plugin-lua: Export a few functions to the Lua script Adds the following functions for use in the script: getenv getnum getstr msg putenv setenv setnum setstr --- plugin-lua.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++- plugin-lua.html | 44 +++++++++++++++ tests/plugin-lua | 65 +++++++++++++++++++++- 3 files changed, 247 insertions(+), 4 deletions(-) diff --git a/plugin-lua.c b/plugin-lua.c index 50bac62..0863a01 100644 --- a/plugin-lua.c +++ b/plugin-lua.c @@ -1,8 +1,145 @@ #include #include #include -#include "mailfront.h" #include +#include "mailfront.h" + +static int l_msg(lua_State *L) +{ + int i; + for (i = 1; i <= lua_gettop(L); i++) + msg1(lua_tostring(L, i)); + return 0; +} + +static int l_getenv(lua_State *L) +{ + int i; + const int nparams = lua_gettop(L); + for (i = 1; i < nparams; i++) { + const char* name = lua_tostring(L, i); + const char* value = session_getenv(name); + if (value == 0) + lua_pushnil(L); + else + lua_pushstring(L, value); + } + return nparams; +} + +static int l_putenv(lua_State *L) +{ + const int nparams = lua_gettop(L); + int i; + for (i = 1; i < nparams; i++) { + if (!session_putenv(lua_tostring(L, i))) { + lua_pushstring(L, "Out of memory"); + lua_error(L); + } + } + return 0; +} + +static int l_setenv(lua_State *L) +{ + if (lua_gettop(L) != 3) { + lua_pushstring(L, "Incorrect number of parameters to setenv"); + lua_error(L); + } + const char* name = lua_tostring(L, 1); + const char* value = lua_tostring(L, 2); + int overwrite = lua_tointeger(L, 3); + if (!session_setenv(name, value, overwrite)) { + lua_pushstring(L, "setenv failed"); + lua_error(L); + } + return 0; +} + +static int l_delnum(lua_State *L) +{ + const int nparams = lua_gettop(L); + int i; + for (i = 1; i < nparams; i++) + session_delnum(lua_tostring(L, i)); + return 0; +} + +static int l_delstr(lua_State *L) +{ + const int nparams = lua_gettop(L); + int i; + for (i = 1; i < nparams; i++) + session_delstr(lua_tostring(L, i)); + return 0; +} + +static int l_getnum(lua_State *L) +{ + if (lua_gettop(L) != 2) { + lua_pushstring(L, "Incorrect number of parameters to getnum"); + lua_error(L); + } + const char* name = lua_tostring(L, 1); + unsigned long dflt = lua_tonumber(L, 2); + lua_pushnumber(L, session_getnum(name, dflt)); + return 1; +} + +static int l_getstr(lua_State *L) +{ + const int nparams = lua_gettop(L); + int i; + for (i = 1; i < nparams; i++) { + const char* s = session_getstr(lua_tostring(L, i)); + if (s != 0) + lua_pushstring(L, s); + else + lua_pushnil(L); + } + return nparams; +} + +static int l_setnum(lua_State *L) +{ + if (lua_gettop(L) != 2) { + lua_pushstring(L, "Incorrect number of parameters to setnum"); + lua_error(L); + } + const char* name = lua_tostring(L, 1); + unsigned long value = lua_tonumber(L, 2); + session_setnum(name, value); + return 0; +} + +static int l_setstr(lua_State *L) +{ + if (lua_gettop(L) != 2) { + lua_pushstring(L, "Incorrect number of parameters to setstr"); + lua_error(L); + } + const char* name = lua_tostring(L, 1); + const char* value = lua_tostring(L, 2); + session_setstr(name, value); + return 0; +} + +static const luaL_Reg library[] = { + { "msg", l_msg }, + + { "getenv", l_getenv }, + { "putenv", l_putenv }, + { "setenv", l_setenv }, + + { "delnum", l_delnum }, + { "delstr", l_delstr }, + { "getnum", l_getnum }, + { "getstr", l_getstr }, + { "setnum", l_setnum }, + { "setstr", l_setstr }, + + { NULL, NULL } +}; static lua_State* L = 0; @@ -53,6 +190,7 @@ static const response* callit(int nparams) static const response* init(void) { const char* env; + const luaL_Reg *lp; env = session_getenv("LUA_SCRIPT"); if (env != 0) { @@ -73,6 +211,8 @@ static const response* init(void) default: return &resp_internal; } + for (lp = library; lp->name != 0; lp++) + lua_register(L, lp->name, lp->func); return callit(0); } return 0; diff --git a/plugin-lua.html b/plugin-lua.html index f04b5e4..9d8a98f 100644 --- a/plugin-lua.html +++ b/plugin-lua.html @@ -28,5 +28,49 @@

Configuration

+

Library

+ +

The plugin registers some mailfront functions for script use:

+ +
+ +
getenv(...)
+
Returns the (string) value of each of the named session environment +variables.
+ +
getnum(name, default)
+
Returns the named session number, or default if it is not set.
+ +
getstr(...)
+
Returns the (string) value of each of the named session strings.
+ +
msg(...)
+
Outputs one message line (into the logs) for each parameter. The +script is responsible for formatting.
+ +
putenv(...)
+
Puts the given environment strings into the session environment. +The strings must be in the form "NAME=VALUE"
+ +
setenv(name, value, overwrite)
+
Sets a session environment variable which may be used by other +plugins to modify their behavior. Does not return any values. Raises +an error if any problems occur.
+ +
setnum(name, value)
+
Sets the named session number. These numbers may be used by other +plugins to modify their behavior. For example, +the counters plugin uses the minimum +value of the session environment variable $DATABYTES and the +session number maxdatabytes for the maximum message size.
+ +
setstr(name, value)
+
Sets the named session string. These strings may be used by other +plugins to modify their behavior. For example, +the add-received plugin uses +the helo_domain string in the header(s) it generates.
+ +
+ diff --git a/tests/plugin-lua b/tests/plugin-lua index 8ee27ac..8dc8e09 100644 --- a/tests/plugin-lua +++ b/tests/plugin-lua @@ -2,9 +2,9 @@ export LUA_SCRIPT=script.lua doit() { cat > script.lua echo - sfecho lua < +MAIL FROM: SIZE=13 RCPT TO: RCPT TO: DATA @@ -41,6 +41,36 @@ function data_start(fd) end EOF +doit <