plugins/msg2pubsub.lua

1
local verse = require "verse";
2
local jid = require "prosody.util.jid";
3
local sha256 = require "prosody.util.hashes".sha256;
4
 
5
function riddim.plugins.msg2pubsub(bot)
6
	bot.stream:add_plugin("pubsub");
7
 
8
	local config = bot.config.msg2pubsub;
9
	if not config then return; end
10
 
11
	bot:hook("message", function (message)
12
		print("MSG", message.body);
13
 
14
		if not message.body or message.body == "" then
15
			return;
16
		end
17
 
18
		local payload = verse.stanza("data", { xmlns = "https://prosody.im/protocol/data" })
19
			:text(message.body);
20
 
21
		bot.stream.pubsub(config.host, config.node):publish(
22
			"current", -- item id
23
			nil, -- options (not implemented anyway)
24
			payload,
25
			function (response) -- callback
26
				if response.attr.type == "error" then
27
					local e = verse.error_reply(message.stanza, "wait", "recipient-unavailable", "Failed to relay message ("..select(2, response:get_error())..")");
28
					bot:send(e);
29
					return true;
30
				else
31
					if message.stanza:get_child("request", "urn:xmpp:receipts") then
32
						local rcpt = verse.reply(message.stanza)
33
							:tag("received", { xmlns = "urn:xmpp:receipts", id = message.stanza.attr.id });
34
						bot:send(rcpt);
35
					end
36
				end
37
			end
38
		);
39
		return true;
40
 
41
	end);
42
end