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

mod_av_moderation_component.lua 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. local get_room_by_name_and_subdomain = module:require 'util'.get_room_by_name_and_subdomain;
  2. local is_healthcheck_room = module:require 'util'.is_healthcheck_room;
  3. local json = require 'util.json';
  4. local st = require 'util.stanza';
  5. local muc_component_host = module:get_option_string('muc_component');
  6. if muc_component_host == nil then
  7. log('error', 'No muc_component specified. No muc to operate on!');
  8. return;
  9. end
  10. module:log('info', 'Starting av_moderation for %s', muc_component_host);
  11. -- Sends a json-message to the destination jid
  12. -- @param to_jid the destination jid
  13. -- @param json_message the message content to send
  14. function send_json_message(to_jid, json_message)
  15. local stanza = st.message({ from = module.host; to = to_jid; })
  16. :tag('json-message', { xmlns = 'http://jitsi.org/jitmeet' }):text(json_message):up();
  17. module:send(stanza);
  18. end
  19. -- Notifies that av moderation has been enabled or disabled
  20. -- @param jid the jid to notify, if missing will notify all occupants
  21. -- @param enable whether it is enabled or disabled
  22. -- @param room the room
  23. -- @param actorJid the jid that is performing the enable/disable operation (the muc jid)
  24. -- @param mediaType the media type for the moderation
  25. function notify_occupants_enable(jid, enable, room, actorJid, mediaType)
  26. local body_json = {};
  27. body_json.type = 'av_moderation';
  28. body_json.enabled = enable;
  29. body_json.room = room.jid;
  30. body_json.actor = actorJid;
  31. body_json.mediaType = mediaType;
  32. local body_json_str = json.encode(body_json);
  33. if jid then
  34. send_json_message(jid, body_json_str)
  35. else
  36. for _, occupant in room:each_occupant() do
  37. send_json_message(occupant.jid, body_json_str)
  38. end
  39. end
  40. end
  41. -- Notifies about a jid added to the whitelist. Notifies all moderators and admin and the jid itself
  42. -- @param jid the jid to notify about the change
  43. -- @param moderators whether to notify all moderators in the room
  44. -- @param room the room where to send it
  45. -- @param mediaType used only when a participant is approved (not sent to moderators)
  46. function notify_whitelist_change(jid, moderators, room, mediaType)
  47. local body_json = {};
  48. body_json.type = 'av_moderation';
  49. body_json.room = room.jid;
  50. body_json.whitelists = room.av_moderation;
  51. local moderators_body_json_str = json.encode(body_json);
  52. body_json.whitelists = nil;
  53. body_json.approved = true; -- we want to send to participants only that they were approved to unmute
  54. body_json.mediaType = mediaType;
  55. local participant_body_json_str = json.encode(body_json);
  56. for _, occupant in room:each_occupant() do
  57. if moderators and occupant.role == 'moderator' then
  58. send_json_message(occupant.jid, moderators_body_json_str);
  59. elseif occupant.jid == jid then
  60. send_json_message(occupant.jid, participant_body_json_str);
  61. end
  62. end
  63. end
  64. -- receives messages from clients to the component sending A/V moderation enable/disable commands or adding
  65. -- jids to the whitelist
  66. function on_message(event)
  67. local session = event.origin;
  68. -- Check the type of the incoming stanza to avoid loops:
  69. if event.stanza.attr.type == 'error' then
  70. return; -- We do not want to reply to these, so leave.
  71. end
  72. if not session or not session.jitsi_web_query_room then
  73. return false;
  74. end
  75. local moderation_command = event.stanza:get_child('av_moderation');
  76. if moderation_command then
  77. -- get room name with tenant and find room
  78. local room = get_room_by_name_and_subdomain(session.jitsi_web_query_room, session.jitsi_web_query_prefix);
  79. if not room then
  80. module:log('warn', 'No room found found for %s/%s',
  81. session.jitsi_web_query_prefix, session.jitsi_web_query_room);
  82. return false;
  83. end
  84. -- check that the participant requesting is a moderator and is an occupant in the room
  85. local from = event.stanza.attr.from;
  86. local occupant = room:get_occupant_by_real_jid(from);
  87. if not occupant then
  88. log('warn', 'No occupant %s found for %s', from, room.jid);
  89. return false;
  90. end
  91. if occupant.role ~= 'moderator' then
  92. log('warn', 'Occupant %s is not moderator and not allowed this operation for %s', from, room.jid);
  93. return false;
  94. end
  95. local mediaType = moderation_command.attr.mediaType;
  96. if mediaType then
  97. if mediaType ~= 'audio' and mediaType ~= 'video' then
  98. module:log('warn', 'Wrong mediaType %s for %s', mediaType, room.jid);
  99. return false;
  100. end
  101. else
  102. module:log('warn', 'Missing mediaType for %s', room.jid);
  103. return false;
  104. end
  105. if moderation_command.attr.enable ~= nil then
  106. local enabled;
  107. if moderation_command.attr.enable == 'true' then
  108. enabled = true;
  109. if room.av_moderation and room.av_moderation[mediaType] then
  110. module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
  111. return true;
  112. else
  113. room.av_moderation = {};
  114. room.av_moderation_actors = {};
  115. room.av_moderation[mediaType] = {};
  116. room.av_moderation_actors[mediaType] = occupant.nick;
  117. end
  118. else
  119. enabled = false;
  120. if not room.av_moderation or not room.av_moderation[mediaType] then
  121. module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
  122. return true;
  123. else
  124. room.av_moderation[mediaType] = nil;
  125. room.av_moderation_actors[mediaType] = nil;
  126. -- clears room.av_moderation if empty
  127. local is_empty = false;
  128. for key,_ in pairs(room.av_moderation) do
  129. if room.av_moderation[key] then
  130. is_empty = true;
  131. end
  132. end
  133. if is_empty then
  134. room.av_moderation = nil;
  135. end
  136. end
  137. end
  138. -- send message to all occupants
  139. notify_occupants_enable(nil, enabled, room, occupant.nick, mediaType);
  140. return true;
  141. elseif moderation_command.attr.jidToWhitelist and room.av_moderation then
  142. local occupant_jid = moderation_command.attr.jidToWhitelist;
  143. -- check if jid is in the room, if so add it to whitelist
  144. -- inform all moderators and admins and the jid
  145. local occupant_to_add = room:get_occupant_by_nick(occupant_jid);
  146. if not occupant_to_add then
  147. module:log('warn', 'No occupant %s found for %s', occupant_jid, room.jid);
  148. return false;
  149. end
  150. local whitelist = room.av_moderation[mediaType];
  151. if not whitelist then
  152. whitelist = {};
  153. room.av_moderation[mediaType] = whitelist;
  154. end
  155. table.insert(whitelist, occupant_jid);
  156. notify_whitelist_change(occupant_to_add.jid, true, room, mediaType);
  157. return true;
  158. end
  159. end
  160. -- return error
  161. return false
  162. end
  163. -- handles new occupants to inform them about the state enabled/disabled, new moderators also get and the whitelist
  164. function occupant_joined(event)
  165. local room, occupant = event.room, event.occupant;
  166. if is_healthcheck_room(room.jid) then
  167. return;
  168. end
  169. if room.av_moderation then
  170. for _,mediaType in pairs({'audio', 'video'}) do
  171. if room.av_moderation[mediaType] then
  172. notify_occupants_enable(
  173. occupant.jid, true, room, room.av_moderation_actors[mediaType], mediaType);
  174. end
  175. end
  176. -- NOTE for some reason event.occupant.role is not reflecting the actual occupant role (when changed
  177. -- from allowners module) but iterating over room occupants returns the correct role
  178. for _, room_occupant in room:each_occupant() do
  179. -- if moderator send the whitelist
  180. if room_occupant.nick == occupant.nick and room_occupant.role == 'moderator' then
  181. notify_whitelist_change(room_occupant.jid, false, room);
  182. end
  183. end
  184. end
  185. end
  186. -- when a occupant was granted moderator we need to update him with the whitelist
  187. function occupant_affiliation_changed(event)
  188. -- the actor can be nil if is coming from allowners or similar module we want to skip it here
  189. -- as we will handle it in occupant_joined
  190. if event.actor and event.affiliation == 'owner' and event.room.av_moderation then
  191. local room = event.room;
  192. -- event.jid is the bare jid of participant
  193. for _, occupant in room:each_occupant() do
  194. if occupant.bare_jid == event.jid then
  195. notify_whitelist_change(occupant.jid, false, room);
  196. end
  197. end
  198. end
  199. end
  200. -- we will receive messages from the clients
  201. module:hook('message/host', on_message);
  202. -- executed on every host added internally in prosody, including components
  203. function process_host(host)
  204. if host == muc_component_host then -- the conference muc component
  205. module:log('info','Hook to muc events on %s', host);
  206. local muc_module = module:context(host);
  207. muc_module:hook('muc-occupant-joined', occupant_joined, -2); -- make sure it runs after allowners or similar
  208. muc_module:hook('muc-set-affiliation', occupant_affiliation_changed, -1);
  209. end
  210. end
  211. if prosody.hosts[muc_component_host] == nil then
  212. module:log('info', 'No muc component found, will listen for it: %s', muc_component_host);
  213. -- when a host or component is added
  214. prosody.events.add_handler('host-activated', process_host);
  215. else
  216. process_host(muc_component_host);
  217. end