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

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