Comparison

mod_muc_restrict_rooms/mod_muc_restrict_rooms.lua @ 1613:ca04f75958f7

mod_muc_restrict_rooms: Some fixes based on Matthew's comments + a few more
author Nicolás Kovac <nkneumann(at)gmail.com>
date Fri, 20 Feb 2015 21:28:39 +0000
parent 1612:247e6e43843e
child 1614:79adec50b24d
comparison
equal deleted inserted replaced
1612:247e6e43843e 1613:ca04f75958f7
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 local jid = require "util.jid";
2 local nodeprep = require "util.encodings".stringprep.nodeprep; 3 local nodeprep = require "util.encodings".stringprep.nodeprep;
3 4
4 local rooms = module:shared "muc/rooms"; 5 local rooms = module:shared "muc/rooms";
5 if not rooms then 6 if not rooms then
6 module:log("error", "This module only works on MUC components!"); 7 module:log("error", "This module only works on MUC components!");
7 return; 8 return;
8 end 9 end
9 10
10 local admins = module:get_option_set("admins", {});
11 local restrict_patterns = module:get_option("muc_restrict_matching", {}); 11 local restrict_patterns = module:get_option("muc_restrict_matching", {});
12 local restrict_excepts = module:get_option_set("muc_restrict_exceptions", {}); 12 local restrict_excepts = module:get_option_set("muc_restrict_exceptions", {});
13 local restrict_allow_admins = module:get_option_set("muc_restrict_allow_admins", false); 13 local restrict_allow_admins = module:get_option_boolean("muc_restrict_allow_admins", false);
14 14
15 local function is_restricted(room, who) 15 local function is_restricted(room, who)
16 -- If admins can join prohibited rooms, we allow them to 16 -- If admins can join prohibited rooms, we allow them to
17 if (restrict_allow_admins == true) and (admins:contains(who)) then 17 if restrict_allow_admins and usermanager.is_admin(who, module.host) then
18 module:log("debug", "Admins are allowed to enter restricted rooms (%s on %s)", who, room) 18 module:log("debug", "Admins are allowed to enter restricted rooms (%s on %s)", who, room)
19 return false; 19 return nil;
20 end 20 end
21 21
22 -- Don't evaluate exceptions 22 -- Don't evaluate exceptions
23 if restrict_excepts:contains(room:lower()) then 23 if restrict_excepts:contains(room) then
24 module:log("debug", "Room %s is amongst restriction exceptions", room:lower()) 24 module:log("debug", "Room %s is amongst restriction exceptions", room())
25 return false; 25 return nil;
26 end 26 end
27 27
28 -- Evaluate regexps of restricted patterns 28 -- Evaluate regexps of restricted patterns
29 for pattern,reason in pairs(restrict_patterns) do 29 for pattern,reason in pairs(restrict_patterns) do
30 if room:match(pattern) then 30 if room:match(pattern) then
42 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Leaving events get discarded 42 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Leaving events get discarded
43 return; 43 return;
44 end 44 end
45 45
46 -- Get the room 46 -- Get the room
47 local room = stanza.attr.from:match("([^@]+)@[^@]+") 47 local room = jid.split(stanza.attr.from);
48 if not room then return; end 48 if not room then return; end
49 49
50 -- Get who has tried to join it 50 -- Get who has tried to join it
51 local who = stanza.attr.to:match("([^\/]+)\/[^\/]+") 51 local who = jid.bare(stanza.attr.to)
52 52
53 -- Checking whether room is restricted 53 -- Checking whether room is restricted
54 local check_restricted = is_restricted(room, who) 54 local check_restricted = is_restricted(room, who)
55 if check_restricted ~= nil then 55 if check_restricted ~= nil then
56 event.allowed = false; 56 event.allowed = false;