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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. user, domain, res = split_jid(occupant.bare_jid);
  42. if not whitelist:contains(domain) and not whitelist:contains(user..'@'..domain) then
  43. slots = slots - 1
  44. end
  45. end
  46. -- If the room is full (<0 slots left), error out.
  47. if slots <= 0 then
  48. module:log("info", "Attempt to enter a maxed out MUC");
  49. origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
  50. return true;
  51. end
  52. end
  53. end
  54. if MAX_OCCUPANTS > 0 then
  55. module:hook("muc-occupant-pre-join", check_for_max_occupants, 10);
  56. end