瀏覽代碼

feat: Move checks for moderator in pre-join and filter extra presences.

We will filter the initial presence where participant is announced as `participant` and shortly after that we send a second presence with the new `moderator` role.
master
damencho 4 年之前
父節點
當前提交
b559cb8ec6

+ 51
- 3
resources/prosody-plugins/mod_muc_allowners.lua 查看文件

@@ -1,8 +1,12 @@
1
+local filters = require 'util.filters';
1 2
 local jid = require "util.jid";
3
+local jid_bare = require "util.jid".bare;
2 4
 local um_is_admin = require "core.usermanager".is_admin;
3 5
 local util = module:require "util";
4 6
 local is_healthcheck_room = util.is_healthcheck_room;
5 7
 local extract_subdomain = util.extract_subdomain;
8
+local presence_check_status = util.presence_check_status;
9
+local MUC_NS = 'http://jabber.org/protocol/muc';
6 10
 
7 11
 local moderated_subdomains;
8 12
 local moderated_rooms;
@@ -17,6 +21,11 @@ local function is_admin(jid)
17 21
     return um_is_admin(jid, module.host);
18 22
 end
19 23
 
24
+-- List of the bare_jids of all occupants that are currently joining (went through pre-join) and will be promoted
25
+-- as moderators. As pre-join (where added) and joined event (where removed) happen one after another this list should
26
+-- have length of 1
27
+local joining_moderator_participants = {};
28
+
20 29
 -- Checks whether the jid is moderated, the room name is in moderated_rooms
21 30
 -- or if the subdomain is in the moderated_subdomains
22 31
 -- @return returns on of the:
@@ -43,10 +52,10 @@ local function is_moderated(room_jid)
43 52
     return false;
44 53
 end
45 54
 
