Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mod_muc_size.lua 6.1KB

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