Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mod_muc_size.lua 5.9KB

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