From f037213749e6c3d2bf35318b100c5e4767cc69f8 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 6 Mar 2016 14:32:08 +0000 Subject: [PATCH] [Feature] Return map object for further actions Map object could be used to manage maps, for example, by LUA API. --- src/libutil/map.c | 12 ++++++------ src/libutil/map.h | 2 +- src/lua/lua_config.c | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/libutil/map.c b/src/libutil/map.c index 7958867b3..0632439b9 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -756,7 +756,7 @@ rspamd_map_is_map (const gchar *map_line) return ret; } -gboolean +struct rspamd_map * rspamd_map_add (struct rspamd_config *cfg, const gchar *map_line, const gchar *description, @@ -784,7 +784,7 @@ rspamd_map_add (struct rspamd_config *cfg, /* First of all detect protocol line */ if (rspamd_map_check_proto (cfg, map_line, new_map) == NULL) { - return FALSE; + return NULL; } new_map->read_callback = read_callback; @@ -810,7 +810,7 @@ rspamd_map_add (struct rspamd_config *cfg, if (errno != ENOENT) { msg_err_config ("cannot open file '%s': %s", def, strerror (errno)); - return FALSE; + return NULL; } msg_info_config ( @@ -834,12 +834,12 @@ rspamd_map_add (struct rspamd_config *cfg, if (http_parser_parse_url (new_map->uri, strlen (new_map->uri), TRUE, &up) != 0) { msg_err_config ("cannot parse HTTP url: %s", new_map->uri); - return FALSE; + return NULL; } else { if (!(up.field_set & 1 << UF_HOST)) { msg_err_config ("cannot parse HTTP url: %s: no host", new_map->uri); - return FALSE; + return NULL; } tok.begin = new_map->uri + up.field_data[UF_HOST].off; @@ -876,7 +876,7 @@ rspamd_map_add (struct rspamd_config *cfg, cfg->maps = g_list_prepend (cfg->maps, new_map); - return TRUE; + return new_map; } static gchar* diff --git a/src/libutil/map.h b/src/libutil/map.h index 26a47abf8..9c5b18670 100644 --- a/src/libutil/map.h +++ b/src/libutil/map.h @@ -48,7 +48,7 @@ gboolean rspamd_map_is_map (const gchar *map_line); /** * Add map from line */ -gboolean rspamd_map_add (struct rspamd_config *cfg, +struct rspamd_map* rspamd_map_add (struct rspamd_config *cfg, const gchar *map_line, const gchar *description, map_cb_t read_callback, diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 868fa5087..1fbf6efdc 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -767,6 +767,7 @@ lua_config_add_radix_map (lua_State *L) struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; struct rspamd_lua_map *map, **pmap; + struct rspamd_map *m; if (cfg) { map_line = luaL_checkstring (L, 2); @@ -775,16 +776,17 @@ lua_config_add_radix_map (lua_State *L) map->data.radix = radix_create_compressed (); map->type = RSPAMD_LUA_MAP_RADIX; - if (!rspamd_map_add (cfg, map_line, description, + if ((m = rspamd_map_add (cfg, map_line, description, rspamd_radix_read, rspamd_radix_fin, - (void **)&map->data.radix)) { + (void **)&map->data.radix)) == NULL) { msg_warn_config ("invalid radix map %s", map_line); radix_destroy_compressed (map->data.radix); lua_pushnil (L); return 1; } + map->map = m; pmap = lua_newuserdata (L, sizeof (void *)); *pmap = map; rspamd_lua_setclass (L, "rspamd{map}", -1); @@ -843,6 +845,7 @@ lua_config_add_hash_map (lua_State *L) struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; struct rspamd_lua_map *map, **pmap; + struct rspamd_map *m; if (cfg) { map_line = luaL_checkstring (L, 2); @@ -852,16 +855,17 @@ lua_config_add_hash_map (lua_State *L) rspamd_strcase_equal); map->type = RSPAMD_LUA_MAP_SET; - if (!rspamd_map_add (cfg, map_line, description, + if ((m = rspamd_map_add (cfg, map_line, description, rspamd_hosts_read, rspamd_hosts_fin, - (void **)&map->data.hash)) { + (void **)&map->data.hash)) == NULL) { msg_warn_config ("invalid set map %s", map_line); g_hash_table_destroy (map->data.hash); lua_pushnil (L); return 1; } + map->map = m; pmap = lua_newuserdata (L, sizeof (void *)); *pmap = map; rspamd_lua_setclass (L, "rspamd{map}", -1); @@ -880,6 +884,7 @@ lua_config_add_kv_map (lua_State *L) struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *map_line, *description; struct rspamd_lua_map *map, **pmap; + struct rspamd_map *m; if (cfg) { map_line = luaL_checkstring (L, 2); @@ -889,16 +894,17 @@ lua_config_add_kv_map (lua_State *L) rspamd_strcase_equal); map->type = RSPAMD_LUA_MAP_HASH; - if (!rspamd_map_add (cfg, map_line, description, + if ((m = rspamd_map_add (cfg, map_line, description, rspamd_kv_list_read, rspamd_kv_list_fin, - (void **)&map->data.hash)) { + (void **)&map->data.hash)) == NULL) { msg_warn_config ("invalid hash map %s", map_line); g_hash_table_destroy (map->data.hash); lua_pushnil (L); return 1; } + map->map = m; pmap = lua_newuserdata (L, sizeof (void *)); *pmap = map; rspamd_lua_setclass (L, "rspamd{map}", -1); @@ -1864,6 +1870,7 @@ lua_config_add_map (lua_State *L) const gchar *map_line, *description; struct lua_map_callback_data *cbdata, **pcbdata; struct rspamd_lua_map *map; + struct rspamd_map *m; int cbidx; if (cfg) { @@ -1892,12 +1899,14 @@ lua_config_add_map (lua_State *L) pcbdata = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (cbdata)); *pcbdata = cbdata; - if (!rspamd_map_add (cfg, map_line, description, lua_map_read, lua_map_fin, - (void **)pcbdata)) { + if ((m = rspamd_map_add (cfg, map_line, description, + lua_map_read, lua_map_fin, + (void **)pcbdata)) == NULL) { msg_warn_config ("invalid hash map %s", map_line); lua_pushboolean (L, false); } else { + map->map = m; lua_pushboolean (L, true); } } -- 2.39.5