1 local st
= require
"util.stanza";
2 local jid
= require
"util.jid";
3 local nodeprep
= require
"util.encodings".stringprep
.nodeprep
;
5 local unprepped_access_lists
= module
:get_option("muc_access_lists", {});
6 local access_lists
= {};
8 -- Make sure all input is prepped
9 for unprepped_room_name
, unprepped_list
in pairs(unprepped_access_lists
) do
10 local prepped_room_name
= nodeprep(unprepped_room_name
);
11 if not prepped_room_name
then
12 module
:log("error", "Invalid room name: %s", unprepped_room_name
);
14 local prepped_list
= {};
15 for _
, unprepped_jid
in ipairs(unprepped_list
) do
16 local prepped_jid
= jid
.prep(unprepped_jid
);
17 if not prepped_jid
then
18 module
:log("error", "Invalid JID: %s", unprepped_jid
);
20 prepped_list
[prepped_jid
] = true;
23 access_lists
[prepped_room_name
] = prepped_list
;
27 local function is_restricted(room
, who
)
28 local allowed
= access_lists
[room
];
30 if allowed
== nil or allowed
[who
] or allowed
[select(2, jid
.split(who
))] then
37 module
:hook("presence/full", function(event
)
38 local stanza
= event
.stanza
;
40 if stanza
.name
== "presence" and stanza
.attr
.type == "unavailable" then -- Leaving events get discarded
45 local room
= jid
.split(stanza
.attr
.to
);
46 if not room
then return; end
48 -- Get who has tried to join it
49 local who
= jid
.bare(stanza
.attr
.from
)
51 -- Checking whether room is restricted
52 local check_restricted
= is_restricted(room
, who
)
53 if check_restricted
~= nil then
54 event
.allowed
= false;
55 event
.stanza
.attr
.type = 'error';
56 return event
.origin
.send(st
.error_reply(event
.stanza
, "cancel", "forbidden", "You're not allowed to enter this room: " .. check_restricted
));