46
-module:hook("muc-occupant-joined", function (event)
55
+module:hook("muc-occupant-pre-join", function (event)
47 56
     local room, occupant = event.room, event.occupant;
48 57
 
49
-    if is_healthcheck_room(room.jid) or is_admin(occupant.jid) then
58
+    if is_healthcheck_room(room.jid) or is_admin(occupant.bare_jid) then
50 59
         return;
51 60
     end
52 61
 
@@ -71,7 +80,20 @@ module:hook("muc-occupant-joined", function (event)
71 80
         end
72 81
     end
73 82
 
74
-    room:set_affiliation(true, occupant.bare_jid, "owner");
83
+    -- mark this participant that it will be promoted and is currently joining
84
+    joining_moderator_participants[occupant.bare_jid] = true;
85
+end, 2);
86
+
87
+module:hook("muc-occupant-joined", function (event)
88
+    local room, occupant = event.room, event.occupant;
89
+
90
+    local promote_to_moderator = joining_moderator_participants[occupant.bare_jid];
91
+    -- clear it
92
+    joining_moderator_participants[occupant.bare_jid] = nil;
93
+
94
+    if promote_to_moderator ~= nil then
95
+        room:set_affiliation(true, occupant.bare_jid, "owner");
96
+    end
75 97
 end, 2);
76 98
 
77 99
 module:hook("muc-occupant-left", function (event)
@@ -85,3 +107,29 @@ module:hook("muc-occupant-left", function (event)
85 107
 end, 2);
86 108
 
87 109
 module:hook_global('config-reloaded', load_config);
110
+
111
+-- Filters self-presences to a jid that exist in joining_participants array
112
+-- We want to filter those presences where we send first `participant` and just after it `moderator`
113
+function filter_stanza(stanza)
114
+    if not stanza.attr or not stanza.attr.to or stanza.name ~= "presence" then
115
+        return stanza;
116
+    end
117
+
118
+    -- Allow self-presence (code=110)
119
+    local bare_to = jid_bare(stanza.attr.to);
120
+
121
+    if joining_moderator_participants[bare_to] then
122
+        if presence_check_status(stanza:get_child('x', MUC_NS..'#user'), '110') then
123
+            return nil;
124
+        end
125
+    end
126
+
127
+    return stanza;
128
+end
129
+function filter_session(session)
130
+    -- domain mapper is filtering on default priority 0, and we need it after that
131
+    filters.add_filter(session, 'stanzas/out', filter_stanza, -1);
132
+end
133
+
134
+-- enable filtering presences
135
+filters.add_filter_hook(filter_session);

+ 5
- 19
resources/prosody-plugins/mod_muc_lobby_rooms.lua 查看文件

@@ -38,7 +38,9 @@ local NOTIFY_LOBBY_ENABLED = 'LOBBY-ENABLED';
38 38
 local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
39 39
 local NOTIFY_LOBBY_ACCESS_DENIED = 'LOBBY-ACCESS-DENIED';
40 40
 
41
-local is_healthcheck_room = module:require 'util'.is_healthcheck_room;
41
+local util = module:require "util";
42
+local is_healthcheck_room = util.is_healthcheck_room;
43
+local presence_check_status = util.presence_check_status;
42 44
 
43 45
 local main_muc_component_config = module:get_option_string('main_muc');
44 46
 if main_muc_component_config == nil then
@@ -63,21 +65,6 @@ load_config();
63 65
 local lobby_muc_service;
64 66
 local main_muc_service;
65 67
 
66
-function check_status(muc_x, status)
67
-    if not muc_x then
68
-        return false;
69
-    end
70
-
71
-    for statusNode in muc_x:childtags('status') do
72
-        if statusNode.attr.code == status then
73
-            return true;
74
-        end
75
-    end
76
-
77
-    return false;
78
-end
79
-
80 68
 function broadcast_json_msg(room, from, json_msg)
81 69
     json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
82 70
 
@@ -124,9 +111,7 @@ function filter_stanza(stanza)
124 111
 
125 112
     if from_domain == lobby_muc_component_config then
126 113
         if stanza.name == 'presence' then
127
-            local muc_x = stanza:get_child('x', MUC_NS..'#user');
128
-
129
-            if check_status(muc_x, '110') then
114
+            if presence_check_status(stanza:get_child('x', MUC_NS..'#user'), '110') then
130 115
                 return stanza;
131 116
             end
132 117
 
@@ -252,7 +237,7 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
252 237
     -- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
253 238
     host_module:hook('muc-broadcast-presence', function (event)
254 239
         local actor, occupant, room, x = event.actor, event.occupant, event.room, event.x;
255
-        if check_status(x, '307') then
240
+        if presence_check_status(x, '307') then
256 241
             local display_name = occupant:get_presence():get_child_text(
257 242
                 'nick', 'http://jabber.org/protocol/nick');
258 243
             -- we need to notify in the main room

+ 19
- 0
resources/prosody-plugins/util.lib.lua 查看文件

@@ -324,12 +324,31 @@ function http_get_with_retry(url, retry)
324 324
     return content;
325 325
 end
326 326
 
327
+-- Checks whether there is status in the <x node
328
+-- @param muc_x the <x element from presence
329
+-- @param status checks for this status
330
+-- @returns true if the status is found, false otherwise or if no muc_x is provided.
331
+function presence_check_status(muc_x, status)
332
+    if not muc_x then
333
+        return false;
334
+    end
335
+
336
+    for statusNode in muc_x:childtags('status') do
337
+        if statusNode.attr.code == status then
338
+            return true;
339
+        end
340
+    end
341
+
342
+    return false;
343
+end
344
+
327 345
 return {
328 346
     extract_subdomain = extract_subdomain;
329 347
     is_feature_allowed = is_feature_allowed;
330 348
     is_healthcheck_room = is_healthcheck_room;
331 349
     get_room_from_jid = get_room_from_jid;
332 350
     async_handler_wrapper = async_handler_wrapper;
351
+    presence_check_status = presence_check_status;
333 352
     room_jid_match_rewrite = room_jid_match_rewrite;
334 353
     room_jid_split_subdomain = room_jid_split_subdomain;
335 354
     internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;

Loading…
取消
儲存