plugins/rtbl_admin.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.rtbl_admin(bot)
6
	bot.stream:add_plugin("pubsub");
7
 
8
	local config = bot.config.rtbl_admin;
9
	if not config then return; end
10
 
11
	local permitted_affiliations = config.permitted_affiliations or { admin = true, owner = true };
12
 
13
	bot:hook("commands/rtbl-add", function (command)
14
		if config.control_room ~= (command.room and command.room.jid) then
15
			return;
16
		elseif not permitted_affiliations[command.sender.affiliation] then
17
			return "You have insufficient permissions to use this command";
18
		end
19
 
20
		local reported_jid, comment = command.param:match("^(%S+)%s*(.*)$");
21
		if not command.param or not reported_jid then
22
			return "rtbl-add JID [spam|abuse] [COMMENT]";
23
		end
24
 
25
		reported_jid = jid.prep(reported_jid);
26
		if not reported_jid then
27
			return "Invalid JID";
28
		end
29
 
30
		-- <report xmlns="urn:xmpp:reporting:1" reason="urn:xmpp:reporting:abuse">
31
		--   <text>OPTIONAL</text>
32
		-- </report>
33
		local report = verse.stanza("report", { xmlns = "urn:xmpp:reporting:1",  reason = "urn:xmpp:reporting:abuse" });
34
		if comment and comment ~= "" then
35
			local tag = comment:lower():match("^%w+");
36
			if tag == "spam" or tag == "abuse" then
37
				report.attr.reason = "urn:xmpp:reporting:"..tag;
38
				comment = comment:match("^%w+%s+(.+)$");
39
			end
40
			if comment then
41
				report:tag("text"):text(comment):up();
42
			end
43
		end
44
 
45
		local hash = sha256(reported_jid, true);
46
		bot.stream.pubsub(config.host, config.node):publish(
47
			hash, -- item id
48
			nil, -- options (not implemented anyway)
49
			report,
50
			function (success) -- callback
51
				if not success then
52
					command:reply("Failed to update RTBL");
53
					return;
54
				end
55
				command:reply("RTBL entry added");
56
			end
57
		);
58
		return true;
59
	end);
60
 
61
	bot:hook("commands/rtbl-remove", function (command)
62
		if config.control_room ~= (command.room and command.room.jid) then
63
			return;
64
		elseif not permitted_affiliations[command.sender.affiliation] then
65
			return "You have insufficient permissions to use this command";
66
		end
67
		local reported_jid = command.param and jid.prep(command.param:match("^%S+"));
68
		local hash = sha256(reported_jid, true);
69
		bot.stream.pubsub(config.host, config.node):retract(
70
			hash, -- item id
71
			true, -- notify subscribers
72
			function (success) -- callback
73
				if not success then
74
					command:reply("Failed to update RTBL");
75
					return;
76
				end
77
				command:reply("RTBL entry removed");
78
			end
79
		);
80
		return true;
81
	end);
82
end