mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
authorMatthew Wild <mwild1@gmail.com>
Mon, 01 Jun 2020 16:14:06 +0100
changeset 10863 8de0057b4279
parent 10862 efa49d484560
child 10864 934dca972f2c
mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result Fixes the client pausing for input after output from commands.
plugins/mod_admin_shell.lua
plugins/mod_admin_telnet.lua
util/prosodyctl/shell.lua
--- a/plugins/mod_admin_shell.lua	Mon Jun 01 15:44:44 2020 +0100
+++ b/plugins/mod_admin_shell.lua	Mon Jun 01 16:14:06 2020 +0100
@@ -61,21 +61,21 @@
 	self.data.print("Error: "..tostring(err));
 end
 
-local function send_repl_result(session, line)
-	return session.send(st.stanza("repl-result"):text(tostring(line)));
+local function send_repl_output(session, line)
+	return session.send(st.stanza("repl-output"):text(tostring(line)));
 end
 
 function console:new_session(admin_session)
 	local session = {
 			send = function (t)
-				return send_repl_result(admin_session, t);
+				return send_repl_output(admin_session, t);
 			end;
 			print = function (...)
 				local t = {};
 				for i=1,select("#", ...) do
 					t[i] = tostring(select(i, ...));
 				end
-				return send_repl_result(admin_session, table.concat(t, "\t"));
+				return send_repl_output(admin_session, table.concat(t, "\t"));
 			end;
 			serialize = tostring;
 			disconnect = function () admin_session:close(); end;
@@ -107,8 +107,8 @@
 	local line = event.stanza:get_text();
 	local useglobalenv;
 
+	local result = st.stanza("repl-result");
 
-		module:log("debug", "HELLO: %s", line)
 	if line:match("^>") then
 		line = line:gsub("^>", "");
 		useglobalenv = true;
@@ -116,6 +116,7 @@
 		local command = line:match("^%w+") or line:match("%p");
 		if commands[command] then
 			commands[command](session, line);
+			event.origin.send(result);
 			return;
 		end
 	end
@@ -124,6 +125,7 @@
 
 	if not useglobalenv and commands[line:lower()] then
 		commands[line:lower()](session, line);
+		event.origin.send(result);
 		return;
 	end
 
@@ -137,29 +139,33 @@
 			err = err:gsub("^%[string .-%]:%d+: ", "");
 			err = err:gsub("^:%d+: ", "");
 			err = err:gsub("'<eof>'", "the end of the line");
-			session.print("Sorry, I couldn't understand that... "..err);
+			result.attr.type = "error";
+			result:text("Sorry, I couldn't understand that... "..err);
+			event.origin.send(result);
 			return;
 		end
 	end
 
 	local taskok, message = chunk();
 
+	local result = st.stanza("repl-result");
+
 	if not message then
 		if type(taskok) ~= "string" and useglobalenv then
 			taskok = session.serialize(taskok);
 		end
-		session.print("Result: "..tostring(taskok));
-		return;
+		result:text("Result: "..tostring(taskok));
 	elseif (not taskok) and message then
-		session.print("Command completed with a problem");
-		session.print("Message: "..tostring(message));
-		return;
+		result.attr.type = "error";
+		result:text("Error: "..tostring(message));
+	else
+		result:text("OK: "..tostring(message));
 	end
 
-	session.print("OK: "..tostring(message));
+	event.origin.send(result);
 end
 
-module:hook("admin/repl-line", function (event)
+module:hook("admin/repl-input", function (event)
 	local ok, err = pcall(handle_line, event);
 	if not ok then
 		event.origin.send(st.stanza("repl-result", { type = "error" }):text(err));
--- a/plugins/mod_admin_telnet.lua	Mon Jun 01 15:44:44 2020 +0100
+++ b/plugins/mod_admin_telnet.lua	Mon Jun 01 16:14:06 2020 +0100
@@ -65,7 +65,7 @@
 	local w = function(s) conn:write(s:gsub("\n", "\r\n")); end;
 	local session = { conn = conn;
 			send = function (t)
-				if st.is_stanza(t) and t.name == "repl-result" then
+				if st.is_stanza(t) and (t.name == "repl-result" or t.name == "repl-output") then
 					t = "| "..t:get_text().."\n";
 				end
 				w(tostring(t));
@@ -106,7 +106,7 @@
 		session:disconnect();
 		return;
 	end
-	return module:fire_event("admin/repl-line", { origin = session, stanza = st.stanza("repl"):text(line) });
+	return module:fire_event("admin/repl-input", { origin = session, stanza = st.stanza("repl-input"):text(line) });
 end
 
 local sessions = {};
--- a/util/prosodyctl/shell.lua	Mon Jun 01 15:44:44 2020 +0100
+++ b/util/prosodyctl/shell.lua	Mon Jun 01 16:14:06 2020 +0100
@@ -27,7 +27,7 @@
 end
 
 local function send_line(client, line)
-	client.send(st.stanza("repl-line"):text(line));
+	client.send(st.stanza("repl-input"):text(line));
 end
 
 local function repl(client)
@@ -103,9 +103,11 @@
 	end);
 
 	client.events.add_handler("received", function (stanza)
-		if stanza.name == "repl-result" then
+		if stanza.name == "repl-output" or stanza.name == "repl-result" then
 			local result_prefix = stanza.attr.type == "error" and "!" or "|";
 			print(result_prefix.." "..stanza:get_text());
+		end
+		if stanza.name == "repl-result" then
 			repl(client);
 		end
 	end);