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 hosts = _G.hosts;
12 local connlisteners_register = require "net.connlisteners".register;
14 local console_listener = { default_port = 5582; default_mode = "*l"; };
18 local default_env_mt = { __index = def_env };
22 function console:new_session(conn)
23 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
24 local session = { conn = conn;
25 send = function (t) w(tostring(t)); end;
26 print = function (t) w("| "..tostring(t).."\n"); end;
27 disconnect = function () conn.close(); end;
29 session.env = setmetatable({}, default_env_mt);
31 -- Load up environment with helper objects
32 for name, t in pairs(def_env) do
33 if type(t) == "table" then
34 session.env[name] = setmetatable({ session = session }, { __index = t });
43 function console_listener.listener(conn, data)
44 local session = sessions[conn];
47 -- Handle new connection
48 session = console:new_session(conn);
49 sessions[conn] = session;
54 (function(session, data)
55 if data:match("[!.]$") then
56 local command = data:lower();
57 command = data:match("^%w+") or data:match("%p");
58 if commands[command] then
59 commands[command](session, data);
66 local chunk, err = loadstring("return "..data);
68 chunk, err = loadstring(data);
70 err = err:gsub("^%[string .-%]:%d+: ", "");
71 err = err:gsub("^:%d+: ", "");
72 err = err:gsub("'<eof>'", "the end of the line");
73 session.print("Sorry, I couldn't understand that... "..err);
78 setfenv(chunk, session.env);
79 local ranok, taskok, message = pcall(chunk);
82 session.print("Fatal error while running command, it did not complete");
83 session.print("Error: "..taskok);
88 session.print("Result: "..tostring(taskok));
90 elseif (not taskok) and message then
91 session.print("Command completed with a problem");
92 session.print("Message: "..tostring(message));
96 session.print("OK: "..tostring(message));
99 session.send(string.char(0));
102 function console_listener.disconnect(conn, err)
106 connlisteners_register('console', console_listener);
108 -- Console commands --
109 -- These are simple commands, not valid standalone in Lua
111 function commands.bye(session)
112 session.print("See you! :)");
113 session.disconnect();
116 commands["!"] = function (session, data)
117 if data:match("^!!") then
118 session.print("!> "..session.env._);
119 return console_listener.listener(session.conn, session.env._);
121 local old, new = data:match("^!(.-[^\\])!(.-)!$");
123 local ok, res = pcall(string.gsub, session.env._, old, new);
128 session.print("!> "..res);
129 return console_listener.listener(session.conn, res);
131 session.print("Sorry, not sure what you want");
134 -- Session environment --
135 -- Anything in def_env will be accessible within the session as a global variable
138 function def_env.server:reload()
140 return true, "Server reloaded";
144 function def_env.module:load(name, host, config)
145 local mm = require "modulemanager";
146 local ok, err = mm.load(host or self.env.host, name, config);
148 return false, err or "Unknown error loading module";
150 return true, "Module loaded";
153 function def_env.module:unload(name, host)
154 local mm = require "modulemanager";
155 local ok, err = mm.unload(host or self.env.host, name);
157 return false, err or "Unknown error unloading module";
159 return true, "Module unloaded";
162 function def_env.module:reload(name, host)
163 local mm = require "modulemanager";
164 local ok, err = mm.reload(host or self.env.host, name);
166 return false, err or "Unknown error reloading module";
168 return true, "Module reloaded";
172 function def_env.config:load(filename, format)
173 local config_load = require "core.configmanager".load;
174 local ok, err = config_load(filename, format);
176 return false, err or "Unknown error loading config";
178 return true, "Config loaded";
181 function def_env.config:get(host, section, key)
182 local config_get = require "core.configmanager".get
183 return true, tostring(config_get(host, section, key));
187 function def_env.hosts:list()
188 for host, host_session in pairs(hosts) do
189 self.session.print(host);
194 function def_env.hosts:add(name)
199 local function show_c2s(callback)
200 for hostname, host in pairs(hosts) do
201 for username, user in pairs(host.sessions or {}) do
202 for resource, session in pairs(user.sessions or {}) do
203 local jid = username.."@"..hostname.."/"..resource;
204 callback(jid, session);
210 function def_env.c2s:show(match_jid)
211 local print, count = self.session.print, 0;
212 show_c2s(function (jid)
213 if (not match_jid) or jid:match(match_jid) then
218 return true, "Total: "..count.." clients";
221 function def_env.c2s:show_insecure(match_jid)
222 local print, count = self.session.print, 0;
223 show_c2s(function (jid, session)
224 if ((not match_jid) or jid:match(match_jid)) and not session.secure then
229 return true, "Total: "..count.." insecure client connections";
232 function def_env.c2s:show_secure(match_jid)
233 local print, count = self.session.print, 0;
234 show_c2s(function (jid, session)
235 if ((not match_jid) or jid:match(match_jid)) and session.secure then
240 return true, "Total: "..count.." secure client connections";
245 function def_env.s2s:show(match_jid)
246 local _print = self.session.print;
247 local print = self.session.print;
248 for host, host_session in pairs(hosts) do
249 print = function (...) _print(host); _print(...); print = _print; end
250 for remotehost, session in pairs(host_session.s2sout) do
251 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then
252 print(" "..host.." -> "..remotehost);
253 if session.sendq then
254 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection");
256 if session.type == "s2sout_unauthed" then
257 if session.connecting then
258 print(" Connection not yet established");
259 if not session.srv_hosts then
260 if not session.conn then
261 print(" We do not yet have a DNS answer for this host's SRV records");
263 print(" This host has no SRV records, using A record instead");
265 elseif session.srv_choice then
266 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
267 local srv_choice = session.srv_hosts[session.srv_choice];
268 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
270 elseif session.notopen then
271 print(" The <stream> has not yet been opened");
272 elseif not session.dialback_key then
273 print(" Dialback has not been initiated yet");
274 elseif session.dialback_key then
275 print(" Dialback has been requested, but no result received");
281 for session in pairs(incoming_s2s) do
282 if session.to_host == host and ((not match_jid) or host:match(match_jid)
283 or (session.from_host and session.from_host:match(match_jid))) then
284 print(" "..host.." <- "..(session.from_host or "(unknown)"));
285 if session.type == "s2sin_unauthed" then
286 print(" Connection not yet authenticated");
288 for name in pairs(session.hosts) do
289 if name ~= session.from_host then
290 print(" also hosts "..tostring(name));
299 for session in pairs(incoming_s2s) do
300 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
301 print("Other incoming s2s connections");
302 print(" (unknown) <- "..(session.from_host or "(unknown)"));
309 function printbanner(session)
312 | _ \ _ __ ___ ___ _-_ __| |_ _
313 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
314 | __/| | | (_) \__ \ |_| | (_| | |_| |
315 |_| |_| \___/|___/\___/ \__,_|\__, |
316 A study in simplicity |___/
319 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
320 session.print("You may find more help on using this console in our online documentation at ");
321 session.print("http://prosody.im/doc/console\n");