Skip to content

Commit

Permalink
[Project] Move action definition to avoid uthash pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Jan 14, 2019
1 parent 96986ba commit dce02c4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
15 changes: 2 additions & 13 deletions src/libserver/cfg_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,8 @@ enum rspamd_action_flags {
RSPAMD_ACTION_HAM = (1u << 2),
};

struct UT_hash_handle;
/**
* Action config definition
*/
struct rspamd_action {
enum rspamd_action_type action;
enum rspamd_action_flags flags;
guint priority;
gint lua_handler_ref; /* If special handling is needed */
gdouble threshold;
gchar *name;
struct UT_hash_handle hh; /* Index by name */
};

struct rspamd_action;

struct rspamd_config_post_load_script {
gint cbref;
Expand Down
38 changes: 38 additions & 0 deletions src/libserver/cfg_file_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*-
* Copyright 2019 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef RSPAMD_CFG_FILE_PRIVATE_H
#define RSPAMD_CFG_FILE_PRIVATE_H

#include "cfg_file.h"
#include "../../contrib/mumhash/mum.h"
#define HASH_CASELESS
#include "uthash_strcase.h"

/**
* Action config definition
*/
struct rspamd_action {
enum rspamd_action_type action;
enum rspamd_action_flags flags;
guint priority;
gint lua_handler_ref; /* If special handling is needed */
gdouble threshold;
gchar *name;
struct UT_hash_handle hh; /* Index by name */
};

#endif
4 changes: 1 addition & 3 deletions src/libserver/cfg_rcl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
#include "cfg_rcl.h"
#include "rspamd.h"
#include "../../contrib/mumhash/mum.h"
#define HASH_CASELESS
#include "uthash_strcase.h"
#include "cfg_file_private.h"
#include "utlist.h"
#include "cfg_file.h"
#include "lua/lua_common.h"
Expand Down
29 changes: 5 additions & 24 deletions src/libserver/cfg_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

#include "cfg_file.h"
#include "rspamd.h"
#include "../../contrib/mumhash/mum.h"
#define HASH_CASELESS
#include "uthash_strcase.h"
#include "cfg_file_private.h"
#include "filter.h"
#include "lua/lua_common.h"
#include "lua/lua_thread_pool.h"
Expand Down Expand Up @@ -848,29 +846,10 @@ rspamd_config_post_load (struct rspamd_config *cfg,
/* Validate cache */
if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
/* Check for actions sanity */
int i, prev_act = 0;
gdouble prev_score = NAN;
gboolean seen_controller = FALSE;
GList *cur;
struct rspamd_worker_conf *wcf;

for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) {
if (!isnan (prev_score) && !isnan (cfg->actions[i].threshold)) {
if (prev_score <= isnan (cfg->actions[i].threshold)) {
msg_warn_config ("incorrect metrics scores: action %s"
" has lower score: %.2f than action %s: %.2f",
rspamd_action_to_str (prev_act), prev_score,
rspamd_action_to_str (i), cfg->actions[i].threshold);
ret = FALSE;
}
}

if (!isnan (cfg->actions[i].threshold)) {
prev_score = cfg->actions[i].score;
prev_act = i;
}
}

cur = cfg->workers;
while (cur) {
wcf = cur->data;
Expand Down Expand Up @@ -1034,8 +1013,6 @@ rspamd_config_new_statfile (struct rspamd_config *cfg,
void
rspamd_config_init_metric (struct rspamd_config *cfg)
{
int i;

cfg->grow_factor = 1.0;
cfg->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
cfg->groups = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
Expand Down Expand Up @@ -2195,6 +2172,8 @@ rspamd_action_to_str (enum rspamd_action_type action)
return "no action";
case METRIC_ACTION_MAX:
return "invalid max action";
case METRIC_ACTION_CUSTOM:
return "custom";
}

return "unknown action";
Expand All @@ -2218,6 +2197,8 @@ rspamd_action_to_str_alt (enum rspamd_action_type action)
return "no action";
case METRIC_ACTION_MAX:
return "invalid max action";
case METRIC_ACTION_CUSTOM:
return "custom";
}

return "unknown action";
Expand Down
13 changes: 7 additions & 6 deletions src/lua/lua_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "libmime/message.h"
#include "libutil/expression.h"
#include "libserver/composites.h"
#include "libserver/cfg_file_private.h"
#include "libmime/lang_detection.h"
#include "lua/lua_map.h"
#include "lua/lua_thread_pool.h"
Expand Down Expand Up @@ -2220,15 +2221,15 @@ lua_config_get_all_actions (lua_State * L)
{
LUA_TRACE_POINT;
struct rspamd_config *cfg = lua_check_config (L, 1);
gint act = 0;
struct rspamd_action *act, *tmp;

if (cfg) {
lua_newtable (L);
lua_createtable (L, 0, HASH_COUNT (cfg->actions));

for (act = METRIC_ACTION_REJECT; act < METRIC_ACTION_MAX; act ++) {
if (!isnan (cfg->actions[act].threshold)) {
lua_pushstring (L, rspamd_action_to_str (act));
lua_pushnumber (L, cfg->actions[act].threshold);
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);
}
}
Expand Down

0 comments on commit dce02c4

Please sign in to comment.