]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Pass shingles to Lua scripts
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 6 Jan 2025 12:57:07 +0000 (12:57 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 6 Jan 2025 12:57:07 +0000 (12:57 +0000)
src/fuzzy_storage.c
src/lua/lua_common.h
src/lua/lua_shingles.cxx

index 00f226532fd15bb27b660e770003333bc874d739..c7a1af470b97b68b5dbde95dac4298318a3d90e0 100644 (file)
@@ -1288,7 +1288,7 @@ rspamd_fuzzy_check_callback(struct rspamd_fuzzy_reply *result, void *ud)
        if (session->ctx->lua_post_handler_cbref != -1) {
                /* Start lua post handler */
                lua_State *L = session->ctx->cfg->lua_state;
-               int err_idx, ret;
+               int err_idx, ret, nargs = 9;
 
                lua_pushcfunction(L, &rspamd_lua_traceback);
                err_idx = lua_gettop(L);
@@ -1319,8 +1319,15 @@ rspamd_fuzzy_check_callback(struct rspamd_fuzzy_reply *result, void *ud)
                lua_pushinteger(L, result->ts);
                /* TODO: add additional data maybe (encryption, pubkey, etc) */
                rspamd_fuzzy_extensions_tolua(L, session);
+               /* We push shingles merely for commands that modify content to avoid extra work */
+               if (is_shingle && cmd->cmd != FUZZY_CHECK) {
+                       struct rspamd_shingle **pshingle = lua_newuserdata(L, sizeof(*pshingle));
+                       rspamd_lua_setclass(L, RSPAMD_LUA_SHINGLE_CLASS, -1);
+                       *pshingle = &session->cmd.sgl;
+                       nargs++;
+               }
 
-               if ((ret = lua_pcall(L, 9, LUA_MULTRET, err_idx)) != 0) {
+               if ((ret = lua_pcall(L, nargs, LUA_MULTRET, err_idx)) != 0) {
                        msg_err("call to lua_post_handler lua "
                                        "script failed (%d): %s",
                                        ret, lua_tostring(L, -1));
@@ -1478,7 +1485,7 @@ rspamd_fuzzy_process_command(struct fuzzy_session *session)
        if (session->ctx->lua_pre_handler_cbref != -1) {
                /* Start lua pre handler */
                lua_State *L = session->ctx->cfg->lua_state;
-               int err_idx, ret;
+               int err_idx, ret, nargs = 5;
 
                lua_pushcfunction(L, &rspamd_lua_traceback);
                err_idx = lua_gettop(L);
@@ -1497,7 +1504,15 @@ rspamd_fuzzy_process_command(struct fuzzy_session *session)
                /* TODO: add additional data maybe (encryption, pubkey, etc) */
                rspamd_fuzzy_extensions_tolua(L, session);
 
-               if ((ret = lua_pcall(L, 5, LUA_MULTRET, err_idx)) != 0) {
+               /* We push shingles merely for commands that modify content to avoid extra work */
+               if (is_shingle && cmd->cmd != FUZZY_CHECK) {
+                       struct rspamd_shingle **pshingle = lua_newuserdata(L, sizeof(*pshingle));
+                       rspamd_lua_setclass(L, RSPAMD_LUA_SHINGLE_CLASS, -1);
+                       *pshingle = &session->cmd.sgl;
+                       nargs++;
+               }
+
+               if ((ret = lua_pcall(L, nargs, LUA_MULTRET, err_idx)) != 0) {
                        msg_err("call to lua_pre_handler lua "
                                        "script failed (%d): %s",
                                        ret, lua_tostring(L, -1));
index accc6be86a0e4c23ce5b1c53cb48837dc7d76d2b..23bccbd3047af25d5884cdb9b92aaae347cb2e59 100644 (file)
@@ -708,6 +708,8 @@ int rspamd_lua_geti(lua_State *L, int index, int i);
 #define RSPAMD_PREFIX_INDEX "PREFIX"
 #define RSPAMD_VERSION_INDEX "VERSION"
 
+#define RSPAMD_LUA_SHINGLE_CLASS "rspamd{shingle}"
+
 #ifdef WITH_LUA_TRACE
 extern ucl_object_t *lua_traces;
 #define LUA_TRACE_POINT                                                            \
index 355a9e7f2f2086c313200a78aff6da823da97c62..b19a00c08bba66c5f849ffebf2eadc419e3e84b8 100644 (file)
@@ -18,8 +18,6 @@
 #include "shingles.h"
 #include "fmt/format.h"
 
-#define RSPAMD_SHINGLE_CLASS "rspamd{shingle}"
-
 /***
  * @module rspamd_shingle
  * This module provides methods to work with text shingles
@@ -58,9 +56,9 @@ static const struct luaL_reg shinglelib_m[] = {
 static struct rspamd_shingle *
 lua_check_shingle(lua_State *L, int pos)
 {
-       void *ud = rspamd_lua_check_udata(L, pos, RSPAMD_SHINGLE_CLASS);
+       void *ud = rspamd_lua_check_udata(L, pos, RSPAMD_LUA_SHINGLE_CLASS);
        luaL_argcheck(L, ud != nullptr, pos, "'shingle' expected");
-       return static_cast<struct rspamd_shingle *>(ud);
+       return *static_cast<struct rspamd_shingle **>(ud);
 }
 
 static int
@@ -117,6 +115,6 @@ lua_shingle_get_string(lua_State *L)
 
 void luaopen_shingle(lua_State *L)
 {
-       rspamd_lua_new_class(L, RSPAMD_SHINGLE_CLASS, shinglelib_m);
+       rspamd_lua_new_class(L, RSPAMD_LUA_SHINGLE_CLASS, shinglelib_m);
        lua_pop(L, 1);
 }