Browse Source

[Rework] More abstractions to hide C++ internals

pull/4573/head
Vsevolod Stakhov 2 years ago
parent
commit
bb516b454f
No known key found for this signature in database GPG Key ID: 7647B6790081437
  1. 36
      src/controller.c
  2. 34
      src/libmime/scan_result.c
  3. 26
      src/libserver/cfg_file.h
  4. 485
      src/libserver/cfg_rcl.cxx
  5. 20
      src/libserver/cfg_rcl.h
  6. 23
      src/libserver/cfg_utils.cxx
  7. 31
      src/lua/lua_config.c
  8. 16
      src/lua/lua_task.c

36
src/controller.c

@ -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
@ -884,8 +898,7 @@ 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;
@ -893,18 +906,7 @@ rspamd_controller_handle_actions(struct rspamd_http_connection_entry *conn_ent,
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);
@ -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++;
}

34
src/libmime/scan_result.c

@ -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)
@ -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,

26
src/libserver/cfg_file.h

@ -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);

485
src/libserver/cfg_rcl.cxx
File diff suppressed because it is too large
View File

20
src/libserver/cfg_rcl.h

@ -1,11 +1,11 @@
/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -166,20 +166,6 @@ gboolean rspamd_rcl_parse(struct rspamd_rcl_section *top,
gpointer ptr, rspamd_mempool_t *pool,
const ucl_object_t *obj, GError **err);
/**
* Parse default structure for a section
* @param section section
* @param cfg config file
* @param obj object to parse
* @param ptr ptr to pass
* @param err error ptr
* @return TRUE if the object has been parsed
*/
gboolean rspamd_rcl_section_parse_defaults(struct rspamd_config *cfg,
struct rspamd_rcl_section *section,
rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ptr,
GError **err);
/**
* Here is a section of common handlers that accepts rcl_struct_parser
* which itself contains a struct pointer and the offset of a member in a

23
src/libserver/cfg_utils.cxx

@ -2167,6 +2167,29 @@ rspamd_config_get_action_by_type(struct rspamd_config *cfg,
return nullptr;
}
void rspamd_config_actions_foreach(struct rspamd_config *cfg,
void (*func)(struct rspamd_action *act, void *d),
void *data)
{
for (const auto &act: RSPAMD_CFG_ACTIONS(cfg)->actions) {
func(act.get(), data);
}
}
void rspamd_config_actions_foreach_enumerate(struct rspamd_config *cfg,
void (*func)(int idx, struct rspamd_action *act, void *d),
void *data)
{
for (const auto &[idx, act]: rspamd::enumerate(RSPAMD_CFG_ACTIONS(cfg)->actions)) {
func(idx, act.get(), data);
}
}
gsize rspamd_config_actions_size(struct rspamd_config *cfg)
{
return RSPAMD_CFG_ACTIONS(cfg)->actions.size();
}
gboolean
rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj, const gchar *description,
struct rspamd_radix_map_helper **target, GError **err,

31
src/lua/lua_config.c

@ -1,11 +1,11 @@
/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -2581,24 +2581,27 @@ lua_config_get_metric_action(lua_State *L)
return 1;
}
static void
lua_config_actions_cb(struct rspamd_action *act, void *cbd)
{
lua_State *L = (lua_State *) cbd;
if (!isnan(act->threshold)) {
lua_pushstring(L, act->name);
lua_pushnumber(L, act->threshold);
lua_settable(L, -3);
}
}
static gint
lua_config_get_all_actions(lua_State *L)
{
LUA_TRACE_POINT;
struct rspamd_config *cfg = lua_check_config(L, 1);
struct rspamd_action *act, *tmp;
if (cfg) {
lua_createtable(L, 0, HASH_COUNT(cfg->actions));
HASH_ITER(hh, cfg->actions, act, tmp)
{
if (!isnan(act->threshold)) {
lua_pushstring(L, act->name);
lua_pushnumber(L, act->threshold);
lua_settable(L, -3);
}
}
lua_createtable(L, 0, rspamd_config_actions_size(cfg));
rspamd_config_actions_foreach(cfg, lua_config_actions_cb, L);
}
else {
return luaL_error(L, "invalid arguments, rspamd_config expected");

16
src/lua/lua_task.c

@ -1,11 +1,11 @@
/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -2313,14 +2313,6 @@ lua_task_set_pre_result(lua_State *L)
action = rspamd_config_get_action(task->cfg, act_str);
if (action == NULL) {
struct rspamd_action *tmp;
HASH_ITER(hh, task->cfg->actions, action, tmp)
{
msg_err_task("known defined action: %s = %f",
action->name, action->threshold);
}
return luaL_error(L, "unknown action %s", act_str);
}
@ -5730,7 +5722,7 @@ lua_task_set_settings(lua_State *L)
if (!isnan(act_score)) {
struct rspamd_action *new_act;
HASH_FIND_STR(task->cfg->actions, act_name, new_act);
new_act = rspamd_config_get_action(task->cfg, act_name);
if (new_act == NULL) {
/* New action! */

Loading…
Cancel
Save