Comparison

plugins/muc/muc.lib.lua @ 6211:a4d26656df04

plugins/muc/muc.lib: Clean up whois handling - adds functions `can_see_real_jids` and `get_base_presence` - In `publicise_occupant_status`, we don't generate each type of presence until it's used
author daurnimator <quae@daurnimator.com>
date Thu, 03 Apr 2014 12:25:03 -0400
parent 6209:cc00e78e6a31
child 6212:b82523f92b06
comparison
equal deleted inserted replaced
6210:e9d62fff82a8 6211:a4d26656df04
215 self:route_to_occupant(occupant, stanza) 215 self:route_to_occupant(occupant, stanza)
216 end 216 end
217 end 217 end
218 end 218 end
219 219
220 -- Broadcasts an occupant's presence to the whole room 220 local function can_see_real_jids(whois, occupant)
221 -- Takes (and modifies) the x element that goes into the stanzas 221 if whois == "anyone" then
222 function room_mt:publicise_occupant_status(occupant, full_x, actor, reason) 222 return true;
223 local anon_x; 223 elseif whois == "moderators" then
224 local has_anonymous = self:get_whois() ~= "anyone"; 224 return valid_roles[occupant.role or "none"] >= valid_roles.moderator;
225 if has_anonymous then 225 end
226 anon_x = st.clone(full_x); 226 end
227 self:build_item_list(occupant, anon_x, true, nil, actor, reason); 227
228 end 228 local function get_base_presence(occupant)
229 self:build_item_list(occupant,full_x, false, nil, actor, reason);
230
231 -- General populance
232 local full_p
233 if occupant.role ~= nil then 229 if occupant.role ~= nil then
234 -- Try to use main jid's presence 230 -- Try to use main jid's presence
235 local pr = occupant:get_presence(); 231 local pr = occupant:get_presence();
236 if pr ~= nil then 232 if pr ~= nil then
237 full_p = st.clone(pr); 233 return st.clone(pr);
238 end 234 end
239 end 235 end
240 if full_p == nil then 236 return st.presence {from = occupant.nick; type = "unavailable";};
241 full_p = st.presence{from=occupant.nick; type="unavailable"}; 237 end
242 end 238
243 local anon_p; 239 -- Broadcasts an occupant's presence to the whole room
244 if has_anonymous then 240 -- Takes the x element that goes into the stanzas
245 anon_p = st.clone(full_p); 241 function room_mt:publicise_occupant_status(occupant, base_x, actor, reason)
246 anon_p:add_child(anon_x); 242 -- Build real jid and (optionally) occupant jid template presences
247 end 243 local function get_presence(is_anonymous)
248 full_p:add_child(full_x); 244 local x = st.clone(base_x);
249 245 self:build_item_list(occupant, x, is_anonymous, actor, reason);
246 return get_base_presence(occupant):add_child(x), x;
247 end
248 local full_p, full_x = get_presence(false);
249 local anon_p, anon_x;
250 local function get_anon_p()
251 if anon_p == nil then
252 anon_p, anon_x = get_presence(true);
253 end
254 return anon_p, anon_x;
255 end
256
257 local whois = self:get_whois();
258
259 -- General populance
250 for nick, n_occupant in self:each_occupant() do 260 for nick, n_occupant in self:each_occupant() do
251 if nick ~= occupant.nick or n_occupant.role == nil then 261 if nick ~= occupant.nick then
252 local pr = full_p; 262 local pr;
253 if has_anonymous and n_occupant.role ~= "moderator" and occupant.bare_jid ~= n_occupant.bare_jid then 263 if can_see_real_jids(whois, occupant) or occupant.bare_jid == n_occupant.bare_jid then
254 pr = anon_p; 264 pr = full_p;
265 else
266 pr = get_anon_p();
255 end 267 end
256 self:route_to_occupant(n_occupant, pr); 268 self:route_to_occupant(n_occupant, pr);
257 end 269 end
258 end 270 end
259 271
277 end 289 end
278 290
279 function room_mt:send_occupant_list(to, filter) 291 function room_mt:send_occupant_list(to, filter)
280 local to_bare = jid_bare(to); 292 local to_bare = jid_bare(to);
281 local is_anonymous = true; 293 local is_anonymous = true;
282 if self:get_whois() ~= "anyone" then 294 local whois = self:get_whois();
295 if whois ~= "anyone" then
283 local affiliation = self:get_affiliation(to); 296 local affiliation = self:get_affiliation(to);
284 if affiliation ~= "admin" and affiliation ~= "owner" then 297 if affiliation ~= "admin" and affiliation ~= "owner" then
285 local occupant = self:get_occupant_by_real_jid(to); 298 local occupant = self:get_occupant_by_real_jid(to);
286 if not occupant or occupant.role ~= "moderator" then 299 if not occupant or can_see_real_jids(whois, occupant) then
287 is_anonymous = false; 300 is_anonymous = false;
288 end 301 end
289 end 302 end
290 end 303 end
291 for occupant_jid, occupant in self:each_occupant() do 304 for occupant_jid, occupant in self:each_occupant() do