comparison mod_muc_rtbl/mod_muc_rtbl.lua @ 5174:354832098f2f

mod_muc_rtbl: move use of "private" attributes to single function This way, we reduce the scope where you can have a typo in the attribute name to that function and we encourage users to actually always call update_hashes.
author Jonas Schäfer <jonas@wielicki.name>
date Tue, 21 Feb 2023 21:41:19 +0100
parents 460f78654864
children 432587ad1642
comparison
equal deleted inserted replaced
5173:460f78654864 5174:354832098f2f
90 end 90 end
91 91
92 module:hook("iq-result/host/rtbl-request", update_list); 92 module:hook("iq-result/host/rtbl-request", update_list);
93 93
94 function update_hashes(occupant) 94 function update_hashes(occupant)
95 local bare_hash, host_hash;
95 if not occupant.mod_muc_rtbl_bare_hash then 96 if not occupant.mod_muc_rtbl_bare_hash then
96 local bare_hash = sha256(jid.bare(event.stanza.attr.from), true); 97 bare_hash = sha256(jid.bare(event.stanza.attr.from), true);
97 occupant.mod_muc_rtbl_bare_hash = bare_hash; 98 occupant.mod_muc_rtbl_bare_hash = bare_hash;
99 else
100 bare_hash = occupant.mod_muc_rtbl_bare_hash;
98 end 101 end
99 if not occupant.mod_muc_rtbl_host_hash then 102 if not occupant.mod_muc_rtbl_host_hash then
100 local host_hash = sha256(jid.host(event.stanza.attr.from), true); 103 host_hash = sha256(jid.host(event.stanza.attr.from), true);
101 event.occupant.mod_muc_rtbl_host_hash = host_hash; 104 event.occupant.mod_muc_rtbl_host_hash = host_hash;
105 else
106 host_hash = event.occupant.mod_muc_rtbl_host_hash;
102 end 107 end
108 return bare_hash, host_hash
103 end 109 end
104 110
105 module:hook("muc-occupant-pre-join", function (event) 111 module:hook("muc-occupant-pre-join", function (event)
106 if next(banned_hashes) == nil then return end 112 if next(banned_hashes) == nil then return end
107 113
111 if affiliation and affiliation ~= "none" then 117 if affiliation and affiliation ~= "none" then
112 -- Skip check for affiliated users 118 -- Skip check for affiliated users
113 return; 119 return;
114 end 120 end
115 121
116 update_hashes(event.occupant); 122 local bare_hash, host_hash = update_hashes(event.occupant);
117 if banned_hashes[event.occupant.mod_muc_rtbl_bare_hash] or banned_hashes[event.occupant.mod_muc_rtbl_host_hash] then 123 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
118 module:log("info", "Blocked user <%s> from room <%s> due to RTBL match", from_bare, event.stanza.attr.to); 124 module:log("info", "Blocked user <%s> from room <%s> due to RTBL match", from_bare, event.stanza.attr.to);
119 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid); 125 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
120 event.origin.send(error_reply); 126 event.origin.send(error_reply);
121 return true; 127 return true;
122 end 128 end
123 end); 129 end);
124 130
125 module:hook("muc-occupant-groupchat", function(event) 131 module:hook("muc-occupant-groupchat", function(event)
126 update_hashes(event.occupant); 132 local bare_hash, host_hash = update_hashes(event.occupant);
127 if banned_hashes[event.occupant.mod_muc_rtbl_bare_hash] or banned_hashes[event.occupant.mod_muc_rtbl_host_hash] then 133 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
128 module:log("debug", "Blocked message from user <%s> to room <%s> due to RTBL match", event.stanza.attr.from, event.stanza.attr.to); 134 module:log("debug", "Blocked message from user <%s> to room <%s> due to RTBL match", event.stanza.attr.from, event.stanza.attr.to);
129 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid); 135 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
130 event.origin.send(error_reply); 136 event.origin.send(error_reply);
131 return true; 137 return true;
132 end 138 end
133 end); 139 end);
134 140
135 module:hook("muc-private-message", function(event) 141 module:hook("muc-private-message", function(event)
136 local occupant = event.room:get_occupant_by_nick(event.stanza.attr.from); 142 local occupant = event.room:get_occupant_by_nick(event.stanza.attr.from);
137 update_hashes(occupant); 143 local bare_hash, host_hash = update_hashes(occupant);
138 if banned_hashes[occupant.mod_muc_rtbl_bare_hash] or banned_hashes[occupant.mod_muc_rtbl_host_hash] then 144 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
139 module:log("debug", "Blocked private message from user <%s> from room <%s> due to RTBL match", occupant.bare_jid, event.stanza.attr.to); 145 module:log("debug", "Blocked private message from user <%s> from room <%s> due to RTBL match", occupant.bare_jid, event.stanza.attr.to);
140 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid); 146 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
141 event.origin.send(error_reply); 147 event.origin.send(error_reply);
142 return true; 148 return true;
143 end 149 end