"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-4434/resources/prosody-plugins/mod_muc_size.lua" (25 Sep 2020, 6183 Bytes) of package /linux/misc/jitsi-meet-4434.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 -- Prosody IM
    2 -- Copyright (C) 2017 Atlassian
    3 --
    4 
    5 local jid = require "util.jid";
    6 local it = require "util.iterators";
    7 local json = require "util.json";
    8 local iterators = require "util.iterators";
    9 local array = require"util.array";
   10 
   11 local have_async = pcall(require, "util.async");
   12 if not have_async then
   13     module:log("error", "requires a version of Prosody with util.async");
   14     return;
   15 end
   16 
   17 local async_handler_wrapper = module:require "util".async_handler_wrapper;
   18 
   19 local tostring = tostring;
   20 local neturl = require "net.url";
   21 local parse = neturl.parseQuery;
   22 
   23 -- option to enable/disable room API token verifications
   24 local enableTokenVerification
   25     = module:get_option_boolean("enable_roomsize_token_verification", false);
   26 
   27 local token_util = module:require "token/util".new(module);
   28 local get_room_from_jid = module:require "util".get_room_from_jid;
   29 
   30 -- no token configuration but required
   31 if token_util == nil and enableTokenVerification then
   32     log("error", "no token configuration but it is required");
   33     return;
   34 end
   35 
   36 -- required parameter for custom muc component prefix,
   37 -- defaults to "conference"
   38 local muc_domain_prefix
   39     = module:get_option_string("muc_mapper_domain_prefix", "conference");
   40 
   41 --- Verifies room name, domain name with the values in the token
   42 -- @param token the token we received
   43 -- @param room_address the full room address jid
   44 -- @return true if values are ok or false otherwise
   45 function verify_token(token, room_address)
   46     if not enableTokenVerification then
   47         return true;
   48     end
   49 
   50     -- if enableTokenVerification is enabled and we do not have token
   51     -- stop here, cause the main virtual host can have guest access enabled
   52     -- (allowEmptyToken = true) and we will allow access to rooms info without
   53     -- a token
   54     if token == nil then
   55         log("warn", "no token provided");
   56         return false;
   57     end
   58 
   59     local session = {};
   60     session.auth_token = token;
   61     local verified, reason = token_util:process_and_verify_token(session);
   62     if not verified then
   63         log("warn", "not a valid token %s", tostring(reason));
   64         return false;
   65     end
   66 
   67     if not token_util:verify_room(session, room_address) then
   68         log("warn", "Token %s not allowed to join: %s",
   69             tostring(token), tostring(room_address));
   70         return false;
   71     end
   72 
   73     return true;
   74 end
   75 
   76 --- Handles request for retrieving the room size
   77 -- @param event the http event, holds the request query
   78 -- @return GET response, containing a json with participants count,
   79 --         tha value is without counting the focus.
   80 function handle_get_room_size(event)
   81     if (not event.request.url.query) then
   82         return { status_code = 400; };
   83     end
   84 
   85     local params = parse(event.request.url.query);
   86     local room_name = params["room"];
   87     local domain_name = params["domain"];
   88     local subdomain = params["subdomain"];
   89 
   90     local room_address
   91         = jid.join(room_name, muc_domain_prefix.."."..domain_name);
   92 
   93     if subdomain and subdomain ~= "" then
   94         room_address = "["..subdomain.."]"..room_address;
   95     end
   96 
   97     if not verify_token(params["token"], room_address) then
   98         return { status_code = 403; };
   99     end
  100 
  101     local room = get_room_from_jid(room_address);
  102     local participant_count = 0;
  103 
  104     log("debug", "Querying room %s", tostring(room_address));
  105 
  106     if room then
  107         local occupants = room._occupants;
  108         if occupants then
  109             participant_count = iterators.count(room:each_occupant());
  110         end
  111         log("debug",
  112             "there are %s occupants in room", tostring(participant_count));
  113     else
  114         log("debug", "no such room exists");
  115         return { status_code = 404; };
  116     end
  117 
  118     if participant_count > 1 then
  119         participant_count = participant_count - 1;
  120     end
  121 
  122     return { status_code = 200; body = [[{"participants":]]..participant_count..[[}]] };
  123 end
  124 
  125 --- Handles request for retrieving the room participants details
  126 -- @param event the http event, holds the request query
  127 -- @return GET response, containing a json with participants details
  128 function handle_get_room (event)
  129     if (not event.request.url.query) then
  130         return { status_code = 400; };
  131     end
  132 
  133     local params = parse(event.request.url.query);
  134     local room_name = params["room"];
  135     local domain_name = params["domain"];
  136     local subdomain = params["subdomain"];
  137     local room_address
  138         = jid.join(room_name, muc_domain_prefix.."."..domain_name);
  139 
  140     if subdomain and subdomain ~= "" then
  141         room_address = "["..subdomain.."]"..room_address;
  142     end
  143 
  144     if not verify_token(params["token"], room_address) then
  145         return { status_code = 403; };
  146     end
  147 
  148     local room = get_room_from_jid(room_address);
  149     local participant_count = 0;
  150     local occupants_json = array();
  151 
  152     log("debug", "Querying room %s", tostring(room_address));
  153 
  154     if room then
  155         local occupants = room._occupants;
  156         if occupants then
  157             participant_count = iterators.count(room:each_occupant());
  158             for _, occupant in room:each_occupant() do
  159                 -- filter focus as we keep it as hidden participant
  160                 if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
  161                     for _, pr in occupant:each_session() do
  162                     local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
  163                     local email = pr:get_child_text("email") or "";
  164                     occupants_json:push({
  165                         jid = tostring(occupant.nick),
  166                         email = tostring(email),
  167                         display_name = tostring(nick)});
  168                     end
  169                 end
  170             end
  171         end
  172         log("debug",
  173             "there are %s occupants in room", tostring(participant_count));
  174     else
  175         log("debug", "no such room exists");
  176         return { status_code = 404; };
  177     end
  178 
  179     if participant_count > 1 then
  180         participant_count = participant_count - 1;
  181     end
  182 
  183     return { status_code = 200; body = json.encode(occupants_json); };
  184 end;
  185 
  186 function module.load()
  187     module:depends("http");
  188     module:provides("http", {
  189         default_path = "/";
  190         route = {
  191             ["GET room-size"] = function (event) return async_handler_wrapper(event,handle_get_room_size) end;
  192             ["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
  193             ["GET room"] = function (event) return async_handler_wrapper(event,handle_get_room) end;
  194         };
  195     });
  196 end
  197