]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Fix various issues
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 May 2018 12:59:54 +0000 (13:59 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 May 2018 12:59:54 +0000 (13:59 +0100)
lualib/rspamadm/grep.lua
src/rspamadm/CMakeLists.txt
src/rspamadm/commands.c
src/rspamadm/rspamadm.c
src/rspamadm/rspamadm.h

index 7d0341e5b19c5f64be0ded884b01769ec9618f99..0e66b97709a4a7fdcb0af1edc550e8cc535de11e 100644 (file)
@@ -166,6 +166,6 @@ end
 
 return {
   handler = handler,
-  description = parser.description,
+  description = 'Search for patterns in rspamd logs',
   name = 'grep'
 }
\ No newline at end of file
index fb3f252290df54cf09b079d67b5187ed06377ff2..1370fde89c19d7755745f56ddd72b90f03f4ae17 100644 (file)
@@ -5,7 +5,6 @@ SET(RSPAMADMSRC rspamadm.c
         configtest.c
         fuzzy_convert.c
         fuzzy_merge.c
-        grep.c
         configdump.c
         control.c
         confighelp.c
index 744212afbb07d70747dafc168edcdeeb09132a35..d787713f6f6f90eede819b251cb918db93fe069c 100644 (file)
@@ -27,7 +27,6 @@ extern struct rspamadm_command control_command;
 extern struct rspamadm_command confighelp_command;
 extern struct rspamadm_command statconvert_command;
 extern struct rspamadm_command fuzzyconvert_command;
-extern struct rspamadm_command grep_command;
 extern struct rspamadm_command signtool_command;
 extern struct rspamadm_command lua_command;
 extern struct rspamadm_command dkim_keygen_command;
@@ -46,7 +45,6 @@ const struct rspamadm_command *commands[] = {
        &confighelp_command,
        &statconvert_command,
        &fuzzyconvert_command,
-       &grep_command,
        &signtool_command,
        &lua_command,
        &dkim_keygen_command,
@@ -97,14 +95,13 @@ rspamadm_lua_command_run (gint argc, gchar **argv,
        gint i, err_idx, ret;
        GString *tb;
 
-       lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
-
        lua_pushcfunction (L, &rspamd_lua_traceback);
        err_idx = lua_gettop (L);
 
        /* Function */
+       lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
        lua_pushstring (L, "handler");
-       lua_gettable (L, -1);
+       lua_gettable (L, -2);
 
        /* Args */
        lua_createtable (L, argc + 1, 0);
@@ -114,7 +111,7 @@ rspamadm_lua_command_run (gint argc, gchar **argv,
                lua_rawseti (L, -2, i); /* Starting from zero ! */
        }
 
-       if ((ret = lua_pcall (L, 2, 0, err_idx)) != 0) {
+       if ((ret = lua_pcall (L, 1, 0, err_idx)) != 0) {
                tb = lua_touserdata (L, -1);
                msg_err ("call to rspamadm lua script %s failed (%d): %v", cmd->name,
                                ret, tb);
@@ -139,41 +136,48 @@ rspamadm_lua_command_help (gboolean full_help,
        gint err_idx, ret;
        GString *tb;
 
-       lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
-
-       lua_pushcfunction (L, &rspamd_lua_traceback);
-       err_idx = lua_gettop (L);
+       if (full_help) {
+               lua_pushcfunction (L, &rspamd_lua_traceback);
+               err_idx = lua_gettop (L);
 
-       /* Function */
-       lua_pushstring (L, "handler");
-       lua_gettable (L, -1);
+               lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
+               /* Function */
+               lua_pushstring (L, "handler");
+               lua_gettable (L, -2);
 
-       /* Args */
-       lua_createtable (L, 2, 0);
-       lua_pushstring (L, cmd->name);
-       lua_rawseti (L, -2, 0); /* Starting from zero ! */
+               /* Args */
+               lua_createtable (L, 2, 0);
+               lua_pushstring (L, cmd->name);
+               lua_rawseti (L, -2, 0); /* Starting from zero ! */
 
-       if (full_help) {
                lua_pushstring (L, "--help");
                lua_rawseti (L, -2, 1);
-       }
-       else {
-               lua_pushstring (L, "--usage");
-               lua_rawseti (L, -2, 1);
-       }
 
-       if ((ret = lua_pcall (L, 2, 0, err_idx)) != 0) {
-               tb = lua_touserdata (L, -1);
-               msg_err ("call to rspamadm lua script %s failed (%d): %v", cmd->name,
-                               ret, tb);
+               if ((ret = lua_pcall (L, 1, 0, err_idx)) != 0) {
+                       tb = lua_touserdata (L, -1);
+                       msg_err ("call to rspamadm lua script %s failed (%d): %v", cmd->name,
+                                       ret, tb);
 
-               if (tb) {
-                       g_string_free (tb, TRUE);
-               }
+                       if (tb) {
+                               g_string_free (tb, TRUE);
+                       }
 
-               lua_settop (L, 0);
+                       lua_settop (L, 0);
 
-               exit (EXIT_FAILURE);
+                       exit (EXIT_FAILURE);
+               }
+       }
+       else {
+               lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx);
+               lua_pushstring (L, "description");
+               lua_gettable (L, -2);
+
+               if (lua_isstring (L, -1)) {
+                       printf ("  %-18s %-60s\n", cmd->name, lua_tostring (L, -1));
+               }
+               else {
+                       printf ("  %-18s %-60s\n", cmd->name, "no description available");
+               }
        }
 
        lua_settop (L, 0);
@@ -217,6 +221,8 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest)
                                lua_gettable (L, -2);
                        }
                        else {
+                               msg_err ("bad return type in %s: %s", path,
+                                               lua_typename (L, lua_type (L, -1)));
                                continue; /* Something goes wrong, huh */
                        }
 
@@ -260,6 +266,8 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest)
                        lua_cmd->flags |= RSPAMADM_FLAG_LUA;
                        lua_cmd->run = rspamadm_lua_command_run;
                        lua_cmd->help = rspamadm_lua_command_help;
+
+                       g_ptr_array_add (dest, lua_cmd);
                }
        }
 }
index 7c96ae3ed7680850416a9dae0e5e811949585d3a..a804d0059d08ef143b11f06a69248f77c214d196 100644 (file)
@@ -71,7 +71,7 @@ rspamadm_error (void)
 static void
 rspamadm_version (void)
 {
-       printf ("Rspamadm %s\n", RVERSION);
+       rspamd_printf ("Rspamadm %s\n", RVERSION);
 }
 
 static void
@@ -80,7 +80,7 @@ rspamadm_usage (GOptionContext *context)
        gchar *help_str;
 
        help_str = g_option_context_get_help (context, TRUE, NULL);
-       printf ("%s", help_str);
+       rspamd_printf ("%s", help_str);
 }
 
 static void
