Diff

plugins/muc/muc.lib.lua @ 4797:e239668aa6d2

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Sun, 29 Apr 2012 02:10:55 +0100
parent 4785:36234dc4b177
child 4839:4905aed00382
line wrap: on
line diff
--- a/plugins/muc/muc.lib.lua	Sun Apr 29 02:09:12 2012 +0100
+++ b/plugins/muc/muc.lib.lua	Sun Apr 29 02:10:55 2012 +0100
@@ -9,7 +9,6 @@
 local select = select;
 local pairs, ipairs = pairs, ipairs;
 
-local datamanager = require "util.datamanager";
 local datetime = require "util.datetime";
 
 local dataform = require "util.dataforms";
@@ -19,7 +18,6 @@
 local jid_prep = require "util.jid".prep;
 local st = require "util.stanza";
 local log = require "util.logger".init("mod_muc");
-local multitable_new = require "util.multitable".new;
 local t_insert, t_remove = table.insert, table.remove;
 local setmetatable = setmetatable;
 local base64 = require "util.encodings".base64;
@@ -133,12 +131,11 @@
 		stanza = st.clone(stanza);
 		stanza.attr.to = "";
 		local stamp = datetime.datetime();
-		local chars = #tostring(stanza);
 		stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = stamp}):up(); -- XEP-0203
 		stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
 		local entry = { stanza = stanza, stamp = stamp };
 		t_insert(history, entry);
-		while #history > (self._data.history_length or default_history_length) do t_remove(history, 1) end
+		while #history > self._data.history_length do t_remove(history, 1) end
 	end
 end
 function room_mt:broadcast_except_nick(stanza, nick)
@@ -185,7 +182,6 @@
 
 		local n = 0;
 		local charcount = 0;
-		local stanzacount = 0;
 		
 		for i=#history,1,-1 do
 			local entry = history[i];
@@ -339,6 +335,21 @@
 function room_mt:get_changesubject()
 	return self._data.changesubject;
 end
+function room_mt:get_historylength()
+	return self._data.history_length or default_history_length;
+end
+function room_mt:set_historylength(length)
+	if tonumber(length) == nil then
+		return
+	end
+	length = tonumber(length);
+	log("debug", "max_history_length %s", self._data.max_history_length or "nil");
+	if self._data.max_history_length and length > self._data.max_history_length then
+		length = self._data.max_history_length
+	end
+	self._data.history_length = length;
+end
+
 
 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
 	local from, to = stanza.attr.from, stanza.attr.to;
@@ -608,6 +619,12 @@
 			type = 'boolean',
 			label = 'Make Room Members-Only?',
 			value = self:is_members_only()
+		},
+		{
+			name = 'muc#roomconfig_historylength',
+			type = 'text-single',
+			label = 'Maximum Number of History Messages Returned by Room',
+			value = tostring(self:get_historylength())
 		}
 	});
 end
@@ -659,6 +676,11 @@
 	dirty = dirty or (self:get_changesubject() ~= (not changesubject and true or nil))
 	module:log('debug', 'changesubject=%s', changesubject and "true" or "false")
 
+	local historylength = fields['muc#roomconfig_historylength'];
+	dirty = dirty or (self:get_historylength() ~= (historylength and true or nil))
+	module:log('debug', 'historylength=%s', historylength)
+
+
 	local whois = fields['muc#roomconfig_whois'];
 	if not valid_whois[whois] then
 	    origin.send(st.error_reply(stanza, 'cancel', 'bad-request', "Invalid value for 'whois'"));
@@ -677,6 +699,7 @@
 	self:set_persistent(persistent);
 	self:set_hidden(not public);
 	self:set_changesubject(changesubject);
+	self:set_historylength(historylength);
 
 	if self.save then self:save(true); end
 	origin.send(st.reply(stanza));
@@ -828,7 +851,6 @@
 		end
 	elseif stanza.name == "message" and type == "groupchat" then
 		local from, to = stanza.attr.from, stanza.attr.to;
-		local room = jid_bare(to);
 		local current_nick = self._jid_nick[from];
 		local occupant = self._occupants[current_nick];
 		if not occupant then -- not in room
@@ -848,7 +870,7 @@
 					origin.send(st.error_reply(stanza, "cancel", "forbidden"));
 				end
 			else
-				self:broadcast_message(stanza, true);
+				self:broadcast_message(stanza, self:get_historylength() > 0);
 			end
 			stanza.attr.from = from;
 		end
@@ -1102,7 +1124,8 @@
 		_occupants = {};
 		_data = {
 		    whois = 'moderators';
-		    history_length = (config and config.history_length);
+		    history_length = (config and config.max_history_length) or default_history_length;
+		    max_history_length = (config and config.max_history_length) or default_history_length;
 		};
 		_affiliations = {};
 	}, room_mt);