aboutsummaryrefslogtreecommitdiffstats
path: root/src/rspamadm/lua_repl.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-10-18 16:06:31 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-10-18 16:06:31 +0100
commitfa9c32770a7229abad55ad63cfc01cc7b3f76e9b (patch)
tree117841b85701bc5549b51972c36abf2719eb8569 /src/rspamadm/lua_repl.c
parent6fd9dcb0d8419b07460a174c6bd7baf78f6f3ebe (diff)
downloadrspamd-fa9c32770a7229abad55ad63cfc01cc7b3f76e9b.tar.gz
rspamd-fa9c32770a7229abad55ad63cfc01cc7b3f76e9b.zip
[Feature] Allow to run rspamadm lua just as a lua interpreter
Diffstat (limited to 'src/rspamadm/lua_repl.c')
-rw-r--r--src/rspamadm/lua_repl.c72
1 files changed, 68 insertions, 4 deletions
diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c
index a7c598be0..ce764c7f8 100644
--- a/src/rspamadm/lua_repl.c
+++ b/src/rspamadm/lua_repl.c
@@ -35,6 +35,7 @@ static gchar **scripts = NULL;
static gchar *histfile = NULL;
static guint max_history = 2000;
static gchar *serve = NULL;
+static gint batch = -1;
static const char *default_history_file = ".rspamd_repl.hist";
@@ -67,6 +68,7 @@ struct rspamadm_lua_dot_command {
static void rspamadm_lua_help_handler (lua_State *L, gint argc, gchar **argv);
static void rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv);
+static void rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv);
static void rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv);
static struct rspamadm_lua_dot_command cmds[] = {
@@ -81,6 +83,11 @@ static struct rspamadm_lua_dot_command cmds[] = {
.handler = rspamadm_lua_load_handler
},
{
+ .name = "exec",
+ .description = "exec lua file",
+ .handler = rspamadm_lua_exec_handler
+ },
+ {
.name = "message",
.description = "scans message using specified callback: .message <callback_name> <file>...",
.handler = rspamadm_lua_message_handler
@@ -100,6 +107,8 @@ static GOptionEntry entries[] = {
"Store this number of history entries", NULL},
{"serve", 'S', 0, G_OPTION_ARG_STRING, &serve,
"Serve http lua server", NULL},
+ {"batch", 'b', 0, G_OPTION_ARG_NONE, &batch,
+ "Batch execution mode", NULL},
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
};
@@ -279,6 +288,37 @@ rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv)
}
static void
+rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv)
+{
+ GString *tb;
+ gint err_idx, i;
+
+ for (i = 1; argv[i] != NULL; i ++) {
+ lua_pushcfunction (L, &rspamd_lua_traceback);
+ err_idx = lua_gettop (L);
+
+ if (luaL_loadfile (L, argv[i]) != 0) {
+ rspamd_fprintf (stderr, "cannot load script %s: %s\n",
+ argv[i], strerror (errno));
+ lua_settop (L, 0);
+
+ return;
+ }
+
+ if (lua_pcall (L, 0, 0, err_idx) != 0) {
+ tb = lua_touserdata (L, -1);
+ rspamd_fprintf (stderr, "call to %s failed: %v", argv[i], tb);
+ g_string_free (tb, TRUE);
+ lua_settop (L, 0);
+
+ return;
+ }
+
+ lua_settop (L, 0);
+ }
+}
+
+static void
rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv)
{
gulong cbref;
@@ -624,6 +664,15 @@ rspamadm_lua (gint argc, gchar **argv)
exit (1);
}
+ if (batch == -1) {
+ if (isatty (STDIN_FILENO)) {
+ batch = 0;
+ }
+ else {
+ batch = 1;
+ }
+ }
+
L = rspamd_lua_init ();
rspamd_lua_set_path (L, NULL);
@@ -711,6 +760,16 @@ rspamadm_lua (gint argc, gchar **argv)
g_string_free (hist_path, FALSE);
}
+ if (argc > 1) {
+ for (i = 1; i < argc; i ++) {
+ if (!rspamadm_lua_load_script (L, argv[i])) {
+ exit (EXIT_FAILURE);
+ }
+ }
+
+ exit (EXIT_SUCCESS);
+ }
+
/* Init dot commands */
cmds_hash = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
@@ -718,8 +777,13 @@ rspamadm_lua (gint argc, gchar **argv)
g_hash_table_insert (cmds_hash, (gpointer)cmds[i].name, &cmds[i]);
}
- linenoiseHistorySetMaxLen (max_history);
- linenoiseHistoryLoad (histfile);
- rspamadm_lua_run_repl (L);
- linenoiseHistorySave (histfile);
+ if (!batch) {
+ linenoiseHistorySetMaxLen (max_history);
+ linenoiseHistoryLoad (histfile);
+ rspamadm_lua_run_repl (L);
+ linenoiseHistorySave (histfile);
+ }
+ else {
+ rspamadm_lua_run_repl (L);
+ }
}