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

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