]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add aliases support to rspamadm commands
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 1 Jun 2018 13:52:30 +0000 (14:52 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 1 Jun 2018 13:52:30 +0000 (14:52 +0100)
src/rspamadm/commands.c
src/rspamadm/rspamadm.c
src/rspamadm/rspamadm.h

index 8d93a86da1354a1602c885b106a0ef65dad83ecd..83a0507eca780e8a05b6957c503d0b8d5c99a960 100644 (file)
@@ -53,7 +53,8 @@ const struct rspamadm_command *
 rspamadm_search_command (const gchar *name, GPtrArray *all_commands)
 {
        const struct rspamadm_command *ret = NULL, *cmd;
-       guint i;
+       const gchar *alias;
+       guint i, j;
 
        if (name == NULL) {
                name = "help";
@@ -64,6 +65,13 @@ rspamadm_search_command (const gchar *name, GPtrArray *all_commands)
                                ret = cmd;
                                break;
                        }
+
+               PTR_ARRAY_FOREACH (cmd->aliases, j, alias) {
+                       if (strcmp (name, alias) == 0) {
+                               ret = cmd;
+                               break;
+                       }
+               }
        }
 
        return ret;
@@ -208,6 +216,7 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest)
                if (luaL_dofile (L, path) != 0) {
                        msg_err ("cannot execute lua script %s: %s",
                                        path, lua_tostring (L, -1));
+                       lua_settop (L, 0);
                        continue;
                } else {
                        if (lua_type (L, -1) == LUA_TTABLE) {
@@ -252,6 +261,24 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest)
 
                        lua_pop (L, 1);
 
+                       lua_pushstring (L, "aliases");
+                       lua_gettable (L, -2);
+
+                       if (lua_type (L, -1) == LUA_TTABLE) {
+                               lua_cmd->aliases = g_ptr_array_new_full (
+                                               rspamd_lua_table_size (L, -1),
+                                               g_free);
+
+                               for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 2)) {
+                                       if (lua_isstring (L, -1)) {
+                                               g_ptr_array_add (lua_cmd->aliases,
+                                                               g_strdup (lua_tostring (L, -1)));
+                                       }
+                               }
+                       }
+
+                       lua_pop (L, 1);
+
                        lua_pushvalue (L, -1);
                        /* Reference table itself */
                        lua_cmd->command_data = GINT_TO_POINTER (luaL_ref (L, LUA_REGISTRYINDEX));
@@ -259,7 +286,10 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest)
                        lua_cmd->run = rspamadm_lua_command_run;
                        lua_cmd->help = rspamadm_lua_command_help;
 
+
                        g_ptr_array_add (dest, lua_cmd);
                }
+
+               lua_settop (L, 0);
        }
 }
index eff27c1e9479ad1a8c70e2c55bb7fc4fb62f4147..68dbdeb852afc1f1add52d9dd6f17896931b1a70 100644 (file)
@@ -282,6 +282,28 @@ rspamdadm_commands_sort_func (gconstpointer a, gconstpointer b)
        return strcmp (cmda->name, cmdb->name);
 }
 
+static gboolean
+rspamadm_command_maybe_match_name (const gchar *cmd, const gchar *input)
+{
+       gsize clen, inplen;
+
+       clen = strlen (cmd);
+       inplen = strlen (input);
+
+       if (rspamd_strings_levenshtein_distance (cmd, clen,
+                       input, inplen, 1) == 1) {
+               return TRUE;
+       }
+       else if ((clen > inplen &&
+                         rspamd_substring_search (cmd, clen, input, inplen) != -1) ||
+                        (inplen > clen &&
+                         rspamd_substring_search (input, inplen, cmd, clen) != -1)) {
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
 gint
 main (gint argc, gchar **argv, gchar **env)
 {
@@ -427,20 +449,18 @@ main (gint argc, gchar **argv, gchar **env)
                /* Try fuzz search */
                rspamd_fprintf (stderr, "Suggested commands:\n");
                PTR_ARRAY_FOREACH (all_commands, i, cmd) {
-                       gsize clen, inplen;
+                       guint j;
+                       const gchar *alias;
 
-                       clen = strlen (cmd->name);
-                       inplen = strlen (cmd_name);
-
-                       if (rspamd_strings_levenshtein_distance (cmd->name, clen,
-                                       cmd_name, inplen, 1) == 1) {
+                       if (rspamadm_command_maybe_match_name (cmd->name, cmd_name)) {
                                rspamd_fprintf (stderr, "%s\n", cmd->name);
                        }
-                       else if ((clen > inplen &&
-                                         rspamd_substring_search (cmd->name, clen, cmd_name, inplen) != -1) ||
-                                        (inplen > clen &&
-                                         rspamd_substring_search (cmd_name, inplen, cmd->name, clen) != -1)) {
-                               rspamd_fprintf (stderr, "%s\n", cmd->name);
+                       else {
+                               PTR_ARRAY_FOREACH (cmd->aliases, j, alias) {
+                                       if (rspamadm_command_maybe_match_name (alias, cmd_name)) {
+                                               rspamd_fprintf (stderr, "%s\n", alias);
+                                       }
+                               }
                        }
                }
 
index 25f512595aec5bf44a30e39cc631f8a9dd333025..3d9799dd5820cc6562f1f81a044a854652042a2f 100644 (file)
@@ -43,6 +43,7 @@ struct rspamadm_command {
        rspamadm_help_func help;
        rspamadm_run_func run;
        rspamadm_lua_exports_func lua_subrs;
+       GPtrArray *aliases;
        gpointer command_data; /* Opaque data */
 };