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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 wrap_async_run = module:require "util".wrap_async_run;
  10. local tostring = tostring;
  11. local neturl = require "net.url";
  12. local parse = neturl.parseQuery;
  13. -- option to enable/disable room API token verifications
  14. local enableTokenVerification
  15. = module:get_option_boolean("enable_roomsize_token_verification", false);
  16. local token_util = module:require "token/util".new(module);
  17. local get_room_from_jid = module:require "util".get_room_from_jid;
  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. --- Verifies room name, domain name with the values in the token
  28. -- @param token the token we received
  29. -- @param room_address the full room address jid
  30. -- @return true if values are ok or false otherwise
  31. function verify_token(token, room_address)
  32. if not enableTokenVerification then
  33. return true;
  34. end
  35. -- if enableTokenVerification is enabled and we do not have token
  36. -- stop here, cause the main virtual host can have guest access enabled
  37. -- (allowEmptyToken = true) and we will allow access to rooms info without
  38. -- a token
  39. if token == nil then
  40. log("warn", "no token provided");
  41. return false;
  42. end
  43. local session = {};
  44. session.auth_token = token;
  45. local verified, reason = token_util:process_and_verify_token(session);
  46. if not verified then
  47. log("warn", "not a valid token %s", tostring(reason));
  48. return false;
  49. end
  50. if not token_util:verify_room(session, room_address) then
  51. log("warn", "Token %s not allowed to join: %s",
  52. tostring(token), tostring(room_address));
  53. return false;
  54. end
  55. return true;
  56. end
  57. --- Handles request for retrieving the room size
  58. -- @param event the http event, holds the request query
  59. -- @return GET response, containing a json with participants count,
  60. -- tha value is without counting the focus.
  61. function handle_get_room_size(event)
  62. if (not event.request.url.query) then
  63. return 400;
  64. end
  65. local params = parse(event.request.url.query);
  66. local room_name = params["room"];
  67. local domain_name = params["domain"];
  68. local subdomain = params["subdomain"];
  69. local room_address
  70. = jid.join(room_name, muc_domain_prefix.."."..domain_name);
  71. if subdomain and subdomain ~= "" then
  72. room_address = "["..subdomain.."]"..room_address;
  73. end
  74. if not verify_token(params["token"], room_address) then
  75. return 403;
  76. end
  77. local room = get_room_from_jid(room_address);
  78. local participant_count = 0;
  79. log("debug", "Querying room %s", tostring(room_address));
  80. if room then
  81. local occupants = room._occupants;
  82. if occupants then
  83. participant_count = iterators.count(room:each_occupant());
  84. end
  85. log("debug",
  86. "there are %s occupants in room", tostring(participant_count));
  87. else
  88. log("debug", "no such room exists");
  89. return 404;
  90. end
  91. if participant_count > 1 then
  92. participant_count = participant_count - 1;
  93. end
  94. return [[{"participants":]]..participant_count..[[}]];
  95. end
  96. --- Handles request for retrieving the room participants details
  97. -- @param event the http event, holds the request query
  98. -- @return GET response, containing a json with participants details
  99. function handle_get_room (event)
  100. if (not event.request.url.query) then
  101. return 400;
  102. end
  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. return 404;
  142. end
  143. if participant_count > 1 then
  144. participant_count = participant_count - 1;
  145. end
  146. return json.encode(occupants_json);
  147. end;
  148. function module.load()
  149. module:depends("http");
  150. module:provides("http", {
  151. default_path = "/";
  152. route = {
  153. ["GET room-size"] = function (event) return wrap_async_run(event,handle_get_room_size) end;
  154. ["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
  155. ["GET room"] = function (event) return wrap_async_run(event,handle_get_room) end;
  156. };
  157. });
  158. end