Thu, 23 Mar 2023 09:54:45 +0000
squishy: Add missing servercontact plugin
173
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | local jid_bare = require "util.jid".bare; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local jid_prep = require "util.jid".prep; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local sha256 = require "util.hashes".sha256; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | local function get_ban_reason(blocklist, entry) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | local msg = { ("Banned by %s"):format(blocklist.name or "RTBL"), nil, nil }; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | local reason = entry.reason:match("^urn:xmpp:reporting:(%w+)$"); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | if reason then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | msg[2] = "for "..reason; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | if entry.text then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | msg[#msg+1] = "("..entry.text..")"; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | return table.concat(msg, " "); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | function riddim.plugins.rtbl_guard(bot) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | bot.stream:add_plugin("pubsub"); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | local config = bot.config.rtbl_guard; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | if not config then return; end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | -- { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | -- [rtbl_name] = { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | -- entries = { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | -- [jid/hash] = { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | -- reason = <string> |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | -- text = <string> |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | -- } |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | -- } |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | -- } |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | local blocklists = {}; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | local function check_blocklists(jid, only_blocklist, only_entry_id) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | jid = jid_bare(jid_prep(jid)); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | local hash = sha256(jid, true); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | if only_blocklist and only_entry_id then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | if only_entry_id ~= jid and only_entry_id ~= hash then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | bot.stream:debug("No match for single entry %q == (%q | %q)", only_entry_id, jid, hash) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | return; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | return only_blocklist.entries[only_entry_id], only_blocklist; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | for rtbl_id, blocklist in pairs(only_blocklist and {only_blocklist} or blocklists) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | bot.stream:debug("Checking %s for (%q | %q)", rtbl_id, jid, hash); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | for k in pairs(blocklist.entries) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | bot.stream:debug("[%q]", k); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | local entry = blocklist.entries[hash] or blocklist.entries[jid]; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | if entry then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | return entry, blocklist; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | local function guard_room(room) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | local function process_occupant(occupant, only_blocklist, only_entry_id) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | if not occupant.real_jid then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | bot.stream:debug("Unable to determine real JID for %s - skipping RTBL checks", occupant.nick); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | return; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | local matched_entry, matched_blocklist = check_blocklists(occupant.real_jid, only_blocklist, only_entry_id); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | if not matched_entry then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | bot.stream:debug("%s is not on any RTBLs", occupant.nick); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | return; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | room:ban(occupant.nick, get_ban_reason(matched_blocklist, matched_entry)); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | -- Check future occupants when they join |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | room:hook("occupant-joined", process_occupant); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | local function process_all_occupants(blocklist, entry_id) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | bot.stream:debug("Checking all occupants of %s against RTBL (%s)", room.jid, entry_id or "all entries"); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | for _, occupant in pairs(room.occupants) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | process_occupant(occupant, blocklist, entry_id); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | -- Check existing occupants against all existing entries |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | process_all_occupants(); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | -- Check existing occupants when new entries are added |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | room:hook("rtbl-entry-added", function (event) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | process_all_occupants(event.blocklist, event.entry_id); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | room:hook("rtbl-updated", function (event) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | process_all_occupants(event.blocklist); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | local function handle_entry(blocklist, item) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 | local report = item:get_child("report", "urn:xmpp:reporting:1"); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 | if not report then return; end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | blocklist.entries[item.attr.id] = { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | reason = report.attr.reason; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | text = report:get_child_text("text"); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | }; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 | return blocklist.entries[item.attr.id]; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | local function clear_entry(blocklist, retract) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 | local cleared_entry = blocklist.entries[retract.attr.id]; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | if not cleared_entry then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | return; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | blocklist.entries[retract.attr.id] = nil; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 | return cleared_entry; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 | bot.stream:hook("pubsub/event", function (event) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 | local blocklist_id = event.from.."::"..event.node; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 | local blocklist = blocklists[blocklist_id]; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 | if not blocklist then return; end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 | local entry = handle_entry(blocklist, event.item); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 | for _, room in pairs(bot.rooms) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 | room:event("rtbl-entry-added", { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 | blocklist = blocklist; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 | entry = entry; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 | entry_id = event.item.attr.id; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 | }); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 | -- COMPAT: current verse does not emit an event for retraction |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 | bot.stream:hook("message", function (message) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 | local m_from = message.attr.from; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 | for pubsub_event in message:childtags("event", "http://jabber.org/protocol/pubsub#event") do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 | local items = pubsub_event:get_child("items"); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 | if items then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 | local node = items.attr.node; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 | local blocklist_id = m_from.."::"..node; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 | local blocklist = blocklists[blocklist_id]; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 | if not blocklist then return; end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 | for retract in items:childtags("retract") do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 | local entry = clear_entry(blocklist, retract); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 | if entry then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 | for _, room in pairs(bot.rooms) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 | room:event("rtbl-entry-removed", { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 | blocklist = blocklist; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 | entry = entry; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 | }); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 | bot:hook("started", function () |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 | for _, rtbl_config in ipairs(config) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 | local host, node = rtbl_config.host, rtbl_config.node; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 | if host and node then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 | bot.stream.pubsub(host, node):subscribe(nil, nil, function (response) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 | if response.attr.type ~= "result" then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 | bot.stream:warn("Failed to subscribe to RTBL %s::%s", host, node); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 | return; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 | bot.stream:info("Subscribed to RTBL %s::%s", host, node); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 | local blocklist = { name = rtbl_config.name, entries = {} }; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 | bot.stream.pubsub(host, node):items(true, function (items_response) |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 | if items_response.attr.type ~= "result" then |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 | bot.stream:warn("Failed to synchronize with RTBL: %s", tostring(items_response)); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 | return; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 | local items = items_response.tags[1].tags[1]; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 | bot.stream:debug("RTBL sync: %s", items); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 | for item in items:childtags("item") do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 | bot.stream:debug("RTBL item: %s", item); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 | handle_entry(blocklist, item); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 | for _, room in pairs(bot.rooms) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 | room:event("rtbl-updated", { |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 | blocklist = blocklist; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 | }); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 | local blocklist_id = host.."::"..node; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 | blocklists[blocklist_id] = blocklist; |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 | for _, room in pairs(bot.rooms) do |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 | guard_room(room); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 | end |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 | |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 | bot:hook("groupchat/joined", guard_room); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 | end); |
e46ac57fa60b
rtbl_guard: New plugin to subscribe to RTBLs and act on them in MUCs
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 | end |