21 settimeout = function () end; |
21 settimeout = function () end; |
22 send = function (_, d) return #d, 0; end; |
22 send = function (_, d) return #d, 0; end; |
23 close = function () end; |
23 close = function () end; |
24 receive = function (_, patt) |
24 receive = function (_, patt) |
25 local data = io.stdin:read(patt); |
25 local data = io.stdin:read(patt); |
|
26 if data == nil then |
|
27 conn:close(); |
|
28 end |
26 if opts.echo then |
29 if opts.echo then |
27 io.write(data, patt == "*l" and "\n" or ""); |
30 io.write(data, patt == "*l" and "\n" or ""); |
28 end |
31 end |
29 return data; |
32 return data; |
30 end |
33 end |
31 }; |
34 }; |
|
35 local env = setmetatable({}, { __index = { |
|
36 s = verse.stanza, |
|
37 m = verse.message, |
|
38 p = verse.presence, |
|
39 iq = verse.iq, |
|
40 }}); |
32 local function on_incoming(stdin, data) |
41 local function on_incoming(stdin, data) |
33 conn:send(data); |
42 if not data then |
|
43 conn:close(); |
|
44 return |
|
45 end |
|
46 if data:sub(1,1) ~= "<" then |
|
47 local chunk = assert(loadstring("return "..data, "@stdin")); |
|
48 data = ""; |
|
49 setfenv(chunk, env); |
|
50 local ok, ret = pcall(chunk); |
|
51 if ok then |
|
52 data = ret |
|
53 end |
|
54 end |
|
55 if data then |
|
56 conn:send(data); |
|
57 end |
34 if not opts.interactive then |
58 if not opts.interactive then |
35 conn:close(); |
59 conn:close(); |
36 end |
60 end |
37 end |
61 end |
38 stdin = require "net.server".wrapclient(stdin, "stdin", 0, { |
62 stdin = require "net.server".wrapclient(stdin, "stdin", 0, { |