clix/send.lua

changeset 18
6ad3ea055905
parent 16
3a1c076d9382
child 21
cdeb02d9546d
equal deleted inserted replaced
17:fa9efbef8a0c 18:6ad3ea055905
1 short_opts.i = "interactive"; 1 short_opts.i = "interactive";
2 short_opts.e = "echo";
2 3
3 return function (opts, arg) 4 return function (opts, arg)
4 if (#arg == 0 or opts.help) and not opts.interactive then 5 if (#arg == 0 or opts.help) and not opts.interactive then
5 return 0; 6 return 0;
6 end 7 end
7 local function on_connect(conn) 8 local function on_connect(conn)
8 local function send_message(_, text) 9 local function send_message(text)
9 conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(text)); 10 conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(text));
10 end 11 end
11 if opts.interactive then 12 if opts.interactive or opts.stdin then
12 -- Fake socket object around stdin 13 -- Fake socket object around stdin
13 local stdin = { 14 local stdin = {
14 getfd = function () return 0; end; 15 getfd = function () return 0; end;
15 dirty = function (self) return false; end; 16 dirty = function (self) return false; end;
16 settimeout = function () end; 17 settimeout = function () end;
17 send = function (_, d) return #d, 0; end; 18 send = function (_, d) return #d, 0; end;
18 close = function () end; 19 close = function () end;
19 receive = function (_, patt) 20 receive = function (_, patt)
20 local data = io.stdin:read(patt); 21 local data = io.stdin:read(patt);
21 io.write(data, patt == "*l" and "\n" or ""); 22 if opts.echo then
23 io.write(data, patt == "*l" and "\n" or "");
24 end
22 return data; 25 return data;
23 end 26 end
24 }; 27 };
25 stdin = require "net.server".wrapclient(stdin, "stdin", 0, { onincoming = send_message, ondisconnect = function () end }, "*l"); 28 local function on_incoming(stdin, text)
29 send_message(text);
30 if not opts.interactive then
31 conn:close();
32 end
33 end
34 stdin = require "net.server".wrapclient(stdin, "stdin", 0, {
35 onincoming = on_incoming, ondisconnect = function () end
36 }, "*l");
26 else 37 else
27 send_message(nil, table.concat(arg, " ")); 38 send_message(table.concat(arg, " "));
28 conn:close(); 39 conn:close();
29 end 40 end
30 end 41 end
31 clix_connect(opts, on_connect); 42 clix_connect(opts, on_connect);
32 end 43 end

mercurial