Skip to content

Commit

Permalink
[Rework] More abstractions to hide C++ internals
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Aug 14, 2023
1 parent 2778ec2 commit bb516b4
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 315 deletions.
36 changes: 20 additions & 16 deletions src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,20 @@ rspamd_controller_handle_symbols(struct rspamd_http_connection_entry *conn_ent,
return 0;
}

static void
rspamd_controller_actions_cb(struct rspamd_action *act, void *cbd)
{
ucl_object_t *top = (ucl_object_t *) cbd;
ucl_object_t *obj = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(obj,
ucl_object_fromstring(act->name),
"action", 0, false);
ucl_object_insert_key(obj,
ucl_object_fromdouble(act->threshold),
"value", 0, false);
ucl_array_append(top, obj);
}

/*
* Actions command handler:
* request: /actions
Expand All @@ -884,27 +898,15 @@ rspamd_controller_handle_actions(struct rspamd_http_connection_entry *conn_ent,
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
struct rspamd_action *act, *tmp;
ucl_object_t *obj, *top;
ucl_object_t *top;

if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) {
return 0;
}

top = ucl_object_typed_new(UCL_ARRAY);

HASH_ITER(hh, session->cfg->actions, act, tmp)
{
obj = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(obj,
ucl_object_fromstring(act->name),
"action", 0, false);
ucl_object_insert_key(obj,
ucl_object_fromdouble(act->threshold),
"value", 0, false);
ucl_array_append(top, obj);
}

rspamd_config_actions_foreach(session->cfg, rspamd_controller_actions_cb, top);
rspamd_controller_send_ucl(conn_ent, top);
ucl_object_unref(top);

Expand Down Expand Up @@ -2300,8 +2302,10 @@ rspamd_controller_handle_saveactions(
score = ucl_object_todouble(cur);
}

if ((isnan(session->cfg->actions[act].threshold) != isnan(score)) ||
(session->cfg->actions[act].threshold != score)) {
struct rspamd_action *cfg_action = rspamd_config_get_action_by_type(ctx->cfg, act);

if (cfg_action && ((isnan(cfg_action->threshold) != isnan(score)) ||
(cfg_action->threshold != score))) {
add_dynamic_action(ctx->cfg, DEFAULT_METRIC, act, score);
added++;
}
Expand Down
34 changes: 18 additions & 16 deletions src/libmime/scan_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ rspamd_scan_result_dtor(gpointer d)
kh_destroy(rspamd_symbols_group_hash, r->sym_groups);
}

static void
rspamd_metric_actions_foreach_cb(int i, struct rspamd_action *act, void *cbd)
{
struct rspamd_scan_result *metric_res = (struct rspamd_scan_result *) cbd;
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;
}
else {
metric_res->actions_config[i].flags |= RSPAMD_ACTION_RESULT_NO_THRESHOLD;
}
metric_res->actions_config[i].action = act;
}

struct rspamd_scan_result *
rspamd_create_metric_result(struct rspamd_task *task,
const gchar *name, gint lua_sym_cbref)
Expand Down Expand Up @@ -91,25 +105,13 @@ rspamd_create_metric_result(struct rspamd_task *task,
if (task->cfg) {
struct rspamd_action *act, *tmp;

int nact = rspamd_config_actions_size(task->cfg);
metric_res->actions_config = rspamd_mempool_alloc0(task->task_pool,
sizeof(struct rspamd_action_config) * HASH_COUNT(task->cfg->actions));
i = 0;
sizeof(struct rspamd_action_config) * nact);

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;
}
else {
metric_res->actions_config[i].flags |= RSPAMD_ACTION_RESULT_NO_THRESHOLD;
}
metric_res->actions_config[i].action = act;

i++;
}
rspamd_config_actions_foreach_enumerate(task->cfg, rspamd_metric_actions_foreach_cb, metric_res);

metric_res->nactions = i;
metric_res->nactions = nact;
}

rspamd_mempool_add_destructor(task->task_pool,
Expand Down
26 changes: 26 additions & 0 deletions src/libserver/cfg_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,32 @@ struct rspamd_action *rspamd_config_get_action(struct rspamd_config *cfg,
struct rspamd_action *rspamd_config_get_action_by_type(struct rspamd_config *cfg,
enum rspamd_action_type type);

/**
* Iterate over all actions
* @param cfg
* @param func
* @param data
*/
void rspamd_config_actions_foreach(struct rspamd_config *cfg,
void (*func)(struct rspamd_action *act, void *d),
void *data);
/**
* Iterate over all actions with index
* @param cfg
* @param func
* @param data
*/
void rspamd_config_actions_foreach_enumerate(struct rspamd_config *cfg,
void (*func)(int idx, struct rspamd_action *act, void *d),
void *data);

/**
* Returns number of actions defined in the config
* @param cfg
* @return
*/
gsize rspamd_config_actions_size(struct rspamd_config *cfg);

int rspamd_config_ev_backend_get(struct rspamd_config *cfg);
const gchar *rspamd_config_ev_backend_to_string(int ev_backend, gboolean *effective);

Expand Down

0 comments on commit bb516b4

Please sign in to comment.