Thu, 28 Jun 2012 11:33:01 +0200
clix.archive: Show timestamps
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 | 4 | return function (opts, arg) |
27
5b58c002d6ad
clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
5 | if opts.short_help then |
5b58c002d6ad
clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
6 | print("Send messages"); |
5b58c002d6ad
clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
7 | return; |
5b58c002d6ad
clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
8 | 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
|
9 | if (#arg == 0 or opts.help) and not opts.interactive then |
0 | 10 | return 0; |
11 | end | |
12 | 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
|
13 | local function send_message(text) |
21
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
14 | conn:send(verse.message({ to = opts.to, |
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
15 | type = opts.type or (opts.chatroom and "groupchat") or "chat" }) |
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
16 | :body(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
|
17 | 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
|
18 | 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
|
19 | -- 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
|
20 | 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
|
21 | 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
|
22 | 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
|
23 | 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
|
24 | 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
|
25 | 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
|
26 | 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
|
27 | local data = io.stdin:read(patt); |
36
0712fda16ab6
close connection when no more input is available in interactive mode
Hubert Chathi <hubert@uhoreg.ca>
parents:
27
diff
changeset
|
28 | if data == nil then |
0712fda16ab6
close connection when no more input is available in interactive mode
Hubert Chathi <hubert@uhoreg.ca>
parents:
27
diff
changeset
|
29 | conn:close(); |
0712fda16ab6
close connection when no more input is available in interactive mode
Hubert Chathi <hubert@uhoreg.ca>
parents:
27
diff
changeset
|
30 | 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
|
31 | 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
|
32 | 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
|
33 | 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
|
34 | 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
|
35 | end |
c1d591488695
clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
36 | }; |
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
|
37 | 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
|
38 | 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
|
39 | 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
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | }, "*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
|
46 | 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
|
47 | 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
|
48 | 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
|
49 | end |
0 | 50 | end |
51 | clix_connect(opts, on_connect); | |
52 | end |