aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstat/classifiers/lua_classifier.c
blob: c1aee7261528368485e3cffa4e5412d4cfab2996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*-
 * Copyright 2016 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#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;
}
gboolean
lua_classifier_classify (struct rspamd_classifier *ctx,
		GPtrArray *tokens,
		struct rspamd_task *task)
{
	return TRUE;
}

gboolean
lua_classifier_learn_spam (struct rspamd_classifier *ctx,
		GPtrArray *tokens,
		struct rspamd_task *task,
		gboolean is_spam,
		gboolean unlearn,
		GError **err)
{
	return TRUE;
}