]> source.dussan.org Git - rspamd.git/commitdiff
* Add initial LUA filters support
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 27 Mar 2009 15:30:40 +0000 (18:30 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 27 Mar 2009 15:30:40 +0000 (18:30 +0300)
CMakeLists.txt
config.h.in
src/cfg_file.y
src/filter.c
src/lua-rspamd.h [new file with mode: 0644]
src/lua.c [new file with mode: 0644]
src/main.c

index c1c807382f1fbc9f0d9efbe3311eae2d36134185..536d27cc6ef4569cc9d80a9685a067d4a4c0a13d 100644 (file)
@@ -19,6 +19,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR)
 OPTION(DEBUG_MODE          "Enable debug output [default: ON]"                  ON)
 OPTION(ENABLE_OPTIMIZATION "Enable optimization [default: OFF]"                 OFF)
 OPTION(ENABLE_PERL         "Enable perl support [default: ON]"                  ON)
+OPTION(ENABLE_LUA          "Enable lua support [default: OFF]"                  OFF)
 OPTION(SKIP_RELINK_RPATH   "Skip relinking and full RPATH for the install tree" OFF)
 OPTION(ENABLE_REDIRECTOR   "Enable redirector install [default: OFF]"           OFF)
 
@@ -61,6 +62,25 @@ ELSE(ENABLE_PERL MATCHES "ON")
        SET(WITHOUT_PERL 1)
 ENDIF(ENABLE_PERL MATCHES "ON")
 
+IF(ENABLE_LUA MATCHES "ON")
+       IF (ENABLE_PERL MATCHES "ON")
+               MESSAGE(FATAL_ERROR "Error: Perl and Lua support cannot be turned on together")
+       ENDIF (ENABLE_PERL MATCHES "ON")
+
+       INCLUDE(FindLua51)
+       IF(NOT LUA_FOUND)
+               # Automatic check failed, check passed variable
+               IF(LUA_INCLUDE_DIR)
+                       INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
+               ELSE(LUA_INCLUDE_DIR)
+                       MESSAGE(FATAL_ERROR "Error: Lua not found but its support is enabled")
+               ENDIF(LUA_INCLUDE_DIR)
+       ELSE(NOT LUA_FOUND)
+               INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
+       ENDIF(NOT LUA_FOUND)
+       SET(WITH_LUA 1)
+ENDIF(ENABLE_LUA MATCHES "ON")
+
 # Lex and yacc
 FIND_PROGRAM(LEX_EXECUTABLE lex)
 FIND_PROGRAM(YACC_EXECUTABLE yacc)
