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_av_moderation_component.lua 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. if not room.av_moderation then
  120. room.av_moderation = {};
  121. room.av_moderation_actors = {};
  122. end
  123. room.av_moderation[mediaType] = {};
  124. room.av_moderation_actors[mediaType] = occupant.nick;
  125. end
  126. else
  127. enabled = false;
  128. if not room.av_moderation then
  129. module:log('warn', 'Concurrent moderator enable/disable request or something is out of sync');
  130. return true;
  131. else
  132. room.av_moderation[mediaType] = nil;
  133. room.av_moderation_actors[mediaType] = nil;
  134. -- clears room.av_moderation if empty
  135. local is_empty = true;
  136. for key,_ in pairs(room.av_moderation) do
  137. if room.av_moderation[key] then
  138. is_empty = false;
  139. end
  140. end
  141. if is_empty then
  142. room.av_moderation = nil;
  143. end
  144. end
  145. end
  146. -- send message to all occupants
  147. notify_occupants_enable(nil, enabled, room, occupant.nick, mediaType);
  148. return true;
  149. elseif moderation_command.attr.jidToWhitelist and room.av_moderation then
  150. local occupant_jid = moderation_command.attr.jidToWhitelist;
  151. -- check if jid is in the room, if so add it to whitelist
  152. -- inform all moderators and admins and the jid
  153. local occupant_to_add = room:get_occupant_by_nick(occupant_jid);
  154. if not occupant_to_add then
  155. module:log('warn', 'No occupant %s found for %s', occupant_jid, room.jid);
  156. return false;
  157. end
  158. local whitelist = room.av_moderation[mediaType];
  159. if not whitelist then
  160. whitelist = {};
  161. room.av_moderation[mediaType] = whitelist;
  162. end
  163. table.insert(whitelist, occupant_jid);
  164. notify_whitelist_change(occupant_to_add.jid, true, room, mediaType);
  165. return true;
  166. end
  167. end
  168. -- return error
  169. return false
  170. end
  171. -- handles new occupants to inform them about the state enabled/disabled, new moderators also get and the whitelist
  172. function occupant_joined(event)
  173. local room, occupant = event.room, event.occupant;
  174. if is_healthcheck_room(room.jid) then
  175. return;
  176. end
  177. if room.av_moderation then
  178. for _,mediaType in pairs({'audio', 'video'}) do
  179. if room.av_moderation[mediaType] then
  180. notify_occupants_enable(
  181. occupant.jid, true, room, room.av_moderation_actors[mediaType], mediaType);
  182. end
  183. end
  184. -- NOTE for some reason event.occupant.role is not reflecting the actual occupant role (when changed
  185. -- from allowners module) but iterating over room occupants returns the correct role
  186. for _, room_occupant in room:each_occupant() do
  187. -- if moderator send the whitelist
  188. if room_occupant.nick == occupant.nick and room_occupant.role == 'moderator' then
  189. notify_whitelist_change(room_occupant.jid, false, room);
  190. end
  191. end
  192. end
  193. end
  194. -- when a occupant was granted moderator we need to update him with the whitelist
  195. function occupant_affiliation_changed(event)
  196. -- the actor can be nil if is coming from allowners or similar module we want to skip it here
  197. -- as we will handle it in occupant_joined
  198. if event.actor and event.affiliation == 'owner' and event.room.av_moderation then
  199. local room = event.room;
  200. -- event.jid is the bare jid of participant
  201. for _, occupant in room:each_occupant() do
  202. if occupant.bare_jid == event.jid then
  203. notify_whitelist_change(occupant.jid, false, room);
  204. end
  205. end
  206. end
  207. end
  208. -- we will receive messages from the clients
  209. module:hook('message/host', on_message);
  210. -- executed on every host added internally in prosody, including components
  211. function process_host(host)
  212. if host == muc_component_host then -- the conference muc component
  213. module:log('info','Hook to muc events on %s', host);
  214. local muc_module = module:context(host);
  215. muc_module:hook('muc-occupant-joined', occupant_joined, -2); -- make sure it runs after allowners or similar
  216. muc_module:hook('muc-set-affiliation', occupant_affiliation_changed, -1);
  217. end
  218. end
  219. if prosody.hosts[muc_component_host] == nil then
  220. module:log('info', 'No muc component found, will listen for it: %s', muc_component_host);
  221. -- when a host or component is added
  222. prosody.events.add_handler('host-activated', process_host);
  223. else
  224. process_host(muc_component_host);
  225. end