Browse Source

Add function to execute lua rspamadm filter

tags/1.1.0
Vsevolod Stakhov 8 years ago
parent
commit
d10258807c
2 changed files with 65 additions and 0 deletions
  1. 61
    0
      src/rspamadm/rspamadm.c
  2. 4
    0
      src/rspamadm/rspamadm.h

+ 61
- 0
src/rspamadm/rspamadm.c View File

@@ -26,6 +26,8 @@
#include "rspamadm.h"
#include "rspamd.h"
#include "ottery.h"
#include "lua/lua_common.h"
#include "lua_ucl.h"
#include "unix-std.h"

#ifdef HAVE_LIBUTIL_H
@@ -191,6 +193,65 @@ rspamadm_parse_ucl_var (const gchar *option_name,
return TRUE;
}

gboolean
rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
const ucl_object_t *res, const gchar *script)
{
lua_State *L = pL;
gint err_idx, cb_idx, i;
GString *tb;

g_assert (script != NULL);
g_assert (res != NULL);
g_assert (L != NULL);

if (luaL_dostring (L, script) != 0) {
msg_err ("cannot execute lua script: %s",
lua_tostring (L, -1));
return FALSE;
}
else {
if (lua_type (L, -1) == LUA_TFUNCTION) {
cb_idx = luaL_ref (L, LUA_REGISTRYINDEX);
}
else {
msg_err ("lua script must return "
"function and not %s",
lua_typename (L,
lua_type (L, -1)));
return FALSE;
}
}

lua_pushcfunction (L, &rspamd_lua_traceback);
err_idx = lua_gettop (L);

/* Push argv */
lua_newtable (L);

for (i = 0; i < argc; i ++) {
lua_pushstring (L, argv[i]);
lua_rawseti (L, -2, i + 1);
}

/* Push results */
ucl_object_push_lua (L, res, TRUE);

if (lua_pcall (L, 1, 0, err_idx) != 0) {
tb = lua_touserdata (L, -1);
msg_err ("call to user extraction script failed: %v", tb);
g_string_free (tb, TRUE);
lua_pop (L, 1);

return FALSE;
}

/* error function */
lua_pop (L, 1);

return TRUE;
}

gint
main (gint argc, gchar **argv, gchar **env)
{

+ 4
- 0
src/rspamadm/rspamadm.h View File

@@ -26,6 +26,7 @@
#define RSPAMD_RSPAMDADM_H

#include "config.h"
#include "ucl.h"

extern GHashTable *ucl_vars;

@@ -48,4 +49,7 @@ extern struct rspamadm_command help_command;

const struct rspamadm_command *rspamadm_search_command (const gchar *name);

gboolean rspamadm_execute_lua_ucl_subr (gpointer L, gint argc, gchar **argv,
const ucl_object_t *res, const gchar *script);

#endif

Loading…
Cancel
Save