static void rspamadm_confighelp_show (struct rspamd_config *cfg, gint argc, gchar **argv, const char *key, const ucl_object_t *obj) { rspamd_fstring_t *out; rspamd_lua_set_path (cfg->lua_state, NULL, ucl_vars); out = rspamd_fstring_new (); if (json) { rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out); } else if (compact) { rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out); } else { /* TODO: add lua helper for output */ if (key) { rspamd_fprintf (stdout, "Showing help for %s%s:\n", keyword ? "keyword " : "", key); } else { rspamd_fprintf (stdout, "Showing help for all options:\n"); } rspamadm_execute_lua_ucl_subr (argc, argv, obj, "confighelp", TRUE); rspamd_fstring_free (out); return; } rspamd_fprintf (stdout, "%V", out); rspamd_fprintf (stdout, "\n"); rspamd_fstring_free (out); }
static void rspamadm_rescore (gint argc, gchar **argv) { GOptionContext *context; GError *error = NULL; lua_State *L; ucl_object_t *obj; context = g_option_context_new ( "rescore - Estimate optimal symbol weights from log files"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (EXIT_FAILURE); } if (!HAS_TORCH) { rspamd_fprintf (stderr, "Torch is not enabled. " "Use -DENABLE_TORCH=ON option while running cmake.\n"); exit (EXIT_FAILURE); } if (logdir == NULL) { rspamd_fprintf (stderr, "Please specify log directory.\n"); exit (EXIT_FAILURE); } L = rspamd_lua_init (); rspamd_lua_set_path (L, NULL, NULL); obj = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, ucl_object_fromstring (logdir), "logdir", 0, false); ucl_object_insert_key (obj, ucl_object_fromstring (output), "output", 0, false); ucl_object_insert_key (obj, ucl_object_fromdouble (threshold), "threshold", 0, false); ucl_object_insert_key (obj, ucl_object_fromint (iters), "iters", 0, false); ucl_object_insert_key (obj, ucl_object_frombool (score_diff), "diff", 0, false); rspamadm_execute_lua_ucl_subr (L, argc, argv, obj, "rescore"); lua_close (L); ucl_object_unref (obj); }
static void rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd) { struct rspamd_config *cfg; ucl_object_t *doc_obj; const ucl_object_t *elt; GOptionContext *context; GError *error = NULL; module_t *mod, **pmod; worker_t **pworker; struct module_ctx *mod_ctx; gint i, ret = 0, processed_args = 0; context = g_option_context_new ( "confighelp - displays help for the configuration options"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } pworker = &workers[0]; while (*pworker) { /* Init string quarks */ (void) g_quark_from_static_string ((*pworker)->name); pworker++; } cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = rspamd_main->cfg->lua_state; cfg->compiled_modules = modules; cfg->compiled_workers = workers; rspamd_rcl_config_init (cfg, NULL); lua_pushboolean (cfg->lua_state, true); lua_setglobal (cfg->lua_state, "confighelp"); rspamd_rcl_add_lua_plugins_path (cfg, plugins_path, FALSE, NULL, NULL); /* Init modules to get documentation strings */ i = 0; for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) { mod = *pmod; mod_ctx = g_malloc0 (sizeof (struct module_ctx)); if (mod->module_init_func (cfg, &mod_ctx) == 0) { g_ptr_array_add (cfg->c_modules, mod_ctx); mod_ctx->mod = mod; mod->ctx_offset = i++; mod_ctx->mod = mod; } } /* Also init all workers */ for (pworker = cfg->compiled_workers; *pworker != NULL; pworker ++) { (*pworker)->worker_init_func (cfg); } /* Init lua modules */ rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, ucl_vars); rspamd_init_lua_filters (cfg, TRUE); if (argc > 1) { for (i = 1; i < argc; i ++) { if (argv[i][0] != '-') { if (keyword) { doc_obj = rspamadm_confighelp_search_word (cfg->doc_strings, argv[i]); } else { doc_obj = ucl_object_typed_new (UCL_OBJECT); elt = ucl_object_lookup_path (cfg->doc_strings, argv[i]); if (elt) { ucl_object_insert_key (doc_obj, ucl_object_ref (elt), argv[i], 0, false); } } if (doc_obj != NULL) { rspamadm_confighelp_show (cfg, argc, argv, argv[i], doc_obj); ucl_object_unref (doc_obj); } else { rspamd_fprintf (stderr, "Cannot find help for %s\n", argv[i]); ret = EXIT_FAILURE; } processed_args ++; } } } if (processed_args == 0) { /* Show all documentation strings */ rspamadm_confighelp_show (cfg, argc, argv, NULL, cfg->doc_strings); } rspamd_config_free (cfg); exit (ret); }