clix/send.lua

Wed, 01 Dec 2021 17:25:25 +0100

author
Kim Alvefur <zash@zash.se>
date
Wed, 01 Dec 2021 17:25:25 +0100
changeset 164
fafdcde2e2eb
parent 36
0712fda16ab6
child 168
75e8ca131178
permissions
-rw-r--r--

clix: Import Verse where needed

In the olden days of `module()` this would have been a global, but that
is no longer the convention nor the case.

164
fafdcde2e2eb clix: Import Verse where needed
Kim Alvefur <zash@zash.se>
parents: 36
diff changeset
1 local verse = require "verse";
9
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
2 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
3 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
4
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 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
6 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
7 print("Send messages");
5b58c002d6ad clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
8 return;
5b58c002d6ad clix.*: Add --short-help and make sure they are working correctly
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
9 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
10 if (#arg == 0 or opts.help) and not opts.interactive then
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 return 0;
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 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
15 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
16 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
17 :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
18 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
19 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
20 -- 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 conn:close();
0712fda16ab6 close connection when no more input is available in interactive mode
Hubert Chathi <hubert@uhoreg.ca>
parents: 27
diff changeset
31 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
32 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
33 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
34 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
35 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
36 end
c1d591488695 clix.message: Add -i/--interactive to allow piping stdin over XMPP (like sendxmpp)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
37 };
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 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
39 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
40 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
41 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
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 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
44 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
45 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
46 }, "*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
47 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
48 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
49 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
50 end
0
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 clix_connect(opts, on_connect);
ae83411a89c9 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end

mercurial