2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local connlisteners_register = require "net.connlisteners".register;
24 local console_listener = { default_port = 5582; default_mode = "*l"; };
28 local default_env_mt = { __index = def_env };
32 function console:new_session(conn)
33 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
34 local session = { conn = conn;
35 send = function (t) w(tostring(t)); end;
36 print = function (t) w("| "..tostring(t).."\n"); end;
37 disconnect = function () conn.close(); end;
39 session.env = setmetatable({}, default_env_mt);
41 -- Load up environment with helper objects
42 for name, t in pairs(def_env) do
43 if type(t) == "table" then
44 session.env[name] = setmetatable({ session = session }, { __index = t });
53 function console_listener.listener(conn, data)
54 local session = sessions[conn];
57 -- Handle new connection
58 session = console:new_session(conn);
59 sessions[conn] = session;
64 (function(session, data)
65 if data:match("[!.]$") then
66 local command = data:lower();
67 command = data:match("^%w+") or data:match("%p");
68 if commands[command] then
69 commands[command](session, data);
76 local chunk, err = loadstring("return "..data);
78 chunk, err = loadstring(data);
80 err = err:gsub("^%[string .-%]:%d+: ", "");
81 err = err:gsub("^:%d+: ", "");
82 err = err:gsub("'<eof>'", "the end of the line");
83 session.print("Sorry, I couldn't understand that... "..err);
88 setfenv(chunk, session.env);
89 local ranok, taskok, message = pcall(chunk);
92 session.print("Fatal error while running command, it did not complete");
93 session.print("Error: "..taskok);
98 session.print("Result: "..tostring(taskok));
100 elseif (not taskok) and message then
101 session.print("Command completed with a problem");
102 session.print("Message: "..tostring(message));
106 session.print("OK: "..tostring(message));
109 session.send(string.char(0));
112 function console_listener.disconnect(conn, err)
116 connlisteners_register('console', console_listener);
118 -- Console commands --
119 -- These are simple commands, not valid standalone in Lua
121 function commands.bye(session)
122 session.print("See you! :)");
123 session.disconnect();
126 commands["!"] = function (session, data)
127 if data:match("^!!") then
128 session.print("!> "..session.env._);
129 return console_listener.listener(session.conn, session.env._);
131 local old, new = data:match("^!(.-[^\\])!(.-)!$");
133 local ok, res = pcall(string.gsub, session.env._, old, new);
138 session.print("!> "..res);
139 return console_listener.listener(session.conn, res);
141 session.print("Sorry, not sure what you want");
144 -- Session environment --
145 -- Anything in def_env will be accessible within the session as a global variable
148 function def_env.server:reload()
150 return true, "Server reloaded";
154 function def_env.module:load(name, host, config)
155 local mm = require "modulemanager";
156 local ok, err = mm.load(host or self.env.host, name, config);
158 return false, err or "Unknown error loading module";
160 return true, "Module loaded";
163 function def_env.module:unload(name, host)
164 local mm = require "modulemanager";
165 local ok, err = mm.unload(host or self.env.host, name);
167 return false, err or "Unknown error unloading module";
169 return true, "Module unloaded";
172 function def_env.module:reload(name, host)
173 local mm = require "modulemanager";
174 local ok, err = mm.reload(host or self.env.host, name);
176 return false, err or "Unknown error reloading module";
178 return true, "Module reloaded";
182 function def_env.config:load(filename, format)
183 local config_load = require "core.configmanager".load;
184 local ok, err = config_load(filename, format);
186 return false, err or "Unknown error loading config";
188 return true, "Config loaded";
191 function def_env.config:get(host, section, key)
192 local config_get = require "core.configmanager".get
193 return true, tostring(config_get(host, section, key));
197 function def_env.hosts:list()
198 for host, host_session in pairs(hosts) do
199 self.session.print(host);
204 function def_env.hosts:add(name)
209 function printbanner(session)
212 | _ \ _ __ ___ ___ _-_ __| |_ _
213 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
214 | __/| | | (_) \__ \ |_| | (_| | |_| |
215 |_| |_| \___/|___/\___/ \__,_|\__, |
216 A study in simplicity |___/
219 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
220 session.print("You may find more help on using this console in our online documentation at ");
221 session.print("http://prosody.im/doc/console\n");