mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_muc_access_control / mod_muc_access_control.lua
blobdc0f0a0d3b605971a50213728ebdb33be429f6be
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);
13 else
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);
19 else
20 prepped_list[prepped_jid] = true;
21 end
22 end
23 access_lists[prepped_room_name] = prepped_list;
24 end
25 end
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
31 return nil;
32 end
34 return "forbidden";
35 end
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
41 return;
42 end
44 -- Get the room
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));
57 end
58 end, 10);