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.

util.lib.lua 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. local jid = require "util.jid";
  2. local have_async, async = pcall(require, "util.async");
  3. local muc_domain_prefix
  4. = module:get_option_string("muc_mapper_domain_prefix", "conference");
  5. -- defaults to module.host, the module that uses the utility
  6. local muc_domain_base
  7. = module:get_option_string("muc_mapper_domain_base", module.host);
  8. -- The "real" MUC domain that we are proxying to
  9. local muc_domain = module:get_option_string(
  10. "muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
  11. local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
  12. local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
  13. -- The pattern used to extract the target subdomain
  14. -- (e.g. extract 'foo' from 'foo.muc.example.com')
  15. local target_subdomain_pattern
  16. = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  17. --- Utility function to check and convert a room JID from
  18. -- virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
  19. -- @param room_jid the room jid to match and rewrite if needed
  20. -- @return returns room jid [foo]room1@muc.example.com when it has subdomain
  21. -- otherwise room1@muc.example.com(the room_jid value untouched)
  22. local function room_jid_match_rewrite(room_jid)
  23. local node, host, resource = jid.split(room_jid);
  24. local target_subdomain = host and host:match(target_subdomain_pattern);
  25. if not target_subdomain then
  26. module:log("debug", "No need to rewrite out 'to' %s", room_jid);
  27. return room_jid;
  28. end
  29. -- Ok, rewrite room_jid address to new format
  30. local new_node, new_host, new_resource
  31. = "["..target_subdomain.."]"..node, muc_domain, resource;
  32. room_jid = jid.join(new_node, new_host, new_resource);
  33. module:log("debug", "Rewrote to %s", room_jid);
  34. return room_jid
  35. end
  36. --- Finds and returns room by its jid
  37. -- @param room_jid the room jid to search in the muc component
  38. -- @return returns room if found or nil
  39. function get_room_from_jid(room_jid)
  40. local _, host = jid.split(room_jid);
  41. local component = hosts[host];
  42. if component then
  43. local muc = component.modules.muc
  44. if muc and rawget(muc,"rooms") then
  45. -- We're running 0.9.x or 0.10 (old MUC API)
  46. return muc.rooms[room_jid];
  47. elseif muc and rawget(muc,"get_room_from_jid") then
  48. -- We're running >0.10 (new MUC API)
  49. return muc.get_room_from_jid(room_jid);
  50. else
  51. return
  52. end
  53. end
  54. end
  55. function wrap_async_run(event,handler)
  56. local have_async = pcall(require, "util.async");
  57. if not have_async then
  58. module:log("error", "requires a version of Prosody with util.async");
  59. return nil;
  60. end
  61. local runner = async.runner;
  62. -- Grab a local response so that we can send the http response when
  63. -- the handler is done.
  64. local response = event.response;
  65. local async_func = runner(function (event)
  66. local result = handler(event);
  67. -- if it is a number, it is a status code, else it is the body
  68. -- result we will return
  69. if (tonumber(result) ~= nil) then
  70. response.status_code = result;
  71. else
  72. response.body = result;
  73. end;
  74. -- Send the response to the waiting http client.
  75. response:send();
  76. end)
  77. async_func:run(event)
  78. -- return true to keep the client http connection open.
  79. return true;
  80. end
  81. function async_handler_wrapper(event, handler)
  82. -- Grab a local response so that we can send the http response when
  83. -- the handler is done.
  84. local response = event.response;
  85. local async_func = runner(
  86. function (event)
  87. local result = handler(event)
  88. -- If there is a status code in the result from the
  89. -- wrapped handler then add it to the response.
  90. if tonumber(result.status_code) ~= nil then
  91. response.status_code = result.status_code
  92. end
  93. -- If there are headers in the result from the
  94. -- wrapped handler then add them to the response.
  95. if result.headers ~= nil then
  96. response.headers = result.headers
  97. end
  98. -- Send the response to the waiting http client with
  99. -- or without the body from the wrapped handler.
  100. if result.body ~= nil then
  101. response:send(result.body)
  102. else
  103. response:send();
  104. end
  105. end
  106. )
  107. async_func:run(event)
  108. -- return true to keep the client http connection open.
  109. return true;
  110. end
  111. --- Updates presence stanza, by adding identity node
  112. -- @param stanza the presence stanza
  113. -- @param user the user to which presence we are updating identity
  114. -- @param group the group of the user to which presence we are updating identity
  115. -- @param creator_user the user who created the user which presence we
  116. -- are updating (this is the poltergeist case, where a user creates
  117. -- a poltergeist), optional.
  118. -- @param creator_group the group of the user who created the user which
  119. -- presence we are updating (this is the poltergeist case, where a user creates
  120. -- a poltergeist), optional.
  121. function update_presence_identity(
  122. stanza, user, group, creator_user, creator_group)
  123. -- First remove any 'identity' element if it already
  124. -- exists, so it cannot be spoofed by a client
  125. stanza:maptags(
  126. function(tag)
  127. for k, v in pairs(tag) do
  128. if k == "name" and v == "identity" then
  129. return nil
  130. end
  131. end
  132. return tag
  133. end
  134. )
  135. module:log("debug",
  136. "Presence after previous identity stripped: %s", tostring(stanza));
  137. stanza:tag("identity"):tag("user");
  138. for k, v in pairs(user) do
  139. stanza:tag(k):text(v):up();
  140. end
  141. stanza:up();
  142. -- Add the group information if it is present
  143. if group then
  144. stanza:tag("group"):text(group):up();
  145. end
  146. -- Add the creator user information if it is present
  147. if creator_user then
  148. stanza:tag("creator_user");
  149. for k, v in pairs(creator_user) do
  150. stanza:tag(k):text(v):up();
  151. end
  152. stanza:up();
  153. -- Add the creator group information if it is present
  154. if creator_group then
  155. stanza:tag("creator_group"):text(creator_group):up();
  156. end
  157. stanza:up();
  158. end
  159. module:log("debug",
  160. "Presence with identity inserted %s", tostring(stanza))
  161. end
  162. -- Utility function to check whether feature is present and enabled. Allow
  163. -- a feature if there are features present in the session(coming from
  164. -- the token) and the value of the feature is true.
  165. -- If features is not present in the token we skip feature detection and allow
  166. -- everything.
  167. function is_feature_allowed(session, feature)
  168. if (session.jitsi_meet_context_features == nil
  169. or session.jitsi_meet_context_features[feature] == "true") then
  170. return true;
  171. else
  172. return false;
  173. end
  174. end
  175. return {
  176. is_feature_allowed = is_feature_allowed;
  177. get_room_from_jid = get_room_from_jid;
  178. wrap_async_run = wrap_async_run;
  179. async_handler_wrapper = async_handler_wrapper;
  180. room_jid_match_rewrite = room_jid_match_rewrite;
  181. update_presence_identity = update_presence_identity;
  182. };