plugins/rtbl_admin.lua

Thu, 23 Mar 2023 09:54:45 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 09:54:45 +0000
changeset 174
56316e345595
parent 171
7362add76bcd
permissions
-rw-r--r--

squishy: Add missing servercontact plugin

local verse = require "verse";
local jid = require "util.jid";
local sha256 = require "util.hashes".sha256;

function riddim.plugins.rtbl_admin(bot)
	bot.stream:add_plugin("pubsub");

	local config = bot.config.rtbl_admin;
	if not config then return; end

	local permitted_affiliations = config.permitted_affiliations or { admin = true, owner = true };

	bot:hook("commands/rtbl-add", function (command)
		if config.control_room ~= (command.room and command.room.jid) then
			return;
		elseif not permitted_affiliations[command.sender.affiliation] then
			return "You have insufficient permissions to use this command";
		end

		local reported_jid, comment = command.param:match("^(%S+)%s*(.*)$");
		if not command.param or not reported_jid then
			return "rtbl-add JID [spam|abuse] [COMMENT]";
		end

		reported_jid = jid.prep(reported_jid);
		if not reported_jid then
			return "Invalid JID";
		end

		-- <report xmlns="urn:xmpp:reporting:1" reason="urn:xmpp:reporting:abuse">
		--   <text>OPTIONAL</text>
		-- </report>
		local report = verse.stanza("report", { xmlns = "urn:xmpp:reporting:1",  reason = "urn:xmpp:reporting:abuse" });
		if comment and comment ~= "" then
			local tag = comment:lower():match("^%w+");
			if tag == "spam" or tag == "abuse" then
				report.attr.reason = "urn:xmpp:reporting:"..tag;
				comment = comment:match("^%w+%s+(.+)$");
			end
			if comment then
				report:tag("text"):text(comment):up();
			end
		end

		local hash = sha256(reported_jid, true);
		bot.stream.pubsub(config.host, config.node):publish(
			hash, -- item id
			nil, -- options (not implemented anyway)
			report,
			function (success) -- callback
				if not success then
					command:reply("Failed to update RTBL");
					return;
				end
				command:reply("RTBL entry added");
			end
		);
		return true;
	end);

	bot:hook("commands/rtbl-remove", function (command)
		if config.control_room ~= (command.room and command.room.jid) then
			return;
		elseif not permitted_affiliations[command.sender.affiliation] then
			return "You have insufficient permissions to use this command";
		end
		local reported_jid = command.param and jid.prep(command.param:match("^%S+"));
		local hash = sha256(reported_jid, true);
		bot.stream.pubsub(config.host, config.node):retract(
			hash, -- item id
			true, -- notify subscribers
			function (success) -- callback
				if not success then
					command:reply("Failed to update RTBL");
					return;
				end
				command:reply("RTBL entry removed");
			end
		);
		return true;
	end);
end

mercurial