Comparison

mod_muc_restrict_rooms/mod_muc_restrict_rooms.lua @ 1612:247e6e43843e

Adding new mod_muc_restrict_rooms module
author Nicolás Kovac <nkneumann(at)gmail.com>
date Fri, 20 Feb 2015 18:00:56 +0000
child 1613:ca04f75958f7
comparison
equal deleted inserted replaced
1611:770236ea9678 1612:247e6e43843e
1 local st = require "util.stanza";
2 local nodeprep = require "util.encodings".stringprep.nodeprep;
3
4 local rooms = module:shared "muc/rooms";
5 if not rooms then
6 module:log("error", "This module only works on MUC components!");
7 return;
8 end
9
10 local admins = module:get_option_set("admins", {});
11 local restrict_patterns = module:get_option("muc_restrict_matching", {});
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);
14
15 local function is_restricted(room, who)
16 -- If admins can join prohibited rooms, we allow them to
17 if (restrict_allow_admins == true) and (admins:contains(who)) then
18 module:log("debug", "Admins are allowed to enter restricted rooms (%s on %s)", who, room)
19 return false;
20 end
21
22 -- Don't evaluate exceptions
23 if restrict_excepts:contains(room:lower()) then
24 module:log("debug", "Room %s is amongst restriction exceptions", room:lower())
25 return false;
26 end
27
28 -- Evaluate regexps of restricted patterns
29 for pattern,reason in pairs(restrict_patterns) do
30 if room:match(pattern) then
31 module:log("debug", "Room %s is restricted by pattern %s, user %s is not allowed to join (%s)", room, pattern, who, reason)
32 return reason;
33 end
34 end
35
36 return nil
37 end
38
39 module:hook("presence/full", function(event)
40 local stanza = event.stanza;
41
42 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Leaving events get discarded
43 return;
44 end
45
46 -- Get the room
47 local room = stanza.attr.from:match("([^@]+)@[^@]+")
48 if not room then return; end
49
50 -- Get who has tried to join it
51 local who = stanza.attr.to:match("([^\/]+)\/[^\/]+")
52
53 -- Checking whether room is restricted
54 local check_restricted = is_restricted(room, who)
55 if check_restricted ~= nil then
56 event.allowed = false;
57 event.stanza.attr.type = 'error';
58 return event.origin.send(st.error_reply(event.stanza, "cancel", "forbidden", "You're not allowed to enter this room: " .. check_restricted));
59 end
60 end, 10);