aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-05-15 14:21:41 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-05-15 14:21:41 +0100
commitf57c6e2c50e05aebbc9020fc9fe58b647b15b9dd (patch)
tree53f4d90ce06efaabf776586bdd8e038744b33a5f /src/libserver
parent1d2cb297a1cd882e6b0ea75ca5c081f844742c18 (diff)
downloadrspamd-f57c6e2c50e05aebbc9020fc9fe58b647b15b9dd.tar.gz
rspamd-f57c6e2c50e05aebbc9020fc9fe58b647b15b9dd.zip
[Feature] Allow multiple groups for symbols
Diffstat (limited to 'src/libserver')
-rw-r--r--src/libserver/cfg_file.h26
-rw-r--r--src/libserver/cfg_rcl.c34
-rw-r--r--src/libserver/cfg_utils.c101
3 files changed, 140 insertions, 21 deletions
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 7a6cc1c8a..5a846a1f5 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -123,8 +123,8 @@ struct rspamd_symbol {
gdouble *weight_ptr;
gdouble score;
guint priority;
- struct rspamd_symbols_group *gr;
- GList *groups;
+ struct rspamd_symbols_group *gr; /* Main group */
+ GPtrArray *groups; /* Other groups */
guint flags;
gint nshots;
};
@@ -585,10 +585,24 @@ gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig);
* @return TRUE if symbol has been inserted or FALSE if symbol already exists with higher priority
*/
gboolean 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);
+ const gchar *symbol,
+ gdouble score,
+ const gchar *description,
+ const gchar *group,
+ guint flags,
+ guint priority,
+ gint nshots);
+
+/**
+ * Adds new group for a symbol
+ * @param cfg
+ * @param symbol
+ * @param group
+ * @return
+ */
+gboolean rspamd_config_add_symbol_group (struct rspamd_config *cfg,
+ const gchar *symbol,
+ const gchar *group);
/**
* Sets action score for a specified metric with the specified priority
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index 47e730af0..d4aa5314b 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -438,6 +438,22 @@ rspamd_rcl_symbol_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
description, NULL, flags, priority, nshots);
}
+ elt = ucl_object_lookup (obj, "groups");
+
+ if (elt) {
+ ucl_object_iter_t gr_it;
+ const ucl_object_t *cur_gr;
+
+ gr_it = ucl_object_iterate_new (elt);
+
+ while ((cur_gr = ucl_object_iterate_safe (gr_it, true)) != NULL) {
+ rspamd_config_add_symbol_group (cfg, key,
+ ucl_object_tostring (cur_gr));
+ }
+
+ ucl_object_iterate_free (gr_it);
+ }
+
return TRUE;
}
@@ -1423,7 +1439,7 @@ rspamd_rcl_composite_handler (rspamd_mempool_t *pool,
struct rspamd_rcl_section *section,
GError **err)
{
- const ucl_object_t *val;
+ const ucl_object_t *val, *elt;
struct rspamd_expression *expr;
struct rspamd_config *cfg = ud;
struct rspamd_composite *composite;
@@ -1500,6 +1516,22 @@ rspamd_rcl_composite_handler (rspamd_mempool_t *pool,
rspamd_config_add_symbol (cfg, composite_name, score,
description, group, FALSE, FALSE,
1);
+
+ elt = ucl_object_lookup (obj, "groups");
+
+ if (elt) {
+ ucl_object_iter_t gr_it;
+ const ucl_object_t *cur_gr;
+
+ gr_it = ucl_object_iterate_new (elt);
+
+ while ((cur_gr = ucl_object_iterate_safe (gr_it, true)) != NULL) {
+ rspamd_config_add_symbol_group (cfg, key,
+ ucl_object_tostring (cur_gr));
+ }
+
+ ucl_object_iterate_free (gr_it);
+ }
}
val = ucl_object_lookup (obj, "policy");
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index ed9876b8d..bf63b2188 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -1470,6 +1470,9 @@ rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol,
sym_def->priority = priority;
sym_def->flags = flags;
sym_def->nshots = nshots;
+ sym_def->groups = g_ptr_array_sized_new (1);
+ rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard,
+ sym_def->groups);
if (description) {
sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, description);
@@ -1502,30 +1505,49 @@ rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol,
gboolean
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)
+ const gchar *symbol,
+ gdouble score, const gchar *description,
+ const gchar *group,
+ guint flags, guint priority, gint nshots)
{
struct rspamd_symbol *sym_def;
+ struct rspamd_symbols_group *sym_group;
+ guint i;
+
g_assert (cfg != NULL);
g_assert (symbol != NULL);
- struct rspamd_symbols_group *sym_group;
sym_def = g_hash_table_lookup (cfg->symbols, symbol);
if (sym_def != NULL) {
- if (sym_def->flags & RSPAMD_SYMBOL_FLAG_UNGROUPPED && group != NULL) {
- /* Non-empty group has a priority over non-groupped one */
- sym_group = g_hash_table_lookup (cfg->groups, group);
-
- if (sym_group == NULL) {
- /* Create new group */
- sym_group = rspamd_config_new_group (cfg, group);
+ if (group != NULL) {
+ gboolean has_group = FALSE;
+
+ PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) {
+ if (g_ascii_strcasecmp (sym_group->name, group) == 0) {
+ /* Group is already here */
+ has_group = TRUE;
+ break;
+ }
}
- sym_def->gr = sym_group;
- g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
- sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPPED);
+ if (!has_group) {
+ /* Non-empty group has a priority over non-groupped one */
+ sym_group = g_hash_table_lookup (cfg->groups, group);
+
+ if (sym_group == NULL) {
+ /* Create new group */
+ sym_group = rspamd_config_new_group (cfg, group);
+ }
+
+ if (!sym_def->gr) {
+ sym_def->gr = sym_group;
+ }
+
+ g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
+ sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPPED);
+ g_ptr_array_add (sym_def->groups, sym_group);
+ }
}
if (sym_def->priority > priority) {
@@ -1593,6 +1615,57 @@ rspamd_config_add_symbol (struct rspamd_config *cfg,
}
gboolean
+rspamd_config_add_symbol_group (struct rspamd_config *cfg,
+ const gchar *symbol,
+ const gchar *group)
+{
+ struct rspamd_symbol *sym_def;
+ struct rspamd_symbols_group *sym_group;
+ guint i;
+
+ g_assert (cfg != NULL);
+ g_assert (symbol != NULL);
+ g_assert (group != NULL);
+
+ sym_def = g_hash_table_lookup (cfg->symbols, symbol);
+
+ if (sym_def != NULL) {
+ gboolean has_group = FALSE;
+
+ PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) {
+ if (g_ascii_strcasecmp (sym_group->name, group) == 0) {
+ /* Group is already here */
+ has_group = TRUE;
+ break;
+ }
+ }
+
+ if (!has_group) {
+ /* Non-empty group has a priority over non-groupped one */
+ sym_group = g_hash_table_lookup (cfg->groups, group);
+
+ if (sym_group == NULL) {
+ /* Create new group */
+ sym_group = rspamd_config_new_group (cfg, group);
+ }
+
+ if (!sym_def->gr) {
+ sym_def->gr = sym_group;
+ }
+
+ g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
+ sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPPED);
+ g_ptr_array_add (sym_def->groups, sym_group);
+
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+
+gboolean
rspamd_config_is_module_enabled (struct rspamd_config *cfg,
const gchar *module_name)
{