aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2011-03-05 22:47:28 +0300
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2011-03-05 22:47:28 +0300
commitc9af649b3316857e77ec3509ca93c4a4ff37f477 (patch)
tree5b997f78a49366d3bb07f32cae29ed6b4dd25cf4 /src/lua
parent5b4b47c6c4d81d0ca082617293d7284396998e0d (diff)
downloadrspamd-c9af649b3316857e77ec3509ca93c4a4ff37f477.tar.gz
rspamd-c9af649b3316857e77ec3509ca93c4a4ff37f477.zip
* Add ability to lookup CDB maps from lua
* Add cdb:// map to multimap plugin
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/CMakeLists.txt3
-rw-r--r--src/lua/lua_cdb.c152
-rw-r--r--src/lua/lua_common.c1
-rw-r--r--src/lua/lua_common.h3
4 files changed, 157 insertions, 2 deletions
diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt
index 3ec763539..63ecfe42b 100644
--- a/src/lua/CMakeLists.txt
+++ b/src/lua/CMakeLists.txt
@@ -5,7 +5,8 @@ SET(LUASRC lua_common.c
lua_config.c
lua_classifier.c
lua_cfg_file.c
- lua_regexp.c)
+ lua_regexp.c
+ lua_cdb.c)
ADD_LIBRARY(rspamd_lua STATIC ${LUASRC})
TARGET_LINK_LIBRARIES(rspamd_lua ${LUALIB})
diff --git a/src/lua/lua_cdb.c b/src/lua/lua_cdb.c
new file mode 100644
index 000000000..ef6041d7c
--- /dev/null
+++ b/src/lua/lua_cdb.c
@@ -0,0 +1,152 @@
+/* Copyright (c) 2010, Vsevolod Stakhov
+ * 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 ''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 "lua_common.h"
+#include "../cdb/cdb.h"
+
+LUA_FUNCTION_DEF (cdb, create);
+LUA_FUNCTION_DEF (cdb, lookup);
+LUA_FUNCTION_DEF (cdb, get_name);
+LUA_FUNCTION_DEF (cdb, destroy);
+
+static const struct luaL_reg cdblib_m[] = {
+ LUA_INTERFACE_DEF (cdb, lookup),
+ LUA_INTERFACE_DEF (cdb, get_name),
+ {"__tostring", lua_class_tostring},
+ {"__gc", lua_cdb_destroy},
+ {NULL, NULL}
+};
+static const struct luaL_reg cdblib_f[] = {
+ LUA_INTERFACE_DEF (cdb, create),
+ {NULL, NULL}
+};
+
+static struct cdb *
+lua_check_cdb (lua_State * L)
+{
+ void *ud = luaL_checkudata (L, 1, "rspamd{cdb}");
+
+ luaL_argcheck (L, ud != NULL, 1, "'cdb' expected");
+ return *((struct cdb **)ud);
+}
+
+static gint
+lua_cdb_create (lua_State *L)
+{
+ struct cdb *cdb, **pcdb;
+ const gchar *filename;
+ gint fd;
+
+ filename = luaL_checkstring (L, 1);
+ /* If file begins with cdb://, just skip it */
+ if (g_ascii_strncasecmp (filename, "cdb://", sizeof ("cdb://") - 1) == 0) {
+ filename += sizeof ("cdb://") - 1;
+ }
+
+ if ((fd = open (filename, O_RDONLY)) == -1) {
+ msg_warn ("cannot open cdb: %s, %s", filename, strerror (errno));
+ lua_pushnil (L);
+ }
+ else {
+ cdb = g_malloc (sizeof (struct cdb));
+ cdb->filename = g_strdup (filename);
+ if (cdb_init (cdb, fd) == -1) {
+ msg_warn ("cannot open cdb: %s, %s", filename, strerror (errno));
+ lua_pushnil (L);
+ }
+ else {
+ pcdb = lua_newuserdata (L, sizeof (struct cdb*));
+ lua_setclass (L, "rspamd{cdb}", -1);
+ *pcdb = cdb;
+ }
+ }
+
+ return 1;
+}
+
+static gint
+lua_cdb_get_name (lua_State *L)
+{
+ struct cdb *cdb = lua_check_cdb (L);
+
+ lua_pushstring (L, cdb->filename);
+ return 1;
+}
+
+static gint
+lua_cdb_lookup (lua_State *L)
+{
+ struct cdb *cdb = lua_check_cdb (L);
+ const gchar *what;
+ gchar *value;
+ gsize vlen;
+ goffset vpos;
+
+ what = luaL_checkstring (L, 2);
+ if (cdb_find (cdb, what, strlen (what)) > 0) {
+ /* Extract and push value to lua as string */
+ vpos = cdb_datapos (cdb);
+ vlen = cdb_datalen (cdb);
+ value = g_malloc (vlen);
+ cdb_read (cdb, value, vlen, vpos);
+ lua_pushlstring (L, value, vlen);
+ g_free (value);
+ }
+ else {
+ lua_pushnil (L);
+ }
+
+ return 1;
+}
+
+static gint
+lua_cdb_destroy (lua_State *L)
+{
+ struct cdb *cdb = lua_check_cdb (L);
+
+ if (cdb) {
+ cdb_free (cdb);
+ g_free (cdb->filename);
+ g_free (cdb);
+ }
+
+ return 0;
+}
+
+gint
+luaopen_cdb (lua_State * L)
+{
+ luaL_newmetatable (L, "rspamd{cdb}");
+ lua_pushstring (L, "__index");
+ lua_pushvalue (L, -2);
+ lua_settable (L, -3);
+
+ lua_pushstring (L, "class");
+ lua_pushstring (L, "rspamd{cdb}");
+ lua_rawset (L, -3);
+
+ luaL_openlib (L, NULL, cdblib_m, 0);
+ luaL_openlib(L, "cdb", cdblib_f, 0);
+
+ return 1;
+}
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index 1f9f7285d..ada44981b 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -238,6 +238,7 @@ init_lua (struct config_file *cfg)
(void)luaopen_classifier (L);
(void)luaopen_statfile (L);
(void)luaopen_glib_regexp (L);
+ (void)luaopen_cdb (L);
cfg->lua_state = L;
memory_pool_add_destructor (cfg->cfg_pool, (pool_destruct_func)lua_close, L);
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index f6b69e0e0..9b0c356f3 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -16,7 +16,7 @@
extern const luaL_reg null_reg[];
-#define RSPAMD_LUA_API_VERSION 2
+#define RSPAMD_LUA_API_VERSION 3
/* Common utility functions */
void lua_newclass (lua_State *L, const gchar *classname, const struct luaL_reg *func);
@@ -36,6 +36,7 @@ gint luaopen_url (lua_State *L);
gint luaopen_classifier (lua_State *L);
gint luaopen_statfile (lua_State * L);
gint luaopen_glib_regexp (lua_State *L);
+gint luaopen_cdb (lua_State *L);
void init_lua (struct config_file *cfg);
gboolean init_lua_filters (struct config_file *cfg);