diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-10-06 18:30:08 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-10-06 18:30:08 +0100 |
commit | 26339b503192a377e3ddce7366ddaab41fff3b86 (patch) | |
tree | 7614fc114bc55f6eb1e62d93fe8fdb379467fd1d /src/libstat/classifiers | |
parent | adedfbabb36a30576a090c2768cc6c6352327dd9 (diff) | |
download | rspamd-26339b503192a377e3ddce7366ddaab41fff3b86.tar.gz rspamd-26339b503192a377e3ddce7366ddaab41fff3b86.zip |
[Feature] Add configuration for lua classifiers
Diffstat (limited to 'src/libstat/classifiers')
-rw-r--r-- | src/libstat/classifiers/lua_classifier.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/libstat/classifiers/lua_classifier.c b/src/libstat/classifiers/lua_classifier.c index dea0f6a24..c1aee7261 100644 --- a/src/libstat/classifiers/lua_classifier.c +++ b/src/libstat/classifiers/lua_classifier.c @@ -17,12 +17,108 @@ #include "classifiers.h" #include "cfg_file.h" #include "stat_internal.h" +#include "lua/lua_common.h" + +struct rspamd_lua_classifier_ctx { + gchar *name; + gint classify_ref; + gint learn_ref; +}; + +static GHashTable *lua_classifiers = NULL; + +#define msg_err_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ + "luacl", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ + "luacl", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ + "luacl", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \ + "luacl", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) gboolean lua_classifier_init (rspamd_mempool_t *pool, struct rspamd_classifier *cl) { + struct rspamd_lua_classifier_ctx *ctx; + lua_State *L = cl->ctx->cfg->lua_state; + gint cb_classify = -1, cb_learn = -1; + + if (lua_classifiers == NULL) { + lua_classifiers = g_hash_table_new_full (rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); + } + + ctx = g_hash_table_lookup (lua_classifiers, cl->subrs->name); + + if (ctx != NULL) { + msg_err_pool ("duplicate lua classifier definition: %s", + cl->subrs->name); + + return FALSE; + } + + lua_getglobal (L, "rspamd_classifiers"); + if (lua_type (L, -1) != LUA_TTABLE) { + msg_err_pool ("cannot register classifier %s: no rspamd_classifier global", + cl->subrs->name); + lua_pop (L, 1); + + return FALSE; + } + + lua_pushstring (L, cl->subrs->name); + lua_gettable (L, -2); + + if (lua_type (L, -1) != LUA_TTABLE) { + msg_err_pool ("cannot register classifier %s: bad lua type: %s", + cl->subrs->name, lua_typename (L, lua_type (L, -1))); + lua_pop (L, 2); + + return FALSE; + } + + lua_pushstring (L, "classify"); + lua_gettable (L, -2); + + if (lua_type (L, -1) != LUA_TFUNCTION) { + msg_err_pool ("cannot register classifier %s: bad lua type for classify: %s", + cl->subrs->name, lua_typename (L, lua_type (L, -1))); + lua_pop (L, 3); + + return FALSE; + } + + cb_classify = luaL_ref (L, LUA_REGISTRYINDEX); + + lua_pushstring (L, "learn"); + lua_gettable (L, -2); + + if (lua_type (L, -1) != LUA_TFUNCTION) { + msg_err_pool ("cannot register classifier %s: bad lua type for learn: %s", + cl->subrs->name, lua_typename (L, lua_type (L, -1))); + lua_pop (L, 3); + + return FALSE; + } + + cb_learn = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pop (L, 2); /* Table + global */ + + ctx = g_malloc0 (sizeof (*ctx)); + ctx->name = g_strdup (cl->subrs->name); + ctx->classify_ref = cb_classify; + ctx->learn_ref = cb_learn; cl->cfg->flags |= RSPAMD_FLAG_CLASSIFIER_NO_BACKEND; + g_hash_table_insert (lua_classifiers, ctx->name, ctx); return TRUE; } |