Changeset

6208:d724289a5226

plugins/muc: Move password functions to seperate module
author daurnimator <quae@daurnimator.com>
date Wed, 02 Apr 2014 16:57:59 -0400
parents 6207:a5928fdeaf97
children 6209:cc00e78e6a31
files plugins/muc/muc.lib.lua plugins/muc/password.lib.lua
diffstat 2 files changed, 74 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/muc/muc.lib.lua	Wed Apr 02 15:56:37 2014 -0400
+++ b/plugins/muc/muc.lib.lua	Wed Apr 02 16:57:59 2014 -0400
@@ -400,9 +400,6 @@
 	event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
 end);
 module:hook("muc-disco#info", function(event)
-	event.reply:tag("feature", {var = event.room:get_password() and "muc_passwordprotected" or "muc_unsecured"}):up();
-end);
-module:hook("muc-disco#info", function(event)
 	event.reply:tag("feature", {var = event.room:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up();
 end);
 module:hook("muc-disco#info", function(event)
@@ -480,16 +477,6 @@
 function room_mt:get_name()
 	return self._data.name or jid_split(self.jid);
 end
-function room_mt:set_password(password)
-	if password == "" or type(password) ~= "string" then password = nil; end
-	if self._data.password ~= password then
-		self._data.password = password;
-		if self.save then self:save(true); end
-	end
-end
-function room_mt:get_password()
-	return self._data.password;
-end
 function room_mt:set_moderated(moderated)
 	moderated = moderated and true or nil;
 	if self._data.moderated ~= moderated then
@@ -576,21 +563,6 @@
 	event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
 end, -1);
 
-module:hook("muc-occupant-pre-join", function(event)
-	local room, stanza = event.room, event.stanza;
-	local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
-	password = password and password:get_child_text("password", "http://jabber.org/protocol/muc");
-	if not password or password == "" then password = nil; end
-	if room:get_password() ~= password then
-		local from, to = stanza.attr.from, stanza.attr.to;
-		log("debug", "%s couldn't join due to invalid password: %s", from, to);
-		local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
-		reply.tags[1].attr.code = "401";
-		event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
-		return true;
-	end
-end, -20);
-
 -- registration required for entering members-only room
 module:hook("muc-occupant-pre-join", function(event)
 	local room, stanza = event.room, event.stanza;
@@ -925,14 +897,6 @@
 end);
 module:hook("muc-config-form", function(event)
 	table.insert(event.form, {
-		name = 'muc#roomconfig_roomsecret',
-		type = 'text-private',
-		label = 'Password',
-		value = event.room:get_password() or "",
-	});
-end);
-module:hook("muc-config-form", function(event)
-	table.insert(event.form, {
 		name = 'muc#roomconfig_moderatedroom',
 		type = 'boolean',
 		label = 'Make Room Moderated?',
@@ -1023,9 +987,6 @@
 		event.status_codes[code] = true;
 	end
 end);
-module:hook("muc-config-submitted", function(event)
-	event.update_option("password", "muc#roomconfig_roomsecret");
-end);
 
 -- Removes everyone from the room
 function room_mt:clear(x)
@@ -1251,15 +1212,6 @@
 	return true;
 end
 
--- Add password to outgoing invite
-module:hook("muc-invite", function(event)
-	local password = event.room:get_password();
-	if password then
-		local x = event.stanza:get_child("x", "http://jabber.org/protocol/muc#user");
-		x:tag("password"):text(password):up();
-	end
-end);
-
 -- COMPAT: Some older clients expect this
 module:hook("muc-invite", function(event)
 	local room, stanza = event.room, event.stanza;
@@ -1509,6 +1461,10 @@
 room_mt.get_description = description.get;
 room_mt.set_description = description.set;
 
+local password = module:require "muc/password";
+room_mt.get_password = password.get;
+room_mt.set_password = password.set;
+
 local _M = {}; -- module "muc"
 
 function _M.new_room(jid, config)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/password.lib.lua	Wed Apr 02 16:57:59 2014 -0400
@@ -0,0 +1,70 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local function get_password(room)
+	return room._data.password;
+end
+
+local function set_password(room, password)
+	if password == "" then password = nil; end
+	if room._data.password == password then return false; end
+	room._data.password = password;
+	if room.save then room:save(true); end
+	return true;
+end
+
+module:hook("muc-disco#info", function(event)
+	event.reply:tag("feature", {var = get_password(event.room) and "muc_passwordprotected" or "muc_unsecured"}):up();
+end);
+
+module:hook("muc-config-form", function(event)
+	table.insert(event.form, {
+		name = "muc#roomconfig_roomsecret";
+		type = "text-private";
+		label = "Password";
+		value = get_password(event.room) or "";
+	});
+end);
+
+module:hook("muc-config-submitted", function(event)
+	local new = event.fields["muc#roomconfig_roomsecret"];
+	if new ~= nil and set_password(event.room, new) then
+		event.status_codes["104"] = true;
+	end
+end);
+
+-- Don't allow anyone to join room unless they provide the password
+module:hook("muc-occupant-pre-join", function(event)
+	local room, stanza = event.room, event.stanza;
+	local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
+	password = password and password:get_child_text("password", "http://jabber.org/protocol/muc");
+	if not password or password == "" then password = nil; end
+	if get_password(room) ~= password then
+		local from, to = stanza.attr.from, stanza.attr.to;
+		module:log("debug", "%s couldn't join due to invalid password: %s", from, to);
+		local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
+		reply.tags[1].attr.code = "401";
+		event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
+		return true;
+	end
+end, -20);
+
+-- Add password to outgoing invite
+module:hook("muc-invite", function(event)
+	local password = get_password(event.room);
+	if password then
+		local x = event.stanza:get_child("x", "http://jabber.org/protocol/muc#user");
+		x:tag("password"):text(password):up();
+	end
+end);
+
+return {
+	get = get_password;
+	set = set_password;
+};