plugins/mod_console.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 05 Jun 2009 20:18:19 +0100
changeset 1316 28ae044f1aaf
parent 1315 bfcd3f0a49df
child 1317 f6e56a555c37
permissions -rw-r--r--
mod_console: Some "improvements" to the useless server:reload() command :)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
     1
-- Prosody IM v0.4
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 736
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 736
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
     8
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
     9
module.host = "*";
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
    10
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    11
local prosody = _G.prosody;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    12
local hosts = prosody.hosts;
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    13
local connlisteners_register = require "net.connlisteners".register;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    14
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    15
local console_listener = { default_port = 5582; default_mode = "*l"; };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    16
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    17
require "util.iterators";
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    18
local set, array = require "util.set", require "util.array";
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    19
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    20
local commands = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    21
local def_env = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    22
local default_env_mt = { __index = def_env };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    23
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    24
console = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    25
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    26
function console:new_session(conn)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    27
	local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    28
	local session = { conn = conn;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    29
			send = function (t) w(tostring(t)); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    30
			print = function (t) w("| "..tostring(t).."\n"); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    31
			disconnect = function () conn.close(); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    32
			};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    33
	session.env = setmetatable({}, default_env_mt);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    34
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    35
	-- Load up environment with helper objects
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    36
	for name, t in pairs(def_env) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    37
		if type(t) == "table" then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    38
			session.env[name] = setmetatable({ session = session }, { __index = t });
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    39
		end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    40
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    41
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    42
	return session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    43
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    44
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    45
local sessions = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    46
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    47
function console_listener.listener(conn, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    48
	local session = sessions[conn];
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    49
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    50
	if not session then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    51
		-- Handle new connection
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    52
		session = console:new_session(conn);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    53
		sessions[conn] = session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    54
		printbanner(session);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    55
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    56
	if data then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    57
		-- Handle data
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    58
		(function(session, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    59
			if data:match("[!.]$") then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    60
				local command = data:lower();
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    61
				command = data:match("^%w+") or data:match("%p");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    62
				if commands[command] then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    63
					commands[command](session, data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    64
					return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    65
				end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    66
			end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    67
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    68
			session.env._ = data;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    69
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    70
			local chunk, err = loadstring("return "..data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    71
			if not chunk then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    72
				chunk, err = loadstring(data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    73
				if not chunk then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    74
					err = err:gsub("^%[string .-%]:%d+: ", "");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    75
					err = err:gsub("^:%d+: ", "");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    76
					err = err:gsub("'<eof>'", "the end of the line");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    77
					session.print("Sorry, I couldn't understand that... "..err);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    78
					return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    79
				end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    80
			end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    81
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    82
			setfenv(chunk, session.env);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    83
			local ranok, taskok, message = pcall(chunk);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    84
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    85
			if not ranok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    86
				session.print("Fatal error while running command, it did not complete");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    87
				session.print("Error: "..taskok);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    88
				return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    89
			end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    90
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    91
			if not message then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    92
				session.print("Result: "..tostring(taskok));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    93
				return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    94
			elseif (not taskok) and message then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    95
				session.print("Command completed with a problem");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    96
				session.print("Message: "..tostring(message));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    97
				return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    98
			end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    99
			
669
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
   100
			session.print("OK: "..tostring(message));
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
   101
		end)(session, data);
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   102
	end
669
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
   103
	session.send(string.char(0));
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   104
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   105
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   106
function console_listener.disconnect(conn, err)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   107
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   108
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   109
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   110
connlisteners_register('console', console_listener);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   111
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   112
-- Console commands --
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   113
-- These are simple commands, not valid standalone in Lua
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   114
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   115
function commands.bye(session)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   116
	session.print("See you! :)");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   117
	session.disconnect();
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   118
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   119
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   120
commands["!"] = function (session, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   121
	if data:match("^!!") then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   122
		session.print("!> "..session.env._);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   123
		return console_listener.listener(session.conn, session.env._);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   124
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   125
	local old, new = data:match("^!(.-[^\\])!(.-)!$");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   126
	if old and new then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   127
		local ok, res = pcall(string.gsub, session.env._, old, new);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   128
		if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   129
			session.print(res)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   130
			return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   131
		end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   132
		session.print("!> "..res);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   133
		return console_listener.listener(session.conn, res);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   134
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   135
	session.print("Sorry, not sure what you want");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   136
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   137
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   138
-- Session environment --
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   139
-- Anything in def_env will be accessible within the session as a global variable
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   140
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   141
def_env.server = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   142
function def_env.server:reload()
1316
28ae044f1aaf mod_console: Some "improvements" to the useless server:reload() command :)
Matthew Wild <mwild1@gmail.com>
parents: 1315
diff changeset
   143
	prosody.unlock_globals();
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   144
	dofile "prosody"
1316
28ae044f1aaf mod_console: Some "improvements" to the useless server:reload() command :)
Matthew Wild <mwild1@gmail.com>
parents: 1315
diff changeset
   145
	prosody = _G.prosody;
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   146
	return true, "Server reloaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   147
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   148
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   149
def_env.module = {};
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   150
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   151
local function get_hosts_set(hosts)
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   152
	if type(hosts) == "table" then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   153
		if hosts[1] then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   154
			return set.new(hosts);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   155
		elseif hosts._items then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   156
			return hosts;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   157
		end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   158
	elseif type(hosts) == "string" then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   159
		return set.new { hosts };
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   160
	elseif hosts == nil then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   161
		return set.new(array.collect(keys(prosody.hosts)))
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   162
			/ function (host) return prosody.hosts[host].type == "local"; end;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   163
	end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   164
end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   165
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   166
function def_env.module:load(name, hosts, config)
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   167
	local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   168
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   169
	hosts = get_hosts_set(hosts);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   170
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   171
	-- Load the module for each host
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   172
	local ok, err, count = true, nil, 0;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   173
	for host in hosts do
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   174
		if (not mm.is_loaded(host, name)) then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   175
			ok, err = mm.load(host, name, config);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   176
			if not ok then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   177
				ok = false;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   178
				self.session.print(err or "Unknown error loading module");
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   179
			else
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   180
				count = count + 1;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   181
				self.session.print("Loaded for "..host);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   182
			end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   183
		end
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   184
	end
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   185
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   186
	return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));	
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   187
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   188
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   189
function def_env.module:unload(name, hosts)
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   190
	local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   191
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   192
	hosts = get_hosts_set(hosts);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   193
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   194
	-- Unload the module for each host
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   195
	local ok, err, count = true, nil, 0;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   196
	for host in hosts do
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   197
		if mm.is_loaded(host, name) then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   198
			ok, err = mm.unload(host, name);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   199
			if not ok then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   200
				ok = false;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   201
				self.session.print(err or "Unknown error unloading module");
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   202
			else
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   203
				count = count + 1;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   204
				self.session.print("Unloaded from "..host);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   205
			end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   206
		end
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   207
	end
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   208
	return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   209
end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   210
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   211
function def_env.module:reload(name, hosts)
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   212
	local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   213
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   214
	hosts = get_hosts_set(hosts);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   215
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   216
	-- Reload the module for each host
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   217
	local ok, err, count = true, nil, 0;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   218
	for host in hosts do
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   219
		if mm.is_loaded(host, name) then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   220
			ok, err = mm.reload(host, name);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   221
			if not ok then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   222
				ok = false;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   223
				self.session.print(err or "Unknown error reloading module");
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   224
			else
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   225
				count = count + 1;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   226
				if ok == nil then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   227
					ok = true;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   228
				end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   229
				self.session.print("Reloaded on "..host);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   230
			end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   231
		end
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   232
	end
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   233
	return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   234
end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   235
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   236
def_env.config = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   237
function def_env.config:load(filename, format)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   238
	local config_load = require "core.configmanager".load;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   239
	local ok, err = config_load(filename, format);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   240
	if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   241
		return false, err or "Unknown error loading config";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   242
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   243
	return true, "Config loaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   244
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   245
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   246
function def_env.config:get(host, section, key)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   247
	local config_get = require "core.configmanager".get
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   248
	return true, tostring(config_get(host, section, key));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   249
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   250
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   251
def_env.hosts = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   252
function def_env.hosts:list()
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   253
	for host, host_session in pairs(hosts) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   254
		self.session.print(host);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   255
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   256
	return true, "Done";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   257
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   258
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   259
function def_env.hosts:add(name)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   260
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   261
1241
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   262
def_env.c2s = {};
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   263
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   264
local function show_c2s(callback)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   265
	for hostname, host in pairs(hosts) do
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   266
		for username, user in pairs(host.sessions or {}) do
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   267
			for resource, session in pairs(user.sessions or {}) do
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   268
				local jid = username.."@"..hostname.."/"..resource;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   269
				callback(jid, session);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   270
			end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   271
		end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   272
	end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   273
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   274
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   275
function def_env.c2s:show(match_jid)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   276
	local print, count = self.session.print, 0;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   277
	show_c2s(function (jid)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   278
		if (not match_jid) or jid:match(match_jid) then
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   279
			count = count + 1;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   280
			print(jid);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   281
		end		
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   282
	end);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   283
	return true, "Total: "..count.." clients";
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   284
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   285
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   286
function def_env.c2s:show_insecure(match_jid)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   287
	local print, count = self.session.print, 0;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   288
	show_c2s(function (jid, session)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   289
		if ((not match_jid) or jid:match(match_jid)) and not session.secure then
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   290
			count = count + 1;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   291
			print(jid);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   292
		end		
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   293
	end);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   294
	return true, "Total: "..count.." insecure client connections";
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   295
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   296
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   297
function def_env.c2s:show_secure(match_jid)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   298
	local print, count = self.session.print, 0;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   299
	show_c2s(function (jid, session)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   300
		if ((not match_jid) or jid:match(match_jid)) and session.secure then
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   301
			count = count + 1;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   302
			print(jid);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   303
		end		
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   304
	end);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   305
	return true, "Total: "..count.." secure client connections";
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   306
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   307
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   308
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   309
def_env.s2s = {};
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   310
function def_env.s2s:show(match_jid)
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   311
	local _print = self.session.print;
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   312
	local print = self.session.print;
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   313
	for host, host_session in pairs(hosts) do
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   314
		print = function (...) _print(host); _print(...); print = _print; end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   315
		for remotehost, session in pairs(host_session.s2sout) do
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   316
			if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   317
				print("    "..host.." -> "..remotehost);
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   318
				if session.sendq then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   319
					print("        There are "..#session.sendq.." queued outgoing stanzas for this connection");
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   320
				end
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   321
				if session.type == "s2sout_unauthed" then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   322
					if session.connecting then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   323
						print("        Connection not yet established");
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   324
						if not session.srv_hosts then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   325
							if not session.conn then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   326
								print("        We do not yet have a DNS answer for this host's SRV records");
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   327
							else
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   328
								print("        This host has no SRV records, using A record instead");
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   329
							end
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   330
						elseif session.srv_choice then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   331
							print("        We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   332
							local srv_choice = session.srv_hosts[session.srv_choice];
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   333
							print("        Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   334
						end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   335
					elseif session.notopen then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   336
						print("        The <stream> has not yet been opened");
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   337
					elseif not session.dialback_key then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   338
						print("        Dialback has not been initiated yet");
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   339
					elseif session.dialback_key then
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   340
						print("        Dialback has been requested, but no result received");
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   341
					end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   342
				end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   343
			end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   344
		end	
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   345
		
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   346
		for session in pairs(incoming_s2s) do
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   347
			if session.to_host == host and ((not match_jid) or host:match(match_jid) 
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   348
				or (session.from_host and session.from_host:match(match_jid))) then
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   349
				print("    "..host.." <- "..(session.from_host or "(unknown)"));
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   350
				if session.type == "s2sin_unauthed" then
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   351
						print("        Connection not yet authenticated");
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   352
				end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   353
				for name in pairs(session.hosts) do
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   354
					if name ~= session.from_host then
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   355
						print("        also hosts "..tostring(name));
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   356
					end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   357
				end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   358
			end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   359
		end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   360
		
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   361
		print = _print;
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   362
	end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   363
	
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   364
	for session in pairs(incoming_s2s) do
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   365
		if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   366
			print("Other incoming s2s connections");
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   367
			print("    (unknown) <- "..(session.from_host or "(unknown)"));			
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   368
		end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   369
	end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   370
end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   371
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   372
-------------
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   373
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   374
function printbanner(session)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   375
session.print [[
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   376
                   ____                \   /     _       
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   377
                    |  _ \ _ __ ___  ___  _-_   __| |_   _ 
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   378
                    | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   379
                    |  __/| | | (_) \__ \ |_| | (_| | |_| |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   380
                    |_|   |_|  \___/|___/\___/ \__,_|\__, |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   381
                    A study in simplicity            |___/ 
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   382
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   383
]]
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   384
session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   385
session.print("You may find more help on using this console in our online documentation at ");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   386
session.print("http://prosody.im/doc/console\n");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   387
end