plugins/mod_console.lua
author Matthew Wild <mwild1@gmail.com>
Thu, 09 Jul 2009 14:41:56 +0100
changeset 1506 2c8aa16b4f64
parent 1503 5970e06d9335
child 1523 841d61be198f
permissions -rw-r--r--
mod_console: Expose commands and environment table
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
1342
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    11
local _G = _G;
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    12
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    13
local prosody = _G.prosody;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    14
local hosts = prosody.hosts;
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    15
local connlisteners_register = require "net.connlisteners".register;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    16
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    17
local console_listener = { default_port = 5582; default_mode = "*l"; };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    18
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    19
require "util.iterators";
1491
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
    20
local jid_bare = require "util.jid".bare;
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    21
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
    22
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    23
local commands = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    24
local def_env = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    25
local default_env_mt = { __index = def_env };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    26
1506
2c8aa16b4f64 mod_console: Expose commands and environment table
Matthew Wild <mwild1@gmail.com>
parents: 1503
diff changeset
    27
prosody.console = { commands = commands, env = def_env };
2c8aa16b4f64 mod_console: Expose commands and environment table
Matthew Wild <mwild1@gmail.com>
parents: 1503
diff changeset
    28
1342
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    29
local function redirect_output(_G, session)
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    30
	return setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end, __newindex = function (t, k, v) rawset(_G, k, v); end });
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    31
end
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    32
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    33
console = {};
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
function console:new_session(conn)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    36
	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
    37
	local session = { conn = conn;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    38
			send = function (t) w(tostring(t)); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    39
			print = function (t) w("| "..tostring(t).."\n"); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    40
			disconnect = function () conn.close(); 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
	session.env = setmetatable({}, default_env_mt);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    43
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    44
	-- Load up environment with helper objects
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    45
	for name, t in pairs(def_env) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    46
		if type(t) == "table" then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    47
			session.env[name] = setmetatable({ session = session }, { __index = t });
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    48
		end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    49
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    50
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    51
	return session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    52
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    53
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    54
local sessions = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    55
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    56
function console_listener.listener(conn, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    57
	local session = sessions[conn];
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    58
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    59
	if not session then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    60
		-- Handle new connection
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    61
		session = console:new_session(conn);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    62
		sessions[conn] = session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    63
		printbanner(session);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    64
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    65
	if data then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    66
		-- Handle data
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    67
		(function(session, data)
1317
f6e56a555c37 mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents: 1316
diff changeset
    68
			local useglobalenv;
f6e56a555c37 mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents: 1316
diff changeset
    69
			
1502
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
    70
			if data:match("^>") then
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
    71
				data = data:gsub("^>", "");
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
    72
				useglobalenv = true;
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
    73
			else
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    74
				local command = data:lower();
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    75
				command = data:match("^%w+") or data:match("%p");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    76
				if commands[command] then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    77
					commands[command](session, data);
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
1317
f6e56a555c37 mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents: 1316
diff changeset
    81
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    82
			session.env._ = data;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    83
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    84
			local chunk, err = loadstring("return "..data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    85
			if not chunk then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    86
				chunk, err = loadstring(data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    87
				if not chunk then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    88
					err = err:gsub("^%[string .-%]:%d+: ", "");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    89
					err = err:gsub("^:%d+: ", "");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    90
					err = err:gsub("'<eof>'", "the end of the line");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    91
					session.print("Sorry, I couldn't understand that... "..err);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    92
					return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    93
				end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    94
			end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    95
			
1342
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    96
			setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
1317
f6e56a555c37 mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents: 1316
diff changeset
    97
			
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    98
			local ranok, taskok, message = pcall(chunk);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    99
			
1502
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
   100
			if not (ranok or message or useglobalenv) and commands[data:lower()] then
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
   101
				commands[data:lower()](session, data);
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
   102
				return;
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
   103
			end
0f895c06e03f mod_console: Check for commands when not executing in the global environment
Matthew Wild <mwild1@gmail.com>
parents: 1496
diff changeset
   104
			
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   105
			if not ranok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   106
				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
   107
				session.print("Error: "..taskok);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   108
				return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   109
			end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   110
			
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   111
			if not message then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   112
				session.print("Result: "..tostring(taskok));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   113
				return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   114
			elseif (not taskok) and message then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   115
				session.print("Command completed with a problem");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   116
				session.print("Message: "..tostring(message));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   117
				return;
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
			
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
   120
			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
   121
		end)(session, data);
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   122
	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
   123
	session.send(string.char(0));
736
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
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   126
function console_listener.disconnect(conn, err)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   127
	
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   128
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   129
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   130
connlisteners_register('console', console_listener);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   131
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   132
-- Console commands --
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   133
-- These are simple commands, not valid standalone in Lua
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   134
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   135
function commands.bye(session)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   136
	session.print("See you! :)");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   137
	session.disconnect();
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   138
end
1503
5970e06d9335 mod_console: Add quit and exit as aliases for 'bye' command
Matthew Wild <mwild1@gmail.com>
parents: 1502
diff changeset
   139
commands.quit, commands.exit = commands.bye, commands.bye;
736
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
commands["!"] = function (session, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   142
	if data:match("^!!") then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   143
		session.print("!> "..session.env._);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   144
		return console_listener.listener(session.conn, session.env._);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   145
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   146
	local old, new = data:match("^!(.-[^\\])!(.-)!$");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   147
	if old and new then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   148
		local ok, res = pcall(string.gsub, session.env._, old, new);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   149
		if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   150
			session.print(res)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   151
			return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   152
		end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   153
		session.print("!> "..res);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   154
		return console_listener.listener(session.conn, res);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   155
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   156
	session.print("Sorry, not sure what you want");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   157
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   158
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   159
-- Session environment --
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   160
-- 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
   161
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   162
def_env.server = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   163
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
   164
	prosody.unlock_globals();
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   165
	dofile "prosody"
1316
28ae044f1aaf mod_console: Some "improvements" to the useless server:reload() command :)
Matthew Wild <mwild1@gmail.com>
parents: 1315
diff changeset
   166
	prosody = _G.prosody;
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   167
	return true, "Server reloaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   168
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   169
1496
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   170
function def_env.server:version()
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   171
	return true, tostring(prosody.version or "unknown");
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   172
end
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   173
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   174
function def_env.server:uptime()
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   175
	local t = os.time()-prosody.start_time;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   176
	local seconds = t%60;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   177
	t = (t - seconds)/60;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   178
	local minutes = t%60;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   179
	t = (t - minutes)/60;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   180
	local hours = t%24;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   181
	t = (t - hours)/24;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   182
	local days = t;
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   183
	return true, string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)", 
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   184
		days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "", 
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   185
		minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   186
end
4fa337035f46 mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents: 1491
diff changeset
   187
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   188
def_env.module = {};
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   189
1433
e7bd00e70973 mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents: 1342
diff changeset
   190
local function get_hosts_set(hosts, module)
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   191
	if type(hosts) == "table" then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   192
		if hosts[1] then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   193
			return set.new(hosts);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   194
		elseif hosts._items then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   195
			return hosts;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   196
		end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   197
	elseif type(hosts) == "string" then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   198
		return set.new { hosts };
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   199
	elseif hosts == nil then
1433
e7bd00e70973 mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents: 1342
diff changeset
   200
		local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   201
		return set.new(array.collect(keys(prosody.hosts)))
1433
e7bd00e70973 mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents: 1342
diff changeset
   202
			/ function (host) return prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module); end;
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   203
	end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   204
end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   205
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   206
function def_env.module:load(name, hosts, config)
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   207
	local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   208
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   209
	hosts = get_hosts_set(hosts);
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   210
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   211
	-- 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
   212
	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
   213
	for host in hosts do
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   214
		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
   215
			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
   216
			if not ok then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   217
				ok = false;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   218
				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
   219
			else
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   220
				count = count + 1;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   221
				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
   222
			end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   223
		end
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   224
	end
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   225
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   226
	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
   227
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   228
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   229
function def_env.module:unload(name, hosts)
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   230
	local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   231
1433
e7bd00e70973 mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents: 1342
diff changeset
   232
	hosts = get_hosts_set(hosts, name);
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   233
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   234
	-- 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
   235
	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
   236
	for host in hosts do
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   237
		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
   238
			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
   239
			if not ok then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   240
				ok = false;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   241
				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
   242
			else
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   243
				count = count + 1;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   244
				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
   245
			end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   246
		end
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   247
	end
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   248
	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
   249
end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   250
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   251
function def_env.module:reload(name, hosts)
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   252
	local mm = require "modulemanager";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   253
1433
e7bd00e70973 mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents: 1342
diff changeset
   254
	hosts = get_hosts_set(hosts, name);
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   255
	
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   256
	-- 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
   257
	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
   258
	for host in hosts do
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   259
		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
   260
			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
   261
			if not ok then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   262
				ok = false;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   263
				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
   264
			else
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   265
				count = count + 1;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   266
				if ok == nil then
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   267
					ok = true;
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   268
				end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   269
				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
   270
			end
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   271
		end
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   272
	end
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
   273
	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
   274
end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
   275
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   276
def_env.config = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   277
function def_env.config:load(filename, format)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   278
	local config_load = require "core.configmanager".load;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   279
	local ok, err = config_load(filename, format);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   280
	if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   281
		return false, err or "Unknown error loading config";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   282
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   283
	return true, "Config loaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   284
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   285
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   286
function def_env.config:get(host, section, key)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   287
	local config_get = require "core.configmanager".get
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   288
	return true, tostring(config_get(host, section, key));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   289
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   290
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   291
def_env.hosts = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   292
function def_env.hosts:list()
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   293
	for host, host_session in pairs(hosts) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   294
		self.session.print(host);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   295
	end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   296
	return true, "Done";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   297
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   298
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   299
function def_env.hosts:add(name)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   300
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   301
1241
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   302
def_env.c2s = {};
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   303
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   304
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
   305
	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
   306
		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
   307
			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
   308
				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
   309
				callback(jid, session);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   310
			end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   311
		end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   312
	end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   313
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   314
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   315
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
   316
	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
   317
	show_c2s(function (jid)
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   318
		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
   319
			count = count + 1;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   320
			print(jid);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   321
		end		
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   322
	end);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   323
	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
   324
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   325
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   326
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
   327
	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
   328
	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
   329
		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
   330
			count = count + 1;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   331
			print(jid);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   332
		end		
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   333
	end);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   334
	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
   335
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   336
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   337
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
   338
	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
   339
	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
   340
		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
   341
			count = count + 1;
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   342
			print(jid);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   343
		end		
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   344
	end);
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   345
	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
   346
