};
/* Copy to avoid linking with librspamdserver */
-enum rspamd_metric_action {
+enum rspamd_action_type {
METRIC_ACTION_REJECT = 0,
METRIC_ACTION_SOFT_REJECT,
METRIC_ACTION_REWRITE_SUBJECT,
GHashTableIter it, sit;
struct rspamd_symbols_group *gr;
struct rspamd_symbol *sym;
- struct rspamd_metric *metric;
ucl_object_t *obj, *top, *sym_obj, *group_symbols;
gpointer k, v;
top = ucl_object_typed_new (UCL_ARRAY);
/* Go through all symbols groups in the default metric */
- metric = g_hash_table_lookup (session->ctx->cfg->metrics, DEFAULT_METRIC);
- g_assert (metric != NULL);
- g_hash_table_iter_init (&it, metric->groups);
+ g_hash_table_iter_init (&it, session->cfg->groups);
while (g_hash_table_iter_next (&it, &k, &v)) {
gr = v;
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
- struct rspamd_metric *metric;
- struct metric_action *act;
+ struct rspamd_action *act;
gint i;
ucl_object_t *obj, *top;
top = ucl_object_typed_new (UCL_ARRAY);
/* Get actions for default metric */
- metric = g_hash_table_lookup (session->ctx->cfg->metrics, DEFAULT_METRIC);
- if (metric != NULL) {
- for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
- act = &metric->actions[i];
- if (act->score >= 0) {
- obj = ucl_object_typed_new (UCL_OBJECT);
- ucl_object_insert_key (obj,
+ for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
+ act = &session->cfg->actions[i];
+ if (act->score >= 0) {
+ obj = ucl_object_typed_new (UCL_OBJECT);
+ ucl_object_insert_key (obj,
ucl_object_fromstring (rspamd_action_to_str (
- act->action)), "action", 0, false);
- ucl_object_insert_key (obj, ucl_object_fromdouble (
- act->score), "value", 0, false);
- ucl_array_append (top, obj);
- }
+ act->action)), "action", 0, false);
+ ucl_object_insert_key (obj, ucl_object_fromdouble (
+ act->score), "value", 0, false);
+ ucl_array_append (top, obj);
}
}
}
static ucl_object_t *
-rspamd_controller_pie_element (enum rspamd_metric_action action,
+rspamd_controller_pie_element (enum rspamd_action_type action,
const char *label, gdouble data)
{
ucl_object_t *res = ucl_object_typed_new (UCL_OBJECT);
{
struct rspamd_controller_session *session = conn_ent->ud;
struct ucl_parser *parser;
- struct rspamd_metric *metric;
ucl_object_t *obj;
const ucl_object_t *cur;
struct rspamd_controller_worker_ctx *ctx;
const gchar *error;
gdouble score;
gint i, added = 0;
- enum rspamd_metric_action act;
+ enum rspamd_action_type act;
ucl_object_iter_t it = NULL;
ctx = session->ctx;
return 0;
}
- metric = g_hash_table_lookup (ctx->cfg->metrics, DEFAULT_METRIC);
- if (metric == NULL) {
- msg_err_session ("cannot find default metric");
- rspamd_controller_send_error (conn_ent, 500,
- "Default metric is absent");
- return 0;
- }
-
parser = ucl_parser_new (0);
if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) {
if ((error = ucl_parser_get_error (parser)) != NULL) {
return 0;
}
-
-
obj = ucl_parser_get_object (parser);
ucl_parser_free (parser);
break;
}
score = ucl_object_todouble (cur);
- if (metric->actions[act].score != score) {
+ if (session->cfg->actions[act].score != score) {
add_dynamic_action (ctx->cfg, DEFAULT_METRIC, act, score);
added ++;
}
{
struct rspamd_controller_session *session = conn_ent->ud;
struct ucl_parser *parser;
- struct rspamd_metric *metric;
ucl_object_t *obj;
const ucl_object_t *cur, *jname, *jvalue;
ucl_object_iter_t iter = NULL;
return 0;
}
- metric = g_hash_table_lookup (ctx->cfg->metrics, DEFAULT_METRIC);
- if (metric == NULL) {
- msg_err_session ("cannot find default metric");
- rspamd_controller_send_error (conn_ent, 500,
- "Default metric is absent");
- return 0;
- }
-
parser = ucl_parser_new (0);
if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) {
if ((error = ucl_parser_get_error (parser)) != NULL) {
jname = ucl_object_lookup (cur, "name");
jvalue = ucl_object_lookup (cur, "value");
val = ucl_object_todouble (jvalue);
- sym = g_hash_table_lookup (metric->symbols, ucl_object_tostring (jname));
+ sym = g_hash_table_lookup (session->cfg->symbols, ucl_object_tostring (jname));
if (sym && fabs (*sym->weight_ptr - val) > 0.01) {
if (!add_dynamic_symbol (ctx->cfg, DEFAULT_METRIC,
rspamd_create_metric_result (struct rspamd_task *task)
{
struct rspamd_metric_result *metric_res;
- struct rspamd_metric *metric;
guint i;
metric_res = task->result;
return metric_res;
}
- metric = task->cfg->default_metric;
-
- if (metric == NULL) {
- return NULL;
- }
-
- metric_res =
- rspamd_mempool_alloc (task->task_pool,
+ metric_res = rspamd_mempool_alloc (task->task_pool,
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,
(rspamd_mempool_destruct_t) g_hash_table_unref,
metric_res->sym_groups);
- metric_res->metric = metric;
metric_res->grow_factor = 0;
metric_res->score = 0;
metric_res->changes = 0;
for (i = 0; i < METRIC_ACTION_MAX; i++) {
- metric_res->actions_limits[i] = metric->actions[i].score;
+ metric_res->actions_limits[i] = task->cfg->actions[i].score;
}
metric_res->action = METRIC_ACTION_MAX;
static struct rspamd_symbol_result *
insert_metric_result (struct rspamd_task *task,
- struct rspamd_metric *metric,
const gchar *symbol,
double flag,
const gchar *opt,
flag = 0.0;
}
- sdef = g_hash_table_lookup (metric->symbols, symbol);
+ sdef = g_hash_table_lookup (task->cfg->symbols, symbol);
if (sdef == NULL) {
w = 0.0;
}
/* Handle grow factor */
if (metric_res->grow_factor && diff > 0) {
diff *= metric_res->grow_factor;
- next_gf *= metric->grow_factor;
+ next_gf *= task->cfg->grow_factor;
}
else if (diff > 0) {
- next_gf = metric->grow_factor;
+ next_gf = task->cfg->grow_factor;
}
diff = rspamd_check_group_score (task, symbol, gr, gr_score, diff);
/* Handle grow factor */
if (metric_res->grow_factor && w > 0) {
w *= metric_res->grow_factor;
- next_gf *= metric->grow_factor;
+ next_gf *= task->cfg->grow_factor;
}
else if (w > 0) {
- next_gf = metric->grow_factor;
+ next_gf = task->cfg->grow_factor;
}
s->name = symbol;
g_hash_table_insert (metric_res->symbols, (gpointer) symbol, s);
}
- msg_debug_task ("symbol %s, score %.2f, metric %s, factor: %f",
+ msg_debug_task ("symbol %s, score %.2f, factor: %f",
symbol,
s->score,
- metric->name,
w);
metric_res->changes ++;
/* Insert symbol to default metric */
s = insert_metric_result (task,
- task->cfg->default_metric,
symbol,
flag,
opt,
return ret;
}
-enum rspamd_metric_action
+enum rspamd_action_type
rspamd_check_action_metric (struct rspamd_task *task, struct rspamd_metric_result *mres)
{
- struct metric_action *action, *selected_action = NULL;
+ struct rspamd_action *action, *selected_action = NULL;
double max_score = 0, sc;
int i;
if (task->pre_result.action == METRIC_ACTION_MAX) {
for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
- action = &mres->metric->actions[i];
+ action = &task->cfg->actions[i];
sc = mres->actions_limits[i];
if (isnan (sc)) {
sc = NAN;
for (i = task->pre_result.action; i < METRIC_ACTION_MAX; i ++) {
- selected_action = &mres->metric->actions[i];
+ selected_action = &task->cfg->actions[i];
sc = mres->actions_limits[i];
if (isnan (sc)) {
* Result of metric processing
*/
struct rspamd_metric_result {
- struct rspamd_metric *metric; /**< pointer to metric structure */
double score; /**< total score */
double grow_factor; /**< current grow factor */
GHashTable *symbols; /**< symbols of metric */
GHashTable *sym_groups; /**< groups of symbols */
gdouble actions_limits[METRIC_ACTION_MAX]; /**< set of actions for this metric */
guint changes;
- enum rspamd_metric_action action; /**< the current action */
+ enum rspamd_action_type action; /**< the current action */
};
/**
/*
* Get action for specific metric
*/
-enum rspamd_metric_action rspamd_check_action_metric (struct rspamd_task *task,
+enum rspamd_action_type rspamd_check_action_metric (struct rspamd_task *task,
struct rspamd_metric_result *mres);
#endif
struct rspamd_log_format *prev, *next;
};
-enum rspamd_metric_action {
+enum rspamd_action_type {
METRIC_ACTION_REJECT = 0,
METRIC_ACTION_SOFT_REJECT,
METRIC_ACTION_REWRITE_SUBJECT,
METRIC_ACTION_MAX
};
-struct metric_action {
- enum rspamd_metric_action action;
+struct rspamd_action {
+ enum rspamd_action_type action;
gdouble score;
guint priority;
};
-/**
- * Common definition of metric
- */
-struct rspamd_metric {
- const gchar *name; /**< name of metric */
- gchar *func_name; /**< name of consolidation function */
- gboolean accept_unknown_symbols; /**< if true unknown symbols are registered here */
- gdouble unknown_weight; /**< weight of unknown symbols */
- gdouble grow_factor; /**< grow factor for metric */
- GHashTable *symbols; /**< weights of symbols in metric */
- const gchar *subject; /**< subject rewrite string */
- GHashTable * groups; /**< groups of symbols */
- struct metric_action actions[METRIC_ACTION_MAX]; /**< all actions of the metric */
-};
-
struct rspamd_config_post_load_script {
gint cbref;
struct rspamd_config_post_load_script *prev, *next;
gchar *profile_path;
#endif
+ gboolean accept_unknown_symbols; /**< if true unknown symbols are registered here */
+ gdouble unknown_weight; /**< weight of unknown symbols */
+ gdouble grow_factor; /**< grow factor for metric */
+ GHashTable *symbols; /**< weights of symbols in metric */
+ const gchar *subject; /**< subject rewrite string */
+ GHashTable * groups; /**< groups of symbols */
+ struct rspamd_action actions[METRIC_ACTION_MAX]; /**< all actions of the metric */
+
gboolean raw_mode; /**< work in raw mode instead of utf one */
gboolean one_shot_mode; /**< rules add only one symbol */
gboolean check_text_attachements; /**< check text attachements as text */
ucl_object_t *rcl_obj; /**< rcl object */
ucl_object_t *config_comments; /**< comments saved from the config */
ucl_object_t *doc_strings; /**< documentation strings for config options */
- GHashTable * metrics; /**< hash of metrics indexed by metric name */
- GList * metrics_list; /**< linked list of metrics */
- GHashTable * metrics_symbols; /**< hash table of metrics indexed by symbol */
GHashTable * c_modules; /**< hash of c modules indexed by module name */
GHashTable * composite_symbols; /**< hash of composite symbols indexed by its name */
GList *classifiers; /**< list of all classifiers defined */
struct symbols_cache *cache; /**< symbols cache object */
gchar *cache_filename; /**< filename of cache file */
gdouble cache_reload_time; /**< how often cache reload should be performed */
- struct rspamd_metric *default_metric; /**< default metric */
-
gchar * checksum; /**< real checksum of config file */
gchar * dump_checksum; /**< dump checksum of config file */
gpointer lua_state; /**< pointer to lua state */
/*
* Return a new metric structure, setting default and non-conflicting attributes
*/
-struct rspamd_metric * rspamd_config_new_metric (struct rspamd_config *cfg,
- struct rspamd_metric *c, const gchar *name);
+void rspamd_config_init_metric (struct rspamd_config *cfg);
/*
* Return new symbols group definition
*/
struct rspamd_symbols_group * rspamd_config_new_group (
- struct rspamd_config *cfg, struct rspamd_metric *metric,
+ struct rspamd_config *cfg,
const gchar *name);
/*
* Return a new statfile structure, setting default and non-conflicting attributes
struct rspamd_config *cfg,
struct rspamd_statfile_config *c);
-/*
- * Read XML configuration file
- */
-gboolean rspamd_config_read (struct rspamd_config *cfg,
- const gchar *filename, const gchar *convert_to,
- rspamd_rcl_section_fin_t logger_fin, gpointer logger_ud,
- GHashTable *vars);
-
/*
* Register symbols of classifiers inside metrics
*/
* @param nshots means maximum number of hits for a symbol in metric (-1 for unlimited)
* @return TRUE if symbol has been inserted or FALSE if symbol already exists with higher priority
*/
-gboolean rspamd_config_add_metric_symbol (struct rspamd_config *cfg,
+gboolean rspamd_config_add_symbol (struct rspamd_config *cfg,
const gchar *symbol, gdouble score, const gchar *description,
const gchar *group, guint flags,
guint priority,
* @return TRUE if symbol has been inserted or FALSE if action already exists with higher priority
*/
gboolean rspamd_config_set_action_score (struct rspamd_config *cfg,
- const gchar *metric,
const gchar *action_name,
gdouble score,
guint priority);
/*
* Return textual representation of action enumeration
*/
-const gchar * rspamd_action_to_str (enum rspamd_metric_action action);
-const gchar * rspamd_action_to_str_alt (enum rspamd_metric_action action);
+const gchar * rspamd_action_to_str (enum rspamd_action_type action);
+const gchar * rspamd_action_to_str_alt (enum rspamd_action_type action);
/**
* Parse radix tree or radix map from ucl object
}
struct rspamd_rcl_symbol_data {
- struct rspamd_metric *metric;
struct rspamd_symbols_group *gr;
struct rspamd_config *cfg;
};
const gchar *key, gpointer ud,
struct rspamd_rcl_section *section, GError **err)
{
- struct rspamd_rcl_symbol_data *sd = ud;
- struct rspamd_metric *metric;
+ struct rspamd_config *cfg = ud;
struct rspamd_symbols_group *gr;
const ucl_object_t *val, *cur;
struct rspamd_rcl_section *subsection;
+ struct rspamd_rcl_symbol_data sd;
g_assert (key != NULL);
- metric = sd->metric;
-
- gr = g_hash_table_lookup (metric->groups, key);
+ gr = g_hash_table_lookup (cfg->groups, key);
if (gr == NULL) {
- gr = rspamd_config_new_group (sd->cfg, metric, key);
+ gr = rspamd_config_new_group (cfg, key);
}
- if (!rspamd_rcl_section_parse_defaults (sd->cfg, section, pool, obj,
+ if (!rspamd_rcl_section_parse_defaults (cfg, section, pool, obj,
gr, err)) {
return FALSE;
}
- sd->gr = gr;
+ sd.gr = gr;
+ sd.cfg = cfg;
/* Handle symbols */
- val = ucl_object_lookup (obj, "symbol");
+ val = ucl_object_lookup (obj, "symbols");
if (val != NULL && ucl_object_type (val) == UCL_OBJECT) {
- HASH_FIND_STR (section->subsections, "symbol", subsection);
+ HASH_FIND_STR (section->subsections, "symbols", subsection);
g_assert (subsection != NULL);
LL_FOREACH (val, cur) {
- if (!rspamd_rcl_process_section (sd->cfg, subsection, sd, cur,
+ if (!rspamd_rcl_process_section (cfg, subsection, &sd, cur,
pool, err)) {
return FALSE;
}
}
if (sd->gr) {
- rspamd_config_add_metric_symbol (cfg, key, score,
+ rspamd_config_add_symbol (cfg, key, score,
description, sd->gr->name, flags, priority, nshots);
}
else {
- rspamd_config_add_metric_symbol (cfg, key, score,
+ rspamd_config_add_symbol (cfg, key, score,
description, NULL, flags, priority, nshots);
}
return TRUE;
}
-struct metric_actions_cbdata {
- struct rspamd_config *cfg;
- struct rspamd_metric *metric;
-};
-
static gboolean
rspamd_rcl_actions_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
const gchar *key, gpointer ud,
struct rspamd_rcl_section *section, GError **err)
{
gdouble action_score;
- struct metric_actions_cbdata *cbdata = ud;
+ struct rspamd_config *cfg = ud;
gint action_value;
const ucl_object_t *cur;
ucl_object_iter_t it;
- struct rspamd_metric *metric;
- struct rspamd_config *cfg;
- metric = cbdata->metric;
- cfg = cbdata->cfg;
it = ucl_object_iterate_new (obj);
while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
return FALSE;
}
else {
- rspamd_config_set_action_score (cfg, metric->name,
- ucl_object_key (cur), action_score,
+ rspamd_config_set_action_score (cfg,
+ ucl_object_key (cur),
+ action_score,
ucl_object_get_priority (cur));
}
}
return TRUE;
}
-static gboolean
-rspamd_rcl_metric_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
- const gchar *key, gpointer ud,
- struct rspamd_rcl_section *section, GError **err)
-{
- const ucl_object_t *val, *cur, *elt;
- ucl_object_iter_t it;
- struct rspamd_config *cfg = ud;
- struct rspamd_metric *metric;
- struct rspamd_rcl_section *subsection;
- struct rspamd_rcl_symbol_data sd;
- struct rspamd_symbol *sym_def;
- struct metric_actions_cbdata acts_cbdata;
-
- g_assert (key != NULL);
-
- metric = g_hash_table_lookup (cfg->metrics, key);
- if (metric == NULL) {
- metric = rspamd_config_new_metric (cfg, metric, key);
- }
-
- if (!rspamd_rcl_section_parse_defaults (cfg, section, cfg->cfg_pool, obj,
- metric, err)) {
- return FALSE;
- }
-
- if (metric->unknown_weight > 0) {
- metric->accept_unknown_symbols = TRUE;
- }
-
- /* Handle actions */
- val = ucl_object_lookup (obj, "actions");
- if (val != NULL) {
- if (val->type != UCL_OBJECT) {
- g_set_error (err, CFG_RCL_ERROR, EINVAL,
- "actions must be an object");
- return FALSE;
- }
-
- HASH_FIND_STR (section->subsections, "actions", subsection);
- g_assert (subsection != NULL);
- acts_cbdata.cfg = cfg;
- acts_cbdata.metric = metric;
-
- if (!rspamd_rcl_process_section (cfg, subsection, &acts_cbdata, val,
- cfg->cfg_pool, err)) {
- return FALSE;
- }
- }
-
- /* No more legacy mode */
-
- /* Handle grouped symbols */
- val = ucl_object_lookup (obj, "group");
- if (val != NULL && ucl_object_type (val) == UCL_OBJECT) {
- HASH_FIND_STR (section->subsections, "group", subsection);
- g_assert (subsection != NULL);
- sd.gr = NULL;
- sd.cfg = cfg;
- sd.metric = metric;
-
- LL_FOREACH (val, cur) {
- if (!rspamd_rcl_process_section (cfg, subsection, &sd, cur,
- cfg->cfg_pool, err)) {
- return FALSE;
- }
- }
- }
-
- /* Handle symbols */
- val = ucl_object_lookup (obj, "symbol");
- if (val != NULL && ucl_object_type (val) == UCL_OBJECT) {
- HASH_FIND_STR (section->subsections, "symbol", subsection);
- g_assert (subsection != NULL);
- sd.gr = NULL;
- sd.cfg = cfg;
- sd.metric = metric;
-
- LL_FOREACH (val, cur) {
- if (!rspamd_rcl_process_section (cfg, subsection, &sd, cur,
- cfg->cfg_pool, err)) {
- return FALSE;
- }
- }
- }
-
- /* Handle ignored symbols */
- val = ucl_object_lookup (obj, "ignore");
- if (val != NULL && ucl_object_type (val) == UCL_ARRAY) {
- LL_FOREACH (val, cur) {
- it = NULL;
-
- while ((elt = ucl_object_iterate (cur, &it, true)) != NULL) {
- if (ucl_object_type (elt) == UCL_STRING) {
- sym_def = g_hash_table_lookup (metric->symbols,
- ucl_object_tostring (elt));
-
- if (sym_def != NULL) {
- sym_def->flags |= RSPAMD_SYMBOL_FLAG_IGNORE;
- }
- else {
- msg_warn ("cannot find symbol %s to set ignore flag",
- ucl_object_tostring (elt));
- }
- }
- }
- }
- }
-
- return TRUE;
-}
-
static gboolean
rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
const gchar *key, gpointer ud,
description = composite_expression;
}
- rspamd_config_add_metric_symbol (cfg, composite_name, score,
+ rspamd_config_add_symbol (cfg, composite_name, score,
description, group, FALSE, FALSE,
1);
}
"Time before attempting to recover upstream after an error");
/**
- * Metric section
+ * Symbols and actions sections
*/
sub = rspamd_rcl_add_section_doc (&new,
- "metric", "name",
- rspamd_rcl_metric_handler,
+ "actions", NULL,
+ rspamd_rcl_actions_handler,
UCL_OBJECT,
FALSE,
TRUE,
cfg->doc_strings,
- "Metrics configuration");
- sub->default_key = DEFAULT_METRIC;
+ "Actions configuration");
rspamd_rcl_add_default_handler (sub,
"unknown_weight",
rspamd_rcl_parse_struct_double,
- G_STRUCT_OFFSET (struct rspamd_metric, unknown_weight),
+ G_STRUCT_OFFSET (struct rspamd_config, unknown_weight),
0,
"Accept unknown symbols with the specified weight");
rspamd_rcl_add_default_handler (sub,
"grow_factor",
rspamd_rcl_parse_struct_double,
- G_STRUCT_OFFSET (struct rspamd_metric, grow_factor),
+ G_STRUCT_OFFSET (struct rspamd_config, grow_factor),
0,
"Multiply the subsequent symbols by this number "
"(does not affect symbols with score less or "
rspamd_rcl_add_default_handler (sub,
"subject",
rspamd_rcl_parse_struct_string,
- G_STRUCT_OFFSET (struct rspamd_metric, subject),
+ G_STRUCT_OFFSET (struct rspamd_config, subject),
0,
"Rewrite subject with this value");
- /* Ungrouped symbols */
- ssub = rspamd_rcl_add_section_doc (&sub->subsections,
- "symbol", "name",
- rspamd_rcl_symbol_handler,
- UCL_OBJECT,
- TRUE,
- TRUE,
- sub->doc_ref,
- "Symbols settings");
-
- /* Actions part */
- ssub = rspamd_rcl_add_section_doc (&sub->subsections,
- "actions", NULL,
- rspamd_rcl_actions_handler,
+ sub = rspamd_rcl_add_section_doc (&new,
+ "groups", "name",
+ rspamd_rcl_group_handler,
UCL_OBJECT,
+ FALSE,
TRUE,
- TRUE,
- sub->doc_ref,
- "Actions settings");
+ cfg->doc_strings,
+ "Symbol groups configuration");
/* Group part */
ssub = rspamd_rcl_add_section_doc (&sub->subsections,
- "group", "name",
- rspamd_rcl_group_handler,
+ "symbols", "name",
+ rspamd_rcl_symbol_handler,
UCL_OBJECT,
TRUE,
TRUE,
#define CFG_RCL_H_
#include "config.h"
+#include "cfg_file.h"
#include "ucl.h"
#include "mem_pool.h"
enum rspamd_rcl_flag flags;
};
+
/**
* Common handler type
* @param cfg configuration
gboolean rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
const gchar *path,
GError **err);
+
+/*
+ * Read configuration file
+ */
+gboolean rspamd_config_read (struct rspamd_config *cfg,
+ const gchar *filename, const gchar *convert_to,
+ rspamd_rcl_section_fin_t logger_fin, gpointer logger_ud,
+ GHashTable *vars);
#endif /* CFG_RCL_H_ */
/* 20 Kb */
cfg->max_diff = 20480;
- cfg->metrics = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
- rspamd_config_new_metric (cfg, NULL, DEFAULT_METRIC);
+ rspamd_config_init_metric (cfg);
cfg->c_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
cfg->composite_symbols =
g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
cfg->classifiers_symbols = g_hash_table_new (rspamd_str_hash,
rspamd_str_equal);
cfg->cfg_params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
- cfg->metrics_symbols = g_hash_table_new_full (rspamd_str_hash, rspamd_str_equal,
- NULL, (GDestroyNotify)g_list_free);
cfg->debug_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
cfg->explicit_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
cfg->wrk_parsers = g_hash_table_new (g_int_hash, g_int_equal);
}
g_list_free (cfg->classifiers);
- g_list_free (cfg->metrics_list);
g_list_free (cfg->workers);
rspamd_symbols_cache_destroy (cfg->cache);
#ifdef WITH_HIREDIS
ucl_object_unref (cfg->config_comments);
ucl_object_unref (cfg->doc_strings);
ucl_object_unref (cfg->neighbours);
- g_hash_table_remove_all (cfg->metrics);
- g_hash_table_unref (cfg->metrics);
g_hash_table_unref (cfg->c_modules);
g_hash_table_remove_all (cfg->composite_symbols);
g_hash_table_unref (cfg->composite_symbols);
g_hash_table_remove_all (cfg->cfg_params);
g_hash_table_unref (cfg->cfg_params);
- g_hash_table_destroy (cfg->metrics_symbols);
g_hash_table_unref (cfg->classifiers_symbols);
g_hash_table_unref (cfg->debug_modules);
g_hash_table_unref (cfg->explicit_modules);
if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
/* Check for actions sanity */
int i, prev_act = 0;
- struct rspamd_metric *metric = cfg->default_metric;
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 (metric->actions[i].score)) {
- if (prev_score <= isnan (metric->actions[i].score)) {
+ if (!isnan (prev_score) && !isnan (cfg->actions[i].score)) {
+ if (prev_score <= isnan (cfg->actions[i].score)) {
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), metric->actions[i].score);
+ rspamd_action_to_str (i), cfg->actions[i].score);
ret = FALSE;
}
}
- if (!isnan (metric->actions[i].score)) {
- prev_score = metric->actions[i].score;
+ if (!isnan (cfg->actions[i].score)) {
+ prev_score = cfg->actions[i].score;
prev_act = i;
}
}
return c;
}
-struct rspamd_metric *
-rspamd_config_new_metric (struct rspamd_config *cfg, struct rspamd_metric *c,
- const gchar *name)
+void
+rspamd_config_init_metric (struct rspamd_config *cfg)
{
int i;
- if (c == NULL) {
- c = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_metric));
- c->grow_factor = 1.0;
- c->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
- c->groups = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
-
- for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
- c->actions[i].score = NAN;
- c->actions[i].action = i;
- c->actions[i].priority = 0;
- }
-
- c->subject = SPAM_SUBJECT;
- c->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
- rspamd_mempool_add_destructor (cfg->cfg_pool,
- (rspamd_mempool_destruct_t) g_hash_table_unref,
- c->symbols);
- rspamd_mempool_add_destructor (cfg->cfg_pool,
- (rspamd_mempool_destruct_t) g_hash_table_unref,
- c->groups);
-
- g_hash_table_insert (cfg->metrics, (void *)c->name, c);
- cfg->metrics_list = g_list_prepend (cfg->metrics_list, c);
+ 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);
- if (strcmp (c->name, DEFAULT_METRIC) == 0) {
- cfg->default_metric = c;
- }
+ for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
+ cfg->actions[i].score = NAN;
+ cfg->actions[i].action = i;
+ cfg->actions[i].priority = 0;
}
- return c;
+ cfg->subject = SPAM_SUBJECT;
+ rspamd_mempool_add_destructor (cfg->cfg_pool,
+ (rspamd_mempool_destruct_t) g_hash_table_unref,
+ cfg->symbols);
+ rspamd_mempool_add_destructor (cfg->cfg_pool,
+ (rspamd_mempool_destruct_t) g_hash_table_unref,
+ cfg->groups);
}
struct rspamd_symbols_group *
-rspamd_config_new_group (struct rspamd_config *cfg, struct rspamd_metric *metric,
- const gchar *name)
+rspamd_config_new_group (struct rspamd_config *cfg, const gchar *name)
{
struct rspamd_symbols_group *gr;
(rspamd_mempool_destruct_t)g_hash_table_unref, gr->symbols);
gr->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
- g_hash_table_insert (metric->groups, gr->name, gr);
+ g_hash_table_insert (cfg->groups, gr->name, gr);
return gr;
}
}
static void
-rspamd_config_new_metric_symbol (struct rspamd_config *cfg,
- struct rspamd_metric *metric, const gchar *symbol,
+rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol,
gdouble score, const gchar *description, const gchar *group,
guint flags, guint priority, gint nshots)
{
struct rspamd_symbols_group *sym_group;
struct rspamd_symbol *sym_def;
- GList *metric_list;
gdouble *score_ptr;
sym_def =
sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, description);
}
- msg_debug_config ("registered symbol %s with weight %.2f in metric %s and group %s",
- sym_def->name, score, metric->name, group);
+ msg_debug_config ("registered symbol %s with weight %.2f in and group %s",
+ sym_def->name, score, group);
- g_hash_table_insert (metric->symbols, sym_def->name, sym_def);
-
- if ((metric_list =
- g_hash_table_lookup (cfg->metrics_symbols, sym_def->name)) == NULL) {
- metric_list = g_list_prepend (NULL, metric);
- g_hash_table_insert (cfg->metrics_symbols, sym_def->name, metric_list);
- }
- else {
- /* Slow but keep start element of list in safe */
- if (!g_list_find (metric_list, metric)) {
- metric_list = g_list_append (metric_list, metric);
- }
- }
+ g_hash_table_insert (cfg->symbols, sym_def->name, sym_def);
/* Search for symbol group */
if (group == NULL) {
group = "ungrouped";
}
- sym_group = g_hash_table_lookup (metric->groups, group);
+ sym_group = g_hash_table_lookup (cfg->groups, group);
if (sym_group == NULL) {
/* Create new group */
- sym_group = rspamd_config_new_group (cfg, metric, group);
+ sym_group = rspamd_config_new_group (cfg, group);
}
sym_def->gr = sym_group;
gboolean
-rspamd_config_add_metric_symbol (struct rspamd_config *cfg,
+rspamd_config_add_symbol (struct rspamd_config *cfg,
const gchar *symbol,
gdouble score, const gchar *description, const gchar *group,
guint flags, guint priority, gint nshots)
{
struct rspamd_symbol *sym_def;
- struct rspamd_metric *metric;
-
g_assert (cfg != NULL);
g_assert (symbol != NULL);
- metric = cfg->default_metric;
- sym_def = g_hash_table_lookup (metric->symbols, symbol);
+ sym_def = g_hash_table_lookup (cfg->symbols, symbol);
if (sym_def != NULL) {
if (sym_def->priority > priority) {
}
}
- rspamd_config_new_metric_symbol (cfg, metric, symbol, score, description,
+ rspamd_config_new_symbol (cfg, symbol, score, description,
group, flags, priority, nshots);
return TRUE;
const gchar *module_name)
{
gboolean is_c = FALSE;
- struct rspamd_metric *metric;
const ucl_object_t *conf, *enabled;
GList *cur;
struct rspamd_symbols_group *gr;
- metric = cfg->default_metric;
if (g_hash_table_lookup (cfg->c_modules, module_name)) {
is_c = TRUE;
}
}
- if (metric) {
- /* Now we check symbols group */
- gr = g_hash_table_lookup (metric->groups, module_name);
+ /* Now we check symbols group */
+ gr = g_hash_table_lookup (cfg->groups, module_name);
- if (gr) {
- if (gr->disabled) {
- msg_info_config ("%s module %s is disabled in the configuration as "
- "its group has been disabled",
- is_c ? "internal" : "lua", module_name);
- return FALSE;
- }
+ if (gr) {
+ if (gr->disabled) {
+ msg_info_config ("%s module %s is disabled in the configuration as "
+ "its group has been disabled",
+ is_c ? "internal" : "lua", module_name);
+ return FALSE;
}
}
gboolean
rspamd_config_set_action_score (struct rspamd_config *cfg,
- const gchar *metric_name,
const gchar *action_name,
gdouble score,
guint priority)
{
- struct metric_action *act;
- struct rspamd_metric *metric;
+ struct rspamd_action *act;
gint act_num;
g_assert (cfg != NULL);
g_assert (action_name != NULL);
- if (metric_name == NULL) {
- metric_name = DEFAULT_METRIC;
- }
-
- metric = g_hash_table_lookup (cfg->metrics, metric_name);
-
- if (metric == NULL) {
- msg_err_config ("metric %s has not been found", metric_name);
- return FALSE;
- }
-
if (!rspamd_action_from_str (action_name, &act_num)) {
msg_err_config ("invalid action name: %s", action_name);
return FALSE;
g_assert (act_num >= METRIC_ACTION_REJECT && act_num < METRIC_ACTION_MAX);
- act = &metric->actions[act_num];
+ act = &cfg->actions[act_num];
if (isnan (act->score)) {
act->score = score;
}
const gchar *
-rspamd_action_to_str (enum rspamd_metric_action action)
+rspamd_action_to_str (enum rspamd_action_type action)
{
switch (action) {
case METRIC_ACTION_REJECT:
}
const gchar *
-rspamd_action_to_str_alt (enum rspamd_metric_action action)
+rspamd_action_to_str_alt (enum rspamd_action_type action)
{
switch (action) {
case METRIC_ACTION_REJECT:
struct rspamd_symbol_result *ms = NULL;
struct rspamd_symbols_group *gr;
struct rspamd_symbol *sdef;
- struct rspamd_metric *metric;
GHashTableIter it;
gpointer k, v;
gint rc = 0;
}
if (strncmp (sym, "g:", 2) == 0) {
- metric = g_hash_table_lookup (cd->task->cfg->metrics, DEFAULT_METRIC);
- g_assert (metric != NULL);
- gr = g_hash_table_lookup (metric->groups, sym + 2);
+ gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 2);
if (gr != NULL) {
g_hash_table_iter_init (&it, gr->symbols);
gint test_act;
const ucl_object_t *cur_elt, *cur_nm, *it_val;
ucl_object_iter_t it = NULL;
- struct rspamd_metric *real_metric;
const gchar *name;
gdouble nscore;
static const guint priority = 3;
"loaded json metric object element has no 'metric' attribute");
continue;
}
- real_metric = g_hash_table_lookup (cfg->metrics,
- ucl_object_tostring (cur_nm));
- if (real_metric == NULL) {
- msg_warn ("cannot find metric %s", ucl_object_tostring (cur_nm));
- continue;
- }
cur_nm = ucl_object_lookup (cur_elt, "symbols");
/* Parse symbols */
/*
* We use priority = 3 here
*/
- rspamd_config_add_metric_symbol (cfg,
+ rspamd_config_add_symbol (cfg,
ucl_object_tostring (n), nscore, NULL, NULL,
0, priority, cfg->default_max_shots);
}
}
nscore = ucl_object_todouble (ucl_object_lookup (it_val,
"value"));
- rspamd_config_set_action_score (cfg, real_metric->name,
- name, nscore, priority);
+ rspamd_config_set_action_score (cfg, name, nscore, priority);
}
else {
msg_info (
/* Write new subject */
static const gchar *
-make_rewritten_subject (struct rspamd_metric *metric, struct rspamd_task *task)
+make_rewritten_subject (struct rspamd_task *task)
{
GString *subj_buf;
gchar *res;
c = rspamd_mempool_get_variable (task->task_pool, "metric_subject");
if (c == NULL) {
- c = metric->subject;
+ c = task->cfg->subject;
}
if (c == NULL) {
}
static ucl_object_t *
-rspamd_metric_symbol_ucl (struct rspamd_task *task, struct rspamd_metric *m,
- struct rspamd_symbol_result *sym)
+rspamd_metric_symbol_ucl (struct rspamd_task *task, struct rspamd_symbol_result *sym)
{
ucl_object_t *obj = NULL, *ar;
const gchar *description = NULL;
{
GHashTableIter hiter;
struct rspamd_symbol_result *sym;
- struct rspamd_metric *m;
gboolean is_spam;
- enum rspamd_metric_action action = METRIC_ACTION_NOACTION;
+ enum rspamd_action_type action = METRIC_ACTION_NOACTION;
ucl_object_t *obj = NULL, *sobj;
gpointer h, v;
const gchar *subject;
- m = mres->metric;
-
if (mres->action == METRIC_ACTION_MAX) {
mres->action = rspamd_check_action_metric (task, mres);
}
"action", 0, false);
if (action == METRIC_ACTION_REWRITE_SUBJECT) {
- subject = make_rewritten_subject (m, task);
+ subject = make_rewritten_subject (task);
if (subject) {
ucl_object_insert_key (obj, ucl_object_fromstring (subject),
while (g_hash_table_iter_next (&hiter, &h, &v)) {
sym = (struct rspamd_symbol_result *)v;
- sobj = rspamd_metric_symbol_ucl (task, m, sym);
+ sobj = rspamd_metric_symbol_ucl (task, sym);
ucl_object_insert_key (obj, sobj, h, 0, false);
}
/* Read number of regexps */
g_assert (fd != -1);
lseek (fd, RSPAMD_HS_MAGIC_LEN + sizeof (cache->plt), SEEK_SET);
- read (fd, &n, sizeof (n));
+ g_assert (read (fd, &n, sizeof (n)) == sizeof (n));
close (fd);
if (re_class->type_len > 0) {
rspamd_symbols_cache_validate_cb (gpointer k, gpointer v, gpointer ud)
{
struct cache_item *item = v, *parent;
+ struct rspamd_config *cfg;
struct symbols_cache *cache = (struct symbols_cache *)ud;
- GList *cur;
- struct rspamd_metric *m;
struct rspamd_symbol *s;
gboolean skipped, ghost;
gint p1, p2;
ghost = item->st->weight == 0 ? TRUE : FALSE;
+ cfg = cache->cfg;
/* Check whether this item is skipped */
skipped = !ghost;
- g_assert (cache->cfg != NULL);
+ g_assert (cfg != NULL);
if ((item->type &
(SYMBOL_TYPE_NORMAL|SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_COMPOSITE|SYMBOL_TYPE_CLASSIFIER))
- && g_hash_table_lookup (cache->cfg->metrics_symbols, item->symbol) == NULL) {
- cur = g_list_first (cache->cfg->metrics_list);
- while (cur) {
- m = cur->data;
-
- if (m->accept_unknown_symbols) {
- GList *mlist;
-
- skipped = FALSE;
- item->st->weight = m->unknown_weight;
- s = rspamd_mempool_alloc0 (cache->static_pool,
- sizeof (*s));
- s->name = item->symbol;
- s->weight_ptr = &item->st->weight;
- g_hash_table_insert (m->symbols, item->symbol, s);
- mlist = g_hash_table_lookup (cache->cfg->metrics_symbols,
- item->symbol);
- mlist = g_list_append (mlist, m);
- g_hash_table_insert (cache->cfg->metrics_symbols,
- item->symbol, mlist);
-
- msg_info_cache ("adding unknown symbol %s to metric %s", item->symbol,
- m->name);
- ghost = FALSE;
- }
+ && g_hash_table_lookup (cfg->symbols, item->symbol) == NULL) {
+
+ if (cfg->accept_unknown_symbols) {
- cur = g_list_next (cur);
+ skipped = FALSE;
+ item->st->weight = cfg->unknown_weight;
+ s = rspamd_mempool_alloc0 (cache->static_pool,
+ sizeof (*s));
+ s->name = item->symbol;
+ s->weight_ptr = &item->st->weight;
+ g_hash_table_insert (cfg->symbols, item->symbol, s);
+
+ msg_info_cache ("adding unknown symbol %s", item->symbol);
+ ghost = FALSE;
}
}
else {
{
struct cache_item *item;
GHashTableIter it;
- GList *cur;
gpointer k, v;
struct rspamd_symbol *sym_def;
- struct rspamd_metric *metric;
gboolean ignore_symbol = FALSE, ret = TRUE;
if (cache == NULL) {
}
/* Now adjust symbol weights according to default metric */
- if (cfg->default_metric != NULL) {
- g_hash_table_foreach (cfg->default_metric->symbols,
+ g_hash_table_foreach (cfg->symbols,
rspamd_symbols_cache_metric_validate_cb,
cache);
- }
g_hash_table_foreach (cache->items_by_symbol,
rspamd_symbols_cache_validate_cb,
cache);
/* Now check each metric item and find corresponding symbol in a cache */
- g_hash_table_iter_init (&it, cfg->metrics_symbols);
+ g_hash_table_iter_init (&it, cfg->symbols);
while (g_hash_table_iter_next (&it, &k, &v)) {
ignore_symbol = FALSE;
- cur = v;
+ sym_def = v;
- while (cur) {
- metric = cur->data;
- sym_def = g_hash_table_lookup (metric->symbols, k);
-
- if (sym_def && (sym_def->flags & RSPAMD_SYMBOL_FLAG_IGNORE)) {
- ignore_symbol = TRUE;
- break;
- }
-
- cur = g_list_next (cur);
+ if (sym_def && (sym_def->flags & RSPAMD_SYMBOL_FLAG_IGNORE)) {
+ ignore_symbol = TRUE;
+ break;
}
if (!ignore_symbol) {
struct symbols_cache *cache)
{
const ucl_object_t *wl, *cur, *disabled, *enabled;
- struct rspamd_metric *def;
struct rspamd_symbols_group *gr;
GHashTableIter gr_it;
ucl_object_iter_t it = NULL;
/* Enable groups of symbols */
enabled = ucl_object_lookup (task->settings, "groups_enabled");
- def = g_hash_table_lookup (task->cfg->metrics, DEFAULT_METRIC);
- if (def && enabled) {
+ if (enabled) {
it = NULL;
rspamd_symbols_cache_disable_all_symbols (task, cache);
while ((cur = ucl_iterate_object (enabled, &it, true)) != NULL) {
if (ucl_object_type (cur) == UCL_STRING) {
- gr = g_hash_table_lookup (def->groups,
+ gr = g_hash_table_lookup (task->cfg->groups,
ucl_object_tostring (cur));
if (gr) {
/* Disable groups of symbols */
disabled = ucl_object_lookup (task->settings, "groups_disabled");
- def = g_hash_table_lookup (task->cfg->metrics, DEFAULT_METRIC);
- if (def && disabled) {
+ if (disabled) {
it = NULL;
while ((cur = ucl_iterate_object (disabled, &it, true)) != NULL) {
if (ucl_object_type (cur) == UCL_STRING) {
- gr = g_hash_table_lookup (def->groups,
+ gr = g_hash_table_lookup (task->cfg->groups,
ucl_object_tostring (cur));
if (gr) {
{
struct _pool_chain *cur;
guint i;
- gsize len;
POOL_MTX_LOCK ();
g_atomic_int_add (&mem_pool_stat->bytes_allocated,
-((gint)cur->len));
g_atomic_int_add (&mem_pool_stat->chunks_allocated, -1);
- len = cur->len + sizeof (struct _pool_chain);
g_free (cur);
}
return 0;
}
- (void)write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") -
- 1);
+ g_assert (write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") -
+ 1) != -1);
/* Save the current sighandler */
for (i = 0; i < NSIG; i++) {
}
}
*p = '\0';
- (void)write (output, "\n", 1);
+ g_assert (write (output, "\n", 1) == 1);
/* Restore terminal state */
if (memcmp (&term, &oterm, sizeof (term)) != 0) {
static void
lua_process_metric (lua_State *L, const gchar *name, struct rspamd_config *cfg)
{
- GList *metric_list;
gchar *symbol;
const gchar *desc = NULL;
- struct rspamd_metric *metric;
gdouble *score;
struct rspamd_symbol *s;
- /* Get module opt structure */
- if ((metric = g_hash_table_lookup (cfg->metrics, name)) == NULL) {
- metric = rspamd_config_new_metric (cfg, metric, name);
- }
-
/* Now iterate through module table */
for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
/* key - -2, value - -1 */
- symbol =
- rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, -2));
+ symbol = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, -2));
if (symbol != NULL) {
if (lua_istable (L, -1)) {
/* We got a table, so extract individual attributes */
lua_pushstring (L, "weight");
lua_gettable (L, -2);
if (lua_isnumber (L, -1)) {
- score =
- rspamd_mempool_alloc (cfg->cfg_pool, sizeof (double));
+ score = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (double));
*score = lua_tonumber (L, -1);
}
else {
}
/* Insert symbol */
if ((s =
- g_hash_table_lookup (metric->symbols, symbol)) != NULL) {
+ g_hash_table_lookup (cfg->symbols, symbol)) != NULL) {
msg_info_config("replacing weight for symbol %s: %.2f -> %.2f",
- symbol,
- *s->weight_ptr,
- *score);
+ symbol,
+ *s->weight_ptr,
+ *score);
s->weight_ptr = score;
}
else {
s = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*s));
s->name = symbol;
s->weight_ptr = score;
- g_hash_table_insert (metric->symbols, symbol, s);
+ g_hash_table_insert (cfg->symbols, symbol, s);
}
if (desc) {
s->description = rspamd_mempool_strdup (cfg->cfg_pool, desc);
}
-
- if ((metric_list =
- g_hash_table_lookup (cfg->metrics_symbols, symbol)) == NULL) {
- metric_list = g_list_prepend (NULL, metric);
- rspamd_mempool_add_destructor (cfg->cfg_pool,
- (rspamd_mempool_destruct_t)g_list_free, metric_list);
- g_hash_table_insert (cfg->metrics_symbols, symbol, metric_list);
- }
- else {
- /* Slow but keep start element of list in safe */
- if (!g_list_find (metric_list, metric)) {
- metric_list = g_list_append (metric_list, metric);
- }
- }
}
}
}
nshots = 1;
}
- rspamd_config_add_metric_symbol (cfg, name,
- score, description, group, flags, (guint)priority, nshots);
+ rspamd_config_add_symbol (cfg, name,
+ score, description, group, flags, (guint) priority, nshots);
}
}
else {
lua_config_set_metric_symbol (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
- const gchar *metric_name = DEFAULT_METRIC, *description = NULL,
+ const gchar *description = NULL,
*group = NULL, *name = NULL, *flags_str = NULL;
double weight;
- struct rspamd_metric *metric;
gboolean one_shot = FALSE, one_param = FALSE;
GError *err = NULL;
gdouble priority = 0.0;
if (lua_type (L, 2) == LUA_TTABLE) {
if (!rspamd_lua_parse_table_arguments (L, 2, &err,
"*name=S;score=N;description=S;"
- "group=S;one_shot=B;one_param=B;metric=S;priority=N;flags=S;"
+ "group=S;one_shot=B;one_param=B;priority=N;flags=S;"
"nshots=I",
&name, &weight, &description,
&group, &one_shot, &one_param,
- &metric_name, &priority, &flags_str, &nshots)) {
+ &priority, &flags_str, &nshots)) {
msg_err_config ("bad arguments: %e", err);
g_error_free (err);
description = luaL_checkstring (L, 4);
}
if (lua_gettop (L) > 4 && lua_type (L, 5) == LUA_TSTRING) {
- metric_name = luaL_checkstring (L, 5);
+ /* XXX: metrics */
}
if (lua_gettop (L) > 5 && lua_type (L, 6) == LUA_TSTRING) {
group = luaL_checkstring (L, 6);
}
}
- if (metric_name == NULL) {
- metric_name = DEFAULT_METRIC;
- }
-
if (nshots == 0) {
nshots = cfg->default_max_shots;
}
- metric = g_hash_table_lookup (cfg->metrics, metric_name);
if (one_shot) {
nshots = 1;
}
}
}
- if (metric == NULL) {
- msg_err_config ("metric named %s is not defined", metric_name);
- }
- else if (name != NULL && weight != 0) {
- rspamd_config_add_metric_symbol (cfg, name,
- weight, description, group, flags, (guint)priority, nshots);
- }
+ rspamd_config_add_symbol (cfg, name,
+ weight, description, group, flags, (guint) priority, nshots);
}
else {
return luaL_error (L, "invalid arguments, rspamd_config expected");
lua_config_get_metric_symbol (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
- const gchar *sym_name = luaL_checkstring (L, 2),
- *metric_name = DEFAULT_METRIC;
+ const gchar *sym_name = luaL_checkstring (L, 2);
struct rspamd_symbol *sym_def;
- struct rspamd_metric *metric;
if (cfg && sym_name) {
- metric = g_hash_table_lookup (cfg->metrics, metric_name);
+ sym_def = g_hash_table_lookup (cfg->symbols, sym_name);
- if (metric == NULL) {
- msg_err_config ("metric named %s is not defined", metric_name);
+ if (sym_def == NULL) {
lua_pushnil (L);
}
else {
- sym_def = g_hash_table_lookup (metric->symbols, sym_name);
+ lua_createtable (L, 0, 3);
+ lua_pushstring (L, "score");
+ lua_pushnumber (L, sym_def->score);
+ lua_settable (L, -3);
- if (sym_def == NULL) {
- lua_pushnil (L);
- }
- else {
- lua_createtable (L, 0, 3);
- lua_pushstring (L, "score");
- lua_pushnumber (L, sym_def->score);
+ if (sym_def->description) {
+ lua_pushstring (L, "description");
+ lua_pushstring (L, sym_def->description);
lua_settable (L, -3);
+ }
- if (sym_def->description) {
- lua_pushstring (L, "description");
- lua_pushstring (L, sym_def->description);
- lua_settable (L, -3);
- }
-
- if (sym_def->gr) {
- lua_pushstring (L, "group");
- lua_pushstring (L, sym_def->gr->name);
- lua_settable (L, -3);
- }
+ if (sym_def->gr) {
+ lua_pushstring (L, "group");
+ lua_pushstring (L, sym_def->gr->name);
+ lua_settable (L, -3);
}
}
}
lua_config_set_metric_action (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
- const gchar *metric_name = DEFAULT_METRIC, *name = NULL;
+ const gchar *name = NULL;
double weight;
- struct rspamd_metric *metric;
GError *err = NULL;
gdouble priority = 0.0;
if (lua_type (L, 2) == LUA_TTABLE) {
if (!rspamd_lua_parse_table_arguments (L, 2, &err,
"*action=S;score=N;"
- "metric=S;priority=N",
+ "priority=N",
&name, &weight,
- &metric_name, &priority)) {
+ &priority)) {
msg_err_config ("bad arguments: %e", err);
g_error_free (err);
return luaL_error (L, "invalid arguments, table expected");
}
- if (metric_name == NULL) {
- metric_name = DEFAULT_METRIC;
- }
-
- metric = g_hash_table_lookup (cfg->metrics, metric_name);
-
- if (metric == NULL) {
- msg_err_config ("metric named %s is not defined", metric_name);
- }
- else if (name != NULL && weight != 0) {
- rspamd_config_set_action_score (cfg, metric_name, name,
- weight, (guint)priority);
+ if (name != NULL && weight != 0) {
+ rspamd_config_set_action_score (cfg, name, weight, (guint)priority);
}
}
else {
lua_config_get_metric_action (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
- const gchar *metric_name = DEFAULT_METRIC,
- *act_name = luaL_checkstring (L, 2);
- struct rspamd_metric *metric;
+ const gchar *act_name = luaL_checkstring (L, 2);
gint act = 0;
if (cfg && act_name) {
- metric = g_hash_table_lookup (cfg->metrics, metric_name);
-
- if (metric == NULL) {
- msg_err_config ("metric named %s is not defined", metric_name);
- lua_pushnil (L);
- }
- else {
- if (rspamd_action_from_str (act_name, &act)) {
- if (!isnan (metric->actions[act].score)) {
- lua_pushnumber (L, metric->actions[act].score);
- }
- else {
- lua_pushnil (L);
- }
+ if (rspamd_action_from_str (act_name, &act)) {
+ if (!isnan (cfg->actions[act].score)) {
+ lua_pushnumber (L, cfg->actions[act].score);
}
else {
lua_pushnil (L);
}
}
+ else {
+ lua_pushnil (L);
+ }
}
else {
return luaL_error (L, "invalid arguments, rspamd_config expected");
* Now check if a symbol has not been registered in any metric and
* insert default value if applicable
*/
- if (g_hash_table_lookup (cfg->metrics_symbols, name) == NULL) {
+ if (g_hash_table_lookup (cfg->symbols, name) == NULL) {
nshots = cfg->default_max_shots;
lua_pushstring (L, "score");
lua_gettable (L, -2);
* Do not override the existing symbols (using zero priority),
* since we are defining default values here
*/
- rspamd_config_add_metric_symbol (cfg, name, score,
+ rspamd_config_add_symbol (cfg, name, score,
description, group, flags, 0, nshots);
}
else {
lua_fann_push_train_result (cbdata, rep.errcode, rep.mse, rep.errmsg);
}
- write (cbdata->pair[0], "", 1);
+ g_assert (write (cbdata->pair[0], "", 1) == 1);
g_thread_join (cbdata->t);
close (cbdata->pair[0]);
close (cbdata->pair[1]);
lua_task_set_pre_result (lua_State * L)
{
struct rspamd_task *task = lua_check_task (L, 1);
+ struct rspamd_config *cfg;
struct rspamd_metric_result *mres;
gchar *action_str;
gint action = METRIC_ACTION_MAX;
if (task != NULL) {
+ cfg = task->cfg;
+
if (lua_type (L, 2) == LUA_TNUMBER) {
action = lua_tointeger (L, 2);
}
if (!task->result) {
mres = rspamd_create_metric_result (task);
if (mres != NULL) {
- mres->score = mres->metric->actions[action].score;
+ mres->score = cfg->actions[action].score;
mres->action = action;
}
}
{
struct rspamd_task *task = lua_check_task (L, 1);
struct rspamd_metric_result *metric_res;
- enum rspamd_metric_action action;
+ enum rspamd_action_type action;
if (task) {
if ((metric_res = task->result) != NULL) {
"DKIM_SIGN", dkim_module_ctx->symbol_reject);
}
- rspamd_config_add_metric_symbol (cfg,
+ rspamd_config_add_symbol (cfg,
"DKIM_SIGN", 0.0, "DKIM signature fake symbol",
"dkim", RSPAMD_SYMBOL_FLAG_IGNORE, 1, 1);
- rspamd_config_add_metric_symbol (cfg,
+ rspamd_config_add_symbol (cfg,
"DKIM_TRACE", 0.0, "DKIM trace symbol",
"dkim", RSPAMD_SYMBOL_FLAG_IGNORE, 1, 1);
priority = ucl_object_toint (elt);
}
- rspamd_config_add_metric_symbol (cfg, cur_item->symbol,
+ rspamd_config_add_symbol (cfg, cur_item->symbol,
score, description, group, flags, priority, nshots);
}
}