Changeset

6224:2a9aff163545

plugins/muc: Move subject code to seperate module
author daurnimator <quae@daurnimator.com>
date Wed, 16 Apr 2014 13:54:51 -0400
parents 6223:2a7ce69844ca
children 6225:12537f1c1fec
files plugins/muc/muc.lib.lua plugins/muc/subject.lib.lua
diffstat 2 files changed, 100 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/muc/muc.lib.lua	Tue Apr 15 18:20:56 2014 -0400
+++ b/plugins/muc/muc.lib.lua	Wed Apr 16 13:54:51 2014 -0400
@@ -296,28 +296,6 @@
 	return reply;
 end
 
-function room_mt:get_subject()
-	return self._data.subject_from, self._data.subject;
-end
-local function create_subject_message(from, subject)
-	return st.message({from = from; type = "groupchat"})
-		:tag('subject'):text(subject):up();
-end
-function room_mt:send_subject(to)
-	local msg = create_subject_message(self:get_subject());
-	msg.attr.to = to;
-	self:route_stanza(msg);
-end
-function room_mt:set_subject(current_nick, subject)
-	if subject == "" then subject = nil; end
-	self._data['subject'] = subject;
-	self._data['subject_from'] = current_nick;
-	if self.save then self:save(); end
-	local msg = create_subject_message(current_nick, subject);
-	self:broadcast_message(msg);
-	return true;
-end
-
 function room_mt:handle_kickable(origin, stanza)
 	local real_jid = stanza.attr.from;
 	local occupant = self:get_occupant_by_real_jid(real_jid);
@@ -362,16 +340,6 @@
 function room_mt:set_public(public)
 	return self:set_hidden(not public);
 end
-function room_mt:set_changesubject(changesubject)
-	changesubject = changesubject and true or nil;
-	if self._data.changesubject ~= changesubject then
-		self._data.changesubject = changesubject;
-		if self.save then self:save(true); end
-	end
-end
-function room_mt:get_changesubject()
-	return self._data.changesubject;
-end
 
 -- Give the room creator owner affiliation
 module:hook("muc-room-pre-create", function(event)
@@ -400,11 +368,6 @@
 	end);
 end, 80);
 
--- Send subject to joining user
-module:hook("muc-occupant-joined", function(event)
-	event.room:send_subject(event.stanza.attr.from);
-end, 20);
-
 function room_mt:handle_presence_to_occupant(origin, stanza)
 	local type = stanza.attr.type;
 	if type == "error" then -- error, kick em out!
@@ -668,14 +631,6 @@
 end);
 module:hook("muc-config-form", function(event)
 	table.insert(event.form, {
-		name = 'muc#roomconfig_changesubject',
-		type = 'boolean',
-		label = 'Allow Occupants to Change Subject?',
-		value = event.room:get_changesubject()
-	});
-end);
-module:hook("muc-config-form", function(event)
-	table.insert(event.form, {
 		name = 'muc#roomconfig_moderatedroom',
 		type = 'boolean',
 		label = 'Make Room Moderated?',
@@ -729,9 +684,6 @@
 module:hook("muc-config-submitted", function(event)
 	event.update_option("public", "muc#roomconfig_publicroom");
 end);
-module:hook("muc-config-submitted", function(event)
-	event.update_option("changesubject", "muc#roomconfig_changesubject");
-end);
 
 -- Removes everyone from the room
 function room_mt:clear(x)
@@ -878,19 +830,6 @@
 	end
 end
 
-module:hook("muc-subject-change", function(event)
-	local room, stanza = event.room, event.stanza;
-	local occupant = room:get_occupant_by_real_jid(stanza.attr.from);
-	if occupant.role == "moderator" or
-		( occupant.role == "participant" and room:get_changesubject() ) then -- and participant
-		local subject = stanza:get_child_text("subject");
-		room:set_subject(occupant.nick, subject);
-	else
-		event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
-	end
-	return true;
-end);
-
 function room_mt:handle_groupchat_to_room(origin, stanza)
 	-- Prosody has made the decision that messages with <subject/> are exclusively subject changes
 	-- e.g. body will be ignored; even if the subject change was not allowed
@@ -1204,6 +1143,13 @@
 room_mt.get_persistent = persistent.get;
 room_mt.set_persistent = persistent.set;
 
+local subject = module:require "muc/subject";
+room_mt.get_changesubject = subject.get_changesubject;
+room_mt.set_changesubject = subject.set_changesubject;
+room_mt.get_subject = subject.get;
+room_mt.set_subject = subject.set;
+room_mt.send_subject = subject.send;
+
 local history = module:require "muc/history";
 room_mt.send_history = history.send;
 room_mt.get_historylength = history.get_length;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/subject.lib.lua	Wed Apr 16 13:54:51 2014 -0400
@@ -0,0 +1,93 @@
+-- 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 st = require "util.stanza";
+
+local function create_subject_message(from, subject)
+	return st.message({from = from; type = "groupchat"})
+		:tag("subject"):text(subject):up();
+end
+
+local function get_changesubject(room)
+	return room._data.changesubject;
+end
+
+local function set_changesubject(room, changesubject)
+	changesubject = changesubject and true or nil;
+	if get_changesubject(room) == changesubject then return false; end
+	room._data.changesubject = changesubject;
+	if room.save then room:save(true); end
+	return true;
+end
+
+module:hook("muc-config-form", function(event)
+	table.insert(event.form, {
+		name = "muc#roomconfig_changesubject";
+		type = "boolean";
+		label = "Allow Occupants to Change Subject?";
+		value = get_changesubject(event.room);
+	});
+end);
+
+module:hook("muc-config-submitted", function(event)
+	local new = event.fields["muc#roomconfig_changesubject"];
+	if new ~= nil and set_changesubject(event.room, new) then
+		event.status_codes["104"] = true;
+	end
+end);
+
+local function get_subject(room)
+	return room._data.subject_from, room._data.subject;
+end
+
+local function send_subject(room, to)
+	local msg = create_subject_message(get_subject(room));
+	msg.attr.to = to;
+	room:route_stanza(msg);
+end
+
+local function set_subject(room, from, subject)
+	if subject == "" then subject = nil; end
+	local old_from, old_subject = get_subject(room);
+	if old_subject == subject and old_from == from then return false; end
+	room._data.subject_from = from;
+	room._data.subject = subject;
+	if room.save then room:save(); end
+	local msg = create_subject_message(from, subject);
+	room:broadcast_message(msg);
+	return true;
+end
+
+-- Send subject to joining user
+module:hook("muc-occupant-joined", function(event)
+	event.room:send_subject(event.stanza.attr.from);
+end, 20);
+
+-- Role check for subject changes
+module:hook("muc-subject-change", function(event)
+	local room, stanza = event.room, event.stanza;
+	local occupant = room:get_occupant_by_real_jid(stanza.attr.from);
+	if occupant.role == "moderator" or
+		( occupant.role == "participant" and get_changesubject(room) ) then -- and participant
+		local subject = stanza:get_child_text("subject");
+		set_subject(room, occupant.nick, subject);
+		return true;
+	else
+		event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
+		return true;
+	end
+end);
+
+return {
+	get_changesubject = get_changesubject;
+	set_changesubject = set_changesubject;
+	get = get_subject;
+	set = set_subject;
+	send = send_subject;
+};