aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_expression.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/lua_expression.c')
-rw-r--r--src/lua/lua_expression.c314
1 files changed, 155 insertions, 159 deletions
diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c
index 8c44eba08..1ac6f864c 100644
--- a/src/lua/lua_expression.c
+++ b/src/lua/lua_expression.c
@@ -50,14 +50,14 @@ local expr,err = rspamd_expression.create('A & B | !C', {parse_func, process_fun
-- Expression is destroyed when the corresponding pool is destroyed
pool:destroy()
*/
-LUA_FUNCTION_DEF (expr, create);
+LUA_FUNCTION_DEF(expr, create);
/***
* @method rspamd_expression:to_string()
* Converts rspamd expression to string
* @return {string} string representation of rspamd expression
*/
-LUA_FUNCTION_DEF (expr, to_string);
+LUA_FUNCTION_DEF(expr, to_string);
/***
* @method rspamd_expression:process([callback, ]input[, flags])
@@ -66,7 +66,7 @@ LUA_FUNCTION_DEF (expr, to_string);
* @param {any} input input data for processing callbacks
* @return {number} result of the expression evaluation
*/
-LUA_FUNCTION_DEF (expr, process);
+LUA_FUNCTION_DEF(expr, process);
/***
* @method rspamd_expression:process_traced([callback, ]input[, flags])
@@ -75,39 +75,36 @@ LUA_FUNCTION_DEF (expr, process);
* @param {any} input input data for processing callbacks
* @return {number, table of matched atoms} result of the expression evaluation
*/
-LUA_FUNCTION_DEF (expr, process_traced);
+LUA_FUNCTION_DEF(expr, process_traced);
/***
* @method rspamd_expression:atoms()
* Extract all atoms from the expression as table of strings
* @return {table/strings} list of all atoms in the expression
*/
-LUA_FUNCTION_DEF (expr, atoms);
+LUA_FUNCTION_DEF(expr, atoms);
static const struct luaL_reg exprlib_m[] = {
- LUA_INTERFACE_DEF (expr, to_string),
- LUA_INTERFACE_DEF (expr, atoms),
- LUA_INTERFACE_DEF (expr, process),
- LUA_INTERFACE_DEF (expr, process_traced),
+ LUA_INTERFACE_DEF(expr, to_string),
+ LUA_INTERFACE_DEF(expr, atoms),
+ LUA_INTERFACE_DEF(expr, process),
+ LUA_INTERFACE_DEF(expr, process_traced),
{"__tostring", lua_expr_to_string},
- {NULL, NULL}
-};
+ {NULL, NULL}};
static const struct luaL_reg exprlib_f[] = {
- LUA_INTERFACE_DEF (expr, create),
- {NULL, NULL}
-};
+ LUA_INTERFACE_DEF(expr, create),
+ {NULL, NULL}};
-static rspamd_expression_atom_t * lua_atom_parse (const gchar *line, gsize len,
- rspamd_mempool_t *pool, gpointer ud, GError **err);
-static gdouble lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom);
+static rspamd_expression_atom_t *lua_atom_parse(const gchar *line, gsize len,
+ rspamd_mempool_t *pool, gpointer ud, GError **err);
+static gdouble lua_atom_process(gpointer runtime_ud, rspamd_expression_atom_t *atom);
static const struct rspamd_atom_subr lua_atom_subr = {
.parse = lua_atom_parse,
.process = lua_atom_process,
.priority = NULL,
- .destroy = NULL
-};
+ .destroy = NULL};
struct lua_expression {
struct rspamd_expression *expr;
@@ -118,64 +115,64 @@ struct lua_expression {
};
static GQuark
-lua_expr_quark (void)
+lua_expr_quark(void)
{
- return g_quark_from_static_string ("lua-expression");
+ return g_quark_from_static_string("lua-expression");
}
struct lua_expression *
-rspamd_lua_expression (lua_State * L, gint pos)
+rspamd_lua_expression(lua_State *L, gint pos)
{
- void *ud = rspamd_lua_check_udata (L, pos, "rspamd{expr}");
- luaL_argcheck (L, ud != NULL, pos, "'expr' expected");
- return ud ? *((struct lua_expression **)ud) : NULL;
+ void *ud = rspamd_lua_check_udata(L, pos, "rspamd{expr}");
+ luaL_argcheck(L, ud != NULL, pos, "'expr' expected");
+ return ud ? *((struct lua_expression **) ud) : NULL;
}
static void
-lua_expr_dtor (gpointer p)
+lua_expr_dtor(gpointer p)
{
- struct lua_expression *e = (struct lua_expression *)p;
+ struct lua_expression *e = (struct lua_expression *) p;
if (e->parse_idx != -1) {
- luaL_unref (e->L, LUA_REGISTRYINDEX, e->parse_idx);
+ luaL_unref(e->L, LUA_REGISTRYINDEX, e->parse_idx);
}
if (e->process_idx != -1) {
- luaL_unref (e->L, LUA_REGISTRYINDEX, e->process_idx);
+ luaL_unref(e->L, LUA_REGISTRYINDEX, e->process_idx);
}
}
static rspamd_expression_atom_t *
-lua_atom_parse (const gchar *line, gsize len,
- rspamd_mempool_t *pool, gpointer ud, GError **err)
+lua_atom_parse(const gchar *line, gsize len,
+ rspamd_mempool_t *pool, gpointer ud, GError **err)
{
- struct lua_expression *e = (struct lua_expression *)ud;
+ struct lua_expression *e = (struct lua_expression *) ud;
rspamd_expression_atom_t *atom;
gsize rlen;
const gchar *tok;
- lua_rawgeti (e->L, LUA_REGISTRYINDEX, e->parse_idx);
- lua_pushlstring (e->L, line, len);
+ lua_rawgeti(e->L, LUA_REGISTRYINDEX, e->parse_idx);
+ lua_pushlstring(e->L, line, len);
- if (lua_pcall (e->L, 1, 1, 0) != 0) {
- msg_info ("callback call failed: %s", lua_tostring (e->L, -1));
- lua_pop (e->L, 1);
+ if (lua_pcall(e->L, 1, 1, 0) != 0) {
+ msg_info("callback call failed: %s", lua_tostring(e->L, -1));
+ lua_pop(e->L, 1);
return NULL;
}
- if (lua_type (e->L, -1) != LUA_TSTRING) {
- g_set_error (err, lua_expr_quark(), 500, "cannot parse lua atom");
- lua_pop (e->L, 1);
+ if (lua_type(e->L, -1) != LUA_TSTRING) {
+ g_set_error(err, lua_expr_quark(), 500, "cannot parse lua atom");
+ lua_pop(e->L, 1);
return NULL;
}
- tok = lua_tolstring (e->L, -1, &rlen);
- atom = rspamd_mempool_alloc0 (e->pool, sizeof (*atom));
- atom->str = rspamd_mempool_strdup (e->pool, tok);
+ tok = lua_tolstring(e->L, -1, &rlen);
+ atom = rspamd_mempool_alloc0(e->pool, sizeof(*atom));
+ atom->str = rspamd_mempool_strdup(e->pool, tok);
atom->len = rlen;
atom->data = ud;
- lua_pop (e->L, 1);
+ lua_pop(e->L, 1);
return atom;
}
@@ -188,9 +185,9 @@ struct lua_atom_process_data {
};
static gdouble
-lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom)
+lua_atom_process(gpointer runtime_ud, rspamd_expression_atom_t *atom)
{
- struct lua_atom_process_data *pd = (struct lua_atom_process_data *)runtime_ud;
+ struct lua_atom_process_data *pd = (struct lua_atom_process_data *) runtime_ud;
gdouble ret = 0;
guint nargs;
gint err_idx;
@@ -202,91 +199,91 @@ lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom)
nargs = 1;
}
- lua_pushcfunction (pd->L, &rspamd_lua_traceback);
- err_idx = lua_gettop (pd->L);
+ lua_pushcfunction(pd->L, &rspamd_lua_traceback);
+ err_idx = lua_gettop(pd->L);
/* Function position */
- lua_pushvalue (pd->L, pd->process_cb_pos);
+ lua_pushvalue(pd->L, pd->process_cb_pos);
/* Atom name */
- lua_pushlstring (pd->L, atom->str, atom->len);
+ lua_pushlstring(pd->L, atom->str, atom->len);
/* If we have data passed */
if (pd->stack_item != -1) {
- lua_pushvalue (pd->L, pd->stack_item);
+ lua_pushvalue(pd->L, pd->stack_item);
}
- if (lua_pcall (pd->L, nargs, 1, err_idx) != 0) {
- msg_info ("expression process callback failed: %s", lua_tostring (pd->L, -1));
+ if (lua_pcall(pd->L, nargs, 1, err_idx) != 0) {
+ msg_info("expression process callback failed: %s", lua_tostring(pd->L, -1));
}
else {
- ret = lua_tonumber (pd->L, -1);
+ ret = lua_tonumber(pd->L, -1);
}
- lua_settop (pd->L, err_idx - 1);
+ lua_settop(pd->L, err_idx - 1);
return ret;
}
static gint
-lua_expr_process (lua_State *L)
+lua_expr_process(lua_State *L)
{
LUA_TRACE_POINT;
- struct lua_expression *e = rspamd_lua_expression (L, 1);
+ struct lua_expression *e = rspamd_lua_expression(L, 1);
struct lua_atom_process_data pd;
gdouble res;
gint flags = 0, old_top;
pd.L = L;
pd.e = e;
- old_top = lua_gettop (L);
+ old_top = lua_gettop(L);
if (e->process_idx == -1) {
- if (!lua_isfunction (L, 2)) {
- return luaL_error (L, "expression process is called with no callback function");
+ if (!lua_isfunction(L, 2)) {
+ return luaL_error(L, "expression process is called with no callback function");
}
pd.process_cb_pos = 2;
- if (lua_type (L, 3) != LUA_TNONE && lua_type (L, 3) != LUA_TNIL) {
+ if (lua_type(L, 3) != LUA_TNONE && lua_type(L, 3) != LUA_TNIL) {
pd.stack_item = 3;
}
else {
pd.stack_item = -1;
}
- if (lua_isnumber (L, 4)) {
- flags = lua_tointeger (L, 4);
+ if (lua_isnumber(L, 4)) {
+ flags = lua_tointeger(L, 4);
}
}
else {
- lua_rawgeti (L, LUA_REGISTRYINDEX, e->process_idx);
- pd.process_cb_pos = lua_gettop (L);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, e->process_idx);
+ pd.process_cb_pos = lua_gettop(L);
- if (lua_type (L, 2) != LUA_TNONE && lua_type (L, 2) != LUA_TNIL) {
+ if (lua_type(L, 2) != LUA_TNONE && lua_type(L, 2) != LUA_TNIL) {
pd.stack_item = 2;
}
else {
pd.stack_item = -1;
}
- if (lua_isnumber (L, 3)) {
- flags = lua_tointeger (L, 3);
+ if (lua_isnumber(L, 3)) {
+ flags = lua_tointeger(L, 3);
}
}
- res = rspamd_process_expression (e->expr, flags, &pd);
+ res = rspamd_process_expression(e->expr, flags, &pd);
- lua_settop (L, old_top);
- lua_pushnumber (L, res);
+ lua_settop(L, old_top);
+ lua_pushnumber(L, res);
return 1;
}
static gint
-lua_expr_process_traced (lua_State *L)
+lua_expr_process_traced(lua_State *L)
{
LUA_TRACE_POINT;
- struct lua_expression *e = rspamd_lua_expression (L, 1);
+ struct lua_expression *e = rspamd_lua_expression(L, 1);
struct lua_atom_process_data pd;
gdouble res;
gint flags = 0, old_top;
@@ -294,51 +291,51 @@ lua_expr_process_traced (lua_State *L)
pd.L = L;
pd.e = e;
- old_top = lua_gettop (L);
+ old_top = lua_gettop(L);
if (e->process_idx == -1) {
- if (!lua_isfunction (L, 2)) {
- return luaL_error (L, "expression process is called with no callback function");
+ if (!lua_isfunction(L, 2)) {
+ return luaL_error(L, "expression process is called with no callback function");
}
pd.process_cb_pos = 2;
pd.stack_item = 3;
- if (lua_isnumber (L, 4)) {
- flags = lua_tointeger (L, 4);
+ if (lua_isnumber(L, 4)) {
+ flags = lua_tointeger(L, 4);
}
}
else {
- lua_rawgeti (L, LUA_REGISTRYINDEX, e->process_idx);
- pd.process_cb_pos = lua_gettop (L);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, e->process_idx);
+ pd.process_cb_pos = lua_gettop(L);
pd.stack_item = 2;
- if (lua_isnumber (L, 3)) {
- flags = lua_tointeger (L, 3);
+ if (lua_isnumber(L, 3)) {
+ flags = lua_tointeger(L, 3);
}
}
- res = rspamd_process_expression_track (e->expr, flags, &pd, &trace);
+ res = rspamd_process_expression_track(e->expr, flags, &pd, &trace);
- lua_settop (L, old_top);
- lua_pushnumber (L, res);
+ lua_settop(L, old_top);
+ lua_pushnumber(L, res);
- lua_createtable (L, trace->len, 0);
+ lua_createtable(L, trace->len, 0);
- for (guint i = 0; i < trace->len; i ++) {
- struct rspamd_expression_atom_s *atom = g_ptr_array_index (trace, i);
+ for (guint i = 0; i < trace->len; i++) {
+ struct rspamd_expression_atom_s *atom = g_ptr_array_index(trace, i);
- lua_pushlstring (L, atom->str, atom->len);
- lua_rawseti (L, -2, i + 1);
+ lua_pushlstring(L, atom->str, atom->len);
+ lua_rawseti(L, -2, i + 1);
}
- g_ptr_array_free (trace, TRUE);
+ g_ptr_array_free(trace, TRUE);
return 2;
}
static gint
-lua_expr_create (lua_State *L)
+lua_expr_create(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_expression *e, **pe;
@@ -349,44 +346,44 @@ lua_expr_create (lua_State *L)
rspamd_mempool_t *pool;
/* Check sanity of the arguments */
- if (lua_type (L, 1) != LUA_TSTRING ||
- (lua_type (L, 2) != LUA_TTABLE && lua_type (L, 2) != LUA_TFUNCTION) ||
- rspamd_lua_check_mempool (L, 3) == NULL) {
- lua_pushnil (L);
- lua_pushstring (L, "bad arguments");
+ if (lua_type(L, 1) != LUA_TSTRING ||
+ (lua_type(L, 2) != LUA_TTABLE && lua_type(L, 2) != LUA_TFUNCTION) ||
+ rspamd_lua_check_mempool(L, 3) == NULL) {
+ lua_pushnil(L);
+ lua_pushstring(L, "bad arguments");
}
else {
- line = lua_tolstring (L, 1, &len);
- pool = rspamd_lua_check_mempool (L, 3);
+ line = lua_tolstring(L, 1, &len);
+ pool = rspamd_lua_check_mempool(L, 3);
- e = rspamd_mempool_alloc (pool, sizeof (*e));
+ e = rspamd_mempool_alloc(pool, sizeof(*e));
e->L = L;
e->pool = pool;
/* Check callbacks */
- if (lua_istable (L, 2)) {
- lua_pushvalue (L, 2);
- lua_pushnumber (L, 1);
- lua_gettable (L, -2);
+ if (lua_istable(L, 2)) {
+ lua_pushvalue(L, 2);
+ lua_pushnumber(L, 1);
+ lua_gettable(L, -2);
- if (lua_type (L, -1) != LUA_TFUNCTION) {
- lua_pop (L, 1);
- lua_pushnil (L);
- lua_pushstring (L, "bad parse callback");
+ if (lua_type(L, -1) != LUA_TFUNCTION) {
+ lua_pop(L, 1);
+ lua_pushnil(L);
+ lua_pushstring(L, "bad parse callback");
return 2;
}
- lua_pop (L, 1);
+ lua_pop(L, 1);
- lua_pushnumber (L, 2);
- lua_gettable (L, -2);
+ lua_pushnumber(L, 2);
+ lua_gettable(L, -2);
- if (lua_type (L, -1) != LUA_TFUNCTION) {
- if (lua_type (L, -1) != LUA_TNIL && lua_type (L, -1) != LUA_TNONE) {
- lua_pop (L, 1);
- lua_pushnil (L);
- lua_pushstring (L, "bad process callback");
+ if (lua_type(L, -1) != LUA_TFUNCTION) {
+ if (lua_type(L, -1) != LUA_TNIL && lua_type(L, -1) != LUA_TNONE) {
+ lua_pop(L, 1);
+ lua_pushnil(L);
+ lua_pushstring(L, "bad process callback");
return 2;
}
@@ -395,70 +392,70 @@ lua_expr_create (lua_State *L)
}
}
- lua_pop (L, 1);
+ lua_pop(L, 1);
/* Table is still on the top of stack */
- lua_pushnumber (L, 1);
- lua_gettable (L, -2);
- e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+ lua_pushnumber(L, 1);
+ lua_gettable(L, -2);
+ e->parse_idx = luaL_ref(L, LUA_REGISTRYINDEX);
if (!no_process) {
- lua_pushnumber (L, 2);
- lua_gettable (L, -2);
- e->process_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+ lua_pushnumber(L, 2);
+ lua_gettable(L, -2);
+ e->process_idx = luaL_ref(L, LUA_REGISTRYINDEX);
}
else {
e->process_idx = -1;
}
- lua_pop (L, 1); /* Table */
+ lua_pop(L, 1); /* Table */
}
else {
/* Process function is just a function, not a table */
- lua_pushvalue (L, 2);
- e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+ lua_pushvalue(L, 2);
+ e->parse_idx = luaL_ref(L, LUA_REGISTRYINDEX);
e->process_idx = -1;
}
- if (!rspamd_parse_expression (line, len, &lua_atom_subr, e, pool, &err,
- &e->expr)) {
- lua_pushnil (L);
- lua_pushstring (L, err->message);
- g_error_free (err);
- lua_expr_dtor (e);
+ if (!rspamd_parse_expression(line, len, &lua_atom_subr, e, pool, &err,
+ &e->expr)) {
+ lua_pushnil(L);
+ lua_pushstring(L, err->message);
+ g_error_free(err);
+ lua_expr_dtor(e);
return 2;
}
- rspamd_mempool_add_destructor (pool, lua_expr_dtor, e);
- pe = lua_newuserdata (L, sizeof (struct lua_expression *));
- rspamd_lua_setclass (L, "rspamd{expr}", -1);
+ rspamd_mempool_add_destructor(pool, lua_expr_dtor, e);
+ pe = lua_newuserdata(L, sizeof(struct lua_expression *));
+ rspamd_lua_setclass(L, "rspamd{expr}", -1);
*pe = e;
- lua_pushnil (L);
+ lua_pushnil(L);
}
return 2;
}
static gint
-lua_expr_to_string (lua_State *L)
+lua_expr_to_string(lua_State *L)
{
LUA_TRACE_POINT;
- struct lua_expression *e = rspamd_lua_expression (L, 1);
+ struct lua_expression *e = rspamd_lua_expression(L, 1);
GString *str;
if (e != NULL && e->expr != NULL) {
- str = rspamd_expression_tostring (e->expr);
+ str = rspamd_expression_tostring(e->expr);
if (str) {
- lua_pushlstring (L, str->str, str->len);
- g_string_free (str, TRUE);
+ lua_pushlstring(L, str->str, str->len);
+ g_string_free(str, TRUE);
}
else {
- lua_pushnil (L);
+ lua_pushnil(L);
}
}
else {
- lua_pushnil (L);
+ lua_pushnil(L);
}
return 1;
@@ -470,47 +467,46 @@ struct lua_expr_atoms_cbdata {
};
static void
-lua_exr_atom_cb (const rspamd_ftok_t *tok, gpointer ud)
+lua_exr_atom_cb(const rspamd_ftok_t *tok, gpointer ud)
{
struct lua_expr_atoms_cbdata *cbdata = ud;
- lua_pushlstring (cbdata->L, tok->begin, tok->len);
- lua_rawseti (cbdata->L, -2, cbdata->idx ++);
+ lua_pushlstring(cbdata->L, tok->begin, tok->len);
+ lua_rawseti(cbdata->L, -2, cbdata->idx++);
}
static gint
-lua_expr_atoms (lua_State *L)
+lua_expr_atoms(lua_State *L)
{
LUA_TRACE_POINT;
- struct lua_expression *e = rspamd_lua_expression (L, 1);
+ struct lua_expression *e = rspamd_lua_expression(L, 1);
struct lua_expr_atoms_cbdata cbdata;
if (e != NULL && e->expr != NULL) {
- lua_newtable (L);
+ lua_newtable(L);
cbdata.L = L;
cbdata.idx = 1;
- rspamd_expression_atom_foreach (e->expr, lua_exr_atom_cb, &cbdata);
+ rspamd_expression_atom_foreach(e->expr, lua_exr_atom_cb, &cbdata);
}
else {
- lua_pushnil (L);
+ lua_pushnil(L);
}
return 1;
}
static gint
-lua_load_expression (lua_State * L)
+lua_load_expression(lua_State *L)
{
- lua_newtable (L);
- luaL_register (L, NULL, exprlib_f);
+ lua_newtable(L);
+ luaL_register(L, NULL, exprlib_f);
return 1;
}
-void
-luaopen_expression (lua_State * L)
+void luaopen_expression(lua_State *L)
{
- rspamd_lua_new_class (L, "rspamd{expr}", exprlib_m);
- lua_pop (L, 1);
- rspamd_lua_add_preload (L, "rspamd_expression", lua_load_expression);
+ rspamd_lua_new_class(L, "rspamd{expr}", exprlib_m);
+ lua_pop(L, 1);
+ rspamd_lua_add_preload(L, "rspamd_expression", lua_load_expression);
}