]> source.dussan.org Git - rspamd.git/commitdiff
* Fix variables substitution
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 27 Mar 2009 16:39:01 +0000 (19:39 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 27 Mar 2009 16:39:01 +0000 (19:39 +0300)
* Add simple lua interface to access task structures
* Improve error reporting for regexp module

perl/Rspamd/Task.xs
src/cfg_file.h
src/cfg_utils.c
src/lua.c
src/plugins/regexp.c

index 31928bf7b0bc5cee6b4c51d13aa94740bc312cd2..e46a1148cac4685760512e0982852c38596c1e99 100644 (file)
@@ -73,7 +73,7 @@ rspamd_task_get_urls (task)
                struct uri *url;
        CODE:
                retav = newAV ();
-               TAILQ_FOREACH (url, &task.urls, next) {
+               TAILQ_FOREACH (url, &task->urls, next) {
                        av_push (retav, newSVpv ((char *)g_strdup (struri (url)), 0));
                }
 
index fbf7309d3fb4a1f00ff074bd5fd49134e5ab5051..b1cbd61252b423c819b334ecd95a924aea588ff7 100644 (file)
@@ -285,11 +285,12 @@ char parse_flag (const char *str);
 /**
  * Substitutes variable in specified string, may be recursive (eg. ${var1${var2}})
  * @param cfg config file
+ * @param name variable's name
  * @param str incoming string
  * @param recursive whether do recursive scanning
  * @return new string with substituted variables (uses cfg memory pool for allocating)
  */
-char* substitute_variable (struct config_file *cfg, char *str, u_char recursive);
+char* substitute_variable (struct config_file *cfg, char *name, char *str, u_char recursive);
 
 /**
  * Do post load actions for config
index b7c540ec44de4409c1b6f9757028a1ac8785bca8..fa2bcc918d942a7e9fc81b2661f1bf8629fbe0ab 100644 (file)
@@ -352,10 +352,11 @@ parse_flag (const char *str)
  * Return: newly allocated string with substituted variables (original string may be freed if variables are found)
  */
 char *
-substitute_variable (struct config_file *cfg, char *str, u_char recursive)
+substitute_variable (struct config_file *cfg, char *name, char *str, u_char recursive)
 {
        char *var, *new, *v_begin, *v_end;
        size_t len;
+       gboolean changed = FALSE;
 
        if (str == NULL) {
                yywarn ("substitute_variable: trying to substitute variable in NULL string");
@@ -378,7 +379,7 @@ substitute_variable (struct config_file *cfg, char *str, u_char recursive)
                        var = "";
                }
                else if (recursive) {
-                       var = substitute_variable (cfg, var, recursive);
+                       new = substitute_variable (cfg, v_begin, var, recursive);
                }
                /* Allocate new string */
                new = memory_pool_alloc (cfg->cfg_pool, len - strlen (v_begin) + strlen (var) + 1);
@@ -386,8 +387,12 @@ substitute_variable (struct config_file *cfg, char *str, u_char recursive)
                snprintf (new, len - strlen (v_begin) + strlen (var) + 1, "%s%s%s",
                                                str, var, v_end + 1);
                str = new;
+               changed = TRUE;
+       }
+       
+       if (changed && name != NULL) {
+               g_hash_table_insert (cfg->variables, name, str);
        }
-
        return str;
 }
 
@@ -400,7 +405,7 @@ substitute_module_variables (gpointer key, gpointer value, gpointer data)
 
        LIST_FOREACH_SAFE (cur, cur_module_opt, next, tmp) {
                if (cur->value) {
-                       cur->value = substitute_variable (cfg, cur->value, 0);
+                       cur->value = substitute_variable (cfg, NULL, cur->value, 1);
                }
        }
 }
@@ -412,10 +417,7 @@ substitute_all_variables (gpointer key, gpointer value, gpointer data)
        char *new;
 
        /* Do recursive substitution */
-       new = substitute_variable (cfg, (char *)value, 1);
-       if (new != value) {
-               g_hash_table_replace (cfg->variables, key, new);
-       }
+       new = substitute_variable (cfg, (char *)key, (char *)value, 1);
 }
 
 static void
index e92b4e9996ddc4f1a9969b6734a9ab2df6e50f24..1df52310ac79f7b21013c77754a2da6d82fd890c 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
 
 lua_State *L = NULL;
 
