2017-04-04 18:25:37 +00:00
|
|
|
-- Prosody IM
|
|
|
|
-- Copyright (C) 2017 Atlassian
|
|
|
|
--
|
|
|
|
|
2017-05-04 22:12:23 +00:00
|
|
|
local jid = require "util.jid";
|
2017-04-04 18:25:37 +00:00
|
|
|
local it = require "util.iterators";
|
|
|
|
local json = require "util.json";
|
|
|
|
local iterators = require "util.iterators";
|
|
|
|
local array = require"util.array";
|
2019-12-05 10:09:58 +00:00
|
|
|
|
|
|
|
local have_async = pcall(require, "util.async");
|
|
|
|
if not have_async then
|
|
|
|
module:log("error", "requires a version of Prosody with util.async");
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2017-07-20 21:04:16 +00:00
|
|
|
local wrap_async_run = module:require "util".wrap_async_run;
|
2017-04-04 18:25:37 +00:00
|
|
|
|
|
|
|
local tostring = tostring;
|
|
|
|
local neturl = require "net.url";
|
|
|
|
local parse = neturl.parseQuery;
|
|
|
|
|
2017-05-04 22:12:23 +00:00
|
|
|
-- option to enable/disable room API token verifications
|
|
|
|
local enableTokenVerification
|
|
|
|
= module:get_option_boolean("enable_roomsize_token_verification", false);
|
|
|
|
|
|
|
|
local token_util = module:require "token/util".new(module);
|
2017-07-15 03:03:36 +00:00
|
|
|
local get_room_from_jid = module:require "util".get_room_from_jid;
|
2017-05-04 22:12:23 +00:00
|
|
|
|
|
|
|
-- no token configuration but required
|
|
|
|
if token_util == nil and enableTokenVerification then
|
|
|
|
log("error", "no token configuration but it is required");
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- required parameter for custom muc component prefix,
|
|
|
|
-- defaults to "conference"
|
|
|
|
local muc_domain_prefix
|
|
|
|
= module:get_option_string("muc_mapper_domain_prefix", "conference");
|
|
|
|
|
|
|
|
--- Verifies room name, domain name with the values in the token
|
|
|
|
-- @param token the token we received
|
|
|
|
-- @param room_address the full room address jid
|
|
|
|
-- @return true if values are ok or false otherwise
|
|
|
|
function verify_token(token, room_address)
|
|
|
|
if not enableTokenVerification then
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if enableTokenVerification is enabled and we do not have token
|
|
|
|
-- stop here, cause the main virtual host can have guest access enabled
|
|
|
|
-- (allowEmptyToken = true) and we will allow access to rooms info without
|
|
|
|
-- a token
|
|
|
|
if token == nil then
|
|
|
|
log("warn", "no token provided");
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
local session = {};
|
|
|
|
session.auth_token = token;
|
|
|
|
local verified, reason = token_util:process_and_verify_token(session);
|
|
|
|
if not verified then
|
|
|
|
log("warn", "not a valid token %s", tostring(reason));
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
if not token_util:verify_room(session, room_address) then
|
|
|
|
log("warn", "Token %s not allowed to join: %s",
|
|
|
|
tostring(token), tostring(room_address));
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Handles request for retrieving the room size
|
|
|
|
-- @param event the http event, holds the request query
|
|
|
|
-- @return GET response, containing a json with participants count,
|
|
|
|
-- tha value is without counting the focus.
|
2017-04-04 18:25:37 +00:00
|
|
|
function handle_get_room_size(event)
|
2017-07-17 22:38:29 +00:00
|
|
|
if (not event.request.url.query) then
|
|
|
|
return 400;
|
|
|
|
end
|
|
|
|
|
2017-04-04 18:25:37 +00:00
|
|
|
local params = parse(event.request.url.query);
|
|
|
|
local room_name = params["room"];
|
2017-04-05 18:41:21 +00:00
|
|
|
local domain_name = params["domain"];
|
2017-05-04 22:12:23 +00:00
|
|
|
local subdomain = params["subdomain"];
|
|
|
|
|
|
|
|
local room_address
|
|
|
|
= jid.join(room_name, muc_domain_prefix.."."..domain_name);
|
|
|
|
|
|
|
|
if subdomain and subdomain ~= "" then
|
|
|
|
room_address = "["..subdomain.."]"..room_address;
|
|
|
|
end
|
|
|
|
|
|
|
|
if not verify_token(params["token"], room_address) then
|
|
|
|
return 403;
|
|
|
|
end
|
|
|
|
|
2017-04-05 18:41:21 +00:00
|
|
|
local room = get_room_from_jid(room_address);
|
2017-04-04 18:25:37 +00:00
|
|
|
local participant_count = 0;
|
|
|
|
|
2017-04-05 18:41:21 +00:00
|
|
|
log("debug", "Querying room %s", tostring(room_address));
|
2017-04-04 18:25:37 +00:00
|
|
|
|
|
|
|
if room then
|
|
|
|
local occupants = room._occupants;
|
|
|
|
if occupants then
|
|
|
|
participant_count = iterators.count(room:each_occupant());
|
|
|
|
end
|
2017-05-04 22:12:23 +00:00
|
|
|
log("debug",
|
|
|
|
"there are %s occupants in room", tostring(participant_count));
|
2017-04-04 18:25:37 +00:00
|
|
|
else
|
|
|
|
log("debug", "no such room exists");
|
2018-07-06 21:05:29 +00:00
|
|
|
return 404;
|
2017-04-04 18:25:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if participant_count > 1 then
|
|
|
|
participant_count = participant_count - 1;
|
|
|
|
end
|
|
|
|
|
2018-07-06 21:05:29 +00:00
|
|
|
return [[{"participants":]]..participant_count..[[}]];
|
2017-04-04 18:25:37 +00:00
|
|
|
end
|
|
|
|
|
2017-05-04 22:12:23 +00:00
|
|
|
--- Handles request for retrieving the room participants details
|
|
|
|
-- @param event the http event, holds the request query
|
|
|
|
-- @return GET response, containing a json with participants details
|
2017-04-04 18:25:37 +00:00
|
|
|
function handle_get_room (event)
|
2017-07-17 22:38:29 +00:00
|
|
|
if (not event.request.url.query) then
|
|
|
|
return 400;
|
|
|
|
end
|
|
|
|
|
2017-04-04 18:25:37 +00:00
|
|
|
local params = parse(event.request.url.query);
|
|
|
|
local room_name = params["room"];
|
2017-04-05 18:41:21 +00:00
|
|
|
local domain_name = params["domain"];
|
2017-05-04 22:12:23 +00:00
|
|
|
local subdomain = params["subdomain"];
|
|
|
|
local room_address
|
|
|
|
= jid.join(room_name, muc_domain_prefix.."."..domain_name);
|
|
|
|
|
|
|
|
if subdomain ~= "" then
|
|
|
|
room_address = "["..subdomain.."]"..room_address;
|
|
|
|
end
|
|
|
|
|
|
|
|
if not verify_token(params["token"], room_address) then
|
|
|
|
return 403;
|
|
|
|
end
|
|
|
|
|
2017-04-05 18:41:21 +00:00
|
|
|
local room = get_room_from_jid(room_address);
|
2017-04-04 18:25:37 +00:00
|
|
|
local participant_count = 0;
|
|
|
|
local occupants_json = array();
|
|
|
|
|
2017-04-05 18:41:21 +00:00
|
|
|
log("debug", "Querying room %s", tostring(room_address));
|
2017-04-04 18:25:37 +00:00
|
|
|
|
|
|
|
if room then
|
|
|
|
local occupants = room._occupants;
|
|
|
|
if occupants then
|
|
|
|
participant_count = iterators.count(room:each_occupant());
|
|
|
|
for _, occupant in room:each_occupant() do
|
|
|
|
-- filter focus as we keep it as hidden participant
|
|
|
|
if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
|
|
|
|
for _, pr in occupant:each_session() do
|
|
|
|
local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
|
|
|
|
local email = pr:get_child_text("email") or "";
|
|
|
|
occupants_json:push({
|
|
|
|
jid = tostring(occupant.nick),
|
|
|
|
email = tostring(email),
|
|
|
|
display_name = tostring(nick)});
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-04 22:12:23 +00:00
|
|
|
log("debug",
|
|
|
|
"there are %s occupants in room", tostring(participant_count));
|
2017-04-04 18:25:37 +00:00
|
|
|
else
|
|
|
|
log("debug", "no such room exists");
|
2018-07-06 21:05:29 +00:00
|
|
|
return 404;
|
2017-04-04 18:25:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if participant_count > 1 then
|
|
|
|
participant_count = participant_count - 1;
|
|
|
|
end
|
|
|
|
|
2018-07-06 21:05:29 +00:00
|
|
|
return json.encode(occupants_json);
|
2017-04-04 18:25:37 +00:00
|
|
|
end;
|
|
|
|
|
2017-05-04 22:12:23 +00:00
|
|
|
function module.load()
|
|
|
|
module:depends("http");
|
2017-04-04 18:25:37 +00:00
|
|
|
module:provides("http", {
|
|
|
|
default_path = "/";
|
|
|
|
route = {
|
2017-07-20 21:04:16 +00:00
|
|
|
["GET room-size"] = function (event) return wrap_async_run(event,handle_get_room_size) end;
|
2017-04-04 18:25:37 +00:00
|
|
|
["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
|
2017-07-20 21:04:16 +00:00
|
|
|
["GET room"] = function (event) return wrap_async_run(event,handle_get_room) end;
|
2017-04-04 18:25:37 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
end
|
|
|
|
|