2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local connlisteners_register = require "net.connlisteners".register;
13 local console_listener = { default_port = 5582; default_mode = "*l"; };
17 local default_env_mt = { __index = def_env };
21 function console:new_session(conn)
22 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
23 local session = { conn = conn;
24 send = function (t) w(tostring(t)); end;
25 print = function (t) w("| "..tostring(t).."\n"); end;
26 disconnect = function () conn.close(); end;
28 session.env = setmetatable({}, default_env_mt);
30 -- Load up environment with helper objects
31 for name, t in pairs(def_env) do
32 if type(t) == "table" then
33 session.env[name] = setmetatable({ session = session }, { __index = t });
42 function console_listener.listener(conn, data)
43 local session = sessions[conn];
46 -- Handle new connection
47 session = console:new_session(conn);
48 sessions[conn] = session;
53 (function(session, data)
54 if data:match("[!.]$") then
55 local command = data:lower();
56 command = data:match("^%w+") or data:match("%p");
57 if commands[command] then
58 commands[command](session, data);
65 local chunk, err = loadstring("return "..data);
67 chunk, err = loadstring(data);
69 err = err:gsub("^%[string .-%]:%d+: ", "");
70 err = err:gsub("^:%d+: ", "");
71 err = err:gsub("'<eof>'", "the end of the line");
72 session.print("Sorry, I couldn't understand that... "..err);
77 setfenv(chunk, session.env);
78 local ranok, taskok, message = pcall(chunk);
81 session.print("Fatal error while running command, it did not complete");
82 session.print("Error: "..taskok);
87 session.print("Result: "..tostring(taskok));
89 elseif (not taskok) and message then
90 session.print("Command completed with a problem");
91 session.print("Message: "..tostring(message));
95 session.print("OK: "..tostring(message));
98 session.send(string.char(0));
101 function console_listener.disconnect(conn, err)
105 connlisteners_register('console', console_listener);
107 -- Console commands --
108 -- These are simple commands, not valid standalone in Lua
110 function commands.bye(session)
111 session.print("See you! :)");
112 session.disconnect();
115 commands["!"] = function (session, data)
116 if data:match("^!!") then
117 session.print("!> "..session.env._);
118 return console_listener.listener(session.conn, session.env._);
120 local old, new = data:match("^!(.-[^\\])!(.-)!$");
122 local ok, res = pcall(string.gsub, session.env._, old, new);
127 session.print("!> "..res);
128 return console_listener.listener(session.conn, res);
130 session.print("Sorry, not sure what you want");
133 -- Session environment --
134 -- Anything in def_env will be accessible within the session as a global variable
137 function def_env.server:reload()
139 return true, "Server reloaded";
143 function def_env.module:load(name, host, config)
144 local mm = require "modulemanager";
145 local ok, err = mm.load(host or self.env.host, name, config);
147 return false, err or "Unknown error loading module";
149 return true, "Module loaded";
152 function def_env.module:unload(name, host)
153 local mm = require "modulemanager";
154 local ok, err = mm.unload(host or self.env.host, name);
156 return false, err or "Unknown error unloading module";
158 return true, "Module unloaded";
161 function def_env.module:reload(name, host)
162 local mm = require "modulemanager";
163 local ok, err = mm.reload(host or self.env.host, name);
165 return false, err or "Unknown error reloading module";
167 return true, "Module reloaded";
171 function def_env.config:load(filename, format)
172 local config_load = require "core.configmanager".load;
173 local ok, err = config_load(filename, format);
175 return false, err or "Unknown error loading config";
177 return true, "Config loaded";
180 function def_env.config:get(host, section, key)
181 local config_get = require "core.configmanager".get
182 return true, tostring(config_get(host, section, key));
186 function def_env.hosts:list()
187 for host, host_session in pairs(hosts) do
188 self.session.print(host);
193 function def_env.hosts:add(name)
198 function printbanner(session)
201 | _ \ _ __ ___ ___ _-_ __| |_ _
202 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
203 | __/| | | (_) \__ \ |_| | (_| | |_| |
204 |_| |_| \___/|___/\___/ \__,_|\__, |
205 A study in simplicity |___/
208 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
209 session.print("You may find more help on using this console in our online documentation at ");
210 session.print("http://prosody.im/doc/console\n");