瀏覽代碼

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
+local filters = require 'util.filters';
1
 local jid = require "util.jid";
2
 local jid = require "util.jid";
3
+local jid_bare = require "util.jid".bare;
2
 local um_is_admin = require "core.usermanager".is_admin;
4
 local um_is_admin = require "core.usermanager".is_admin;
3
 local util = module:require "util";
5
 local util = module:require "util";
4
 local is_healthcheck_room = util.is_healthcheck_room;
6
 local is_healthcheck_room = util.is_healthcheck_room;
5
 local extract_subdomain = util.extract_subdomain;
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
 local moderated_subdomains;
11
 local moderated_subdomains;
8
 local moderated_rooms;
12
 local moderated_rooms;
17
     return um_is_admin(jid, module.host);
21
     return um_is_admin(jid, module.host);
18
 end
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
 -- Checks whether the jid is moderated, the room name is in moderated_rooms
29
 -- Checks whether the jid is moderated, the room name is in moderated_rooms
21
 -- or if the subdomain is in the moderated_subdomains
30
 -- or if the subdomain is in the moderated_subdomains
22
 -- @return returns on of the:
31
 -- @return returns on of the:
43
     return false;
52
     return false;
44
 end
53
 end
45
 
54
 
46
-module:hook("muc-occupant-joined", function (event)
55
+module:hook("muc-occupant-pre-join", function (event)
47
     local room, occupant = event.room, event.occupant;
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
         return;
59
         return;
51
     end
60
     end
52
 
61
 
71
         end
80
         end
72
     end
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
 end, 2);
97
 end, 2);
76
 
98
 
77
 module:hook("muc-occupant-left", function (event)
99
 module:hook("muc-occupant-left", function (event)
85
 end, 2);
107
 end, 2);
86
 
108
 
87
 module:hook_global('config-reloaded', load_config);
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
 local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
38
 local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
39
 local NOTIFY_LOBBY_ACCESS_DENIED = 'LOBBY-ACCESS-DENIED';
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
 local main_muc_component_config = module:get_option_string('main_muc');
45
 local main_muc_component_config = module:get_option_string('main_muc');
44
 if main_muc_component_config == nil then
46
 if main_muc_component_config == nil then
63
 local lobby_muc_service;
65
 local lobby_muc_service;
64
 local main_muc_service;
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
 function broadcast_json_msg(room, from, json_msg)
68
 function broadcast_json_msg(room, from, json_msg)
81
     json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
69
     json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
82
 
70
 
124
 
111
 
125
     if from_domain == lobby_muc_component_config then
112
     if from_domain == lobby_muc_component_config then
126
         if stanza.name == 'presence' then
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
                 return stanza;
115
                 return stanza;
131
             end
116
             end
132
 
117
 
252
     -- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
237
     -- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
253
     host_module:hook('muc-broadcast-presence', function (event)
238
     host_module:hook('muc-broadcast-presence', function (event)
254
         local actor, occupant, room, x = event.actor, event.occupant, event.room, event.x;
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
             local display_name = occupant:get_presence():get_child_text(
241
             local display_name = occupant:get_presence():get_child_text(
257
                 'nick', 'http://jabber.org/protocol/nick');
242
                 'nick', 'http://jabber.org/protocol/nick');
258
             -- we need to notify in the main room
243
             -- we need to notify in the main room

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

324
     return content;
324
     return content;
325
 end
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
 return {
345
 return {
328
     extract_subdomain = extract_subdomain;
346
     extract_subdomain = extract_subdomain;
329
     is_feature_allowed = is_feature_allowed;
347
     is_feature_allowed = is_feature_allowed;
330
     is_healthcheck_room = is_healthcheck_room;
348
     is_healthcheck_room = is_healthcheck_room;
331
     get_room_from_jid = get_room_from_jid;
349
     get_room_from_jid = get_room_from_jid;
332
     async_handler_wrapper = async_handler_wrapper;
350
     async_handler_wrapper = async_handler_wrapper;
351
+    presence_check_status = presence_check_status;
333
     room_jid_match_rewrite = room_jid_match_rewrite;
352
     room_jid_match_rewrite = room_jid_match_rewrite;
334
     room_jid_split_subdomain = room_jid_split_subdomain;
353
     room_jid_split_subdomain = room_jid_split_subdomain;
335
     internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
354
     internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;

Loading…
取消
儲存