plugins/mod_admin_shell.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 17 Mar 2024 10:10:24 +0000
changeset 13464 a688947fab1e
parent 13358 83f3076965f6
child 13477 9eb081616842
permissions -rw-r--r--
mod_bosh: Set base_type on session This fixes a traceback with mod_saslauth. Ideally we move this to util.session at some point, though.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
-- Prosody IM
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
--
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
--
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
-- luacheck: ignore 212/self
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
module:set_global();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
module:depends("admin_socket");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    13
local hostmanager = require "prosody.core.hostmanager";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    14
local modulemanager = require "prosody.core.modulemanager";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    15
local s2smanager = require "prosody.core.s2smanager";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    16
local portmanager = require "prosody.core.portmanager";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    17
local helpers = require "prosody.util.helpers";
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    18
local it = require "prosody.util.iterators";
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    19
local server = require "prosody.net.server";
13354
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
    20
local schema = require "prosody.util.jsonschema";
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    21
local st = require "prosody.util.stanza";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
local _G = _G;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
local prosody = _G.prosody;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
12593
39ae08180c81 compat: Remove handling of Lua 5.1 location of 'unpack' function
Kim Alvefur <zash@zash.se>
parents: 12555
diff changeset
    27
local unpack = table.unpack;
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    28
local iterators = require "prosody.util.iterators";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local keys, values = iterators.keys, iterators.values;
13017
430333198e4c mod_admin_shell: Allow matching on host or bare JID in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12997
diff changeset
    30
local jid_bare, jid_split, jid_join, jid_resource, jid_compare = import("prosody.util.jid", "bare", "prepped_split", "join", "resource", "compare");
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    31
local set, array = require "prosody.util.set", require "prosody.util.array";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    32
local cert_verify_identity = require "prosody.util.x509".verify_identity;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    33
local envload = require "prosody.util.envload".envload;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    34
local envloadfile = require "prosody.util.envload".envloadfile;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    35
local has_pposix, pposix = pcall(require, "prosody.util.pposix");
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    36
local async = require "prosody.util.async";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    37
local serialization = require "prosody.util.serialization";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
local serialize_config = serialization.new ({ fatal = false, unquoted = true});
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    39
local time = require "prosody.util.time";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    40
local promise = require "prosody.util.promise";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    41
local logger = require "prosody.util.logger";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
    43
local t_insert = table.insert;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
    44
local t_concat = table.concat;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
    45
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    46
local format_number = require "prosody.util.human.units".format;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
    47
local format_table = require "prosody.util.human.io".table;
10891
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
    48
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
    49
local function capitalize(s)
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
    50
	if not s then return end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
    51
	return (s:gsub("^%a", string.upper):gsub("_", " "));
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
    52
end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
    53
11935
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    54
local function pre(prefix, str, alt)
12509
604bb5b8362d mod_admin_shell: Tighten up type checks to fix #1754 (thanks clouded)
Kim Alvefur <zash@zash.se>
parents: 12308
diff changeset
    55
	if type(str) ~= "string" or str == "" then return alt or ""; end
11935
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    56
	return prefix .. str;
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    57
end
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    58
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    59
local function suf(str, suffix, alt)
12509
604bb5b8362d mod_admin_shell: Tighten up type checks to fix #1754 (thanks clouded)
Kim Alvefur <zash@zash.se>
parents: 12308
diff changeset
    60
	if type(str) ~= "string" or str == "" then return alt or ""; end
11935
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    61
	return str .. suffix;
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    62
end
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
    63
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
local commands = module:shared("commands")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
local def_env = module:shared("env");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
local default_env_mt = { __index = def_env };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    68
local function new_section(section_desc)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    69
	return setmetatable({}, {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    70
		help = {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    71
			desc = section_desc;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    72
			commands = {};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    73
		};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    74
	});
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    75
end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    76
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    77
local help_topics = {};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    78
local function help_topic(name)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    79
	return function (desc)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    80
		return function (content)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    81
			help_topics[name] = {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    82
				desc = desc;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    83
				content = content;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    84
			};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    85
		end;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    86
	end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    87
