Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

mod_av_moderation_component.lua 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. -- if the occupant is not moderator we send him that it is approved
  61. -- if it is moderator we update him with the list, this is moderator joining or grant moderation was executed
  62. if occupant.role == 'moderator' then
  63. send_json_message(occupant.jid, moderators_body_json_str);
  64. else
  65. send_json_message(occupant.jid, participant_body_json_str);
  66. end
  67. end
  68. end
  69. end
  70. -- receives messages from clients to the component sending A/V moderation enable/disable commands or adding
  71. -- jids to the whitelist
  72. function on_message(event)
  73. local session = event.origin;
  74. -- Check the type of the incoming stanza to avoid loops:
  75. if event.stanza.attr.type == 'error' then
  76. return; -- We do not want to reply to these, so leave.
  77. end
  78. if not session or not session.jitsi_web_query_room then
  79. return false;
  80. end
  81. local moderation_command = event.stanza:get_child('av_moderation');
  82. if moderation_command then
  83. -- get room name with tenant and find room
  84. local room = get_room_by_name_and_subdomain(session.jitsi_web_query_room, session.jitsi_web_query_prefix);
  85. if not room then
  86. module:log('warn', 'No room found found for %s/%s',
  87. session.jitsi_web_query_prefix, session.jitsi_web_query_room);
  88. return false;
  89. end
  90. -- check that the participant requesting is a moderator and is an occupant in the room
  91. local from = event.stanza.attr.from;
  92. local occupant = room:get_occupant_by_real_jid(from);
  93. if not occupant then
  94. log('warn', 'No occupant %s found for %s', from, room.jid);
  95. return false;
  96. end
  97. if occupant.role ~= 'moderator' then
  98. log('warn', 'Occupant %s is not moderator and not allowed this operation for %s', from, room.jid);
  99. return false;
  100. end
  101. local mediaType = moderation_command.attr.mediaType;
  102. if mediaType then
  103. if mediaType ~= 'audio' and mediaType ~= 'video' then
  104. module:log('warn', 'Wrong mediaType %s for %s', mediaType, room.jid);
  105. return false;
  106. end
  107. else
  108. module:log('warn', 'Missing mediaType for %s', room.jid);
  109. return false;
  110. end
  111. if moderation_command.attr.enable ~= nil then
  112. local enabled;
  113. if moderation_command.attr.enable == 'true' then
  114. enabled = true;
  115. if room.av_moderation and room.av_moderation[mediaType] then
  116. module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
  117. return true;
  118. else
  119. room.av_moderation = {};
  120. room.av_moderation_actors = {};
  121. room.av_moderation[mediaType] = {};
  122. room.av_moderation_actors[mediaType] = occupant.nick;
  123. end
  124. else
  125. enabled = false;
  126. if not room.av_moderation or not room.av_moderation[mediaType] then
  127. module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
  128. return true;
  129. else
  130. room.av_moderation[mediaType] = nil;
  131. room.av_moderation_actors[mediaType] = nil;
  132. -- clears room.av_moderation if empty
  133. local is_empty = false;
  134. for key,_ in pairs(room.av_moderation) do
  135. if room.av_moderation[key] then
  136. is_empty = true;
  137. end
  138. end
  139. if is_empty then
  140. room.av_moderation = nil;
  141. end
  142. end
  143. end
  144. -- send message to all occupants
  145. notify_occupants_enable(nil, enabled, room, occupant.nick, mediaType);
  146. return true;
  147. elseif moderation_command.attr.jidToWhitelist and room.av_moderation then
  148. local occupant_jid = moderation_command.attr.jidToWhitelist;
  149. -- check if jid is in the room, if so add it to whitelist
  150. -- inform all moderators and admins and the jid
  151. local occupant_to_add = room:get_occupant_by_nick(occupant_jid);
  152. if not occupant_to_add then
  153. module:log('warn', 'No occupant %s found for %s', occupant_jid, room.jid);
  154. return false;
  155. end
  156. local whitelist = room.av_moderation[mediaType];
  157. if not whitelist then
  158. whitelist = {};
  159. room.av_moderation[mediaType] = whitelist;
  160. end
  161. table.insert(whitelist, occupant_jid);
  162. notify_whitelist_change(occupant_to_add.jid, true, room, mediaType);
  163. return true;
  164. end
  165. end
  166. -- return error
  167. return false
  168. end
  169. -- handles new occupants to inform them about the state enabled/disabled, new moderators also get and the whitelist
  170. function occupant_joined(event)
  171. local room, occupant = event.room, event.occupant;
  172. if is_healthcheck_room(room.jid) then
  173. return;
  174. end
  175. if room.av_moderation then
  176. for _,mediaType in pairs({'audio', 'video'}) do
  177. if room.av_moderation[mediaType] then
  178. notify_occupants_enable(
  179. occupant.jid, true, room, room.av_moderation_actors[mediaType], mediaType);
  180. end
  181. end
  182. -- NOTE for some reason event.occupant.role is not reflecting the actual occupant role (when changed
  183. -- from allowners module) but iterating over room occupants returns the correct role
  184. for _, room_occupant in room:each_occupant() do
  185. -- if moderator send the whitelist
  186. if room_occupant.nick == occupant.nick and room_occupant.role == 'moderator' then
  187. notify_whitelist_change(room_occupant.jid, false, room);
  188. end
  189. end
  190. end
  191. end
  192. -- when a occupant was granted moderator we need to update him with the whitelist
  193. function occupant_affiliation_changed(event)
  194. -- the actor can be nil if is coming from allowners or similar module we want to skip it here
  195. -- as we will handle it in occupant_joined
  196. if event.actor and event.affiliation == 'owner' and event.room.av_moderation then
  197. local room = event.room;
  198. -- event.jid is the bare jid of participant
  199. for _, occupant in room:each_occupant() do
  200. if occupant.bare_jid == event.jid then
  201. notify_whitelist_change(occupant.jid, false, room);
  202. end
  203. end
  204. end
  205. end
  206. -- we will receive messages from the clients
  207. module:hook('message/host', on_message);
  208. -- executed on every host added internally in prosody, including components
  209. function process_host(host)
  210. if host == muc_component_host then -- the conference muc component
  211. module:log('info','Hook to muc events on %s', host);
  212. local muc_module = module:context(host);
  213. muc_module:hook('muc-occupant-joined', occupant_joined, -2); -- make sure it runs after allowners or similar
  214. muc_module:hook('muc-set-affiliation', occupant_affiliation_changed, -1);
  215. end
  216. end
  217. if prosody.hosts[muc_component_host] == nil then
  218. module:log('info', 'No muc component found, will listen for it: %s', muc_component_host);
  219. -- when a host or component is added
  220. prosody.events.add_handler('host-activated', process_host);
  221. else
  222. process_host(muc_component_host);
  223. end