2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
13 local prosody = _G.prosody;
14 local hosts = prosody.hosts;
15 local connlisteners_register = require "net.connlisteners".register;
17 local console_listener = { default_port = 5582; default_mode = "*l"; };
19 require "util.iterators";
20 local jid_bare = require "util.jid".bare;
21 local set, array = require "util.set", require "util.array";
25 local default_env_mt = { __index = def_env };
27 local function redirect_output(_G, session)
28 return setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end, __newindex = function (t, k, v) rawset(_G, k, v); end });
33 function console:new_session(conn)
34 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
35 local session = { conn = conn;
36 send = function (t) w(tostring(t)); end;
37 print = function (t) w("| "..tostring(t).."\n"); end;
38 disconnect = function () conn.close(); end;
40 session.env = setmetatable({}, default_env_mt);
42 -- Load up environment with helper objects
43 for name, t in pairs(def_env) do
44 if type(t) == "table" then
45 session.env[name] = setmetatable({ session = session }, { __index = t });
54 function console_listener.listener(conn, data)
55 local session = sessions[conn];
58 -- Handle new connection
59 session = console:new_session(conn);
60 sessions[conn] = session;
65 (function(session, data)
68 if data:match("^>") then
69 data = data:gsub("^>", "");
72 local command = data:lower();
73 command = data:match("^%w+") or data:match("%p");
74 if commands[command] then
75 commands[command](session, data);
82 local chunk, err = loadstring("return "..data);
84 chunk, err = loadstring(data);
86 err = err:gsub("^%[string .-%]:%d+: ", "");
87 err = err:gsub("^:%d+: ", "");
88 err = err:gsub("'<eof>'", "the end of the line");
89 session.print("Sorry, I couldn't understand that... "..err);
94 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
96 local ranok, taskok, message = pcall(chunk);
98 if not (ranok or message or useglobalenv) and commands[data:lower()] then
99 commands[data:lower()](session, data);
104 session.print("Fatal error while running command, it did not complete");
105 session.print("Error: "..taskok);
110 session.print("Result: "..tostring(taskok));
112 elseif (not taskok) and message then
113 session.print("Command completed with a problem");
114 session.print("Message: "..tostring(message));
118 session.print("OK: "..tostring(message));
121 session.send(string.char(0));
124 function console_listener.disconnect(conn, err)
128 connlisteners_register('console', console_listener);
130 -- Console commands --
131 -- These are simple commands, not valid standalone in Lua
133 function commands.bye(session)
134 session.print("See you! :)");
135 session.disconnect();
138 commands["!"] = function (session, data)
139 if data:match("^!!") then
140 session.print("!> "..session.env._);
141 return console_listener.listener(session.conn, session.env._);
143 local old, new = data:match("^!(.-[^\\])!(.-)!$");
145 local ok, res = pcall(string.gsub, session.env._, old, new);
150 session.print("!> "..res);
151 return console_listener.listener(session.conn, res);
153 session.print("Sorry, not sure what you want");
156 -- Session environment --
157 -- Anything in def_env will be accessible within the session as a global variable
160 function def_env.server:reload()
161 prosody.unlock_globals();
163 prosody = _G.prosody;
164 return true, "Server reloaded";
167 function def_env.server:version()
168 return true, tostring(prosody.version or "unknown");
171 function def_env.server:uptime()
172 local t = os.time()-prosody.start_time;
173 local seconds = t%60;
174 t = (t - seconds)/60;
175 local minutes = t%60;
176 t = (t - minutes)/60;
180 return true, string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)",
181 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
182 minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
187 local function get_hosts_set(hosts, module)
188 if type(hosts) == "table" then
190 return set.new(hosts);
191 elseif hosts._items then
194 elseif type(hosts) == "string" then
195 return set.new { hosts };
196 elseif hosts == nil then
197 local mm = require "modulemanager";
198 return set.new(array.collect(keys(prosody.hosts)))
199 / function (host) return prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module); end;
203 function def_env.module:load(name, hosts, config)
204 local mm = require "modulemanager";
206 hosts = get_hosts_set(hosts);
208 -- Load the module for each host
209 local ok, err, count = true, nil, 0;
211 if (not mm.is_loaded(host, name)) then
212 ok, err = mm.load(host, name, config);
215 self.session.print(err or "Unknown error loading module");
218 self.session.print("Loaded for "..host);
223 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
226 function def_env.module:unload(name, hosts)
227 local mm = require "modulemanager";
229 hosts = get_hosts_set(hosts, name);
231 -- Unload the module for each host
232 local ok, err, count = true, nil, 0;
234 if mm.is_loaded(host, name) then
235 ok, err = mm.unload(host, name);
238 self.session.print(err or "Unknown error unloading module");
241 self.session.print("Unloaded from "..host);
245 return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
248 function def_env.module:reload(name, hosts)
249 local mm = require "modulemanager";
251 hosts = get_hosts_set(hosts, name);
253 -- Reload the module for each host
254 local ok, err, count = true, nil, 0;
256 if mm.is_loaded(host, name) then
257 ok, err = mm.reload(host, name);
260 self.session.print(err or "Unknown error reloading module");
266 self.session.print("Reloaded on "..host);
270 return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
274 function def_env.config:load(filename, format)
275 local config_load = require "core.configmanager".load;
276 local ok, err = config_load(filename, format);
278 return false, err or "Unknown error loading config";
280 return true, "Config loaded";
283 function def_env.config:get(host, section, key)
284 local config_get = require "core.configmanager".get
285 return true, tostring(config_get(host, section, key));
289 function def_env.hosts:list()
290 for host, host_session in pairs(hosts) do
291 self.session.print(host);
296 function def_env.hosts:add(name)
301 local function show_c2s(callback)
302 for hostname, host in pairs(hosts) do
303 for username, user in pairs(host.sessions or {}) do
304 for resource, session in pairs(user.sessions or {}) do
305 local jid = username.."@"..hostname.."/"..resource;
306 callback(jid, session);
312 function def_env.c2s:show(match_jid)
313 local print, count = self.session.print, 0;
314 show_c2s(function (jid)
315 if (not match_jid) or jid:match(match_jid) then
320 return true, "Total: "..count.." clients";
323 function def_env.c2s:show_insecure(match_jid)
324 local print, count = self.session.print, 0;
325 show_c2s(function (jid, session)
326 if ((not match_jid) or jid:match(match_jid)) and not session.secure then
331 return true, "Total: "..count.." insecure client connections";
334 function def_env.c2s:show_secure(match_jid)
335 local print, count = self.session.print, 0;
336 show_c2s(function (jid, session)
337 if ((not match_jid) or jid:match(match_jid)) and session.secure then
342 return true, "Total: "..count.." secure client connections";
345 function def_env.c2s:close(match_jid)
346 local print, count = self.session.print, 0;
347 show_c2s(function (jid, session)
348 if jid == match_jid or jid_bare(jid) == match_jid then
353 return true, "Total: "..count.." sessions closed";
357 function def_env.s2s:show(match_jid)
358 local _print = self.session.print;
359 local print = self.session.print;
361 local count_in, count_out = 0,0;
363 for host, host_session in pairs(hosts) do
364 print = function (...) _print(host); _print(...); print = _print; end
365 for remotehost, session in pairs(host_session.s2sout) do
366 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then
367 count_out = count_out + 1;
368 print(" "..host.." -> "..remotehost);
369 if session.sendq then
370 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection");
372 if session.type == "s2sout_unauthed" then
373 if session.connecting then
374 print(" Connection not yet established");
375 if not session.srv_hosts then
376 if not session.conn then
377 print(" We do not yet have a DNS answer for this host's SRV records");
379 print(" This host has no SRV records, using A record instead");
381 elseif session.srv_choice then
382 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
383 local srv_choice = session.srv_hosts[session.srv_choice];
384 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
386 elseif session.notopen then
387 print(" The <stream> has not yet been opened");
388 elseif not session.dialback_key then
389 print(" Dialback has not been initiated yet");
390 elseif session.dialback_key then
391 print(" Dialback has been requested, but no result received");
397 for session in pairs(incoming_s2s) do
398 if session.to_host == host and ((not match_jid) or host:match(match_jid)
399 or (session.from_host and session.from_host:match(match_jid))) then
400 count_in = count_in + 1;
401 print(" "..host.." <- "..(session.from_host or "(unknown)"));
402 if session.type == "s2sin_unauthed" then
403 print(" Connection not yet authenticated");
405 for name in pairs(session.hosts) do
406 if name ~= session.from_host then
407 print(" also hosts "..tostring(name));
416 for session in pairs(incoming_s2s) do
417 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
418 count_in = count_in + 1;
419 print("Other incoming s2s connections");
420 print(" (unknown) <- "..(session.from_host or "(unknown)"));
424 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections";
427 function def_env.s2s:close(from, to)
428 local print, count = self.session.print, 0;
430 if not (from and to) then
431 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'";
432 elseif from == to then
433 return false, "Both from and to are the same... you can't do that :)";
436 if hosts[from] and not hosts[to] then
437 -- Is an outgoing connection
438 local session = hosts[from].s2sout[to];
440 print("No outgoing connection from "..from.." to "..to)
442 s2smanager.destroy_session(session);
444 print("Closed outgoing session from "..from.." to "..to);
446 elseif hosts[to] and not hosts[from] then
447 -- Is an incoming connection
448 for session in pairs(incoming_s2s) do
449 if session.to_host == to and session.from_host == from then
450 s2smanager.destroy_session(session);
456 print("No incoming connections from "..from.." to "..to);
458 print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to);
460 elseif hosts[to] and hosts[from] then
461 return false, "Both of the hostnames you specified are local, there are no s2s sessions to close";
463 return false, "Neither of the hostnames you specified are being used on this server";
466 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
471 function printbanner(session)
472 local option = config.get("*", "core", "console_banner");
473 if option == nil or option == "full" or option == "graphic" then
476 | _ \ _ __ ___ ___ _-_ __| |_ _
477 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
478 | __/| | | (_) \__ \ |_| | (_| | |_| |
479 |_| |_| \___/|___/\___/ \__,_|\__, |
480 A study in simplicity |___/
484 if option == nil or option == "short" or option == "full" then
485 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
486 session.print("You may find more help on using this console in our online documentation at ");
487 session.print("http://prosody.im/doc/console\n");
489 if option and option ~= "short" and option ~= "full" and option ~= "graphic" then
490 if type(option) == "string" then
491 session.print(option)
492 elseif type(option) == "function" then
493 setfenv(option, redirect_output(_G, session));
494 pcall(option, session);