Browse Source

* Fix variables substitution

* Add simple lua interface to access task structures
* Improve error reporting for regexp module
tags/0.2.7
Vsevolod Stakhov 15 years ago
parent
commit
182b158dc8
5 changed files with 92 additions and 14 deletions
  1. 1
    1
      perl/Rspamd/Task.xs
  2. 2
    1
      src/cfg_file.h
  3. 10
    8
      src/cfg_utils.c
  4. 75
    0
      src/lua.c
  5. 4
    4
      src/plugins/regexp.c

+ 1
- 1
perl/Rspamd/Task.xs View 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));
}


+ 2
- 1
src/cfg_file.h View 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

+ 10
- 8
src/cfg_utils.c View 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

+ 75
- 0
src/lua.c View File

@@ -33,6 +33,80 @@

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);
}



+ 4
- 4
src/plugins/regexp.c View 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);

Loading…
Cancel
Save