]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Return map object for further actions
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 6 Mar 2016 14:32:08 +0000 (14:32 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 6 Mar 2016 14:32:08 +0000 (14:32 +0000)
Map object could be used to manage maps, for example, by LUA API.

src/libutil/map.c
src/libutil/map.h
src/lua/lua_config.c

index 7958867b3b3c819d6ca4d364c2222e7ba2ea244c..0632439b9ecc53d42b0f71ed22d78d228b70ebd9 100644 (file)
@@ -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*
index 26a47abf8420a222198808199f2230f15ac35516..9c5b1867049385588dbf4cd159059abfa5a3254f 100644 (file)
@@ -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,
index 868fa50876c67e1b3717c9f5a77d6074dd7ca98a..1fbf6efdc1c7e98f0d2522e8f0292fc1d057010e 100644 (file)
@@ -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);
                        }
                }