# HG changeset patch # User Matthew Wild # Date 1262830130 0 # Node ID c1d591488695e94ed8c22e77b13b1c335297b5e1 # Parent df4cb4a73549e3a31c06f346b2fddae8d7717fef clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp) diff -r df4cb4a73549 -r c1d591488695 clix/message.lua --- a/clix/message.lua Thu Jan 07 02:05:48 2010 +0000 +++ b/clix/message.lua Thu Jan 07 02:08:50 2010 +0000 @@ -1,10 +1,32 @@ +short_opts.i = "interactive"; + return function (opts, arg) - if #arg == 0 or opts.help then + if (#arg == 0 or opts.help) and not opts.interactive then return 0; end local function on_connect(conn) - conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(table.concat(arg, " "))); - conn:close(); + local function send_message(_, text) + conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(text)); + end + if opts.interactive then + -- Fake socket object around stdin + 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); + io.write(data, patt == "*l" and "\n" or ""); + return data; + end + }; + stdin = require "net.server".wrapclient(stdin, "stdin", 0, { onincoming = send_message, ondisconnect = function () end }, "*l"); + else + send_message(nil, table.concat(arg, " ")); + conn:close(); + end end clix_connect(opts, on_connect); end