您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

util.lib.lua 7.9KB

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