+static int lua_task_get_message (lua_State *L);
+static int lua_task_insert_result (lua_State *L);
+static int lua_task_get_urls (lua_State *L);
+
+/* Task methods */
+static const struct luaL_reg tasklib_m[] = {
+       {"get_message", lua_task_get_message},
+       {"insert_result", lua_task_insert_result},
+       {"get_urls", lua_task_get_urls},
+       {NULL, NULL},
+};
+
+static struct worker_task *
+lua_check_task (lua_State *L) 
+{
+       void *ud = luaL_checkudata (L, 1, "Rspamd.task");
+       luaL_argcheck (L, ud != NULL, 1, "'task' expected");
+       return (struct worker_task *)ud;
+}
+
+static int
+lua_task_get_message (lua_State *L)
+{
+       struct worker_task *task = lua_check_task (L);
+       if (task != NULL) {
+               /* XXX write handler for message object */
+       }
+       return 1;
+}
+
+static int 
+lua_task_insert_result (lua_State *L)
+{
+       struct worker_task *task = lua_check_task (L);
+       const char *metric_name, *symbol_name;
+       double flag;
+
+       if (task != NULL) {
+               metric_name = luaL_checkstring (L, 2);
+               symbol_name = luaL_checkstring (L, 3);
+               flag = luaL_checknumber (L, 4);
+               insert_result (task, metric_name, symbol_name, flag, NULL);
+       }
+       return 1;
+}
+
+static int 
+lua_task_get_urls (lua_State *L)
+{
+       struct worker_task *task = lua_check_task (L);
+       struct uri *url;
+
+       if (task != NULL) {
+               TAILQ_FOREACH (url, &task->urls, next) {        
+                       lua_pushstring (L, struri (url));       
+               }
+       }
+       return 1;
+}
+
+static int
+luaopen_task (lua_State *L)
+{
+       luaL_newmetatable(L, "Rspamd.task");
+    
+       lua_pushstring(L, "__index");
+       lua_pushvalue(L, -2);  /* pushes the metatable */
+       lua_settable(L, -3);  /* metatable.__index = metatable */
+    
+       luaL_openlib(L, NULL, tasklib_m, 0);
+    
+       return 1;
+}
+
 void
 init_lua_filters (struct config_file *cfg)
 {
@@ -59,6 +133,7 @@ init_lua_filters (struct config_file *cfg)
                        }
                }
        }
+       luaopen_task (L);
 }
 
 
index e9c4ca7bf58bd2bd61462ae5ab6ba8a9a9a60ab9..15edb7d70bfd1c812915cc784beb2d929544d457 100644 (file)
@@ -76,13 +76,13 @@ regexp_module_init (struct config_file *cfg, struct module_ctx **ctx)
 }
 
 static gboolean
-read_regexp_expression (memory_pool_t *pool, struct regexp_module_item *chain, char *line)
+read_regexp_expression (memory_pool_t *pool, struct regexp_module_item *chain, char *symbol, char *line)
 {      
        struct expression *e, *cur;
 
        e = parse_expression (regexp_module_ctx->regexp_pool, line);
        if (e == NULL) {
-               msg_warn ("read_regexp_expression: %s is invalid regexp expression", line);
+               msg_warn ("read_regexp_expression: %s = \"%s\" is invalid regexp expression", symbol, line);
                return FALSE;
        }
        chain->expr = e;
@@ -91,7 +91,7 @@ read_regexp_expression (memory_pool_t *pool, struct regexp_module_item *chain, c
                if (cur->type == EXPR_REGEXP) {
                        cur->content.operand = parse_regexp (pool, cur->content.operand);
                        if (cur->content.operand == NULL) {
-                               msg_warn ("read_regexp_expression: cannot parse regexp, skip expression %s", line);
+                               msg_warn ("read_regexp_expression: cannot parse regexp, skip expression %s = \"%s\"", symbol, line);
                                return FALSE;
                        }
                        chain->regexp_number ++;
@@ -130,7 +130,7 @@ regexp_module_config (struct config_file *cfg)
                        }
                        cur_item = memory_pool_alloc0 (regexp_module_ctx->regexp_pool, sizeof (struct regexp_module_item));
                        cur_item->symbol = cur->param;
-                       if (!read_regexp_expression (regexp_module_ctx->regexp_pool, cur_item, cur->value)) {
+                       if (!read_regexp_expression (regexp_module_ctx->regexp_pool, cur_item, cur->param, cur->value)) {
                                res = FALSE;
                        }
                        regexp_module_ctx->items = g_list_prepend (regexp_module_ctx->items, cur_item);