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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. local params = parse(event.request.url.query);
  64. local room_name = params["room"];
  65. local domain_name = params["domain"];
  66. local subdomain = params["subdomain"];
  67. local room_address
  68. = jid.join(room_name, muc_domain_prefix.."."..domain_name);
  69. if subdomain and subdomain ~= "" then
  70. room_address = "["..subdomain.."]"..room_address;
  71. end
  72. if not verify_token(params["token"], room_address) then
  73. return 403;
  74. end
  75. local room = get_room_from_jid(room_address);
  76. local participant_count = 0;
  77. log("debug", "Querying room %s", tostring(room_address));
  78. if room then
  79. local occupants = room._occupants;
  80. if occupants then
  81. participant_count = iterators.count(room:each_occupant());
  82. end
  83. log("debug",
  84. "there are %s occupants in room", tostring(participant_count));
  85. else
  86. log("debug", "no such room exists");
  87. end
  88. if participant_count > 1 then
  89. participant_count = participant_count - 1;
  90. end
  91. local GET_response = {
  92. headers = {
  93. content_type = "application/json";
  94. };
  95. body = [[{"participants":]]..participant_count..[[}]];
  96. };
  97. return GET_response;
  98. end
  99. --- Handles request for retrieving the room participants details
  100. -- @param event the http event, holds the request query
  101. -- @return GET response, containing a json with participants details
  102. function handle_get_room (event)
  103. local params = parse(event.request.url.query);
  104. local room_name = params["room"];
  105. local domain_name = params["domain"];
  106. local subdomain = params["subdomain"];
  107. local room_address
  108. = jid.join(room_name, muc_domain_prefix.."."..domain_name);
  109. if subdomain ~= "" then
  110. room_address = "["..subdomain.."]"..room_address;
  111. end
  112. if not verify_token(params["token"], room_address) then
  113. return 403;
  114. end
  115. local room = get_room_from_jid(room_address);
  116. local participant_count = 0;
  117. local occupants_json = array();
  118. log("debug", "Querying room %s", tostring(room_address));
  119. if room then
  120. local occupants = room._occupants;
  121. if occupants then
  122. participant_count = iterators.count(room:each_occupant());
  123. for _, occupant in room:each_occupant() do
  124. -- filter focus as we keep it as hidden participant
  125. if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
  126. for _, pr in occupant:each_session() do
  127. local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
  128. local email = pr:get_child_text("email") or "";
  129. occupants_json:push({
  130. jid = tostring(occupant.nick),
  131. email = tostring(email),
  132. display_name = tostring(nick)});
  133. end
  134. end
  135. end
  136. end
  137. log("debug",
  138. "there are %s occupants in room", tostring(participant_count));
  139. else
  140. log("debug", "no such room exists");
  141. end
  142. if participant_count > 1 then
  143. participant_count = participant_count - 1;
  144. end
  145. local GET_response = {
  146. headers = {
  147. content_type = "application/json";
  148. };
  149. body = json.encode(occupants_json);
  150. };
  151. return GET_response;
  152. end;
  153. function module.load()
  154. module:depends("http");
  155. module:provides("http", {
  156. default_path = "/";
  157. route = {
  158. ["GET room-size"] = handle_get_room_size;
  159. ["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
  160. ["GET room"] = handle_get_room;
  161. };
  162. });
  163. end