From d111e5e161f9bc3ae388e8e874651d6b5656ba1a Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 17 Apr 2015 16:01:47 +0100 Subject: [PATCH] Start common lua routines module. --- src/lua/CMakeLists.txt | 3 +- src/lua/lua_common.c | 1 + src/lua/lua_common.h | 2 + src/lua/lua_config.c | 48 ++++++------- src/lua/lua_util.c | 148 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 25 deletions(-) create mode 100644 src/lua/lua_util.c diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt index 4c7867b71..34ec9b2dc 100644 --- a/src/lua/CMakeLists.txt +++ b/src/lua/CMakeLists.txt @@ -20,6 +20,7 @@ SET(LUASRC ${CMAKE_CURRENT_SOURCE_DIR}/lua_common.c ${CMAKE_CURRENT_SOURCE_DIR}/lua_expression.c ${CMAKE_CURRENT_SOURCE_DIR}/lua_trie.c ${CMAKE_CURRENT_SOURCE_DIR}/lua_mimepart.c - ${CMAKE_CURRENT_SOURCE_DIR}/lua_url.c) + ${CMAKE_CURRENT_SOURCE_DIR}/lua_url.c + ${CMAKE_CURRENT_SOURCE_DIR}/lua_util.c) SET(RSPAMD_LUA ${LUASRC} PARENT_SCOPE) \ No newline at end of file diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 1077fd00e..125acfaf0 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -235,6 +235,7 @@ rspamd_lua_init (struct rspamd_config *cfg) luaopen_ip (L); luaopen_expression (L); luaopen_text (L); + luaopen_util (L); rspamd_lua_add_preload (L, "ucl", luaopen_ucl); diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index fe4344c20..824a8dcfe 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -211,6 +211,7 @@ void luaopen_ip (lua_State * L); void luaopen_expression (lua_State * L); void luaopen_logger (lua_State * L); void luaopen_text (lua_State *L); +void luaopen_util (lua_State * L); gint rspamd_lua_call_filter (const gchar *function, struct rspamd_task *task); gint rspamd_lua_call_chain_filter (const gchar *function, @@ -254,6 +255,7 @@ void rspamd_lua_dumpstack (lua_State *L); void rspamd_lua_set_path (lua_State *L, struct rspamd_config *cfg); struct memory_pool_s * rspamd_lua_check_mempool (lua_State * L, gint pos); +struct rspamd_config * lua_check_config (lua_State * L, gint pos); #endif /* WITH_LUA */ diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index accb47fab..54c4d74de 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -335,11 +335,11 @@ static const struct luaL_reg hashlib_m[] = { {NULL, NULL} }; -static struct rspamd_config * -lua_check_config (lua_State * L) +struct rspamd_config * +lua_check_config (lua_State * L, gint pos) { - void *ud = luaL_checkudata (L, 1, "rspamd{config}"); - luaL_argcheck (L, ud != NULL, 1, "'config' expected"); + void *ud = luaL_checkudata (L, pos, "rspamd{config}"); + luaL_argcheck (L, ud != NULL, pos, "'config' expected"); return ud ? *((struct rspamd_config **)ud) : NULL; } @@ -370,7 +370,7 @@ lua_config_get_api_version (lua_State *L) static gint lua_config_get_module_opt (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *mname, *optname; const ucl_object_t *obj; @@ -393,7 +393,7 @@ static int lua_config_get_mempool (lua_State * L) { rspamd_mempool_t **ppool; - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); if (cfg != NULL) { ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *)); @@ -406,7 +406,7 @@ lua_config_get_mempool (lua_State * L) static gint lua_config_get_all_opt (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *mname; const ucl_object_t *obj; @@ -428,7 +428,7 @@ lua_config_get_all_opt (lua_State * L) static gint lua_config_get_classifier (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); struct rspamd_classifier_config *clc = NULL, **pclc = NULL; const gchar *name; GList *cur; @@ -522,7 +522,7 @@ rspamd_lua_call_post_filters (struct rspamd_task *task) static gint lua_config_register_post_filter (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); struct lua_callback_data *cd; if (cfg) { @@ -582,7 +582,7 @@ rspamd_lua_call_pre_filters (struct rspamd_task *task) static gint lua_config_register_pre_filter (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); struct lua_callback_data *cd; if (cfg) { @@ -612,7 +612,7 @@ lua_config_register_pre_filter (lua_State *L) static gint lua_config_add_radix_map (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; radix_compressed_t **r, ***ud; @@ -643,7 +643,7 @@ lua_config_add_radix_map (lua_State *L) static gint lua_config_radix_from_config (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *mname, *optname; const ucl_object_t *obj; radix_compressed_t **r, ***ud; @@ -681,7 +681,7 @@ lua_config_radix_from_config (lua_State *L) static gint lua_config_add_hash_map (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; GHashTable **r, ***ud; @@ -715,7 +715,7 @@ lua_config_add_hash_map (lua_State *L) static gint lua_config_add_kv_map (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; GHashTable **r, ***ud; @@ -749,7 +749,7 @@ lua_config_add_kv_map (lua_State *L) static gint lua_config_get_key (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *name; size_t namelen; const ucl_object_t *val; @@ -865,7 +865,7 @@ rspamd_register_symbol_fromlua (lua_State *L, static gint lua_config_register_symbol (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; @@ -896,7 +896,7 @@ lua_config_register_symbol (lua_State * L) static gint lua_config_register_symbols (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); gint i, top, idx; gchar *sym; gdouble weight = 1.0; @@ -956,7 +956,7 @@ lua_config_register_symbols (lua_State *L) static gint lua_config_register_virtual_symbol (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; @@ -973,7 +973,7 @@ lua_config_register_virtual_symbol (lua_State * L) static gint lua_config_register_callback_symbol (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; @@ -1004,7 +1004,7 @@ lua_config_register_callback_symbol (lua_State * L) static gint lua_config_register_callback_symbol_priority (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; gint priority; @@ -1037,7 +1037,7 @@ lua_config_register_callback_symbol_priority (lua_State * L) static gint lua_config_set_metric_symbol (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); GList *metric_list; gchar *name; const gchar *metric_name = DEFAULT_METRIC, *description = NULL; @@ -1104,7 +1104,7 @@ lua_config_set_metric_symbol (lua_State * L) static gint lua_config_add_composite (lua_State * L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); struct rspamd_expression *expr; gchar *name; const gchar *expr_str; @@ -1153,7 +1153,7 @@ lua_config_add_composite (lua_State * L) static gint lua_config_newindex (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *name; name = luaL_checkstring (L, 2); @@ -1313,7 +1313,7 @@ lua_map_fin (rspamd_mempool_t * pool, struct map_cb_data *data) static gint lua_config_add_map (lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L); + struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; struct lua_map_callback_data *cbdata, **pcbdata; int cbidx; diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c new file mode 100644 index 000000000..3a91634db --- /dev/null +++ b/src/lua/lua_util.c @@ -0,0 +1,148 @@ +/* Copyright (c) 2015, 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 AUTHOR 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 "task.h" +#include "main.h" +#include "cfg_rcl.h" + +/*** + * @function util.create_event_base() + * Creates new event base for processing asynchronous events + * @return {ev_base} new event processing base + */ +LUA_FUNCTION_DEF (util, create_event_base); +/*** + * @function util.load_rspamd_config(filename) + * Load rspamd config from the specified file + * @return {confg} new configuration object suitable for access + */ +LUA_FUNCTION_DEF (util, load_rspamd_config); +/*** + * @function util.config_from_ucl(any) + * Load rspamd config from ucl reperesented by any lua table + * @return {confg} new configuration object suitable for access + */ +LUA_FUNCTION_DEF (util, config_from_ucl); +LUA_FUNCTION_DEF (util, process_message); + +static const struct luaL_reg utillib_f[] = { + LUA_INTERFACE_DEF (util, create_event_base), + LUA_INTERFACE_DEF (util, load_rspamd_config), + LUA_INTERFACE_DEF (util, config_from_ucl), + LUA_INTERFACE_DEF (util, process_message), + {NULL, NULL} +}; + +static gint +lua_util_create_event_base (lua_State *L) +{ + struct event_base **pev_base; + + pev_base = lua_newuserdata (L, sizeof (struct event_base *)); + rspamd_lua_setclass (L, "rspamd{ev_base}", -1); + *pev_base = event_init (); + + return 1; +} + +static gint +lua_util_load_rspamd_config (lua_State *L) +{ + struct rspamd_config *cfg, **pcfg; + const gchar *cfg_name; + + cfg_name = luaL_checkstring (L, 1); + + if (cfg_name) { + cfg = g_malloc0 (sizeof (struct rspamd_config)); + rspamd_init_cfg (cfg, FALSE); + + if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL)) { + msg_err ("cannot load config from %s", cfg_name); + lua_pushnil (L); + } + else { + rspamd_config_post_load (cfg); + pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); + rspamd_lua_setclass (L, "rspamd{config}", -1); + *pcfg = cfg; + } + } + + return 1; +} + +static gint +lua_util_config_from_ucl (lua_State *L) +{ + struct rspamd_config *cfg, **pcfg; + struct rspamd_rcl_section *top; + GError *err = NULL; + ucl_object_t *obj; + + obj = ucl_object_lua_import (L, 1); + + if (obj) { + cfg = g_malloc0 (sizeof (struct rspamd_config)); + rspamd_init_cfg (cfg, FALSE); + + cfg->rcl_obj = obj; + top = rspamd_rcl_config_init (); + + if (!rspamd_rcl_parse (top, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { + msg_err ("rcl parse error: %s", err->message); + ucl_object_unref (obj); + lua_pushnil (L); + } + else { + rspamd_config_post_load (cfg); + pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); + rspamd_lua_setclass (L, "rspamd{config}", -1); + *pcfg = cfg; + } + } + + return 1; +} + +static gint +lua_util_process_message (lua_State *L) +{ + return 0; +} + +static gint +lua_load_util (lua_State * L) +{ + lua_newtable (L); + luaL_register (L, NULL, utillib_f); + + return 1; +} + +void +luaopen_util (lua_State * L) +{ + rspamd_lua_add_preload (L, "rspamd_util", lua_load_util); +} -- 2.39.5