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"; default_interface = "127.0.0.1" };
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 prosody.console = { commands = commands, env = def_env };
29 local function redirect_output(_G, session)
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 });
35 function console:new_session(conn)
36 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
37 local session = { conn = conn;
38 send = function (t) w(tostring(t)); end;
39 print = function (t) w("| "..tostring(t).."\n"); end;
40 disconnect = function () conn.close(); end;
42 session.env = setmetatable({}, default_env_mt);
44 -- Load up environment with helper objects
45 for name, t in pairs(def_env) do
46 if type(t) == "table" then
47 session.env[name] = setmetatable({ session = session }, { __index = t });
56 function console_listener.listener(conn, data)
57 local session = sessions[conn];
60 -- Handle new connection
61 session = console:new_session(conn);
62 sessions[conn] = session;
67 (function(session, data)
70 if data:match("^>") then
71 data = data:gsub("^>", "");
74 local command = data:lower();
75 command = data:match("^%w+") or data:match("%p");
76 if commands[command] then
77 commands[command](session, data);
84 local chunk, err = loadstring("return "..data);
86 chunk, err = loadstring(data);
88 err = err:gsub("^%[string .-%]:%d+: ", "");
89 err = err:gsub("^:%d+: ", "");
90 err = err:gsub("'<eof>'", "the end of the line");
91 session.print("Sorry, I couldn't understand that... "..err);
96 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
98 local ranok, taskok, message = pcall(chunk);
100 if not (ranok or message or useglobalenv) and commands[data:lower()] then
101 commands[data:lower()](session, data);
106 session.print("Fatal error while running command, it did not complete");
107 session.print("Error: "..taskok);
112 session.print("Result: "..tostring(taskok));
114 elseif (not taskok) and message then
115 session.print("Command completed with a problem");
116 session.print("Message: "..tostring(message));
120 session.print("OK: "..tostring(message));
123 session.send(string.char(0));
126 function console_listener.disconnect(conn, err)
130 connlisteners_register('console', console_listener);
132 -- Console commands --
133 -- These are simple commands, not valid standalone in Lua
135 function commands.bye(session)
136 session.print("See you! :)");
137 session.disconnect();
139 commands.quit, commands.exit = commands.bye, commands.bye;
141 commands["!"] = function (session, data)
142 if data:match("^!!") then
143 session.print("!> "..session.env._);
144 return console_listener.listener(session.conn, session.env._);
146 local old, new = data:match("^!(.-[^\\])!(.-)!$");
148 local ok, res = pcall(string.gsub, session.env._, old, new);
153 session.print("!> "..res);
154 return console_listener.listener(session.conn, res);
156 session.print("Sorry, not sure what you want");
159 -- Session environment --
160 -- Anything in def_env will be accessible within the session as a global variable
164 function def_env.server:insane_reload()
165 prosody.unlock_globals();
167 prosody = _G.prosody;
168 return true, "Server reloaded";
171 function def_env.server:version()
172 return true, tostring(prosody.version or "unknown");
175 function def_env.server:uptime()
176 local t = os.time()-prosody.start_time;
177 local seconds = t%60;
178 t = (t - seconds)/60;
179 local minutes = t%60;
180 t = (t - minutes)/60;
184 return true, string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)",
185 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
186 minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
189 function def_env.server:shutdown(reason)
190 prosody.shutdown(reason);
191 return true, "Shutdown initiated";
196 local function get_hosts_set(hosts, module)
197 if type(hosts) == "table" then
199 return set.new(hosts);
200 elseif hosts._items then
203 elseif type(hosts) == "string" then
204 return set.new { hosts };
205 elseif hosts == nil then
206 local mm = require "modulemanager";
207 return set.new(array.collect(keys(prosody.hosts)))
208 / function (host) return prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module); end;
212 function def_env.module:load(name, hosts, config)
213 local mm = require "modulemanager";
215 hosts = get_hosts_set(hosts);
217 -- Load the module for each host
218 local ok, err, count = true, nil, 0;
220 if (not mm.is_loaded(host, name)) then
221 ok, err = mm.load(host, name, config);
224 self.session.print(err or "Unknown error loading module");
227 self.session.print("Loaded for "..host);
232 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
235 function def_env.module:unload(name, hosts)
236 local mm = require "modulemanager";
238 hosts = get_hosts_set(hosts, name);
240 -- Unload the module for each host
241 local ok, err, count = true, nil, 0;
243 if mm.is_loaded(host, name) then
244 ok, err = mm.unload(host, name);
247 self.session.print(err or "Unknown error unloading module");
250 self.session.print("Unloaded from "..host);
254 return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
257 function def_env.module:reload(name, hosts)
258 local mm = require "modulemanager";
260 hosts = get_hosts_set(hosts, name);
262 -- Reload the module for each host
263 local ok, err, count = true, nil, 0;
265 if mm.is_loaded(host, name) then
266 ok, err = mm.reload(host, name);
269 self.session.print(err or "Unknown error reloading module");
275 self.session.print("Reloaded on "..host);
279 return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
283 function def_env.config:load(filename, format)
284 local config_load = require "core.configmanager".load;
285 local ok, err = config_load(filename, format);
287 return false, err or "Unknown error loading config";
289 return true, "Config loaded";
292 function def_env.config:get(host, section, key)
293 local config_get = require "core.configmanager".get
294 return true, tostring(config_get(host, section, key));
297 function def_env.config:reload()
298 local ok, err = prosody.reload_config();
299 return ok, (ok and "Config reloaded (you may need to reload modules to take effect)") or tostring(err);
303 function def_env.hosts:list()
304 for host, host_session in pairs(hosts) do
305 self.session.print(host);
310 function def_env.hosts:add(name)
315 local function show_c2s(callback)
316 for hostname, host in pairs(hosts) do
317 for username, user in pairs(host.sessions or {}) do
318 for resource, session in pairs(user.sessions or {}) do
319 local jid = username.."@"..hostname.."/"..resource;
320 callback(jid, session);
326 function def_env.c2s:show(match_jid)
327 local print, count = self.session.print, 0;
328 show_c2s(function (jid, session)
329 if (not match_jid) or jid:match(match_jid) then
331 local status, priority = "unavailable", tostring(session.priority or "-");
332 if session.presence then
333 status = session.presence:child_with_name("show");
335 status = status:get_text() or "[invalid!]";
337 status = "available";
340 print(jid.." - "..status.."("..priority..")");
343 return true, "Total: "..count.." clients";
346 function def_env.c2s:show_insecure(match_jid)
347 local print, count = self.session.print, 0;
348 show_c2s(function (jid, session)
349 if ((not match_jid) or jid:match(match_jid)) and not session.secure then
354 return true, "Total: "..count.." insecure client connections";
357 function def_env.c2s:show_secure(match_jid)
358 local print, count = self.session.print, 0;
359 show_c2s(function (jid, session)
360 if ((not match_jid) or jid:match(match_jid)) and session.secure then
365 return true, "Total: "..count.." secure client connections";
368 function def_env.c2s:close(match_jid)
369 local print, count = self.session.print, 0;
370 show_c2s(function (jid, session)
371 if jid == match_jid or jid_bare(jid) == match_jid then
376 return true, "Total: "..count.." sessions closed";
380 function def_env.s2s:show(match_jid)
381 local _print = self.session.print;
382 local print = self.session.print;
384 local count_in, count_out = 0,0;
386 for host, host_session in pairs(hosts) do
387 print = function (...) _print(host); _print(...); print = _print; end
388 for remotehost, session in pairs(host_session.s2sout) do
389 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then
390 count_out = count_out + 1;
391 print(" "..host.." -> "..remotehost);
392 if session.sendq then
393 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection");
395 if session.type == "s2sout_unauthed" then
396 if session.connecting then
397 print(" Connection not yet established");
398 if not session.srv_hosts then
399 if not session.conn then
400 print(" We do not yet have a DNS answer for this host's SRV records");
402 print(" This host has no SRV records, using A record instead");
404 elseif session.srv_choice then
405 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
406 local srv_choice = session.srv_hosts[session.srv_choice];
407 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
409 elseif session.notopen then
410 print(" The <stream> has not yet been opened");
411 elseif not session.dialback_key then
412 print(" Dialback has not been initiated yet");
413 elseif session.dialback_key then
414 print(" Dialback has been requested, but no result received");
420 for session in pairs(incoming_s2s) do
421 if session.to_host == host and ((not match_jid) or host:match(match_jid)
422 or (session.from_host and session.from_host:match(match_jid))) then
423 count_in = count_in + 1;
424 print(" "..host.." <- "..(session.from_host or "(unknown)"));
425 if session.type == "s2sin_unauthed" then
426 print(" Connection not yet authenticated");
428 for name in pairs(session.hosts) do
429 if name ~= session.from_host then
430 print(" also hosts "..tostring(name));
439 for session in pairs(incoming_s2s) do
440 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
441 count_in = count_in + 1;
442 print("Other incoming s2s connections");
443 print(" (unknown) <- "..(session.from_host or "(unknown)"));
447 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections";
450 function def_env.s2s:close(from, to)
451 local print, count = self.session.print, 0;
453 if not (from and to) then
454 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'";
455 elseif from == to then
456 return false, "Both from and to are the same... you can't do that :)";
459 if hosts[from] and not hosts[to] then
460 -- Is an outgoing connection
461 local session = hosts[from].s2sout[to];
463 print("No outgoing connection from "..from.." to "..to)
465 s2smanager.destroy_session(session);
467 print("Closed outgoing session from "..from.." to "..to);
469 elseif hosts[to] and not hosts[from] then
470 -- Is an incoming connection
471 for session in pairs(incoming_s2s) do
472 if session.to_host == to and session.from_host == from then
473 s2smanager.destroy_session(session);
479 print("No incoming connections from "..from.." to "..to);
481 print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to);
483 elseif hosts[to] and hosts[from] then
484 return false, "Both of the hostnames you specified are local, there are no s2s sessions to close";
486 return false, "Neither of the hostnames you specified are being used on this server";
489 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
494 function printbanner(session)
495 local option = config.get("*", "core", "console_banner");
496 if option == nil or option == "full" or option == "graphic" then
499 | _ \ _ __ ___ ___ _-_ __| |_ _
500 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
501 | __/| | | (_) \__ \ |_| | (_| | |_| |
502 |_| |_| \___/|___/\___/ \__,_|\__, |
503 A study in simplicity |___/
507 if option == nil or option == "short" or option == "full" then
508 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
509 session.print("You may find more help on using this console in our online documentation at ");
510 session.print("http://prosody.im/doc/console\n");
512 if option and option ~= "short" and option ~= "full" and option ~= "graphic" then
513 if type(option) == "string" then
514 session.print(option)
515 elseif type(option) == "function" then
516 setfenv(option, redirect_output(_G, session));
517 pcall(option, session);