diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-03-26 16:38:27 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-03-26 16:38:27 +0000 |
commit | fbe966f7b9d6f1655cbce792fb60d2d1b4633616 (patch) | |
tree | 540effaaeeeefa24a5bee904de3f3a9891e4bb53 /src/lua | |
parent | a5af7ff8d409b63743889c5562c7b735e19313a8 (diff) | |
download | rspamd-fbe966f7b9d6f1655cbce792fb60d2d1b4633616.tar.gz rspamd-fbe966f7b9d6f1655cbce792fb60d2d1b4633616.zip |
[Rework] Change lua global variables registration
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_common.c | 147 | ||||
-rw-r--r-- | src/lua/lua_common.h | 4 |
2 files changed, 99 insertions, 52 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index cff684aad..7f49c090d 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -538,59 +538,10 @@ rspamd_lua_rspamd_version (lua_State *L) } void -rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L, - GHashTable *vars) +rspamd_lua_set_env (lua_State *L, GHashTable *vars) { - struct rspamd_config **pcfg; gint orig_top = lua_gettop (L); - /* First check for global variable 'config' */ - lua_getglobal (L, "config"); - if (lua_isnil (L, -1)) { - /* Assign global table to set up attributes */ - lua_newtable (L); - lua_setglobal (L, "config"); - } - - lua_getglobal (L, "metrics"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "metrics"); - } - - lua_getglobal (L, "composites"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "composites"); - } - - lua_getglobal (L, "rspamd_classifiers"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "rspamd_classifiers"); - } - - lua_getglobal (L, "classifiers"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "classifiers"); - } - - lua_getglobal (L, "rspamd_version"); - if (lua_isnil (L, -1)) { - lua_pushcfunction (L, rspamd_lua_rspamd_version); - lua_setglobal (L, "rspamd_version"); - } - - if (cfg != NULL) { - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); - *pcfg = cfg; - lua_setglobal (L, "rspamd_config"); - } - - lua_settop (L, orig_top); - /* Set known paths as rspamd_paths global */ lua_getglobal (L, "rspamd_paths"); if (lua_isnil (L, -1)) { @@ -728,6 +679,102 @@ rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L, lua_setglobal (L, "rspamd_paths"); } + lua_getglobal (L, "rspamd_env"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + + if (vars != NULL) { + GHashTableIter it; + gpointer k, v; + + g_hash_table_iter_init (&it, vars); + + while (g_hash_table_iter_next (&it, &k, &v)) { + rspamd_lua_table_set (L, k, v); + } + } + + gint hostlen = sysconf (_SC_HOST_NAME_MAX); + + if (hostlen <= 0) { + hostlen = 256; + } + else { + hostlen ++; + } + + gchar *hostbuf = g_alloca (hostlen); + memset (hostbuf, 0, hostlen); + gethostname (hostbuf, hostlen - 1); + + rspamd_lua_table_set (L, "hostname", hostbuf); + + rspamd_lua_table_set (L, "version", RVERSION); + rspamd_lua_table_set (L, "ver_major", RSPAMD_VERSION_MAJOR); + rspamd_lua_table_set (L, "ver_minor", RSPAMD_VERSION_MINOR); + rspamd_lua_table_set (L, "ver_patch", RSPAMD_VERSION_PATCH); + rspamd_lua_table_set (L, "ver_id", RID); + lua_pushstring (L, "ver_num"); + lua_pushinteger (L, RSPAMD_VERSION_NUM); + lua_settable (L, -3); + + lua_setglobal (L, "rspamd_env"); + } + + lua_settop (L, orig_top); +} + +void +rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L) +{ + struct rspamd_config **pcfg; + gint orig_top = lua_gettop (L); + + /* First check for global variable 'config' */ + lua_getglobal (L, "config"); + if (lua_isnil (L, -1)) { + /* Assign global table to set up attributes */ + lua_newtable (L); + lua_setglobal (L, "config"); + } + + lua_getglobal (L, "metrics"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "metrics"); + } + + lua_getglobal (L, "composites"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "composites"); + } + + lua_getglobal (L, "rspamd_classifiers"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "rspamd_classifiers"); + } + + lua_getglobal (L, "classifiers"); + if (lua_isnil (L, -1)) { + lua_newtable (L); + lua_setglobal (L, "classifiers"); + } + + lua_getglobal (L, "rspamd_version"); + if (lua_isnil (L, -1)) { + lua_pushcfunction (L, rspamd_lua_rspamd_version); + lua_setglobal (L, "rspamd_version"); + } + + if (cfg != NULL) { + pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); + rspamd_lua_setclass (L, "rspamd{config}", -1); + *pcfg = cfg; + lua_setglobal (L, "rspamd_config"); + } + lua_settop (L, orig_top); } diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index c8858571a..520f5920d 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -306,8 +306,8 @@ void rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars); /* Set some lua globals */ -void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L, - GHashTable *vars); +void rspamd_lua_set_env (lua_State *L, GHashTable *vars); +void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L); struct memory_pool_s * rspamd_lua_check_mempool (lua_State * L, gint pos); struct rspamd_config * lua_check_config (lua_State * L, gint pos); |