From: Vsevolod Stakhov Date: Mon, 30 Apr 2018 15:11:50 +0000 (+0100) Subject: [Rework] Implement flags for inserting a symbol X-Git-Tag: 1.7.4~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8a1de0ae5f7eba6e86b11c7e5ca0cb36a6988a92;p=rspamd.git [Rework] Implement flags for inserting a symbol --- diff --git a/src/libmime/filter.c b/src/libmime/filter.c index 1efe898ce..5806c1614 100644 --- a/src/libmime/filter.c +++ b/src/libmime/filter.c @@ -23,8 +23,6 @@ #include "contrib/uthash/utlist.h" -#define COMMON_PART_FACTOR 95 - struct rspamd_metric_result * rspamd_create_metric_result (struct rspamd_task *task) { @@ -38,7 +36,7 @@ rspamd_create_metric_result (struct rspamd_task *task) } metric_res = rspamd_mempool_alloc (task->task_pool, - sizeof (struct rspamd_metric_result)); + sizeof (struct rspamd_metric_result)); metric_res->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); rspamd_mempool_add_destructor (task->task_pool, @@ -68,7 +66,7 @@ rspamd_check_group_score (struct rspamd_task *task, if (gr != NULL && group_score && gr->max_score > 0.0 && w > 0.0) { if (*group_score >= gr->max_score && w > 0) { msg_info_task ("maximum group score %.2f for group %s has been reached," - " ignoring symbol %s with weight %.2f", gr->max_score, + " ignoring symbol %s with weight %.2f", gr->max_score, gr->name, symbol, w); return NAN; } @@ -82,10 +80,10 @@ rspamd_check_group_score (struct rspamd_task *task, static struct rspamd_symbol_result * insert_metric_result (struct rspamd_task *task, - const gchar *symbol, - double flag, - const gchar *opt, - gboolean single) + const gchar *symbol, + double flag, + const gchar *opt, + enum rspamd_symbol_insert_flags flags) { struct rspamd_metric_result *metric_res; struct rspamd_symbol_result *s = NULL; @@ -94,6 +92,7 @@ insert_metric_result (struct rspamd_task *task, struct rspamd_symbols_group *gr = NULL; const ucl_object_t *mobj, *sobj; gint max_shots; + gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE); metric_res = rspamd_create_metric_result (task); @@ -105,7 +104,12 @@ insert_metric_result (struct rspamd_task *task, sdef = g_hash_table_lookup (task->cfg->symbols, symbol); if (sdef == NULL) { - w = 0.0; + if (flags & RSPAMD_SYMBOL_INSERT_ENFORCE) { + w = 1.0 * flag; /* Enforce static weight to 1.0 */ + } + else { + w = 0.0; + } } else { w = (*sdef->weight_ptr) * flag; @@ -248,18 +252,18 @@ insert_metric_result (struct rspamd_task *task, return s; } -static struct rspamd_symbol_result * -insert_result_common (struct rspamd_task *task, - const gchar *symbol, - double flag, - const gchar *opt, - gboolean single) +struct rspamd_symbol_result * +rspamd_task_insert_result_full (struct rspamd_task *task, + const gchar *symbol, + double flag, + const gchar *opt, + enum rspamd_symbol_insert_flags flags) { struct rspamd_symbol_result *s = NULL; if (task->processed_stages & (RSPAMD_TASK_STAGE_IDEMPOTENT >> 1)) { msg_err_task ("cannot insert symbol %s on idempotent phase", - symbol); + symbol); return NULL; } @@ -269,7 +273,7 @@ insert_result_common (struct rspamd_task *task, symbol, flag, opt, - single); + flags); /* Process cache item */ if (task->cfg->cache) { @@ -279,27 +283,6 @@ insert_result_common (struct rspamd_task *task, return s; } -/* Insert result that may be increased on next insertions */ -struct rspamd_symbol_result * -rspamd_task_insert_result (struct rspamd_task *task, - const gchar *symbol, - double flag, - const gchar *opt) -{ - return insert_result_common (task, symbol, flag, opt, - FALSE); -} - -/* Insert result as a single option */ -struct rspamd_symbol_result * -rspamd_task_insert_result_single (struct rspamd_task *task, - const gchar *symbol, - double flag, - const gchar *opt) -{ - return insert_result_common (task, symbol, flag, opt, TRUE); -} - gboolean rspamd_task_add_result_option (struct rspamd_task *task, struct rspamd_symbol_result *s, const gchar *val) diff --git a/src/libmime/filter.h b/src/libmime/filter.h index 391441392..120749e3a 100644 --- a/src/libmime/filter.h +++ b/src/libmime/filter.h @@ -49,6 +49,12 @@ struct rspamd_metric_result { */ struct rspamd_metric_result * rspamd_create_metric_result (struct rspamd_task *task); +enum rspamd_symbol_insert_flags { + RSPAMD_SYMBOL_INSERT_DEFAULT = 0, + RSPAMD_SYMBOL_INSERT_SINGLE = (1 << 0), + RSPAMD_SYMBOL_INSERT_ENFORCE = (1 << 1), +}; + /** * Insert a result to task * @param task worker's task that present message from user @@ -57,23 +63,16 @@ struct rspamd_metric_result * rspamd_create_metric_result (struct rspamd_task *t * @param flag numeric weight for symbol * @param opts list of symbol's options */ -struct rspamd_symbol_result* rspamd_task_insert_result (struct rspamd_task *task, +struct rspamd_symbol_result* rspamd_task_insert_result_full (struct rspamd_task *task, const gchar *symbol, double flag, - const gchar *opts); + const gchar *opts, + enum rspamd_symbol_insert_flags flags); -/** - * Insert a single result to task - * @param task worker's task that present message from user - * @param metric_name metric's name to which we need to insert result - * @param symbol symbol to insert - * @param flag numeric weight for symbol - * @param opts list of symbol's options - */ -struct rspamd_symbol_result* rspamd_task_insert_result_single (struct rspamd_task *task, - const gchar *symbol, - double flag, - const gchar *opts); +#define rspamd_task_insert_result_single(task, symbol, flag, opts) \ + rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_SINGLE) +#define rspamd_task_insert_result(task, symbol, flag, opts) \ + rspamd_task_insert_result_full (task, symbol, flag, opts, RSPAMD_SYMBOL_INSERT_DEFAULT) /** diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 9819c4eac..dbd0dcd9b 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -81,9 +81,10 @@ LUA_FUNCTION_DEF (task, get_ev_base); */ LUA_FUNCTION_DEF (task, get_worker); /*** - * @method task:insert_result(symbol, weight[, option1, ...]) + * @method task:insert_result([enforce_symbol,]symbol, weight[, option1, ...]) * Insert specific symbol to the tasks scanning results assigning the initial * weight to it. + * @param {boolean} enforce_symbol if represented and true, then insert symbol even if it is not registered in the metric * @param {string} symbol symbol to insert * @param {number} weight initial weight (this weight is multiplied by the metric weight) * @param {string} options list of optional options attached to a symbol inserted