Browse Source

feat: Sends json messages notifying for lobby actions. (#7209)

* feat: Sends json messages notifying for lobby actions.

* squash: Fixes quotes to be consistent.

* fix: Fixes attempt to call global 'formdecode' (a nil value).
master
Дамян Минков 5 years ago
parent
commit
b3a2905849
No account linked to committer's email address
1 changed files with 88 additions and 24 deletions
  1. 88
    24
      resources/prosody-plugins/mod_muc_lobby_rooms.lua

+ 88
- 24
resources/prosody-plugins/mod_muc_lobby_rooms.lua View File

@@ -16,23 +16,29 @@
16 16
 --     muc_room_default_public_jids = true
17 17
 --
18 18
 -- we use async to detect Prosody 0.10 and earlier
19
-local have_async = pcall(require, "util.async");
19
+local have_async = pcall(require, 'util.async');
20 20
 
21 21
 if not have_async then
22
-    module:log("warn", "Lobby rooms will not work with Prosody version 0.10 or less.");
22
+    module:log('warn', 'Lobby rooms will not work with Prosody version 0.10 or less.');
23 23
     return;
24 24
 end
25 25
 
26
+local formdecode = require "util.http".formdecode;
26 27
 local jid_split = require 'util.jid'.split;
27 28
 local jid_bare = require 'util.jid'.bare;
29
+local json = require 'util.json';
28 30
 local filters = require 'util.filters';
29 31
 local st = require 'util.stanza';
30 32
 local MUC_NS = 'http://jabber.org/protocol/muc';
31 33
 local DISCO_INFO_NS = 'http://jabber.org/protocol/disco#info';
32 34
 local DISPLAY_NAME_REQUIRED_FEATURE = 'http://jitsi.org/protocol/lobbyrooms#displayname_required';
33 35
 local LOBBY_IDENTITY_TYPE = 'lobbyrooms';
36
+local NOTIFY_JSON_MESSAGE_TYPE = 'lobby-notify';
37
+local NOTIFY_LOBBY_ENABLED = 'LOBBY-ENABLED';
38
+local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
39
+local NOTIFY_LOBBY_ACCESS_DENIED = 'LOBBY-ACCESS-DENIED';
34 40
 
35
-local is_healthcheck_room = module:require "util".is_healthcheck_room;
41
+local is_healthcheck_room = module:require 'util'.is_healthcheck_room;
36 42
 
37 43
 local main_muc_component_config = module:get_option_string('main_muc');
38 44
 if main_muc_component_config == nil then
@@ -48,23 +54,23 @@ end
48 54
 local whitelist;
49 55
 local check_display_name_required;
50 56
 local function load_config()
51
-    whitelist = module:get_option_set("muc_lobby_whitelist", {});
57
+    whitelist = module:get_option_set('muc_lobby_whitelist', {});
52 58
     check_display_name_required
53
-        = module:get_option_boolean("muc_lobby_check_display_name_required", true);
59
+        = module:get_option_boolean('muc_lobby_check_display_name_required', true);
54 60
 end
55 61
 load_config();
56 62
 
57 63
 local lobby_muc_service;
58 64
 local main_muc_service;
59 65
 
60
-function check_self_status(muc_x)
66
+-- Checks whether there is status in the <x node
67
+function check_status(muc_x, status)
61 68
     if not muc_x then
62 69
         return false;
63 70
     end
64 71
 
65
-    for status in muc_x:childtags('status') do
66
-        if status.attr.code == '110' then
72
+    for statusNode in muc_x:childtags('status') do
73
+        if statusNode.attr.code == status then
67 74
             return true;
68 75
         end
69 76
     end
@@ -72,6 +78,42 @@ function check_self_status(muc_x)
72 78
     return false;
73 79
 end
74 80
 
81
+function broadcast_json_msg(room, from, json_msg)
82
+    json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
83
+
84
+    local occupant = room:get_occupant_by_real_jid(from);
85
+    if occupant then
86
+        room:broadcast_message(
87
+            st.message({ type = 'groupchat', from = occupant.nick })
88
+              :tag('json-message', {xmlns='http://jitsi.org/jitmeet'})
89
+              :text(json.encode(json_msg)):up());
90
+    end
91
+end
92
+
93
+-- Sends a json message notifying for lobby enabled/disable
94
+-- the message from is the actor that did the operation
95
+function notify_lobby_enabled(room, actor, value)
96
+    broadcast_json_msg(room, actor, {
97
+        event = NOTIFY_LOBBY_ENABLED,
98
+        value = value
99
+    });
100
+end
101
+
102
+-- Sends a json message notifying that the jid was granted/denied access in lobby
103
+-- the message from is the actor that did the operation
104
+function notify_lobby_access(room, actor, jid, granted)
105
+    local notify_json = {
106
+        value = jid
107
+    };
108
+    if granted then
109
+        notify_json.event = NOTIFY_LOBBY_ACCESS_GRANTED;
110
+    else
111
+        notify_json.event = NOTIFY_LOBBY_ACCESS_DENIED;
112
+    end
113
+
114
+    broadcast_json_msg(room, actor, notify_json);
115
+end
116
+
75 117
 function filter_stanza(stanza)
76 118
     if not stanza.attr or not stanza.attr.from or not main_muc_service then
77 119
         return stanza;
@@ -83,7 +125,7 @@ function filter_stanza(stanza)
83 125
         if stanza.name == 'presence' then
84 126
             local muc_x = stanza:get_child('x', MUC_NS..'#user');
85 127
 
86
-            if muc_x and check_self_status(muc_x) then
128
+            if check_status(muc_x, '110') then
87 129
                 return stanza;
88 130
             end
89 131
 
@@ -142,7 +184,7 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
142 184
 
143 185
     -- Tag the disco#info response with a feature that display name is required
144 186
     -- when the conference name from the web request has a lobby enabled.
145
-    host_module:hook("host-disco-info-node", function (event)
187
+    host_module:hook('host-disco-info-node', function (event)
146 188
         local session, reply, node = event.origin, event.reply, event.node;
147 189
         if node == LOBBY_IDENTITY_TYPE
148 190
             and session.jitsi_web_query_room
@@ -151,7 +193,7 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
151 193
             local room = main_muc_service.get_room_from_jid(
152 194
                 jid_bare(session.jitsi_web_query_room .. '@' .. main_muc_component_config));
153 195
             if room and room._data.lobbyroom then
154
-                reply:tag("feature", { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
196
+                reply:tag('feature', { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
155 197
             end
156 198
         end
157 199
         event.exists = true;
@@ -173,6 +215,15 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
173 215
 
174 216
         return 'none';
175 217
     end
218
+
219
+    -- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
220
+    host_module:hook('muc-broadcast-presence', function (event)
221
+        local actor, occupant, room, x = event.actor, event.occupant, event.room, event.x;
222
+        if check_status(x, '307') then
223
+            -- we need to notify in the main room
224
+            notify_lobby_access(room.main_room, actor, occupant.jid, false);
225
+        end
226
+    end);
176 227
 end
177 228
 
178 229
 -- process or waits to process the lobby muc component
@@ -199,7 +250,7 @@ process_host_module(main_muc_component_config, function(host_module, host)
199 250
 
200 251
     -- hooks when lobby is enabled to create its room, only done here or by admin
201 252
     host_module:hook('muc-config-submitted', function(event)
202
-        local room = event.room;
253
+        local actor, room = event.actor, event.room;
203 254
         local members_only = event.fields['muc#roomconfig_membersonly'] and true or nil;
204 255
         if members_only then
205 256
             local node = jid_split(room.jid);
@@ -209,29 +260,31 @@ process_host_module(main_muc_component_config, function(host_module, host)
209 260
                 local new_room = lobby_muc_service.create_room(lobby_room_jid);
210 261
                 new_room.main_room = room;
211 262
                 room._data.lobbyroom = new_room;
212
-                event.status_codes["104"] = true;
263
+                event.status_codes['104'] = true;
264
+                notify_lobby_enabled(room, actor, true);
213 265
             end
214 266
         elseif room._data.lobbyroom then
215 267
             room._data.lobbyroom:destroy(room.jid, 'Lobby room closed.');
216 268
             room._data.lobbyroom = nil;
269
+            notify_lobby_enabled(room, actor, false);
217 270
         end
218 271
     end);
219
-    host_module:hook("muc-room-destroyed",function(event)
272
+    host_module:hook('muc-room-destroyed',function(event)
220 273
         local room = event.room;
221 274
         if room._data.lobbyroom then
222 275
             room._data.lobbyroom:destroy(nil, 'Lobby room closed.');
223 276
             room._data.lobbyroom = nil;
224 277
         end
225 278
     end);
226
-    host_module:hook("muc-disco#info", function (event)
279
+    host_module:hook('muc-disco#info', function (event)
227 280
         local room = event.room;
228 281
         if (room._data.lobbyroom and room:get_members_only()) then
229 282
             table.insert(event.form, {
230
-                name = "muc#roominfo_lobbyroom";
231
-                label = "Lobby room jid";
232
-                value = "";
283
+                name = 'muc#roominfo_lobbyroom';
284
+                label = 'Lobby room jid';
285
+                value = '';
233 286
             });
234
-            event.formdata["muc#roominfo_lobbyroom"] = room._data.lobbyroom.jid;
287
+            event.formdata['muc#roominfo_lobbyroom'] = room._data.lobbyroom.jid;
235 288
         end
236 289
     end);
237 290
 
@@ -242,7 +295,7 @@ process_host_module(main_muc_component_config, function(host_module, host)
242 295
             return;
243 296
         end
244 297
 
245
-        local join = stanza:get_child("x", MUC_NS);
298
+        local join = stanza:get_child('x', MUC_NS);
246 299
         if not join then
247 300
             return;
248 301
         end
@@ -266,7 +319,7 @@ process_host_module(main_muc_component_config, function(host_module, host)
266 319
             local affiliation = room:get_affiliation(invitee);
267 320
             if not affiliation or affiliation == 0 then
268 321
                 event.occupant.role = 'participant';
269
-                room:set_affiliation(true, invitee_bare_jid, "member");
322
+                room:set_affiliation(true, invitee_bare_jid, 'member');
270 323
                 room:save();
271 324
 
272 325
                 return;
@@ -285,6 +338,16 @@ process_host_module(main_muc_component_config, function(host_module, host)
285 338
             return true;
286 339
         end
287 340
     end, -4); -- the default hook on members_only module is on -5
341
+
342
+    -- listens for invites for participants to join the main room
343
+    host_module:hook('muc-invite', function(event)
344
+        local room, stanza = event.room, event.stanza;
345
+        local invitee = stanza.attr.to;
346
+        local from = stanza:get_child('x', 'http://jabber.org/protocol/muc#user')
347
+            :get_child('invite').attr.from;
348
+
349
+        notify_lobby_access(room, from, invitee, true);
350
+    end);
288 351
 end);
289 352
 
290 353
 -- Extract 'room' param from URL when session is created
@@ -301,10 +364,10 @@ function update_session(event)
301 364
         local params = formdecode(query);
302 365
         -- The room name and optional prefix from the web query
303 366
         session.jitsi_web_query_room = params.room;
304
-        session.jitsi_web_query_prefix = params.prefix or "";
367
+        session.jitsi_web_query_prefix = params.prefix or '';
305 368
     end
306 369
 end
307 370
 
308
-module:hook_global("bosh-session", update_session);
309
-module:hook_global("websocket-session", update_session);
371
+module:hook_global('bosh-session', update_session);
372
+module:hook_global('websocket-session', update_session);
310 373
 module:hook_global('config-reloaded', load_config);

Loading…
Cancel
Save