Changeset

6138:fa746d834424

plugins/muc/muc.lib: Add muc-get-history event; it uses an iterator in the event object so that messages don't need to be all in memory at once
author daurnimator <quae@daurnimator.com>
date Mon, 24 Mar 2014 13:34:06 -0400
parents 6137:7db24f237a83
children 6139:544f75256883
files plugins/muc/muc.lib.lua
diffstat 1 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/muc/muc.lib.lua	Mon Mar 24 13:10:54 2014 -0400
+++ b/plugins/muc/muc.lib.lua	Mon Mar 24 13:34:06 2014 -0400
@@ -193,13 +193,17 @@
 
 	return maxchars, maxstanzas, since
 end
--- Get history for 'to'
-function room_mt:get_history(to, maxchars, maxstanzas, since)
-	local history = self._data['history']; -- send discussion history
-	if not history then return function() end end
+
+module:hook("muc-get-history", function(event)
+	local room = event.room
+	local history = room._data['history']; -- send discussion history
+	if not history then return nil end
 	local history_len = #history
 
-	maxstanzas = maxstanzas or history_len
+	local to = event.to
+	local maxchars = event.maxchars
+	local maxstanzas = event.maxstanzas or history_len
+	local since = event.since
 	local n = 0;
 	local charcount = 0;
 	for i=history_len,1,-1 do
@@ -218,7 +222,7 @@
 	end
 
 	local i = history_len-n+1
-	return function()
+	function event:next_stanza()
 		if i > history_len then return nil end
 		local entry = history[i]
 		local msg = entry.stanza
@@ -226,10 +230,19 @@
 		i = i + 1
 		return msg
 	end
-end
-function room_mt:send_history(to, stanza)
+	return true;
+end)
+
+function room_mt:send_history(stanza)
 	local maxchars, maxstanzas, since = parse_history(stanza)
-	for msg in self:get_history(to, maxchars, maxstanzas, since) do
+	local event = {
+		room = self;
+		to = stanza.attr.from; -- `to` is required to calculate the character count for `maxchars`
+		maxchars = maxchars, maxstanzas = maxstanzas, since = since;
+		next_stanza = function() end; -- events should define this iterator
+	}
+	module:fire_event("muc-get-history", event)
+	for msg in event.next_stanza , event do
 		self:_route_stanza(msg);
 	end
 end