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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. local bare = require "util.jid".bare;
  2. local generate_uuid = require "util.uuid".generate;
  3. local jid = require "util.jid";
  4. local neturl = require "net.url";
  5. local parse = neturl.parseQuery;
  6. local st = require "util.stanza";
  7. local get_room_from_jid = module:require "util".get_room_from_jid;
  8. local wrap_async_run = module:require "util".wrap_async_run;
  9. local update_presence_identity = module:require "util".update_presence_identity;
  10. local timer = require "util.timer";
  11. local MUC_NS = "http://jabber.org/protocol/muc";
  12. local expired_status = "expired";
  13. -- Options
  14. local poltergeist_component
  15. = module:get_option_string("poltergeist_component", module.host);
  16. -- defaults to 3 min
  17. local poltergeist_timeout
  18. = module:get_option_string("poltergeist_leave_timeout", 180);
  19. -- this basically strips the domain from the conference.domain address
  20. local parentHostName = string.gmatch(tostring(module.host), "%w+.(%w.+)")();
  21. if parentHostName == nil then
  22. log("error", "Failed to start - unable to get parent hostname");
  23. return;
  24. end
  25. local parentCtx = module:context(parentHostName);
  26. if parentCtx == nil then
  27. log("error",
  28. "Failed to start - unable to get parent context for host: %s",
  29. tostring(parentHostName));
  30. return;
  31. end
  32. local token_util = module:require "token/util".new(parentCtx);
  33. -- option to enable/disable token verifications
  34. local disableTokenVerification
  35. = module:get_option_boolean("disable_polergeist_token_verification", false);
  36. -- table to store all poltergeists we create
  37. local poltergeists = {};
  38. -- table to mark that outgoing unavailable presences
  39. -- should be marked with ignore
  40. local poltergeists_pr_ignore = {};
  41. -- poltergaist management functions
  42. -- Returns the room if available, work and in multidomain mode
  43. -- @param room_name the name of the room
  44. -- @param group name of the group (optional)
  45. -- @return returns room if found or nil
  46. function get_room(room_name, group)
  47. local room_address = jid.join(room_name, module:get_host());
  48. -- if there is a group we are in multidomain mode and that group is not
  49. -- our parent host
  50. if group and group ~= "" and group ~= parentHostName then
  51. room_address = "["..group.."]"..room_address;
  52. end
  53. return get_room_from_jid(room_address);
  54. end
  55. -- Stores the username in the table where we store poltergeist usernames
  56. -- based on their room names
  57. -- @param room the room instance
  58. -- @param user_id the user id
  59. -- @param username the username to store
  60. function store_username(room, user_id, username)
  61. local room_name = jid.node(room.jid);
  62. -- we store in poltergeist user ids for room names
  63. if (not poltergeists[room_name]) then
  64. poltergeists[room_name] = {};
  65. end
  66. poltergeists[room_name][user_id] = username;
  67. log("debug", "stored in session: %s", username);
  68. end
  69. -- Retrieve the username for a user
  70. -- @param room the room instance
  71. -- @param user_id the user id
  72. -- @return returns the stored username for user or nil
  73. function get_username(room, user_id)
  74. local room_name = jid.node(room.jid);
  75. if (not poltergeists[room_name]) then
  76. return nil;
  77. end
  78. return poltergeists[room_name][user_id];
  79. end
  80. -- Removes poltergeist values from table
  81. -- @param room the room instance
  82. -- @param nick the user nick
  83. function remove_username(room, nick)
  84. local room_name = jid.node(room.jid);
  85. if (poltergeists[room_name]) then
  86. local user_id_to_remove;
  87. for name,username in pairs(poltergeists[room_name]) do
  88. if (string.sub(username, 0, 8) == nick) then
  89. user_id_to_remove = name;
  90. end
  91. end
  92. if (user_id_to_remove) then
  93. poltergeists[room_name][user_id_to_remove] = nil;
  94. end
  95. end
  96. end
  97. -- Provides a new presence stanza for a poltergeist.
  98. -- @param room the room instance
  99. -- @param nick the user nick
  100. function generate_poltergeist_presence(room, nick, status)
  101. local presence_stanza = st.presence({
  102. to = room.jid.."/"..nick,
  103. from = poltergeist_component.."/"..nick,
  104. }):tag("x", { xmlns = MUC_NS }):up();
  105. presence_stanza:tag("call_cancel"):text(nil):up();
  106. presence_stanza:tag("call_id"):text(nil):up();
  107. if status then
  108. presence_stanza:tag("status"):text(status):up();
  109. else
  110. presence_stanza:tag("status"):text(nil):up();
  111. end
  112. return presence_stanza;
  113. end
  114. --- Verifies room name, domain name with the values in the token
  115. -- @param token the token we received
  116. -- @param room_name the room name
  117. -- @param group name of the group (optional)
  118. -- @param session the session to use for storing token specific fields
  119. -- @return true if values are ok or false otherwise
  120. function verify_token(token, room_name, group, session)
  121. if disableTokenVerification then
  122. return true;
  123. end
  124. -- if not disableTokenVerification and we do not have token
  125. -- stop here, cause the main virtual host can have guest access enabled
  126. -- (allowEmptyToken = true) and we will allow access to rooms info without
  127. -- a token
  128. if token == nil then
  129. log("warn", "no token provided");
  130. return false;
  131. end
  132. session.auth_token = token;
  133. local verified, reason = token_util:process_and_verify_token(session);
  134. if not verified then
  135. log("warn", "not a valid token %s", tostring(reason));
  136. return false;
  137. end
  138. local room_address = jid.join(room_name, module:get_host());
  139. -- if there is a group we are in multidomain mode and that group is not
  140. -- our parent host
  141. if group and group ~= "" and group ~= parentHostName then
  142. room_address = "["..group.."]"..room_address;
  143. end
  144. if not token_util:verify_room(session, room_address) then
  145. log("warn", "Token %s not allowed to join: %s",
  146. tostring(token), tostring(room_address));
  147. return false;
  148. end
  149. return true;
  150. end
  151. -- if we found that a session for a user with id has a poltergiest already
  152. -- created, retrieve its jid and return it to the authentication
  153. -- so we can reuse it and we that real user will replace the poltergiest
  154. prosody.events.add_handler("pre-jitsi-authentication", function(session)
  155. if (session.jitsi_meet_context_user) then
  156. local room = get_room(
  157. session.jitsi_bosh_query_room,
  158. session.jitsi_meet_domain);
  159. if (not room) then
  160. return nil;
  161. end
  162. local username
  163. = get_username(room, session.jitsi_meet_context_user["id"]);
  164. if (not username) then
  165. return nil;
  166. end
  167. log("debug", "Found predefined username %s", username);
  168. -- let's find the room and if the poltergeist occupant is there
  169. -- lets remove him before the real participant joins
  170. -- when we see the unavailable presence to go out the server
  171. -- we will mark it with ignore tag
  172. local nick = string.sub(username, 0, 8);
  173. if (have_poltergeist_occupant(room, nick)) then
  174. -- notify that user connected using the poltergeist
  175. update_poltergeist_occupant_status(
  176. room, nick, "connected");
  177. remove_poltergeist_occupant(room, nick, true);
  178. end
  179. return username;
  180. end
  181. return nil;
  182. end);
  183. -- Creates poltergeist occupant
  184. -- @param room the room instance where we create the occupant
  185. -- @param nick the nick to use for the new occupant
  186. -- @param name the display name fot the occupant (optional)
  187. -- @param avatar the avatar to use for the new occupant (optional)
  188. -- @param status the initial status to use for the new occupant (optional)
  189. -- @param context the information that we will store for this poltergeist
  190. function create_poltergeist_occupant(room, nick, name, avatar, status, context)
  191. log("debug", "create_poltergeist_occupant %s", nick);
  192. -- Join poltergeist occupant to room, with the invited JID as their nick
  193. local join_presence = generate_poltergeist_presence(room, nick, status)
  194. if (name) then
  195. join_presence:tag(
  196. "nick",
  197. { xmlns = "http://jabber.org/protocol/nick" }):text(name):up();
  198. end
  199. if (avatar) then
  200. join_presence:tag("avatar-url"):text(avatar):up();
  201. end
  202. -- If the room has a password set, let the poltergeist enter using it
  203. local room_password = room:get_password();
  204. if room_password then
  205. local join = join_presence:get_child("x", MUC_NS);
  206. join:tag("password", { xmlns = MUC_NS }):text(room_password);
  207. end
  208. local call_id = get_username(room, context.user.id);
  209. join_presence:tag("call_id"):text(get_username(room, context.user.id)):up();
  210. update_presence_identity(
  211. join_presence,
  212. context.user,
  213. context.group,
  214. context.creator_user,
  215. context.creator_group
  216. );
  217. room:handle_first_presence(
  218. prosody.hosts[poltergeist_component], join_presence);
  219. -- the timeout before removing so participants can see the status update
  220. local removeTimeout = 5;
  221. local timeout = poltergeist_timeout - removeTimeout;
  222. timer.add_task(timeout,
  223. function ()
  224. update_poltergeist_occupant_status(
  225. room, nick, expired_status);
  226. -- and remove it after some time so participant can see
  227. -- the update
  228. timer.add_task(removeTimeout,
  229. function ()
  230. if (have_poltergeist_occupant(room, nick)) then
  231. remove_poltergeist_occupant(room, nick, false);
  232. end
  233. end);
  234. end);
  235. end
  236. -- Removes poltergeist occupant
  237. -- @param room the room instance where to remove the occupant
  238. -- @param nick the nick of the occupant to remove
  239. -- @param ignore to mark the poltergeist unavailble presence to be ignored
  240. function remove_poltergeist_occupant(room, nick, ignore)
  241. log("debug", "remove_poltergeist_occupant %s", nick);
  242. local leave_presence = st.presence({
  243. to = room.jid.."/"..nick,
  244. from = poltergeist_component.."/"..nick,
  245. type = "unavailable" });
  246. if (ignore) then
  247. poltergeists_pr_ignore[room.jid.."/"..nick] = true;
  248. end
  249. room:handle_normal_presence(
  250. prosody.hosts[poltergeist_component], leave_presence);
  251. remove_username(room, nick);
  252. end
  253. -- Updates poltergeist occupant status
  254. -- @param room the room instance where to remove the occupant
  255. -- @param nick the nick of the occupant to remove
  256. -- @param status the status to update
  257. -- @param call_details is a table of call flow details
  258. function update_poltergeist_occupant_status(room, nick, status, call_details)
  259. local update_presence = get_presence(room, nick);
  260. if (not update_presence) then
  261. -- no presence found for occupant, create one
  262. update_presence = generate_poltergeist_presence(room, nick)
  263. else
  264. -- update occupant presence with appropriate to and from
  265. -- so we can send it again
  266. update_presence = st.clone(update_presence);
  267. update_presence.attr.to = room.jid.."/"..nick;
  268. update_presence.attr.from = poltergeist_component.."/"..nick;
  269. end
  270. update_presence = update_presence_tags(update_presence, status, call_details)
  271. room:handle_normal_presence(
  272. prosody.hosts[poltergeist_component], update_presence);
  273. end
  274. -- Updates the status tags and call flow tags of an existing poltergeist's
  275. -- presence.
  276. -- @param presence_stanza is the actual presence stanza for a poltergeist.
  277. -- @param status is the new status to be updated in the stanza.
  278. -- @param call_details is a table of call flow signal information.
  279. function update_presence_tags(presence_stanza, status, call_details)
  280. local call_cancel = false;
  281. local call_id = nil;
  282. -- Extract optional call flow signal information.
  283. if call_details then
  284. call_id = call_details["id"];
  285. if call_details["cancel"] then
  286. call_cancel = call_details["cancel"];
  287. end
  288. end
  289. presence_stanza:maptags(function (tag)
  290. if tag.name == "status" then
  291. if call_cancel then
  292. -- If call cancel is set then the status should not be changed.
  293. return tag
  294. end
  295. return st.stanza("status"):text(status);
  296. elseif tag.name == "call_id" then
  297. if call_id then
  298. return st.stanza("call_id"):text(call_id);
  299. else
  300. -- If no call id is provided the re-use the existing id.
  301. return tag;
  302. end
  303. elseif tag.name == "call_cancel" then
  304. if call_cancel then
  305. return st.stanza("call_cancel"):text("true");
  306. else
  307. return st.stanza("call_cancel"):text("false");
  308. end
  309. end
  310. end);
  311. return presence_stanza
  312. end
  313. -- Checks for existance of a poltergeist occupant
  314. -- @param room the room instance where to check for occupant
  315. -- @param nick the nick of the occupant
  316. -- @return true if occupant is found, false otherwise
  317. function have_poltergeist_occupant(room, nick)
  318. -- Find out if we have a poltergeist occupant in the room for this JID
  319. return not not room:get_occupant_jid(poltergeist_component.."/"..nick);
  320. end
  321. -- Returns the last presence of occupant
  322. -- @param room the room instance where to check for occupant
  323. -- @param nick the nick of the occupant
  324. -- @return presence of the occupant
  325. function get_presence(room, nick)
  326. local occupant_jid
  327. = room:get_occupant_jid(poltergeist_component.."/"..nick);
  328. if (occupant_jid) then
  329. return room:get_occupant_by_nick(occupant_jid):get_presence();
  330. end
  331. return nil;
  332. end
  333. -- Event handlers
  334. --- Note: mod_muc and some of its sub-modules add event handlers between 0 and -100,
  335. --- e.g. to check for banned users, etc.. Hence adding these handlers at priority -100.
  336. module:hook("muc-decline", function (event)
  337. remove_poltergeist_occupant(event.room, bare(event.stanza.attr.from), false);
  338. end, -100);
  339. -- before sending the presence for a poltergeist leaving add ignore tag
  340. -- as poltergeist is leaving just before the real user joins and in the client
  341. -- we ignore this presence to avoid leaving/joining experience and the real
  342. -- user will reuse all currently created UI components for the same nick
  343. module:hook("muc-broadcast-presence", function (event)
  344. if (bare(event.occupant.jid) == poltergeist_component) then
  345. if(event.stanza.attr.type == "unavailable"
  346. and poltergeists_pr_ignore[event.occupant.nick]) then
  347. event.stanza:tag(
  348. "ignore", { xmlns = "http://jitsi.org/jitmeet/" }):up();
  349. poltergeists_pr_ignore[event.occupant.nick] = nil;
  350. end
  351. end
  352. end, -100);
  353. -- cleanup room table after room is destroyed
  354. module:hook("muc-room-destroyed",function(event)
  355. local room_name = jid.node(event.room.jid);
  356. if (poltergeists[room_name]) then
  357. poltergeists[room_name] = nil;
  358. end
  359. end);
  360. --- Handles request for creating/managing poltergeists
  361. -- @param event the http event, holds the request query
  362. -- @return GET response, containing a json with response details
  363. function handle_create_poltergeist (event)
  364. if (not event.request.url.query) then
  365. return 400;
  366. end
  367. local params = parse(event.request.url.query);
  368. local user_id = params["user"];
  369. local room_name = params["room"];
  370. local group = params["group"];
  371. local name = params["name"];
  372. local avatar = params["avatar"];
  373. local status = params["status"];
  374. local session = {};
  375. if not verify_token(params["token"], room_name, group, session) then
  376. return 403;
  377. end
  378. local room = get_room(room_name, group);
  379. if (not room) then
  380. log("error", "no room found %s", room_name);
  381. return 404;
  382. end
  383. local username = get_username(room, user_id);
  384. if (username ~= nil
  385. and have_poltergeist_occupant(room, string.sub(username, 0, 8))) then
  386. log("warn", "poltergeist for username:%s already in the room:%s",
  387. username, room_name);
  388. return 202;
  389. else
  390. username = generate_uuid();
  391. store_username(room, user_id, username);
  392. local context = {
  393. user = {
  394. id = user_id;
  395. };
  396. group = group;
  397. creator_user = session.jitsi_meet_context_user;
  398. creator_group = session.jitsi_meet_context_group;
  399. };
  400. create_poltergeist_occupant(
  401. room, string.sub(username, 0, 8), name, avatar, status, context);
  402. return 200;
  403. end
  404. end
  405. --- Handles request for updating poltergeists status
  406. -- @param event the http event, holds the request query
  407. -- @return GET response, containing a json with response details
  408. function handle_update_poltergeist (event)
  409. if (not event.request.url.query) then
  410. return 400;
  411. end
  412. local params = parse(event.request.url.query);
  413. local user_id = params["user"];
  414. local room_name = params["room"];
  415. local group = params["group"];
  416. local status = params["status"];
  417. local call_id = params["callid"];
  418. local call_cancel = false
  419. if params["callcancel"] == "true" then
  420. call_cancel = true;
  421. end
  422. if not verify_token(params["token"], room_name, group, {}) then
  423. return 403;
  424. end
  425. local room = get_room(room_name, group);
  426. if (not room) then
  427. log("error", "no room found %s", room_name);
  428. return 404;
  429. end
  430. local username = get_username(room, user_id);
  431. if (not username) then
  432. return 404;
  433. end
  434. local call_details = {
  435. ["cancel"] = call_cancel;
  436. ["id"] = call_id;
  437. };
  438. local nick = string.sub(username, 0, 8);
  439. if (have_poltergeist_occupant(room, nick)) then
  440. update_poltergeist_occupant_status(room, nick, status, call_details);
  441. return 200;
  442. else
  443. return 404;
  444. end
  445. end
  446. --- Handles remove poltergeists
  447. -- @param event the http event, holds the request query
  448. -- @return GET response, containing a json with response details
  449. function handle_remove_poltergeist (event)
  450. if (not event.request.url.query) then
  451. return 400;
  452. end
  453. local params = parse(event.request.url.query);
  454. local user_id = params["user"];
  455. local room_name = params["room"];
  456. local group = params["group"];
  457. if not verify_token(params["token"], room_name, group, {}) then
  458. return 403;
  459. end
  460. local room = get_room(room_name, group);
  461. if (not room) then
  462. log("error", "no room found %s", room_name);
  463. return 404;
  464. end
  465. local username = get_username(room, user_id);
  466. if (not username) then
  467. return 404;
  468. end
  469. local nick = string.sub(username, 0, 8);
  470. if (have_poltergeist_occupant(room, nick)) then
  471. remove_poltergeist_occupant(room, nick, false);
  472. return 200;
  473. else
  474. return 404;
  475. end
  476. end
  477. log("info", "Loading poltergeist service");
  478. module:depends("http");
  479. module:provides("http", {
  480. default_path = "/";
  481. name = "poltergeist";
  482. route = {
  483. ["GET /poltergeist/create"] = function (event) return wrap_async_run(event,handle_create_poltergeist) end;
  484. ["GET /poltergeist/update"] = function (event) return wrap_async_run(event,handle_update_poltergeist) end;
  485. ["GET /poltergeist/remove"] = function (event) return wrap_async_run(event,handle_remove_poltergeist) end;
  486. };
  487. });