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 connlisteners_register = require "net.connlisteners".register;
13 local console_listener = { default_port = 5582; default_mode = "*l"; };
15 local sha256, missingglobal = require "util.hashes".sha256;
19 local debug_env_mt = { __index = function (t, k) return rawget(_G, k) or missingglobal(k); end, __newindex = function (t, k, v) rawset(_G, k, v); end };
21 local t_insert, t_concat = table.insert, table.concat;
22 local t_concatall = function (t, sep) local tt = {}; for k, s in pairs(t) do tt[k] = tostring(s); end return t_concat(tt, sep); end
25 setmetatable(debug_env, debug_env_mt);
29 function console:new_session(conn)
30 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
31 local session = { conn = conn;
32 send = function (t) w(tostring(t)); end;
33 print = function (t) w("| "..tostring(t).."\n"); end;
34 disconnect = function () conn.close(); end;
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);
63 local chunk, err = loadstring("return "..data);
65 chunk, err = loadstring(data);
67 err = err:gsub("^%[string .-%]:%d+: ", "");
68 err = err:gsub("^:%d+: ", "");
69 err = err:gsub("'<eof>'", "the end of the line");
70 session.print("Sorry, I couldn't understand that... "..err);
75 debug_env.print = session.print;
77 setfenv(chunk, debug_env);
79 local ret = { pcall(chunk) };
82 session.print("Fatal error while running command, it did not complete");
83 session.print("Error: "..ret[2]);
89 local retstr = t_concatall(ret, ", ");
91 session.print("Result: "..retstr);
93 session.print("No result, or nil");
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 function printbanner(session)
136 | _ \ _ __ ___ ___ _-_ __| |_ _
137 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
138 | __/| | | (_) \__ \ |_| | (_| | |_| |
139 |_| |_| \___/|___/\___/ \__,_|\__, |
140 A study in simplicity |___/
143 session.print("Welcome to the Prosody debug console. For a list of commands, type: help");
144 session.print("You may find more help on using this console in our online documentation at ");
145 session.print("http://prosody.im/doc/debugconsole\n");
148 local t_insert = table.insert;
149 local byte, char = string.byte, string.char;
150 local gmatch, gsub = string.gmatch, string.gsub;
152 local function vdecode(ciphertext, key)
154 for l in gmatch(key, ".") do t_insert(keyarr, byte(l) - 32) end
155 local pos, keylen = 0, #keyarr;
156 return (gsub(ciphertext, ".", function (letter)
157 if byte(letter) < 32 then return ""; end
158 pos = (pos%keylen)+1;
159 return char(((byte(letter) - 32 - keyarr[pos]) % 94) + 32);
164 ["fc3a2603a0795a7d1b192704a3af95fa661e1c5bc63b393ebf75904fa53d3683"] =
165 [=[<M|V2n]c30, )Y|X1H" '7 %W3KI1zf6-(vY1(&[cf$[x-(s]=];
166 ["40a0da62932391196c18baa1c297e97b14b27bf64689dbe7f8b3b9cfad6cfbee"] =
167 [=[]0W!RG6-**2t'%vzz^=8MWh&c<CA30xl;>c38]=];
168 ["1ba18bc69e1584170a4ca5d676903141a79c629236e91afa2e14b3e6b0f75a19"] =
169 [=[dSU%3nc1*\1y)$8-0Ku[H5K&(-"x3cU^a-*cz{.$!w`9'KQV2Tv)WtN{]=];
170 ["a4d8bdafa6ae55d75fc971d193eef41f89499a79dbd24f44999d06025fb7a4f9"] =
171 [=[+yNDbYHMP+a`&,d}&]S}7'Nz.3VUM4Ko8Z$42D2EdXNs$S)4!*-dq$|2
172 0WY+a)]+S%X.ndDVG6FVyzp7vVI9x}R14$\YfvvQ("4-$J!/dMT2uZ{+( )
174 ["7a2ea4b076b8df73131059ac54434337084fd86d05814b37b7beb510d74b2728"] =
175 [=[pR)eG%R7-6H}YM++v3'x .aJv)*x(3x wD4ZKy$R+53"+bw(R>Xe|>]=];
178 function missingglobal(name)
180 local hash = sha256(name..name:reverse(), true);
183 return vdecode(subst[hash], hash);