annotate mod_muc_rtbl/mod_muc_rtbl.lua @ 4847:a280878c4ef2

mod_bookmarks: So long, and thanks for all the fish!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 04 Jan 2022 23:22:31 +0100
parents 0a257d1402c3
children 460f78654864
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
1 local array = require "util.array";
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
2 local it = require "util.iterators";
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local jid = require "util.jid";
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local sha256 = require "util.hashes".sha256;
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
5 local set = require "util.set";
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local st = require "util.stanza";
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local rtbl_service_jid = assert(module:get_option_string("muc_rtbl_jid"), "No RTBL JID supplied");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local rtbl_node = module:get_option_string("muc_rtbl_node", "muc_bans_sha256");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local banned_hashes = module:shared("banned_hashes");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module:depends("pubsub_subscription");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 module:add_item("pubsub-subscription", {
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 service = rtbl_service_jid;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 node = rtbl_node;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- Callbacks:
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 on_subscribed = function()
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 module:log("info", "RTBL active");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 on_error = function(err)
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 module:log("error", "Failed to subscribe to RTBL: %s::%s: %s", err.type, err.condition, err.text);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 on_item = function(event)
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local hash = event.item.attr.id;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if not hash then return; end
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 module:log("debug", "Received new hash: %s", hash);
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
32 banned_hashes[hash] = true;
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 on_retract = function (event)
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local hash = event.item.attr.id;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if not hash then return; end
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 module:log("debug", "Retracted hash: %s", hash);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 banned_hashes[hash] = nil;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 });
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
43 function request_list()
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
44 local items_request = st.iq({ to = rtbl_service_jid, from = module.host, type = "get", id = "rtbl-request" })
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
45 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
46 :tag("items", { node = rtbl_node }):up()
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
47 :up();
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
48
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
49 module:send(items_request);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
50 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
51
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
52 function update_list(event)
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
53 local from_jid = event.stanza.attr.from;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
54 if from_jid ~= rtbl_service_jid then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
55 module:log("debug", "Ignoring RTBL response from unknown sender");
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
56 return;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
57 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
58 local items_el = event.stanza:find("{http://jabber.org/protocol/pubsub}pubsub/items");
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
59 if not items_el then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
60 module:log("warn", "Invalid items response from RTBL service");
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
61 return;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
62 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
63
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
64 local old_entries = set.new(array.collect(it.keys(banned_hashes)));
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
65
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
66 local n_added, n_removed, n_total = 0, 0, 0;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
67 for item in items_el:childtags("item") do
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
68 local hash = item.attr.id;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
69 if hash then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
70 n_total = n_total + 1;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
71 if not old_entries:contains(hash) then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
72 -- New entry
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
73 n_added = n_added + 1;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
74 banned_hashes[hash] = true;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
75 else
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
76 -- Entry already existed
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
77 old_entries:remove(hash);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
78 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
79 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
80 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
81
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
82 -- Remove old entries that weren't in the received list
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
83 for hash in old_entries do
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
84 n_removed = n_removed + 1;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
85 banned_hashes[hash] = nil;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
86 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
87
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
88 module:log("info", "%d RTBL entries received from %s (%d added, %d removed)", n_total, from_jid, n_added, n_removed);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
89 return true;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
90 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
91
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
92 module:hook("iq-result/host/rtbl-request", update_list);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
93
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 module:hook("muc-occupant-pre-join", function (event)
4813
0a257d1402c3 mod_muc_rtbl: Optimize case with zero hashes
Kim Alvefur <zash@zash.se>
parents: 4812
diff changeset
95 if next(banned_hashes) == nil then return end
0a257d1402c3 mod_muc_rtbl: Optimize case with zero hashes
Kim Alvefur <zash@zash.se>
parents: 4812
diff changeset
96
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local from_bare = jid.bare(event.stanza.attr.from);
4810
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
98
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
99 local affiliation = event.room:get_affiliation(from_bare);
4811
a1fe59c06c48 mod_muc_rtbl: Fix typo in variable name in previous commit (thanks luacheck)
Matthew Wild <mwild1@gmail.com>
parents: 4810
diff changeset
100 if affiliation and affiliation ~= "none" then
4810
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
101 -- Skip check for affiliated users
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
102 return;
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
103 end
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
104
4812
9cdbb1b5e6f2 mod_muc_rtbl: Check joining user's host against RTBL
Matthew Wild <mwild1@gmail.com>
parents: 4811
diff changeset
105 local bare_hash = sha256(jid.bare(event.stanza.attr.from), true);
9cdbb1b5e6f2 mod_muc_rtbl: Check joining user's host against RTBL
Matthew Wild <mwild1@gmail.com>
parents: 4811
diff changeset
106 local host_hash = sha256(jid.host(event.stanza.attr.from), true);
9cdbb1b5e6f2 mod_muc_rtbl: Check joining user's host against RTBL
Matthew Wild <mwild1@gmail.com>
parents: 4811
diff changeset
107 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 module:log("info", "Blocked user <%s> from room <%s> due to RTBL match", from_bare, event.stanza.attr.to);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 event.origin.send(error_reply);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 return true;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end);
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
114
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
115 if prosody.start_time then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
116 request_list();
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
117 else
4809
9e9ec0f0b128 mod_muc_rtbl: Fix to hook server-started globally, to fetch entries at startup (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
118 module:hook_global("server-started", function ()
9e9ec0f0b128 mod_muc_rtbl: Fix to hook server-started globally, to fetch entries at startup (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
119 request_list();
9e9ec0f0b128 mod_muc_rtbl: Fix to hook server-started globally, to fetch entries at startup (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
120 end);
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
121 end