diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2025-01-08 15:14:37 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2025-01-08 15:14:37 +0000 |
commit | 694d15a9af0baa186c6185c1c9da01f08f79dbbc (patch) | |
tree | e943c05544397ed1ed20dde71ad3ed730478fb9e | |
parent | 5c5598eb81a644d6866a4ea4c3ef4a0c2b593ae6 (diff) | |
download | rspamd-694d15a9af0baa186c6185c1c9da01f08f79dbbc.tar.gz rspamd-694d15a9af0baa186c6185c1c9da01f08f79dbbc.zip |
[Minor] Slightly rework lua shingles to simplify storage
-rw-r--r-- | src/fuzzy_storage.c | 8 | ||||
-rw-r--r-- | src/lua/lua_classnames.c | 4 | ||||
-rw-r--r-- | src/lua/lua_classnames.h | 5 | ||||
-rw-r--r-- | src/lua/lua_common.h | 8 | ||||
-rw-r--r-- | src/lua/lua_shingles.cxx | 19 |
5 files changed, 30 insertions, 14 deletions
diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index c7a1af470..a916af3c9 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -1321,9 +1321,7 @@ rspamd_fuzzy_check_callback(struct rspamd_fuzzy_reply *result, void *ud) 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; + lua_newshingle(L, &session->cmd.sgl); nargs++; } @@ -1506,9 +1504,7 @@ rspamd_fuzzy_process_command(struct fuzzy_session *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; + lua_newshingle(L, &session->cmd.sgl); nargs++; } diff --git a/src/lua/lua_classnames.c b/src/lua/lua_classnames.c index 7ce2f8abc..2b5a90fe0 100644 --- a/src/lua/lua_classnames.c +++ b/src/lua/lua_classnames.c @@ -1,5 +1,5 @@ /* - * Copyright 2024 Vsevolod Stakhov + * Copyright 2025 Vsevolod Stakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,6 +67,7 @@ const char *rspamd_url_classname = "rspamd{url}"; const char *rspamd_worker_classname = "rspamd{worker}"; const char *rspamd_zstd_compress_classname = "rspamd{zstd_compress}"; const char *rspamd_zstd_decompress_classname = "rspamd{zstd_decompress}"; +const char *rspamd_shingle_classname = "rspamd{shingle}"; KHASH_INIT(rspamd_lua_static_classes, const char *, const char *, 1, rspamd_str_hash, rspamd_str_equal); @@ -133,6 +134,7 @@ RSPAMD_CONSTRUCTOR(rspamd_lua_init_classnames) CLASS_PUT_STR(worker); CLASS_PUT_STR(zstd_compress); CLASS_PUT_STR(zstd_decompress); + CLASS_PUT_STR(shingle); /* Check consistency */ g_assert(kh_size(lua_static_classes) == RSPAMD_MAX_LUA_CLASSES); diff --git a/src/lua/lua_classnames.h b/src/lua/lua_classnames.h index 53db5f8c2..6e3a6441f 100644 --- a/src/lua/lua_classnames.h +++ b/src/lua/lua_classnames.h @@ -1,5 +1,5 @@ /* - * Copyright 2024 Vsevolod Stakhov + * Copyright 2025 Vsevolod Stakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,9 +70,10 @@ extern const char *rspamd_url_classname; extern const char *rspamd_worker_classname; extern const char *rspamd_zstd_compress_classname; extern const char *rspamd_zstd_decompress_classname; +extern const char *rspamd_shingle_classname; /* Keep it consistent when adding new classes */ -#define RSPAMD_MAX_LUA_CLASSES 48 +#define RSPAMD_MAX_LUA_CLASSES 49 /* * Return a static class name for a given name (only for known classes) or NULL diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 23bccbd30..f5a4967ba 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -456,6 +456,12 @@ struct rspamd_dns_resolver *lua_check_dns_resolver(lua_State *L, int pos); struct rspamd_lua_url *lua_check_url(lua_State *L, int pos); +/** + * Creates a new shingle object from the existing shingle + */ +struct rspamd_shingle; +void lua_newshingle(lua_State *L, const struct rspamd_shingle *sh); + enum rspamd_lua_parse_arguments_flags { RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT = 0, RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING, @@ -708,8 +714,6 @@ 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 \ diff --git a/src/lua/lua_shingles.cxx b/src/lua/lua_shingles.cxx index b19a00c08..8e14d8ba8 100644 --- a/src/lua/lua_shingles.cxx +++ b/src/lua/lua_shingles.cxx @@ -15,6 +15,7 @@ */ #include "lua_common.h" +#include "lua_classnames.h" #include "shingles.h" #include "fmt/format.h" @@ -56,9 +57,21 @@ 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_LUA_SHINGLE_CLASS); + void *ud = rspamd_lua_check_udata(L, pos, rspamd_shingle_classname); luaL_argcheck(L, ud != nullptr, pos, "'shingle' expected"); - return *static_cast<struct rspamd_shingle **>(ud); + return static_cast<struct rspamd_shingle *>(ud); +} + +void lua_newshingle(lua_State *L, const struct rspamd_shingle *sh) +{ + auto *nsh = static_cast<struct rspamd_shingle *>( + lua_newuserdata(L, sizeof(struct rspamd_shingle))); + + if (sh != nullptr) { + memcpy(nsh, sh, sizeof(struct rspamd_shingle)); + } + + rspamd_lua_setclass(L, rspamd_shingle_classname, -1); } static int @@ -115,6 +128,6 @@ lua_shingle_get_string(lua_State *L) void luaopen_shingle(lua_State *L) { - rspamd_lua_new_class(L, RSPAMD_LUA_SHINGLE_CLASS, shinglelib_m); + rspamd_lua_new_class(L, rspamd_shingle_classname, shinglelib_m); lua_pop(L, 1); } |