]> source.dussan.org Git - rspamd.git/commitdiff
Unbreak lua_worker with ucl.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 9 Nov 2013 22:36:18 +0000 (22:36 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 9 Nov 2013 22:36:18 +0000 (22:36 +0000)
src/cfg_file.h
src/cfg_rcl.c
src/cfg_rcl.h
src/lua_worker.c

index 659d6981e9d6e1a2395d928869b14c70d2225fb0..9dec61bc3c7418f061abc7fa76d1fadb9648b9c3 100644 (file)
@@ -265,6 +265,8 @@ struct rspamd_worker_param_parser {
 struct rspamd_worker_cfg_parser {
        struct rspamd_worker_param_parser *parsers;             /**< parsers hash                                                                               */
        gint type;                                                                              /**< workers quark                                                                              */
+       gboolean (*def_obj_parser)(ucl_object_t *obj, gpointer ud);     /**< default object parser                                                              */
+       gpointer def_ud;
        UT_hash_handle hh;                                                              /**< hash by type                                                                               */
 };
 
index 41a1cd8c8b3730a9806dd9522299ecdf165e1c64..3f419ceb8bc3963eb47b7c5023a09c75f941223c 100644 (file)
@@ -507,6 +507,11 @@ rspamd_rcl_worker_handler (struct config_file *cfg, ucl_object_t *obj,
                                }
                        }
                }
+               if (wparser->def_obj_parser != NULL) {
+                       if (! wparser->def_obj_parser (obj, wparser->def_ud)) {
+                               return FALSE;
+                       }
+               }
        }
 
        cfg->workers = g_list_prepend (cfg->workers, wrk);
@@ -1393,3 +1398,21 @@ rspamd_rcl_register_worker_option (struct config_file *cfg, gint type, const gch
        nhandler->handler = handler;
        HASH_ADD_STR (nparser->parsers, name, nhandler);
 }
+
+
+void
+rspamd_rcl_register_worker_parser (struct config_file *cfg, gint type,
+               gboolean (*func)(ucl_object_t *, gpointer), gpointer ud)
+{
+       struct rspamd_worker_cfg_parser *nparser;
+       HASH_FIND_INT (cfg->wrk_parsers, &type, nparser);
+       if (nparser == NULL) {
+               /* Allocate new parser for this worker */
+               nparser = memory_pool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_worker_cfg_parser));
+               nparser->type = type;
+               HASH_ADD_INT (cfg->wrk_parsers, type, nparser);
+       }
+
+       nparser->def_obj_parser = func;
+       nparser->def_ud = ud;
+}
index 82f9b98024bb6e4c735b8ee2ba62111e3491510a..6949c48aa14d418b26c3bc23109c9578138b8ae3 100644 (file)
@@ -215,4 +215,14 @@ gboolean rspamd_rcl_parse_struct_boolean (struct config_file *cfg, ucl_object_t
  */
 void rspamd_rcl_register_worker_option (struct config_file *cfg, gint type, const gchar *name,
                rspamd_rcl_handler_t handler, gpointer target, gsize offset, gint flags);
+
+/**
+ * Regiester a default parser for a worker
+ * @param cfg config structure
+ * @param type type of worker (GQuark)
+ * @param func handler function
+ * @param ud userdata for handler function
+ */
+void rspamd_rcl_register_worker_parser (struct config_file *cfg, gint type,
+               gboolean (*func)(ucl_object_t *, gpointer), gpointer ud);
 #endif /* CFG_RCL_H_ */
index 0849415c6bc41780c2d48a91e0a69aaf6dcda21a..8b04b8838a911d5ccd06d2c4064b847788deacf8 100644 (file)
@@ -77,6 +77,8 @@ struct rspamd_lua_worker_ctx {
        gint                                                    cbref_fin;
        /* Config file */
        struct config_file                         *cfg;
+       /* The rest options */
+       ucl_object_t                               *opts;
 };
 
 /* Lua bindings */
@@ -185,13 +187,11 @@ lua_worker_register_exit_callback (lua_State *L)
 }
 
 /* XXX: This fucntions should be rewritten completely */
-#warning "lua_worker_get_option is broken"
 static int
 lua_worker_get_option (lua_State *L)
 {
        struct rspamd_lua_worker_ctx                                    *ctx = lua_check_lua_worker (L);
-       GList                                                                                   *val;
-       gint                                                                                     i;
+       ucl_object_t                                                                    *val;
        const gchar                                                                             *name;
 
        if (ctx) {
@@ -201,19 +201,12 @@ lua_worker_get_option (lua_State *L)
                        lua_pushnil (L);
                }
                else {
-                       val = g_hash_table_lookup (ctx->params, name);
+                       val = ucl_object_find_key (ctx->opts, name);
                        if (val == NULL) {
                                lua_pushnil (L);
                        }
                        else {
-                               /* Push the array */
-                               i = 1;
-                               lua_newtable (L);
-                               while (val) {
-                                       lua_pushstring (L, val->data);
-                                       lua_rawseti (L, -2, i++);
-                                       val = g_list_next (val);
-                               }
+                               lua_rcl_obj_push (L, val, TRUE);
                        }
                }
        }
@@ -399,6 +392,16 @@ lua_accept_socket (gint fd, short what, void *arg)
        }
 }
 
+static gboolean
+rspamd_lua_worker_parser (ucl_object_t *obj, gpointer ud)
+{
+       struct rspamd_lua_worker_ctx       *ctx = ud;
+
+       ctx->opts = obj;
+
+       return TRUE;
+}
+
 gpointer
 init_lua_worker (struct config_file *cfg)
 {
@@ -415,6 +418,8 @@ init_lua_worker (struct config_file *cfg)
                        rspamd_rcl_parse_struct_string, ctx,
                        G_STRUCT_OFFSET (struct rspamd_lua_worker_ctx, file), 0);
 
+       rspamd_rcl_register_worker_parser (cfg, type, rspamd_lua_worker_parser, ctx);
+
        return ctx;
 }