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_poltergeist.lua 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. local bare = require "util.jid".bare;
  2. local get_room_by_name_and_subdomain = module:require "util".get_room_by_name_and_subdomain;
  3. local jid = require "util.jid";
  4. local neturl = require "net.url";
  5. local parse = neturl.parseQuery;
  6. local poltergeist = module:require "poltergeist";
  7. local have_async = pcall(require, "util.async");
  8. if not have_async then
  9. module:log("error", "requires a version of Prosody with util.async");
  10. return;
  11. end
  12. local async_handler_wrapper = module:require "util".async_handler_wrapper;
  13. -- Options
  14. local poltergeist_component
  15. = module:get_option_string("poltergeist_component", module.host);
  16. -- this basically strips the domain from the conference.domain address
  17. local parentHostName = string.gmatch(tostring(module.host), "%w+.(%w.+)")();
  18. if parentHostName == nil then
  19. log("error", "Failed to start - unable to get parent hostname");
  20. return;
  21. end
  22. local parentCtx = module:context(parentHostName);
  23. if parentCtx == nil then
  24. log("error",
  25. "Failed to start - unable to get parent context for host: %s",
  26. tostring(parentHostName));
  27. return;
  28. end
  29. local token_util = module:require "token/util".new(parentCtx);
  30. -- option to enable/disable token verifications
  31. local disableTokenVerification
  32. = module:get_option_boolean("disable_polergeist_token_verification", false);
  33. -- poltergaist management functions
  34. --- Verifies room name, domain name with the values in the token
  35. -- @param token the token we received
  36. -- @param room_name the room name
  37. -- @param group name of the group (optional)
  38. -- @param session the session to use for storing token specific fields
  39. -- @return true if values are ok or false otherwise
  40. function verify_token(token, room_name, group, session)
  41. if disableTokenVerification then
  42. return true;
  43. end
  44. -- if not disableTokenVerification and we do not have token
  45. -- stop here, cause the main virtual host can have guest access enabled
  46. -- (allowEmptyToken = true) and we will allow access to rooms info without
  47. -- a token
  48. if token == nil then
  49. log("warn", "no token provided");
  50. return false;
  51. end
  52. session.auth_token = token;
  53. local verified, reason = token_util:process_and_verify_token(session);
  54. if not verified then
  55. log("warn", "not a valid token %s", tostring(reason));
  56. return false;
  57. end
  58. local room_address = jid.join(room_name, module:get_host());
  59. -- if there is a group we are in multidomain mode and that group is not
  60. -- our parent host
  61. if group and group ~= "" and group ~= parentHostName then
  62. room_address = "["..group.."]"..room_address;
  63. end
  64. if not token_util:verify_room(session, room_address) then
  65. log("warn", "Token %s not allowed to join: %s",
  66. tostring(token), tostring(room_address));
  67. return false;
  68. end
  69. return true;
  70. end
  71. -- Event handlers
  72. -- if we found that a session for a user with id has a poltergiest already
  73. -- created, retrieve its jid and return it to the authentication
  74. -- so we can reuse it and we that real user will replace the poltergiest
  75. prosody.events.add_handler("pre-jitsi-authentication", function(session)
  76. if (session.jitsi_meet_context_user) then
  77. local room = get_room_by_name_and_subdomain(
  78. session.jitsi_web_query_room,
  79. session.jitsi_web_query_prefix);
  80. if (not room) then
  81. return nil;
  82. end
  83. local username = poltergeist.get_username(
  84. room,
  85. session.jitsi_meet_context_user["id"]
  86. );
  87. if (not username) then
  88. return nil;
  89. end
  90. log("debug", "Found predefined username %s", username);
  91. -- let's find the room and if the poltergeist occupant is there
  92. -- lets remove him before the real participant joins
  93. -- when we see the unavailable presence to go out the server
  94. -- we will mark it with ignore tag
  95. local nick = poltergeist.create_nick(username);
  96. if (poltergeist.occupies(room, nick)) then
  97. module:log("info", "swapping poltergeist for user: %s/%s", room, nick)
  98. -- notify that user connected using the poltergeist
  99. poltergeist.update(room, nick, "connected");
  100. poltergeist.remove(room, nick, true);
  101. end
  102. return username;
  103. end
  104. return nil;
  105. end);
  106. --- Note: mod_muc and some of its sub-modules add event handlers between 0 and -100,
  107. --- e.g. to check for banned users, etc.. Hence adding these handlers at priority -100.
  108. module:hook("muc-decline", function (event)
  109. poltergeist.remove(event.room, bare(event.stanza.attr.from), false);
  110. end, -100);
  111. -- before sending the presence for a poltergeist leaving add ignore tag
  112. -- as poltergeist is leaving just before the real user joins and in the client
  113. -- we ignore this presence to avoid leaving/joining experience and the real
  114. -- user will reuse all currently created UI components for the same nick
  115. module:hook("muc-broadcast-presence", function (event)
  116. if (bare(event.occupant.jid) == poltergeist_component) then
  117. if(event.stanza.attr.type == "unavailable"
  118. and poltergeist.should_ignore(event.occupant.nick)) then
  119. event.stanza:tag(
  120. "ignore", { xmlns = "http://jitsi.org/jitmeet/" }):up();
  121. poltergeist.reset_ignored(event.occupant.nick);
  122. end
  123. end
  124. end, -100);
  125. -- cleanup room table after room is destroyed
  126. module:hook(
  127. "muc-room-destroyed",
  128. function(event)
  129. poltergeist.remove_room(event.room);
  130. end
  131. );
  132. --- Handles request for creating/managing poltergeists
  133. -- @param event the http event, holds the request query
  134. -- @return GET response, containing a json with response details
  135. function handle_create_poltergeist (event)
  136. if (not event.request.url.query) then
  137. return { status_code = 400; };
  138. end
  139. local params = parse(event.request.url.query);
  140. local user_id = params["user"];
  141. local room_name = params["room"];
  142. local group = params["group"];
  143. local name = params["name"];
  144. local avatar = params["avatar"];
  145. local status = params["status"];
  146. local conversation = params["conversation"];
  147. local session = {};
  148. if not verify_token(params["token"], room_name, group, session) then
  149. return { status_code = 403; };
  150. end
  151. -- If the provided room conference doesn't exist then we
  152. -- can't add a poltergeist to it.
  153. local room = get_room_by_name_and_subdomain(room_name, group);
  154. if (not room) then
  155. log("error", "no room found %s", room_name);
  156. return { status_code = 404; };
  157. end
  158. -- If the poltergiest is already in the conference then it will
  159. -- be in our username store and another can't be added.
  160. local username = poltergeist.get_username(room, user_id);
  161. if (username ~=nil and
  162. poltergeist.occupies(room, poltergeist.create_nick(username))) then
  163. log("warn",
  164. "poltergeist for username:%s already in the room:%s",
  165. username,
  166. room_name
  167. );
  168. return { status_code = 202; };
  169. end
  170. local context = {
  171. user = {
  172. id = user_id;
  173. };
  174. group = group;
  175. creator_user = session.jitsi_meet_context_user;
  176. creator_group = session.jitsi_meet_context_group;
  177. };
  178. if avatar ~= nil then
  179. context.user.avatar = avatar
  180. end
  181. local resources = {};
  182. if conversation ~= nil then
  183. resources["conversation"] = conversation
  184. end
  185. poltergeist.add_to_muc(room, user_id, name, avatar, context, status, resources)
  186. return { status_code = 200; };
  187. end
  188. --- Handles request for updating poltergeists status
  189. -- @param event the http event, holds the request query
  190. -- @return GET response, containing a json with response details
  191. function handle_update_poltergeist (event)
  192. if (not event.request.url.query) then
  193. return { status_code = 400; };
  194. end
  195. local params = parse(event.request.url.query);
  196. local user_id = params["user"];
  197. local room_name = params["room"];
  198. local group = params["group"];
  199. local status = params["status"];
  200. local call_id = params["callid"];
  201. local call_cancel = false
  202. if params["callcancel"] == "true" then
  203. call_cancel = true;
  204. end
  205. if not verify_token(params["token"], room_name, group, {}) then
  206. return { status_code = 403; };
  207. end
  208. local room = get_room_by_name_and_subdomain(room_name, group);
  209. if (not room) then
  210. log("error", "no room found %s", room_name);
  211. return { status_code = 404; };
  212. end
  213. local username = poltergeist.get_username(room, user_id);
  214. if (not username) then
  215. return { status_code = 404; };
  216. end
  217. local call_details = {
  218. ["cancel"] = call_cancel;
  219. ["id"] = call_id;
  220. };
  221. local nick = poltergeist.create_nick(username);
  222. if (not poltergeist.occupies(room, nick)) then
  223. return { status_code = 404; };
  224. end
  225. poltergeist.update(room, nick, status, call_details);
  226. return { status_code = 200; };
  227. end
  228. --- Handles remove poltergeists
  229. -- @param event the http event, holds the request query
  230. -- @return GET response, containing a json with response details
  231. function handle_remove_poltergeist (event)
  232. if (not event.request.url.query) then
  233. return { status_code = 400; };
  234. end
  235. local params = parse(event.request.url.query);
  236. local user_id = params["user"];
  237. local room_name = params["room"];
  238. local group = params["group"];
  239. if not verify_token(params["token"], room_name, group, {}) then
  240. return { status_code = 403; };
  241. end
  242. local room = get_room_by_name_and_subdomain(room_name, group);
  243. if (not room) then
  244. log("error", "no room found %s", room_name);
  245. return { status_code = 404; };
  246. end
  247. local username = poltergeist.get_username(room, user_id);
  248. if (not username) then
  249. return { status_code = 404; };
  250. end
  251. local nick = poltergeist.create_nick(username);
  252. if (not poltergeist.occupies(room, nick)) then
  253. return { status_code = 404; };
  254. end
  255. poltergeist.remove(room, nick, false);
  256. return { status_code = 200; };
  257. end
  258. log("info", "Loading poltergeist service");
  259. module:depends("http");
  260. module:provides("http", {
  261. default_path = "/";
  262. name = "poltergeist";
  263. route = {
  264. ["GET /poltergeist/create"] = function (event) return async_handler_wrapper(event,handle_create_poltergeist) end;
  265. ["GET /poltergeist/update"] = function (event) return async_handler_wrapper(event,handle_update_poltergeist) end;
  266. ["GET /poltergeist/remove"] = function (event) return async_handler_wrapper(event,handle_remove_poltergeist) end;
  267. };
  268. });