commit b0cf71d: [Minor] Allow lua maps to process opaque texts
Vsevolod Stakhov
vsevolod at highsecure.ru
Mon Feb 24 09:35:12 UTC 2020
Author: Vsevolod Stakhov
Date: 2020-02-22 09:59:43 +0000
URL: https://github.com/rspamd/rspamd/commit/b0cf71db435fa0e010b1cc372f6b7ef834d7bb22
[Minor] Allow lua maps to process opaque texts
---
src/lua/lua_map.c | 21 ++++++++++++++++++---
src/plugins/lua/settings.lua | 12 +++++++++---
2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c
index 087478ccc..b279bacbd 100644
--- a/src/lua/lua_map.c
+++ b/src/lua/lua_map.c
@@ -129,6 +129,7 @@ static const struct luaL_reg maplib_m[] = {
struct lua_map_callback_data {
lua_State *L;
gint ref;
+ gboolean opaque;
rspamd_fstring_t *data;
struct rspamd_lua_map *lua_map;
};
@@ -442,7 +443,19 @@ lua_map_fin (struct map_cb_data *data, void **target)
}
else if (cbdata->data != NULL && cbdata->data->len != 0) {
lua_rawgeti (cbdata->L, LUA_REGISTRYINDEX, cbdata->ref);
- lua_pushlstring (cbdata->L, cbdata->data->str, cbdata->data->len);
+
+ if (cbdata->opaque) {
+ lua_pushlstring (cbdata->L, cbdata->data->str, cbdata->data->len);
+ }
+ else {
+ struct rspamd_lua_text *t;
+
+ t = lua_newuserdata (cbdata->L, sizeof (*t));
+ t->flags = 0;
+ t->len = cbdata->data->len;
+ t->start = cbdata->data->str;
+ }
+
pmap = lua_newuserdata (cbdata->L, sizeof (void *));
*pmap = cbdata->lua_map;
rspamd_lua_setclass (cbdata->L, "rspamd{map}", -1);
@@ -493,14 +506,15 @@ lua_config_add_map (lua_State *L)
struct lua_map_callback_data *cbdata;
struct rspamd_lua_map *map, **pmap;
struct rspamd_map *m;
+ gboolean opaque_data = FALSE;
int cbidx = -1, ret;
GError *err = NULL;
if (cfg) {
if (!rspamd_lua_parse_table_arguments (L, 2, &err,
RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT,
- "*url=O;description=S;callback=F;type=S",
- &map_obj, &description, &cbidx, &type)) {
+ "*url=O;description=S;callback=F;type=S;opaque_data=B",
+ &map_obj, &description, &cbidx, &type, &opaque_data)) {
ret = luaL_error (L, "invalid table arguments: %s", err->message);
g_error_free (err);
if (map_obj) {
@@ -529,6 +543,7 @@ lua_config_add_map (lua_State *L)
cbdata->data = NULL;
cbdata->lua_map = map;
cbdata->ref = cbidx;
+ cbdata->opaque = opaque_data;
if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description,
lua_map_read,
diff --git a/src/plugins/lua/settings.lua b/src/plugins/lua/settings.lua
index bc91bf4c8..7427e779d 100644
--- a/src/plugins/lua/settings.lua
+++ b/src/plugins/lua/settings.lua
@@ -1036,9 +1036,9 @@ end
-- Parse settings map from the ucl line
local settings_map_pool
-local function process_settings_map(string)
+local function process_settings_map(map_text)
local parser = ucl.parser()
- local res,err = parser:parse_string(string)
+ local res,err = parser:parse_string(map_text)
if not res then
rspamd_logger.warnx(rspamd_config, 'cannot parse settings map: ' .. err)
else
@@ -1162,7 +1162,13 @@ local set_section = rspamd_config:get_all_opt("settings")
if set_section and set_section[1] and type(set_section[1]) == "string" then
-- Just a map of ucl
- if not rspamd_config:add_map(set_section[1], "settings map", process_settings_map) then
+ local map_attrs = {
+ url = set_section[1],
+ description = "settings map",
+ callback = process_settings_map,
+ opaque_data = true
+ }
+ if not rspamd_config:add_map(map_attrs) then
rspamd_logger.errx(rspamd_config, 'cannot load settings from %1', set_section)
end
elseif set_section and type(set_section) == "table" then
More information about the Commits
mailing list