end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    88
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    89
-- Seed with default sections and their description text
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    90
help_topic "console" "Help regarding the console itself" [[
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    91
Hey! Welcome to Prosody's admin console.
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    92
First thing, if you're ever wondering how to get out, simply type 'quit'.
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    93
Secondly, note that we don't support the full telnet protocol yet (it's coming)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    94
so you may have trouble using the arrow keys, etc. depending on your system.
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    95
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    96
For now we offer a couple of handy shortcuts:
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    97
!! - Repeat the last command
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    98
!old!new! - repeat the last command, but with 'old' replaced by 'new'
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
    99
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   100
For those well-versed in Prosody's internals, or taking instruction from those who are,
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   101
you can prefix a command with > to escape the console sandbox, and access everything in
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   102
the running server. Great fun, but be careful not to break anything :)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   103
]];
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   104
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   105
local available_columns; --forward declaration so it is reachable from the help
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   106
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   107
help_topic "columns" "Information about customizing session listings" (function (self, print)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   108
	print [[The columns shown by c2s:show() and s2s:show() can be customizied via the]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   109
	print [['columns' argument as described here.]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   110
	print [[]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   111
	print [[Columns can be specified either as "id jid ipv" or as {"id", "jid", "ipv"}.]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   112
	print [[Available columns are:]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   113
	local meta_columns = {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   114
		{ title = "ID"; width = 5 };
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   115
		{ title = "Column Title"; width = 12 };
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   116
		{ title = "Description"; width = 12 };
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   117
	};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   118
	-- auto-adjust widths
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   119
	for column, spec in pairs(available_columns) do
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   120
		meta_columns[1].width = math.max(meta_columns[1].width or 0, #column);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   121
		meta_columns[2].width = math.max(meta_columns[2].width or 0, #(spec.title or ""));
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   122
		meta_columns[3].width = math.max(meta_columns[3].width or 0, #(spec.description or ""));
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   123
	end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   124
	local row = format_table(meta_columns, self.session.width)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   125
	print(row());
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   126
	for column, spec in iterators.sorted_pairs(available_columns) do
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   127
		print(row({ column, spec.title, spec.description }));
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   128
	end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   129
	print [[]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   130
	print [[Most fields on the internal session structures can also be used as columns]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   131
	-- Also, you can pass a table column specification directly, with mapper callback and all
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   132
end);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   133
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   134
help_topic "roles"   "Show information about user roles" [[
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   135
Roles may grant access or restrict users from certain operations.
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   136
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   137
Built-in roles are:
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   138
  prosody:guest      - Guest/anonymous user
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   139
  prosody:registered - Registered user
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   140
  prosody:member     - Provisioned user
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   141
  prosody:admin      - Host administrator
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   142
  prosody:operator - Server administrator
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   143
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   144
Roles can be assigned using the user management commands (see 'help user').
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   145
]];
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   146
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   147
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
local function redirect_output(target, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
	local env = setmetatable({ print = session.print }, { __index = function (_, k) return rawget(target, k); end });
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
	env.dofile = function(name)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
		local f, err = envloadfile(name, env);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
		if not f then return f, err; end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
		return f();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
	end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
	return env;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
console = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
local runner_callbacks = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
function runner_callbacks:error(err)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
	module:log("error", "Traceback[shell]: %s", err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
	self.data.print("Fatal error while running command, it did not complete");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
	self.data.print("Error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
12401
ddf02f2a3354 mod_admin_shell: Add session.write() method to write data to client with no \n
Matthew Wild <mwild1@gmail.com>
parents: 12308
diff changeset
   169
local function send_repl_output(session, line, attr)
ddf02f2a3354 mod_admin_shell: Add session.write() method to write data to client with no \n
Matthew Wild <mwild1@gmail.com>
parents: 12308
diff changeset
   170
	return session.send(st.stanza("repl-output", attr):text(tostring(line)));
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
function console:new_session(admin_session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
	local session = {
11954
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   175
		send = function (t)
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   176
			return send_repl_output(admin_session, t);
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   177
		end;
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   178
		print = function (...)
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   179
			local t = {};
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   180
			for i=1,select("#", ...) do
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   181
				t[i] = tostring(select(i, ...));
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   182
			end
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   183
			return send_repl_output(admin_session, table.concat(t, "\t"));
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   184
		end;
12401
ddf02f2a3354 mod_admin_shell: Add session.write() method to write data to client with no \n
Matthew Wild <mwild1@gmail.com>
parents: 12308
diff changeset
   185
		write = function (t)
ddf02f2a3354 mod_admin_shell: Add session.write() method to write data to client with no \n
Matthew Wild <mwild1@gmail.com>
parents: 12308
diff changeset
   186
			return send_repl_output(admin_session, t, { eol = "0" });
ddf02f2a3354 mod_admin_shell: Add session.write() method to write data to client with no \n
Matthew Wild <mwild1@gmail.com>
parents: 12308
diff changeset
   187
		end;
11954
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   188
		serialize = tostring;
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   189
		disconnect = function () admin_session:close(); end;
12402
478fff93ac37 mod_admin_shell: Add session.is_connected() method
Matthew Wild <mwild1@gmail.com>
parents: 12401
diff changeset
   190
		is_connected = function ()
478fff93ac37 mod_admin_shell: Add session.is_connected() method
Matthew Wild <mwild1@gmail.com>
parents: 12401
diff changeset
   191
			return not not admin_session.conn;
478fff93ac37 mod_admin_shell: Add session.is_connected() method
Matthew Wild <mwild1@gmail.com>
parents: 12401
diff changeset
   192
		end
11954
d2a9e95fd27b mod_admin_shell: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 11953
diff changeset
   193
	};
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
	session.env = setmetatable({}, default_env_mt);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
	session.thread = async.runner(function (line)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
		console:process_line(session, line);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
	end, runner_callbacks, session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
	-- Load up environment with helper objects
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   201
	for name, t in pairs(def_env) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
		if type(t) == "table" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
			session.env[name] = setmetatable({ session = session }, { __index = t });
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
	session.env.output:configure();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
	return session;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
local function handle_line(event)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   213
	local session = event.origin.shell_session;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
	if not session then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
		session = console:new_session(event.origin);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
		event.origin.shell_session = session;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
	end
12533
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
   218
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
   219
	local default_width = 132; -- The common default of 80 is a bit too narrow for e.g. s2s:show(), 132 was another common width for hardware terminals
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
   220
	local margin = 2; -- To account for '| ' when lines are printed
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
   221
	session.width = (tonumber(event.stanza.attr.width) or default_width)-margin;
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
   222
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
	local line = event.stanza:get_text();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
	local useglobalenv;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   226
	local result = st.stanza("repl-result");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
	if line:match("^>") then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
		line = line:gsub("^>", "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
		useglobalenv = true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
	else
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   232
		local command = line:match("^(%w+) ") or line:match("^%w+$") or line:match("%p");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
		if commands[command] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
			commands[command](session, line);
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   235
			event.origin.send(result);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
			return;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
	session.env._ = line;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
	if not useglobalenv and commands[line:lower()] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
		commands[line:lower()](session, line);
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   244
		event.origin.send(result);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
		return;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
11740
ddb87df3de30 mod_admin_shell: Keep unrestricted environment for session lifetime
Kim Alvefur <zash@zash.se>
parents: 11611
diff changeset
   248
	if useglobalenv and not session.globalenv then
ddb87df3de30 mod_admin_shell: Keep unrestricted environment for session lifetime
Kim Alvefur <zash@zash.se>
parents: 11611
diff changeset
   249
		session.globalenv = redirect_output(_G, session);
ddb87df3de30 mod_admin_shell: Keep unrestricted environment for session lifetime
Kim Alvefur <zash@zash.se>
parents: 11611
diff changeset
   250
	end
ddb87df3de30 mod_admin_shell: Keep unrestricted environment for session lifetime
Kim Alvefur <zash@zash.se>
parents: 11611
diff changeset
   251
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
	local chunkname = "=console";
11740
ddb87df3de30 mod_admin_shell: Keep unrestricted environment for session lifetime
Kim Alvefur <zash@zash.se>
parents: 11611
diff changeset
   253
	local env = (useglobalenv and session.globalenv) or session.env or nil
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
	-- luacheck: ignore 311/err
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
	local chunk, err = envload("return "..line, chunkname, env);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
	if not chunk then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
		chunk, err = envload(line, chunkname, env);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
		if not chunk then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
			err = err:gsub("^%[string .-%]:%d+: ", "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
			err = err:gsub("^:%d+: ", "");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   261
			err = err:gsub("'<eof>'", "the end of the line");
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   262
			result.attr.type = "error";
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   263
			result:text("Sorry, I couldn't understand that... "..err);
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   264
			event.origin.send(result);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   265
			return;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
	local taskok, message = chunk();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
11953
ae4bc56f18e0 mod_admin_shell: Wait for promises
Kim Alvefur <zash@zash.se>
parents: 11950
diff changeset
   271
	if promise.is_promise(taskok) then
ae4bc56f18e0 mod_admin_shell: Wait for promises
Kim Alvefur <zash@zash.se>
parents: 11950
diff changeset
   272
		taskok, message = async.wait_for(taskok);
ae4bc56f18e0 mod_admin_shell: Wait for promises
Kim Alvefur <zash@zash.se>
parents: 11950
diff changeset
   273
	end
ae4bc56f18e0 mod_admin_shell: Wait for promises
Kim Alvefur <zash@zash.se>
parents: 11950
diff changeset
   274
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
	if not message then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
		if type(taskok) ~= "string" and useglobalenv then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
			taskok = session.serialize(taskok);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
		end
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   279
		result:text("Result: "..tostring(taskok));
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   280
	elseif (not taskok) and message then
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   281
		result.attr.type = "error";
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   282
		result:text("Error: "..tostring(message));
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   283
	else
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   284
		result:text("OK: "..tostring(message));
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   285
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   287
	event.origin.send(result);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10860
diff changeset
   290
module:hook("admin/repl-input", function (event)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   291
	local ok, err = pcall(handle_line, event);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   292
	if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   293
		event.origin.send(st.stanza("repl-result", { type = "error" }):text(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   294
	end
12891
68df46926c26 mod_admin_socket: Return error on unhandled input to prevent apparent freeze
Kim Alvefur <zash@zash.se>
parents: 12792
diff changeset
   295
	return true;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   296
end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   297
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   298
local function describe_command(s)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   299
	local section, name, args, desc = s:match("^([%w_]+):([%w_]+)%(([^)]*)%) %- (.+)$");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   300
	if not section then
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   301
		error("Failed to parse command description: "..s);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   302
	end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   303
	local command_help = getmetatable(def_env[section]).help.commands;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   304
	command_help[name] = {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   305
		desc = desc;
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   306
		args = array.collect(args:gmatch("[%w_]+")):map(function (arg_name)
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   307
			return { name = arg_name };
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   308
		end);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   309
	};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   310
end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   311
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
-- Console commands --
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   313
-- These are simple commands, not valid standalone in Lua
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   315
-- Help about individual topics is handled by def_env.help
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
function commands.help(session, data)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
	local print = session.print;
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   318
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   319
	local topic = data:match("^help (%w+)");
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   320
	if topic then
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   321
		return def_env.help[topic]({ session = session });
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   322
	end
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   323
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   324
	print [[Commands are divided into multiple sections. For help on a particular section, ]]
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   325
	print [[type: help SECTION (for example, 'help c2s'). Sections are: ]]
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   326
	print [[]]
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   327
	local row = format_table({ { title = "Section", width = 7 }, { title = "Description", width = "100%" } }, session.width)
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   328
	print(row())
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   329
	for section_name, section in it.sorted_pairs(def_env) do
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   330
		local section_mt = getmetatable(section);
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   331
		local section_help = section_mt and section_mt.help;
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   332
		print(row { section_name; section_help and section_help.desc or "" });
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   333
	end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   334
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   335
	print("");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   336
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   337
	print [[In addition to info about commands, the following general topics are available:]]
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   338
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   339
	print("");
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   340
	for topic_name, topic_info in it.sorted_pairs(help_topics) do
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
   341
		print(topic_name .. " - "..topic_info.desc);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   342
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   343
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   345
-- Session environment --
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
-- Anything in def_env will be accessible within the session as a global variable
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
--luacheck: ignore 212/self
13136
5bfcfd12c423 mod_admin_shell: Use new serialize preset to simplify default config
Kim Alvefur <zash@zash.se>
parents: 13132
diff changeset
   349
local serialize_defaults = module:get_option("console_prettyprint_settings", {
5bfcfd12c423 mod_admin_shell: Use new serialize preset to simplify default config
Kim Alvefur <zash@zash.se>
parents: 13132
diff changeset
   350
	preset = "pretty";
5bfcfd12c423 mod_admin_shell: Use new serialize preset to simplify default config
Kim Alvefur <zash@zash.se>
parents: 13132
diff changeset
   351
	maxdepth = 2;
5bfcfd12c423 mod_admin_shell: Use new serialize preset to simplify default config
Kim Alvefur <zash@zash.se>
parents: 13132
diff changeset
   352
	table_iterator = "pairs";
5bfcfd12c423 mod_admin_shell: Use new serialize preset to simplify default config
Kim Alvefur <zash@zash.se>
parents: 13132
diff changeset
   353
})
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   355
def_env.output = new_section("Configure admin console output");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
function def_env.output:configure(opts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
	if type(opts) ~= "table" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
		opts = { preset = opts };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   359
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   360
	if not opts.fallback then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   361
		-- XXX Error message passed to fallback is lost, does it matter?
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   362
		opts.fallback = tostring;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   363
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   364
	for k,v in pairs(serialize_defaults) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
		if opts[k] == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
			opts[k] = v;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   367
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
	end
11895
6a241e66eec5 mod_admin_shell: Respect metatables in output serialization
Kim Alvefur <zash@zash.se>
parents: 11893
diff changeset
   369
	if opts.table_iterator == "pairs" then
6a241e66eec5 mod_admin_shell: Respect metatables in output serialization
Kim Alvefur <zash@zash.se>
parents: 11893
diff changeset
   370
		opts.table_iterator = pairs;
6a241e66eec5 mod_admin_shell: Respect metatables in output serialization
Kim Alvefur <zash@zash.se>
parents: 11893
diff changeset
   371
	elseif type(opts.table_iterator) ~= "function" then
6a241e66eec5 mod_admin_shell: Respect metatables in output serialization
Kim Alvefur <zash@zash.se>
parents: 11893
diff changeset
   372
		opts.table_iterator = nil; -- rawpairs is the default
6a241e66eec5 mod_admin_shell: Respect metatables in output serialization
Kim Alvefur <zash@zash.se>
parents: 11893
diff changeset
   373
	end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
	self.session.serialize = serialization.new(opts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   376
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   377
def_env.help = setmetatable({}, {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   378
	help = {
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   379
		desc = "Show this help about available commands";
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   380
		commands = {};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   381
	};
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   382
	__index = function (_, section_name)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   383
		return function (self)
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   384
			local print = self.session.print;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   385
			local section_mt = getmetatable(def_env[section_name]);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   386
			local section_help = section_mt and section_mt.help;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   387
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   388
			local c = 0;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   389
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   390
			if section_help then
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   391
				print("Help: "..section_name);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   392
				if section_help.desc then
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   393
					print(section_help.desc);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   394
				end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   395
				print(("-"):rep(#(section_help.desc or section_name)));
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   396
				print("");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   397
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   398
				if section_help.content then
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   399
					print(section_help.content);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   400
					print("");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   401
				end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   402
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   403
				for command, command_help in it.sorted_pairs(section_help.commands or {}) do
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   404
					c = c + 1;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   405
					local args = command_help.args:pluck("name"):concat(", ");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   406
					local desc = command_help.desc or command_help.module and ("Provided by mod_"..command_help.module) or "";
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   407
					print(("%s:%s(%s) - %s"):format(section_name, command, args, desc));
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   408
				end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   409
			elseif help_topics[section_name] then
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   410
				local topic = help_topics[section_name];
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   411
				if type(topic.content) == "function" then
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   412
					topic.content(self, print);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   413
				else
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   414
					print(topic.content);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   415
				end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   416
				print("");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   417
				return true, "Showing help topic '"..section_name.."'";
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   418
			else
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   419
				print("Unknown topic: "..section_name);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   420
			end
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   421
			print("");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   422
			return true, ("%d command(s) listed"):format(c);
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   423
		end;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   424
	end;
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   425
});
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   426
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   427
def_env.server = new_section("Uptime, version, shutting down, etc.");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   428
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   429
function def_env.server:insane_reload()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   430
	prosody.unlock_globals();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   431
	dofile "prosody"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   432
	prosody = _G.prosody;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   433
	return true, "Server reloaded";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   434
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   435
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   436
describe_command [[server:version() - Show the server's version number]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   437
function def_env.server:version()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   438
	return true, tostring(prosody.version or "unknown");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   439
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   440
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   441
describe_command [[server:uptime() - Show how long the server has been running]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   442
function def_env.server:uptime()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   443
	local t = os.time()-prosody.start_time;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   444
	local seconds = t%60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   445
	t = (t - seconds)/60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   446
	local minutes = t%60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   447
	t = (t - minutes)/60;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   448
	local hours = t%24;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   449
	t = (t - hours)/24;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   450
	local days = t;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   451
	return true, string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   452
		days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   453
		minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   454
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   455
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   456
describe_command [[server:shutdown(reason) - Shut down the server, with an optional reason to be broadcast to all connections]]
11835
94cd363116a3 mod_admin_shell: Allow passing an exit code to server:shutdown()
Kim Alvefur <zash@zash.se>
parents: 11740
diff changeset
   457
function def_env.server:shutdown(reason, code)
94cd363116a3 mod_admin_shell: Allow passing an exit code to server:shutdown()
Kim Alvefur <zash@zash.se>
parents: 11740
diff changeset
   458
	prosody.shutdown(reason, code);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   459
	return true, "Shutdown initiated";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   460
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   461
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   462
local function human(kb)
10891
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
   463
	return format_number(kb*1024, "B", "b");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   464
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   465
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   466
describe_command [[server:memory() - Show details about the server's memory usage]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   467
function def_env.server:memory()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   468
	if not has_pposix or not pposix.meminfo then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   469
		return true, "Lua is using "..human(collectgarbage("count"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   470
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   471
	local mem, lua_mem = pposix.meminfo(), collectgarbage("count");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   472
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   473
	print("Process: "..human((mem.allocated+mem.allocated_mmap)/1024));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   474
	print("   Used: "..human(mem.used/1024).." ("..human(lua_mem).." by Lua)");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   475
	print("   Free: "..human(mem.unused/1024).." ("..human(mem.returnable/1024).." returnable)");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   476
	return true, "OK";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   477
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   478
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   479
def_env.module = new_section("Commands to load/reload/unload modules/plugins");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   480
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   481
local function get_hosts_set(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   482
	if type(hosts) == "table" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   483
		if hosts[1] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   484
			return set.new(hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   485
		elseif hosts._items then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   486
			return hosts;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   487
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   488
	elseif type(hosts) == "string" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   489
		return set.new { hosts };
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   490
	elseif hosts == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   491
		return set.new(array.collect(keys(prosody.hosts)));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   492
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   493
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   494
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   495
-- Hosts with a module or all virtualhosts if no module given
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   496
-- matching modules_enabled in the global section
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   497
local function get_hosts_with_module(hosts, module)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   498
	local hosts_set = get_hosts_set(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   499
	/ function (host)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   500
			if module then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   501
				-- Module given, filter in hosts with this module loaded
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   502
				if modulemanager.is_loaded(host, module) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   503
					return host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   504
				else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   505
					return nil;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   506
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   507
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   508
			if not hosts then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   509
				-- No hosts given, filter in VirtualHosts
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   510
				if prosody.hosts[host].type == "local" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   511
					return host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   512
				else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   513
					return nil
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   514
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   515
			end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   516
			-- No module given, but hosts are, don't filter at all
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   517
			return host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   518
		end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   519
	if module and modulemanager.get_module("*", module) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   520
		hosts_set:add("*");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   521
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   522
	return hosts_set;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   523
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   524
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   525
describe_command [[module:info(module, host) - Show information about a loaded module]]
11605
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   526
function def_env.module:info(name, hosts)
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   527
	if not name then
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   528
		return nil, "module name expected";
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   529
	end
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   530
	local print = self.session.print;
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   531
	hosts = get_hosts_with_module(hosts, name);
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   532
	if hosts:empty() then
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   533
		return false, "mod_" .. name .. " does not appear to be loaded on the specified hosts";
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   534
	end
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   535
11936
92925f1320e7 mod_admin_shell: Factor out simple function in module:info for reuse
Kim Alvefur <zash@zash.se>
parents: 11935
diff changeset
   536
	local function item_name(item) return item.name; end
92925f1320e7 mod_admin_shell: Factor out simple function in module:info for reuse
Kim Alvefur <zash@zash.se>
parents: 11935
diff changeset
   537
12544
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   538
	local function task_timefmt(t)
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   539
		if not t then
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   540
			return "no last run time"
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   541
		elseif os.difftime(os.time(), t) < 86400 then
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   542
			return os.date("last run today at %H:%M", t);
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   543
		else
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   544
			return os.date("last run %A at %H:%M", t);
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   545
		end
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   546
	end
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   547
11610
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   548
	local friendly_descriptions = {
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   549
		["adhoc-provider"] = "Ad-hoc commands",
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   550
		["auth-provider"] = "Authentication provider",
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   551
		["http-provider"] = "HTTP services",
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   552
		["net-provider"] = "Network service",
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   553
		["storage-provider"] = "Storage driver",
11935
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
   554
		["measure"] = "Legacy metrics",
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
   555
		["metric"] = "Metrics",
11995
bef2a59b00d1 mod_admin_shell: List periodic tasks in module:info
Kim Alvefur <zash@zash.se>
parents: 11958
diff changeset
   556
		["task"] = "Periodic task",
11610
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   557
	};
11611
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   558
	local item_formatters = {
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   559
		["feature"] = tostring,
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   560
		["identity"] = function(ident) return ident.type .. "/" .. ident.category; end,
11936
92925f1320e7 mod_admin_shell: Factor out simple function in module:info for reuse
Kim Alvefur <zash@zash.se>
parents: 11935
diff changeset
   561
		["adhoc-provider"] = item_name,
92925f1320e7 mod_admin_shell: Factor out simple function in module:info for reuse
Kim Alvefur <zash@zash.se>
parents: 11935
diff changeset
   562
		["auth-provider"] = item_name,
92925f1320e7 mod_admin_shell: Factor out simple function in module:info for reuse
Kim Alvefur <zash@zash.se>
parents: 11935
diff changeset
   563
		["storage-provider"] = item_name,
11947
cf47834d3698 mod_admin_shell: Fix showing default HTTP path in module:info
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
   564
		["http-provider"] = function(item, mod) return mod:http_url(item.name, item.default_path); end,
12542
0f56587bb37f mod_admin_shell: Drop unused argument [luacheck]
Kim Alvefur <zash@zash.se>
parents: 12541
diff changeset
   565
		["net-provider"] = function(item)
12541
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   566
			local service_name = item.name;
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   567
			local ports_list = {};
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   568
			for _, interface, port in portmanager.get_active_services():iter(service_name, nil, nil) do
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   569
				table.insert(ports_list, "["..interface.."]:"..port);
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   570
			end
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   571
			if not ports_list[1] then
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   572
				return service_name..": not listening on any ports";
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   573
			end
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   574
			return service_name..": "..table.concat(ports_list, ", ");
74418f8096b0 mod_admin_shell: Show bound ports in module:info
Kim Alvefur <zash@zash.se>
parents: 12540
diff changeset
   575
		end,
11935
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
   576
		["measure"] = function(item) return item.name .. " (" .. suf(item.conf and item.conf.unit, " ") .. item.type .. ")"; end,
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
   577
		["metric"] = function(item)
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
   578
			return ("%s (%s%s)%s"):format(item.name, suf(item.mf.unit, " "), item.mf.type_, pre(": ", item.mf.description));
c65d5da8e99a mod_admin_shell: List collected metrics in module:info
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
   579
		end,
12544
0684506c99d3 mod_admin_shell: Include last (mod_cron) task run time in module:info()
Kim Alvefur <zash@zash.se>
parents: 12542
diff changeset
   580
		["task"] = function (item) return string.format("%s (%s, %s)", item.name or item.id, item.when, task_timefmt(item.last)); end
11611
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   581
	};
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   582
11605
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   583
	for host in hosts do
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   584
		local mod = modulemanager.get_module(host, name);
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   585
		if mod.module.host == "*" then
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   586
			print("in global context");
11608
7466bf65d7c8 mod_admin_shell: module:info: Use existing host string representation
Kim Alvefur <zash@zash.se>
parents: 11607
diff changeset
   587
		else
7466bf65d7c8 mod_admin_shell: module:info: Use existing host string representation
Kim Alvefur <zash@zash.se>
parents: 11607
diff changeset
   588
			print("on " .. tostring(prosody.hosts[mod.module.host]));
11605
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   589
		end
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   590
		print("  path: " .. (mod.module.path or "n/a"));
11606
78ec0741c2bc mod_admin_shell: module:info: Show module status
Kim Alvefur <zash@zash.se>
parents: 11605
diff changeset
   591
		if mod.module.status_message then
78ec0741c2bc mod_admin_shell: module:info: Show module status
Kim Alvefur <zash@zash.se>
parents: 11605
diff changeset
   592
			print("  status: [" .. mod.module.status_type .. "] " .. mod.module.status_message);
78ec0741c2bc mod_admin_shell: module:info: Show module status
Kim Alvefur <zash@zash.se>
parents: 11605
diff changeset
   593
		end
11609
225ef07f2bee mod_admin_shell: module:info: List provided 'items'
Kim Alvefur <zash@zash.se>
parents: 11608
diff changeset
   594
		if mod.module.items and next(mod.module.items) ~= nil then
225ef07f2bee mod_admin_shell: module:info: List provided 'items'
Kim Alvefur <zash@zash.se>
parents: 11608
diff changeset
   595
			print("  provides:");
225ef07f2bee mod_admin_shell: module:info: List provided 'items'
Kim Alvefur <zash@zash.se>
parents: 11608
diff changeset
   596
			for kind, items in pairs(mod.module.items) do
11610
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   597
				local label = friendly_descriptions[kind] or kind:gsub("%-", " "):gsub("^%a", string.upper);
0b65d43f4da4 mod_admin_shell: module:info: Show friendlier name for known 'items'
Kim Alvefur <zash@zash.se>
parents: 11609
diff changeset
   598
				print(string.format("  - %s (%d item%s)", label, #items, #items > 1 and "s" or ""));
11611
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   599
				local formatter = item_formatters[kind];
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   600
				if formatter then
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   601
					for _, item in ipairs(items) do
11854
bfa85965106e mod_admin_shell: Show HTTP base-URLs in module:info()
Kim Alvefur <zash@zash.se>
parents: 11835
diff changeset
   602
						print("    - " .. formatter(item, mod.module));
11611
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   603
					end
03eb4c0dca27 mod_admin_shell: module:info: List 'items' that can be formatted easily
Kim Alvefur <zash@zash.se>
parents: 11610
diff changeset
   604
				end
11609
225ef07f2bee mod_admin_shell: module:info: List provided 'items'
Kim Alvefur <zash@zash.se>
parents: 11608
diff changeset
   605
			end
225ef07f2bee mod_admin_shell: module:info: List provided 'items'
Kim Alvefur <zash@zash.se>
parents: 11608
diff changeset
   606
		end
11607
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   607
		if mod.module.dependencies and next(mod.module.dependencies) ~= nil then
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   608
			print("  dependencies:");
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   609
			for dep in pairs(mod.module.dependencies) do
12931
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   610
				-- Dependencies are per module instance, not per host, so dependencies
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   611
				-- of/on global modules may list modules not actually loaded on the
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   612
				-- current host.
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   613
				if modulemanager.is_loaded(host, dep) then
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   614
					print("  - mod_" .. dep);
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   615
				end
11607
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   616
			end
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   617
		end
12926
aaf055d6fe7a mod_admin_shell: Show reverse dependencies in module:info()
Kim Alvefur <zash@zash.se>
parents: 12912
diff changeset
   618
		if mod.module.reverse_dependencies and next(mod.module.reverse_dependencies) ~= nil then
aaf055d6fe7a mod_admin_shell: Show reverse dependencies in module:info()
Kim Alvefur <zash@zash.se>
parents: 12912
diff changeset
   619
			print("  reverse dependencies:");
aaf055d6fe7a mod_admin_shell: Show reverse dependencies in module:info()
Kim Alvefur <zash@zash.se>
parents: 12912
diff changeset
   620
			for dep in pairs(mod.module.reverse_dependencies) do
12931
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   621
				if modulemanager.is_loaded(host, dep) then
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   622
					print("  - mod_" .. dep);
918dfbb330fd mod_admin_shell: Limit module dependency listings to loaded on current host
Kim Alvefur <zash@zash.se>
parents: 12926
diff changeset
   623
				end
11607
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   624
			end
4e24408a3f57 mod_admin_shell: module:info: List dependencies
Kim Alvefur <zash@zash.se>
parents: 11606
diff changeset
   625
		end
11605
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   626
	end
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   627
	return true;
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   628
end
9483728f890f mod_admin_shell: Add basic command that shows more info about loaded modules
Kim Alvefur <zash@zash.se>
parents: 11527
diff changeset
   629
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   630
describe_command [[module:load(module, host) - Load the specified module on the specified host (or all hosts if none given)]]
12607
05ca75309fa0 mod_admin_shell: Remove obsolete module:load() argument from 0.8 time
Kim Alvefur <zash@zash.se>
parents: 12593
diff changeset
   631
function def_env.module:load(name, hosts)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   632
	hosts = get_hosts_with_module(hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   633
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   634
	-- Load the module for each host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   635
	local ok, err, count, mod = true, nil, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   636
	for host in hosts do
13132
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   637
		local configured_modules, component = modulemanager.get_modules_for_host(host);
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   638
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   639
		if (not modulemanager.is_loaded(host, name)) then
12607
05ca75309fa0 mod_admin_shell: Remove obsolete module:load() argument from 0.8 time
Kim Alvefur <zash@zash.se>
parents: 12593
diff changeset
   640
			mod, err = modulemanager.load(host, name);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   641
			if not mod then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   642
				ok = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   643
				if err == "global-module-already-loaded" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   644
					if count > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   645
						ok, err, count = true, nil, 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   646
					end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   647
					break;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   648
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   649
				self.session.print(err or "Unknown error loading module");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   650
			else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   651
				count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   652
				self.session.print("Loaded for "..mod.module.host);
13132
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   653
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   654
				if not (configured_modules:contains(name) or name == component) then
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   655
					self.session.print("Note: Module will not be loaded after restart unless enabled in configuration");
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   656
				end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   657
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   658
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   659
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   660
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   661
	return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   662
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   663
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   664
describe_command [[module:unload(module, host) - The same, but just unloads the module from memory]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   665
function def_env.module:unload(name, hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   666
	hosts = get_hosts_with_module(hosts, name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   667
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   668
	-- Unload the module for each host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   669
	local ok, err, count = true, nil, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   670
	for host in hosts do
13132
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   671
		local configured_modules, component = modulemanager.get_modules_for_host(host);
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   672
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   673
		if modulemanager.is_loaded(host, name) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   674
			ok, err = modulemanager.unload(host, name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   675
			if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   676
				ok = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   677
				self.session.print(err or "Unknown error unloading module");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   678
			else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   679
				count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   680
				self.session.print("Unloaded from "..host);
13132
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   681
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   682
				if configured_modules:contains(name) or name == component then
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   683
					self.session.print("Note: Module will be loaded after restart unless disabled in configuration");
38582771b593 mod_admin_shell: Warn when (un-)loading module would be undone by restart
Kim Alvefur <zash@zash.se>
parents: 13121
diff changeset
   684
				end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   685
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   686
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   687
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   688
	return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   689
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   690
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   691
local function _sort_hosts(a, b)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   692
	if a == "*" then return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   693
	elseif b == "*" then return false
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   694
	else return a:gsub("[^.]+", string.reverse):reverse() < b:gsub("[^.]+", string.reverse):reverse(); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   695
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   696
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   697
describe_command [[module:reload(module, host) - The same, but unloads and loads the module (saving state if the module supports it)]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   698
function def_env.module:reload(name, hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   699
	hosts = array.collect(get_hosts_with_module(hosts, name)):sort(_sort_hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   700
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   701
	-- Reload the module for each host
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   702
	local ok, err, count = true, nil, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   703
	for _, host in ipairs(hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   704
		if modulemanager.is_loaded(host, name) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   705
			ok, err = modulemanager.reload(host, name);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   706
			if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   707
				ok = false;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   708
				self.session.print(err or "Unknown error reloading module");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   709
			else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   710
				count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   711
				if ok == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   712
					ok = true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   713
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   714
				self.session.print("Reloaded on "..host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   715
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   716
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   717
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   718
	return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   719
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   720
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   721
describe_command [[module:list(host) - List the modules loaded on the specified host]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   722
function def_env.module:list(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   723
	hosts = array.collect(set.new({ not hosts and "*" or nil }) + get_hosts_set(hosts)):sort(_sort_hosts);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   724
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   725
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   726
	for _, host in ipairs(hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   727
		print((host == "*" and "Global" or host)..":");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   728
		local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   729
		if #modules == 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   730
			if prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   731
				print("    No modules loaded");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   732
			else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   733
				print("    Host not found");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   734
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   735
		else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   736
			for _, name in ipairs(modules) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   737
				local status, status_text = modulemanager.get_module(host, name).module:get_status();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   738
				local status_summary = "";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   739
				if status == "warn" or status == "error" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   740
					status_summary = (" (%s: %s)"):format(status, status_text);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   741
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   742
				print(("    %s%s"):format(name, status_summary));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   743
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   744
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   745
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   746
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   747
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   748
def_env.config = new_section("Reloading the configuration, etc.");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   749
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   750
function def_env.config:load(filename, format)
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
   751
	local config_load = require "prosody.core.configmanager".load;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   752
	local ok, err = config_load(filename, format);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   753
	if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   754
		return false, err or "Unknown error loading config";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   755
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   756
	return true, "Config loaded";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   757
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   758
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   759
describe_command [[config:get([host,] option) - Show the value of a config option.]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   760
function def_env.config:get(host, key)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   761
	if key == nil then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   762
		host, key = "*", host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   763
	end
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
   764
	local config_get = require "prosody.core.configmanager".get
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   765
	return true, serialize_config(config_get(host, key));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   766
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   767
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   768
describe_command [[config:set([host,] option, value) - Update the value of a config option without writing to the config file.]]
13066
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   769
function def_env.config:set(host, key, value)
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   770
	if host ~= "*" and not prosody.hosts[host] then
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   771
		host, key, value = "*", host, key;
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   772
	end
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   773
	return require "prosody.core.configmanager".set(host, key, value);
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   774
end
da51c36ed1e7 mod_admin_shell: Add config:set([host,] key, value) because why not
Kim Alvefur <zash@zash.se>
parents: 13065
diff changeset
   775
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   776
describe_command [[config:reload() - Reload the server configuration. Modules may need to be reloaded for changes to take effect.]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   777
function def_env.config:reload()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   778
	local ok, err = prosody.reload_config();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   779
	return ok, (ok and "Config reloaded (you may need to reload modules to take effect)") or tostring(err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   780
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   781
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   782
def_env.c2s = new_section("Commands to manage local client-to-server sessions");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   783
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   784
local function get_jid(session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   785
	if session.username then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   786
		return session.full_jid or jid_join(session.username, session.host, session.resource);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   787
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   788
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   789
	local conn = session.conn;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   790
	local ip = session.ip or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   791
	local clientport = conn and conn:clientport() or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   792
	local serverip = conn and conn.server and conn:server():ip() or "?";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   793
	local serverport = conn and conn:serverport() or "?"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   794
	return jid_join("["..ip.."]:"..clientport, session.host or "["..serverip.."]:"..serverport);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   795
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   796
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   797
local function get_c2s()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   798
	local c2s = array.collect(values(prosody.full_sessions));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   799
	c2s:append(array.collect(values(module:shared"/*/c2s/sessions")));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   800
	c2s:append(array.collect(values(module:shared"/*/bosh/sessions")));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   801
	c2s:unique();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   802
	return c2s;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   803
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   804
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   805
local function _sort_by_jid(a, b)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   806
	if a.host == b.host then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   807
		if a.username == b.username then return (a.resource or "") > (b.resource or ""); end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   808
		return (a.username or "") > (b.username or "");
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   809
	end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   810
	return _sort_hosts(a.host or "", b.host or "");
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   811
end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   812
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   813
local function show_c2s(callback)
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   814
	get_c2s():sort(_sort_by_jid):map(function (session)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   815
		callback(get_jid(session), session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   816
	end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   817
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   818
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
   819
describe_command [[c2s:count() - Count sessions without listing them]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   820
function def_env.c2s:count()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   821
	local c2s = get_c2s();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   822
	return true, "Total: "..  #c2s .." clients";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   823
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   824
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   825
local function get_s2s_hosts(session) --> local,remote
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   826
	if session.direction == "outgoing" then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   827
		return session.host or session.from_host, session.to_host;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   828
	elseif session.direction == "incoming" then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   829
		return session.host or session.to_host, session.from_host;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   830
	end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   831
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   832
12228
2b348d65cd69 mod_admin_shell: Add help section about customizing table columns
Kim Alvefur <zash@zash.se>
parents: 12213
diff changeset
   833
available_columns = {
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   834
	jid = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   835
		title = "JID";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   836
		description = "Full JID of user session";
13041
635ed9a362ee mod_admin_shell: Dynamically size JIDs and hosts
Kim Alvefur <zash@zash.se>
parents: 13040
diff changeset
   837
		width = "3p";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   838
		key = "full_jid";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   839
		mapper = function(full_jid, session) return full_jid or get_jid(session) end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   840
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   841
	host = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   842
		title = "Host";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   843
		description = "Local hostname";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   844
		key = "host";
13041
635ed9a362ee mod_admin_shell: Dynamically size JIDs and hosts
Kim Alvefur <zash@zash.se>
parents: 13040
diff changeset
   845
		width = "1p";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   846
		mapper = function(host, session)
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
   847
			return host or get_s2s_hosts(session) or "?";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   848
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   849
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   850
	remote = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   851
		title = "Remote";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   852
		description = "Remote hostname";
13041
635ed9a362ee mod_admin_shell: Dynamically size JIDs and hosts
Kim Alvefur <zash@zash.se>
parents: 13040
diff changeset
   853
		width = "1p";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   854
		mapper = function(_, session)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   855
			return select(2, get_s2s_hosts(session));
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   856
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   857
	};
12027
5a3781a12285 mod_admin_shell: Add port as a c2s/s2s:show column definition
Kim Alvefur <zash@zash.se>
parents: 12023
diff changeset
   858
	port = {
5a3781a12285 mod_admin_shell: Add port as a c2s/s2s:show column definition
Kim Alvefur <zash@zash.se>
parents: 12023
diff changeset
   859
		title = "Port";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   860
		description = "Server port used";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   861
		width = #string.format("%d", 0xffff); -- max 16 bit unsigned integer
12027
5a3781a12285 mod_admin_shell: Add port as a c2s/s2s:show column definition
Kim Alvefur <zash@zash.se>
parents: 12023
diff changeset
   862
		align = "right";
5a3781a12285 mod_admin_shell: Add port as a c2s/s2s:show column definition
Kim Alvefur <zash@zash.se>
parents: 12023
diff changeset
   863
		key = "conn";
12791
3735ad8d6f8e mod_admin_shell: Ensure connection exists to get port from (fixes #1777)
Kim Alvefur <zash@zash.se>
parents: 12641
diff changeset
   864
		mapper = function(conn)
3735ad8d6f8e mod_admin_shell: Ensure connection exists to get port from (fixes #1777)
Kim Alvefur <zash@zash.se>
parents: 12641
diff changeset
   865
			if conn then
3735ad8d6f8e mod_admin_shell: Ensure connection exists to get port from (fixes #1777)
Kim Alvefur <zash@zash.se>
parents: 12641
diff changeset
   866
				return conn:serverport();
3735ad8d6f8e mod_admin_shell: Ensure connection exists to get port from (fixes #1777)
Kim Alvefur <zash@zash.se>
parents: 12641
diff changeset
   867
			end
3735ad8d6f8e mod_admin_shell: Ensure connection exists to get port from (fixes #1777)
Kim Alvefur <zash@zash.se>
parents: 12641
diff changeset
   868
		end;
12027
5a3781a12285 mod_admin_shell: Add port as a c2s/s2s:show column definition
Kim Alvefur <zash@zash.se>
parents: 12023
diff changeset
   869
	};
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   870
	dir = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   871
		title = "Dir";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   872
		description = "Direction of server-to-server connection";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   873
		width = #"<->";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   874
		key = "direction";
11892
050c515fe9aa mod_admin_shell: Indicate bi-directional s2s connections
Kim Alvefur <zash@zash.se>
parents: 11891
diff changeset
   875
		mapper = function(dir, session)
050c515fe9aa mod_admin_shell: Indicate bi-directional s2s connections
Kim Alvefur <zash@zash.se>
parents: 11891
diff changeset
   876
			if session.incoming and session.outgoing then return "<->"; end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   877
			if dir == "outgoing" then return "-->"; end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   878
			if dir == "incoming" then return "<--"; end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   879
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   880
	};
13040
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   881
	id = {
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   882
		title = "Session ID";
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   883
		description = "Internal session ID used in logging";
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   884
		-- Depends on log16(?) of pointers which may vary over runtime, so + some margin
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   885
		width = math.max(#"c2s", #"s2sin", #"s2sout") + #(tostring({}):match("%x+$")) + 2;
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   886
		key = "id";
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   887
	};
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   888
	type = {
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   889
		title = "Type";
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   890
		description = "Session type";
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   891
		width = math.max(#"c2s_unauthed", #"s2sout_unauthed");
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   892
		key = "type";
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   893
	};
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   894
	method = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   895
		title = "Method";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   896
		description = "Connection method";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   897
		width = math.max(#"BOSH", #"WebSocket", #"TCP");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   898
		mapper = function(_, session)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   899
			if session.bosh_version then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   900
				return "BOSH";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   901
			elseif session.websocket_request then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   902
				return "WebSocket";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   903
			else
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   904
				return "TCP";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   905
			end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   906
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   907
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   908
	ipv = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   909
		title = "IPv";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   910
		description = "Internet Protocol version (4 or 6)";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   911
		width = #"IPvX";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   912
		key = "ip";
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
   913
		mapper = function(ip) if ip then return ip:find(":") and "IPv6" or "IPv4"; end end;
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   914
	};
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   915
	ip = {
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   916
		title = "IP address";
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   917
		description = "IP address the session connected from";
13047
d4f7118d1531 mod_admin_shell: Make IP column thinner if IPv6 is disabled
Kim Alvefur <zash@zash.se>
parents: 13046
diff changeset
   918
		width = module:get_option_boolean("use_ipv6", true) and #"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" or #"198.051.100.255";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   919
		key = "ip";
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   920
	};
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   921
	status = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   922
		title = "Status";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   923
		description = "Presence status";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   924
		width = math.max(#"online", #"chat");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   925
		key = "presence";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   926
		mapper = function(p)
11950
c0a01e5f5656 mod_admin_shell: Reduce width of 'Status' column
Kim Alvefur <zash@zash.se>
parents: 11949
diff changeset
   927
			if not p then return ""; end
c0a01e5f5656 mod_admin_shell: Reduce width of 'Status' column
Kim Alvefur <zash@zash.se>
parents: 11949
diff changeset
   928
			return p:get_child_text("show") or "online";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   929
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   930
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   931
	secure = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   932
		title = "Security";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   933
		description = "TLS version or security status";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   934
		key = "conn";
13045
61b00bd20074 mod_admin_shell: Fix attempt to compare number with string
Kim Alvefur <zash@zash.se>
parents: 13041
diff changeset
   935
		width = math.max(#"secure", #"TLSvX.Y");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   936
		mapper = function(conn, session)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   937
			if not session.secure then return "insecure"; end
11909
bbfa707a4756 mod_admin_shell: Handle absence of connection in security column (thanks arcseconds)
Kim Alvefur <zash@zash.se>
parents: 11896
diff changeset
   938
			if not conn or not conn:ssl() then return "secure" end
12484
7e9ebdc75ce4 net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents: 12468
diff changeset
   939
			local tls_info = conn.ssl_info and conn:ssl_info();
11949
142b9c4010fe mod_admin_shell: Reduce width of 'Security' column (thanks Link Mauve)
Kim Alvefur <zash@zash.se>
parents: 11947
diff changeset
   940
			return tls_info and tls_info.protocol or "secure";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   941
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   942
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   943
	encryption = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   944
		title = "Encryption";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   945
		description = "Encryption algorithm used (TLS cipher suite)";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   946
		-- openssl ciphers 'ALL:COMPLEMENTOFALL' | tr : \\n | awk 'BEGIN {n=1} length() > n {n=length()} END {print(n)}'
13040
1612c7f7dd55 mod_admin_shell: More dynamic widths calculations
Kim Alvefur <zash@zash.se>
parents: 13039
diff changeset
   947
		width = #"ECDHE-ECDSA-CHACHA20-POLY1305";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   948
		key = "conn";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   949
		mapper = function(conn)
12484
7e9ebdc75ce4 net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents: 12468
diff changeset
   950
			local info = conn and conn.ssl_info and conn:ssl_info();
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   951
			if info then return info.cipher end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   952
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   953
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   954
	cert = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   955
		title = "Certificate";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   956
		description = "Validation status of certificate";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   957
		key = "cert_identity_status";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   958
		width = math.max(#"Expired", #"Self-signed", #"Untrusted", #"Mismatched", #"Unknown");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   959
		mapper = function(cert_status, session)
13093
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   960
			if cert_status == "invalid" then
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   961
				-- non-nil cert_identity_status implies valid chain, which covers just
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   962
				-- about every error condition except mismatched certificate names
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   963
				return "Mismatched";
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   964
			elseif cert_status then
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   965
				-- basically only "valid"
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   966
				return capitalize(cert_status);
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   967
			end
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   968
			-- no certificate status,
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   969
			if session.cert_chain_errors then
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   970
				local cert_errors = set.new(session.cert_chain_errors[1]);
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   971
				if cert_errors:contains("certificate has expired") then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   972
					return "Expired";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   973
				elseif cert_errors:contains("self signed certificate") then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   974
					return "Self-signed";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   975
				end
13093
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   976
				-- Some other cert issue, or something up the chain
41598b7ec543 mod_admin_shell: Refactor 'cert' column
Kim Alvefur <zash@zash.se>
parents: 13083
diff changeset
   977
				-- TODO borrow more logic from mod_s2s/friendly_cert_error()
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   978
				return "Untrusted";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   979
			end
12297
145cb8305c67 mod_admin_shell: Squeeze some characters out of the Certificate column
Kim Alvefur <zash@zash.se>
parents: 12295
diff changeset
   980
			return "Unknown";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   981
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   982
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   983
	sni = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   984
		title = "SNI";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   985
		description = "Hostname requested in TLS";
13041
635ed9a362ee mod_admin_shell: Dynamically size JIDs and hosts
Kim Alvefur <zash@zash.se>
parents: 13040
diff changeset
   986
		width = "1p"; -- same as host, remote etc
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   987
		mapper = function(_, session)
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
   988
			if not session.conn then return end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   989
			local sock = session.conn:socket();
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   990
			return sock and sock.getsniname and sock:getsniname() or "";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   991
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   992
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   993
	alpn = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   994
		title = "ALPN";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
   995
		description = "Protocol requested in TLS";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
   996
		width = math.max(#"http/1.1", #"xmpp-client", #"xmpp-server");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   997
		mapper = function(_, session)
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
   998
			if not session.conn then return end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
   999
			local sock = session.conn:socket();
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1000
			return sock and sock.getalpn and sock:getalpn() or "";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1001
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1002
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1003
	smacks = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1004
		title = "SM";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
  1005
		description = "Stream Management (XEP-0198) status";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1006
		key = "smacks";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
  1007
		-- FIXME shorter synonym for hibernating
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
  1008
		width = math.max(#"yes", #"no", #"hibernating");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1009
		mapper = function(smacks_xmlns, session)
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
  1010
			if not smacks_xmlns then return "no"; end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1011
			if session.hibernating then return "hibernating"; end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1012
			return "yes";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1013
		end;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1014
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1015
	smacks_queue = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1016
		title = "SM Queue";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
  1017
		description = "Length of Stream Management stanza queue";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1018
		key = "outgoing_stanza_queue";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1019
		width = 8;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1020
		align = "right";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1021
		mapper = function (queue)
12060
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12027
diff changeset
  1022
			return queue and tostring(queue:count_unacked());
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1023
		end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1024
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1025
	csi = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1026
		title = "CSI State";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
  1027
		description = "Client State Indication (XEP-0352)";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1028
		key = "state";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1029
		-- TODO include counter
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1030
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1031
	s2s_sasl = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1032
		title = "SASL";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
  1033
		description = "Server authentication status";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1034
		key = "external_auth";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1035
		width = 10;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1036
		mapper = capitalize
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1037
	};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1038
	dialback = {
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1039
		title = "Dialback";
12233
30ea791ce817 mod_admin_shell: Add descriptions of each column to 'help columns'
Kim Alvefur <zash@zash.se>
parents: 12232
diff changeset
  1040
		description = "Legacy server verification";
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1041
		key = "dialback_key";
13039
93c1590b5951 mod_admin_shell: Calculate widths of columns from example values
Kim Alvefur <zash@zash.se>
parents: 13038
diff changeset
  1042
		width = math.max(#"Not used", #"Not initiated", #"Initiated", #"Completed");
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1043
		mapper = function (dialback_key, session)
11896
e712133b4de1 util.human.io: Pass nil to cell mapper to signal missing value
Kim Alvefur <zash@zash.se>
parents: 11895
diff changeset
  1044
			if not dialback_key then
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1045
				if session.type == "s2sin" or session.type == "s2sout" then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1046
					return "Not used";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1047
				end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1048
				return "Not initiated";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1049
			elseif session.type == "s2sin_unauthed" or session.type == "s2sout_unauthed" then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1050
				return "Initiated";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1051
			else
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1052
				return "Completed";
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1053
			end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1054
		end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1055
	};
12664
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1056
	role = {
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1057
		title = "Role";
13038
1387888a5596 mod_admin_shell: Strip 'prosody:' prefix to allow narrower Role column
Kim Alvefur <zash@zash.se>
parents: 13017
diff changeset
  1058
		description = "Session role with 'prosody:' prefix removed";
13291
1e2d65403867 mod_admin_shell: Make 'Role' column dynamically sized
Kim Alvefur <zash@zash.se>
parents: 13174
diff changeset
  1059
		width = "1p";
12664
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1060
		key = "role";
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1061
		mapper = function(role)
13038
1387888a5596 mod_admin_shell: Strip 'prosody:' prefix to allow narrower Role column
Kim Alvefur <zash@zash.se>
parents: 13017
diff changeset
  1062
			local name = role and role.name;
1387888a5596 mod_admin_shell: Strip 'prosody:' prefix to allow narrower Role column
Kim Alvefur <zash@zash.se>
parents: 13017
diff changeset
  1063
			return name and name:match"^prosody:(%w+)" or name;
12664
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1064
		end;
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1065
	}
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1066
};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1067
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1068
local function get_colspec(colspec, default)
11891
b043e1bb8e8e mod_admin_shell: Allow passing columns as a string for convenience
Kim Alvefur <zash@zash.se>
parents: 11890
diff changeset
  1069
	if type(colspec) == "string" then colspec = array(colspec:gmatch("%S+")); end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1070
	local columns = {};
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1071
	for i, col in pairs(colspec or default) do
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1072
		if type(col) == "string" then
13046
0f05804e974d mod_admin_shell: Make default column width 1 part
Kim Alvefur <zash@zash.se>
parents: 13045
diff changeset
  1073
			columns[i] = available_columns[col] or { title = capitalize(col); width = "1p"; key = col };
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1074
		elseif type(col) ~= "table" then
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1075
			return false, ("argument %d: expected string|table but got %s"):format(i, type(col));
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1076
		else
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1077
			columns[i] = col;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1078
		end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1079
	end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1080
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1081
	return columns;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1082
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1083
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1084
describe_command [[c2s:show(jid, columns) - Show all client sessions with the specified JID (or all if no JID given)]]
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1085
function def_env.c2s:show(match_jid, colspec)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1086
	local print = self.session.print;
12664
e8f57970ced5 mod_admin_shell: Show session role in c2s:show
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
  1087
	local columns = get_colspec(colspec, { "id"; "jid"; "role"; "ipv"; "status"; "secure"; "smacks"; "csi" });
12533
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
  1088
	local row = format_table(columns, self.session.width);
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1089
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1090
	local function match(session)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1091
		local jid = get_jid(session)
13057
8128c4f1b08b mod_admin_shell: Allow "*" as substitute for 'nil' for easier CLI usage
Kim Alvefur <zash@zash.se>
parents: 13047
diff changeset
  1092
		return (not match_jid) or match_jid == "*" or jid_compare(jid, match_jid);
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1093
	end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1094
11890
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1095
	local group_by_host = true;
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1096
	for _, col in ipairs(columns) do
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1097
		if col.key == "full_jid" or col.key == "host" then
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1098
			group_by_host = false;
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1099
			break
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1100
		end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1101
	end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1102
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1103
	if not group_by_host then print(row()); end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1104
	local currenthost = nil;
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1105
11921
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1106
	local c2s_sessions = get_c2s();
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1107
	local total_count = #c2s_sessions;
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1108
	c2s_sessions:filter(match):sort(_sort_by_jid);
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1109
	local shown_count = #c2s_sessions;
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1110
	for _, session in ipairs(c2s_sessions) do
11890
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1111
		if group_by_host and session.host ~= currenthost then
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1112
			currenthost = session.host;
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1113
			print("#",prosody.hosts[currenthost] or "Unknown host");
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1114
			print(row());
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1115
		end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1116
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1117
		print(row(session));
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1118
	end
11921
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1119
	if total_count ~= shown_count then
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1120
		return true, ("%d out of %d c2s sessions shown"):format(shown_count, total_count);
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1121
	end
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1122
	return true, ("%d c2s sessions shown"):format(total_count);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1123
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1124
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1125
describe_command [[c2s:show_tls(jid) - Show TLS cipher info for encrypted sessions]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1126
function def_env.c2s:show_tls(match_jid)
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1127
	return self:show(match_jid, { "jid"; "id"; "secure"; "encryption" });
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1128
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1129
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1130
local function build_reason(text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1131
	if text or condition then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1132
		return {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1133
			text = text,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1134
			condition = condition or "undefined-condition",
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1135
		};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1136
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1137
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1138
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1139
describe_command [[c2s:close(jid) - Close all sessions for the specified JID]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1140
function def_env.c2s:close(match_jid, text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1141
	local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1142
	show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1143
		if jid == match_jid or jid_bare(jid) == match_jid then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1144
			count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1145
			session:close(build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1146
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1147
	end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1148
	return true, "Total: "..count.." sessions closed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1149
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1150
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1151
describe_command [[c2s:closeall() - Close all active c2s connections ]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1152
function def_env.c2s:closeall(text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1153
	local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1154
	--luacheck: ignore 212/jid
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1155
	show_c2s(function (jid, session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1156
		count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1157
		session:close(build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1158
	end);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1159
	return true, "Total: "..count.." sessions closed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1160
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1161
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1162
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1163
def_env.s2s = new_section("Commands to manage sessions between this server and others");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1164
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1165
local function _sort_s2s(a, b)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1166
	local a_local, a_remote = get_s2s_hosts(a);
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1167
	local b_local, b_remote = get_s2s_hosts(b);
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1168
	if (a_local or "") == (b_local or "") then return _sort_hosts(a_remote or "", b_remote or ""); end
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1169
	return _sort_hosts(a_local or "", b_local or "");
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1170
end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1171
13075
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1172
local function match_wildcard(match_jid, jid)
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1173
	-- host == host or (host) == *.(host) or sub(.host) == *(.host)
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1174
	return jid == match_jid or jid == match_jid:sub(3) or jid:sub(-#match_jid + 1) == match_jid:sub(2);
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1175
end
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1176
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1177
local function match_s2s_jid(session, match_jid)
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1178
	local host, remote = get_s2s_hosts(session);
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1179
	if not match_jid or match_jid == "*" then
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1180
		return true;
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1181
	elseif host == match_jid or remote == match_jid then
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1182
		return true;
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1183
	elseif match_jid:sub(1, 2) == "*." then
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1184
		return match_wildcard(match_jid, host) or match_wildcard(match_jid, remote);
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1185
	end
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1186
	return false;
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1187
end
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1188
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1189
describe_command [[s2s:show(domain, columns) - Show all s2s connections for the given domain (or all if no domain given)]]
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1190
function def_env.s2s:show(match_jid, colspec)
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1191
	local print = self.session.print;
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1192
	local columns = get_colspec(colspec, { "id"; "host"; "dir"; "remote"; "ipv"; "secure"; "s2s_sasl"; "dialback" });
12533
7dae6d29b71d prosodyctl shell: Communicate width of terminal to mod_admin_shell
Kim Alvefur <zash@zash.se>
parents: 12510
diff changeset
  1193
	local row = format_table(columns, self.session.width);
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1194
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1195
	local function match(session)
13075
b5a419ac0f84 mod_admin_shell: Factor apart wildcard matching into function for reuse
Kim Alvefur <zash@zash.se>
parents: 13066
diff changeset
  1196
		return match_s2s_jid(session, match_jid);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1197
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1198
11890
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1199
	local group_by_host = true;
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1200
	local currenthost = nil;
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1201
	for _, col in ipairs(columns) do
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1202
		if col.key == "host" then
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1203
			group_by_host = false;
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1204
			break
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1205
		end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1206
	end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1207
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1208
	if not group_by_host then print(row()); end
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1209
11921
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1210
	local s2s_sessions = array(iterators.values(module:shared"/*/s2s/sessions"));
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1211
	local total_count = #s2s_sessions;
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1212
	s2s_sessions:filter(match):sort(_sort_s2s);
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1213
	local shown_count = #s2s_sessions;
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1214
11890
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1215
	for _, session in ipairs(s2s_sessions) do
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1216
		if group_by_host and currenthost ~= get_s2s_hosts(session) then
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1217
			currenthost = get_s2s_hosts(session);
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1218
			print("#",prosody.hosts[currenthost] or "Unknown host");
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1219
			print(row());
b0b258e092da mod_admin_shell: Optionally group session listings by host when not included as column
Kim Alvefur <zash@zash.se>
parents: 11889
diff changeset
  1220
		end
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1221
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1222
		print(row(session));
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1223
	end
11921
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1224
	if total_count ~= shown_count then
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1225
		return true, ("%d out of %d s2s connections shown"):format(shown_count, total_count);
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1226
	end
d27b74b25105 mod_admin_shell: Return counts of shown vs total from new table views
Kim Alvefur <zash@zash.se>
parents: 11909
diff changeset
  1227
	return true, ("%d s2s connections shown"):format(total_count);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1228
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1229
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1230
describe_command [[s2s:show_tls(domain) - Show TLS cipher info for encrypted sessions]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1231
function def_env.s2s:show_tls(match_jid)
11889
197642f9972f mod_admin_shell: New table based implementation of c2s and s2s:show()
Kim Alvefur <zash@zash.se>
parents: 11854
diff changeset
  1232
	return self:show(match_jid, { "id"; "host"; "dir"; "remote"; "secure"; "encryption"; "cert" });
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1233
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1234
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1235
local function print_subject(print, subject)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1236
	for _, entry in ipairs(subject) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1237
		print(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1238
			("    %s: %q"):format(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1239
				entry.name or entry.oid,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1240
				entry.value:gsub("[\r\n%z%c]", " ")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1241
			)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1242
		);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1243
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1244
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1245
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1246
-- As much as it pains me to use the 0-based depths that OpenSSL does,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1247
-- I think there's going to be more confusion among operators if we
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1248
-- break from that.
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1249
local function print_errors(print, errors)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1250
	for depth, t in pairs(errors) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1251
		print(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1252
			("    %d: %s"):format(
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1253
				depth-1,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1254
				table.concat(t, "\n|        ")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1255
			)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1256
		);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1257
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1258
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1259
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1260
function def_env.s2s:showcert(domain)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1261
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1262
	local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1263
	local domain_sessions = set.new(array.collect(values(s2s_sessions)))
13076
7fcf41b541e0 mod_admin_shell: Use same wildcard matching in other s2s command
Kim Alvefur <zash@zash.se>
parents: 13075
diff changeset
  1264
		/function(session) return match_s2s_jid(session, domain) and session or nil; end;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1265
	local cert_set = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1266
	for session in domain_sessions do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1267
		local conn = session.conn;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1268
		conn = conn and conn:socket();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1269
		if not conn.getpeerchain then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1270
			if conn.dohandshake then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1271
				error("This version of LuaSec does not support certificate viewing");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1272
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1273
		else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1274
			local cert = conn:getpeercertificate();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1275
			if cert then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1276
				local certs = conn:getpeerchain();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1277
				local digest = cert:digest("sha1");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1278
				if not cert_set[digest] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1279
					local chain_valid, chain_errors = conn:getpeerverification();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1280
					cert_set[digest] = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1281
						{
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1282
						  from = session.from_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1283
						  to = session.to_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1284
						  direction = session.direction
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1285
						};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1286
						chain_valid = chain_valid;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1287
						chain_errors = chain_errors;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1288
						certs = certs;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1289
					};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1290
				else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1291
					table.insert(cert_set[digest], {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1292
						from = session.from_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1293
						to = session.to_host,
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1294
						direction = session.direction
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1295
					});
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1296
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1297
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1298
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1299
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1300
	local domain_certs = array.collect(values(cert_set));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1301
	-- Phew. We now have a array of unique certificates presented by domain.
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1302
	local n_certs = #domain_certs;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1303
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1304
	if n_certs == 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1305
		return "No certificates found for "..domain;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1306
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1307
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1308
	local function _capitalize_and_colon(byte)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1309
		return string.upper(byte)..":";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1310
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1311
	local function pretty_fingerprint(hash)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1312
		return hash:gsub("..", _capitalize_and_colon):sub(1, -2);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1313
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1314
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1315
	for cert_info in values(domain_certs) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1316
		local certs = cert_info.certs;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1317
		local cert = certs[1];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1318
		print("---")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1319
		print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1")));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1320
		print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1321
		local n_streams = #cert_info;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1322
		print("Currently used on "..n_streams.." stream"..(n_streams==1 and "" or "s")..":");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1323
		for _, stream in ipairs(cert_info) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1324
			if stream.direction == "incoming" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1325
				print("    "..stream.to.." <- "..stream.from);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1326
			else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1327
				print("    "..stream.from.." -> "..stream.to);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1328
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1329
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1330
		print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1331
		local chain_valid, errors = cert_info.chain_valid, cert_info.chain_errors;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1332
		local valid_identity = cert_verify_identity(domain, "xmpp-server", cert);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1333
		if chain_valid then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1334
			print("Trusted certificate: Yes");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1335
		else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1336
			print("Trusted certificate: No");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1337
			print_errors(print, errors);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1338
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1339
		print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1340
		print("Issuer: ");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1341
		print_subject(print, cert:issuer());
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1342
		print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1343
		print("Valid for "..domain..": "..(valid_identity and "Yes" or "No"));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1344
		print("Subject:");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1345
		print_subject(print, cert:subject());
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1346
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1347
	print("---");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1348
	return ("Showing "..n_certs.." certificate"
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1349
		..(n_certs==1 and "" or "s")
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1350
		.." presented by "..domain..".");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1351
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1352
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1353
describe_command [[s2s:close(from, to) - Close a connection from one domain to another]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1354
function def_env.s2s:close(from, to, text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1355
	local print, count = self.session.print, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1356
	local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1357
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1358
	local match_id;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1359
	if from and not to then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1360
		match_id, from = from, nil;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1361
	elseif not to then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1362
		return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1363
	elseif from == to then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1364
		return false, "Both from and to are the same... you can't do that :)";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1365
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1366
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1367
	for _, session in pairs(s2s_sessions) do
13076
7fcf41b541e0 mod_admin_shell: Use same wildcard matching in other s2s command
Kim Alvefur <zash@zash.se>
parents: 13075
diff changeset
  1368
		local id = session.id or (session.type .. tostring(session):match("[a-f0-9]+$"));
7fcf41b541e0 mod_admin_shell: Use same wildcard matching in other s2s command
Kim Alvefur <zash@zash.se>
parents: 13075
diff changeset
  1369
		if (match_id and match_id == id) or ((from and match_wildcard(from, session.to_host)) or (to and match_wildcard(to, session.to_host))) then
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1370
			print(("Closing connection from %s to %s [%s]"):format(session.from_host, session.to_host, id));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1371
			(session.close or s2smanager.destroy_session)(session, build_reason(text, condition));
13076
7fcf41b541e0 mod_admin_shell: Use same wildcard matching in other s2s command
Kim Alvefur <zash@zash.se>
parents: 13075
diff changeset
  1372
			count = count + 1;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1373
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1374
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1375
	return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1376
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1377
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1378
describe_command [[s2s:closeall(host) - Close all the incoming/outgoing s2s sessions to specified host]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1379
function def_env.s2s:closeall(host, text, condition)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1380
	local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1381
	local s2s_sessions = module:shared"/*/s2s/sessions";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1382
	for _,session in pairs(s2s_sessions) do
13076
7fcf41b541e0 mod_admin_shell: Use same wildcard matching in other s2s command
Kim Alvefur <zash@zash.se>
parents: 13075
diff changeset
  1383
		if not host or host == "*" or match_s2s_jid(session, host) then
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1384
			session:close(build_reason(text, condition));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1385
			count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1386
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1387
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1388
	if count == 0 then return false, "No sessions to close.";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1389
	else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1390
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1391
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1392
def_env.host = new_section("Commands to activate, deactivate and list virtual hosts");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1393
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1394
describe_command [[host:activate(hostname) - Activates the specified host]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1395
function def_env.host:activate(hostname, config)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1396
	return hostmanager.activate(hostname, config);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1397
end
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1398
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1399
describe_command [[host:deactivate(hostname) - Disconnects all clients on this host and deactivates]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1400
function def_env.host:deactivate(hostname, reason)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1401
	return hostmanager.deactivate(hostname, reason);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1402
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1403
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1404
describe_command [[host:list() - List the currently-activated hosts]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1405
function def_env.host:list()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1406
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1407
	local i = 0;
12680
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1408
	local host_type;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1409
	for host, host_session in iterators.sorted_pairs(prosody.hosts, _sort_hosts) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1410
		i = i + 1;
12680
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1411
		host_type = host_session.type;
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1412
		if host_type == "local" then
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1413
			print(host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1414
		else
12680
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1415
			host_type = module:context(host):get_option_string("component_module", host_type);
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1416
			if host_type ~= "component" then
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1417
				host_type = host_type .. " component";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1418
			end
12680
3ab3ef9584e3 mod_admin_shell: Rename variable to avoid confusion with global function
Kim Alvefur <zash@zash.se>
parents: 12679
diff changeset
  1419
			print(("%s (%s)"):format(host, host_type));
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1420
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1421
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1422
	return true, i.." hosts";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1423
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1424
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1425
def_env.port = new_section("Commands to manage ports the server is listening on");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1426
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1427
describe_command [[port:list() - Lists all network ports prosody currently listens on]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1428
function def_env.port:list()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1429
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1430
	local services = portmanager.get_active_services().data;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1431
	local n_services, n_ports = 0, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1432
	for service, interfaces in iterators.sorted_pairs(services) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1433
		n_services = n_services + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1434
		local ports_list = {};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1435
		for interface, ports in pairs(interfaces) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1436
			for port in pairs(ports) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1437
				table.insert(ports_list, "["..interface.."]:"..port);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1438
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1439
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1440
		n_ports = n_ports + #ports_list;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1441
		print(service..": "..table.concat(ports_list, ", "));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1442
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1443
	return true, n_services.." services listening on "..n_ports.." ports";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1444
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1445
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1446
describe_command [[port:close(port, interface) - Close a port]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1447
function def_env.port:close(close_port, close_interface)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1448
	close_port = assert(tonumber(close_port), "Invalid port number");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1449
	local n_closed = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1450
	local services = portmanager.get_active_services().data;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1451
	for service, interfaces in pairs(services) do -- luacheck: ignore 213
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1452
		for interface, ports in pairs(interfaces) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1453
			if not close_interface or close_interface == interface then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1454
				if ports[close_port] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1455
					self.session.print("Closing ["..interface.."]:"..close_port.."...");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1456
					local ok, err = portmanager.close(interface, close_port)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1457
					if not ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1458
						self.session.print("Failed to close "..interface.." "..close_port..": "..err);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1459
					else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1460
						n_closed = n_closed + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1461
					end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1462
				end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1463
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1464
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1465
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1466
	return true, "Closed "..n_closed.." ports";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1467
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1468
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1469
def_env.muc = new_section("Commands to create, list and manage chat rooms");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1470
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1471
local console_room_mt = {
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1472
	__index = function (self, k) return self.room[k]; end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1473
	__tostring = function (self)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1474
		return "MUC room <"..self.room.jid..">";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1475
	end;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1476
};
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1477
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1478
local function check_muc(jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1479
	local room_name, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1480
	if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1481
		return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1482
	elseif not prosody.hosts[host].modules.muc then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1483
		return nil, "Host '"..host.."' is not a MUC service";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1484
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1485
	return room_name, host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1486
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1487
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1488
local function get_muc(room_jid)
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1489
	local room_name, host = check_muc(room_jid);
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1490
	if not room_name then
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1491
		return room_name, host;
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1492
	end
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1493
	local room_obj = prosody.hosts[host].modules.muc.get_room_from_jid(room_jid);
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1494
	if not room_obj then
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1495
		return nil, "No such room: "..room_jid;
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1496
	end
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1497
	return room_obj;
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1498
end
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1499
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1500
local muc_util = module:require"muc/util";
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1501
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1502
describe_command [[muc:create(roomjid, { config }) - Create the specified MUC room with the given config]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1503
function def_env.muc:create(room_jid, config)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1504
	local room_name, host = check_muc(room_jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1505
	if not room_name then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1506
		return room_name, host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1507
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1508
	if not room_name then return nil, host end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1509
	if config ~= nil and type(config) ~= "table" then return nil, "Config must be a table"; end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1510
	if prosody.hosts[host].modules.muc.get_room_from_jid(room_jid) then return nil, "Room exists already" end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1511
	return prosody.hosts[host].modules.muc.create_room(room_jid, config);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1512
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1513
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1514
describe_command [[muc:room(roomjid) - Reference the specified MUC room to access MUC API methods]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1515
function def_env.muc:room(room_jid)
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1516
	local room_obj, err = get_muc(room_jid);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1517
	if not room_obj then
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1518
		return room_obj, err;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1519
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1520
	return setmetatable({ room = room_obj }, console_room_mt);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1521
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1522
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1523
describe_command [[muc:list(host) - List rooms on the specified MUC component]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1524
function def_env.muc:list(host)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1525
	local host_session = prosody.hosts[host];
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1526
	if not host_session or not host_session.modules.muc then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1527
		return nil, "Please supply the address of a local MUC component";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1528
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1529
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1530
	local c = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1531
	for room in host_session.modules.muc.each_room() do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1532
		print(room.jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1533
		c = c + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1534
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1535
	return true, c.." rooms";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1536
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1537
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1538
describe_command [[muc:occupants(roomjid, filter) - List room occupants, optionally filtered on substring or role]]
12869
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1539
function def_env.muc:occupants(room_jid, filter)
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1540
	local room_obj, err = get_muc(room_jid);
12869
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1541
	if not room_obj then
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1542
		return room_obj, err;
12869
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1543
	end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1544
12869
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1545
	local print = self.session.print;
12872
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1546
	local row = format_table({
12873
70ee82579076 mod_admin_shell: Make Role and Affiliation columns the same width for aesthetics
Kim Alvefur <zash@zash.se>
parents: 12872
diff changeset
  1547
		{ title = "Role"; width = 12; key = "role" }; -- longest role name
12872
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1548
		{ title = "JID"; width = "75%"; key = "bare_jid" };
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1549
		{ title = "Nickname"; width = "25%"; key = "nick"; mapper = jid_resource };
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1550
	}, self.session.width);
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1551
	local occupants = array.collect(iterators.select(2, room_obj:each_occupant()));
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1552
	local total = #occupants;
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1553
	if filter then
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1554
		occupants:filter(function(occupant)
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1555
			return occupant.role == filter or jid_resource(occupant.nick):find(filter, 1, true);
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1556
		end);
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1557
	end
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1558
	local displayed = #occupants;
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1559
	occupants:sort(function(a, b)
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1560
		if a.role ~= b.role then
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1561
			return muc_util.valid_roles[a.role] > muc_util.valid_roles[b.role];
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1562
		else
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1563
			return a.bare_jid < b.bare_jid;
12872
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1564
		end
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1565
	end);
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1566
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1567
	if displayed == 0 then
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1568
		return true, ("%d out of %d occupant%s listed"):format(displayed, total, total ~= 1 and "s" or "")
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1569
	end
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1570
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1571
	print(row());
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1572
	for _, occupant in ipairs(occupants) do
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1573
		print(row(occupant));
12869
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1574
	end
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1575
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1576
	if total == displayed then
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1577
		return true, ("%d occupant%s listed"):format(total, total ~= 1 and "s" or "")
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1578
	else
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1579
		return true, ("%d out of %d occupant%s listed"):format(displayed, total, total ~= 1 and "s" or "")
e6324117f124 mod_admin_shell: Add muc:occupants(room) command to list occupants
Kim Alvefur <zash@zash.se>
parents: 12793
diff changeset
  1580
	end
12017
ae45f052b34b mod_admin_shell: Add command for updating roles user:roles(jid, roles)
Kim Alvefur <zash@zash.se>
parents: 12016
diff changeset
  1581
end
ae45f052b34b mod_admin_shell: Add command for updating roles user:roles(jid, roles)
Kim Alvefur <zash@zash.se>
parents: 12016
diff changeset
  1582
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1583
describe_command [[muc:affiliations(roomjid, filter) - List affiliated members of the room, optionally filtered on substring or affiliation]]
12870
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1584
function def_env.muc:affiliations(room_jid, filter)
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1585
	local room_obj, err = get_muc(room_jid);
12870
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1586
	if not room_obj then
12871
2defb0fc2be9 mod_admin_shell: Factor out room retrieval into common function
Kim Alvefur <zash@zash.se>
parents: 12870
diff changeset
  1587
		return room_obj, err;
12870
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1588
	end
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1589
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1590
	local print = self.session.print;
12872
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1591
	local row = format_table({
12873
70ee82579076 mod_admin_shell: Make Role and Affiliation columns the same width for aesthetics
Kim Alvefur <zash@zash.se>
parents: 12872
diff changeset
  1592
		{ title = "Affiliation"; width = 12 }; -- longest affiliation name
12872
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1593
		{ title = "JID"; width = "75%" };
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1594
		{ title = "Nickname"; width = "25%"; key = "reserved_nickname" };
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1595
	}, self.session.width);
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1596
	local affiliated = array();
12870
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1597
	for affiliated_jid, affiliation, affiliation_data in room_obj:each_affiliation() do
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1598
		affiliated:push(setmetatable({ affiliation; affiliated_jid }, { __index = affiliation_data }));
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1599
	end
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1600
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1601
	local total = #affiliated;
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1602
	if filter then
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1603
		affiliated:filter(function(affiliation)
12875
885323e2a1ce mod_admin_shell: Match substring in muc:affiliations() like muc:occupants()
Kim Alvefur <zash@zash.se>
parents: 12874
diff changeset
  1604
			return filter == affiliation[1] or affiliation[2]:find(filter, 1, true);
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1605
		end);
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1606
	end
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1607
	local displayed = #affiliated;
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1608
	local aff_ranking = muc_util.valid_affiliations;
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1609
	affiliated:sort(function(a, b)
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1610
		if a[1] ~= b[1] then
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1611
			return aff_ranking[a[1]] > aff_ranking[b[1]];
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1612
		else
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1613
			return a[2] < b[2];
12872
d5cb86b84d12 mod_admin_shell: Use tables to present MUC users
Kim Alvefur <zash@zash.se>
parents: 12871
diff changeset
  1614
		end
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1615
	end);
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1616
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1617
	if displayed == 0 then
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1618
		return true, ("%d out of %d affiliations%s listed"):format(displayed, total, total ~= 1 and "s" or "")
12870
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1619
	end
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1620
12874
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1621
	print(row());
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1622
	for _, affiliation in ipairs(affiliated) do
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1623
		print(row(affiliation));
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1624
	end
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1625
56397f3b58c1 mod_admin_shell: Sort MUC users by relation and JID
Kim Alvefur <zash@zash.se>
parents: 12873
diff changeset
  1626
12870
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1627
	if total == displayed then
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1628
		return true, ("%d affiliation%s listed"):format(total, total ~= 1 and "s" or "")
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1629
	else
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1630
		return true, ("%d out of %d affiliation%s listed"):format(displayed, total, total ~= 1 and "s" or "")
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1631
	end
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1632
end
54aea2622459 mod_admin_shell: Add muc:affiliations(room) command to list memberships
Kim Alvefur <zash@zash.se>
parents: 12869
diff changeset
  1633
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
  1634
local um = require"prosody.core.usermanager";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1635
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1636
def_env.user = new_section("Commands to create and delete users, and change their passwords");
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1637
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1638
describe_command [[user:create(jid, password, role) - Create the specified user account]]
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1639
function def_env.user:create(jid, password, role)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1640
	local username, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1641
	if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1642
		return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1643
	elseif um.user_exists(username, host) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1644
		return nil, "User exists";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1645
	end
12676
c8f59ce7d3cf mod_admin_shell: Ensure account has role before it is usable
Kim Alvefur <zash@zash.se>
parents: 12674
diff changeset
  1646
13174
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1647
	if not role then
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1648
		role = module:get_option_string("default_provisioned_role", "prosody:member");
12676
c8f59ce7d3cf mod_admin_shell: Ensure account has role before it is usable
Kim Alvefur <zash@zash.se>
parents: 12674
diff changeset
  1649
	end
c8f59ce7d3cf mod_admin_shell: Ensure account has role before it is usable
Kim Alvefur <zash@zash.se>
parents: 12674
diff changeset
  1650
13174
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1651
	local ok, err = um.create_user_with_role(username, password, host, role);
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1652
	if not ok then
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1653
		return nil, "Could not create user: "..err;
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1654
	end
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1655
082c7d856e61 core, plugins: Split prosody:user role into prosody:{guest,registered,member}
Matthew Wild <mwild1@gmail.com>
parents: 13136
diff changeset
  1656
	return true, ("Created %s with role '%s'"):format(jid, role);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1657
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1658
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1659
describe_command [[user:disable(jid) - Disable the specified user account, preventing login]]
12912
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1660
function def_env.user:disable(jid)
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1661
	local username, host = jid_split(jid);
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1662
	if not prosody.hosts[host] then
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1663
		return nil, "No such host: "..host;
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1664
	elseif not um.user_exists(username, host) then
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1665
		return nil, "No such user";
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1666
	end
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1667
	local ok, err = um.disable_user(username, host);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1668
	if ok then
12912
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1669
		return true, "User disabled";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1670
	else
12912
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1671
		return nil, "Could not disable user: "..err;
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1672
	end
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1673
end
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1674
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1675
describe_command [[user:enable(jid) - Enable the specified user account, restoring login access]]
12912
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1676
function def_env.user:enable(jid)
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1677
	local username, host = jid_split(jid);
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1678
	if not prosody.hosts[host] then
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1679
		return nil, "No such host: "..host;
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1680
	elseif not um.user_exists(username, host) then
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1681
		return nil, "No such user";
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1682
	end
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1683
	local ok, err = um.enable_user(username, host);
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1684
	if ok then
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1685
		return true, "User enabled";
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1686
	else
e96c3ea64996 mod_admin_shell: Add commands to disable and enable accounts
Kim Alvefur <zash@zash.se>
parents: 12892
diff changeset
  1687
		return nil, "Could not enable user: "..err;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1688
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1689
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1690
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1691
describe_command [[user:delete(jid) - Permanently remove the specified user account]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1692
function def_env.user:delete(jid)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1693
	local username, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1694
	if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1695
		return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1696
	elseif not um.user_exists(username, host) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1697
		return nil, "No such user";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1698
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1699
	local ok, err = um.delete_user(username, host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1700
	if ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1701
		return true, "User deleted";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1702
	else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1703
		return nil, "Could not delete user: "..err;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1704
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1705
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1706
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1707
describe_command [[user:password(jid, password) - Set the password for the specified user account]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1708
function def_env.user:password(jid, password)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1709
	local username, host = jid_split(jid);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1710
	if not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1711
		return nil, "No such host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1712
	elseif not um.user_exists(username, host) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1713
		return nil, "No such user";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1714
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1715
	local ok, err = um.set_password(username, password, host, nil);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1716
	if ok then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1717
		return true, "User password changed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1718
	else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1719
		return nil, "Could not change password for user: "..err;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1720
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1721
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1722
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1723
describe_command [[user:roles(jid, host) - Show current roles for an user]]
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1724
function def_env.user:role(jid, host)
12679
db8c795ca81a mod_admin_shell: Fix output from user:roles()
Kim Alvefur <zash@zash.se>
parents: 12676
diff changeset
  1725
	local print = self.session.print;
12213
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1726
	local username, userhost = jid_split(jid);
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1727
	if host == nil then host = userhost; end
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1728
	if not prosody.hosts[host] then
12213
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1729
		return nil, "No such host: "..host;
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1730
	elseif prosody.hosts[userhost] and not um.user_exists(username, userhost) then
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1731
		return nil, "No such user";
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1732
	end
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1733
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1734
	local primary_role = um.get_user_role(username, host);
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1735
	local secondary_roles = um.get_user_secondary_roles(username, host);
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1736
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1737
	print(primary_role and primary_role.name or "<none>");
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1738
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1739
	local count = primary_role and 1 or 0;
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1740
	for role_name in pairs(secondary_roles or {}) do
12213
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1741
		count = count + 1;
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1742
		print(role_name.." (secondary)");
12213
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1743
	end
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1744
12213
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1745
	return true, count == 1 and "1 role" or count.." roles";
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1746
end
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1747
def_env.user.roles = def_env.user.role;
12213
3fe2e5da05c3 mod_admin_shell: Add command to show current user roles
Kim Alvefur <zash@zash.se>
parents: 12212
diff changeset
  1748
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1749
describe_command [[user:setrole(jid, host, role) - Set primary role of a user (see 'help roles')]]
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1750
-- user:setrole("someone@example.com", "example.com", "prosody:admin")
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1751
-- user:setrole("someone@example.com", "prosody:admin")
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1752
function def_env.user:setrole(jid, host, new_role)
12018
efbf288b529e mod_admin_shell: Support setting roles on hosts other than the users'
Kim Alvefur <zash@zash.se>
parents: 12017
diff changeset
  1753
	local username, userhost = jid_split(jid);
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1754
	if new_role == nil then host, new_role = userhost, host; end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1755
	if not prosody.hosts[host] then
12017
ae45f052b34b mod_admin_shell: Add command for updating roles user:roles(jid, roles)
Kim Alvefur <zash@zash.se>
parents: 12016
diff changeset
  1756
		return nil, "No such host: "..host;
12022
c65789f5004e mod_admin_shell: Only check that local users exist locally
Kim Alvefur <zash@zash.se>
parents: 12018
diff changeset
  1757
	elseif prosody.hosts[userhost] and not um.user_exists(username, userhost) then
12017
ae45f052b34b mod_admin_shell: Add command for updating roles user:roles(jid, roles)
Kim Alvefur <zash@zash.se>
parents: 12016
diff changeset
  1758
		return nil, "No such user";
ae45f052b34b mod_admin_shell: Add command for updating roles user:roles(jid, roles)
Kim Alvefur <zash@zash.se>
parents: 12016
diff changeset
  1759
	end
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1760
	return um.set_user_role(username, host, new_role);
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1761
end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1762
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1763
describe_command [[user:addrole(jid, host, role) - Add a secondary role to a user]]
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1764
function def_env.user:addrole(jid, host, new_role)
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1765
	local username, userhost = jid_split(jid);
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1766
	if new_role == nil then host, new_role = userhost, host; end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1767
	if not prosody.hosts[host] then
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1768
		return nil, "No such host: "..host;
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1769
	elseif prosody.hosts[userhost] and not um.user_exists(username, userhost) then
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1770
		return nil, "No such user";
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1771
	end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1772
	return um.add_user_secondary_role(username, host, new_role);
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1773
end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1774
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1775
describe_command [[user:delrole(jid, host, role) - Remove a secondary role from a user]]
12672
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1776
function def_env.user:delrole(jid, host, role_name)
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1777
	local username, userhost = jid_split(jid);
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1778
	if role_name == nil then host, role_name = userhost, host; end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1779
	if not prosody.hosts[host] then
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1780
		return nil, "No such host: "..host;
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1781
	elseif prosody.hosts[userhost] and not um.user_exists(username, userhost) then
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1782
		return nil, "No such user";
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1783
	end
5d85de8b0723 mod_admin_shell: Update with new role management commands and help text
Matthew Wild <mwild1@gmail.com>
parents: 12664
diff changeset
  1784
	return um.remove_user_secondary_role(username, host, role_name);
12017
ae45f052b34b mod_admin_shell: Add command for updating roles user:roles(jid, roles)
Kim Alvefur <zash@zash.se>
parents: 12016
diff changeset
  1785
end
12016
71d799a8638f mod_admin_shell: Allow setting roles when creating user
Kim Alvefur <zash@zash.se>
parents: 11995
diff changeset
  1786
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1787
describe_command [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]]
12016
71d799a8638f mod_admin_shell: Allow setting roles when creating user
Kim Alvefur <zash@zash.se>
parents: 11995
diff changeset
  1788
-- TODO switch to table view, include roles
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1789
function def_env.user:list(host, pat)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1790
	if not host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1791
		return nil, "No host given";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1792
	elseif not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1793
		return nil, "No such host";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1794
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1795
	local print = self.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1796
	local total, matches = 0, 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1797
	for user in um.users(host) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1798
		if not pat or user:match(pat) then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1799
			print(user.."@"..host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1800
			matches = matches + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1801
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1802
		total = total + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1803
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1804
	return true, "Showing "..(pat and (matches.." of ") or "all " )..total.." users";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1805
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1806
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  1807
def_env.xmpp = new_section("Commands for sending XMPP stanzas");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1808
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1809
describe_command [[xmpp:ping(localhost, remotehost) - Sends a ping to a remote XMPP server and reports the response]]
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
  1810
local new_id = require "prosody.util.id".medium;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1811
function def_env.xmpp:ping(localhost, remotehost, timeout)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1812
	localhost = select(2, jid_split(localhost));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1813
	remotehost = select(2, jid_split(remotehost));
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1814
	if not localhost then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1815
		return nil, "Invalid sender hostname";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1816
	elseif not prosody.hosts[localhost] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1817
		return nil, "No such local host";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1818
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1819
	if not remotehost then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1820
		return nil, "Invalid destination hostname";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1821
	elseif prosody.hosts[remotehost] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1822
		return nil, "Both hosts are local";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1823
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1824
	local iq = st.iq{ from=localhost, to=remotehost, type="get", id=new_id()}
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1825
			:tag("ping", {xmlns="urn:xmpp:ping"});
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1826
	local time_start = time.now();
12126
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1827
	local print = self.session.print;
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1828
	local function onchange(what)
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1829
		return function(event)
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1830
			local s2s_session = event.session;
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1831
			if (s2s_session.from_host == localhost and s2s_session.to_host == remotehost)
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1832
				or (s2s_session.to_host == localhost and s2s_session.from_host == remotehost) then
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1833
				local dir = available_columns.dir.mapper(s2s_session.direction, s2s_session);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1834
				print(("Session %s (%s%s%s) %s (%gs)"):format(s2s_session.id, localhost, dir, remotehost, what,
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1835
					time.now() - time_start));
12130
0d8e6646ce42 mod_admin_shell: Log creation of incoming s2s connections during ping
Kim Alvefur <zash@zash.se>
parents: 12126
diff changeset
  1836
			elseif s2s_session.type == "s2sin_unauthed" and s2s_session.to_host == nil and s2s_session.from_host == nil then
0d8e6646ce42 mod_admin_shell: Log creation of incoming s2s connections during ping
Kim Alvefur <zash@zash.se>
parents: 12126
diff changeset
  1837
				print(("Session %s %s (%gs)"):format(s2s_session.id, what, time.now() - time_start));
12126
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1838
			end
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1839
		end
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1840
	end
12285
9016071867d7 mod_admin_shell: Track connected events instead of created
Kim Alvefur <zash@zash.se>
parents: 12262
diff changeset
  1841
	local onconnected = onchange("connected");
12126
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1842
	local onauthenticated = onchange("authenticated");
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1843
	local onestablished = onchange("established");
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1844
	local ondestroyed = onchange("destroyed");
12285
9016071867d7 mod_admin_shell: Track connected events instead of created
Kim Alvefur <zash@zash.se>
parents: 12262
diff changeset
  1845
	module:hook("s2s-connected", onconnected, 1);
12126
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1846
	module:context(localhost):hook("s2s-authenticated", onauthenticated, 1);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1847
	module:hook("s2sout-established", onestablished, 1);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1848
	module:hook("s2sin-established", onestablished, 1);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1849
	module:hook("s2s-destroyed", ondestroyed, 1);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1850
	return module:context(localhost):send_iq(iq, nil, timeout):finally(function()
12285
9016071867d7 mod_admin_shell: Track connected events instead of created
Kim Alvefur <zash@zash.se>
parents: 12262
diff changeset
  1851
		module:unhook("s2s-connected", onconnected, 1);
12126
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1852
		module:context(localhost):unhook("s2s-authenticated", onauthenticated);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1853
		module:unhook("s2sout-established", onestablished);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1854
		module:unhook("s2sin-established", onestablished);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1855
		module:unhook("s2s-destroyed", ondestroyed);
50795249b7be mod_admin_shell: Print s2s related events while waiting for ping
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
  1856
	end):next(function(pong)
12555
3b7e97e0a8ef mod_admin_shell: Show session id ping reply came
Kim Alvefur <zash@zash.se>
parents: 12544
diff changeset
  1857
		return ("pong from %s on %s in %gs"):format(pong.stanza.attr.from, pong.origin.id, time.now() - time_start);
11957
848a522fd731 mod_admin_shell: Remove now redundant promise awaiting in xmpp:ping()
Kim Alvefur <zash@zash.se>
parents: 11954
diff changeset
  1858
	end);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1859
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1860
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1861
def_env.dns = new_section("Commands to manage and inspect the internal DNS resolver");
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
  1862
local adns = require"prosody.net.adns";
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1863
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1864
local function get_resolver(session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1865
	local resolver = session.dns_resolver;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1866
	if not resolver then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1867
		resolver = adns.resolver();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1868
		session.dns_resolver = resolver;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1869
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1870
	return resolver;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1871
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1872
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1873
describe_command [[dns:lookup(name, type, class) - Do a DNS lookup]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1874
function def_env.dns:lookup(name, typ, class)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1875
	local resolver = get_resolver(self.session);
11958
b963ac00c967 mod_admin_shell: Remove now redundant promise awaiting in dns:lookup()
Kim Alvefur <zash@zash.se>
parents: 11957
diff changeset
  1876
	return resolver:lookup_promise(name, typ, class)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1877
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1878
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1879
describe_command [[dns:addnameserver(nameserver) - Add a nameserver to the list]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1880
function def_env.dns:addnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1881
	local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1882
	resolver._resolver:addnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1883
	return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1884
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1885
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1886
describe_command [[dns:setnameserver(nameserver) - Replace the list of name servers with the supplied one]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1887
function def_env.dns:setnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1888
	local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1889
	resolver._resolver:setnameserver(...)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1890
	return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1891
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1892
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1893
describe_command [[dns:purge() - Clear the DNS cache]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1894
function def_env.dns:purge()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1895
	local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1896
	resolver._resolver:purge()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1897
	return true
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1898
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1899
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1900
describe_command [[dns:cache() - Show cached records]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1901
function def_env.dns:cache()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1902
	local resolver = get_resolver(self.session);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1903
	return true, "Cache:\n"..tostring(resolver._resolver.cache)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1904
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1905
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1906
def_env.http = new_section("Commands to inspect HTTP services");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1907
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1908
describe_command [[http:list(hosts) - Show HTTP endpoints]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1909
function def_env.http:list(hosts)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1910
	local print = self.session.print;
11365
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1911
	hosts = array.collect(set.new({ not hosts and "*" or nil }) + get_hosts_set(hosts)):sort(_sort_hosts);
13121
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1912
	local output_simple = format_table({
13112
6cd768e6ac7c mod_admin_shell: Show internal URL in addition to external in http:list
Kim Alvefur <zash@zash.se>
parents: 13109
diff changeset
  1913
		{ title = "Module"; width = "1p" };
13121
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1914
		{ title = "External URL"; width = "6p" };
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1915
	}, self.session.width);
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1916
	local output_split = format_table({
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1917
		{ title = "Module"; width = "1p" };
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1918
		{ title = "External URL"; width = "3p" };
13112
6cd768e6ac7c mod_admin_shell: Show internal URL in addition to external in http:list
Kim Alvefur <zash@zash.se>
parents: 13109
diff changeset
  1919
		{ title = "Internal URL"; width = "3p" };
6cd768e6ac7c mod_admin_shell: Show internal URL in addition to external in http:list
Kim Alvefur <zash@zash.se>
parents: 13109
diff changeset
  1920
	}, self.session.width);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1921
11365
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1922
	for _, host in ipairs(hosts) do
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1923
		local http_apps = modulemanager.get_items("http-provider", host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1924
		if #http_apps > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1925
			local http_host = module:context(host):get_option_string("http_host");
11365
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1926
			if host == "*" then
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1927
				print("Global HTTP endpoints available on all hosts:");
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1928
			else
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1929
				print("HTTP endpoints on "..host..(http_host and (" (using "..http_host.."):") or ":"));
dab1a6e46087 mod_admin_shell: List global HTTP endpoints by default
Kim Alvefur <zash@zash.se>
parents: 11046
diff changeset
  1930
			end
13121
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1931
			print(output_split());
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1932
			for _, provider in ipairs(http_apps) do
11366
52d93fba2ee1 mod_admin_shell: List modules providing each HTTP endpoint
Kim Alvefur <zash@zash.se>
parents: 11365
diff changeset
  1933
				local mod = provider._provided_by;
13112
6cd768e6ac7c mod_admin_shell: Show internal URL in addition to external in http:list
Kim Alvefur <zash@zash.se>
parents: 13109
diff changeset
  1934
				local external = module:context(host):http_url(provider.name, provider.default_path);
6cd768e6ac7c mod_admin_shell: Show internal URL in addition to external in http:list
Kim Alvefur <zash@zash.se>
parents: 13109
diff changeset
  1935
				local internal = module:context(host):http_url(provider.name, provider.default_path, "internal");
6cd768e6ac7c mod_admin_shell: Show internal URL in addition to external in http:list
Kim Alvefur <zash@zash.se>
parents: 13109
diff changeset
  1936
				if external==internal then internal="" end
11366
52d93fba2ee1 mod_admin_shell: List modules providing each HTTP endpoint
Kim Alvefur <zash@zash.se>
parents: 11365
diff changeset
  1937
				mod = mod and "mod_"..mod or ""
13121
7eb6244b4984 mod_admin_shell: Show internal URL where different from external
Kim Alvefur <zash@zash.se>
parents: 13112
diff changeset
  1938
				print((internal=="" and output_simple or output_split){mod, external, internal});
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1939
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1940
			print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1941
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1942
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1943
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1944
	local default_host = module:get_option_string("http_default_host");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1945
	if not default_host then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1946
		print("HTTP requests to unknown hosts will return 404 Not Found");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1947
	else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1948
		print("HTTP requests to unknown hosts will be handled by "..default_host);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1949
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1950
	return true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1951
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1952
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1953
def_env.watch = new_section("Commands for watching live logs from the server");
12403
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1954
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1955
describe_command [[watch:log() - Follow debug logs]]
12403
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1956
function def_env.watch:log()
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1957
	local writing = false;
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1958
	local sink = logger.add_simple_sink(function (source, level, message)
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1959
		if writing then return; end
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1960
		writing = true;
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1961
		self.session.print(source, level, message);
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1962
		writing = false;
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1963
	end);
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1964
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1965
	while self.session.is_connected() do
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1966
		async.sleep(3);
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1967
	end
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1968
	if not logger.remove_sink(sink) then
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1969
		module:log("warn", "Unable to remove watch:log() sink");
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1970
	end
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1971
end
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  1972
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  1973
describe_command [[watch:stanzas(target, filter) - Watch live stanzas matching the specified target and filter]]
12468
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1974
local stanza_watchers = module:require("mod_debug_stanzas/watcher");
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1975
function def_env.watch:stanzas(target_spec, filter_spec)
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1976
	local function handler(event_type, stanza, session)
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1977
		if stanza then
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1978
			if event_type == "sent" then
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1979
				self.session.print(("\n<!-- sent to %s -->"):format(session.id));
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1980
			elseif event_type == "received" then
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1981
				self.session.print(("\n<!-- received from %s -->"):format(session.id));
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1982
			else
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1983
				self.session.print(("\n<!-- %s (%s) -->"):format(event_type, session.id));
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1984
			end
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1985
			self.session.print(stanza);
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1986
		elseif session then
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1987
			self.session.print("\n<!-- session "..session.id.." "..event_type.." -->");
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1988
		elseif event_type then
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1989
			self.session.print("\n<!-- "..event_type.." -->");
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1990
		end
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1991
	end
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1992
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1993
	stanza_watchers.add({
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1994
		target_spec = {
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1995
			jid = target_spec;
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1996
		};
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1997
		filter_spec = filter_spec and {
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1998
			with_jid = filter_spec;
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  1999
		};
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2000
	}, handler);
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2001
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2002
	while self.session.is_connected() do
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2003
		async.sleep(3);
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2004
	end
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2005
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2006
	stanza_watchers.remove(handler);
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2007
end
12403
d99772b739e0 mod_admin_shell: Add watch:log() command to tail logs in realtime
Matthew Wild <mwild1@gmail.com>
parents: 12402
diff changeset
  2008
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  2009
def_env.debug = new_section("Commands for debugging the server");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2010
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  2011
describe_command [[debug:logevents(host) - Enable logging of fired events on host]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2012
function def_env.debug:logevents(host)
13108
8c786880e28d mod_admin_shell: Allow logging global events with debug:logevents("*")
Kim Alvefur <zash@zash.se>
parents: 13093
diff changeset
  2013
	if host == "*" then
8c786880e28d mod_admin_shell: Allow logging global events with debug:logevents("*")
Kim Alvefur <zash@zash.se>
parents: 13093
diff changeset
  2014
		helpers.log_events(prosody.events);
13109
7d9e26003b05 mod_admin_shell: Allow logging HTTP events with debug:logevents("http")
Kim Alvefur <zash@zash.se>
parents: 13108
diff changeset
  2015
	elseif host == "http" then
7d9e26003b05 mod_admin_shell: Allow logging HTTP events with debug:logevents("http")
Kim Alvefur <zash@zash.se>
parents: 13108
diff changeset
  2016
		helpers.log_events(require "prosody.net.http.server"._events);
7d9e26003b05 mod_admin_shell: Allow logging HTTP events with debug:logevents("http")
Kim Alvefur <zash@zash.se>
parents: 13108
diff changeset
  2017
		return true
13108
8c786880e28d mod_admin_shell: Allow logging global events with debug:logevents("*")
Kim Alvefur <zash@zash.se>
parents: 13093
diff changeset
  2018
	else
8c786880e28d mod_admin_shell: Allow logging global events with debug:logevents("*")
Kim Alvefur <zash@zash.se>
parents: 13093
diff changeset
  2019
		helpers.log_host_events(host);
8c786880e28d mod_admin_shell: Allow logging global events with debug:logevents("*")
Kim Alvefur <zash@zash.se>
parents: 13093
diff changeset
  2020
	end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2021
	return true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2022
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2023
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  2024
describe_command [[debug:events(host, event) - Show registered event handlers]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2025
function def_env.debug:events(host, event)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2026
	local events_obj;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2027
	if host and host ~= "*" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2028
		if host == "http" then
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
  2029
			events_obj = require "prosody.net.http.server"._events;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2030
		elseif not prosody.hosts[host] then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2031
			return false, "Unknown host: "..host;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2032
		else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2033
			events_obj = prosody.hosts[host].events;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2034
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2035
	else
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2036
		events_obj = prosody.events;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2037
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2038
	return true, helpers.show_events(events_obj, event);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2039
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2040
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  2041
describe_command [[debug:timers() - Show information about scheduled timers]]
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2042
function def_env.debug:timers()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2043
	local print = self.session.print;
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
  2044
	local add_task = require"prosody.util.timer".add_task;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2045
	local h, params = add_task.h, add_task.params;
10992
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2046
	local function normalize_time(t)
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2047
		return t;
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2048
	end
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2049
	local function format_time(t)
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2050
		return os.date("%F %T", math.floor(normalize_time(t)));
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2051
	end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2052
	if h then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2053
		print("-- util.timer");
10990
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2054
	elseif server.timer then
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2055
		print("-- net.server.timer");
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2056
		h = server.timer.add_task.timers;
10992
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2057
		normalize_time = server.timer.to_absolute_time or normalize_time;
10990
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2058
	end
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2059
	if h then
11482
c0c4431ab27b mod_admin_shell: Sort timers by time in debug:timers()
Kim Alvefur <zash@zash.se>
parents: 11369
diff changeset
  2060
		local timers = {};
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2061
		for i, id in ipairs(h.ids) do
10990
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2062
			local t, cb = h.priorities[i], h.items[id];
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2063
			if not params then
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2064
				local param = cb.param;
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2065
				if param then
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2066
					cb = param.callback;
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2067
				else
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2068
					cb = cb.timer_callback or cb;
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2069
				end
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2070
			elseif params[id] then
d585deb8c882 mod_admin_shell: Fix debug:timers to handle net.server native timers
Kim Alvefur <zash@zash.se>
parents: 10933
diff changeset
  2071
				cb = params[id].callback or cb;
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2072
			end
11482
c0c4431ab27b mod_admin_shell: Sort timers by time in debug:timers()
Kim Alvefur <zash@zash.se>
parents: 11369
diff changeset
  2073
			table.insert(timers, { format_time(t), cb });
c0c4431ab27b mod_admin_shell: Sort timers by time in debug:timers()
Kim Alvefur <zash@zash.se>
parents: 11369
diff changeset
  2074
		end
c0c4431ab27b mod_admin_shell: Sort timers by time in debug:timers()
Kim Alvefur <zash@zash.se>
parents: 11369
diff changeset
  2075
		table.sort(timers, function (a, b) return a[1] < b[1] end);
c0c4431ab27b mod_admin_shell: Sort timers by time in debug:timers()
Kim Alvefur <zash@zash.se>
parents: 11369
diff changeset
  2076
		for _, t in ipairs(timers) do
c0c4431ab27b mod_admin_shell: Sort timers by time in debug:timers()
Kim Alvefur <zash@zash.se>
parents: 11369
diff changeset
  2077
			print(t[1], t[2])
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2078
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2079
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2080
	if server.event_base then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2081
		local count = 0;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2082
		for _, v in pairs(debug.getregistry()) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2083
			if type(v) == "function" and v.callback and v.callback == add_task._on_timer then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2084
				count = count + 1;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2085
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2086
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2087
		print(count .. " libevent callbacks");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2088
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2089
	if h then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2090
		local next_time = h:peek();
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2091
		if next_time then
10992
9dcbbb1d12c3 mod_admin_shell: Handle server_epoll using monotonic time internally
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
  2092
			return true, ("Next event at %s (in %.6fs)"):format(format_time(next_time), normalize_time(next_time) - time.now());
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2093
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2094
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2095
	return true;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2096
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2097
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  2098
describe_command [[debug:async() - Show information about pending asynchronous tasks]]
13338
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2099
function def_env.debug:async(runner_id)
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2100
	local print = self.session.print;
13342
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2101
	local time_now = time.now();
13338
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2102
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2103
	if runner_id then
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2104
		for runner, since in pairs(async.waiting_runners) do
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2105
			if runner.id == runner_id then
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2106
				print("ID        ", runner.id);
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2107
				local f = runner.func;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2108
				if f == async.default_runner_func then
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2109
					print("Function ", tostring(runner.current_item).." (from work queue)");
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2110
				else
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2111
					print("Function ", tostring(f));
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2112
					if st.is_stanza(runner.current_item) then
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2113
						print("Stanza:")
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2114
						print("\t"..runner.current_item:indent(2):pretty_print());
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2115
					else
13342
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2116
						print("Work item", self.session.serialize(runner.current_item, "debug"));
13338
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2117
					end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2118
				end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2119
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2120
				print("Coroutine ", tostring(runner.thread).." ("..coroutine.status(runner.thread)..")");
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2121
				print("Since     ", since);
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2122
				print("Status    ", ("%s since %s (%0.2f seconds ago)"):format(runner.state, os.date("%Y-%m-%d %R:%S", math.floor(since)), time_now-since));
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2123
				print("");
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2124
				print(debug.traceback(runner.thread));
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2125
				return true, "Runner is "..runner.state;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2126
			end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2127
		end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2128
		return nil, "Runner not found or is currently idle";
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2129
	end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2130
13342
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2131
	local row = format_table({
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2132
		{ title = "ID"; width = 12 };
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2133
		{ title = "Function"; width = "10p" };
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2134
		{ title = "Status"; width = "16" };
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2135
		{ title = "Location"; width = "10p" };
470ab6b55e93 mod_admin_shell: Fix lint [luacheck]
Kim Alvefur <zash@zash.se>
parents: 13338
diff changeset
  2136
	}, self.session.width);
13338
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2137
	print(row())
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2138
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2139
	local c = 0;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2140
	for runner, since in pairs(async.waiting_runners) do
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2141
		c = c + 1;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2142
		local f = runner.func;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2143
		if f == async.default_runner_func then
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2144
			f = runner.current_item;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2145
		end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2146
		-- We want to fetch the location in the code that the runner yielded from,
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2147
		-- excluding util.async's wrapper code. A level of  `2` assumes that we
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2148
		-- yielded directly from a function in util.async. This is *currently* true
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2149
		-- of all util.async yields, but it's fragile.
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2150
		local location = debug.getinfo(runner.thread, 2);
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2151
		print(row {
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2152
			runner.id;
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2153
			tostring(f);
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2154
			("%s (%0.2fs)"):format(runner.state, time_now - since);
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2155
			location.short_src..(location.currentline and ":"..location.currentline or "");
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2156
		});
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2157
	end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2158
	return true, ("%d runners pending"):format(c);
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2159
end
aefcb6b4f47d mod_admin_shell: Add debug:async() command to show blocked async runners
Matthew Wild <mwild1@gmail.com>
parents: 13291
diff changeset
  2160
13353
d5d4e386c6fb mod_admin_shell: Refactor help to data structures for extensibility
Matthew Wild <mwild1@gmail.com>
parents: 13342
diff changeset
  2161
def_env.stats = new_section("Commands to show internal statistics");
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2162
10891
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
  2163
local short_units = {
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
  2164
	seconds = "s",
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
  2165
	bytes = "B",
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
  2166
};
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
  2167
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2168
local stats_methods = {};
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2169
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2170
function stats_methods:render_single_fancy_histogram_ex(print, prefix, metric_family, metric, cumulative)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2171
	local creation_timestamp, sum, count
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2172
	local buckets = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2173
	local prev_bucket_count = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2174
	for suffix, extra_labels, value in metric:iter_samples() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2175
		if suffix == "_created" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2176
			creation_timestamp = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2177
		elseif suffix == "_sum" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2178
			sum = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2179
		elseif suffix == "_count" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2180
			count = value
12230
7db81c9cbbbf mod_admin_shell: Fix traceback on rendering graph of stats without extra labels
Kim Alvefur <zash@zash.se>
parents: 12229
diff changeset
  2181
		elseif extra_labels then
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2182
			local bucket_threshold = extra_labels["le"]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2183
			local bucket_count
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2184
			if cumulative then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2185
				bucket_count = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2186
			else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2187
				bucket_count = value - prev_bucket_count
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2188
				prev_bucket_count = value
10891
3debe04a6162 mod_admin_shell: Format stats with util.human.units
Kim Alvefur <zash@zash.se>
parents: 10882
diff changeset
  2189
			end
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2190
			if bucket_threshold == "+Inf" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2191
				t_insert(buckets, {threshold = 1/0, count = bucket_count})
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2192
			elseif bucket_threshold ~= nil then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2193
				t_insert(buckets, {threshold = tonumber(bucket_threshold), count = bucket_count})
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2194
			end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2195
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2196
	end
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2197
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2198
	if #buckets == 0 or not creation_timestamp or not sum or not count then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2199
		print("[no data or not a histogram]")
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2200
		return false
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2201
	end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2202
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2203
	local graph_width, graph_height, wscale = #buckets, 10, 1;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2204
	if graph_width < 8 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2205
		wscale = 8
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2206
	elseif graph_width < 16 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2207
		wscale = 4
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2208
	elseif graph_width < 32 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2209
		wscale = 2
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2210
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2211
	local eighth_chars = "   ▁▂▃▄▅▆▇█";
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2212
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2213
	local max_bin_samples = 0
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2214
	for _, bucket in ipairs(buckets) do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2215
		if bucket.count > max_bin_samples then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2216
			max_bin_samples = bucket.count
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2217
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2218
	end
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2219
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2220
	print("");
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2221
	print(prefix)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2222
	print(("_"):rep(graph_width*wscale).." "..max_bin_samples);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2223
	for row = graph_height, 1, -1 do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2224
		local row_chars = {};
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2225
		local min_eighths, max_eighths = 8, 0;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2226
		for i = 1, #buckets do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2227
			local char_eighths = math.ceil(math.max(math.min((graph_height/(max_bin_samples/buckets[i].count))-(row-1), 1), 0)*8);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2228
			if char_eighths < min_eighths then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2229
				min_eighths = char_eighths;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2230
			end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2231
			if char_eighths > max_eighths then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2232
				max_eighths = char_eighths;
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2233
			end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2234
			if char_eighths == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2235
				row_chars[i] = ("-"):rep(wscale);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2236
			else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2237
				local char = eighth_chars:sub(char_eighths*3+1, char_eighths*3+3);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2238
				row_chars[i] = char:rep(wscale);
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2239
			end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2240
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2241
		print(table.concat(row_chars).."|- "..string.format("%.8g", math.ceil((max_bin_samples/graph_height)*(row-0.5))));
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2242
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2243
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2244
	local legend_pat = string.format("%%%d.%dg", wscale-1, wscale-1)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2245
	local row = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2246
	for i = 1, #buckets do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2247
		local threshold = buckets[i].threshold
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2248
		t_insert(row, legend_pat:format(threshold))
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2249
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2250
	t_insert(row, " " .. metric_family.unit)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2251
	print(t_concat(row, "/"))
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2252
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2253
	return true
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2254
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2255
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2256
function stats_methods:render_single_fancy_histogram(print, prefix, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2257
	return self:render_single_fancy_histogram_ex(print, prefix, metric_family, metric, false)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2258
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2259
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2260
function stats_methods:render_single_fancy_histogram_cf(print, prefix, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2261
	-- cf = cumulative frequency
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2262
	return self:render_single_fancy_histogram_ex(print, prefix, metric_family, metric, true)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2263
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2264
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2265
function stats_methods:cfgraph()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2266
	for _, stat_info in ipairs(self) do
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2267
		local family_name, metric_family = unpack(stat_info, 1, 2)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2268
		local function print(s)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2269
			table.insert(stat_info.output, s);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2270
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2271
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2272
		if not self:render_family(print, family_name, metric_family, self.render_single_fancy_histogram_cf) then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2273
			return self
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2274
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2275
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2276
	return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2277
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2278
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2279
function stats_methods:histogram()
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2280
	for _, stat_info in ipairs(self) do
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2281
		local family_name, metric_family = unpack(stat_info, 1, 2)
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2282
		local function print(s)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2283
			table.insert(stat_info.output, s);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2284
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2285
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2286
		if not self:render_family(print, family_name, metric_family, self.render_single_fancy_histogram) then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2287
			return self
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2288
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2289
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2290
	return self;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2291
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2292
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2293
function stats_methods:render_single_counter(print, prefix, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2294
	local created_timestamp, current_value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2295
	for suffix, _, value in metric:iter_samples() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2296
		if suffix == "_created" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2297
			created_timestamp = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2298
		elseif suffix == "_total" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2299
			current_value = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2300
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2301
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2302
	if current_value and created_timestamp then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2303
		local base_unit = short_units[metric_family.unit] or metric_family.unit
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2304
		local unit = base_unit .. "/s"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2305
		local factor = 1
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2306
		if base_unit == "s" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2307
			-- be smart!
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2308
			unit = "%"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2309
			factor = 100
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2310
		elseif base_unit == "" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2311
			unit = "events/s"
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2312
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2313
		print(("%-50s %s"):format(prefix, format_number(factor * current_value / (self.now - created_timestamp), unit.." [avg]")));
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2314
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2315
end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2316
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2317
function stats_methods:render_single_gauge(print, prefix, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2318
	local current_value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2319
	for _, _, value in metric:iter_samples() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2320
		current_value = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2321
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2322
	if current_value then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2323
		local unit = short_units[metric_family.unit] or metric_family.unit
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2324
		print(("%-50s %s"):format(prefix, format_number(current_value, unit)));
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2325
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2326
end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2327
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2328
function stats_methods:render_single_summary(print, prefix, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2329
	local sum, count
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2330
	for suffix, _, value in metric:iter_samples() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2331
		if suffix == "_sum" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2332
			sum = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2333
		elseif suffix == "_count" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2334
			count = value
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2335
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2336
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2337
	if sum and count then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2338
		local unit = short_units[metric_family.unit] or metric_family.unit
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2339
		if count == 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2340
			print(("%-50s %s"):format(prefix, "no obs."));
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2341
		else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2342
			print(("%-50s %s"):format(prefix, format_number(sum / count, unit.."/event [avg]")));
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2343
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2344
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2345
end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2346
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2347
function stats_methods:render_family(print, family_name, metric_family, render_func)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2348
	local labelkeys = metric_family.label_keys
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2349
	if #labelkeys > 0 then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2350
		print(family_name)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2351
		for labelset, metric in metric_family:iter_metrics() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2352
			local labels = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2353
			for i, k in ipairs(labelkeys) do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2354
				local v = labelset[i]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2355
				t_insert(labels, ("%s=%s"):format(k, v))
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2356
			end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2357
			local prefix = "  "..t_concat(labels, " ")
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2358
			render_func(self, print, prefix, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2359
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2360
	else
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2361
		for _, metric in metric_family:iter_metrics() do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2362
			render_func(self, print, family_name, metric_family, metric)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2363
		end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2364
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2365
end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2366
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2367
local function stats_tostring(stats)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2368
	local print = stats.session.print;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2369
	for _, stat_info in ipairs(stats) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2370
		if #stat_info.output > 0 then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2371
			print("\n#"..stat_info[1]);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2372
			print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2373
			for _, v in ipairs(stat_info.output) do
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2374
				print(v);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2375
			end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2376
			print("");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2377
		else
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2378
			local metric_family = stat_info[2]
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2379
			if metric_family.type_ == "counter" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2380
				stats:render_family(print, stat_info[1], metric_family, stats.render_single_counter)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2381
			elseif metric_family.type_ == "gauge" or metric_family.type_ == "unknown" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2382
				stats:render_family(print, stat_info[1], metric_family, stats.render_single_gauge)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2383
			elseif metric_family.type_ == "summary" or metric_family.type_ == "histogram" then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2384
				stats:render_family(print, stat_info[1], metric_family, stats.render_single_summary)
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2385
			end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2386
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2387
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2388
	return #stats.." statistics displayed";
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2389
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2390
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2391
local stats_mt = {__index = stats_methods, __tostring = stats_tostring }
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2392
local function new_stats_context(self)
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2393
	-- TODO: instead of now(), it might be better to take the time of the last
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2394
	-- interval, if the statistics backend is set to use periodic collection
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2395
	-- Otherwise we get strange stuff like average cpu usage decreasing until
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2396
	-- the next sample and so on.
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2397
	return setmetatable({ session = self.session, stats = true, now = time.now() }, stats_mt);
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2398
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2399
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  2400
describe_command [[stats:show(pattern) - Show internal statistics, optionally filtering by name with a pattern.]]
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  2401
-- Undocumented currently, you can append :histogram() or :cfgraph() to stats:show() for rendered graphs.
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2402
function def_env.stats:show(name_filter)
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12931
diff changeset
  2403
	local statsman = require "prosody.core.statsmanager"
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2404
	local collect = statsman.collect
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2405
	if collect then
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2406
		-- force collection if in manual mode
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2407
		collect()
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2408
	end
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2409
	local metric_registry = statsman.get_metric_registry();
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2410
	local displayed_stats = new_stats_context(self);
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2411
	for family_name, metric_family in iterators.sorted_pairs(metric_registry:get_metric_families()) do
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2412
		if not name_filter or family_name:match(name_filter) then
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2413
			table.insert(displayed_stats, {
11527
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2414
				family_name,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2415
				metric_family,
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2416
				output = {}
5f15ab7c6ae5 Statistics: Rewrite statistics backends to use OpenMetrics
Jonas Schäfer <jonas@wielicki.name>
parents: 11508
diff changeset
  2417
			})
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2418
		end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2419
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2420
	return displayed_stats;
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2421
end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2422
13354
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2423
local command_metadata_schema = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2424
	type = "object";
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2425
	properties = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2426
		section = { type = "string" };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2427
		section_desc = { type = "string" };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2428
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2429
		name = { type = "string" };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2430
		desc = { type = "string" };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2431
		help = { type = "string" };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2432
		args = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2433
			type = "array";
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2434
			items = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2435
				type = "object";
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2436
				properties = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2437
					name = { type = "string", required = true };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2438
					type = { type = "string", required = false };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2439
				};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2440
			};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2441
		};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2442
	};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2443
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2444
	required = { "name", "section", "desc", "args" };
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2445
};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2446
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2447
-- host_commands[section..":"..name][host] = handler
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2448
-- host_commands[section..":"..name][false] = metadata
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2449
local host_commands = {};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2450
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2451
local function new_item_handlers(command_host)
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2452
	local function on_command_added(event)
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2453
		local command = event.item;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2454
		local mod_name = command._provided_by and ("mod_"..command._provided_by) or "<unknown module>";
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2455
		if not schema.validate(command_metadata_schema, command) or type(command.handler) ~= "function" then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2456
			module:log("warn", "Ignoring command added by %s: missing or invalid data", mod_name);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2457
			return;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2458
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2459
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2460
		local handler = command.handler;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2461
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2462
		if command_host then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2463
			if type(command.host_selector) ~= "string" then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2464
				module:log("warn", "Ignoring command %s:%s() added by %s - missing/invalid host_selector", command.section, command.name, mod_name);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2465
				return;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2466
			end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2467
			local qualified_name = command.section..":"..command.name;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2468
			local host_command_info = host_commands[qualified_name];
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2469
			if not host_command_info then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2470
				local selector_index;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2471
				for i, arg in ipairs(command.args) do
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2472
					if arg.name == command.host_selector then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2473
						selector_index = i + 1; -- +1 to account for 'self'
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2474
						break;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2475
					end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2476
				end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2477
				if not selector_index then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2478
					module:log("warn", "Command %s() host selector argument '%s' not found - not registering", qualified_name, command.host_selector);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2479
					return;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2480
				end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2481
				host_command_info = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2482
					[false] = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2483
						host_selector = command.host_selector;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2484
						handler = function (...)
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2485
							local selected_host = select(2, jid_split((select(selector_index, ...))));
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2486
							if type(selected_host) ~= "string" then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2487
								return nil, "Invalid or missing argument '"..command.host_selector.."'";
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2488
							end
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  2489
							if not prosody.hosts[selected_host] then
13354
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2490
								return nil, "Unknown host: "..selected_host;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2491
							end
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  2492
							local host_handler = host_commands[qualified_name][selected_host];
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  2493
							if not host_handler then
13354
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2494
								return nil, "This command is not available on "..selected_host;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2495
							end
13358
83f3076965f6 mod_admin_shell: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13356
diff changeset
  2496
							return host_handler(...);
13354
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2497
						end;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2498
					};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2499
				};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2500
				host_commands[qualified_name] = host_command_info;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2501
			end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2502
			if host_command_info[command_host] then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2503
				module:log("warn", "Command %s() is already registered - overwriting with %s", qualified_name, mod_name);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2504
			end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2505
			host_command_info[command_host] = handler;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2506
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2507
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2508
		local section_t = def_env[command.section];
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2509
		if not section_t then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2510
			section_t = {};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2511
			def_env[command.section] = section_t;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2512
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2513
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2514
		if command_host then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2515
			section_t[command.name] = host_commands[command.section..":"..command.name][false].handler;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2516
		else
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2517
			section_t[command.name] = command.handler;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2518
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2519
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2520
		local section_mt = getmetatable(section_t);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2521
		if not section_mt then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2522
			section_mt = {};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2523
			setmetatable(section_t, section_mt);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2524
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2525
		local section_help = section_mt.help;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2526
		if not section_help then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2527
			section_help = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2528
				desc = command.section_desc;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2529
				commands = {};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2530
			};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2531
			section_mt.help = section_help;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2532
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2533
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2534
		section_help.commands[command.name] = {
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2535
			desc = command.desc;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2536
			full = command.help;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2537
			args = array(command.args);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2538
			module = command._provided_by;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2539
		};
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2540
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2541
		module:log("debug", "Shell command added by mod_%s: %s:%s()", mod_name, command.section, command.name);
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2542
	end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2543
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2544
	local function on_command_removed(event)
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2545
		local command = event.item;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2546
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2547
		local handler = event.item.handler;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2548
		if type(handler) ~= "function" or not schema.validate(command_metadata_schema, command) then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2549
			return;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2550
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2551
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2552
		local section_t = def_env[command.section];
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2553
		if not section_t or section_t[command.name] ~= handler then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2554
			return;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2555
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2556
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2557
		section_t[command.name] = nil;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2558
		if next(section_t) == nil then -- Delete section if empty
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2559
			def_env[command.section] = nil;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2560
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2561
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2562
		if command_host then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2563
			local host_command_info = host_commands[command.section..":"..command.name];
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2564
			if host_command_info then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2565
				-- Remove our host handler
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2566
				host_command_info[command_host] = nil;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2567
				-- Clean up entire command entry if there are no per-host handlers left
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2568
				local any_hosts = false;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2569
				for k in pairs(host_command_info) do
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2570
					if k then -- metadata is false, ignore it
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2571
						any_hosts = true;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2572
						break;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2573
					end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2574
				end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2575
				if not any_hosts then
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2576
					host_commands[command.section..":"..command.name] = nil;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2577
				end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2578
			end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2579
		end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2580
	end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2581
	return on_command_added, on_command_removed;
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2582
end
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2583
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2584
module:handle_items("shell-command", new_item_handlers());
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2585
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2586
function module.add_host(host_module)
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2587
	host_module:handle_items("shell-command", new_item_handlers(host_module.host));
0573401b0f9f mod_admin_shell: Support for 'shell-command' items (global and per-host)
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
  2588
end
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2589
12468
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2590
function module.unload()
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2591
	stanza_watchers.cleanup();
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2592
end
f4c59af273a3 mod_admin_shell: Add watch:stanzas() command
Matthew Wild <mwild1@gmail.com>
parents: 12403
diff changeset
  2593
10860
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2594
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2595
-------------
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2596
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2597
function printbanner(session)
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2598
	local option = module:get_option_string("console_banner", "full");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2599
	if option == "full" or option == "graphic" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2600
		session.print [[
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2601
                   ____                \   /     _
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2602
                    |  _ \ _ __ ___  ___  _-_   __| |_   _
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2603
                    | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2604
                    |  __/| | | (_) \__ \ |_| | (_| | |_| |
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2605
                    |_|   |_|  \___/|___/\___/ \__,_|\__, |
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2606
                    A study in simplicity            |___/
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2607
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2608
]]
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2609
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2610
	if option == "short" or option == "full" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2611
	session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2612
	session.print("You may find more help on using this console in our online documentation at ");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2613
	session.print("https://prosody.im/doc/console\n");
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2614
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2615
	if option ~= "short" and option ~= "full" and option ~= "graphic" then
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2616
		session.print(option);
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2617
	end
c99711eda0d1 mod_admin_shell: New module that implements the console interface over an admin socket
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  2618
end