util/prosodyctl/shell.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 24 Apr 2024 11:45:37 +0100
changeset 13487 7b070909bd15
parent 13402 4ba11c2b915a
permissions -rw-r--r--
prosodyctl shell: Fix invocation with 3+ command arguments The code correctly inserted the ',' when there was already a "%q" in the format string, but then the next argument would fail to match because it inserted ", %q" instead of "%q". The code now matches both, ensuring the generated code will not produce a syntax error with multiple arguments.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     1
local config = require "prosody.core.configmanager";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     2
local server = require "prosody.net.server";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     3
local st = require "prosody.util.stanza";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     4
local path = require "prosody.util.paths";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     5
local parse_args = require "prosody.util.argparse".parse;
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     6
local tc = require "prosody.util.termcolours";
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
     7
local isatty = require "prosody.util.pposix".isatty;
13050
4c3dc767fb11 util.prosodyctl.shell: Use new term_width() for width
Kim Alvefur <zash@zash.se>
parents: 12979
diff changeset
     8
local term_width = require"prosody.util.human.io".term_width;
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local have_readline, readline = pcall(require, "readline");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12868
diff changeset
    12
local adminstream = require "prosody.util.adminstream";
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
if have_readline then
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
	readline.set_readline_name("prosody");
10881
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    16
	readline.set_options({
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    17
			histfile = path.join(prosody.paths.data, ".shell_history");
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    18
			ignoredups = true;
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    19
		});
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
    22
local function read_line(prompt_string)
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	if have_readline then
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
    24
		return readline.readline(prompt_string);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	else
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
    26
		io.write(prompt_string);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
		return io.read("*line");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
local function send_line(client, line)
13054
4ae759490e31 util.prosodyctl.shell: Coerce terminal width to string (for util.stanza)
Kim Alvefur <zash@zash.se>
parents: 13051
diff changeset
    32
	client.send(st.stanza("repl-input", { width = tostring(term_width()) }):text(line));
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
local function repl(client)
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
    36
	local line = read_line(client.prompt_string or "prosody> ");
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	if not line or line == "quit" or line == "exit" or line == "bye" then
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
		if not line then
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
			print("");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		end
10881
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    41
		if have_readline then
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    42
			readline.save_history();
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    43
		end
12866
3dfb87814d65 util.prosodyctl.shell: Close state on exit to fix saving shell history
Kim Alvefur <zash@zash.se>
parents: 11906
diff changeset
    44
		os.exit(0, true);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	send_line(client, line);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
local function printbanner()
11906
98fd531594bd util.prosodyctl.shell: Bring back banner set from config!
Kim Alvefur <zash@zash.se>
parents: 11894
diff changeset
    50
	local banner = config.get("*", "console_banner");
