summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2009-03-27 18:30:40 +0300
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2009-03-27 18:30:40 +0300
commit89a83f2ef6c2358fe45faac5b15da6344fa7728c (patch)
tree54afb9d99c14830db4aef6a0490a92b82c1cd089 /src
parent4e3304bed44a8c275710db3d7ae7387430a52193 (diff)
downloadrspamd-89a83f2ef6c2358fe45faac5b15da6344fa7728c.tar.gz
rspamd-89a83f2ef6c2358fe45faac5b15da6344fa7728c.zip
* Add initial LUA filters support
Diffstat (limited to 'src')
-rw-r--r--src/cfg_file.y2
-rw-r--r--src/filter.c18
-rw-r--r--src/lua-rspamd.h21
-rw-r--r--src/lua.c171
-rw-r--r--src/main.c9
5 files changed, 218 insertions, 3 deletions
diff --git a/src/cfg_file.y b/src/cfg_file.y
index a12e047c4..5cd98e9fc 100644
--- a/src/cfg_file.y
+++ b/src/cfg_file.y
@@ -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) {
diff --git a/src/filter.c b/src/filter.c
index 801b2f8bb..a1679d499 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -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
index 000000000..a99a121a3
--- /dev/null
+++ b/src/lua-rspamd.h
@@ -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
index 000000000..e92b4e999
--- /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;
+}
+
diff --git a/src/main.c b/src/main.c
index bbe24dd1b..7e78fce9a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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 */