# HG changeset patch # User Kim Alvefur # Date 1392925046 -3600 # Node ID cc9831033f5d56ba55f4d3c5ecc584be90dfcecb # Parent 440d7276ca62022c8835ff724127e38be4d58791 mod_mam_muc: Add and remove method overrides based on configuration instead of modifying room metatable diff -r 440d7276ca62 -r cc9831033f5d mod_mam_muc/mod_mam_muc.lua --- 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