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

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