Changeset

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 (2015-02-20)
parents 1611:770236ea9678
children 1613:ca04f75958f7
files mod_muc_restrict_rooms/mod_muc_restrict_rooms.lua
diffstat 1 files changed, 60 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_restrict_rooms/mod_muc_restrict_rooms.lua	Fri Feb 20 18:00:56 2015 +0000
@@ -0,0 +1,60 @@
+local st = require "util.stanza";
+local nodeprep = require "util.encodings".stringprep.nodeprep;
+
+local rooms = module:shared "muc/rooms";
+if not rooms then
+        module:log("error", "This module only works on MUC components!");
+        return;
+end
+
+local admins = module:get_option_set("admins", {});
+local restrict_patterns = module:get_option("muc_restrict_matching", {});
+local restrict_excepts = module:get_option_set("muc_restrict_exceptions", {});
+local restrict_allow_admins = module:get_option_set("muc_restrict_allow_admins", false);
+
+local function is_restricted(room, who)
+	-- If admins can join prohibited rooms, we allow them to
+	if (restrict_allow_admins == true) and (admins:contains(who)) then
+		module:log("debug", "Admins are allowed to enter restricted rooms (%s on %s)", who, room)
+		return false;
+	end
+
+	-- Don't evaluate exceptions
+	if restrict_excepts:contains(room:lower()) then
+		module:log("debug", "Room %s is amongst restriction exceptions", room:lower())
+		return false;
+	end
+
+	-- Evaluate regexps of restricted patterns
+        for pattern,reason in pairs(restrict_patterns) do
+                if room:match(pattern) then
+			module:log("debug", "Room %s is restricted by pattern %s, user %s is not allowed to join (%s)", room, pattern, who, reason)
+                        return reason;
+                end
+        end
+
+	return nil
+end
+
+module:hook("presence/full", function(event)
+        local stanza = event.stanza;
+
+        if stanza.name == "presence" and stanza.attr.type == "unavailable" then   -- Leaving events get discarded
+                return;
+        end
+
+	-- Get the room
+        local room = stanza.attr.from:match("([^@]+)@[^@]+")
+        if not room then return; end
+
+	-- Get who has tried to join it
+	local who = stanza.attr.to:match("([^\/]+)\/[^\/]+")
+
+	-- Checking whether room is restricted
+	local check_restricted = is_restricted(room, who)
+        if check_restricted ~= nil then
+                event.allowed = false;
+                event.stanza.attr.type = 'error';
+	        return event.origin.send(st.error_reply(event.stanza, "cancel", "forbidden", "You're not allowed to enter this room: " .. check_restricted));
+        end
+end, 10);