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 5.8KB

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