clix/send.lua

Thu, 07 Jan 2010 20:57:59 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 07 Jan 2010 20:57:59 +0000
changeset 18
6ad3ea055905
parent 16
3a1c076d9382
child 21
cdeb02d9546d
permissions
-rw-r--r--

clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)

9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
1 short_opts.i = "interactive";
18
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
2 short_opts.e = "echo";
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
3
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 return function (opts, arg)
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
5 if (#arg == 0 or opts.help) and not opts.interactive then
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 return 0;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local function on_connect(conn)
18
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
9 local function send_message(text)
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
10 conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(text));
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
11 end
18
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
12 if opts.interactive or opts.stdin then
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
13 -- Fake socket object around stdin
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
14 local stdin = {
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
15 getfd = function () return 0; end;
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
16 dirty = function (self) return false; end;
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
17 settimeout = function () end;
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
18 send = function (_, d) return #d, 0; end;
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
19 close = function () end;
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
20 receive = function (_, patt)
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
21 local data = io.stdin:read(patt);
18
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
22 if opts.echo then
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
23 io.write(data, patt == "*l" and "\n" or "");
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
24 end
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
25 return data;
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
26 end
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
27 };
18
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
28 local function on_incoming(stdin, text)
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
29 send_message(text);
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
30 if not opts.interactive then
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
31 conn:close();
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
32 end
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
33 end
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
34 stdin = require "net.server".wrapclient(stdin, "stdin", 0, {
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
35 onincoming = on_incoming, ondisconnect = function () end
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
36 }, "*l");
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
37 else
18
6ad3ea055905 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively)
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
38 send_message(table.concat(arg, " "));
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
39 conn:close();
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
40 end
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 clix_connect(opts, on_connect);
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end

mercurial