Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

mod_muc_poltergeist.lua 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. --- 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. -- @param session the session to use for storing token specific fields
  102. -- @return true if values are ok or false otherwise
  103. function verify_token(token, room_name, group, session)
  104. if disableTokenVerification then
  105. return true;
  106. end
  107. -- if not disableTokenVerification and we do not have token
  108. -- stop here, cause the main virtual host can have guest access enabled
  109. -- (allowEmptyToken = true) and we will allow access to rooms info without
  110. -- a token
  111. if token == nil then
  112. log("warn", "no token provided");
  113. return false;
  114. end
  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. -- @param context the information that we will store for this poltergeist
  173. function create_poltergeist_occupant(room, nick, name, avatar, status, context)
  174. log("debug", "create_poltergeist_occupant %s", nick);
  175. -- Join poltergeist occupant to room, with the invited JID as their nick
  176. local join_presence = st.presence({
  177. to = room.jid.."/"..nick,
  178. from = poltergeist_component.."/"..nick
  179. }):tag("x", { xmlns = MUC_NS }):up();
  180. if (name) then
  181. join_presence:tag(
  182. "nick",
  183. { xmlns = "http://jabber.org/protocol/nick" }):text(name):up();
  184. end
  185. if (avatar) then
  186. join_presence:tag("avatar-url"):text(avatar):up();
  187. end
  188. if (status) then
  189. join_presence:tag("status"):text(status):up();
  190. end
  191. -- If the room has a password set, let the poltergeist enter using it
  192. local room_password = room:get_password();
  193. if room_password then
  194. local join = join_presence:get_child("x", MUC_NS);
  195. join:tag("password", { xmlns = MUC_NS }):text(room_password);
  196. end
  197. update_presence_identity(
  198. join_presence,
  199. context.user,
  200. context.group,
  201. context.creator_user,
  202. context.creator_group
  203. );
  204. room:handle_first_presence(
  205. prosody.hosts[poltergeist_component], join_presence);
  206. -- the timeout before removing so participants can see the status update
  207. local removeTimeout = 5;
  208. local timeout = poltergeist_timeout - removeTimeout;
  209. timer.add_task(timeout,
  210. function ()
  211. update_poltergeist_occupant_status(
  212. room, nick, expired_status);
  213. -- and remove it after some time so participant can see
  214. -- the update
  215. timer.add_task(removeTimeout,
  216. function ()
  217. if (have_poltergeist_occupant(room, nick)) then
  218. remove_poltergeist_occupant(room, nick, false);
  219. end
  220. end);
  221. end);
  222. end
  223. -- Removes poltergeist occupant
  224. -- @param room the room instance where to remove the occupant
  225. -- @param nick the nick of the occupant to remove
  226. -- @param ignore to mark the poltergeist unavailble presence to be ignored
  227. function remove_poltergeist_occupant(room, nick, ignore)
  228. log("debug", "remove_poltergeist_occupant %s", nick);
  229. local leave_presence = st.presence({
  230. to = room.jid.."/"..nick,
  231. from = poltergeist_component.."/"..nick,
  232. type = "unavailable" });
  233. if (ignore) then
  234. poltergeists_pr_ignore[room.jid.."/"..nick] = true;
  235. end
  236. room:handle_normal_presence(
  237. prosody.hosts[poltergeist_component], leave_presence);
  238. remove_username(room, nick);
  239. end
  240. -- Updates poltergeist occupant status
  241. -- @param room the room instance where to remove the occupant
  242. -- @param nick the nick of the occupant to remove
  243. -- @param status the status to update
  244. function update_poltergeist_occupant_status(room, nick, status)
  245. local update_presence = get_presence(room, nick);
  246. if (not update_presence) then
  247. -- no presence found for occupant, create one
  248. update_presence = st.presence({
  249. to = room.jid.."/"..nick,
  250. from = poltergeist_component.."/"..nick
  251. });
  252. else
  253. -- update occupant presence with appropriate to and from
  254. -- so we can send it again
  255. update_presence = st.clone(update_presence);
  256. update_presence.attr.to = room.jid.."/"..nick;
  257. update_presence.attr.from = poltergeist_component.."/"..nick;
  258. end
  259. local once = false;
  260. -- the status tag we will attach
  261. local statusTag = st.stanza("status"):text(status);
  262. -- if there is already a status tag replace it
  263. update_presence:maptags(function (tag)
  264. if tag.name == statusTag.name then
  265. if not once then
  266. once = true;
  267. return statusTag;
  268. else
  269. return nil;
  270. end
  271. end
  272. return tag;
  273. end);
  274. if (not once) then
  275. -- no status tag was repleced, attach it
  276. update_presence:add_child(statusTag);
  277. end
  278. room:handle_normal_presence(
  279. prosody.hosts[poltergeist_component], update_presence);
  280. end
  281. -- Checks for existance of a poltergeist occupant
  282. -- @param room the room instance where to check for occupant
  283. -- @param nick the nick of the occupant
  284. -- @return true if occupant is found, false otherwise
  285. function have_poltergeist_occupant(room, nick)
  286. -- Find out if we have a poltergeist occupant in the room for this JID
  287. return not not room:get_occupant_jid(poltergeist_component.."/"..nick);
  288. end
  289. -- Returns the last presence of occupant
  290. -- @param room the room instance where to check for occupant
  291. -- @param nick the nick of the occupant
  292. -- @return presence of the occupant
  293. function get_presence(room, nick)
  294. local occupant_jid
  295. = room:get_occupant_jid(poltergeist_component.."/"..nick);
  296. if (occupant_jid) then
  297. return room:get_occupant_by_nick(occupant_jid):get_presence();
  298. end
  299. return nil;
  300. end
  301. -- Event handlers
  302. --- Note: mod_muc and some of its sub-modules add event handlers between 0 and -100,
  303. --- e.g. to check for banned users, etc.. Hence adding these handlers at priority -100.
  304. module:hook("muc-decline", function (event)
  305. remove_poltergeist_occupant(event.room, bare(event.stanza.attr.from), false);
  306. end, -100);
  307. -- before sending the presence for a poltergeist leaving add ignore tag
  308. -- as poltergeist is leaving just before the real user joins and in the client
  309. -- we ignore this presence to avoid leaving/joining experience and the real
  310. -- user will reuse all currently created UI components for the same nick
  311. module:hook("muc-broadcast-presence", function (event)
  312. if (bare(event.occupant.jid) == poltergeist_component) then
  313. if(event.stanza.attr.type == "unavailable"
  314. and poltergeists_pr_ignore[event.occupant.nick]) then
  315. event.stanza:tag(
  316. "ignore", { xmlns = "http://jitsi.org/jitmeet/" }):up();
  317. poltergeists_pr_ignore[event.occupant.nick] = nil;
  318. end
  319. end
  320. end, -100);
  321. -- cleanup room table after room is destroyed
  322. module:hook("muc-room-destroyed",function(event)
  323. local room_name = jid.node(event.room.jid);
  324. if (poltergeists[room_name]) then
  325. poltergeists[room_name] = nil;
  326. end
  327. end);
  328. --- Handles request for creating/managing poltergeists
  329. -- @param event the http event, holds the request query
  330. -- @return GET response, containing a json with response details
  331. function handle_create_poltergeist (event)
  332. if (not event.request.url.query) then
  333. return 400;
  334. end
  335. local params = parse(event.request.url.query);
  336. local user_id = params["user"];
  337. local room_name = params["room"];
  338. local group = params["group"];
  339. local name = params["name"];
  340. local avatar = params["avatar"];
  341. local status = params["status"];
  342. local session = {};
  343. if not verify_token(params["token"], room_name, group, session) then
  344. return 403;
  345. end
  346. local room = get_room(room_name, group);
  347. if (not room) then
  348. log("error", "no room found %s", room_name);
  349. return 404;
  350. end
  351. local username = get_username(room, user_id);
  352. if (username ~= nil
  353. and have_poltergeist_occupant(room, string.sub(username, 0, 8))) then
  354. log("warn", "poltergeist for username:%s already in the room:%s",
  355. username, room_name);
  356. return 202;
  357. else
  358. username = generate_uuid();
  359. store_username(room, user_id, username);
  360. local context = {
  361. user = {
  362. id = user_id;
  363. };
  364. group = group;
  365. creator_user = session.jitsi_meet_context_user;
  366. creator_group = session.jitsi_meet_context_group;
  367. };
  368. create_poltergeist_occupant(
  369. room, string.sub(username, 0, 8), name, avatar, status, context);
  370. return 200;
  371. end
  372. end
  373. --- Handles request for updating poltergeists status
  374. -- @param event the http event, holds the request query
  375. -- @return GET response, containing a json with response details
  376. function handle_update_poltergeist (event)
  377. if (not event.request.url.query) then
  378. return 400;
  379. end
  380. local params = parse(event.request.url.query);
  381. local user_id = params["user"];
  382. local room_name = params["room"];
  383. local group = params["group"];
  384. local status = params["status"];
  385. if not verify_token(params["token"], room_name, group, {}) then
  386. return 403;
  387. end
  388. local room = get_room(room_name, group);
  389. if (not room) then
  390. log("error", "no room found %s", room_name);
  391. return 404;
  392. end
  393. local username = get_username(room, user_id);
  394. if (not username) then
  395. return 404;
  396. end
  397. local nick = string.sub(username, 0, 8);
  398. if (have_poltergeist_occupant(room, nick)) then
  399. update_poltergeist_occupant_status(room, nick, status);
  400. return 200;
  401. else
  402. return 404;
  403. end
  404. end
  405. --- Handles remove poltergeists
  406. -- @param event the http event, holds the request query
  407. -- @return GET response, containing a json with response details
  408. function handle_remove_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. if not verify_token(params["token"], room_name, group, {}) then
  417. return 403;
  418. end
  419. local room = get_room(room_name, group);
  420. if (not room) then
  421. log("error", "no room found %s", room_name);
  422. return 404;
  423. end
  424. local username = get_username(room, user_id);
  425. if (not username) then
  426. return 404;
  427. end
  428. local nick = string.sub(username, 0, 8);
  429. if (have_poltergeist_occupant(room, nick)) then
  430. remove_poltergeist_occupant(room, nick, false);
  431. return 200;
  432. else
  433. return 404;
  434. end
  435. end
  436. log("info", "Loading poltergeist service");
  437. module:depends("http");
  438. module:provides("http", {
  439. default_path = "/";
  440. name = "poltergeist";
  441. route = {
  442. ["GET /poltergeist/create"] = function (event) return wrap_async_run(event,handle_create_poltergeist) end;
  443. ["GET /poltergeist/update"] = function (event) return wrap_async_run(event,handle_update_poltergeist) end;
  444. ["GET /poltergeist/remove"] = function (event) return wrap_async_run(event,handle_remove_poltergeist) end;
  445. };
  446. });