]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add simple routine to get configuration help
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 28 Dec 2015 17:09:34 +0000 (17:09 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 28 Dec 2015 17:09:34 +0000 (17:09 +0000)
src/lua/lua_util.c
src/rspamadm/CMakeLists.txt
src/rspamadm/commands.c
src/rspamadm/confighelp.c [new file with mode: 0644]
src/rspamadm/control.c

index 7f89234c6c8754715a99ed5e566b1c54d606cfcf..3cae28211a6a9b7ecf66bd665231523ac1a986b9 100644 (file)
@@ -225,7 +225,7 @@ lua_util_config_from_ucl (lua_State *L)
 
                cfg->rcl_obj = obj;
                cfg->cache = rspamd_symbols_cache_new (cfg);
-               top = rspamd_rcl_config_init ();
+               top = rspamd_rcl_config_init (cfg);
 
                if (!rspamd_rcl_parse (top, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) {
                        msg_err_config ("rcl parse error: %s", err->message);
index d57325f0e0ce7e8ee4cd2ff51ed18eebca055289..258ed454b84dae7c7a4bed8f0246f1b31e7961e4 100644 (file)
@@ -6,6 +6,7 @@ SET(RSPAMADMSRC rspamadm.c
         fuzzy_merge.c
         configdump.c
         control.c
+        confighelp.c
         ${CMAKE_BINARY_DIR}/src/workers.c
         ${CMAKE_BINARY_DIR}/src/modules.c
         ${CMAKE_SOURCE_DIR}/src/controller.c
index b2e2c87f57ac8962522d56c68cb91b0733b24f80..589e154402e0525a00ad09fbb9ed3ce7897b1de8 100644 (file)
@@ -29,6 +29,7 @@ extern struct rspamadm_command configtest_command;
 extern struct rspamadm_command fuzzy_merge_command;
 extern struct rspamadm_command configdump_command;
 extern struct rspamadm_command control_command;
+extern struct rspamadm_command confighelp_command;
 
 const struct rspamadm_command *commands[] = {
        &help_command,
@@ -38,6 +39,7 @@ const struct rspamadm_command *commands[] = {
        &fuzzy_merge_command,
        &configdump_command,
        &control_command,
+       &confighelp_command,
        NULL
 };
 
diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c
new file mode 100644 (file)
index 0000000..d4ead3e
--- /dev/null
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2015, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *      * Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ *      * Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "rspamadm.h"
+#include "cfg_file.h"
+#include "cfg_rcl.h"
+#include "rspamd.h"
+#include "lua/lua_common.h"
+
+static gchar *config = NULL;
+static gboolean json = FALSE;
+static gboolean compact = FALSE;
+extern struct rspamd_main *rspamd_main;
+/* Defined in modules.c */
+extern module_t *modules[];
+extern worker_t *workers[];
+
+static void rspamadm_confighelp (gint argc, gchar **argv);
+
+static const char *rspamadm_confighelp_help (gboolean full_help);
+
+struct rspamadm_command confighelp_command = {
+               .name = "confighelp",
+               .flags = 0,
+               .help = rspamadm_confighelp_help,
+               .run = rspamadm_confighelp
+};
+
+static GOptionEntry entries[] = {
+               {"json",    'j', 0, G_OPTION_ARG_NONE, &json,
+                               "Output json",      NULL},
+               {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact,
+                               "Output compacted", NULL},
+               {NULL,     0,   0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
+};
+
+static const char *
+rspamadm_confighelp_help (gboolean full_help)
+{
+       const char *help_str;
+
+       if (full_help) {
+               help_str = "Shows help for the specified configuration options\n\n"
+                               "Usage: rspamadm confighelp [option[, option...]]\n"
+                               "Where options are:\n\n"
+                               "--help: shows available options and commands";
+       }
+       else {
+               help_str = "Shows help for configuration options";
+       }
+
+       return help_str;
+}
+
+static void
+rspamadm_confighelp_show (const ucl_object_t *obj)
+{
+       rspamd_fstring_t *out;
+
+       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 */
+               rspamd_ucl_emit_fstring (obj, UCL_EMIT_CONFIG, &out);
+       }
+
+       rspamd_fprintf (stdout, "%V", out);
+
+       rspamd_fstring_free (out);
+}
+
+static void
+rspamadm_confighelp (gint argc, gchar **argv)
+{
+       struct rspamd_rcl_section *top;
+       struct rspamd_config *cfg;
+       const ucl_object_t *doc_obj;
+       GOptionContext *context;
+       GError *error = NULL;
+       gint i = 1, ret = 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);
+       }
+
+       cfg = rspamd_config_new ();
+
+       top = rspamd_rcl_config_init (cfg);
+
+       if (argc > 1) {
+               while (argc > 1) {
+                       doc_obj = ucl_lookup_path (cfg->doc_strings, argv[i]);
+
+                       if (doc_obj != NULL) {
+                               rspamadm_confighelp_show (doc_obj);
+                       }
+                       else {
+                               rspamd_fprintf (stderr, "Cannot find help for %s\n", argv[i]);
+                               ret = EXIT_FAILURE;
+                       }
+
+                       i++;
+                       argc--;
+               }
+       }
+       else {
+               /* Show all documentation strings */
+               rspamadm_confighelp_show (cfg->doc_strings);
+       }
+
+       exit (ret);
+}
index 2b0332414eb63af3146128b598cbc2e94b7e3562..16170196eb66b5217efb9c4718d78d0991dbbb61 100644 (file)
@@ -35,9 +35,9 @@
 #include "fuzzy_stat.lua.h"
 
 static gchar *control_path = RSPAMD_DBDIR "/rspamd.sock";
-gboolean json = FALSE;
-gboolean compact = FALSE;
-gdouble timeout = 1.0;
+static gboolean json = FALSE;
+static gboolean compact = FALSE;
+static gdouble timeout = 1.0;
 
 static void rspamadm_control (gint argc, gchar **argv);
 static const char *rspamadm_control_help (gboolean full_help);