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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 timer = require "util.timer";
  10. -- Options
  11. local poltergeist_component
  12. = module:get_option_string("poltergeist_component", module.host);
  13. -- defaults to 3 min
  14. local poltergeist_timeout
  15. = module:get_option_string("poltergeist_leave_timeout", 180);
  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. -- option to expire poltergeist with custom status text
  34. local poltergeistExpiredStatus
  35. = module:get_option_string("poltergeist_expired_status");
  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. --- Verifies room name, domain name with the values in the token
  98. -- @param token the token we received
  99. -- @param room_name the room name
  100. -- @param group name of the group (optional)
  101. -- @return true if values are ok or false otherwise
  102. function verify_token(token, room_name, group)
  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. local session = {};
  115. session.auth_token = token;
  116. local verified, reason = token_util:process_and_verify_token(session);
  117. if not verified then
  118. log("warn", "not a valid token %s", tostring(reason));
  119. return false;
  120. end
  121. local room_address = jid.join(room_name, module:get_host());
  122. -- if there is a group we are in multidomain mode and that group is not
  123. -- our parent host
  124. if group and group ~= "" and group ~= parentHostName then
  125. room_address = "["..group.."]"..room_address;
  126. end
  127. if not token_util:verify_room(session, room_address) then
  128. log("warn", "Token %s not allowed to join: %s",
  129. tostring(token), tostring(room_address));
  130. return false;
  131. end
  132. return true;
  133. end
  134. -- if we found that a session for a user with id has a poltergiest already
  135. -- created, retrieve its jid and return it to the authentication
  136. -- so we can reuse it and we that real user will replace the poltergiest
  137. prosody.events.add_handler("pre-jitsi-authentication", function(session)
  138. if (session.jitsi_meet_context_user) then
  139. local room = get_room(
  140. session.jitsi_bosh_query_room,
  141. session.jitsi_meet_domain);
  142. if (not room) then
  143. return nil;
  144. end
  145. local username
  146. = get_username(room, session.jitsi_meet_context_user["id"]);
  147. if (not username) then
  148. return nil;
  149. end
  150. log("debug", "Found predefined username %s", username);
  151. -- let's find the room and if the poltergeist occupant is there
  152. -- lets remove him before the real participant joins
  153. -- when we see the unavailable presence to go out the server
  154. -- we will mark it with ignore tag
  155. local nick = string.sub(username, 0, 8);
  156. if (have_poltergeist_occupant(room, nick)) then
  157. -- notify that user connected using the poltergeist
  158. update_poltergeist_occupant_status(
  159. room, nick, "connected");
  160. remove_poltergeist_occupant(room, nick, true);
  161. end
  162. return username;
  163. end
  164. return nil;
  165. end);
  166. -- Creates poltergeist occupant
  167. -- @param room the room instance where we create the occupant
  168. -- @param nick the nick to use for the new occupant
  169. -- @param name the display name fot the occupant (optional)
  170. -- @param avatar the avatar to use for the new occupant (optional)
  171. -- @param status the initial status to use for the new occupant (optional)
  172. function create_poltergeist_occupant(room, nick, name, avatar, status)
  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 = "http://jabber.org/protocol/muc" }):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. room:handle_first_presence(
  191. prosody.hosts[poltergeist_component], join_presence);
  192. local timeout = poltergeist_timeout;
  193. -- the timeout before removing so participants can see the status update
  194. local removeTimeout = 5;
  195. if (poltergeistExpiredStatus) then
  196. timeout = timeout - removeTimeout;
  197. end
  198. timer.add_task(timeout,
  199. function ()
  200. if (poltergeistExpiredStatus) then
  201. update_poltergeist_occupant_status(
  202. room, nick, poltergeistExpiredStatus);
  203. -- and remove it after some time so participant can see
  204. -- the update
  205. timer.add_task(removeTimeout,
  206. function ()
  207. if (have_poltergeist_occupant(room, nick)) then
  208. remove_poltergeist_occupant(room, nick, false);
  209. end
  210. end);
  211. else
  212. if (have_poltergeist_occupant(room, nick)) then
  213. remove_poltergeist_occupant(room, nick, false);
  214. end
  215. end
  216. end);
  217. end
  218. -- Removes poltergeist occupant
  219. -- @param room the room instance where to remove the occupant
  220. -- @param nick the nick of the occupant to remove
  221. -- @param ignore to mark the poltergeist unavailble presence to be ignored
  222. function remove_poltergeist_occupant(room, nick, ignore)
  223. log("debug", "remove_poltergeist_occupant %s", nick);
  224. local leave_presence = st.presence({
  225. to = room.jid.."/"..nick,
  226. from = poltergeist_component.."/"..nick,
  227. type = "unavailable" });
  228. if (ignore) then
  229. poltergeists_pr_ignore[room.jid.."/"..nick] = true;
  230. end
  231. room:handle_normal_presence(
  232. prosody.hosts[poltergeist_component], leave_presence);
  233. remove_username(room, nick);
  234. end
  235. -- Updates poltergeist occupant status
  236. -- @param room the room instance where to remove the occupant
  237. -- @param nick the nick of the occupant to remove
  238. -- @param status the status to update
  239. function update_poltergeist_occupant_status(room, nick, status)
  240. local update_presence = get_presence(room, nick);
  241. if (not update_presence) then
  242. -- no presence found for occupant, create one
  243. update_presence = st.presence({
  244. to = room.jid.."/"..nick,
  245. from = poltergeist_component.."/"..nick
  246. });
  247. else
  248. -- update occupant presence with appropriate to and from
  249. -- so we can send it again
  250. update_presence = st.clone(update_presence);
  251. update_presence.attr.to = room.jid.."/"..nick;
  252. update_presence.attr.from = poltergeist_component.."/"..nick;
  253. end
  254. local once = false;
  255. -- the status tag we will attach
  256. local statusTag = st.stanza("status"):text(status);
  257. -- if there is already a status tag replace it
  258. update_presence:maptags(function (tag)
  259. if tag.name == statusTag.name then
  260. if not once then
  261. once = true;
  262. return statusTag;
  263. else
  264. return nil;
  265. end
  266. end
  267. return tag;
  268. end);
  269. if (not once) then
  270. -- no status tag was repleced, attach it
  271. update_presence:add_child(statusTag);
  272. end
  273. room:handle_normal_presence(
  274. prosody.hosts[poltergeist_component], update_presence);
  275. end
  276. -- Checks for existance of a poltergeist occupant
  277. -- @param room the room instance where to check for occupant
  278. -- @param nick the nick of the occupant
  279. -- @return true if occupant is found, false otherwise
  280. function have_poltergeist_occupant(room, nick)
  281. -- Find out if we have a poltergeist occupant in the room for this JID
  282. return not not room:get_occupant_jid(poltergeist_component.."/"..nick);
  283. end
  284. -- Returns the last presence of occupant
  285. -- @param room the room instance where to check for occupant
  286. -- @param nick the nick of the occupant
  287. -- @return presence of the occupant
  288. function get_presence(room, nick)
  289. local occupant_jid
  290. = room:get_occupant_jid(poltergeist_component.."/"..nick);
  291. if (occupant_jid) then
  292. return room:get_occupant_by_nick(occupant_jid):get_presence();
  293. end
  294. return nil;
  295. end
  296. -- Event handlers
  297. --- Note: mod_muc and some of its sub-modules add event handlers between 0 and -100,
  298. --- e.g. to check for banned users, etc.. Hence adding these handlers at priority -100.
  299. module:hook("muc-decline", function (event)
  300. remove_poltergeist_occupant(event.room, bare(event.stanza.attr.from), false);
  301. end, -100);
  302. -- before sending the presence for a poltergeist leaving add ignore tag
  303. -- as poltergeist is leaving just before the real user joins and in the client
  304. -- we ignore this presence to avoid leaving/joining experience and the real
  305. -- user will reuse all currently created UI components for the same nick
  306. module:hook("muc-broadcast-presence", function (event)
  307. if (bare(event.occupant.jid) == poltergeist_component) then
  308. if(event.stanza.attr.type == "unavailable"
  309. and poltergeists_pr_ignore[event.occupant.nick]) then
  310. event.stanza:tag(
  311. "ignore", { xmlns = "http://jitsi.org/jitmeet/" }):up();
  312. poltergeists_pr_ignore[event.occupant.nick] = nil;
  313. end
  314. end
  315. end, -100);
  316. -- cleanup room table after room is destroyed
  317. module:hook("muc-room-destroyed",function(event)
  318. local room_name = jid.node(event.room.jid);
  319. if (poltergeists[room_name]) then
  320. poltergeists[room_name] = nil;
  321. end
  322. end);
  323. --- Handles request for creating/managing poltergeists
  324. -- @param event the http event, holds the request query
  325. -- @return GET response, containing a json with response details
  326. function handle_create_poltergeist (event)
  327. if (not event.request.url.query) then
  328. return 400;
  329. end
  330. local params = parse(event.request.url.query);
  331. local user_id = params["user"];
  332. local room_name = params["room"];
  333. local group = params["group"];
  334. local name = params["name"];
  335. local avatar = params["avatar"];
  336. local status = params["status"];
  337. if not verify_token(params["token"], room_name, group) then
  338. return 403;
  339. end
  340. local room = get_room(room_name, group);
  341. if (not room) then
  342. log("error", "no room found %s", room_name);
  343. return 404;
  344. end
  345. local username = get_username(room, user_id);
  346. if (username ~= nil
  347. and have_poltergeist_occupant(room, string.sub(username, 0, 8))) then
  348. log("warn", "poltergeist for username:%s already in the room:%s",
  349. username, room_name);
  350. return 202;
  351. else
  352. username = generate_uuid();
  353. store_username(room, user_id, username);
  354. create_poltergeist_occupant(
  355. room, string.sub(username, 0, 8), name, avatar, status);
  356. return 200;
  357. end
  358. end
  359. --- Handles request for updating poltergeists status
  360. -- @param event the http event, holds the request query
  361. -- @return GET response, containing a json with response details
  362. function handle_update_poltergeist (event)
  363. if (not event.request.url.query) then
  364. return 400;
  365. end
  366. local params = parse(event.request.url.query);
  367. local user_id = params["user"];
  368. local room_name = params["room"];
  369. local group = params["group"];
  370. local status = params["status"];
  371. if not verify_token(params["token"], room_name, group) then
  372. return 403;
  373. end
  374. local room = get_room(room_name, group);
  375. if (not room) then
  376. log("error", "no room found %s", room_name);
  377. return 404;
  378. end
  379. local username = get_username(room, user_id);
  380. if (not username) then
  381. return 404;
  382. end
  383. local nick = string.sub(username, 0, 8);
  384. if (have_poltergeist_occupant(room, nick)) then
  385. update_poltergeist_occupant_status(room, nick, status);
  386. return 200;
  387. else
  388. return 404;
  389. end
  390. end
  391. --- Handles remove poltergeists
  392. -- @param event the http event, holds the request query
  393. -- @return GET response, containing a json with response details
  394. function handle_remove_poltergeist (event)
  395. if (not event.request.url.query) then
  396. return 400;
  397. end
  398. local params = parse(event.request.url.query);
  399. local user_id = params["user"];
  400. local room_name = params["room"];
  401. local group = params["group"];
  402. if not verify_token(params["token"], room_name, group) then
  403. return 403;
  404. end
  405. local room = get_room(room_name, group);
  406. if (not room) then
  407. log("error", "no room found %s", room_name);
  408. return 404;
  409. end
  410. local username = get_username(room, user_id);
  411. if (not username) then
  412. return 404;
  413. end
  414. local nick = string.sub(username, 0, 8);
  415. if (have_poltergeist_occupant(room, nick)) then
  416. remove_poltergeist_occupant(room, nick, false);
  417. return 200;
  418. else
  419. return 404;
  420. end
  421. end
  422. log("info", "Loading poltergeist service");
  423. module:depends("http");
  424. module:provides("http", {
  425. default_path = "/";
  426. name = "poltergeist";
  427. route = {
  428. ["GET /poltergeist/create"] = function (event) return wrap_async_run(event,handle_create_poltergeist) end;
  429. ["GET /poltergeist/update"] = function (event) return wrap_async_run(event,handle_update_poltergeist) end;
  430. ["GET /poltergeist/remove"] = function (event) return wrap_async_run(event,handle_remove_poltergeist) end;
  431. };
  432. });