end
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   347
1491
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   348
function def_env.c2s:close(match_jid)
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   349
	local print, count = self.session.print, 0;
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   350
	show_c2s(function (jid, session)
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   351
		if jid == match_jid or jid_bare(jid) == match_jid then
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   352
			count = count + 1;
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   353
			session:close();
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   354
		end
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   355
	end);
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   356
	return true, "Total: "..count.." sessions closed";
694a0a00e1a5 mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents: 1483
diff changeset
   357
end
1241
9c53fb182044 mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
   358
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   359
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
   360
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
   361
	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
   362
	local print = self.session.print;
1322
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   363
	
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   364
	local count_in, count_out = 0,0;
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   365
	
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   366
	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
   367
		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
   368
		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
   369
			if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then
1322
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   370
				count_out = count_out + 1;
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   371
				print("    "..host.." -> "..remotehost);
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   372
				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
   373
					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
   374
				end
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   375
				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
   376
					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
   377
						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
   378
						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
   379
							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
   380
								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
   381
							else
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   382
								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
   383
							end
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   384
						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
   385
							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
   386
							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
   387
							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
   388
						end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   389
					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
   390
						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
   391
					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
   392
						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
   393
					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
   394
						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
   395
					end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   396
				end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   397
			end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   398
		end	
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   399
		
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   400
		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
   401
			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
   402
				or (session.from_host and session.from_host:match(match_jid))) then
