aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-10-06 18:50:43 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-10-06 18:50:43 +0100
commita36e4736d858cc5ee138fc3abca1bcaf5f3cbaa9 (patch)
tree77ff996e3d7eff6ec9d8ea9b6fdb36f375b62dec /src
parent26339b503192a377e3ddce7366ddaab41fff3b86 (diff)
downloadrspamd-a36e4736d858cc5ee138fc3abca1bcaf5f3cbaa9.tar.gz
rspamd-a36e4736d858cc5ee138fc3abca1bcaf5f3cbaa9.zip
[Feature] Implement classifying for lua classifiers
Diffstat (limited to 'src')
-rw-r--r--src/libstat/classifiers/lua_classifier.c49
1 files changed, 47 insertions, 2 deletions
diff --git a/src/libstat/classifiers/lua_classifier.c b/src/libstat/classifiers/lua_classifier.c
index c1aee7261..cd9fc3bd1 100644
--- a/src/libstat/classifiers/lua_classifier.c
+++ b/src/libstat/classifiers/lua_classifier.c
@@ -123,15 +123,60 @@ lua_classifier_init (rspamd_mempool_t *pool,
return TRUE;
}
gboolean
-lua_classifier_classify (struct rspamd_classifier *ctx,
+lua_classifier_classify (struct rspamd_classifier *cl,
GPtrArray *tokens,
struct rspamd_task *task)
{
+ struct rspamd_lua_classifier_ctx *ctx;
+ struct rspamd_task **ptask;
+ struct rspamd_classifier_config **pcfg;
+ lua_State *L;
+ rspamd_token_t *tok;
+ guint i;
+ guint64 v;
+
+ ctx = g_hash_table_lookup (lua_classifiers, cl->subrs->name);
+ g_assert (ctx != NULL);
+ L = task->cfg->lua_state;
+
+ lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->classify_ref);
+ ptask = lua_newuserdata (L, sizeof (*ptask));
+ *ptask = task;
+ rspamd_lua_setclass (L, "rspamd{task}", -1);
+ pcfg = lua_newuserdata (L, sizeof (*pcfg));
+ *pcfg = cl->cfg;
+ rspamd_lua_setclass (L, "rspamd{classifier}", -1);
+
+ lua_createtable (L, tokens->len, 0);
+
+ for (i = 0; i < tokens->len; i ++) {
+ tok = g_ptr_array_index (tokens, i);
+ v = 0;
+ memcpy (&v, tok->data, MIN (sizeof (v), tok->datalen));
+ lua_createtable (L, 3, 0);
+ /* High word, low word, order */
+ lua_pushnumber (L, (guint32)(v >> 32));
+ lua_rawseti (L, -2, 1);
+ lua_pushnumber (L, (guint32)(v));
+ lua_rawseti (L, -2, 2);
+ lua_pushnumber (L, tok->window_idx);
+ lua_rawseti (L, -2, 3);
+ lua_rawseti (L, -2, i + 1);
+ }
+
+ if (lua_pcall (L, 3, 0, 0) != 0) {
+ msg_err_luacl ("error running classify function for %s: %s", ctx->name,
+ lua_tostring (L, -1));
+ lua_pop (L, 1);
+
+ return FALSE;
+ }
+
return TRUE;
}
gboolean
-lua_classifier_learn_spam (struct rspamd_classifier *ctx,
+lua_classifier_learn_spam (struct rspamd_classifier *cl,
GPtrArray *tokens,
struct rspamd_task *task,
gboolean is_spam,