MUC: Make number of stored history messages configurable with option max_history_messages (thanks michal and others who requested)

Tue, 06 Jul 2010 17:09:23 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 06 Jul 2010 17:09:23 +0100
changeset 3330
bdc325ce9fbc
parent 3329
9adafeeadecb
child 3331
57a9cf5f4259

MUC: Make number of stored history messages configurable with option max_history_messages (thanks michal and others who requested)

plugins/muc/mod_muc.lua file | annotate | diff | comparison | revisions
plugins/muc/muc.lib.lua file | annotate | diff | comparison | revisions
--- a/plugins/muc/mod_muc.lua	Mon Jul 05 12:17:09 2010 +0100
+++ b/plugins/muc/mod_muc.lua	Tue Jul 06 17:09:23 2010 +0100
@@ -31,6 +31,9 @@
 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
 local component;
 
+-- Configurable options
+local max_history_messages = module:get_option_number("max_history_messages");
+
 local function is_admin(jid)
 	return um_is_admin(jid) or um_is_admin(jid, module.host);
 end
@@ -58,15 +61,20 @@
 for jid in pairs(persistent_rooms) do
 	local node = jid_split(jid);
 	local data = datamanager.load(node, muc_host, "config") or {};
-	local room = muc_new_room(jid);
+	local room = muc_new_room(jid, {
+		history_length = max_history_messages;
+	});
 	room._data = data._data;
+	room._data.history_length = max_history_messages; --TODO: Need to allow per-room with a global limit
 	room._affiliations = data._affiliations;
 	room.route_stanza = room_route_stanza;
 	room.save = room_save;
 	rooms[jid] = room;
 end
 
-local host_room = muc_new_room(muc_host);
+local host_room = muc_new_room(muc_host, {
+	history_length = max_history_messages;
+});
 host_room.route_stanza = room_route_stanza;
 host_room.save = room_save;
 
@@ -113,7 +121,9 @@
 			local room = rooms[bare];
 			if not room then
 				if not(restrict_room_creation) or is_admin(stanza.attr.from) then
-					room = muc_new_room(bare);
+					room = muc_new_room(bare, {
+						history_length = max_history_messages;
+					});
 					room.route_stanza = room_route_stanza;
 					room.save = room_save;
 					rooms[bare] = room;
--- a/plugins/muc/muc.lib.lua	Mon Jul 05 12:17:09 2010 +0100
+++ b/plugins/muc/muc.lib.lua	Tue Jul 06 17:09:23 2010 +0100
@@ -24,7 +24,7 @@
 local md5 = require "util.hashes".md5;
 
 local muc_domain = nil; --module:get_host();
-local history_length = 20;
+local default_history_length = 20;
 
 ------------
 local function filter_xmlns_from_array(array, filters)
@@ -136,7 +136,7 @@
 		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 > 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)
@@ -972,13 +972,14 @@
 
 local _M = {}; -- module "muc"
 
-function _M.new_room(jid)
+function _M.new_room(jid, config)
 	return setmetatable({
 		jid = jid;
 		_jid_nick = {};
 		_occupants = {};
 		_data = {
-		    whois = 'moderators',
+		    whois = 'moderators';
+		    history_length = (config and config.history_length) or default_history_length;
 		};
 		_affiliations = {};
 	}, room_mt);

mercurial