rtbl_admin: Allow reason and text annotations when adding/updated an entry

Wed, 15 Mar 2023 12:11:29 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 15 Mar 2023 12:11:29 +0000
changeset 171
7362add76bcd
parent 170
eae589a33624
child 172
3420808b8d3f

rtbl_admin: Allow reason and text annotations when adding/updated an entry

plugins/rtbl_admin.lua file | annotate | diff | comparison | revisions
--- a/plugins/rtbl_admin.lua	Wed Mar 15 12:11:01 2023 +0000
+++ b/plugins/rtbl_admin.lua	Wed Mar 15 12:11:29 2023 +0000
@@ -6,6 +6,7 @@
 	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 };
 
@@ -15,15 +16,37 @@
 		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 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 xmlns="urn:xmpp:reporting:1" reason="urn:xmpp:reporting:abuse"/>
-			verse.stanza("report", {
-				xmlns = "urn:xmpp:reporting:1";
-				reason = "urn:xmpp:reporting:abuse"; }),
+			report,
 			function (success) -- callback
 				if not success then
 					command:reply("Failed to update RTBL");

mercurial