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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "lobbyrooms.damencho.jitsi.net" "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 jid_split = require 'util.jid'.split;
  25. local jid_bare = require 'util.jid'.bare;
  26. local filters = require 'util.filters';
  27. local st = require 'util.stanza';
  28. local MUC_NS = 'http://jabber.org/protocol/muc';
  29. local is_healthcheck_room = module:require "util".is_healthcheck_room;
  30. local main_muc_component_config = module:get_option_string('main_muc');
  31. if main_muc_component_config == nil then
  32. module:log('error', 'lobby not enabled missing main_muc config');
  33. return ;
  34. end
  35. local lobby_muc_component_config = module:get_option_string('lobby_muc');
  36. if lobby_muc_component_config == nil then
  37. module:log('error', 'lobby not enabled missing lobby_muc config');
  38. return ;
  39. end
  40. local lobby_muc_service;
  41. local main_muc_service;
  42. -- Checks whether there is self-status 110 of the <x node
  43. function check_self_status(muc_x)
  44. if not muc_x then
  45. return false;
  46. end
  47. for status in muc_x:childtags('status') do
  48. if status.attr.code == '110' then
  49. return true;
  50. end
  51. end
  52. return false;
  53. end
  54. function filter_stanza(stanza)
  55. if not stanza.attr or not stanza.attr.from or not main_muc_service then
  56. return stanza;
  57. end
  58. -- Allow self-presence (code=110)
  59. local node, from_domain = jid_split(stanza.attr.from);
  60. if from_domain == lobby_muc_component_config then
  61. if stanza.name == 'presence' then
  62. local muc_x = stanza:get_child('x', MUC_NS..'#user');
  63. if muc_x and check_self_status(muc_x) then
  64. return stanza;
  65. end
  66. -- check is an owner, only owners can receive the presence
  67. local room = main_muc_service.get_room_from_jid(jid_bare(node .. '@' .. main_muc_component_config));
  68. if room.get_affiliation(room, stanza.attr.to) == 'owner' then
  69. return stanza;
  70. end
  71. return nil;
  72. end
  73. return nil;
  74. else
  75. return stanza;
  76. end
  77. end
  78. function filter_session(session)
  79. if session.host and session.host == module.host then
  80. -- domain mapper is filtering on default priority 0, and we need it after that
  81. filters.add_filter(session, 'stanzas/out', filter_stanza, -1);
  82. end
  83. end
  84. -- process a host module directly if loaded or hooks to wait for its load
  85. function process_host_module(name, callback)
  86. local function process_host(host)
  87. if host == name then
  88. callback(module:context(host), host);
  89. end
  90. end
  91. if prosody.hosts[name] == nil then
  92. module:log('debug', 'No host/component found, will wait for it: %s', name)
  93. -- when a host or component is added
  94. prosody.events.add_handler('host-activated', process_host);
  95. else
  96. process_host(name);
  97. end
  98. end
  99. -- operates on already loaded lobby muc module
  100. function process_lobby_muc_loaded(lobby_muc, host_module)
  101. module:log('debug', 'Lobby muc loaded');
  102. lobby_muc_service = lobby_muc;
  103. -- enable filtering presences in the lobby muc rooms
  104. filters.add_filter_hook(filter_session);
  105. -- Advertise lobbyrooms support on main domain so client can pick up the address and use it
  106. module:add_identity('component', 'lobbyrooms', lobby_muc_component_config);
  107. local room_mt = lobby_muc_service.room_mt;
  108. -- we base affiliations (roles) in lobby muc component to be based on the roles in the main muc
  109. room_mt.get_affiliation = function(room, jid)
  110. if not room.main_room then
  111. module:log('error', 'No main room(%s) for %s!', room.jid, jid);
  112. return 'none';
  113. end
  114. -- moderators in main room are moderators here
  115. local role = room.main_room.get_affiliation(room.main_room, jid);
  116. if role then
  117. return role;
  118. end
  119. return 'none';
  120. end
  121. end
  122. -- process or waits to process the lobby muc component
  123. process_host_module(lobby_muc_component_config, function(host_module, host)
  124. -- lobby muc component created
  125. module:log('info', 'Lobby component loaded %s', host);
  126. local muc_module = prosody.hosts[host].modules.muc;
  127. if muc_module then
  128. process_lobby_muc_loaded(muc_module, host_module);
  129. else
  130. module:log('debug', 'Will wait for muc to be available');
  131. prosody.hosts[host].events.add_handler('module-loaded', function(event)
  132. if (event.module == 'muc') then
  133. process_lobby_muc_loaded(prosody.hosts[host].modules.muc, host_module);
  134. end
  135. end);
  136. end
  137. end);
  138. -- process or waits to process the main muc component
  139. process_host_module(main_muc_component_config, function(host_module, host)
  140. main_muc_service = prosody.hosts[host].modules.muc;
  141. -- adds new field to the form so moderators can use it to set shared password
  142. host_module:hook('muc-config-form', function(event)
  143. table.insert(event.form, {
  144. name = 'muc#roomconfig_lobbypassword';
  145. type = 'text-private';
  146. label = 'Shared Password';
  147. value = '';
  148. });
  149. end, 90-4);
  150. -- hooks when lobby is enabled to create its room, only done here or by admin
  151. host_module:hook('muc-config-submitted', function(event)
  152. local members_only = event.fields['muc#roomconfig_membersonly'] and true or nil;
  153. if members_only then
  154. local node = jid_split(event.room.jid);
  155. local lobby_room_jid = node .. '@' .. lobby_muc_component_config;
  156. if not lobby_muc_service.get_room_from_jid(lobby_room_jid) then
  157. local new_room = lobby_muc_service.create_room(lobby_room_jid);
  158. new_room.main_room = event.room;
  159. event.room._data.lobbyroom = lobby_room_jid;
  160. event.status_codes["104"] = true;
  161. local lobby_password = event.fields['muc#roomconfig_lobbypassword'];
  162. if lobby_password then
  163. new_room.main_room.lobby_password = lobby_password;
  164. end
  165. end
  166. end
  167. end);
  168. host_module:hook("muc-disco#info", function (event)
  169. if (event.room._data.lobbyroom) then
  170. table.insert(event.form, {
  171. name = "muc#roominfo_lobbyroom";
  172. label = "Lobby room jid";
  173. value = "";
  174. });
  175. event.formdata["muc#roominfo_lobbyroom"] = event.room._data.lobbyroom;
  176. end
  177. end);
  178. host_module:hook('muc-occupant-pre-join', function (event)
  179. local room, stanza = event.room, event.stanza;
  180. if is_healthcheck_room(room.jid) then
  181. return;
  182. end
  183. local join = stanza:get_child("x", MUC_NS);
  184. if not join then
  185. return;
  186. end
  187. local password = join:get_child_text("lobbySharedPassword");
  188. if password and event.room.lobby_password and password == room.lobby_password then
  189. local invitee = event.stanza.attr.from;
  190. local affiliation = room:get_affiliation(invitee);
  191. if not affiliation or affiliation == 0 then
  192. event.occupant.role = 'participant';
  193. room:set_affiliation(true, jid_bare(invitee), "member");
  194. room:save();
  195. end
  196. -- we want to add the custom lobbyroom field to fill in the lobby room jid
  197. elseif room._data.members_only then
  198. local invitee = event.stanza.attr.from;
  199. local affiliation = room:get_affiliation(invitee);
  200. if not affiliation or affiliation == 'none' then
  201. local reply = st.error_reply(stanza, 'auth', 'registration-required'):up();
  202. reply.tags[1].attr.code = '407';
  203. reply:tag('x', {xmlns = MUC_NS}):up();
  204. reply:tag('lobbyroom'):text(room._data.lobbyroom);
  205. event.origin.send(reply:tag('x', {xmlns = MUC_NS}));
  206. return true;
  207. end
  208. end
  209. end, -4); -- the default hook on members_only module is on -5
  210. end);