# HG changeset patch # User Kim Alvefur # Date 1414669380 -3600 # Node ID 57fb9ce21f9c6133a7846d11ee0c8ecef0a09f35 # Parent ccb9dc624ebd809b06f2f58350ff76ea705acb90 mod_mam_muc: Add compatibility with the new MUC code in trunk diff -r ccb9dc624ebd -r 57fb9ce21f9c mod_mam_muc/mod_mam_muc.lua --- a/mod_mam_muc/mod_mam_muc.lua Thu Oct 30 12:39:57 2014 +0100 +++ b/mod_mam_muc/mod_mam_muc.lua Thu Oct 30 12:43:00 2014 +0100 @@ -16,7 +16,11 @@ local mod_muc = module:depends"muc"; local room_mt = mod_muc.room_mt; -local rooms = mod_muc.rooms; +local rooms = rawget(mod_muc, "rooms"); +local new_muc = not rooms; +if new_muc then + rooms = module:shared"muc/rooms"; +end local getmetatable = getmetatable; local function is_stanza(x) @@ -57,27 +61,29 @@ local send_history, save_to_history; -- Override history methods for all rooms. -module:hook("muc-room-created", function (event) - local room = event.room; - if logging_enabled(room) then - room.send_history = send_history; - room.save_to_history = save_to_history; - end -end); - -function module.load() - for _, room in pairs(rooms) do +if not new_muc then -- 0.10 or older + module:hook("muc-room-created", function (event) + local room = event.room; if logging_enabled(room) then room.send_history = send_history; room.save_to_history = save_to_history; end + end); + + function module.load() + for _, room in pairs(rooms) do + if logging_enabled(room) then + room.send_history = send_history; + room.save_to_history = save_to_history; + end + 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; + 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 end @@ -228,6 +234,53 @@ first = first, last = last, count = count })); end); +module:hook("muc-get-history", function (event) + local room = event.room; + if not logging_enabled(room) then return end + local room_jid = room.jid; + local maxstanzas = event.maxstanzas; + local maxchars = event.maxchars; + local since = event.since; + local to = event.to; + + -- Load all the data! + local query = { + limit = m_min(maxstanzas or 20, max_history_length); + start = since; + reverse = true; + with = "message maxchars then + break + end + charcount = charcount + chars; + end + history[i], i = item, i+1; + -- module:log("debug", tostring(item)); + end + function event:next_stanza() + i = i - 1; + return history[i]; + end + return true; +end, 1); + function send_history(self, to, stanza) local maxchars, maxstanzas, seconds, since; local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history") @@ -246,34 +299,17 @@ end end - -- Load all the data! - local data, err = archive:find(jid_split(self.jid), { - limit = m_min(maxstanzas or 20, max_history_length); - start = since; - reverse = true; - with = "message maxchars then break end - charcount = charcount + chars; - end - to_send[1+#to_send] = item; - end - for i = #to_send,1,-1 do - self:_route_stanza(to_send[i]); + module:fire_event("muc-get-history", event); + + for msg in event.next_stanza, event do + self:_route_stanza(msg); end end @@ -294,6 +330,13 @@ archive:append(room, nil, time_now(), with, stanza); end +module:hook("muc-broadcast-message", function (event) + local room, stanza = event.room, event.stanza; + if stanza:get_child("body") then + save_to_history(room, stanza); + end +end); + module:hook("muc-room-destroyed", function(event) local username = jid_split(event.room.jid); archive:delete(username);