Comparison

plugins/muc/muc.lib.lua @ 7089:890f4b2cc444

MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
author Kim Alvefur <zash@zash.se>
date Wed, 20 Jan 2016 15:04:42 +0100
parent 7013:f387fae9fb31
child 7119:50b9a7e86de9
comparison
equal deleted inserted replaced
7088:85e72077d40b 7089:890f4b2cc444
6 -- This project is MIT/X11 licensed. Please see the 6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
8 -- 8 --
9 9
10 local select = select; 10 local select = select;
11 local pairs, ipairs = pairs, ipairs; 11 local pairs = pairs;
12 local next = next; 12 local next = next;
13 local setmetatable = setmetatable; 13 local setmetatable = setmetatable;
14 14
15 local dataform = require "util.dataforms"; 15 local dataform = require "util.dataforms";
16 local iterators = require "util.iterators"; 16 local iterators = require "util.iterators";
73 local function next_copied_occupant(occupants, occupant_jid) 73 local function next_copied_occupant(occupants, occupant_jid)
74 local next_occupant_jid, raw_occupant = next(occupants, occupant_jid); 74 local next_occupant_jid, raw_occupant = next(occupants, occupant_jid);
75 if next_occupant_jid == nil then return nil end 75 if next_occupant_jid == nil then return nil end
76 return next_occupant_jid, occupant_lib.copy(raw_occupant); 76 return next_occupant_jid, occupant_lib.copy(raw_occupant);
77 end 77 end
78 function room_mt:each_occupant(read_only) 78 -- FIXME Explain what 'read_only' is supposed to be
79 function room_mt:each_occupant(read_only) -- luacheck: ignore 212
79 return next_copied_occupant, self._occupants, nil; 80 return next_copied_occupant, self._occupants, nil;
80 end 81 end
81 end 82 end
82 83
83 function room_mt:has_occupant() 84 function room_mt:has_occupant()
121 self._occupants[id] = occupant 122 self._occupants[id] = occupant
122 end 123 end
123 124
124 function room_mt:route_to_occupant(occupant, stanza) 125 function room_mt:route_to_occupant(occupant, stanza)
125 local to = stanza.attr.to; 126 local to = stanza.attr.to;
126 for jid, pr in occupant:each_session() do 127 for jid in occupant:each_session() do
127 stanza.attr.to = jid; 128 stanza.attr.to = jid;
128 self:route_stanza(stanza); 129 self:route_stanza(stanza);
129 end 130 end
130 stanza.attr.to = to; 131 stanza.attr.to = to;
131 end 132 end
148 local affiliation = self:get_affiliation(occupant.bare_jid) or "none"; 149 local affiliation = self:get_affiliation(occupant.bare_jid) or "none";
149 local role = occupant.role or "none"; 150 local role = occupant.role or "none";
150 if is_anonymous then 151 if is_anonymous then
151 add_item(x, affiliation, role, nil, nick, actor_nick, actor_jid, reason); 152 add_item(x, affiliation, role, nil, nick, actor_nick, actor_jid, reason);
152 else 153 else
153 for real_jid, session in occupant:each_session() do 154 for real_jid in occupant:each_session() do
154 add_item(x, affiliation, role, real_jid, nick, actor_nick, actor_jid, reason); 155 add_item(x, affiliation, role, real_jid, nick, actor_nick, actor_jid, reason);
155 end 156 end
156 end 157 end
157 return x 158 return x
158 end 159 end
247 self:build_item_list(occupant, self_x, false, nick, actor_nick, nil, reason); 248 self:build_item_list(occupant, self_x, false, nick, actor_nick, nil, reason);
248 self_p = st.clone(base_presence):add_child(self_x); 249 self_p = st.clone(base_presence):add_child(self_x);
249 end 250 end
250 251
251 -- General populance 252 -- General populance
252 for nick, n_occupant in self:each_occupant() do 253 for occupant_nick, n_occupant in self:each_occupant() do
253 if nick ~= occupant.nick then 254 if occupant_nick ~= occupant.nick then
254 local pr; 255 local pr;
255 if can_see_real_jids(whois, n_occupant) then 256 if can_see_real_jids(whois, n_occupant) then
256 pr = get_full_p(); 257 pr = get_full_p();
257 elseif occupant.bare_jid == n_occupant.bare_jid then 258 elseif occupant.bare_jid == n_occupant.bare_jid then
258 pr = self_p; 259 pr = self_p;
327 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up(); 328 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up();
328 end 329 end
329 return reply; 330 return reply;
330 end 331 end
331 332
332 function room_mt:handle_kickable(origin, stanza) 333 function room_mt:handle_kickable(origin, stanza) -- luacheck: ignore 212
333 local real_jid = stanza.attr.from; 334 local real_jid = stanza.attr.from;
334 local occupant = self:get_occupant_by_real_jid(real_jid); 335 local occupant = self:get_occupant_by_real_jid(real_jid);
335 if occupant == nil then return nil; end 336 if occupant == nil then return nil; end
336 local type, condition, text = stanza:get_error(); 337 local type, condition, text = stanza:get_error();
337 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error"); 338 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
386 else 387 else
387 orig_occupant = self:get_occupant_by_real_jid(real_jid); 388 orig_occupant = self:get_occupant_by_real_jid(real_jid);
388 if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room 389 if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room
389 end 390 end
390 local is_first_dest_session; 391 local is_first_dest_session;
391 if type == "unavailable" then 392 if type == "unavailable" then -- luacheck: ignore 542
393 -- FIXME Why the empty if branch?
392 -- dest_occupant = nil 394 -- dest_occupant = nil
393 elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update 395 elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update
394 log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid); 396 log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid);
395 dest_occupant = orig_occupant; 397 dest_occupant = orig_occupant;
396 else 398 else
502 end 504 end
503 self:save_occupant(dest_occupant); 505 self:save_occupant(dest_occupant);
504 506
505 if orig_occupant == nil then 507 if orig_occupant == nil then
506 -- Send occupant list to newly joined user 508 -- Send occupant list to newly joined user
507 self:send_occupant_list(real_jid, function(nick, occupant) 509 self:send_occupant_list(real_jid, function(nick, occupant) -- luacheck: ignore 212
508 -- Don't include self 510 -- Don't include self
509 return occupant:get_presence(real_jid) == nil; 511 return occupant:get_presence(real_jid) == nil;
510 end) 512 end)
511 end 513 end
512 self:publicise_occupant_status(dest_occupant, dest_x); 514 self:publicise_occupant_status(dest_occupant, dest_x);
544 local id = stanza.attr.id; 546 local id = stanza.attr.id;
545 local occupant = self:get_occupant_by_nick(to); 547 local occupant = self:get_occupant_by_nick(to);
546 if (type == "error" or type == "result") then 548 if (type == "error" or type == "result") then
547 do -- deconstruct_stanza_id 549 do -- deconstruct_stanza_id
548 if not occupant then return nil; end 550 if not occupant then return nil; end
549 local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$"); 551 local from_jid, orig_id, to_jid_hash = (base64.decode(id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$");
550 if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end 552 if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
551 local from_occupant_jid = self:get_occupant_jid(from_jid); 553 local from_occupant_jid = self:get_occupant_jid(from_jid);
552 if from_occupant_jid == nil then return nil; end 554 if from_occupant_jid == nil then return nil; end
553 local session_jid 555 local session_jid
554 for to_jid in occupant:each_session() do 556 for to_jid in occupant:each_session() do
556 session_jid = to_jid; 558 session_jid = to_jid;
557 break; 559 break;
558 end 560 end
559 end 561 end
560 if session_jid == nil then return nil; end 562 if session_jid == nil then return nil; end
561 stanza.attr.from, stanza.attr.to, stanza.attr.id = from_occupant_jid, session_jid, id; 563 stanza.attr.from, stanza.attr.to, stanza.attr.id = from_occupant_jid, session_jid, orig_id;
562 end 564 end
563 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to); 565 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
564 self:route_stanza(stanza); 566 self:route_stanza(stanza);
565 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; 567 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
566 return true; 568 return true;
692 694
693 -- Removes everyone from the room 695 -- Removes everyone from the room
694 function room_mt:clear(x) 696 function room_mt:clear(x)
695 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); 697 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
696 local occupants_updated = {}; 698 local occupants_updated = {};
697 for nick, occupant in self:each_occupant() do 699 for nick, occupant in self:each_occupant() do -- luacheck: ignore 213
698 occupant.role = nil; 700 occupant.role = nil;
699 self:save_occupant(occupant); 701 self:save_occupant(occupant);
700 occupants_updated[occupant] = true; 702 occupants_updated[occupant] = true;
701 end 703 end
702 for occupant in pairs(occupants_updated) do 704 for occupant in pairs(occupants_updated) do
881 end 883 end
882 884
883 -- Need visitor role or higher to invite 885 -- Need visitor role or higher to invite
884 module:hook("muc-pre-invite", function(event) 886 module:hook("muc-pre-invite", function(event)
885 local room, stanza = event.room, event.stanza; 887 local room, stanza = event.room, event.stanza;
886 local _from, _to = stanza.attr.from, stanza.attr.to; 888 local _from = stanza.attr.from;
887 local inviter = room:get_occupant_by_real_jid(_from); 889 local inviter = room:get_occupant_by_real_jid(_from);
888 local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from)); 890 local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from));
889 if valid_roles[role or "none"] <= valid_roles.visitor then 891 if valid_roles[role or "none"] <= valid_roles.visitor then
890 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); 892 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
891 return true; 893 return true;
953 :tag("decline", {from = stanza.attr.from}) 955 :tag("decline", {from = stanza.attr.from})
954 :tag("reason"):text(payload:get_child_text("reason")):up() 956 :tag("reason"):text(payload:get_child_text("reason")):up()
955 :up() 957 :up()
956 :up(); 958 :up();
957 if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then 959 if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then
958 local declinee = decline.attr.to; -- re-fetch, in case event modified it 960 declinee = decline.attr.to; -- re-fetch, in case event modified it
959 local occupant 961 local occupant
960 if jid_bare(declinee) == self.jid then -- declinee jid is already an in-room jid 962 if jid_bare(declinee) == self.jid then -- declinee jid is already an in-room jid
961 occupant = self:get_occupant_by_nick(declinee); 963 occupant = self:get_occupant_by_nick(declinee);
962 end 964 end
963 if occupant then 965 if occupant then
989 return self:handle_kickable(origin, stanza) 991 return self:handle_kickable(origin, stanza)
990 elseif type == nil then 992 elseif type == nil then
991 local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user"); 993 local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
992 if x then 994 if x then
993 local payload = x.tags[1]; 995 local payload = x.tags[1];
994 if payload == nil then 996 if payload == nil then --luacheck: ignore 542
995 -- fallthrough 997 -- fallthrough
996 elseif payload.name == "invite" and payload.attr.to then 998 elseif payload.name == "invite" and payload.attr.to then
997 return self:handle_mediated_invite(origin, stanza) 999 return self:handle_mediated_invite(origin, stanza)
998 elseif payload.name == "decline" and payload.attr.to then 1000 elseif payload.name == "decline" and payload.attr.to then
999 return self:handle_mediated_decline(origin, stanza) 1001 return self:handle_mediated_decline(origin, stanza)
1002 return true; 1004 return true;
1003 end 1005 end
1004 end 1006 end
1005 end 1007 end
1006 1008
1007 function room_mt:route_stanza(stanza) 1009 function room_mt:route_stanza(stanza) -- luacheck: ignore 212
1008 module:send(stanza); 1010 module:send(stanza);
1009 end 1011 end
1010 1012
1011 function room_mt:get_affiliation(jid) 1013 function room_mt:get_affiliation(jid)
1012 local node, host, resource = jid_split(jid); 1014 local node, host, resource = jid_split(jid);
1076 1078
1077 -- Update roles 1079 -- Update roles
1078 local role = self:get_default_role(affiliation); 1080 local role = self:get_default_role(affiliation);
1079 local role_rank = valid_roles[role or "none"]; 1081 local role_rank = valid_roles[role or "none"];
1080 local occupants_updated = {}; -- Filled with old roles 1082 local occupants_updated = {}; -- Filled with old roles
1081 for nick, occupant in self:each_occupant() do 1083 for nick, occupant in self:each_occupant() do -- luacheck: ignore 213
1082 if occupant.bare_jid == jid or ( 1084 if occupant.bare_jid == jid or (
1083 -- Outcast can be by host. 1085 -- Outcast can be by host.
1084 is_host_only and affiliation == "outcast" and select(2, jid_split(occupant.bare_jid)) == host 1086 is_host_only and affiliation == "outcast" and select(2, jid_split(occupant.bare_jid)) == host
1085 ) then 1087 ) then
1086 -- need to publcize in all cases; as affiliation in <item/> has changed. 1088 -- need to publcize in all cases; as affiliation in <item/> has changed.
1112 elseif is_semi_anonymous and 1114 elseif is_semi_anonymous and
1113 (old_role == "moderator" and occupant.role ~= "moderator") or 1115 (old_role == "moderator" and occupant.role ~= "moderator") or
1114 (old_role ~= "moderator" and occupant.role == "moderator") then -- Has gained or lost moderator status 1116 (old_role ~= "moderator" and occupant.role == "moderator") then -- Has gained or lost moderator status
1115 -- Send everyone else's presences (as jid visibility has changed) 1117 -- Send everyone else's presences (as jid visibility has changed)
1116 for real_jid in occupant:each_session() do 1118 for real_jid in occupant:each_session() do
1117 self:send_occupant_list(real_jid, function(occupant_jid, occupant) 1119 self:send_occupant_list(real_jid, function(occupant_jid, occupant) --luacheck: ignore 212 433
1118 return occupant.bare_jid ~= jid; 1120 return occupant.bare_jid ~= jid;
1119 end); 1121 end);
1120 end 1122 end
1121 end 1123 end
1122 end 1124 end
1193 room_mt.get_whois = whois.get; 1195 room_mt.get_whois = whois.get;
1194 room_mt.set_whois = whois.set; 1196 room_mt.set_whois = whois.set;
1195 1197
1196 local _M = {}; -- module "muc" 1198 local _M = {}; -- module "muc"
1197 1199
1198 function _M.new_room(jid, config) 1200 function _M.new_room(jid, config) -- luacheck: ignore 212
1201 -- TODO use config?
1199 return setmetatable({ 1202 return setmetatable({
1200 jid = jid; 1203 jid = jid;
1201 _jid_nick = {}; 1204 _jid_nick = {};
1202 _occupants = {}; 1205 _occupants = {};
1203 _data = { 1206 _data = {