Changeset

1314:cc9831033f5d

mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable
author Kim Alvefur <zash@zash.se>
date Thu, 20 Feb 2014 20:37:26 +0100
parents 1313:440d7276ca62
children 1315:e8eebf281405
files mod_mam_muc/mod_mam_muc.lua
diffstat 1 files changed, 39 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_mam_muc/mod_mam_muc.lua	Thu Feb 20 20:22:35 2014 +0100
+++ b/mod_mam_muc/mod_mam_muc.lua	Thu Feb 20 20:37:26 2014 +0100
@@ -42,7 +42,38 @@
 
 local rooms = hosts[module.host].modules.muc.rooms;
 
-if not log_all_rooms then
+local send_history, save_to_history;
+
+if log_all_rooms then
+	-- Override history methods for all rooms.
+	local _send_history = room_mt.send_history;
+	local _save_to_history = room_mt.save_to_history;
+	function module.load()
+		room_mt.send_history = send_history;
+		room_mt.save_to_history = save_to_history;
+	end
+	function module.unload()
+		room_mt.send_history = _send_history;
+		room_mt.save_to_history = _save_to_history;
+	end
+else
+	-- Only override histary on rooms with logging enabled
+	function module.load()
+		for _, room in pairs(rooms) do
+			if room._data.logging then
+				room.send_history = send_history;
+				room.save_to_history = save_to_history;
+			end
+		end
+	end
+	function module.unload()
+		for _, room in pairs(rooms) do
+			if room.send_history == send_history then
+				room.send_history = nil;
+				room.save_to_history = nil;
+			end
+		end
+	end
 	module:hook("muc-config-form", function(event)
 		local room, form = event.room, event.form;
 		local logging_enabled = room._data.logging;
@@ -69,6 +100,13 @@
 			else
 				event.changed = true;
 			end
+			if new then
+				room.send_history = send_history;
+				room.save_to_history = save_to_history;
+			else
+				room.send_history = nil;
+				room.save_to_history = nil;
+			end
 		end
 	end);
 end