util/prosodyctl/shell.lua
author Jonas Schäfer <jonas@wielicki.name>
Sun, 26 Dec 2021 22:32:00 +0100
changeset 12127 7ba686696250
parent 11906 98fd531594bd
child 12400 bdb9577a4830
child 12866 3dfb87814d65
permissions -rw-r--r--
openmetrics/histograms: fix incorrect condition for bucketing The buckets thresholds are to be taken as "less than or equal to". The condition as written in the code did only "less than", not "less than or equal to". That's fixed now.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10872
fa06cf7059cd util.prosodyctl.shell: Use same config option as module for socket path
Kim Alvefur <zash@zash.se>
parents: 10871
diff changeset
     1
local config = require "core.configmanager";
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local server = require "net.server";
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local st = require "util.stanza";
10871
561138169983 util.prosodyctl.shell: Join socket path with current data directory
Kim Alvefur <zash@zash.se>
parents: 10863
diff changeset
     4
local path = require "util.paths";
10878
98c535531450 util.prosodyctl.shell: Really fix --socket option
Kim Alvefur <zash@zash.se>
parents: 10877
diff changeset
     5
local parse_args = require "util.argparse".parse;
11526
5bd38d9197e1 util.prosodyctl.shell: Fix for different location of unpack in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11525
diff changeset
     6
local unpack = table.unpack or _G.unpack;
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
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
     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 adminstream = require "util.adminstream";
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
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
    13
	readline.set_readline_name("prosody");
10881
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    14
	readline.set_options({
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    15
			histfile = path.join(prosody.paths.data, ".shell_history");
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    16
			ignoredups = true;
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    17
		});
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
    20
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
    21
	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
    22
		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
    23
	else
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
    24
		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
    25
		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
    26
	end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local function send_line(client, line)
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
    30
	client.send(st.stanza("repl-input"):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
    31
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
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
    34
	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
    35
	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
    36
		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
    37
			print("");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
		end
10881
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    39
		if have_readline then
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    40
			readline.save_history();
2b015ef8cd06 util.prosodyctl.shell: Save readline history
Kim Alvefur <zash@zash.se>
parents: 10879
diff changeset
    41
		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
    42
		os.exit();
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
	end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	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
    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
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
local function printbanner()
11906
98fd531594bd util.prosodyctl.shell: Bring back banner set from config!
Kim Alvefur <zash@zash.se>
parents: 11894
diff changeset
    48
	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
    49
	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
    50
	print([[
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
                     ____                \   /     _
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
                    |  _ \ _ __ ___  ___  _-_   __| |_   _
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
                    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
    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
]]);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
	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
    60
	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
    61
	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
    62
end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
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
    65
	local client = adminstream.client();
10942
a5b8ae066688 util.prosodyctl.shell: Collect extra return values
Kim Alvefur <zash@zash.se>
parents: 10941
diff changeset
    66
	local opts, err, where = parse_args(arg);
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
10941
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    68
	if not opts then
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    69
		if err == "param-not-found" then
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    70
			print("Unknown command-line option: "..tostring(where));
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    71
		elseif err == "missing-value" then
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    72
			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
    73
		end
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    74
		os.exit(1);
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    75
	end
d86f59c31458 util.prosodyctl.shell: Handle argument parsing errors
Kim Alvefur <zash@zash.se>
parents: 10881
diff changeset
    76
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    77
	if arg[1] then
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    78
		if arg[2] then
11524
0a464a6e88c4 util.prosodyctl.shell: Allow calling console commands with fewer shell quotes
Kim Alvefur <zash@zash.se>
parents: 11427
diff changeset
    79
			-- prosodyctl shell module reload foo bar.com --> module:reload("foo", "bar.com")
11525
16caf35f031f util.prosodyctl.shell: Fix for missing 'sep' arg to string.rep in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11524
diff changeset
    80
			-- COMPAT Lua 5.1 doesn't have the separator argument to string.rep
11526
5bd38d9197e1 util.prosodyctl.shell: Fix for different location of unpack in Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11525
diff changeset
    81
			arg[1] = string.format("%s:%s("..string.rep("%q, ", #arg-2):sub(1, -3)..")", unpack(arg));
11427
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    82
		end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    83
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    84
		client.events.add_handler("connected", function()
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    85
			client.send(st.stanza("repl-input"):text(arg[1]));
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    86
			return true;
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    87
		end, 1);
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    88
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    89
		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
    90
		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
    91
			if stanza.name == "repl-output" or 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
    92
				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
    93
					errors = errors + 1;
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    94
					io.stderr:write(stanza:get_text(), "\n");
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    95
				else
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    96
					print(stanza:get_text());
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    97
				end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    98
			end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
    99
			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
   100
				os.exit(errors);
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   101
			end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   102
			return true;
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   103
		end, 1);
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   104
	end
0a10bb3b129b util.prosodyctl.shell: Allow passing a single command as argument
Kim Alvefur <zash@zash.se>
parents: 11426
diff changeset
   105
10862
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
	client.events.add_handler("connected", function ()
11426
fa5a23d7aabc util.prosodyctl.shell: Fix check for --quiet
Kim Alvefur <zash@zash.se>
parents: 10942
diff changeset
   107
		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
   108
			printbanner();
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
		end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
		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
   111
	end);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
	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
   114
		print("--- session closed ---");
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
		os.exit();
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
	end);
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
	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
   119
		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
   120
			local result_prefix = stanza.attr.type == "error" and "!" or "|";
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
			print(result_prefix.." "..stanza:get_text());
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
   122
		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
   123
		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
   124
			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
   125
		end
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
11894
b9aab1962a2b util.prosodyctl.shell: Allow setting custom prompt (admin_shell_prompt)
Matthew Wild <mwild1@gmail.com>
parents: 11526
diff changeset
   128
	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
   129
10878
98c535531450 util.prosodyctl.shell: Really fix --socket option
Kim Alvefur <zash@zash.se>
parents: 10877
diff changeset
   130
	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
   131
	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
   132
	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
   133
	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
   134
		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
   135
			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
   136
			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
   137
		else
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10878
diff changeset
   138
			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
   139
			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
   140
		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
   141
		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
   142
	end
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
	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
   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
efa49d484560 prosodyctl, util.prosodyctl.shell: `prosodyctl shell` - a client to access the prosodyctl admin shell
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
return {
10875
e5dee71d0ebb prosodyctl+util.prosodyctl.*: Start breaking up the ever-growing prosodyctl
Matthew Wild <mwild1@gmail.com>
parents: 10873
diff changeset
   147
	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
   148
};