You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_muc_max_occupants.lua 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. -- MUC Max Occupants
  2. -- Configuring muc_max_occupants will set a limit of the maximum number
  3. -- of participants that will be able to join in a room.
  4. -- Participants in muc_access_whitelist will not be counted for the
  5. -- max occupants value (values are jids like recorder@jitsi.meeet.example.com).
  6. -- This module is configured under the muc component that is used for jitsi-meet
  7. local split_jid = require "util.jid".split;
  8. local st = require "util.stanza";
  9. local it = require "util.iterators";
  10. local whitelist = module:get_option_set("muc_access_whitelist");
  11. local MAX_OCCUPANTS = module:get_option_number("muc_max_occupants", -1);
  12. local function count_keys(t)
  13. return it.count(it.keys(t));
  14. end
  15. local function check_for_max_occupants(event)
  16. local room, origin, stanza = event.room, event.origin, event.stanza;
  17. local actor = stanza.attr.from;
  18. local user, domain, res = split_jid(stanza.attr.from);
  19. --no user object means no way to check for max occupants
  20. if user == nil then
  21. return
  22. end
  23. -- If we're a whitelisted user joining the room, don't bother checking the max
  24. -- occupants.
  25. if whitelist and whitelist:contains(domain) or whitelist:contains(user..'@'..domain) then
  26. return;
  27. end
  28. if room and not room._jid_nick[stanza.attr.from] then
  29. local count = count_keys(room._occupants);
  30. local slots = MAX_OCCUPANTS;
  31. -- If there is no whitelist, just check the count.
  32. if not whitelist and count >= MAX_OCCUPANTS then
  33. module:log("info", "Attempt to enter a maxed out MUC");
  34. origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
  35. return true;
  36. end
  37. -- TODO: Are Prosody hooks atomic, or is this a race condition?
  38. -- For each person in the room that's not on the whitelist, subtract one
  39. -- from the count.
  40. for _, occupant in room:each_occupant() do
  41. if not whitelist:contains(domain) and not whitelist:contains(user..'@'..domain) then
  42. slots = slots - 1
  43. end
  44. end
  45. -- If the room is full (<0 slots left), error out.
  46. if slots <= 0 then
  47. module:log("info", "Attempt to enter a maxed out MUC");
  48. origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
  49. return true;
  50. end
  51. end
  52. end
  53. if MAX_OCCUPANTS > 0 then
  54. module:hook("muc-occupant-pre-join", check_for_max_occupants, 10);
  55. end