"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-5079/resources/prosody-plugins/mod_muc_allowners.lua" (17 Jun 2021, 5499 Bytes) of package /linux/misc/jitsi-meet-5079.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. See also the last Fossies "Diffs" side-by-side code changes report for "mod_muc_allowners.lua": jitsi-meet_5870_vs_jitsi-meet_5963.

    1 local filters = require 'util.filters';
    2 local jid = require "util.jid";
    3 local jid_bare = require "util.jid".bare;
    4 local jid_host = require "util.jid".host;
    5 local um_is_admin = require "core.usermanager".is_admin;
    6 local util = module:require "util";
    7 local is_healthcheck_room = util.is_healthcheck_room;
    8 local extract_subdomain = util.extract_subdomain;
    9 local presence_check_status = util.presence_check_status;
   10 local MUC_NS = 'http://jabber.org/protocol/muc';
   11 
   12 local moderated_subdomains;
   13 local moderated_rooms;
   14 
   15 local function load_config()
   16     moderated_subdomains = module:get_option_set("allowners_moderated_subdomains", {})
   17     moderated_rooms = module:get_option_set("allowners_moderated_rooms", {})
   18 end
   19 load_config();
   20 
   21 local function is_admin(jid)
   22     return um_is_admin(jid, module.host);
   23 end
   24 
   25 -- List of the bare_jids of all occupants that are currently joining (went through pre-join) and will be promoted
   26 -- as moderators. As pre-join (where added) and joined event (where removed) happen one after another this list should
   27 -- have length of 1
   28 local joining_moderator_participants = {};
   29 
   30 -- Checks whether the jid is moderated, the room name is in moderated_rooms
   31 -- or if the subdomain is in the moderated_subdomains
   32 -- @return returns on of the:
   33 --      -> false
   34 --      -> true, room_name, subdomain
   35 --      -> true, room_name, nil (if no subdomain is used for the room)
   36 local function is_moderated(room_jid)
   37     if moderated_subdomains:empty() and moderated_rooms:empty() then
   38         return false;
   39     end
   40 
   41     local room_node = jid.node(room_jid);
   42     -- parses bare room address, for multidomain expected format is:
   43     -- [subdomain]roomName@conference.domain
   44     local target_subdomain, target_room_name = extract_subdomain(room_node);
   45     if target_subdomain then
   46         if moderated_subdomains:contains(target_subdomain) then
   47             return true, target_room_name, target_subdomain;
   48         end
   49     elseif moderated_rooms:contains(room_node) then
   50         return true, room_node, nil;
   51     end
   52 
   53     return false;
   54 end
   55 
   56 module:hook("muc-occupant-pre-join", function (event)
   57     local room, occupant = event.room, event.occupant;
   58 
   59     if is_healthcheck_room(room.jid) or is_admin(occupant.bare_jid) then
   60         return;
   61     end
   62 
   63     local moderated, room_name, subdomain = is_moderated(room.jid);
   64     if moderated then
   65         local session = event.origin;
   66         local token = session.auth_token;
   67 
   68         if not token then
   69             module:log('debug', 'skip allowners for non-auth user subdomain:%s room_name:%s', subdomain, room_name);
   70             return;
   71         end
   72 
   73         if not (room_name == session.jitsi_meet_room or session.jitsi_meet_room == '*') then
   74             module:log('debug', 'skip allowners for auth user and non matching room name: %s, jwt room name: %s', room_name, session.jitsi_meet_room);
   75             return;
   76         end
   77 
   78         if not (subdomain == session.jitsi_meet_context_group) then
   79             module:log('debug', 'skip allowners for auth user and non matching room subdomain: %s, jwt subdomain: %s', subdomain, session.jitsi_meet_context_group);
   80             return;
   81         end
   82     end
   83 
   84     -- mark this participant that it will be promoted and is currently joining
   85     joining_moderator_participants[occupant.bare_jid] = true;
   86 end, 2);
   87 
   88 module:hook("muc-occupant-joined", function (event)
   89     local room, occupant = event.room, event.occupant;
   90 
   91     local promote_to_moderator = joining_moderator_participants[occupant.bare_jid];
   92     -- clear it
   93     joining_moderator_participants[occupant.bare_jid] = nil;
   94 
   95     if promote_to_moderator ~= nil then
   96         room:set_affiliation(true, occupant.bare_jid, "owner");
   97     end
   98 end, 2);
   99 
  100 module:hook_global('config-reloaded', load_config);
  101 
  102 -- Filters self-presences to a jid that exist in joining_participants array
  103 -- We want to filter those presences where we send first `participant` and just after it `moderator`
  104 function filter_stanza(stanza)
  105     -- when joining_moderator_participants is empty there is nothing to filter
  106     if next(joining_moderator_participants) == nil or not stanza.attr or not stanza.attr.to or stanza.name ~= "presence" then
  107         return stanza;
  108     end
  109 
  110     -- we want to filter presences only on this host for allowners and skip anything like lobby etc.
  111     local host_from = jid_host(stanza.attr.from);
  112     if host_from ~= module.host then
  113         return stanza;
  114     end
  115 
  116     local bare_to = jid_bare(stanza.attr.to);
  117     if stanza:get_error() and joining_moderator_participants[bare_to] then
  118         -- pre-join succeeded but joined did not so we need to clear cache
  119         joining_moderator_participants[bare_to] = nil;
  120         return stanza;
  121     end
  122 
  123     local muc_x = stanza:get_child('x', MUC_NS..'#user');
  124     if not muc_x then
  125         return stanza;
  126     end
  127 
  128     if joining_moderator_participants[bare_to] and presence_check_status(muc_x, '110') then
  129         -- skip the local presence for participant
  130         return nil;
  131     end
  132 
  133     -- skip sending the 'participant' presences to all other people in the room
  134     for item in muc_x:childtags('item') do
  135         if joining_moderator_participants[jid_bare(item.attr.jid)] then
  136             return nil;
  137         end
  138     end
  139 
  140     return stanza;
  141 end
  142 function filter_session(session)
  143     -- domain mapper is filtering on default priority 0, and we need it after that
  144     filters.add_filter(session, 'stanzas/out', filter_stanza, -1);
  145 end
  146 
  147 -- enable filtering presences
  148 filters.add_filter_hook(filter_session);