Browse Source

[Project] Migrate chartable module

tags/1.7.9
Vsevolod Stakhov 6 years ago
parent
commit
8e556de76e
1 changed files with 37 additions and 21 deletions
  1. 37
    21
      src/plugins/chartable.c

+ 37
- 21
src/plugins/chartable.c View File

const gchar *url_symbol; const gchar *url_symbol;
double threshold; double threshold;
guint max_word_len; guint max_word_len;

rspamd_mempool_t *chartable_pool;
}; };


static struct chartable_ctx *chartable_module_ctx = NULL;
static inline struct chartable_ctx *
chartable_get_context (struct rspamd_config *cfg)
{
return (struct chartable_ctx *)g_ptr_array_index (cfg->c_modules,
chartable_module.ctx_offset);
}

static void chartable_symbol_callback (struct rspamd_task *task, void *unused); static void chartable_symbol_callback (struct rspamd_task *task, void *unused);
static void chartable_url_symbol_callback (struct rspamd_task *task, void *unused); static void chartable_url_symbol_callback (struct rspamd_task *task, void *unused);


gint gint
chartable_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) chartable_module_init (struct rspamd_config *cfg, struct module_ctx **ctx)
{ {
if (chartable_module_ctx == NULL) {
chartable_module_ctx = g_malloc (sizeof (struct chartable_ctx));
chartable_module_ctx->max_word_len = 10;
}
struct chartable_ctx *chartable_module_ctx;

chartable_module_ctx = rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (*chartable_module_ctx));
chartable_module_ctx->max_word_len = 10;


*ctx = (struct module_ctx *)chartable_module_ctx; *ctx = (struct module_ctx *)chartable_module_ctx;


{ {
const ucl_object_t *value; const ucl_object_t *value;
gint res = TRUE; gint res = TRUE;
struct chartable_ctx *chartable_module_ctx = chartable_get_context (cfg);


if (!rspamd_config_is_module_enabled (cfg, "chartable")) { if (!rspamd_config_is_module_enabled (cfg, "chartable")) {
return TRUE; return TRUE;


static gdouble static gdouble
rspamd_chartable_process_word_utf (struct rspamd_task *task, rspamd_chartable_process_word_utf (struct rspamd_task *task,
rspamd_stat_token_t *w,
gboolean is_url,
guint *ncap)
rspamd_stat_token_t *w,
gboolean is_url,
guint *ncap,
struct chartable_ctx *chartable_module_ctx)
{ {
const gchar *p, *end; const gchar *p, *end;
gdouble badness = 0.0; gdouble badness = 0.0;


static gdouble static gdouble
rspamd_chartable_process_word_ascii (struct rspamd_task *task, rspamd_chartable_process_word_ascii (struct rspamd_task *task,
rspamd_stat_token_t *w,
gboolean is_url)
rspamd_stat_token_t *w,
gboolean is_url,
struct chartable_ctx *chartable_module_ctx)
{ {
const guchar *p, *end; const guchar *p, *end;
gdouble badness = 0.0; gdouble badness = 0.0;


static void static void
rspamd_chartable_process_part (struct rspamd_task *task, rspamd_chartable_process_part (struct rspamd_task *task,
struct rspamd_mime_text_part *part)
struct rspamd_mime_text_part *part,
struct chartable_ctx *chartable_module_ctx)
{ {
rspamd_stat_token_t *w; rspamd_stat_token_t *w;
guint i, ncap = 0; guint i, ncap = 0;


if (IS_PART_UTF (part)) { if (IS_PART_UTF (part)) {
cur_score += rspamd_chartable_process_word_utf (task, w, FALSE, cur_score += rspamd_chartable_process_word_utf (task, w, FALSE,
&ncap);
&ncap, chartable_module_ctx);
} }
else { else {
cur_score += rspamd_chartable_process_word_ascii (task, w, FALSE);
cur_score += rspamd_chartable_process_word_ascii (task, w,
FALSE, chartable_module_ctx);
} }
} }
} }
{ {
guint i; guint i;
struct rspamd_mime_text_part *part; struct rspamd_mime_text_part *part;
struct chartable_ctx *chartable_module_ctx = chartable_get_context (task->cfg);


for (i = 0; i < task->text_parts->len; i ++) { for (i = 0; i < task->text_parts->len; i ++) {
part = g_ptr_array_index (task->text_parts, i); part = g_ptr_array_index (task->text_parts, i);
rspamd_chartable_process_part (task, part);
rspamd_chartable_process_part (task, part, chartable_module_ctx);
} }


if (task->subject != NULL) { if (task->subject != NULL) {
for (i = 0; i < words->len; i++) { for (i = 0; i < words->len; i++) {
w = &g_array_index (words, rspamd_stat_token_t, i); w = &g_array_index (words, rspamd_stat_token_t, i);
cur_score += rspamd_chartable_process_word_utf (task, w, FALSE, cur_score += rspamd_chartable_process_word_utf (task, w, FALSE,
NULL);
NULL, chartable_module_ctx);
} }


cur_score /= (gdouble)words->len; cur_score /= (gdouble)words->len;
gpointer k, v; gpointer k, v;
rspamd_stat_token_t w; rspamd_stat_token_t w;
gdouble cur_score = 0.0; gdouble cur_score = 0.0;
struct chartable_ctx *chartable_module_ctx = chartable_get_context (task->cfg);


g_hash_table_iter_init (&it, task->urls); g_hash_table_iter_init (&it, task->urls);


w.len = u->hostlen; w.len = u->hostlen;


if (g_utf8_validate (w.begin, w.len, NULL)) { if (g_utf8_validate (w.begin, w.len, NULL)) {
cur_score += rspamd_chartable_process_word_utf (task, &w, TRUE, NULL);
cur_score += rspamd_chartable_process_word_utf (task, &w,
TRUE, NULL, chartable_module_ctx);
} }
else { else {
cur_score += rspamd_chartable_process_word_ascii (task, &w, TRUE);
cur_score += rspamd_chartable_process_word_ascii (task, &w,
TRUE, chartable_module_ctx);
} }
} }
} }
w.len = u->hostlen; w.len = u->hostlen;


if (g_utf8_validate (w.begin, w.len, NULL)) { if (g_utf8_validate (w.begin, w.len, NULL)) {
cur_score += rspamd_chartable_process_word_utf (task, &w, TRUE, NULL);
cur_score += rspamd_chartable_process_word_utf (task, &w,
TRUE, NULL, chartable_module_ctx);
} }
else { else {
cur_score += rspamd_chartable_process_word_ascii (task, &w, TRUE);
cur_score += rspamd_chartable_process_word_ascii (task, &w,
TRUE, chartable_module_ctx);
} }
} }
} }

Loading…
Cancel
Save