Software /
code /
prosody
Comparison
plugins/muc/muc.lib.lua @ 6182:dbf0b09664cd
plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Fri, 28 Mar 2014 13:05:36 -0400 |
parent | 6181:6baa9a59aa03 |
child | 6183:a8e777a19816 |
comparison
equal
deleted
inserted
replaced
6181:6baa9a59aa03 | 6182:dbf0b09664cd |
---|---|
1354 | 1354 |
1355 function room_mt:get_role(nick) | 1355 function room_mt:get_role(nick) |
1356 local occupant = self:get_occupant_by_nick(nick); | 1356 local occupant = self:get_occupant_by_nick(nick); |
1357 return occupant and occupant.role or nil; | 1357 return occupant and occupant.role or nil; |
1358 end | 1358 end |
1359 function room_mt:can_set_role(actor_jid, occupant_jid, role) | 1359 |
1360 local valid_roles = { | |
1361 none = true; | |
1362 visitor = true; | |
1363 participant = true; | |
1364 moderator = true; | |
1365 } | |
1366 function room_mt:set_role(actor, occupant_jid, role, reason) | |
1367 if not actor then return nil, "modify", "not-acceptable"; end | |
1368 | |
1360 local occupant = self:get_occupant_by_nick(occupant_jid); | 1369 local occupant = self:get_occupant_by_nick(occupant_jid); |
1361 if not occupant or not actor_jid then return nil, "modify", "not-acceptable"; end | 1370 if not occupant then return nil, "modify", "not-acceptable"; end |
1362 | 1371 |
1363 if actor_jid == true then return true; end | 1372 if valid_roles[role or "none"] == nil then |
1364 | 1373 return nil, "modify", "not-acceptable"; |
1365 local actor = self:get_occupant_by_real_jid(actor_jid); | 1374 end |
1366 if actor.role == "moderator" then | 1375 role = role ~= "none" and role or nil; -- coerces `role == false` to `nil` |
1367 local occupant_affiliation = self:get_affiliation(occupant.bare_jid) | 1376 |
1368 local actor_affiliation = self:get_affiliation(actor.bare_jid) | 1377 if actor ~= true then |
1369 if occupant_affiliation ~= "owner" and occupant_affiliation ~= "admin" then | 1378 -- Can't do anything to other owners or admins |
1370 if actor_affiliation == "owner" or actor_affiliation == "admin" then | 1379 local occupant_affiliation = self:get_affiliation(occupant.bare_jid); |
1371 return true; | 1380 if occupant_affiliation == "owner" and occupant_affiliation == "admin" then |
1372 elseif occupant.role ~= "moderator" and role ~= "moderator" then | 1381 return nil, "cancel", "not-allowed"; |
1373 return true; | 1382 end |
1374 end | 1383 |
1375 end | 1384 -- If you are trying to give or take moderator role you need to be an owner or admin |
1376 end | 1385 if occupant.role == "moderator" or role == "moderator" then |
1377 return nil, "cancel", "not-allowed"; | 1386 local actor_affiliation = self:get_affiliation(actor); |
1378 end | 1387 if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then |
1379 function room_mt:set_role(actor, occupant_jid, role, reason) | 1388 return nil, "cancel", "not-allowed"; |
1380 if role == "none" then role = nil; end | 1389 end |
1381 if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end | 1390 end |
1382 local allowed, err_type, err_condition = self:can_set_role(actor, occupant_jid, role); | 1391 |
1383 if not allowed then return allowed, err_type, err_condition; end | 1392 -- Need to be in the room and a moderator |
1384 | 1393 local actor_occupant = self:get_occupant_by_real_jid(actor); |
1385 local occupant = self:get_occupant_by_nick(occupant_jid); | 1394 if not actor_occupant or actor_occupant.role ~= "moderator" then |
1386 local occupant_affiliation = self:get_affiliation(occupant.bare_jid); | 1395 return nil, "cancel", "not-allowed"; |
1396 end | |
1397 end | |
1398 | |
1387 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}); | 1399 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}); |
1388 if not role then | 1400 if not role then |
1389 x:tag("status", {code = "307"}):up(); | 1401 x:tag("status", {code = "307"}):up(); |
1390 end | 1402 end |
1391 occupant.role = role; | 1403 occupant.role = role; |