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_size.lua 6.0KB

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