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++;
}
/* 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++;
}
/*
- * 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.
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);
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);
/*
- * 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.
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
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,
#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 \
*/
#include "lua_common.h"
+#include "lua_classnames.h"
#include "shingles.h"
#include "fmt/format.h"
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
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);
}