diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-12-14 19:10:54 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-12-14 19:10:54 +0000 |
commit | d10258807cdfb85ccf103959b50d954d08ddb3cc (patch) | |
tree | 1aaa0e539856241ead5dddc9372211201c38500a /src | |
parent | 0bb433da33b405be40504176e934880618307dfd (diff) | |
download | rspamd-d10258807cdfb85ccf103959b50d954d08ddb3cc.tar.gz rspamd-d10258807cdfb85ccf103959b50d954d08ddb3cc.zip |
Add function to execute lua rspamadm filter
Diffstat (limited to 'src')
-rw-r--r-- | src/rspamadm/rspamadm.c | 61 | ||||
-rw-r--r-- | src/rspamadm/rspamadm.h | 4 |
2 files changed, 65 insertions, 0 deletions
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c index 04587455f..ac12359e1 100644 --- a/src/rspamadm/rspamadm.c +++ b/src/rspamadm/rspamadm.c @@ -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) { diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h index e7a90b5bb..e5458ef4e 100644 --- a/src/rspamadm/rspamadm.h +++ b/src/rspamadm/rspamadm.h @@ -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 |