clix/send.lua

Sun, 15 Nov 2020 20:45:50 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 15 Nov 2020 20:45:50 +0100
changeset 140
8815232cbbeb
parent 36
0712fda16ab6
child 164
fafdcde2e2eb
permissions
-rw-r--r--

clix.watch_pep: Take PEP node as positional argument

Thanks MattJ

short_opts.i = "interactive";
short_opts.e = "echo";

return function (opts, arg)
	if opts.short_help then
		print("Send messages");
		return;
	end
	if (#arg == 0 or opts.help) and not opts.interactive then
		return 0;
	end
	local function on_connect(conn)
		local function send_message(text)
			conn:send(verse.message({ to = opts.to,
				type = opts.type or (opts.chatroom and "groupchat") or "chat" })
				:body(text));
		end
		if opts.interactive or opts.stdin then
			-- Fake socket object around stdin
			local stdin = {
				getfd = function () return 0; end;
				dirty = function (self) return false; end;
				settimeout = function () end;
				send = function (_, d) return #d, 0; end;
				close = function () end;
				receive = function (_, patt)
					local data = io.stdin:read(patt);
					if data == nil then
						conn:close();
					end
					if opts.echo then
						io.write(data, patt == "*l" and "\n" or "");
					end
					return data;
				end
			};
			local function on_incoming(stdin, text)
				send_message(text);
				if not opts.interactive then
					conn:close();
				end
			end
			stdin = require "net.server".wrapclient(stdin, "stdin", 0, {
				onincoming = on_incoming, ondisconnect = function () end
				}, "*l");
		else
			send_message(table.concat(arg, " "));
			conn:close();
		end
	end
	clix_connect(opts, on_connect);
end

mercurial