您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

mod_av_moderation_component.lua 13KB

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