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.
11 local prosody = _G.prosody;
12 local hosts = prosody.hosts;
13 local connlisteners_register = require "net.connlisteners".register;
15 local console_listener = { default_port = 5582; default_mode = "*l"; };
17 require "util.iterators";
18 local set, array = require "util.set", require "util.array";
22 local default_env_mt = { __index = def_env };
26 function console:new_session(conn)
27 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
28 local session = { conn = conn;
29 send = function (t) w(tostring(t)); end;
30 print = function (t) w("| "..tostring(t).."\n"); end;
31 disconnect = function () conn.close(); end;
33 session.env = setmetatable({}, default_env_mt);
35 -- Load up environment with helper objects
36 for name, t in pairs(def_env) do
37 if type(t) == "table" then
38 session.env[name] = setmetatable({ session = session }, { __index = t });
47 function console_listener.listener(conn, data)
48 local session = sessions[conn];
51 -- Handle new connection
52 session = console:new_session(conn);
53 sessions[conn] = session;
58 (function(session, data)
61 if data:match("[!.]$") then
62 local command = data:lower();
63 command = data:match("^%w+") or data:match("%p");
64 if commands[command] then
65 commands[command](session, data);
70 if data:match("^>") then
71 data = data:gsub("^>", "");
77 local chunk, err = loadstring("return "..data);
79 chunk, err = loadstring(data);
81 err = err:gsub("^%[string .-%]:%d+: ", "");
82 err = err:gsub("^:%d+: ", "");
83 err = err:gsub("'<eof>'", "the end of the line");
84 session.print("Sorry, I couldn't understand that... "..err);
89 setfenv(chunk, (useglobalenv and _G) or session.env or nil);
91 local ranok, taskok, message = pcall(chunk);
94 session.print("Fatal error while running command, it did not complete");
95 session.print("Error: "..taskok);
100 session.print("Result: "..tostring(taskok));
102 elseif (not taskok) and message then
103 session.print("Command completed with a problem");
104 session.print("Message: "..tostring(message));
108 session.print("OK: "..tostring(message));
111 session.send(string.char(0));
114 function console_listener.disconnect(conn, err)
118 connlisteners_register('console', console_listener);
120 -- Console commands --
121 -- These are simple commands, not valid standalone in Lua
123 function commands.bye(session)
124 session.print("See you! :)");
125 session.disconnect();
128 commands["!"] = function (session, data)
129 if data:match("^!!") then
130 session.print("!> "..session.env._);
131 return console_listener.listener(session.conn, session.env._);
133 local old, new = data:match("^!(.-[^\\])!(.-)!$");
135 local ok, res = pcall(string.gsub, session.env._, old, new);
140 session.print("!> "..res);
141 return console_listener.listener(session.conn, res);
143 session.print("Sorry, not sure what you want");
146 -- Session environment --
147 -- Anything in def_env will be accessible within the session as a global variable
150 function def_env.server:reload()
151 prosody.unlock_globals();
153 prosody = _G.prosody;
154 return true, "Server reloaded";
159 local function get_hosts_set(hosts)
160 if type(hosts) == "table" then
162 return set.new(hosts);
163 elseif hosts._items then
166 elseif type(hosts) == "string" then
167 return set.new { hosts };
168 elseif hosts == nil then
169 return set.new(array.collect(keys(prosody.hosts)))
170 / function (host) return prosody.hosts[host].type == "local"; end;
174 function def_env.module:load(name, hosts, config)
175 local mm = require "modulemanager";
177 hosts = get_hosts_set(hosts);
179 -- Load the module for each host
180 local ok, err, count = true, nil, 0;
182 if (not mm.is_loaded(host, name)) then
183 ok, err = mm.load(host, name, config);
186 self.session.print(err or "Unknown error loading module");
189 self.session.print("Loaded for "..host);
194 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
197 function def_env.module:unload(name, hosts)
198 local mm = require "modulemanager";
200 hosts = get_hosts_set(hosts);
202 -- Unload the module for each host
203 local ok, err, count = true, nil, 0;
205 if mm.is_loaded(host, name) then
206 ok, err = mm.unload(host, name);
209 self.session.print(err or "Unknown error unloading module");
212 self.session.print("Unloaded from "..host);
216 return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
219 function def_env.module:reload(name, hosts)
220 local mm = require "modulemanager";
222 hosts = get_hosts_set(hosts);
224 -- Reload the module for each host
225 local ok, err, count = true, nil, 0;
227 if mm.is_loaded(host, name) then
228 ok, err = mm.reload(host, name);
231 self.session.print(err or "Unknown error reloading module");
237 self.session.print("Reloaded on "..host);
241 return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err));
245 function def_env.config:load(filename, format)
246 local config_load = require "core.configmanager".load;
247 local ok, err = config_load(filename, format);
249 return false, err or "Unknown error loading config";
251 return true, "Config loaded";
254 function def_env.config:get(host, section, key)
255 local config_get = require "core.configmanager".get
256 return true, tostring(config_get(host, section, key));
260 function def_env.hosts:list()
261 for host, host_session in pairs(hosts) do
262 self.session.print(host);
267 function def_env.hosts:add(name)
272 local function show_c2s(callback)
273 for hostname, host in pairs(hosts) do
274 for username, user in pairs(host.sessions or {}) do
275 for resource, session in pairs(user.sessions or {}) do
276 local jid = username.."@"..hostname.."/"..resource;
277 callback(jid, session);
283 function def_env.c2s:show(match_jid)
284 local print, count = self.session.print, 0;
285 show_c2s(function (jid)
286 if (not match_jid) or jid:match(match_jid) then
291 return true, "Total: "..count.." clients";
294 function def_env.c2s:show_insecure(match_jid)
295 local print, count = self.session.print, 0;
296 show_c2s(function (jid, session)
297 if ((not match_jid) or jid:match(match_jid)) and not session.secure then
302 return true, "Total: "..count.." insecure client connections";
305 function def_env.c2s:show_secure(match_jid)
306 local print, count = self.session.print, 0;
307 show_c2s(function (jid, session)
308 if ((not match_jid) or jid:match(match_jid)) and session.secure then
313 return true, "Total: "..count.." secure client connections";
318 function def_env.s2s:show(match_jid)
319 local _print = self.session.print;
320 local print = self.session.print;
322 local count_in, count_out = 0,0;
324 for host, host_session in pairs(hosts) do
325 print = function (...) _print(host); _print(...); print = _print; end
326 for remotehost, session in pairs(host_session.s2sout) do
327 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then
328 count_out = count_out + 1;
329 print(" "..host.." -> "..remotehost);
330 if session.sendq then
331 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection");
333 if session.type == "s2sout_unauthed" then
334 if session.connecting then
335 print(" Connection not yet established");
336 if not session.srv_hosts then
337 if not session.conn then
338 print(" We do not yet have a DNS answer for this host's SRV records");
340 print(" This host has no SRV records, using A record instead");
342 elseif session.srv_choice then
343 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
344 local srv_choice = session.srv_hosts[session.srv_choice];
345 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
347 elseif session.notopen then
348 print(" The <stream> has not yet been opened");
349 elseif not session.dialback_key then
350 print(" Dialback has not been initiated yet");
351 elseif session.dialback_key then
352 print(" Dialback has been requested, but no result received");
358 for session in pairs(incoming_s2s) do
359 if session.to_host == host and ((not match_jid) or host:match(match_jid)
360 or (session.from_host and session.from_host:match(match_jid))) then
361 count_in = count_in + 1;
362 print(" "..host.." <- "..(session.from_host or "(unknown)"));
363 if session.type == "s2sin_unauthed" then
364 print(" Connection not yet authenticated");
366 for name in pairs(session.hosts) do
367 if name ~= session.from_host then
368 print(" also hosts "..tostring(name));
377 for session in pairs(incoming_s2s) do
378 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
379 count_in = count_in + 1;
380 print("Other incoming s2s connections");
381 print(" (unknown) <- "..(session.from_host or "(unknown)"));
385 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections";
390 function printbanner(session)
393 | _ \ _ __ ___ ___ _-_ __| |_ _
394 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
395 | __/| | | (_) \__ \ |_| | (_| | |_| |
396 |_| |_| \___/|___/\___/ \__,_|\__, |
397 A study in simplicity |___/
400 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
401 session.print("You may find more help on using this console in our online documentation at ");
402 session.print("http://prosody.im/doc/console\n");