aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-03-23 16:33:43 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-03-23 16:33:43 +0000
commitaf06f40be724445d2084503e3f17448af17ff779 (patch)
tree6b8c6ea02288a2d1a167fa0708b21c401f0691a6
parentff86598cc6d61f81cca34b956a064a657ed07c16 (diff)
parent012b099a308711da7982c8dfe39dd89c55e1de14 (diff)
downloadrspamd-af06f40be724445d2084503e3f17448af17ff779.tar.gz
rspamd-af06f40be724445d2084503e3f17448af17ff779.zip
Merge branch 'expr-rework'
-rw-r--r--src/libmime/mime_expressions.c10
-rw-r--r--src/libutil/printf.c20
-rw-r--r--src/libutil/printf.h1
-rw-r--r--src/plugins/regexp.c56
4 files changed, 64 insertions, 23 deletions
diff --git a/src/libmime/mime_expressions.c b/src/libmime/mime_expressions.c
index 841610480..6034e3357 100644
--- a/src/libmime/mime_expressions.c
+++ b/src/libmime/mime_expressions.c
@@ -567,7 +567,9 @@ rspamd_mime_expr_parse (const gchar *line, gsize len,
break;
case bad_atom:
g_set_error (err, rspamd_mime_expr_quark(), 100, "cannot parse"
- " mime atom '%*.s' when reading symbol '%c'", (gint)len, line, t);
+ " mime atom '%s' when reading symbol '%c' at offset %d, "
+ "near %*.s", line, t, (gint)(p - line),
+ (gint)MIN (end - p, 10), p);
return NULL;
case end_atom:
goto set;
@@ -575,9 +577,9 @@ rspamd_mime_expr_parse (const gchar *line, gsize len,
}
set:
- if (p - line == 0 || (state != got_ebrace || state != got_second_slash ||
- state != in_flags)) {
- g_set_error (err, rspamd_mime_expr_quark(), 200, "inclomplete or empty"
+ if (p - line == 0 || (state != got_ebrace && state != got_second_slash &&
+ state != in_flags && state != end_atom)) {
+ g_set_error (err, rspamd_mime_expr_quark(), 200, "incomplete or empty"
" mime atom");
return NULL;
}
diff --git a/src/libutil/printf.c b/src/libutil/printf.c
index 5a04dc432..64deab209 100644
--- a/src/libutil/printf.c
+++ b/src/libutil/printf.c
@@ -339,6 +339,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
guint width, sign, hex, humanize, bytes, frac_width, i;
rspamd_fstring_t *v;
GString *gs;
+ GError *err;
gboolean bv;
while (*fmt) {
@@ -462,6 +463,25 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
continue;
+ case 'e':
+ err = va_arg (args, GError *);
+
+ if (err) {
+ p = err->message;
+
+ if (p == NULL) {
+ p = "(NULL)";
+ }
+ }
+ else {
+ p = "unknown error";
+ }
+
+ slen = strlen (p);
+ RSPAMD_PRINTF_APPEND (p, slen);
+
+ continue;
+
case 's':
p = va_arg (args, gchar *);
if (p == NULL) {
diff --git a/src/libutil/printf.h b/src/libutil/printf.h
index 2bc421143..d0db483d9 100644
--- a/src/libutil/printf.h
+++ b/src/libutil/printf.h
@@ -52,6 +52,7 @@
* %Z '\0'
* %N '\n'
* %c gchar
+ * %e GError *
* %% %
*
*/
diff --git a/src/plugins/regexp.c b/src/plugins/regexp.c
index a140d616c..159399dfd 100644
--- a/src/plugins/regexp.c
+++ b/src/plugins/regexp.c
@@ -73,12 +73,17 @@ read_regexp_expression (rspamd_mempool_t * pool,
gboolean raw_mode)
{
struct rspamd_expression *e = NULL;
+ GError *err = NULL;
+
+ if (!rspamd_parse_expression (line, 0, &mime_expr_subr, NULL, pool, &err,
+ &e)) {
+ msg_warn ("%s = \"%s\" is invalid regexp expression: %e", symbol, line,
+ err);
+ g_error_free (err);
- /* XXX: Implement atoms parsing */
- if (e == NULL) {
- msg_warn ("%s = \"%s\" is invalid regexp expression", symbol, line);
return FALSE;
}
+
chain->expr = e;
return TRUE;
@@ -133,11 +138,13 @@ regexp_module_config (struct rspamd_config *cfg)
ucl_obj_tostring (value), cfg->raw_mode)) {
res = FALSE;
}
- register_symbol (&cfg->cache,
- cur_item->symbol,
- 1,
- process_regexp_item,
- cur_item);
+ else {
+ register_symbol (&cfg->cache,
+ cur_item->symbol,
+ 1,
+ process_regexp_item,
+ cur_item);
+ }
}
else if (value->type == UCL_USERDATA) {
cur_item = rspamd_mempool_alloc0 (regexp_module_ctx->regexp_pool,
@@ -171,7 +178,7 @@ regexp_module_reconfig (struct rspamd_config *cfg)
static gboolean rspamd_lua_call_expression_func(
struct ucl_lua_funcdata *lua_data, struct rspamd_task *task,
- GArray *args, gboolean *res)
+ GArray *args, gint *res)
{
lua_State *L = lua_data->L;
struct rspamd_task **ptask;
@@ -185,7 +192,7 @@ static gboolean rspamd_lua_call_expression_func(
*ptask = task;
/* Now push all arguments */
- for (i = 0; i < args->len; i ++) {
+ for (i = 0; i < (gint)args->len; i ++) {
arg = &g_array_index (args, struct expression_argument, i);
if (arg) {
switch (arg->type) {
@@ -208,12 +215,16 @@ static gboolean rspamd_lua_call_expression_func(
}
pop++;
- if (!lua_isboolean (L, -1)) {
- lua_pop (L, pop);
+ if (lua_type (L, -1) == LUA_TNUMBER) {
+ *res = lua_tonumber (L, -1);
+ }
+ else if (lua_type (L, -1) == LUA_TBOOLEAN) {
+ *res = lua_toboolean (L, -1);
+ }
+ else {
msg_info("lua function must return a boolean");
- return FALSE;
}
- *res = lua_toboolean (L, -1);
+
lua_pop (L, pop);
return TRUE;
@@ -224,7 +235,7 @@ static void
process_regexp_item (struct rspamd_task *task, void *user_data)
{
struct regexp_module_item *item = user_data;
- gboolean res = FALSE;
+ gint res = FALSE;
/* Non-threaded version */
if (item->lua_function) {
@@ -234,12 +245,19 @@ process_regexp_item (struct rspamd_task *task, void *user_data)
&res)) {
msg_err ("error occurred when checking symbol %s", item->symbol);
}
- if (res) {
- rspamd_task_insert_result (task, item->symbol, 1, NULL);
- }
}
else {
/* Process expression */
- /* XXX: add this function */
+ if (item->expr) {
+ res = rspamd_process_expression (item->expr, task);
+ }
+ else {
+ msg_warn ("FIXME: %s symbol is broken with new expressions",
+ item->symbol);
+ }
+ }
+
+ if (res) {
+ rspamd_task_insert_result (task, item->symbol, res, NULL);
}
}