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));
}
/**
* 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
* 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");
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);
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;
}
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);
}
}
}
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
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)
{
}
}
}
+ luaopen_task (L);
}
}
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;
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 ++;
}
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);