clix/message.lua

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

mercurial