@@ -89,16 +89,14 @@ rspamadm_commands (GPtrArray *all_commands)
        const struct rspamadm_command *cmd;
        guint i;
 
-       printf ("Rspamadm %s\n", RVERSION);
-       printf ("Usage: rspamadm [global_options] command [command_options]\n");
-       printf ("\nAvailable commands:\n");
+       rspamd_printf ("Rspamadm %s\n", RVERSION);
+       rspamd_printf ("Usage: rspamadm [global_options] command [command_options]\n");
+       rspamd_printf ("\nAvailable commands:\n");
 
        PTR_ARRAY_FOREACH (all_commands, i, cmd) {
                if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) {
-                       printf ("  %-18s %-60s\n", cmd->name, cmd->help (FALSE, cmd));
+                       rspamd_printf ("  %-18s %-60s\n", cmd->name, cmd->help (FALSE, cmd));
                }
-
-               cmd ++;
        }
 }
 
@@ -125,15 +123,15 @@ rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command)
        const struct rspamadm_command *cmd;
        GPtrArray *all_commands = (GPtrArray *)command->command_data;
 
-       printf ("Rspamadm %s\n", RVERSION);
-       printf ("Usage: rspamadm [global_options] command [command_options]\n\n");
+       rspamd_printf ("Rspamadm %s\n", RVERSION);
+       rspamd_printf ("Usage: rspamadm [global_options] command [command_options]\n\n");
 
        if (argc <= 1) {
                cmd_name = "help";
        }
        else {
                cmd_name = argv[1];
-               printf ("Showing help for %s command\n\n", cmd_name);
+               rspamd_printf ("Showing help for %s command\n\n", cmd_name);
        }
 
        cmd = rspamadm_search_command (cmd_name, all_commands);
@@ -145,7 +143,7 @@ rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command)
 
        if (strcmp (cmd_name, "help") == 0) {
                guint i;
-               printf ("Available commands:\n");
+               rspamd_printf ("Available commands:\n");
 
                PTR_ARRAY_FOREACH (all_commands, i, cmd) {
                        if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) {
@@ -162,7 +160,7 @@ rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command)
        }
        else {
                if (!(cmd->flags & RSPAMADM_FLAG_LUA)) {
-                       printf ("%s\n", cmd->help (TRUE, cmd));
+                       rspamd_printf ("%s\n", cmd->help (TRUE, cmd));
                }
                else {
                        /* Just call lua subr */
@@ -271,6 +269,15 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
        return TRUE;
 }
 
+static gint
+rspamdadm_commands_sort_func (gconstpointer a, gconstpointer b)
+{
+       const struct rspamadm_command *cmda = *((struct rspamadm_command const **)a),
+                       *cmdb = *((struct rspamadm_command const **)b);
+
+       return strcmp (cmda->name, cmdb->name);
+}
+
 gint
 main (gint argc, gchar **argv, gchar **env)
 {
@@ -374,6 +381,7 @@ main (gint argc, gchar **argv, gchar **env)
        lua_setglobal (L, "rspamadm");
 
        rspamadm_fill_lua_commands (L, all_commands);
+       g_ptr_array_sort (all_commands, rspamdadm_commands_sort_func);
 
        g_strfreev (nargv);
 
index 3d0a692048644358cb30e50773a1d2d1574e878f..25f512595aec5bf44a30e39cc631f8a9dd333025 100644 (file)
@@ -35,7 +35,7 @@ typedef void (*rspamadm_run_func) (gint argc, gchar **argv,
 typedef void (*rspamadm_lua_exports_func) (gpointer lua_state);
 
 #define RSPAMADM_FLAG_NOHELP (1 << 0)
-#define RSPAMADM_FLAG_LUA (1 << 0)
+#define RSPAMADM_FLAG_LUA (1 << 1)
 
 struct rspamadm_command {
        const gchar *name;