1322
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   403
				count_in = count_in + 1;
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   404
				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
   405
				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
   406
						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
   407
				end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   408
				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
   409
					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
   410
						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
   411
					end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   412
				end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   413
			end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   414
		end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   415
		
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   416
		print = _print;
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   417
	end
1240
397b6e9c1568 mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents: 1085
diff changeset
   418
	
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   419
	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
   420
		if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
1322
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   421
			count_in = count_in + 1;
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   422
			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
   423
			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
   424
		end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   425
	end
1322
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   426
	
33d103b0283f mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1317
diff changeset
   427
	return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections";
1085
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   428
end
1ac11fb753ca mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
   429
1340
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   430
function def_env.s2s:close(from, to)
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   431
	local print, count = self.session.print, 0;
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   432
	
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   433
	if not (from and to) then
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   434
		return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'";
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   435
	elseif from == to then
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   436
		return false, "Both from and to are the same... you can't do that :)";
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   437
	end
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   438
	
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   439
	if hosts[from] and not hosts[to] then
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   440
		-- Is an outgoing connection
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   441
		local session = hosts[from].s2sout[to];
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   442
		if not session then 
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   443
			print("No outgoing connection from "..from.." to "..to)
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   444
		else
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   445
			s2smanager.destroy_session(session);
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   446
			count = count + 1;
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   447
			print("Closed outgoing session from "..from.." to "..to);
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   448
		end
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   449
	elseif hosts[to] and not hosts[from] then
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   450
		-- Is an incoming connection
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   451
		for session in pairs(incoming_s2s) do
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   452
			if session.to_host == to and session.from_host == from then
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   453
				s2smanager.destroy_session(session);
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   454
				count = count + 1;
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   455
			end
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   456
		end
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   457
		
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   458
		if count == 0 then
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   459
			print("No incoming connections from "..from.." to "..to);
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   460
		else
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   461
			print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to);
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   462
		end
