From 5cb2fe96545d019d9a457f9be307eea165790b09 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 5 Apr 2016 16:24:44 +0100 Subject: [PATCH] [Feature] Extend log structure --- src/libserver/protocol.c | 15 +++++++++++---- src/libserver/protocol.h | 8 +++++++- src/log_helper.c | 16 ++++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index e0df280d5..35c912f59 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -1061,6 +1061,7 @@ rspamd_protocol_write_log_pipe (struct rspamd_worker_ctx *ctx, struct metric_result *mres; GHashTableIter it; gpointer k, v; + struct symbol *sym; gint id, i; gsize sz; @@ -1071,9 +1072,12 @@ rspamd_protocol_write_log_pipe (struct rspamd_worker_ctx *ctx, mres = g_hash_table_lookup (task->results, DEFAULT_METRIC); if (mres) { - sz = sizeof (*ls) + sizeof (guint32) * + sz = sizeof (*ls) + + sizeof (struct rspamd_protocol_log_symbol_result) * g_hash_table_size (mres->symbols); ls = g_slice_alloc (sz); + ls->score = mres->score; + ls->required_score = mres->actions_limits[METRIC_ACTION_REJECT]; ls->nresults = g_hash_table_size (mres->symbols); g_hash_table_iter_init (&it, mres->symbols); @@ -1082,12 +1086,15 @@ rspamd_protocol_write_log_pipe (struct rspamd_worker_ctx *ctx, while (g_hash_table_iter_next (&it, &k, &v)) { id = rspamd_symbols_cache_find_symbol (task->cfg->cache, k); + sym = v; if (id >= 0) { - ls->results[i] = id; + ls->results[i].id = id; + ls->results[i].score = sym->score; } else { - ls->results[i] = -1; + ls->results[i].id = -1; + ls->results[i].score = 0.0; } i ++; @@ -1095,7 +1102,7 @@ rspamd_protocol_write_log_pipe (struct rspamd_worker_ctx *ctx, } else { sz = sizeof (*ls); - ls = g_slice_alloc (sz); + ls = g_slice_alloc0 (sz); ls->nresults = 0; } diff --git a/src/libserver/protocol.h b/src/libserver/protocol.h index 15705c19a..9ba1d67ca 100644 --- a/src/libserver/protocol.h +++ b/src/libserver/protocol.h @@ -18,9 +18,15 @@ #define RSPAMD_LENGTH_ERROR RSPAMD_BASE_ERROR + 4 #define RSPAMD_STATFILE_ERROR RSPAMD_BASE_ERROR + 5 +struct rspamd_protocol_log_symbol_result { + guint32 id; + gdouble score; +}; struct rspamd_protocol_log_message_sum { guint32 nresults; - guint32 results[]; + gdouble score; + gdouble required_score; + struct rspamd_protocol_log_symbol_result results[]; }; struct metric; diff --git a/src/log_helper.c b/src/log_helper.c index fd3ada910..d46122b69 100644 --- a/src/log_helper.c +++ b/src/log_helper.c @@ -87,10 +87,11 @@ rspamd_log_helper_read (gint fd, short what, gpointer ud) if (r >= (gssize)sizeof (struct rspamd_protocol_log_message_sum)) { memcpy (&n, buf, sizeof (n)); - if (n != (r - sizeof (guint32)) / sizeof (guint32)) { + if (n != (r - sizeof (*sm)) / sizeof (struct rspamd_protocol_log_symbol_result)) { msg_warn ("cannot read data from log pipe: bad length: %d elements " "announced but %d available", n, - (r - sizeof (guint32)) / sizeof (guint32)); + (r - sizeof (*sm)) / + sizeof (struct rspamd_protocol_log_symbol_result)); } else { sm = g_malloc (r); @@ -98,10 +99,17 @@ rspamd_log_helper_read (gint fd, short what, gpointer ud) DL_FOREACH (ctx->scripts, sc) { lua_rawgeti (ctx->L, LUA_REGISTRYINDEX, sc->cbref); + lua_pushnumber (ctx->L, sm->score); + lua_pushnumber (ctx->L, sm->required_score); lua_createtable (ctx->L, n, 0); for (i = 0; i < n; i ++) { - lua_pushnumber (ctx->L, sm->results[i]); + lua_createtable (ctx->L, 2, 0); + lua_pushnumber (ctx->L, sm->results[i].id); + lua_rawseti (ctx->L, -2, 1); + lua_pushnumber (ctx->L, sm->results[i].score); + lua_rawseti (ctx->L, -2, 2); + lua_rawseti (ctx->L, -2, (i + 1)); } @@ -109,7 +117,7 @@ rspamd_log_helper_read (gint fd, short what, gpointer ud) *pcfg = ctx->cfg; rspamd_lua_setclass (ctx->L, "rspamd{config}", -1); - if (lua_pcall (ctx->L, 2, 0, 0) != 0) { + if (lua_pcall (ctx->L, 4, 0, 0) != 0) { msg_err ("error executing log handler code: %s", lua_tostring (ctx->L, -1)); lua_pop (ctx->L, 1); -- 2.39.5