From b0684dcde39eac33709c16ee27db197a1dfa5724 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 9 Nov 2013 22:36:18 +0000 Subject: [PATCH] Unbreak lua_worker with ucl. --- src/cfg_file.h | 2 ++ src/cfg_rcl.c | 23 +++++++++++++++++++++++ src/cfg_rcl.h | 10 ++++++++++ src/lua_worker.c | 29 +++++++++++++++++------------ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/cfg_file.h b/src/cfg_file.h index 659d6981e..9dec61bc3 100644 --- a/src/cfg_file.h +++ b/src/cfg_file.h @@ -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 */ }; diff --git a/src/cfg_rcl.c b/src/cfg_rcl.c index 41a1cd8c8..3f419ceb8 100644 --- a/src/cfg_rcl.c +++ b/src/cfg_rcl.c @@ -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; +} diff --git a/src/cfg_rcl.h b/src/cfg_rcl.h index 82f9b9802..6949c48aa 100644 --- a/src/cfg_rcl.h +++ b/src/cfg_rcl.h @@ -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_ */ diff --git a/src/lua_worker.c b/src/lua_worker.c index 0849415c6..8b04b8838 100644 --- a/src/lua_worker.c +++ b/src/lua_worker.c @@ -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; } -- 2.39.5