aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2011-02-16 19:32:39 +0300
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2011-02-16 19:32:39 +0300
commit0c53751a9afce121ec5da230d942e722b6f8f460 (patch)
treef6a88336e17f69103be4f2ec3414cd93d11277e0 /src
parent20b47b683bc310a47cb19833573d22043ca70ef2 (diff)
downloadrspamd-0c53751a9afce121ec5da230d942e722b6f8f460.tar.gz
rspamd-0c53751a9afce121ec5da230d942e722b6f8f460.zip
* Add ability to define composites from lua. [1]
Write to log when lua values override xml ones. [1] [1] Suggested by Victor Ustugov
Diffstat (limited to 'src')
-rw-r--r--src/cfg_xml.c5
-rw-r--r--src/lua/lua_cfg_file.c63
2 files changed, 61 insertions, 7 deletions
diff --git a/src/cfg_xml.c b/src/cfg_xml.c
index aa91bfda0..c1d0be95d 100644
--- a/src/cfg_xml.c
+++ b/src/cfg_xml.c
@@ -906,6 +906,11 @@ handle_lua (struct config_file *cfg, struct rspamd_xml_userdata *ctx, GHashTable
lua_newtable (L);
lua_setglobal (L, "metrics");
}
+ lua_getglobal (L, "composites");
+ if (lua_isnil (L, -1)) {
+ lua_newtable (L);
+ lua_setglobal (L, "composites");
+ }
/* Now config tables can be used for configuring rspamd */
/* First check "src" attribute */
if (attrs != NULL && (val = g_hash_table_lookup (attrs, "src")) != NULL) {
diff --git a/src/lua/lua_cfg_file.c b/src/lua/lua_cfg_file.c
index eff2c138a..e4f0734af 100644
--- a/src/lua/lua_cfg_file.c
+++ b/src/lua/lua_cfg_file.c
@@ -23,6 +23,8 @@
*/
#include "lua_common.h"
+#include "../expressions.h"
+#include "../symbols_cache.h"
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
@@ -101,9 +103,10 @@ static void
lua_process_metric (lua_State *L, const gchar *name, struct config_file *cfg)
{
GList *metric_list;
- gchar *symbol;
+ gchar *symbol, *old_desc;
+ const gchar *desc;
struct metric *metric;
- gdouble *score;
+ gdouble *score, *old_score;
/* Get module opt structure */
if ((metric = g_hash_table_lookup (cfg->metrics, name)) == NULL) {
@@ -132,8 +135,17 @@ lua_process_metric (lua_State *L, const gchar *name, struct config_file *cfg)
lua_pushstring (L, "description");
lua_gettable (L, -2);
if (lua_isstring (L, -1)) {
- g_hash_table_insert (metric->descriptions,
- symbol, memory_pool_strdup (cfg->cfg_pool, lua_tostring (L, -1)));
+ desc = lua_tostring (L, -1);
+ old_desc = g_hash_table_lookup (metric->descriptions, symbol);
+ if (old_desc) {
+ msg_info ("replacing description for symbol %s", symbol);
+ g_hash_table_replace (metric->descriptions,
+ symbol, memory_pool_strdup (cfg->cfg_pool, desc));
+ }
+ else {
+ g_hash_table_insert (metric->descriptions,
+ symbol, memory_pool_strdup (cfg->cfg_pool, desc));
+ }
}
lua_pop (L, 1);
}
@@ -147,7 +159,13 @@ lua_process_metric (lua_State *L, const gchar *name, struct config_file *cfg)
continue;
}
/* Insert symbol */
- g_hash_table_insert (metric->symbols, symbol, score);
+ if ((old_score = g_hash_table_lookup (metric->symbols, symbol)) != NULL) {
+ msg_info ("replacing weight for symbol %s: %.2f -> %.2f", symbol, *old_score, *score);
+ g_hash_table_replace (metric->symbols, symbol, score);
+ }
+ else {
+ g_hash_table_insert (metric->symbols, symbol, score);
+ }
if ((metric_list = g_hash_table_lookup (cfg->metrics_symbols, symbol)) == NULL) {
metric_list = g_list_prepend (NULL, metric);
@@ -244,7 +262,9 @@ void
lua_post_load_config (struct config_file *cfg)
{
lua_State *L = cfg->lua_state;
- const gchar *name;
+ const gchar *name, *val;
+ gchar *sym;
+ struct expression *expr, *old_expr;
/* First check all module options that may be overriden in 'config' global */
lua_getglobal (L, "config");
@@ -261,7 +281,7 @@ lua_post_load_config (struct config_file *cfg)
}
}
- /* First check all module options that may be overriden in 'config' global */
+ /* Check metrics settings */
lua_getglobal (L, "metrics");
if (lua_istable (L, -1)) {
@@ -276,6 +296,35 @@ lua_post_load_config (struct config_file *cfg)
}
}
+ /* Check composites */
+ lua_getglobal (L, "composites");
+
+ if (lua_istable (L, -1)) {
+ /* Iterate */
+ for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
+ /* 'key' is at index -2 and 'value' is at index -1 */
+ /* Key must be a string and value must be a table */
+ name = luaL_checkstring (L, -2);
+ if (name != NULL && lua_isstring (L, -1)) {
+ val = lua_tostring (L, -1);
+ sym = memory_pool_strdup(cfg->cfg_pool, name);
+ if ((expr = parse_expression (cfg->cfg_pool, sym)) == NULL) {
+ msg_err ("cannot parse composite expression: %s", sym);
+ continue;
+ }
+ /* Now check hash table for this composite */
+ if ((old_expr = g_hash_table_lookup (cfg->composite_symbols, name)) != NULL) {
+ msg_info ("replacing composite symbol %s", name);
+ g_hash_table_replace (cfg->composite_symbols, sym, expr);
+ }
+ else {
+ g_hash_table_insert (cfg->composite_symbols, sym, expr);
+ register_virtual_symbol (&cfg->cache, sym, 1);
+ }
+ }
+ }
+ }
+
/* Now parse all lua params */
g_hash_table_foreach (cfg->modules_opts, lua_module_callback, cfg);
}