Fri, 29 Jun 2012 20:29:11 +0200
clix.raw: Print one incoming stanza per line, instead of raw incoming data.
short_opts.i = "interactive"; short_opts.e = "echo"; return function (opts, args) if opts.short_help then print("Send/receive raw XML to/from the server"); return; end local send_xml; if opts.stdin then send_xml = io.read("*a"); end local function on_connect(conn) local print = print; local function stprint(stanza) if stanza.attr.to == conn.jid then stanza.attr.to = nil; end return print(stanza); end conn:hook("stanza", stprint) if opts.interactive then local stdin = { getfd = function () return 0; end; dirty = function (self) return false; end; settimeout = function () end; send = function (_, d) return #d, 0; end; 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) 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 end stdin = require "net.server".wrapclient(stdin, "stdin", 0, { onincoming = on_incoming, ondisconnect = function () end }, "*l"); else if send_xml then conn:send(send_xml); else conn:send(table.concat(arg, " ")); end conn:close(); end end return clix_connect(opts, on_connect); end