@@ -276,6 +296,9 @@ SET(RSPAMDSRC       src/modules.c
 IF(ENABLE_PERL MATCHES "ON")
        LIST(APPEND RSPAMDSRC src/perl.c)
 ENDIF(ENABLE_PERL MATCHES "ON")
+IF(ENABLE_LUA MATCHES "ON")
+       LIST(APPEND RSPAMDSRC src/lua.c)
+ENDIF(ENABLE_LUA MATCHES "ON")
 
 SET(TOKENIZERSSRC  src/tokenizers/tokenizers.c
                                src/tokenizers/osb.c)
@@ -366,6 +389,9 @@ ENDIF(LIBUTIL_LIBRARY)
 TARGET_LINK_LIBRARIES(rspamd event)
 TARGET_LINK_LIBRARIES(rspamd ${GLIB2_LIBRARIES})
 TARGET_LINK_LIBRARIES(rspamd ${GMIME2_LIBRARIES})
+IF(ENABLE_LUA MATCHES "ON")
+       TARGET_LINK_LIBRARIES(rspamd "${LUA_LIBRARY}")
+ENDIF(ENABLE_LUA MATCHES "ON")
 
 ADD_EXECUTABLE(test/rspamd-test ${TESTDEPENDS} ${CONTRIBSRC} ${TESTSRC})
 SET_TARGET_PROPERTIES(test/rspamd-test PROPERTIES LINKER_LANGUAGE C)
index 5aee036186508cc7b19f3b23d2d1d02fd3a2087a..6d07ba8d3aaa9f7190c3d6e88e783e7f13a10c85 100644 (file)
@@ -96,6 +96,8 @@
 
 #cmakedefine WITHOUT_PERL        1
 
+#cmakedefine WITH_LUA            1
+
 #define RVERSION          "${RSPAMD_VERSION}"
 #define RSPAMD_MASTER_SITE_URL "${RSPAMD_MASTER_SITE_URL}"
 
index a12e047c495745726d7a805a57e92bde438e3bf9..5cd98e9fc06cf22cd7226ab89a3297fbf7a2fc77 100644 (file)
@@ -385,7 +385,7 @@ requirebody:
 
 requirecmd:
        MODULE EQSIGN QUOTEDSTRING {
-#ifndef WITHOUT_PERL
+#if !defined(WITHOUT_PERL) || defined(WITH_LUA)
                struct stat st;
                struct perl_module *cur;
                if (stat ($3, &st) == -1) {
index 801b2f8bbec869ad15d543536166ffdc42a67c5f..a1679d499d7b609d9c6a8d3d2c5dcce7431d5133 100644 (file)
@@ -40,6 +40,9 @@
 #ifndef WITHOUT_PERL
 #include "perl.h"
 #endif
+#ifdef WITH_LUA
+#include "lua-rspamd.h"
+#endif
 
 void
 insert_result (struct worker_task *task, const char *metric_name, const char *symbol, double flag, GList *opts)
@@ -186,6 +189,21 @@ call_filter_by_name (struct worker_task *task, const char *name, enum filter_typ
                                        perl_call_message_filter (name, task);
                                        break;
                        }
+#elif defined(WITH_LUA)
+                       switch (sc_type) {
+                               case SCRIPT_HEADER:
+                                       lua_call_header_filter (name, task);
+                                       break;
+                               case SCRIPT_MIME:
+                                       lua_call_mime_filter (name, task);
+                                       break;
+                               case SCRIPT_URL:
+                                       lua_call_url_filter (name, task);
+                                       break;
+                               case SCRIPT_MESSAGE:
+                                       lua_call_message_filter (name, task);
+                                       break;
+                       }
 #else
                        msg_err ("call_filter_by_name: trying to call perl function while perl support is disabled %s", name);
 #endif
diff --git a/src/lua-rspamd.h b/src/lua-rspamd.h
new file mode 100644 (file)
index 0000000..a99a121
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef RSPAMD_LUA_H
+#define RSPAMD_LUA_H
+
+#include "config.h"
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+struct uri;
+struct worker_task;
+struct config_file;
+
+void init_lua_filters (struct config_file *cfg);
+
+int lua_call_header_filter (const char *function, struct worker_task *task);
+int lua_call_mime_filter (const char *function, struct worker_task *task);
+int lua_call_message_filter (const char *function, struct worker_task *task);
+int lua_call_url_filter (const char *function, struct worker_task *task);
+int lua_call_chain_filter (const char *function, struct worker_task *task, int *marks, unsigned int number);
+
+#endif
diff --git a/src/lua.c b/src/lua.c
new file mode 100644 (file)
index 0000000..e92b4e9
--- /dev/null
+++ b/src/lua.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2009, Rambler media
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *      * Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ *      * Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Rambler media ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL Rambler BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "url.h"
+#include "main.h"
+#include "lua-rspamd.h"
+#include "cfg_file.h"
+
+/* Lua module init function */
+#define MODULE_INIT_FUNC "module_init"
+
+lua_State *L = NULL;
+
+void
+init_lua_filters (struct config_file *cfg)
+{
+       struct perl_module *module;
+       char *init_func;
+       size_t funclen;
+
+       L = lua_open ();
+       luaL_openlibs (L);
+       
+       LIST_FOREACH (module, &cfg->perl_modules, next) {
+               if (module->path) {
+                       luaL_loadfile (L, module->path);
+
+                       /* Call module init function */
+                       funclen = strlen (module->path) + sizeof ("::") + sizeof (MODULE_INIT_FUNC) - 1;
+                       init_func = g_malloc (funclen);
+                       snprintf (init_func, funclen, "%s::%s", module->path, MODULE_INIT_FUNC);
+                       lua_getglobal (L, init_func);
+                       lua_pushlightuserdata (L, cfg);
+                       /* do the call (1 arguments, 1 result) */
+                       if (lua_pcall (L, 1, 1, 0) != 0) {
+                               msg_info ("lua_init_filters: call to %s failed", init_func);
+                       }
+               }
+       }
+}
+
+
+int
+lua_call_header_filter (const char *function, struct worker_task *task)
+{
+       int result;
+
+       lua_getglobal (L, function);
+       lua_pushlightuserdata (L, task);
+       
+       if (lua_pcall (L, 1, 1, 0) != 0) {
+               msg_info ("lua_init_filters: call to %s failed", function);
+       }
+
+       /* retrieve result */
+       if (!lua_isnumber (L, -1)) {
+               msg_info ("lua_call_header_filter: function %s must return a number", function);
+       }
+       result = lua_tonumber (L, -1);
+       lua_pop (L, 1);  /* pop returned value */
+       return result;
+}
+
+int
+lua_call_mime_filter (const char *function, struct worker_task *task)
+{
+       int result;
+
+       lua_getglobal (L, function);
+       lua_pushlightuserdata (L, task);
+       
+       if (lua_pcall (L, 1, 1, 0) != 0) {
+               msg_info ("lua_init_filters: call to %s failed", function);
+       }
+
+       /* retrieve result */
+       if (!lua_isnumber (L, -1)) {
+               msg_info ("lua_call_header_filter: function %s must return a number", function);
+       }
+       result = lua_tonumber (L, -1);
+       lua_pop (L, 1);  /* pop returned value */
+       return result;
+}
+
+int
+lua_call_message_filter (const char *function, struct worker_task *task)
+{
+       int result;
+
+       lua_getglobal (L, function);
+       lua_pushlightuserdata (L, task);
+       
+       if (lua_pcall (L, 1, 1, 0) != 0) {
+               msg_info ("lua_init_filters: call to %s failed", function);
+       }
+
+       /* retrieve result */
+       if (!lua_isnumber (L, -1)) {
+               msg_info ("lua_call_header_filter: function %s must return a number", function);
+       }
+       result = lua_tonumber (L, -1);
+       lua_pop (L, 1);  /* pop returned value */
+       return result;
+}
+
+int
+lua_call_url_filter (const char *function, struct worker_task *task)
+{
+       int result;
+
+       lua_getglobal (L, function);
+       lua_pushlightuserdata (L, task);
+       
+       if (lua_pcall (L, 1, 1, 0) != 0) {
+               msg_info ("lua_init_filters: call to %s failed", function);
+       }
+
+       /* retrieve result */
+       if (!lua_isnumber (L, -1)) {
+               msg_info ("lua_call_header_filter: function %s must return a number", function);
+       }
+       result = lua_tonumber (L, -1);
+       lua_pop (L, 1);  /* pop returned value */
+       return result;
+}
+
+int
+lua_call_chain_filter (const char *function, struct worker_task *task, int *marks, unsigned int number)
+{
+       int result, i;
+
+       lua_getglobal (L, function);
+
+       for (i = 0; i < number; i ++) {
+               lua_pushnumber (L, marks[i]);
+       }
+       if (lua_pcall (L, number, 1, 0) != 0) {
+               msg_info ("lua_init_filters: call to %s failed", function);
+       }
+
+       /* retrieve result */
+       if (!lua_isnumber (L, -1)) {
+               msg_info ("lua_call_header_filter: function %s must return a number", function);
+       }
+       result = lua_tonumber (L, -1);
+       lua_pop (L, 1);  /* pop returned value */
+       return result;
+}
+
index bbe24dd1be45429e13023af0fc4fa22828c3e4e0..7e78fce9a4a86e38d7db1d972f5a4f02de43e6c3 100644 (file)
@@ -30,6 +30,8 @@
 
 #ifndef WITHOUT_PERL
 #include "perl.h"
+#elif defined(WITH_LUA)
+#include "lua-rspamd.h"
 #endif
 
 /* 2 seconds to fork new process in place of dead one */
@@ -435,9 +437,9 @@ main (int argc, char **argv, char **env)
        cfg->log_fd = STDERR_FILENO;
        g_log_set_default_handler (file_log_function, cfg);
 
-       #ifndef HAVE_SETPROCTITLE
+#ifndef HAVE_SETPROCTITLE
        init_title (argc, argv, environ);
-       #endif
+#endif
        
        f = fopen (rspamd->cfg->cfg_name , "r");
        if (f == NULL) {
@@ -541,6 +543,9 @@ main (int argc, char **argv, char **env)
        PERL_SET_CONTEXT (perl_interpreter);
        perl_construct (perl_interpreter);
        perl_parse (perl_interpreter, xs_init, 3, args, NULL);
+       init_perl_filters (cfg);
+#elif defined(WITH_LUA)
+       init_lua_filters (cfg);
 #endif
 
        /* Block signals to use sigsuspend in future */