static gint lua_expr_process_traced (lua_State *L) { LUA_TRACE_POINT; struct lua_expression *e = rspamd_lua_expression (L, 1); struct lua_atom_process_data pd; gdouble res; gint flags = 0, old_top; GPtrArray *trace; pd.L = L; pd.e = e; 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"); } pd.process_cb_pos = 2; pd.stack_item = 3; 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); pd.stack_item = 2; if (lua_isnumber (L, 3)) { flags = lua_tointeger (L, 3); } } res = rspamd_process_expression_track (e->expr, flags, &pd, &trace); lua_settop (L, old_top); lua_pushnumber (L, res); 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); lua_pushlstring (L, atom->str, atom->len); lua_rawseti (L, -2, i + 1); } g_ptr_array_free (trace, TRUE); return 2; }
static gint lua_expr_process (lua_State *L) { LUA_TRACE_POINT; 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); if (e->process_idx == -1) { 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) { pd.stack_item = 3; } else { pd.stack_item = -1; } 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); 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); } } res = rspamd_process_expression (e->expr, flags, &pd); lua_settop (L, old_top); lua_pushnumber (L, res); return 1; }
static gint lua_expr_process (lua_State *L) { struct lua_expression *e = rspamd_lua_expression (L, 1); gint res; gint flags = 0; if (lua_gettop (L) >= 3) { flags = lua_tonumber (L, 3); } res = rspamd_process_expression (e->expr, flags, GINT_TO_POINTER (2)); lua_pushnumber (L, res); return 1; }
static gint lua_expr_atoms (lua_State *L) { struct lua_expression *e = rspamd_lua_expression (L, 1); struct lua_expr_atoms_cbdata cbdata; if (e != NULL && e->expr != NULL) { lua_newtable (L); cbdata.L = L; cbdata.idx = 1; rspamd_expression_atom_foreach (e->expr, lua_exr_atom_cb, &cbdata); } else { lua_pushnil (L); } return 1; }
static gint lua_expr_to_string (lua_State *L) { struct lua_expression *e = rspamd_lua_expression (L, 1); GString *str; if (e != NULL && e->expr != NULL) { str = rspamd_expression_tostring (e->expr); if (str) { lua_pushlstring (L, str->str, str->len); g_string_free (str, TRUE); } else { lua_pushnil (L); } } else { lua_pushnil (L); } return 1; }