Skip to content

Commit

Permalink
plugin-lua: Export a few functions to the Lua script
Browse files Browse the repository at this point in the history
Adds the following functions for use in the script:

getenv
getnum
getstr
msg
putenv
setenv
setnum
setstr
  • Loading branch information
bruceg committed Apr 7, 2011
1 parent 8350ee9 commit a25ebb7
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 4 deletions.
142 changes: 141 additions & 1 deletion plugin-lua.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,145 @@
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "mailfront.h"
#include <msg/msg.h>
#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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions plugin-lua.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,49 @@ <h2>Configuration</h2>

</dl>

<h2>Library</h2>

<p>The plugin registers some mailfront functions for script use:</p>

<dl>

<dt><tt>getenv(...)</tt></dt>
<dd>Returns the (string) value of each of the named session environment
variables.</dd>

<dt><tt>getnum(name, default)</tt></dt>
<dd>Returns the named session number, or <tt>default</tt> if it is not set.</dd>

<dt><tt>getstr(...)</tt></dt>
<dd>Returns the (string) value of each of the named session strings.</dd>

<dt><tt>msg(...)</tt></dt>
<dd>Outputs one message line (into the logs) for each parameter. The
script is responsible for formatting.</dd>

<dt><tt>putenv(...)</tt></dt>
<dd>Puts the given environment strings into the session environment.
The strings must be in the form "<tt>NAME=VALUE</tt>"</dd>

<dt><tt>setenv(name, value, overwrite)</tt></dt>
<dd>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.</dd>

<dt><tt>setnum(name, value)</tt></dt>
<dd>Sets the named session number. These numbers may be used by other
plugins to modify their behavior. For example,
the <a href="plugin-counters.html">counters plugin</a> uses the minimum
value of the session environment variable <tt>$DATABYTES</tt> and the
session number <tt>maxdatabytes</tt> for the maximum message size.</dd>

<dt><tt>setstr(name, value)</tt></dt>
<dd>Sets the named session string. These strings may be used by other
plugins to modify their behavior. For example,
the <a href="plugin-add-received.html">add-received plugin</a> uses
the <tt>helo_domain</tt> string in the header(s) it generates.</dd>

</dl>

</body>
</html>
65 changes: 62 additions & 3 deletions tests/plugin-lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export LUA_SCRIPT=script.lua
doit() {
cat > script.lua
echo
sfecho lua <<EOF
sfecho lua add-received counters <<EOF
HELO helohost
MAIL FROM:<sender>
MAIL FROM:<sender> SIZE=13
RCPT TO:<recip1>
RCPT TO:<recip2>
DATA
Expand Down Expand Up @@ -41,6 +41,36 @@ function data_start(fd)
end
EOF

doit <<EOF
setstr('helo_domain', '01234567890123456789012345678901234567890123456789')
EOF

doit <<EOF
setnum('maxdatabytes', 1)
EOF

doit <<EOF
function recipient(s)
setnum('maxdatabytes', 1)
end
EOF

doit <<EOF
count=0
function recipient(s)
count = count + 1
return 252,'recipient #'..count
end
function data_start(fd)
bytes = 0
end
function data_block(s)
bytes = bytes + ( # s )
end
function message_end(fd)
return 450,bytes .. ' bytes, ' .. count .. ' recipients, ' .. (bytes*count) .. ' total'
end
EOF
rm -f $LUA_SCRIPT
unset LUA_SCRIPT

Expand All @@ -53,7 +83,7 @@ unset LUA_SCRIPT
252 Lua recip=recip1^M
252 Lua recip=recip2^M
354 End your message with a period on a line by itself.^M
250 Received 12 bytes.^M
250 Received 137 bytes.^M

250 local.host^M
250 Sender='sender'.^M
Expand All @@ -62,3 +92,32 @@ unset LUA_SCRIPT
421 start fd=-1^M
500 5.5.1 Not implemented.^M
500 5.5.1 Not implemented.^M

250 local.host^M
250 Sender='sender'.^M
250 Recipient='recip1'.^M
250 Recipient='recip2'.^M
354 End your message with a period on a line by itself.^M
250 Received 179 bytes.^M

250 local.host^M
552 5.2.3 The message would exceed the maximum message size.^M
503 5.5.1 You must send MAIL FROM: first^M
503 5.5.1 You must send MAIL FROM: first^M
503 5.5.1 You must send MAIL FROM: first^M
500 5.5.1 Not implemented.^M
500 5.5.1 Not implemented.^M

250 local.host^M
250 Sender='sender'.^M
250 Recipient='recip1'.^M
250 Recipient='recip2'.^M
354 End your message with a period on a line by itself.^M
552 5.2.3 Sorry, that message exceeds the maximum message length.^M

250 local.host^M
250 Sender='sender'.^M
252 recipient #1^M
252 recipient #2^M
354 End your message with a period on a line by itself.^M
450 12 bytes, 2 recipients, 24 total^M

0 comments on commit a25ebb7

Please sign in to comment.