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

165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local verse = require "verse";
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid = require "util.jid";
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local sha256 = require "util.hashes".sha256;
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 function riddim.plugins.rtbl_admin(bot)
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 bot.stream:add_plugin("pubsub");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local config = bot.config.rtbl_admin;
171
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
9 if not config then return; end
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
11 local permitted_affiliations = config.permitted_affiliations or { admin = true, owner = true };
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
12
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 bot:hook("commands/rtbl-add", function (command)
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
14 if config.control_room ~= (command.room and command.room.jid) then
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
15 return;
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
16 elseif not permitted_affiliations[command.sender.affiliation] then
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
17 return "You have insufficient permissions to use this command";
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
18 end
171
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
19
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
20 local reported_jid, comment = command.param:match("^(%S+)%s*(.*)$");
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
21 if not command.param or not reported_jid then
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
22 return "rtbl-add JID [spam|abuse] [COMMENT]";
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
23 end
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
24
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
25 reported_jid = jid.prep(reported_jid);
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
26 if not reported_jid then
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
27 return "Invalid JID";
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
28 end
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
29
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
30 -- <report xmlns="urn:xmpp:reporting:1" reason="urn:xmpp:reporting:abuse">
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
31 -- <text>OPTIONAL</text>
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
32 -- </report>
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
33 local report = verse.stanza("report", { xmlns = "urn:xmpp:reporting:1", reason = "urn:xmpp:reporting:abuse" });
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
34 if comment and comment ~= "" then
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
35 local tag = comment:lower():match("^%w+");
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
36 if tag == "spam" or tag == "abuse" then
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
37 report.attr.reason = "urn:xmpp:reporting:"..tag;
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
38 comment = comment:match("^%w+%s+(.+)$");
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
39 end
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
40 if comment then
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
41 report:tag("text"):text(comment):up();
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
42 end
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
43 end
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
44
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local hash = sha256(reported_jid, true);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 bot.stream.pubsub(config.host, config.node):publish(
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 hash, -- item id
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 nil, -- options (not implemented anyway)
171
7362add76bcd rtbl_admin: Allow reason and text annotations when adding/updated an entry
Matthew Wild <mwild1@gmail.com>
parents: 168
diff changeset
49 report,
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 function (success) -- callback
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if not success then
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 command:reply("Failed to update RTBL");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return;
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 command:reply("RTBL entry added");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 );
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
58 return true;
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 bot:hook("commands/rtbl-remove", function (command)
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
62 if config.control_room ~= (command.room and command.room.jid) then
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
63 return;
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
64 elseif not permitted_affiliations[command.sender.affiliation] then
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
65 return "You have insufficient permissions to use this command";
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
66 end
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local reported_jid = command.param and jid.prep(command.param:match("^%S+"));
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local hash = sha256(reported_jid, true);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 bot.stream.pubsub(config.host, config.node):retract(
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 hash, -- item id
167
2073137bc943 rtbl_admin: Notify subscribers on item removal (requires verse 98dc1750584d)
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
71 true, -- notify subscribers
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 function (success) -- callback
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if not success then
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 command:reply("Failed to update RTBL");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 return;
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 command:reply("RTBL entry removed");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 );
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
80 return true;
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end

mercurial