Diff

plugins/muc/members_only.lib.lua @ 8976:92f0876b9230

MUC: Add config option to allow members to invite other members to the room (previously only owners/admins could do this)
author Matthew Wild <mwild1@gmail.com>
date Fri, 06 Jul 2018 15:33:46 +0100
parent 7401:e16b3fd0bd80
child 8985:4101bcf9639a
line wrap: on
line diff
--- a/plugins/muc/members_only.lib.lua	Fri Jul 06 00:19:22 2018 +0200
+++ b/plugins/muc/members_only.lib.lua	Fri Jul 06 15:33:46 2018 +0100
@@ -47,6 +47,19 @@
 	return true;
 end
 
+local function get_allow_member_invites(room)
+	return room._data.allow_member_invites;
+end
+
+-- Allows members to invite new members into a members-only room,
+-- effectively creating an invite-only room
+local function set_allow_member_invites(room, allow_member_invites)
+	allow_member_invites = allow_member_invites and true or nil;
+	if room._data.allow_member_invites == allow_member_invites then return false; end
+	room._data.allow_member_invites = allow_member_invites;
+	return true;
+end
+
 module:hook("muc-disco#info", function(event)
 	event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
 end);
@@ -58,6 +71,12 @@
 		label = "Make Room Members-Only?";
 		value = get_members_only(event.room);
 	});
+	table.insert(event.form, {
+		name = "{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites";
+		type = "boolean";
+		label = "Allow members to invite new members?";
+		value = get_allow_member_invites(event.room);
+	});
 end, 100-6);
 
 module:hook("muc-config-submitted/muc#roomconfig_membersonly", function(event)
@@ -66,6 +85,12 @@
 	end
 end);
 
+module:hook("muc-config-submitted/{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites", function(event)
+	if set_allow_member_invites(event.room, event.value) then
+		event.status_codes["104"] = true;
+	end
+end);
+
 -- No affiliation => role of "none"
 module:hook("muc-get-default-role", function(event)
 	if not event.affiliation and get_members_only(event.room) then
@@ -96,7 +121,7 @@
 	if get_members_only(room) then
 		local stanza = event.stanza;
 		local affiliation = room:get_affiliation(stanza.attr.from);
-		if valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
+		if not room._data.allow_member_invites and valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
 			event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
 			return true;
 		end
@@ -110,13 +135,13 @@
 		local stanza = event.stanza;
 		local invitee = stanza.attr.to;
 		local affiliation = room:get_affiliation(invitee);
-		if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
+		if valid_affiliations[affiliation or "none"] <= valid_affiliations.none and room._data.allow_member_invites then
 			local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
 				:get_child("invite").attr.from;
 			module:log("debug", "%s invited %s into members only room %s, granting membership",
 				from, invitee, room.jid);
 			-- This might fail; ignore for now
-			room:set_affiliation(from, invitee, "member", "Invited by " .. from);
+			room:set_affiliation(true, invitee, "member", "Invited by " .. from);
 			room:save();
 		end
 	end
@@ -125,4 +150,6 @@
 return {
 	get = get_members_only;
 	set = set_members_only;
+	get_allow_member_invites = get_allow_member_invites;
+	set_allow_member_invites = set_allow_member_invites;
 };