diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/libmime/scan_result.c | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/libmime/scan_result.c')
-rw-r--r-- | src/libmime/scan_result.c | 609 |
1 files changed, 310 insertions, 299 deletions
diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c index 4d8720edb..37bd2ddaa 100644 --- a/src/libmime/scan_result.c +++ b/src/libmime/scan_result.c @@ -25,10 +25,10 @@ #include <math.h> #include "contrib/uthash/utlist.h" -#define msg_debug_metric(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_metric_log_id, "metric", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_metric(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_metric_log_id, "metric", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(metric) @@ -36,40 +36,40 @@ INIT_LOG_MODULE(metric) static struct rspamd_counter_data symbols_count; static void -rspamd_scan_result_dtor (gpointer d) +rspamd_scan_result_dtor(gpointer d) { - struct rspamd_scan_result *r = (struct rspamd_scan_result *)d; + struct rspamd_scan_result *r = (struct rspamd_scan_result *) d; struct rspamd_symbol_result *sres; - rspamd_set_counter_ema (&symbols_count, kh_size (r->symbols), 0.5); + rspamd_set_counter_ema(&symbols_count, kh_size(r->symbols), 0.5); if (r->symbol_cbref != -1) { - luaL_unref (r->task->cfg->lua_state, LUA_REGISTRYINDEX, r->symbol_cbref); + luaL_unref(r->task->cfg->lua_state, LUA_REGISTRYINDEX, r->symbol_cbref); } - kh_foreach_value (r->symbols, sres, { + kh_foreach_value(r->symbols, sres, { if (sres->options) { - kh_destroy (rspamd_options_hash, sres->options); + kh_destroy(rspamd_options_hash, sres->options); } }); - kh_destroy (rspamd_symbols_hash, r->symbols); - kh_destroy (rspamd_symbols_group_hash, r->sym_groups); + kh_destroy(rspamd_symbols_hash, r->symbols); + kh_destroy(rspamd_symbols_group_hash, r->sym_groups); } struct rspamd_scan_result * -rspamd_create_metric_result (struct rspamd_task *task, - const gchar *name, gint lua_sym_cbref) +rspamd_create_metric_result(struct rspamd_task *task, + const gchar *name, gint lua_sym_cbref) { struct rspamd_scan_result *metric_res; guint i; - metric_res = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_scan_result)); - metric_res->symbols = kh_init (rspamd_symbols_hash); - metric_res->sym_groups = kh_init (rspamd_symbols_group_hash); + metric_res = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_scan_result)); + metric_res->symbols = kh_init(rspamd_symbols_hash); + metric_res->sym_groups = kh_init(rspamd_symbols_group_hash); if (name) { - metric_res->name = rspamd_mempool_strdup (task->task_pool, name); + metric_res->name = rspamd_mempool_strdup(task->task_pool, name); } else { metric_res->name = NULL; @@ -79,23 +79,24 @@ rspamd_create_metric_result (struct rspamd_task *task, metric_res->task = task; /* Optimize allocation */ - kh_resize (rspamd_symbols_group_hash, metric_res->sym_groups, 4); + kh_resize(rspamd_symbols_group_hash, metric_res->sym_groups, 4); if (symbols_count.mean > 4) { - kh_resize (rspamd_symbols_hash, metric_res->symbols, symbols_count.mean); + kh_resize(rspamd_symbols_hash, metric_res->symbols, symbols_count.mean); } else { - kh_resize (rspamd_symbols_hash, metric_res->symbols, 4); + kh_resize(rspamd_symbols_hash, metric_res->symbols, 4); } if (task->cfg) { struct rspamd_action *act, *tmp; - metric_res->actions_config = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_action_config) * HASH_COUNT (task->cfg->actions)); + metric_res->actions_config = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_action_config) * HASH_COUNT(task->cfg->actions)); i = 0; - HASH_ITER (hh, task->cfg->actions, act, tmp) { + HASH_ITER(hh, task->cfg->actions, act, tmp) + { metric_res->actions_config[i].flags = RSPAMD_ACTION_RESULT_DEFAULT; if (!(act->flags & RSPAMD_ACTION_NO_THRESHOLD)) { metric_res->actions_config[i].cur_limit = act->threshold; @@ -105,36 +106,35 @@ rspamd_create_metric_result (struct rspamd_task *task, } metric_res->actions_config[i].action = act; - i ++; + i++; } metric_res->nactions = i; } - rspamd_mempool_add_destructor (task->task_pool, - rspamd_scan_result_dtor, - metric_res); - DL_APPEND (task->result, metric_res); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_scan_result_dtor, + metric_res); + DL_APPEND(task->result, metric_res); return metric_res; } static inline int -rspamd_pr_sort (const struct rspamd_passthrough_result *pra, - const struct rspamd_passthrough_result *prb) +rspamd_pr_sort(const struct rspamd_passthrough_result *pra, + const struct rspamd_passthrough_result *prb) { return prb->priority - pra->priority; } -bool -rspamd_add_passthrough_result (struct rspamd_task *task, - struct rspamd_action *action, - guint priority, - double target_score, - const gchar *message, - const gchar *module, - uint flags, - struct rspamd_scan_result *scan_result) +bool rspamd_add_passthrough_result(struct rspamd_task *task, + struct rspamd_action *action, + guint priority, + double target_score, + const gchar *message, + const gchar *module, + uint flags, + struct rspamd_scan_result *scan_result) { struct rspamd_passthrough_result *pr; @@ -145,7 +145,7 @@ rspamd_add_passthrough_result (struct rspamd_task *task, /* Find the specific action config */ struct rspamd_action_config *action_config = NULL; - for (unsigned int i = 0; i < scan_result->nactions; i ++) { + for (unsigned int i = 0; i < scan_result->nactions; i++) { struct rspamd_action_config *cur = &scan_result->actions_config[i]; /* We assume that all action pointers are static */ @@ -156,16 +156,16 @@ rspamd_add_passthrough_result (struct rspamd_task *task, } if (action_config && (action_config->flags & RSPAMD_ACTION_RESULT_DISABLED)) { - msg_info_task ("<%s>: NOT set pre-result to '%s' %s(%.2f): '%s' from %s(%d); action is disabled", - MESSAGE_FIELD_CHECK (task, message_id), action->name, - flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", - target_score, - message, module, priority); + msg_info_task("<%s>: NOT set pre-result to '%s' %s(%.2f): '%s' from %s(%d); action is disabled", + MESSAGE_FIELD_CHECK(task, message_id), action->name, + flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", + target_score, + message, module, priority); return false; } - pr = rspamd_mempool_alloc (task->task_pool, sizeof (*pr)); + pr = rspamd_mempool_alloc(task->task_pool, sizeof(*pr)); pr->action = action; pr->priority = priority; pr->message = message; @@ -173,41 +173,42 @@ rspamd_add_passthrough_result (struct rspamd_task *task, pr->target_score = target_score; pr->flags = flags; - DL_APPEND (scan_result->passthrough_result, pr); - DL_SORT (scan_result->passthrough_result, rspamd_pr_sort); + DL_APPEND(scan_result->passthrough_result, pr); + DL_SORT(scan_result->passthrough_result, rspamd_pr_sort); - if (!isnan (target_score)) { + if (!isnan(target_score)) { - msg_info_task ("<%s>: set pre-result to '%s' %s(%.2f): '%s' from %s(%d)", - MESSAGE_FIELD_CHECK (task, message_id), action->name, - flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", - target_score, - message, module, priority); + msg_info_task("<%s>: set pre-result to '%s' %s(%.2f): '%s' from %s(%d)", + MESSAGE_FIELD_CHECK(task, message_id), action->name, + flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", + target_score, + message, module, priority); } else { - msg_info_task ("<%s>: set pre-result to '%s' %s(no score): '%s' from %s(%d)", - MESSAGE_FIELD_CHECK (task, message_id), action->name, - flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", - message, module, priority); + msg_info_task("<%s>: set pre-result to '%s' %s(no score): '%s' from %s(%d)", + MESSAGE_FIELD_CHECK(task, message_id), action->name, + flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", + message, module, priority); } - scan_result->nresults ++; + scan_result->nresults++; return true; } static inline gdouble -rspamd_check_group_score (struct rspamd_task *task, - const gchar *symbol, - struct rspamd_symbols_group *gr, - gdouble *group_score, - gdouble w) +rspamd_check_group_score(struct rspamd_task *task, + const gchar *symbol, + struct rspamd_symbols_group *gr, + gdouble *group_score, + gdouble w) { 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, - gr->name, symbol, w); + msg_info_task("maximum group score %.2f for group %s has been reached," + " ignoring symbol %s with weight %.2f", + gr->max_score, + gr->name, symbol, w); return NAN; } else if (*group_score + w > gr->max_score) { @@ -223,13 +224,13 @@ rspamd_check_group_score (struct rspamd_task *task, #endif static struct rspamd_symbol_result * -insert_metric_result (struct rspamd_task *task, - const gchar *symbol, - double weight, - const gchar *opt, - struct rspamd_scan_result *metric_res, - enum rspamd_symbol_insert_flags flags, - bool *new_sym) +insert_metric_result(struct rspamd_task *task, + const gchar *symbol, + double weight, + const gchar *opt, + struct rspamd_scan_result *metric_res, + enum rspamd_symbol_insert_flags flags, + bool *new_sym) { struct rspamd_symbol_result *symbol_result = NULL; gdouble final_score, *gr_score = NULL, next_gf = 1.0, diff; @@ -242,16 +243,16 @@ insert_metric_result (struct rspamd_task *task, gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE); gchar *sym_cpy; - if (!isfinite (weight)) { - msg_warn_task ("detected %s score for symbol %s, replace it with zero", - isnan (weight) ? "NaN" : "infinity", symbol); + if (!isfinite(weight)) { + msg_warn_task("detected %s score for symbol %s, replace it with zero", + isnan(weight) ? "NaN" : "infinity", symbol); weight = 0.0; } - msg_debug_metric ("want to insert symbol %s, initial weight %.2f", - symbol, weight); + msg_debug_metric("want to insert symbol %s, initial weight %.2f", + symbol, weight); - sdef = g_hash_table_lookup (task->cfg->symbols, symbol); + sdef = g_hash_table_lookup(task->cfg->symbols, symbol); if (sdef == NULL) { if (flags & RSPAMD_SYMBOL_INSERT_ENFORCE) { final_score = 1.0 * weight; /* Enforce static weight to 1.0 */ @@ -260,67 +261,69 @@ insert_metric_result (struct rspamd_task *task, final_score = 0.0; } - msg_debug_metric ("no symbol definition for %s; final multiplier %.2f", - symbol, final_score); + msg_debug_metric("no symbol definition for %s; final multiplier %.2f", + symbol, final_score); } else { if (sdef->cache_item) { /* Check if we can insert this symbol at all */ - if (!rspamd_symcache_is_item_allowed (task, sdef->cache_item, FALSE)) { - msg_debug_metric ("symbol %s is not allowed to be inserted due to settings", - symbol); + if (!rspamd_symcache_is_item_allowed(task, sdef->cache_item, FALSE)) { + msg_debug_metric("symbol %s is not allowed to be inserted due to settings", + symbol); return NULL; } } final_score = (*sdef->weight_ptr) * weight; - PTR_ARRAY_FOREACH (sdef->groups, i, gr) { - k = kh_get (rspamd_symbols_group_hash, metric_res->sym_groups, gr); + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { + k = kh_get(rspamd_symbols_group_hash, metric_res->sym_groups, gr); - if (k == kh_end (metric_res->sym_groups)) { - k = kh_put (rspamd_symbols_group_hash, metric_res->sym_groups, - gr, &ret); - kh_value (metric_res->sym_groups, k) = 0; + if (k == kh_end(metric_res->sym_groups)) { + k = kh_put(rspamd_symbols_group_hash, metric_res->sym_groups, + gr, &ret); + kh_value(metric_res->sym_groups, k) = 0; } } - msg_debug_metric ("metric multiplier for %s is %.2f", - symbol, *sdef->weight_ptr); + msg_debug_metric("metric multiplier for %s is %.2f", + symbol, *sdef->weight_ptr); } if (task->settings) { gdouble corr; - mobj = ucl_object_lookup (task->settings, "scores"); + mobj = ucl_object_lookup(task->settings, "scores"); if (!mobj) { /* Legacy */ mobj = task->settings; } else { - msg_debug_metric ("found scores in the settings"); + msg_debug_metric("found scores in the settings"); } - sobj = ucl_object_lookup (mobj, symbol); - if (sobj != NULL && ucl_object_todouble_safe (sobj, &corr)) { - msg_debug_metric ("settings: changed weight of symbol %s from %.2f " - "to %.2f * %.2f", - symbol, final_score, corr, weight); + sobj = ucl_object_lookup(mobj, symbol); + if (sobj != NULL && ucl_object_todouble_safe(sobj, &corr)) { + msg_debug_metric("settings: changed weight of symbol %s from %.2f " + "to %.2f * %.2f", + symbol, final_score, corr, weight); final_score = corr * weight; } } - k = kh_get (rspamd_symbols_hash, metric_res->symbols, symbol); - if (k != kh_end (metric_res->symbols)) { + k = kh_get(rspamd_symbols_hash, metric_res->symbols, symbol); + if (k != kh_end(metric_res->symbols)) { /* Existing metric score */ - symbol_result = kh_value (metric_res->symbols, k); + symbol_result = kh_value(metric_res->symbols, k); if (single) { max_shots = 1; } else { if (sdef) { if (sdef->groups) { - PTR_ARRAY_FOREACH(sdef->groups, i, gr) { + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { if (gr->flags & RSPAMD_SYMBOL_GROUP_ONE_SHOT) { max_shots = 1; } @@ -334,32 +337,32 @@ insert_metric_result (struct rspamd_task *task, } } - msg_debug_metric ("nshots: %d for symbol %s", max_shots, symbol); + msg_debug_metric("nshots: %d for symbol %s", max_shots, symbol); if (!single && (max_shots > 0 && (symbol_result->nshots >= max_shots))) { single = TRUE; } - symbol_result->nshots ++; + symbol_result->nshots++; if (opt) { - rspamd_task_add_result_option (task, symbol_result, opt, strlen (opt)); + rspamd_task_add_result_option(task, symbol_result, opt, strlen(opt)); } /* Adjust diff */ if (!single) { diff = final_score; - msg_debug_metric ("symbol %s can be inserted multiple times: %.2f weight", - symbol, diff); + msg_debug_metric("symbol %s can be inserted multiple times: %.2f weight", + symbol, diff); } else { - if (fabs (symbol_result->score) < fabs (final_score) && - signbit (symbol_result->score) == signbit (final_score)) { + if (fabs(symbol_result->score) < fabs(final_score) && + signbit(symbol_result->score) == signbit(final_score)) { /* Replace less significant weight with a more significant one */ diff = final_score - symbol_result->score; - msg_debug_metric ("symbol %s can be inserted single time;" - " weight adjusted %.2f + %.2f", - symbol, symbol_result->score, diff); + msg_debug_metric("symbol %s can be inserted single time;" + " weight adjusted %.2f + %.2f", + symbol, symbol_result->score, diff); } else { diff = 0; @@ -376,26 +379,27 @@ insert_metric_result (struct rspamd_task *task, next_gf = task->cfg->grow_factor; } - msg_debug_metric ("adjust grow factor to %.2f for symbol %s (%.2f final)", - next_gf, symbol, diff); + msg_debug_metric("adjust grow factor to %.2f for symbol %s (%.2f final)", + next_gf, symbol, diff); if (sdef) { - PTR_ARRAY_FOREACH (sdef->groups, i, gr) { + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { gdouble cur_diff; - k = kh_get (rspamd_symbols_group_hash, - metric_res->sym_groups, gr); - g_assert (k != kh_end (metric_res->sym_groups)); - gr_score = &kh_value (metric_res->sym_groups, k); - cur_diff = rspamd_check_group_score (task, symbol, gr, - gr_score, diff); + k = kh_get(rspamd_symbols_group_hash, + metric_res->sym_groups, gr); + g_assert(k != kh_end(metric_res->sym_groups)); + gr_score = &kh_value(metric_res->sym_groups, k); + cur_diff = rspamd_check_group_score(task, symbol, gr, + gr_score, diff); - if (isnan (cur_diff)) { + if (isnan(cur_diff)) { /* Limit reached, do not add result */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " drop score %.2f", - *gr_score, gr->name, symbol, diff); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " drop score %.2f", + *gr_score, gr->name, symbol, diff); diff = NAN; break; @@ -405,27 +409,28 @@ insert_metric_result (struct rspamd_task *task, if (cur_diff < diff) { /* Reduce */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " reduce score %.2f - %.2f", - *gr_score, gr->name, symbol, diff, cur_diff); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " reduce score %.2f - %.2f", + *gr_score, gr->name, symbol, diff, cur_diff); diff = cur_diff; } } } } - if (!isnan (diff)) { + if (!isnan(diff)) { metric_res->score += diff; metric_res->grow_factor = next_gf; if (single) { - msg_debug_metric ("final score for single symbol %s = %.2f; %.2f diff", - symbol, final_score, diff); + msg_debug_metric("final score for single symbol %s = %.2f; %.2f diff", + symbol, final_score, diff); symbol_result->score = final_score; - } else { - msg_debug_metric ("increase final score for multiple symbol %s += %.2f = %.2f", - symbol, symbol_result->score, diff); + } + else { + msg_debug_metric("increase final score for multiple symbol %s += %.2f = %.2f", + symbol, symbol_result->score, diff); symbol_result->score += diff; } } @@ -437,12 +442,12 @@ insert_metric_result (struct rspamd_task *task, *new_sym = true; } - sym_cpy = rspamd_mempool_strdup (task->task_pool, symbol); - k = kh_put (rspamd_symbols_hash, metric_res->symbols, - sym_cpy, &ret); - g_assert (ret > 0); - symbol_result = rspamd_mempool_alloc0 (task->task_pool, sizeof (*symbol_result)); - kh_value (metric_res->symbols, k) = symbol_result; + sym_cpy = rspamd_mempool_strdup(task->task_pool, symbol); + k = kh_put(rspamd_symbols_hash, metric_res->symbols, + sym_cpy, &ret); + g_assert(ret > 0); + symbol_result = rspamd_mempool_alloc0(task->task_pool, sizeof(*symbol_result)); + kh_value(metric_res->symbols, k) = symbol_result; /* Handle grow factor */ if (metric_res->grow_factor && final_score > 0) { @@ -453,8 +458,8 @@ insert_metric_result (struct rspamd_task *task, next_gf = task->cfg->grow_factor; } - msg_debug_metric ("adjust grow factor to %.2f for symbol %s (%.2f final)", - next_gf, symbol, final_score); + msg_debug_metric("adjust grow factor to %.2f for symbol %s (%.2f final)", + next_gf, symbol, final_score); symbol_result->name = sym_cpy; symbol_result->sym = sdef; @@ -462,39 +467,41 @@ insert_metric_result (struct rspamd_task *task, if (sdef) { /* Check group limits */ - PTR_ARRAY_FOREACH (sdef->groups, i, gr) { + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { gdouble cur_score; - k = kh_get (rspamd_symbols_group_hash, metric_res->sym_groups, gr); - g_assert (k != kh_end (metric_res->sym_groups)); - gr_score = &kh_value (metric_res->sym_groups, k); - cur_score = rspamd_check_group_score (task, symbol, gr, - gr_score, final_score); + k = kh_get(rspamd_symbols_group_hash, metric_res->sym_groups, gr); + g_assert(k != kh_end(metric_res->sym_groups)); + gr_score = &kh_value(metric_res->sym_groups, k); + cur_score = rspamd_check_group_score(task, symbol, gr, + gr_score, final_score); - if (isnan (cur_score)) { + if (isnan(cur_score)) { /* Limit reached, do not add result */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " drop score %.2f", - *gr_score, gr->name, symbol, final_score); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " drop score %.2f", + *gr_score, gr->name, symbol, final_score); final_score = NAN; break; - } else if (gr_score) { + } + else if (gr_score) { *gr_score += cur_score; if (cur_score < final_score) { /* Reduce */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " reduce score %.2f - %.2f", - *gr_score, gr->name, symbol, final_score, cur_score); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " reduce score %.2f - %.2f", + *gr_score, gr->name, symbol, final_score, cur_score); final_score = cur_score; } } } } - if (!isnan (final_score)) { + if (!isnan(final_score)) { const double epsilon = DBL_EPSILON; metric_res->score += final_score; @@ -502,12 +509,12 @@ insert_metric_result (struct rspamd_task *task, symbol_result->score = final_score; if (final_score > epsilon) { - metric_res->npositive ++; + metric_res->npositive++; metric_res->positive_score += final_score; } else if (final_score < -epsilon) { - metric_res->nnegative ++; - metric_res->negative_score += fabs (final_score); + metric_res->nnegative++; + metric_res->negative_score += fabs(final_score); } } else { @@ -515,77 +522,78 @@ insert_metric_result (struct rspamd_task *task, } if (opt) { - rspamd_task_add_result_option (task, symbol_result, opt, strlen (opt)); + rspamd_task_add_result_option(task, symbol_result, opt, strlen(opt)); } } - msg_debug_metric ("final insertion for symbol %s, score %.2f, factor: %f", - symbol, - symbol_result->score, - final_score); - metric_res->nresults ++; + msg_debug_metric("final insertion for symbol %s, score %.2f, factor: %f", + symbol, + symbol_result->score, + final_score); + metric_res->nresults++; return symbol_result; } struct rspamd_symbol_result * -rspamd_task_insert_result_full (struct rspamd_task *task, - const gchar *symbol, - double weight, - const gchar *opt, - enum rspamd_symbol_insert_flags flags, - struct rspamd_scan_result *result) +rspamd_task_insert_result_full(struct rspamd_task *task, + const gchar *symbol, + double weight, + const gchar *opt, + enum rspamd_symbol_insert_flags flags, + struct rspamd_scan_result *result) { struct rspamd_symbol_result *symbol_result = NULL, *ret = NULL; struct rspamd_scan_result *mres; if (task->processed_stages & (RSPAMD_TASK_STAGE_IDEMPOTENT >> 1)) { - msg_err_task ("cannot insert symbol %s on idempotent phase", - symbol); + msg_err_task("cannot insert symbol %s on idempotent phase", + symbol); return NULL; } if (result == NULL) { /* Insert everywhere */ - DL_FOREACH (task->result, mres) { + DL_FOREACH(task->result, mres) + { if (mres->symbol_cbref != -1) { /* Check if we can insert this symbol to this symbol result */ GError *err = NULL; lua_State *L = (lua_State *) task->cfg->lua_state; - if (!rspamd_lua_universal_pcall (L, mres->symbol_cbref, - G_STRLOC, 1, "uss", &err, - "rspamd{task}", task, symbol, mres->name ? mres->name : "default")) { - msg_warn_task ("cannot call for symbol_cbref for result %s: %e", - mres->name ? mres->name : "default", err); - g_error_free (err); + if (!rspamd_lua_universal_pcall(L, mres->symbol_cbref, + G_STRLOC, 1, "uss", &err, + "rspamd{task}", task, symbol, mres->name ? mres->name : "default")) { + msg_warn_task("cannot call for symbol_cbref for result %s: %e", + mres->name ? mres->name : "default", err); + g_error_free(err); continue; } else { - if (!lua_toboolean (L, -1)) { + if (!lua_toboolean(L, -1)) { /* Skip symbol */ - msg_debug_metric ("skip symbol %s for result %s due to Lua return value", - symbol, mres->name); - lua_pop (L, 1); /* Remove result */ + msg_debug_metric("skip symbol %s for result %s due to Lua return value", + symbol, mres->name); + lua_pop(L, 1); /* Remove result */ continue; } - lua_pop (L, 1); /* Remove result */ + lua_pop(L, 1); /* Remove result */ } } bool new_symbol = false; - symbol_result = insert_metric_result (task, - symbol, - weight, - opt, - mres, - flags, - &new_symbol); + symbol_result = insert_metric_result(task, + symbol, + weight, + opt, + mres, + flags, + &new_symbol); if (mres->name == NULL) { /* Default result */ @@ -593,34 +601,34 @@ rspamd_task_insert_result_full (struct rspamd_task *task, /* Process cache item */ if (symbol_result && task->cfg->cache && symbol_result->sym && symbol_result->nshots == 1) { - rspamd_symcache_inc_frequency (task->cfg->cache, - symbol_result->sym->cache_item, - symbol_result->sym->name); + rspamd_symcache_inc_frequency(task->cfg->cache, + symbol_result->sym->cache_item, + symbol_result->sym->name); } } else if (new_symbol) { /* O(N) but we normally don't have any shadow results */ - LL_APPEND (ret, symbol_result); + LL_APPEND(ret, symbol_result); } } } else { /* Specific insertion */ - symbol_result = insert_metric_result (task, - symbol, - weight, - opt, - result, - flags, - NULL); + symbol_result = insert_metric_result(task, + symbol, + weight, + opt, + result, + flags, + NULL); ret = symbol_result; if (result->name == NULL) { /* Process cache item */ if (symbol_result && task->cfg->cache && symbol_result->sym && symbol_result->nshots == 1) { - rspamd_symcache_inc_frequency (task->cfg->cache, - symbol_result->sym->cache_item, - symbol_result->sym->name); + rspamd_symcache_inc_frequency(task->cfg->cache, + symbol_result->sym->cache_item, + symbol_result->sym->name); } } } @@ -629,10 +637,10 @@ rspamd_task_insert_result_full (struct rspamd_task *task, } static gchar * -rspamd_task_option_safe_copy (struct rspamd_task *task, - const gchar *val, - gsize vlen, - gsize *outlen) +rspamd_task_option_safe_copy(struct rspamd_task *task, + const gchar *val, + gsize vlen, + gsize *outlen) { const gchar *p, *end; @@ -645,37 +653,37 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, UChar32 uc; gint off = 0; - U8_NEXT (p, off, end - p, uc); + U8_NEXT(p, off, end - p, uc); if (uc > 0) { - if (u_isprint (uc)) { + if (u_isprint(uc)) { vlen += off; } else { /* We will replace it with 0xFFFD */ - vlen += MAX (off, 3); + vlen += MAX(off, 3); } } else { - vlen += MAX (off, 3); + vlen += MAX(off, 3); } p += off; } - else if (!g_ascii_isprint (*p)) { + else if (!g_ascii_isprint(*p)) { /* Another 0xFFFD */ vlen += 3; - p ++; + p++; } else { - p ++; - vlen ++; + p++; + vlen++; } } gchar *dest, *d; - dest = rspamd_mempool_alloc (task->task_pool, vlen + 1); + dest = rspamd_mempool_alloc(task->task_pool, vlen + 1); d = dest; p = val; @@ -684,11 +692,11 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, UChar32 uc; gint off = 0; - U8_NEXT (p, off, end - p, uc); + U8_NEXT(p, off, end - p, uc); if (uc > 0) { - if (u_isprint (uc)) { - memcpy (d, p, off); + if (u_isprint(uc)) { + memcpy(d, p, off); d += off; } else { @@ -706,12 +714,12 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, p += off; } - else if (!g_ascii_isprint (*p)) { + else if (!g_ascii_isprint(*p)) { /* Another 0xFFFD */ *d++ = '\357'; *d++ = '\277'; *d++ = '\275'; - p ++; + p++; } else { *d++ = *p++; @@ -725,10 +733,10 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, } gboolean -rspamd_task_add_result_option (struct rspamd_task *task, - struct rspamd_symbol_result *s, - const gchar *val, - gsize vlen) +rspamd_task_add_result_option(struct rspamd_task *task, + struct rspamd_symbol_result *s, + const gchar *val, + gsize vlen) { struct rspamd_symbol_option *opt, srch; gboolean ret = FALSE; @@ -744,23 +752,24 @@ rspamd_task_add_result_option (struct rspamd_task *task, * symbol from the default result, not some shadow result, or * the option insertion will be wrong */ - LL_FOREACH (s, cur) { + LL_FOREACH(s, cur) + { if (cur->opts_len < 0) { /* Cannot add more options, give up */ - msg_debug_task ("cannot add more options to symbol %s when adding option %s", - cur->name, val); + msg_debug_task("cannot add more options to symbol %s when adding option %s", + cur->name, val); ret = FALSE; continue; } if (!cur->options) { - cur->options = kh_init (rspamd_options_hash); + cur->options = kh_init(rspamd_options_hash); } if (vlen + cur->opts_len > task->cfg->max_opts_len) { /* Add truncated option */ - msg_info_task ("cannot add more options to symbol %s when adding option %s", - cur->name, val); + msg_info_task("cannot add more options to symbol %s when adding option %s", + cur->name, val); val = "..."; vlen = 3; cur->opts_len = -1; @@ -770,23 +779,23 @@ rspamd_task_add_result_option (struct rspamd_task *task, srch.option = (gchar *) val; srch.optlen = vlen; - k = kh_get (rspamd_options_hash, cur->options, &srch); + k = kh_get(rspamd_options_hash, cur->options, &srch); - if (k == kh_end (cur->options)) { - opt_cpy = rspamd_task_option_safe_copy (task, val, vlen, &cpy_len); + if (k == kh_end(cur->options)) { + opt_cpy = rspamd_task_option_safe_copy(task, val, vlen, &cpy_len); if (cpy_len != vlen) { srch.option = (gchar *) opt_cpy; srch.optlen = cpy_len; - k = kh_get (rspamd_options_hash, cur->options, &srch); + k = kh_get(rspamd_options_hash, cur->options, &srch); } /* Append new options */ - if (k == kh_end (cur->options)) { - opt = rspamd_mempool_alloc0 (task->task_pool, sizeof(*opt)); + if (k == kh_end(cur->options)) { + opt = rspamd_mempool_alloc0(task->task_pool, sizeof(*opt)); opt->optlen = cpy_len; opt->option = opt_cpy; - kh_put (rspamd_options_hash, cur->options, opt, &r); - DL_APPEND (cur->opts_head, opt); + kh_put(rspamd_options_hash, cur->options, opt, &r); + DL_APPEND(cur->opts_head, opt); if (s == cur) { ret = TRUE; @@ -810,16 +819,16 @@ rspamd_task_add_result_option (struct rspamd_task *task, ret = TRUE; } - task->result->nresults ++; + task->result->nresults++; return ret; } struct rspamd_action_config * -rspamd_find_action_config_for_action (struct rspamd_scan_result *scan_result, - struct rspamd_action *act) +rspamd_find_action_config_for_action(struct rspamd_scan_result *scan_result, + struct rspamd_action *act) { - for (unsigned int i = 0; i < scan_result->nactions; i ++) { + for (unsigned int i = 0; i < scan_result->nactions; i++) { struct rspamd_action_config *cur = &scan_result->actions_config[i]; if (act == cur->action) { @@ -831,12 +840,12 @@ rspamd_find_action_config_for_action (struct rspamd_scan_result *scan_result, } struct rspamd_action * -rspamd_check_action_metric (struct rspamd_task *task, - struct rspamd_passthrough_result **ppr, - struct rspamd_scan_result *scan_result) +rspamd_check_action_metric(struct rspamd_task *task, + struct rspamd_passthrough_result **ppr, + struct rspamd_scan_result *scan_result) { struct rspamd_action_config *action_lim, - *noaction = NULL; + *noaction = NULL; struct rspamd_action *selected_action = NULL, *least_action = NULL; struct rspamd_passthrough_result *pr, *sel_pr = NULL; double max_score = -(G_MAXDOUBLE), sc; @@ -846,10 +855,11 @@ rspamd_check_action_metric (struct rspamd_task *task, scan_result = task->result; } - if (scan_result->passthrough_result != NULL) { - DL_FOREACH (scan_result->passthrough_result, pr) { + if (scan_result->passthrough_result != NULL) { + DL_FOREACH(scan_result->passthrough_result, pr) + { struct rspamd_action_config *act_config = - rspamd_find_action_config_for_action (scan_result, pr->action); + rspamd_find_action_config_for_action(scan_result, pr->action); /* Skip disabled actions */ if (act_config && (act_config->flags & RSPAMD_ACTION_RESULT_DISABLED)) { @@ -861,9 +871,9 @@ rspamd_check_action_metric (struct rspamd_task *task, selected_action = pr->action; if (!(pr->flags & RSPAMD_PASSTHROUGH_LEAST)) { - if (!isnan (sc)) { + if (!isnan(sc)) { if (pr->action->action_type == METRIC_ACTION_NOACTION) { - scan_result->score = MIN (sc, scan_result->score); + scan_result->score = MIN(sc, scan_result->score); } else { scan_result->score = sc; @@ -880,7 +890,7 @@ rspamd_check_action_metric (struct rspamd_task *task, seen_least = true; least_action = selected_action; - if (isnan (sc)) { + if (isnan(sc)) { if (selected_action->flags & RSPAMD_ACTION_NO_THRESHOLD) { /* @@ -914,7 +924,7 @@ rspamd_check_action_metric (struct rspamd_task *task, /* * Select result by score */ - for (size_t i = scan_result->nactions - 1; i != (size_t)-1; i--) { + for (size_t i = scan_result->nactions - 1; i != (size_t) -1; i--) { action_lim = &scan_result->actions_config[i]; sc = action_lim->cur_limit; @@ -922,12 +932,12 @@ rspamd_check_action_metric (struct rspamd_task *task, noaction = action_lim; } - if ((action_lim->flags & (RSPAMD_ACTION_RESULT_DISABLED|RSPAMD_ACTION_RESULT_NO_THRESHOLD))) { + if ((action_lim->flags & (RSPAMD_ACTION_RESULT_DISABLED | RSPAMD_ACTION_RESULT_NO_THRESHOLD))) { continue; } - if (isnan (sc) || - (action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD|RSPAMD_ACTION_HAM))) { + if (isnan(sc) || + (action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD | RSPAMD_ACTION_HAM))) { continue; } @@ -947,7 +957,7 @@ rspamd_check_action_metric (struct rspamd_task *task, /* Adjust least action */ if (least_action->flags & RSPAMD_ACTION_NO_THRESHOLD) { if (selected_action->action_type != METRIC_ACTION_REJECT && - selected_action->action_type != METRIC_ACTION_DISCARD) { + selected_action->action_type != METRIC_ACTION_DISCARD) { /* Override score based action with least action */ selected_action = least_action; @@ -979,8 +989,8 @@ rspamd_check_action_metric (struct rspamd_task *task, } struct rspamd_symbol_result * -rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym, - struct rspamd_scan_result *result) +rspamd_task_find_symbol_result(struct rspamd_task *task, const char *sym, + struct rspamd_scan_result *result) { struct rspamd_symbol_result *res = NULL; khiter_t k; @@ -992,17 +1002,17 @@ rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym, k = kh_get(rspamd_symbols_hash, result->symbols, sym); - if (k != kh_end (result->symbols)) { - res = kh_value (result->symbols, k); + if (k != kh_end(result->symbols)) { + res = kh_value(result->symbols, k); } return res; } -struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( - struct rspamd_task *task, - const gchar *symbol, - struct rspamd_scan_result *result) +struct rspamd_symbol_result *rspamd_task_remove_symbol_result( + struct rspamd_task *task, + const gchar *symbol, + struct rspamd_scan_result *result) { struct rspamd_symbol_result *res = NULL; khiter_t k; @@ -1012,12 +1022,12 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( result = task->result; } - k = kh_get (rspamd_symbols_hash, result->symbols, symbol); + k = kh_get(rspamd_symbols_hash, result->symbols, symbol); - if (k != kh_end (result->symbols)) { - res = kh_value (result->symbols, k); + if (k != kh_end(result->symbols)) { + res = kh_value(result->symbols, k); - if (!isnan (res->score)) { + if (!isnan(res->score)) { /* Remove score from the result */ result->score -= res->score; @@ -1027,14 +1037,15 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( gint i; khiter_t k_groups; - PTR_ARRAY_FOREACH (res->sym->groups, i, gr) { + PTR_ARRAY_FOREACH(res->sym->groups, i, gr) + { gdouble *gr_score; - k_groups = kh_get (rspamd_symbols_group_hash, - result->sym_groups, gr); + k_groups = kh_get(rspamd_symbols_group_hash, + result->sym_groups, gr); - if (k_groups != kh_end (result->sym_groups)) { - gr_score = &kh_value (result->sym_groups, k_groups); + if (k_groups != kh_end(result->sym_groups)) { + gr_score = &kh_value(result->sym_groups, k_groups); if (gr_score) { *gr_score -= res->score; @@ -1044,7 +1055,7 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( } } - kh_del (rspamd_symbols_hash, result->symbols, k); + kh_del(rspamd_symbols_hash, result->symbols, k); } else { return NULL; @@ -1053,10 +1064,9 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( return res; } -void -rspamd_task_symbol_result_foreach (struct rspamd_task *task, - struct rspamd_scan_result *result, GHFunc func, - gpointer ud) +void rspamd_task_symbol_result_foreach(struct rspamd_task *task, + struct rspamd_scan_result *result, GHFunc func, + gpointer ud) { const gchar *kk; struct rspamd_symbol_result *res; @@ -1067,27 +1077,28 @@ rspamd_task_symbol_result_foreach (struct rspamd_task *task, } if (func) { - kh_foreach (result->symbols, kk, res, { - func ((gpointer)kk, (gpointer)res, ud); + kh_foreach(result->symbols, kk, res, { + func((gpointer) kk, (gpointer) res, ud); }); } } struct rspamd_scan_result * -rspamd_find_metric_result (struct rspamd_task *task, - const gchar *name) +rspamd_find_metric_result(struct rspamd_task *task, + const gchar *name) { struct rspamd_scan_result *res; if (name == NULL) { return task->result; } - else if (strcmp (name, "default") == 0) { + else if (strcmp(name, "default") == 0) { return task->result; } - DL_FOREACH (task->result, res) { - if (res->name && strcmp (res->name, name) == 0) { + DL_FOREACH(task->result, res) + { + if (res->name && strcmp(res->name, name) == 0) { return res; } } |