From: Vsevolod Stakhov Date: Fri, 21 Apr 2017 11:43:48 +0000 (+0100) Subject: [Minor] Add multiple regexp maps support in lua X-Git-Tag: 1.6.0~345 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e1ed8edbcc2f61bc59269441bb23c4148a0eca21;p=rspamd.git [Minor] Add multiple regexp maps support in lua --- diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 20edff778..394798fb8 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -94,6 +94,7 @@ enum rspamd_lua_map_type { RSPAMD_LUA_MAP_SET, RSPAMD_LUA_MAP_HASH, RSPAMD_LUA_MAP_REGEXP, + RSPAMD_LUA_MAP_REGEXP_MULTIPLE, RSPAMD_LUA_MAP_CALLBACK }; diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c index c8130ede8..3e292f4d3 100644 --- a/src/lua/lua_map.c +++ b/src/lua/lua_map.c @@ -476,6 +476,21 @@ lua_config_add_map (lua_State *L) return 1; } } + else if (strcmp (type, "regexp_multi") == 0) { + map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map->data.re_map = NULL; + map->type = RSPAMD_LUA_MAP_REGEXP_MULTIPLE; + + if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, + rspamd_regexp_list_read_multiple, + rspamd_regexp_list_fin, + (void **) &map->data.re_map)) == NULL) { + lua_pushnil (L); + ucl_object_unref (map_obj); + + return 1; + } + } else { ret = luaL_error (L, "invalid arguments: unknown type '%s'", type); ucl_object_unref (map_obj); @@ -615,6 +630,31 @@ lua_map_get_key (lua_State * L) } } } + else if (map->type == RSPAMD_LUA_MAP_REGEXP_MULTIPLE) { + GPtrArray *ar; + guint i; + const gchar *val; + + key = lua_map_process_string_key (L, 2, &len); + + if (key && map->data.re_map) { + ar = rspamd_match_regexp_map_all (map->data.re_map, key, + len); + + if (ar) { + lua_createtable (L, ar->len, 0); + + PTR_ARRAY_FOREACH (ar, i, val) { + lua_pushstring (L, val); + lua_rawseti (L, -2, i + 1); + } + + g_ptr_array_free (ar, TRUE); + + return 1; + } + } + } else if (map->type == RSPAMD_LUA_MAP_HASH) { /* key-value map */ key = lua_map_process_string_key (L, 2, &len);