Browse Source

[Feature] Rework confighelp to load Lua plugins

tags/1.6.0
Vsevolod Stakhov 7 years ago
parent
commit
98d22d8bd5

+ 10
- 10
src/libserver/cfg_rcl.c View File

@@ -1029,10 +1029,10 @@ rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
return TRUE;
}

static gboolean
rspamd_rcl_add_module_path (struct rspamd_config *cfg,
const gchar *path,
GError **err)
gboolean
rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
const gchar *path,
GError **err)
{
struct stat st;
struct script_module *cur_mod;
@@ -1145,16 +1145,16 @@ rspamd_rcl_modules_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
LL_FOREACH (val, cur)
{
if (ucl_object_tostring_safe (cur, &data)) {
if (!rspamd_rcl_add_module_path (cfg,
rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
if (!rspamd_rcl_add_lua_plugins_path (cfg,
rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
return FALSE;
}
}
}
}
else if (ucl_object_tostring_safe (obj, &data)) {
if (!rspamd_rcl_add_module_path (cfg,
rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
if (!rspamd_rcl_add_lua_plugins_path (cfg,
rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
return FALSE;
}
}
@@ -3869,7 +3869,7 @@ rspamd_rcl_add_doc_from_comments (struct rspamd_config *cfg,
ucl_object_t *top_doc, const ucl_object_t *obj,
const ucl_object_t *comments, gboolean is_top)
{
ucl_object_iter_t it;
ucl_object_iter_t it = NULL;
const ucl_object_t *cur, *cmt;
ucl_object_t *cur_doc;

@@ -3927,7 +3927,7 @@ rspamd_rcl_add_doc_by_example (struct rspamd_config *cfg,
top_doc = rspamd_rcl_add_doc_by_path (cfg, root_path, doc_string,
doc_name, ucl_object_type (top), NULL, 0, NULL, FALSE);
ucl_object_insert_key (top_doc,
ucl_object_fromlstring (example_data, example_len),
ucl_object_fromstring_common (example_data, example_len, 0),
"example", 0, false);

rspamd_rcl_add_doc_from_comments (cfg, top_doc, top, comments, TRUE);

+ 11
- 0
src/libserver/cfg_rcl.h View File

@@ -446,4 +446,15 @@ ucl_object_t *rspamd_rcl_add_doc_by_example (struct rspamd_config *cfg,
const gchar *doc_string,
const gchar *doc_name,
const gchar *example_data, gsize example_len);

/**
* Add lua modules path
* @param cfg
* @param path
* @param err
* @return
*/
gboolean rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg,
const gchar *path,
GError **err);
#endif /* CFG_RCL_H_ */

+ 1
- 1
src/libserver/cfg_utils.c View File

@@ -1376,7 +1376,7 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
cur = g_list_next (cur);
}

return rspamd_init_lua_filters (cfg);
return rspamd_init_lua_filters (cfg, 0);
}

static void

+ 6
- 4
src/lua/lua_common.c View File

@@ -340,7 +340,7 @@ rspamd_free_lua_locked (struct lua_locked_state *st)
}

gboolean
rspamd_init_lua_filters (struct rspamd_config *cfg)
rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
{
struct rspamd_config **pcfg;
GList *cur;
@@ -355,9 +355,11 @@ rspamd_init_lua_filters (struct rspamd_config *cfg)
while (cur) {
module = cur->data;
if (module->path) {
if (!rspamd_config_is_module_enabled (cfg, module->name)) {
cur = g_list_next (cur);
continue;
if (!force_load) {
if (!rspamd_config_is_module_enabled (cfg, module->name)) {
cur = g_list_next (cur);
continue;
}
}

lua_pushcfunction (L, &rspamd_lua_traceback);

+ 2
- 1
src/lua/lua_common.h View File

@@ -162,7 +162,8 @@ lua_State *rspamd_lua_init (void);
/**
* Load and initialize lua plugins
*/
gboolean rspamd_init_lua_filters (struct rspamd_config *cfg);
gboolean
rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load);

/**
* Initialize new locked lua_State structure

+ 14
- 6
src/lua/lua_config.c View File

@@ -2603,18 +2603,22 @@ static gint
lua_config_add_doc (lua_State *L)
{
struct rspamd_config *cfg;
const gchar *path, *option, *doc_string;
const gchar *path = NULL, *option, *doc_string;
const gchar *type_str = NULL, *default_value = NULL;
ucl_type_t type = UCL_NULL;
gboolean required = FALSE;
GError *err = NULL;

cfg = lua_check_config (L, 1);
path = luaL_checkstring (L, 2);

if (lua_type (L, 2 ) == LUA_TSTRING) {
path = luaL_checkstring (L, 2);
}

option = luaL_checkstring (L, 3);
doc_string = luaL_checkstring (L, 4);

if (cfg && path && option && doc_string) {
if (cfg && option && doc_string) {
if (lua_type (L, 5) == LUA_TTABLE) {
if (!rspamd_lua_parse_table_arguments (L, 2, &err,
"type=S;default=S;required=B",
@@ -2647,16 +2651,20 @@ static gint
lua_config_add_example (lua_State *L)
{
struct rspamd_config *cfg;
const gchar *path, *option, *doc_string, *example;
const gchar *path = NULL, *option, *doc_string, *example;
gsize example_len;

cfg = lua_check_config (L, 1);
path = luaL_checkstring (L, 2);

if (lua_type (L, 2 ) == LUA_TSTRING) {
path = luaL_checkstring (L, 2);
}

option = luaL_checkstring (L, 3);
doc_string = luaL_checkstring (L, 4);
example = luaL_checklstring (L, 5, &example_len);

if (cfg && path && option && doc_string && example) {
if (cfg && option && doc_string && example) {

rspamd_rcl_add_doc_by_example (cfg, path, doc_string, option,
example, example_len);

+ 10
- 0
src/plugins/lua/dcc.lua View File

@@ -128,3 +128,13 @@ if opts and opts['host'] then
else
logger.infox('DCC module not configured');
end

rspamd_config:add_example(nil, 'dcc',
"Check messages for 'bulkiness' using DCC",
[[
dcc {
host = "/var/dcc/dccifd"; # Unix socket or hostname
port = 1234 # Port to use (needed for TCP socket)
timeout = 2s; # Timeout to wait for checks
}
]])

+ 11
- 1
src/rspamadm/confighelp.c View File

@@ -25,6 +25,7 @@
static gboolean json = FALSE;
static gboolean compact = FALSE;
static gboolean keyword = FALSE;
static const gchar *plugins_path = RSPAMD_PLUGINSDIR;
extern struct rspamd_main *rspamd_main;
/* Defined in modules.c */
extern module_t *modules[];
@@ -48,6 +49,8 @@ static GOptionEntry entries[] = {
"Output compacted", NULL},
{"keyword", 'k', 0, G_OPTION_ARG_NONE, &keyword,
"Search by keyword", NULL},
{"plugins", 'P', 0, G_OPTION_ARG_STRING, &plugins_path,
"Use the following plugin path (" RSPAMD_PLUGINSDIR ")", NULL},
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
};

@@ -63,7 +66,8 @@ rspamadm_confighelp_help (gboolean full_help)
"-c: output compacted JSON\n"
"-j: output pretty formatted JSON\n"
"-k: search by keyword in doc string\n"
"--color: show colored output\n"
"-P: use specific Lua plugins path\n"
"--no-color: show colored output\n"
"--short: show only option names\n"
"--no-examples: do not show examples (impied by --short)\n"
"--help: shows available options and commands";
@@ -224,6 +228,9 @@ rspamadm_confighelp (gint argc, gchar **argv)
cfg->compiled_workers = workers;

rspamd_rcl_config_init (cfg);
lua_pushboolean (cfg->lua_state, true);
lua_setglobal (cfg->lua_state, "confighelp");
rspamd_rcl_add_lua_plugins_path (cfg, plugins_path, NULL);

/* Init modules to get documentation strings */
for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) {
@@ -242,6 +249,9 @@ rspamadm_confighelp (gint argc, gchar **argv)
(*pworker)->worker_init_func (cfg);
}

/* Init lua modules */
rspamd_init_lua_filters (cfg, TRUE);

if (argc > 1) {
for (i = 1; i < argc; i ++) {
if (argv[i][0] != '-') {

+ 1
- 1
src/rspamadm/confighelp.lua View File

@@ -12,7 +12,7 @@ local known_attrs = {


local function maybe_print_color(key)
if opts['color'] then
if not opts['no-color'] then
return ansicolors.white .. key .. ansicolors.reset
else
return key

Loading…
Cancel
Save