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