Browse Source

[Feature] Implement composites policies

tags/1.3.0
Vsevolod Stakhov 7 years ago
parent
commit
3262b2b175
3 changed files with 66 additions and 2 deletions
  1. 16
    1
      src/libserver/cfg_rcl.c
  2. 38
    1
      src/libserver/composites.c
  3. 12
    0
      src/libserver/composites.h

+ 16
- 1
src/libserver/cfg_rcl.c View File

@@ -1430,7 +1430,7 @@ rspamd_rcl_composite_handler (rspamd_mempool_t *pool,
}

composite =
rspamd_mempool_alloc (cfg->cfg_pool, sizeof (struct rspamd_composite));
rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_composite));
composite->expr = expr;
composite->id = g_hash_table_size (cfg->composite_symbols);
g_hash_table_insert (cfg->composite_symbols,
@@ -1474,6 +1474,21 @@ rspamd_rcl_composite_handler (rspamd_mempool_t *pool,
description, group, FALSE, FALSE);
}

val = ucl_object_lookup (obj, "policy");

if (val) {
composite->policy = rspamd_composite_policy_from_str (
ucl_object_tostring (val));

if (composite->policy == RSPAMD_COMPOSITE_POLICY_UNKNOWN) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"composite %s has incorrect policy", composite_name);
return FALSE;
}
}

return TRUE;
}


+ 38
- 1
src/libserver/composites.c View File

@@ -194,7 +194,21 @@ rspamd_composite_expr_process (gpointer input, rspamd_expression_atom_t *atom)
nrd->ms = ms;

/* By default remove symbols */
nrd->action = (RSPAMD_COMPOSITE_REMOVE_SYMBOL|RSPAMD_COMPOSITE_REMOVE_WEIGHT);
switch (cd->composite->policy) {
case RSPAMD_COMPOSITE_POLICY_REMOVE_ALL:
default:
nrd->action = (RSPAMD_COMPOSITE_REMOVE_SYMBOL|RSPAMD_COMPOSITE_REMOVE_WEIGHT);
break;
case RSPAMD_COMPOSITE_POLICY_REMOVE_SYMBOL:
nrd->action = RSPAMD_COMPOSITE_REMOVE_SYMBOL;
break;
case RSPAMD_COMPOSITE_POLICY_REMOVE_WEIGHT:
nrd->action = RSPAMD_COMPOSITE_REMOVE_WEIGHT;
break;
case RSPAMD_COMPOSITE_POLICY_LEAVE:
nrd->action = 0;
break;
}

for (;;) {
t = *beg;
@@ -381,3 +395,26 @@ rspamd_make_composites (struct rspamd_task *task)
{
g_hash_table_foreach (task->results, composites_metric_callback, task);
}


enum rspamd_composite_policy
rspamd_composite_policy_from_str (const gchar *string)
{
enum rspamd_composite_policy ret = RSPAMD_COMPOSITE_POLICY_UNKNOWN;

if (strcmp (string, "remove") == 0 || strcmp (string, "remove_all") == 0 ||
strcmp (string, "default") == 0) {
ret = RSPAMD_COMPOSITE_POLICY_REMOVE_ALL;
}
else if (strcmp (string, "remove_symbol") == 0) {
ret = RSPAMD_COMPOSITE_POLICY_REMOVE_SYMBOL;
}
else if (strcmp (string, "remove_weight") == 0) {
ret = RSPAMD_COMPOSITE_POLICY_REMOVE_WEIGHT;
}
else if (strcmp (string, "leave") == 0 || strcmp (string, "remove_none") == 0) {
ret = RSPAMD_COMPOSITE_POLICY_LEAVE;
}

return ret;
}

+ 12
- 0
src/libserver/composites.h View File

@@ -24,12 +24,22 @@ struct rspamd_task;
* Subr for composite expressions
*/
extern const struct rspamd_atom_subr composite_expr_subr;

enum rspamd_composite_policy {
RSPAMD_COMPOSITE_POLICY_REMOVE_ALL = 0,
RSPAMD_COMPOSITE_POLICY_REMOVE_SYMBOL,
RSPAMD_COMPOSITE_POLICY_REMOVE_WEIGHT,
RSPAMD_COMPOSITE_POLICY_LEAVE,
RSPAMD_COMPOSITE_POLICY_UNKNOWN
};

/**
* Composite structure
*/
struct rspamd_composite {
struct rspamd_expression *expr;
gint id;
enum rspamd_composite_policy policy;
};

/**
@@ -38,4 +48,6 @@ struct rspamd_composite {
*/
void rspamd_make_composites (struct rspamd_task *task);

enum rspamd_composite_policy rspamd_composite_policy_from_str (const gchar *string);

#endif /* SRC_LIBSERVER_COMPOSITES_H_ */

Loading…
Cancel
Save