aboutsummaryrefslogtreecommitdiffstats
path: root/src/rspamadm
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-05-29 13:59:54 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-05-29 13:59:54 +0100
commit4dfc2e53174ccfa28a92d60401d75bde2904e205 (patch)
tree1dec7adfc3f066189268c545c4cac37b485a2462 /src/rspamadm
parent680fd4843c6e0d2b7c33c56828573d09388be420 (diff)
downloadrspamd-4dfc2e53174ccfa28a92d60401d75bde2904e205.tar.gz
rspamd-4dfc2e53174ccfa28a92d60401d75bde2904e205.zip
[Minor] Fix various issues
Diffstat (limited to 'src/rspamadm')
-rw-r--r--src/rspamadm/CMakeLists.txt1
-rw-r--r--src/rspamadm/commands.c72
-rw-r--r--src/rspamadm/rspamadm.c34
-rw-r--r--src/rspamadm/rspamadm.h2
4 files changed, 62 insertions, 47 deletions
diff --git a/src/rspamadm/CMakeLists.txt b/src/rspamadm/CMakeLists.txt
index fb3f25229..1370fde89 100644
--- a/src/rspamadm/CMakeLists.txt
+++ b/src/rspamadm/CMakeLists.txt
@@ -5,7 +5,6 @@ SET(RSPAMADMSRC rspamadm.c
configtest.c
fuzzy_convert.c
fuzzy_merge.c
- grep.c
configdump.c
control.c
confighelp.c
diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c
index 744212afb..d787713f6 100644
--- a/src/rspamadm/commands.c
+++ b/src/rspamadm/commands.c
@@ -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);
}
}
}
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c
index 7c96ae3ed..a804d0059 100644
--- a/src/rspamadm/rspamadm.c
+++ b/src/rspamadm/rspamadm.c
@@ -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);
diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h
index 3d0a69204..25f512595 100644
--- a/src/rspamadm/rspamadm.h
+++ b/src/rspamadm/rspamadm.h
@@ -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;