Comparison

plugins/muc/muc.lib.lua @ 6478:413923bbd1a0

plugins/muc/muc.lib: Add :each_affiliation() iterator
author daurnimator <quae@daurnimator.com>
date Wed, 15 Oct 2014 17:08:19 -0400
parent 6476:fb8a9873728b
child 6512:ad159be9e9d7
comparison
equal deleted inserted replaced
6477:29f979f554d3 6478:413923bbd1a0
744 -- You need to be at least an admin, and be requesting info about your affifiliation or lower 744 -- You need to be at least an admin, and be requesting info about your affifiliation or lower
745 -- e.g. an admin can't ask for a list of owners 745 -- e.g. an admin can't ask for a list of owners
746 local affiliation_rank = valid_affiliations[affiliation]; 746 local affiliation_rank = valid_affiliations[affiliation];
747 if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank then 747 if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank then
748 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin"); 748 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
749 for jid, affiliation in pairs(self._affiliations) do 749 for jid in self:each_affiliation(_aff or "none") do
750 if affiliation == _aff then 750 reply:tag("item", {affiliation = _aff, jid = jid}):up();
751 reply:tag("item", {affiliation = _aff, jid = jid}):up();
752 end
753 end 751 end
754 origin.send(reply:up()); 752 origin.send(reply:up());
755 return true; 753 return true;
756 else 754 else
757 origin.send(st.error_reply(stanza, "auth", "forbidden")); 755 origin.send(st.error_reply(stanza, "auth", "forbidden"));
986 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID. 984 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
987 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned 985 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
988 return result; 986 return result;
989 end 987 end
990 988
989 -- Iterates over jid, affiliation pairs
990 function room_mt:each_affiliation(with_affiliation)
991 if not with_affiliation then
992 return pairs(self._affiliations);
993 else
994 return function(_affiliations, jid)
995 local affiliation;
996 repeat -- Iterate until we get a match
997 jid, affiliation = next(_affiliations, jid);
998 until jid == nil or affiliation == with_affiliation
999 return jid, affiliation;
1000 end, self._affiliations, nil
1001 end
1002 end
1003
991 function room_mt:set_affiliation(actor, jid, affiliation, reason) 1004 function room_mt:set_affiliation(actor, jid, affiliation, reason)
992 if not actor then return nil, "modify", "not-acceptable"; end; 1005 if not actor then return nil, "modify", "not-acceptable"; end;
993 1006
994 local node, host, resource = jid_split(jid); 1007 local node, host, resource = jid_split(jid);
995 if not host then return nil, "modify", "not-acceptable"; end 1008 if not host then return nil, "modify", "not-acceptable"; end
1010 local actor_affiliation = self:get_affiliation(actor); 1023 local actor_affiliation = self:get_affiliation(actor);
1011 if actor_affiliation == "owner" then 1024 if actor_affiliation == "owner" then
1012 if jid_bare(actor) == jid then -- self change 1025 if jid_bare(actor) == jid then -- self change
1013 -- need at least one owner 1026 -- need at least one owner
1014 local is_last = true; 1027 local is_last = true;
1015 for j, aff in pairs(self._affiliations) do if j ~= jid and aff == "owner" then is_last = false; break; end end 1028 for j in self:each_affiliation("owner") do
1029 if j ~= jid then is_last = false; break; end
1030 end
1016 if is_last then 1031 if is_last then
1017 return nil, "cancel", "conflict"; 1032 return nil, "cancel", "conflict";
1018 end 1033 end
1019 end 1034 end
1020 -- owners can do anything else 1035 -- owners can do anything else