Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mod_muc_allowners.lua 5.1KB

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