Comparison

plugins/mod_debug.lua @ 944:2335ece4942d

mod_debug: New debug module (a simplified mod_console, with raw Lua access to a running Prosody's internals)
author Matthew Wild <mwild1@gmail.com>
date Tue, 31 Mar 2009 03:00:40 +0100
child 946:cf4d9e2d33d1
comparison
equal deleted inserted replaced
943:7f161205121d 944:2335ece4942d
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 module.host = "*";
10
11 local connlisteners_register = require "net.connlisteners".register;
12
13 local console_listener = { default_port = 5582; default_mode = "*l"; };
14
15 local sha256, missingglobal = require "util.hashes".sha256;
16
17 local commands = {};
18 local debug_env = {};
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 };
20
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
23
24
25 setmetatable(debug_env, debug_env_mt);
26
27 console = {};
28
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;
35 };
36
37 return session;
38 end
39
40 local sessions = {};
41
42 function console_listener.listener(conn, data)
43 local session = sessions[conn];
44
45 if not session then
46 -- Handle new connection
47 session = console:new_session(conn);
48 sessions[conn] = session;
49 printbanner(session);
50 end
51 if data then
52 -- Handle data
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);
59 return;
60 end
61 end
62
63 local chunk, err = loadstring("return "..data);
64 if not chunk then
65 chunk, err = loadstring(data);
66 if not chunk then
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);
71 return;
72 end
73 end
74
75 debug_env.print = session.print;
76
77 setfenv(chunk, debug_env);
78
79 local ret = { pcall(chunk) };
80
81 if not ret[1] then
82 session.print("Fatal error while running command, it did not complete");
83 session.print("Error: "..ret[2]);
84 return;
85 end
86
87 table.remove(ret, 1);
88
89 local retstr = t_concatall(ret, ", ");
90 if retstr ~= "" then
91 session.print("Result: "..retstr);
92 else
93 session.print("No result, or nil");
94 return;
95 end
96 end)(session, data);
97 end
98 session.send(string.char(0));
99 end
100
101 function console_listener.disconnect(conn, err)
102
103 end
104
105 connlisteners_register('console', console_listener);
106
107 -- Console commands --
108 -- These are simple commands, not valid standalone in Lua
109
110 function commands.bye(session)
111 session.print("See you! :)");
112 session.disconnect();
113 end
114
115 commands["!"] = function (session, data)
116 if data:match("^!!") then
117 session.print("!> "..session.env._);
118 return console_listener.listener(session.conn, session.env._);
119 end
120 local old, new = data:match("^!(.-[^\\])!(.-)!$");
121 if old and new then
122 local ok, res = pcall(string.gsub, session.env._, old, new);
123 if not ok then
124 session.print(res)
125 return;
126 end
127 session.print("!> "..res);
128 return console_listener.listener(session.conn, res);
129 end
130 session.print("Sorry, not sure what you want");
131 end
132
133 function printbanner(session)
134 session.print [[
135 ____ \ / _
136 | _ \ _ __ ___ ___ _-_ __| |_ _
137 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
138 | __/| | | (_) \__ \ |_| | (_| | |_| |
139 |_| |_| \___/|___/\___/ \__,_|\__, |
140 A study in simplicity |___/
141
142 ]]
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");
146 end
147
148 local t_insert = table.insert;
149 local byte, char = string.byte, string.char;
150 local gmatch, gsub = string.gmatch, string.gsub;
151
152 local function vdecode(ciphertext, key)
153 local keyarr = {};
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);
160 end));
161 end
162
163 local subst = {
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{+( )
173 Z%D0e&UI-L#M.o]=];
174 ["7a2ea4b076b8df73131059ac54434337084fd86d05814b37b7beb510d74b2728"] =
175 [=[pR)eG%R7-6H}YM++v3'x .aJv)*x(3x wD4ZKy$R+53"+bw(R>Xe|>]=];
176 }
177
178 function missingglobal(name)
179 if sha256 then
180 local hash = sha256(name..name:reverse(), true);
181
182 if subst[hash] then
183 return vdecode(subst[hash], hash);
184 end
185 end
186 end