"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-6316/resources/prosody-plugins/mod_muc_max_occupants.lua" (5 Jul 2022, 2629 Bytes) of package /linux/misc/jitsi-meet-6316.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 is_healthcheck_room = module:require "util".is_healthcheck_room;
   11 
   12 local whitelist = module:get_option_set("muc_access_whitelist");
   13 local MAX_OCCUPANTS = module:get_option_number("muc_max_occupants", -1);
   14 
   15 local function count_keys(t)
   16   return it.count(it.keys(t));
   17 end
   18 
   19 local function check_for_max_occupants(event)
   20   local room, origin, stanza = event.room, event.origin, event.stanza;
   21   local user, domain, res = split_jid(stanza.attr.from);
   22 
   23   if is_healthcheck_room(room.jid) then
   24     return;
   25   end
   26 
   27   --no user object means no way to check for max occupants
   28   if user == nil then
   29     return
   30   end
   31   -- If we're a whitelisted user joining the room, don't bother checking the max
   32   -- occupants.
   33   if whitelist and whitelist:contains(domain) or whitelist:contains(user..'@'..domain) then
   34     return;
   35   end
   36 
   37     if room and not room._jid_nick[stanza.attr.from] then
   38         local max_occupants_by_room = event.room._data.max_occupants;
   39         local count = count_keys(room._occupants);
   40         -- if no of occupants limit is set per room basis use
   41         -- that settings otherwise use the global one
   42         local slots = max_occupants_by_room or MAX_OCCUPANTS;
   43 
   44         -- If there is no whitelist, just check the count.
   45         if not whitelist and count >= slots then
   46             module:log("info", "Attempt to enter a maxed out MUC");
   47             origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   48             return true;
   49         end
   50 
   51         -- TODO: Are Prosody hooks atomic, or is this a race condition?
   52         -- For each person in the room that's not on the whitelist, subtract one
   53         -- from the count.
   54         for _, occupant in room:each_occupant() do
   55             user, domain, res = split_jid(occupant.bare_jid);
   56             if not whitelist:contains(domain) and not whitelist:contains(user..'@'..domain) then
   57                 slots = slots - 1
   58             end
   59         end
   60 
   61         -- If the room is full (<0 slots left), error out.
   62         if slots <= 0 then
   63             module:log("info", "Attempt to enter a maxed out MUC");
   64             origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   65             return true;
   66         end
   67     end
   68 end
   69 
   70 if MAX_OCCUPANTS > 0 then
   71     module:hook("muc-occupant-pre-join", check_for_max_occupants, 10);
   72 end