clix.raw: Add a small sandbox with util.stanza functions

Mon, 16 Jan 2012 21:27:33 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 16 Jan 2012 21:27:33 +0100
changeset 68
b7d92ed9c268
parent 67
776d97484dc5
child 69
4b65a9a0229b

clix.raw: Add a small sandbox with util.stanza functions

clix/raw.lua file | annotate | diff | comparison | revisions
--- a/clix/raw.lua	Sun Nov 06 20:08:26 2011 +0100
+++ b/clix/raw.lua	Mon Jan 16 21:27:33 2012 +0100
@@ -23,14 +23,38 @@
 				close = function () end;
 				receive = function (_, patt)
 					local data = io.stdin:read(patt);
+				if data == nil then
+					conn:close();
+				end
 					if opts.echo then
 						io.write(data, patt == "*l" and "\n" or "");
 					end
 					return data;
 				end
 			};
+			local env = setmetatable({}, { __index = {
+				s = verse.stanza,
+				m = verse.message,
+				p = verse.presence,
+				iq = verse.iq,
+			}});
 			local function on_incoming(stdin, data)
-				conn:send(data);
+				if not data then
+					conn:close();
+					return
+				end
+				if data:sub(1,1) ~= "<" then
+					local chunk = assert(loadstring("return "..data, "@stdin"));
+					data = "";
+					setfenv(chunk, env);
+					local ok, ret = pcall(chunk);
+					if ok then
+						data = ret
+					end
+				end
+				if data then
+					conn:send(data);
+				end
 				if not opts.interactive then
 					conn:close();
 				end

mercurial