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_lobby_rooms.lua 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. -- This module added under the main virtual host domain
  2. -- It needs a lobby muc component
  3. --
  4. -- VirtualHost "jitmeet.example.com"
  5. -- modules_enabled = {
  6. -- "muc_lobby_rooms"
  7. -- }
  8. -- lobby_muc = "lobby.jitmeet.example.com"
  9. -- main_muc = "conference.jitmeet.example.com"
  10. --
  11. -- Component "lobby.jitmeet.example.com" "muc"
  12. -- storage = "memory"
  13. -- muc_room_cache_size = 1000
  14. -- restrict_room_creation = true
  15. -- muc_room_locking = false
  16. -- muc_room_default_public_jids = true
  17. --
  18. -- we use async to detect Prosody 0.10 and earlier
  19. local have_async = pcall(require, 'util.async');
  20. if not have_async then
  21. module:log('warn', 'Lobby rooms will not work with Prosody version 0.10 or less.');
  22. return;
  23. end
  24. module:depends("jitsi_session");
  25. local jid_split = require 'util.jid'.split;
  26. local jid_bare = require 'util.jid'.bare;
  27. local json = require 'util.json';
  28. local filters = require 'util.filters';
  29. local st = require 'util.stanza';
  30. local MUC_NS = 'http://jabber.org/protocol/muc';
  31. local DISCO_INFO_NS = 'http://jabber.org/protocol/disco#info';
  32. local DISPLAY_NAME_REQUIRED_FEATURE = 'http://jitsi.org/protocol/lobbyrooms#displayname_required';
  33. local LOBBY_IDENTITY_TYPE = 'lobbyrooms';
  34. local NOTIFY_JSON_MESSAGE_TYPE = 'lobby-notify';
  35. local NOTIFY_LOBBY_ENABLED = 'LOBBY-ENABLED';
  36. local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
  37. local NOTIFY_LOBBY_ACCESS_DENIED = 'LOBBY-ACCESS-DENIED';
  38. local util = module:require "util";
  39. local get_room_by_name_and_subdomain = util.get_room_by_name_and_subdomain;
  40. local is_healthcheck_room = util.is_healthcheck_room;
  41. local presence_check_status = util.presence_check_status;
  42. local main_muc_component_config = module:get_option_string('main_muc');
  43. if main_muc_component_config == nil then
  44. module:log('error', 'lobby not enabled missing main_muc config');
  45. return ;
  46. end
  47. local lobby_muc_component_config = module:get_option_string('lobby_muc');
  48. if lobby_muc_component_config == nil then
  49. module:log('error', 'lobby not enabled missing lobby_muc config');
  50. return ;
  51. end
  52. local whitelist;
  53. local check_display_name_required;
  54. local function load_config()
  55. whitelist = module:get_option_set('muc_lobby_whitelist', {});
  56. check_display_name_required
  57. = module:get_option_boolean('muc_lobby_check_display_name_required', true);
  58. end
  59. load_config();
  60. local lobby_muc_service;
  61. local main_muc_service;
  62. function broadcast_json_msg(room, from, json_msg)
  63. json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
  64. local occupant = room:get_occupant_by_real_jid(from);
  65. if occupant then
  66. room:broadcast_message(
  67. st.message({ type = 'groupchat', from = occupant.nick })
  68. :tag('json-message', {xmlns='http://jitsi.org/jitmeet'})
  69. :text(json.encode(json_msg)):up());
  70. end
  71. end
  72. -- Sends a json message notifying for lobby enabled/disable
  73. -- the message from is the actor that did the operation
  74. function notify_lobby_enabled(room, actor, value)
  75. broadcast_json_msg(room, actor, {
  76. event = NOTIFY_LOBBY_ENABLED,
  77. value = value
  78. });
  79. end
  80. -- Sends a json message notifying that the jid was granted/denied access in lobby
  81. -- the message from is the actor that did the operation
  82. function notify_lobby_access(room, actor, jid, display_name, granted)
  83. local notify_json = {
  84. value = jid,
  85. name = display_name
  86. };
  87. if granted then
  88. notify_json.event = NOTIFY_LOBBY_ACCESS_GRANTED;
  89. else
  90. notify_json.event = NOTIFY_LOBBY_ACCESS_DENIED;
  91. end
  92. broadcast_json_msg(room, actor, notify_json);
  93. end
  94. function filter_stanza(stanza)
  95. if not stanza.attr or not stanza.attr.from or not main_muc_service then
  96. return stanza;
  97. end
  98. -- Allow self-presence (code=110)
  99. local node, from_domain = jid_split(stanza.attr.from);
  100. if from_domain == lobby_muc_component_config then
  101. if stanza.name == 'presence' then
  102. if presence_check_status(stanza:get_child('x', MUC_NS..'#user'), '110') then
  103. return stanza;
  104. end
  105. -- check is an owner, only owners can receive the presence
  106. local room = main_muc_service.get_room_from_jid(jid_bare(node .. '@' .. main_muc_component_config));
  107. if not room or room.get_affiliation(room, stanza.attr.to) == 'owner' then
  108. return stanza;
  109. end
  110. return nil;
  111. elseif stanza.name == 'iq' and stanza:get_child('query', DISCO_INFO_NS) then
  112. -- allow disco info from the lobby component
  113. return stanza;
  114. end
  115. return nil;
  116. else
  117. return stanza;
  118. end
  119. end
  120. function filter_session(session)
  121. -- domain mapper is filtering on default priority 0, and we need it after that
  122. filters.add_filter(session, 'stanzas/out', filter_stanza, -1);
  123. end
  124. function attach_lobby_room(room)
  125. local node = jid_split(room.jid);
  126. local lobby_room_jid = node .. '@' .. lobby_muc_component_config;
  127. if not lobby_muc_service.get_room_from_jid(lobby_room_jid) then
  128. local new_room = lobby_muc_service.create_room(lobby_room_jid);
  129. -- set persistent the lobby room to avoid it to be destroyed
  130. -- there are cases like when selecting new moderator after the current one leaves
  131. -- which can leave the room with no occupants and it will be destroyed and we want to
  132. -- avoid lobby destroy while it is enabled
  133. new_room:set_persistent(true);
  134. module:log("debug","Lobby room jid = %s created",lobby_room_jid);
  135. new_room.main_room = room;
  136. room._data.lobbyroom = new_room.jid;
  137. room:save(true);
  138. return true
  139. end
  140. return false
  141. end
  142. -- destroys lobby room for the supplied main room
  143. function destroy_lobby_room(room, newjid, message)
  144. if not message then
  145. message = 'Lobby room closed.';
  146. end
  147. if lobby_muc_service and room and room._data.lobbyroom then
  148. local lobby_room_obj = lobby_muc_service.get_room_from_jid(room._data.lobbyroom);
  149. if lobby_room_obj then
  150. lobby_room_obj:set_persistent(false);
  151. lobby_room_obj:destroy(newjid, message);
  152. end
  153. room._data.lobbyroom = nil;
  154. end
  155. end
  156. -- process a host module directly if loaded or hooks to wait for its load
  157. function process_host_module(name, callback)
  158. local function process_host(host)
  159. if host == name then
  160. callback(module:context(host), host);
  161. end
  162. end
  163. if prosody.hosts[name] == nil then
  164. module:log('debug', 'No host/component found, will wait for it: %s', name)
  165. -- when a host or component is added
  166. prosody.events.add_handler('host-activated', process_host);
  167. else
  168. process_host(name);
  169. end
  170. end
  171. -- operates on already loaded lobby muc module
  172. function process_lobby_muc_loaded(lobby_muc, host_module)
  173. module:log('debug', 'Lobby muc loaded');
  174. lobby_muc_service = lobby_muc;
  175. -- enable filtering presences in the lobby muc rooms
  176. filters.add_filter_hook(filter_session);
  177. -- Advertise lobbyrooms support on main domain so client can pick up the address and use it
  178. module:add_identity('component', LOBBY_IDENTITY_TYPE, lobby_muc_component_config);
  179. -- Tag the disco#info response with a feature that display name is required
  180. -- when the conference name from the web request has a lobby enabled.
  181. host_module:hook('host-disco-info-node', function (event)
  182. local session, reply, node = event.origin, event.reply, event.node;
  183. if node == LOBBY_IDENTITY_TYPE
  184. and session.jitsi_web_query_room
  185. and check_display_name_required then
  186. local room = get_room_by_name_and_subdomain(session.jitsi_web_query_room, session.jitsi_web_query_prefix);
  187. if room and room._data.lobbyroom then
  188. reply:tag('feature', { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
  189. end
  190. end
  191. event.exists = true;
  192. end);
  193. local room_mt = lobby_muc_service.room_mt;
  194. -- we base affiliations (roles) in lobby muc component to be based on the roles in the main muc
  195. room_mt.get_affiliation = function(room, jid)
  196. if not room.main_room then
  197. module:log('error', 'No main room(%s) for %s!', room.jid, jid);
  198. return 'none';
  199. end
  200. -- moderators in main room are moderators here
  201. local role = room.main_room.get_affiliation(room.main_room, jid);
  202. if role then
  203. return role;
  204. end
  205. return 'none';
  206. end
  207. -- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
  208. host_module:hook('muc-broadcast-presence', function (event)
  209. local actor, occupant, room, x = event.actor, event.occupant, event.room, event.x;
  210. if presence_check_status(x, '307') then
  211. local display_name = occupant:get_presence():get_child_text(
  212. 'nick', 'http://jabber.org/protocol/nick');
  213. -- we need to notify in the main room
  214. notify_lobby_access(room.main_room, actor, occupant.nick, display_name, false);
  215. end
  216. end);
  217. end
  218. -- process or waits to process the lobby muc component
  219. process_host_module(lobby_muc_component_config, function(host_module, host)
  220. -- lobby muc component created
  221. module:log('info', 'Lobby component loaded %s', host);
  222. local muc_module = prosody.hosts[host].modules.muc;
  223. if muc_module then
  224. process_lobby_muc_loaded(muc_module, host_module);
  225. else
  226. module:log('debug', 'Will wait for muc to be available');
  227. prosody.hosts[host].events.add_handler('module-loaded', function(event)
  228. if (event.module == 'muc') then
  229. process_lobby_muc_loaded(prosody.hosts[host].modules.muc, host_module);
  230. end
  231. end);
  232. end
  233. end);
  234. -- process or waits to process the main muc component
  235. process_host_module(main_muc_component_config, function(host_module, host)
  236. main_muc_service = prosody.hosts[host].modules.muc;
  237. -- hooks when lobby is enabled to create its room, only done here or by admin
  238. host_module:hook('muc-config-submitted', function(event)
  239. local actor, room = event.actor, event.room;
  240. local actor_node = jid_split(actor);
  241. if actor_node == 'focus' then
  242. return;
  243. end
  244. local members_only = event.fields['muc#roomconfig_membersonly'] and true or nil;
  245. if members_only then
  246. local lobby_created = attach_lobby_room(room);
  247. if lobby_created then
  248. event.status_codes['104'] = true;
  249. notify_lobby_enabled(room, actor, true);
  250. end
  251. elseif room._data.lobbyroom then
  252. destroy_lobby_room(room, room.jid);
  253. notify_lobby_enabled(room, actor, false);
  254. end
  255. end);
  256. host_module:hook('muc-room-destroyed',function(event)
  257. local room = event.room;
  258. if room._data.lobbyroom then
  259. destroy_lobby_room(room, nil);
  260. end
  261. end);
  262. host_module:hook('muc-disco#info', function (event)
  263. local room = event.room;
  264. if (room._data.lobbyroom and room:get_members_only()) then
  265. table.insert(event.form, {
  266. name = 'muc#roominfo_lobbyroom';
  267. label = 'Lobby room jid';
  268. value = '';
  269. });
  270. event.formdata['muc#roominfo_lobbyroom'] = room._data.lobbyroom;
  271. end
  272. end);
  273. host_module:hook('muc-occupant-pre-join', function (event)
  274. local room, stanza = event.room, event.stanza;
  275. if is_healthcheck_room(room.jid) or not room:get_members_only() then
  276. return;
  277. end
  278. local join = stanza:get_child('x', MUC_NS);
  279. if not join then
  280. return;
  281. end
  282. local invitee = event.stanza.attr.from;
  283. local invitee_bare_jid = jid_bare(invitee);
  284. local _, invitee_domain = jid_split(invitee);
  285. local whitelistJoin = false;
  286. -- whitelist participants
  287. if whitelist:contains(invitee_domain) or whitelist:contains(invitee_bare_jid) then
  288. whitelistJoin = true;
  289. end
  290. local password = join:get_child_text('password', MUC_NS);
  291. if password and room:get_password() and password == room:get_password() then
  292. whitelistJoin = true;
  293. end
  294. if whitelistJoin then
  295. local affiliation = room:get_affiliation(invitee);
  296. if not affiliation or affiliation == 0 then
  297. event.occupant.role = 'participant';
  298. room:set_affiliation(true, invitee_bare_jid, 'member');
  299. room:save();
  300. return;
  301. end
  302. end
  303. -- we want to add the custom lobbyroom field to fill in the lobby room jid
  304. local invitee = event.stanza.attr.from;
  305. local affiliation = room:get_affiliation(invitee);
  306. if not affiliation or affiliation == 'none' then
  307. local reply = st.error_reply(stanza, 'auth', 'registration-required'):up();
  308. reply.tags[1].attr.code = '407';
  309. reply:tag('x', {xmlns = MUC_NS}):up();
  310. reply:tag('lobbyroom'):text(room._data.lobbyroom);
  311. event.origin.send(reply:tag('x', {xmlns = MUC_NS}));
  312. return true;
  313. end
  314. end, -4); -- the default hook on members_only module is on -5
  315. -- listens for invites for participants to join the main room
  316. host_module:hook('muc-invite', function(event)
  317. local room, stanza = event.room, event.stanza;
  318. local invitee = stanza.attr.to;
  319. local from = stanza:get_child('x', 'http://jabber.org/protocol/muc#user')
  320. :get_child('invite').attr.from;
  321. if lobby_muc_service and room._data.lobbyroom then
  322. local lobby_room_obj = lobby_muc_service.get_room_from_jid(room._data.lobbyroom);
  323. if lobby_room_obj then
  324. local occupant = lobby_room_obj:get_occupant_by_real_jid(invitee);
  325. if occupant then
  326. local display_name = occupant:get_presence():get_child_text(
  327. 'nick', 'http://jabber.org/protocol/nick');
  328. notify_lobby_access(room, from, occupant.nick, display_name, true);
  329. end
  330. end
  331. end
  332. end);
  333. end);
  334. function handle_create_lobby(event)
  335. local room = event.room;
  336. room:set_members_only(true);
  337. module:log("info","Set room jid = %s as members only",room.jid);
  338. attach_lobby_room(room)
  339. end
  340. function handle_destroy_lobby(event)
  341. destroy_lobby_room(event.room, event.newjid, event.message);
  342. end
  343. module:hook_global('config-reloaded', load_config);
  344. module:hook_global('create-lobby-room', handle_create_lobby);
  345. module:hook_global('destroy-lobby-room', handle_destroy_lobby);