]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add multiple regexp maps support in lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 21 Apr 2017 11:43:48 +0000 (12:43 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 21 Apr 2017 11:43:48 +0000 (12:43 +0100)
src/lua/lua_common.h
src/lua/lua_map.c

index 20edff7781828ae8816b463883a2447d9372e5c7..394798fb8931aa4a4204e338dd457287c1210528 100644 (file)
@@ -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
 };
 
index c8130ede8ad41dd5f8eeb98d968d85593a0ff01b..3e292f4d3e56f4cb411d6cbeabe95fa07af92b83 100644 (file)
@@ -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);