1341
53decd1ee351 mod_console: Fix syntax error
Matthew Wild <mwild1@gmail.com>
parents: 1340
diff changeset
   463
	elseif hosts[to] and hosts[from] then
1340
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   464
		return false, "Both of the hostnames you specified are local, there are no s2s sessions to close";
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   465
	else
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   466
		return false, "Neither of the hostnames you specified are being used on this server";
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   467
	end
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   468
	
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   469
	return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   470
end
f707d0957155 mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents: 1322
diff changeset
   471
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   472
-------------
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   473
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   474
function printbanner(session)
1483
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   475
	local option = config.get("*", "core", "console_banner");
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   476
if option == nil or option == "full" or option == "graphic" then
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   477
session.print [[
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   478
                   ____                \   /     _       
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   479
                    |  _ \ _ __ ___  ___  _-_   __| |_   _ 
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   480
                    | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   481
                    |  __/| | | (_) \__ \ |_| | (_| | |_| |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   482
                    |_|   |_|  \___/|___/\___/ \__,_|\__, |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   483
                    A study in simplicity            |___/ 
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   484
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   485
]]
1483
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   486
end
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   487
if option == nil or option == "short" or option == "full" then
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   488
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
   489
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
   490
session.print("http://prosody.im/doc/console\n");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   491
end
1483
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   492
if option and option ~= "short" and option ~= "full" and option ~= "graphic" then
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   493
	if type(option) == "string" then
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   494
		session.print(option)
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   495
	elseif type(option) == "function" then
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   496
		setfenv(option, redirect_output(_G, session));
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   497
		pcall(option, session);
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   498
	end
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   499
end
efd19cdda6ca mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents: 1433
diff changeset
   500
end