]> source.dussan.org Git - rspamd.git/commitdiff
Allow global lua functions in mime_regexp.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Mar 2015 12:44:53 +0000 (12:44 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Mar 2015 12:44:53 +0000 (12:44 +0000)
src/libmime/mime_expressions.c
src/lua/lua_common.h
src/lua/lua_task.c

index 036620984053b05a7fe678a5d6809dd1a6dc68a7..1a3179e908630f0d474fe859275bcb83d56ad682 100644 (file)
@@ -617,6 +617,9 @@ set:
                        goto err;
                }
        }
+       else if (type == MIME_ATOM_LUA_FUNCTION) {
+               mime_atom->d.lua_function = mime_atom->str;
+       }
        else {
                mime_atom->d.func = rspamd_mime_expr_parse_function_atom (mime_atom->str);
                if (mime_atom->d.func == NULL) {
@@ -955,6 +958,7 @@ rspamd_mime_expr_process (gpointer input, rspamd_expression_atom_t *atom)
 {
        struct rspamd_task *task = input;
        struct rspamd_mime_atom *mime_atom;
+       lua_State *L;
        gint ret = 0;
 
        g_assert (task != NULL);
@@ -965,6 +969,31 @@ rspamd_mime_expr_process (gpointer input, rspamd_expression_atom_t *atom)
        if (mime_atom->type == MIME_ATOM_REGEXP) {
                ret = rspamd_mime_expr_process_regexp (mime_atom->d.re, task);
        }
+       else if (mime_atom->type == MIME_ATOM_LUA_FUNCTION) {
+               L = task->cfg->lua_state;
+               lua_getglobal (L, mime_atom->d.lua_function);
+               rspamd_lua_task_push (L, task);
+
+               if (lua_pcall (L, 1, 1, 0) != 0) {
+                       msg_info ("call to %s failed: %s",
+                               mime_atom->str,
+                               lua_tostring (L, -1));
+               }
+               else {
+                       if (lua_type (L, -1) == LUA_TBOOLEAN) {
+                               ret = lua_toboolean (L, -1);
+                       }
+                       else if (lua_type (L, -1) == LUA_TNUMBER) {
+                               ret = lua_tonumber (L, 1);
+                       }
+                       else {
+                               msg_err ("%s returned wrong return type: %s",
+                                               mime_atom->str, lua_typename (L, lua_type (L, -1)));
+                       }
+                       /* Remove result */
+                       lua_pop (L, 1);
+               }
+       }
        else {
                ret = rspamd_mime_expr_process_function (mime_atom->d.func, task,
                                task->cfg->lua_state);
index ea0947c8a0c9fa13114ee16d89b5b4375aacdf05..d084f2096be96953a77ef6cb0498fa3f9c655a34 100644 (file)
@@ -122,6 +122,11 @@ void rspamd_free_lua_locked (struct lua_locked_state *st);
  */
 void rspamd_lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr);
 
+/**
+ * Push rspamd task structure to lua
+ */
+void rspamd_lua_task_push (lua_State *L, struct rspamd_task *task);
+
 /**
  * Return lua ip structure at the specified address
  */
index b15d3e1816e07efd1f463088e447582434b76cbf..703043599605cbbc9e6616920680cda9d41f9172 100644 (file)
@@ -2502,3 +2502,12 @@ luaopen_url (lua_State * L)
        lua_pop (L, 1);                      /* remove metatable from stack */
 }
 
+void
+rspamd_lua_task_push (lua_State *L, struct rspamd_task *task)
+{
+       struct rspamd_task **ptask;
+
+       ptask = lua_newuserdata (L, sizeof (gpointer));
+       rspamd_lua_setclass (L, "rspamd{task}", -1);
+       *ptask = task;
+}