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

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