plugins/muc/muc.lib.lua
changeset 6138 fa746d834424
parent 6137 7db24f237a83
child 6139 544f75256883
equal deleted inserted replaced
6137:7db24f237a83 6138:fa746d834424
   191 		end
   191 		end
   192 	end
   192 	end
   193 
   193 
   194 	return maxchars, maxstanzas, since
   194 	return maxchars, maxstanzas, since
   195 end
   195 end
   196 -- Get history for 'to'
   196 
   197 function room_mt:get_history(to, maxchars, maxstanzas, since)
   197 module:hook("muc-get-history", function(event)
   198 	local history = self._data['history']; -- send discussion history
   198 	local room = event.room
   199 	if not history then return function() end end
   199 	local history = room._data['history']; -- send discussion history
       
   200 	if not history then return nil end
   200 	local history_len = #history
   201 	local history_len = #history
   201 
   202 
   202 	maxstanzas = maxstanzas or history_len
   203 	local to = event.to
       
   204 	local maxchars = event.maxchars
       
   205 	local maxstanzas = event.maxstanzas or history_len
       
   206 	local since = event.since
   203 	local n = 0;
   207 	local n = 0;
   204 	local charcount = 0;
   208 	local charcount = 0;
   205 	for i=history_len,1,-1 do
   209 	for i=history_len,1,-1 do
   206 		local entry = history[i];
   210 		local entry = history[i];
   207 		if maxchars then
   211 		if maxchars then
   216 		if n + 1 > maxstanzas then break; end
   220 		if n + 1 > maxstanzas then break; end
   217 		n = n + 1;
   221 		n = n + 1;
   218 	end
   222 	end
   219 
   223 
   220 	local i = history_len-n+1
   224 	local i = history_len-n+1
   221 	return function()
   225 	function event:next_stanza()
   222 		if i > history_len then return nil end
   226 		if i > history_len then return nil end
   223 		local entry = history[i]
   227 		local entry = history[i]
   224 		local msg = entry.stanza
   228 		local msg = entry.stanza
   225 		msg.attr.to = to;
   229 		msg.attr.to = to;
   226 		i = i + 1
   230 		i = i + 1
   227 		return msg
   231 		return msg
   228 	end
   232 	end
   229 end
   233 	return true;
   230 function room_mt:send_history(to, stanza)
   234 end)
       
   235 
       
   236 function room_mt:send_history(stanza)
   231 	local maxchars, maxstanzas, since = parse_history(stanza)
   237 	local maxchars, maxstanzas, since = parse_history(stanza)
   232 	for msg in self:get_history(to, maxchars, maxstanzas, since) do
   238 	local event = {
       
   239 		room = self;
       
   240 		to = stanza.attr.from; -- `to` is required to calculate the character count for `maxchars`
       
   241 		maxchars = maxchars, maxstanzas = maxstanzas, since = since;
       
   242 		next_stanza = function() end; -- events should define this iterator
       
   243 	}
       
   244 	module:fire_event("muc-get-history", event)
       
   245 	for msg in event.next_stanza , event do
   233 		self:_route_stanza(msg);
   246 		self:_route_stanza(msg);
   234 	end
   247 	end
   235 end
   248 end
   236 function room_mt:send_subject(to)
   249 function room_mt:send_subject(to)
   237 	if self._data['subject'] then
   250 	if self._data['subject'] then