Software /
code /
clix
Changeset
68:b7d92ed9c268
clix.raw: Add a small sandbox with util.stanza functions
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 16 Jan 2012 21:27:33 +0100 |
parents | 67:776d97484dc5 |
children | 69:4b65a9a0229b |
files | clix/raw.lua |
diffstat | 1 files changed, 25 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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