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_allowners.lua 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. local filters = require 'util.filters';
  2. local jid = require "util.jid";
  3. local jid_bare = require "util.jid".bare;
  4. local jid_host = require "util.jid".host;
  5. local st = require "util.stanza";
  6. local um_is_admin = require "core.usermanager".is_admin;
  7. local util = module:require "util";
  8. local is_healthcheck_room = util.is_healthcheck_room;
  9. local extract_subdomain = util.extract_subdomain;
  10. local get_room_from_jid = util.get_room_from_jid;
  11. local presence_check_status = util.presence_check_status;
  12. local MUC_NS = 'http://jabber.org/protocol/muc';
  13. local moderated_subdomains;
  14. local moderated_rooms;
  15. local disable_revoke_owners;
  16. local function load_config()
  17. moderated_subdomains = module:get_option_set("allowners_moderated_subdomains", {})
  18. moderated_rooms = module:get_option_set("allowners_moderated_rooms", {})
  19. disable_revoke_owners = module:get_option_boolean("allowners_disable_revoke_owners", false);
  20. end
  21. load_config();
  22. local function is_admin(_jid)
  23. return um_is_admin(_jid, module.host);
  24. end
  25. -- List of the bare_jids of all occupants that are currently joining (went through pre-join) and will be promoted
  26. -- as moderators. As pre-join (where added) and joined event (where removed) happen one after another this list should
  27. -- have length of 1
  28. local joining_moderator_participants = {};
  29. -- Checks whether the jid is moderated, the room name is in moderated_rooms
  30. -- or if the subdomain is in the moderated_subdomains
  31. -- @return returns on of the:
  32. -- -> false
  33. -- -> true, room_name, subdomain
  34. -- -> true, room_name, nil (if no subdomain is used for the room)
  35. local function is_moderated(room_jid)
  36. if moderated_subdomains:empty() and moderated_rooms:empty() then
  37. return false;
  38. end
  39. local room_node = jid.node(room_jid);
  40. -- parses bare room address, for multidomain expected format is:
  41. -- [subdomain]roomName@conference.domain
  42. local target_subdomain, target_room_name = extract_subdomain(room_node);
  43. if target_subdomain then
  44. if moderated_subdomains:contains(target_subdomain) then
  45. return true, target_room_name, target_subdomain;
  46. end
  47. elseif moderated_rooms:contains(room_node) then
  48. return true, room_node, nil;
  49. end
  50. return false;
  51. end
  52. module:hook("muc-occupant-pre-join", function (event)
  53. local room, occupant = event.room, event.occupant;
  54. if is_healthcheck_room(room.jid) or is_admin(occupant.bare_jid) then
  55. return;
  56. end
  57. local moderated, room_name, subdomain = is_moderated(room.jid);
  58. if moderated then
  59. local session = event.origin;
  60. local token = session.auth_token;
  61. if not token then
  62. module:log('debug', 'skip allowners for non-auth user subdomain:%s room_name:%s', subdomain, room_name);
  63. return;
  64. end
  65. if not (room_name == session.jitsi_meet_room or session.jitsi_meet_room == '*') then
  66. module:log('debug', 'skip allowners for auth user and non matching room name: %s, jwt room name: %s',
  67. room_name, session.jitsi_meet_room);
  68. return;
  69. end
  70. if not (subdomain == session.jitsi_meet_context_group) then
  71. module:log('debug', 'skip allowners for auth user and non matching room subdomain: %s, jwt subdomain: %s',
  72. subdomain, session.jitsi_meet_context_group);
  73. return;
  74. end
  75. end
  76. -- mark this participant that it will be promoted and is currently joining
  77. joining_moderator_participants[occupant.bare_jid] = true;
  78. end, 2);
  79. module:hook("muc-occupant-joined", function (event)
  80. local room, occupant = event.room, event.occupant;
  81. local promote_to_moderator = joining_moderator_participants[occupant.bare_jid];
  82. -- clear it
  83. joining_moderator_participants[occupant.bare_jid] = nil;
  84. if promote_to_moderator ~= nil then
  85. room:set_affiliation(true, occupant.bare_jid, "owner");
  86. end
  87. end, 2);
  88. module:hook_global('config-reloaded', load_config);
  89. -- Filters self-presences to a jid that exist in joining_participants array
  90. -- We want to filter those presences where we send first `participant` and just after it `moderator`
  91. function filter_stanza(stanza)
  92. -- when joining_moderator_participants is empty there is nothing to filter
  93. if next(joining_moderator_participants) == nil
  94. or not stanza.attr
  95. or not stanza.attr.to
  96. or stanza.name ~= "presence" then
  97. return stanza;
  98. end
  99. -- we want to filter presences only on this host for allowners and skip anything like lobby etc.
  100. local host_from = jid_host(stanza.attr.from);
  101. if host_from ~= module.host then
  102. return stanza;
  103. end
  104. local bare_to = jid_bare(stanza.attr.to);
  105. if stanza:get_error() and joining_moderator_participants[bare_to] then
  106. -- pre-join succeeded but joined did not so we need to clear cache
  107. joining_moderator_participants[bare_to] = nil;
  108. return stanza;
  109. end
  110. local muc_x = stanza:get_child('x', MUC_NS..'#user');
  111. if not muc_x then
  112. return stanza;
  113. end
  114. if joining_moderator_participants[bare_to] and presence_check_status(muc_x, '110') then
  115. -- skip the local presence for participant
  116. return nil;
  117. end
  118. -- skip sending the 'participant' presences to all other people in the room
  119. for item in muc_x:childtags('item') do
  120. if joining_moderator_participants[jid_bare(item.attr.jid)] then
  121. return nil;
  122. end
  123. end
  124. return stanza;
  125. end
  126. function filter_session(session)
  127. -- domain mapper is filtering on default priority 0, and we need it after that
  128. filters.add_filter(session, 'stanzas/out', filter_stanza, -1);
  129. end
  130. -- enable filtering presences
  131. filters.add_filter_hook(filter_session);
  132. -- filters any attempt to revoke owner rights on non moderated rooms
  133. function filter_admin_set_query(event)
  134. local origin, stanza = event.origin, event.stanza;
  135. local room_jid = jid_bare(stanza.attr.to);
  136. local room = get_room_from_jid(room_jid);
  137. local item = stanza.tags[1].tags[1];
  138. local _aff = item.attr.affiliation;
  139. -- if it is a moderated room we skip it
  140. if is_moderated(room.jid) then
  141. return nil;
  142. end
  143. -- any revoking is disabled
  144. if _aff ~= 'owner' then
  145. origin.send(st.error_reply(stanza, "auth", "forbidden"));
  146. return true;
  147. end
  148. end
  149. if not disable_revoke_owners then
  150. -- default prosody priority for handling these is -2
  151. module:hook("iq-set/bare/http://jabber.org/protocol/muc#admin:query", filter_admin_set_query, 5);
  152. module:hook("iq-set/host/http://jabber.org/protocol/muc#admin:query", filter_admin_set_query, 5);
  153. end