98fd531594bd util.prosodyctl.shell: Bring back banner set from config!
Kim Alvefur <zash@zash.se>
parents: 11894
diff changeset
    51
	if banner then return print(banner); end
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	print([[
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
                     ____                \   /     _
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
                    |  _ \ _ __ ___  ___  _-_   __| |_   _
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
                    | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
                    |  __/| | | (_) \__ \ |_| | (_| | |_| |
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
                    |_|   |_|  \___/|___/\___/ \__,_|\__, |
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
                    A study in simplicity            |___/
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
]]);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
	print("Welcome to the Prosody administration console. For a list of commands, type: help");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	print("You may find more help on using this console in our online documentation at ");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	print("https://prosody.im/doc/console\n");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
local function start(arg) --luacheck: ignore 212/arg
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
	local client = adminstream.client();
10942
a5b8ae066688 util.prosodyctl.shell: Collect extra return values
Kim Alvefur <zash@zash.se>
parents: 10941
diff changeset
    68
	local opts, err, where = parse_args(arg);
12543
cfdc8cca64d3 util.prosodyctl.shell: Print errors in red to highlight them
Kim Alvefur <zash@zash.se>
parents: 12533
diff changeset
    69
	local ttyout = isatty(io.stdout);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
10941
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    71
	if not opts then
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    72
		if err == "param-not-found" then
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    73
			print("Unknown command-line option: "..tostring(where));
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    74
		elseif err == "missing-value" then
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    75
			print("Expected a value to follow command-line option: "..where);
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    76
		end
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    77
		os.exit(1);
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    78
	end
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    79
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    80
	if arg[1] then
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    81
		if arg[2] then
13400
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    82
			local fmt = { "%s"; ":%s("; ")" };
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    83
			for i = 3, #arg do
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    84
				if arg[i]:sub(1, 1) == ":" then
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    85
					table.insert(fmt, i, ")%s(");
13487
7b070909bd15 prosodyctl shell: Fix invocation with 3+ command arguments
Matthew Wild <mwild1@gmail.com>
parents: 13402
diff changeset
    86
				elseif i > 3 and fmt[i - 1]:match("%%q$") then
13400
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    87
					table.insert(fmt, i, ", %q");
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    88
				else
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    89
					table.insert(fmt, i, "%q");
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    90
				end
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    91
			end
16413bc29361 util.prosodyctl.shell: Add :method syntax to make e.g. MUC commands easier
Kim Alvefur <zash@zash.se>
parents: 13056
diff changeset
    92
			arg[1] = string.format(table.concat(fmt), table.unpack(arg));
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    93
		end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    94
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    95
		client.events.add_handler("connected", function()
13056
585bd71a1a97 util.prosodyctl.shell: Fix sending terminal width with single argument
Kim Alvefur <zash@zash.se>
parents: 13054
diff changeset
    96
			send_line(client, arg[1]);
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    97
			return true;
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    98
		end, 1);
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    99
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   100
		local errors = 0; -- TODO This is weird, but works for now.
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   101
		client.events.add_handler("received", function(stanza)
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   102
			if stanza.name == "repl-output" or stanza.name == "repl-result" then
12400
bdb9577a4830 util.prosodyctl.shell: Support for receiving partial lines (no automatic \n)
Matthew Wild <mwild1@gmail.com>
parents: 11906
diff changeset
   103
				local dest = io.stdout;
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   104
				if stanza.attr.type == "error" then
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   105
					errors = errors + 1;
12400
bdb9577a4830 util.prosodyctl.shell: Support for receiving partial lines (no automatic \n)
Matthew Wild <mwild1@gmail.com>
parents: 11906
diff changeset
   106
					dest = io.stderr;
bdb9577a4830 util.prosodyctl.shell: Support for receiving partial lines (no automatic \n)
Matthew Wild <mwild1@gmail.com>
parents: 11906
diff changeset
   107
				end
bdb9577a4830 util.prosodyctl.shell: Support for receiving partial lines (no automatic \n)
Matthew Wild <mwild1@gmail.com>
parents: 11906
diff changeset
   108
				if stanza.attr.eol == "0" then
bdb9577a4830 util.prosodyctl.shell: Support for receiving partial lines (no automatic \n)
Matthew Wild <mwild1@gmail.com>
parents: 11906
diff changeset
   109
					dest:write(stanza:get_text());
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   110
				else
12400
bdb9577a4830 util.prosodyctl.shell: Support for receiving partial lines (no automatic \n)
Matthew Wild <mwild1@gmail.com>
parents: 11906
diff changeset
   111
					dest:write(stanza:get_text(), "\n");
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   112
				end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   113
			end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   114
			if stanza.name == "repl-result" then
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   115
				os.exit(errors);
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   116
			end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   117
			return true;
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   118
		end, 1);
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   119
	end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   120
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
	client.events.add_handler("connected", function ()
11426
fa5a23d7aabc util.prosodyctl.shell: Fix check for --quiet
Kim Alvefur <zash@zash.se>
parents: 10942
diff changeset
   122
		if not opts.quiet then
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
			printbanner();
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
		end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
		repl(client);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
	end);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
	client.events.add_handler("disconnected", function ()
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
		print("--- session closed ---");
12866
3dfb87814d65 util.prosodyctl.shell: Close state on exit to fix saving shell history
Kim Alvefur <zash@zash.se>
parents: 11906
diff changeset
   130
		os.exit(0, true);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
	end);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
	client.events.add_handler("received", function (stanza)
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10862
diff changeset
   134
		if stanza.name == "repl-output" or stanza.name == "repl-result" then
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
			local result_prefix = stanza.attr.type == "error" and "!" or "|";
12543
cfdc8cca64d3 util.prosodyctl.shell: Print errors in red to highlight them
Kim Alvefur <zash@zash.se>
parents: 12533
diff changeset
   136
			local out = result_prefix.." "..stanza:get_text();
cfdc8cca64d3 util.prosodyctl.shell: Print errors in red to highlight them
Kim Alvefur <zash@zash.se>
parents: 12533
diff changeset
   137
			if ttyout and stanza.attr.type == "error" then
cfdc8cca64d3 util.prosodyctl.shell: Print errors in red to highlight them
Kim Alvefur <zash@zash.se>
parents: 12533
diff changeset
   138
				out = tc.getstring(tc.getstyle("red"), out);
cfdc8cca64d3 util.prosodyctl.shell: Print errors in red to highlight them
Kim Alvefur <zash@zash.se>
parents: 12533
diff changeset
   139
			end
cfdc8cca64d3 util.prosodyctl.shell: Print errors in red to highlight them
Kim Alvefur <zash@zash.se>
parents: 12533
diff changeset
   140
			print(out);
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10862
diff changeset
   141
		end
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10862
diff changeset
   142
		if stanza.name == "repl-result" then
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
			repl(client);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
		end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
	end);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
   147
	client.prompt_string = config.get("*", "admin_shell_prompt");
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
   148
10878
98c535531450 util.prosodyctl.shell: Really fix --socket option
Kim Alvefur <zash@zash.se>
parents: 10877
diff changeset
   149
	local socket_path = path.resolve_relative_path(prosody.paths.data, opts.socket or config.get("*", "admin_socket") or "prosody.sock");
10879
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   150
	local conn = adminstream.connection(socket_path, client.listeners);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
	local ok, err = conn:connect();
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
	if not ok then
10879
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   153
		if err == "no unix socket support" then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   154
			print("** LuaSocket unix socket support not available or incompatible, ensure your");
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   155
			print("** version is up to date.");
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   156
		else
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   157
			print("** Unable to connect to server - is it running? Is mod_admin_shell enabled?");
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   158
			print("** Connection error: "..err);
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   159
		end
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
		os.exit(1);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
	end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
	server.loop();
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
return {
10875
e5dee71d0ebb prosodyctl+util.prosodyctl.*: Start breaking up the ever-growing prosodyctl
Matthew Wild <mwild1@gmail.com>
parents: 10873
diff changeset
   166
	shell = start;
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
};