Wed, 15 Mar 2023 12:11:29 +0000
rtbl_admin: Allow reason and text annotations when adding/updated an entry
165 | 1 | local verse = require "verse"; |
2 | local jid = require "util.jid"; | |
3 | local sha256 = require "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; | |
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 | 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 | 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 | 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) | |
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 | 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 | ); | |
168
1c2b8d10ceed
rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents:
167
diff
changeset
|
58 | return true; |
165 | 59 | end); |
60 | ||
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 | 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 | |
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 | 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 | ); | |
168
1c2b8d10ceed
rtbl_admin: Add access control around commands
Matthew Wild <mwild1@gmail.com>
parents:
167
diff
changeset
|
80 | return true; |
165 | 81 | end); |
82 | end |