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