plugins/rtbl_admin.lua

Mon, 06 Dec 2021 11:27:36 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 06 Dec 2021 11:27:36 +0000
changeset 168
1c2b8d10ceed
parent 167
2073137bc943
child 171
7362add76bcd
permissions
-rw-r--r--

rtbl_admin: Add access control around commands

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;
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
10 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
11
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 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
13 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
14 return;
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
15 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
16 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
17 end
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 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
19 local hash = sha256(reported_jid, true);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 bot.stream.pubsub(config.host, config.node):publish(
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 hash, -- item id
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 nil, -- options (not implemented anyway)
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- <report xmlns="urn:xmpp:reporting:1" reason="urn:xmpp:reporting:abuse"/>
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 verse.stanza("report", {
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 xmlns = "urn:xmpp:reporting:1";
166
95b668d73ff9 rtbl_admin: Fix reason string (abuse is defined to be the most generic)
Matthew Wild <mwild1@gmail.com>
parents: 165
diff changeset
26 reason = "urn:xmpp:reporting:abuse"; }),
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 function (success) -- callback
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if not success then
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 command:reply("Failed to update RTBL");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return;
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 command:reply("RTBL entry added");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 );
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
35 return true;
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39 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
40 return;
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
41 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
42 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
43 end
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 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
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):retract(
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 hash, -- item id
167
2073137bc943 rtbl_admin: Notify subscribers on item removal (requires verse 98dc1750584d)
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
48 true, -- notify subscribers
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function (success) -- callback
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if not success then
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 command:reply("Failed to update RTBL");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return;
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 command:reply("RTBL entry removed");
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 );
168
1c2b8d10ceed rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents: 167
diff changeset
57 return true;
165
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end);
ec0567256b11 Add rtbl_admin plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end

mercurial