clix/bounce.lua

1
short_opts.h = "history";
2
 
3
return function (opts, arg)
4
	if opts.short_help then
5
		print("Bounce received messages to another JID");
6
		return;
7
	end
8
	local function on_connect(conn)
9
		-- Allow selective bouncing
10
		local only_node, only_server, only_resource;
11
		if opts.only_from then
12
			only_node, only_server, only_resource = jid.split(opts.only_from);
13
		end
14
		local function should_bounce(stanza)
15
			local node, server, resource = jid.split(stanza.attr.from);
16
			if only_node and (node ~= only_node)
17
				or only_server and (server ~= only_server)
18
				or only_resource and (resource ~= only_resource) then
19
				return false;
20
			end
21
			return true;
22
		end
23
		local function on_message(message)
24
			local delay = message:get_child("delay", "urn:xmpp:delay");
25
			if (delay and not opts.history) or not should_bounce(message) then
26
				return;
27
			end
28
			message.attr.from = nil;
29
			message.attr.to = opts.to;
30
			message.attr.type = opts.type or (opts.chatroom and "groupchat") or "chat";
31
			conn:send(message);
32
		end
33
		conn:hook("message", on_message);
34
	end
35
	return clix_connect(opts, on_connect);
36
end