Software /
code /
prosody-modules
Comparison
mod_mam_muc/mod_mam_muc.lua @ 1543:57fb9ce21f9c
mod_mam_muc: Add compatibility with the new MUC code in trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 Oct 2014 12:43:00 +0100 |
parent | 1542:ccb9dc624ebd |
child | 1546:fff2858c554f |
comparison
equal
deleted
inserted
replaced
1542:ccb9dc624ebd | 1543:57fb9ce21f9c |
---|---|
14 local jid_split = require "util.jid".split; | 14 local jid_split = require "util.jid".split; |
15 local dataform = require "util.dataforms".new; | 15 local dataform = require "util.dataforms".new; |
16 | 16 |
17 local mod_muc = module:depends"muc"; | 17 local mod_muc = module:depends"muc"; |
18 local room_mt = mod_muc.room_mt; | 18 local room_mt = mod_muc.room_mt; |
19 local rooms = mod_muc.rooms; | 19 local rooms = rawget(mod_muc, "rooms"); |
20 local new_muc = not rooms; | |
21 if new_muc then | |
22 rooms = module:shared"muc/rooms"; | |
23 end | |
20 | 24 |
21 local getmetatable = getmetatable; | 25 local getmetatable = getmetatable; |
22 local function is_stanza(x) | 26 local function is_stanza(x) |
23 return getmetatable(x) == st.stanza_mt; | 27 return getmetatable(x) == st.stanza_mt; |
24 end | 28 end |
55 end | 59 end |
56 | 60 |
57 local send_history, save_to_history; | 61 local send_history, save_to_history; |
58 | 62 |
59 -- Override history methods for all rooms. | 63 -- Override history methods for all rooms. |
60 module:hook("muc-room-created", function (event) | 64 if not new_muc then -- 0.10 or older |
61 local room = event.room; | 65 module:hook("muc-room-created", function (event) |
62 if logging_enabled(room) then | 66 local room = event.room; |
63 room.send_history = send_history; | |
64 room.save_to_history = save_to_history; | |
65 end | |
66 end); | |
67 | |
68 function module.load() | |
69 for _, room in pairs(rooms) do | |
70 if logging_enabled(room) then | 67 if logging_enabled(room) then |
71 room.send_history = send_history; | 68 room.send_history = send_history; |
72 room.save_to_history = save_to_history; | 69 room.save_to_history = save_to_history; |
73 end | 70 end |
74 end | 71 end); |
75 end | 72 |
76 function module.unload() | 73 function module.load() |
77 for _, room in pairs(rooms) do | 74 for _, room in pairs(rooms) do |
78 if room.send_history == send_history then | 75 if logging_enabled(room) then |
79 room.send_history = nil; | 76 room.send_history = send_history; |
80 room.save_to_history = nil; | 77 room.save_to_history = save_to_history; |
78 end | |
79 end | |
80 end | |
81 function module.unload() | |
82 for _, room in pairs(rooms) do | |
83 if room.send_history == send_history then | |
84 room.send_history = nil; | |
85 room.save_to_history = nil; | |
86 end | |
81 end | 87 end |
82 end | 88 end |
83 end | 89 end |
84 | 90 |
85 if not log_all_rooms then | 91 if not log_all_rooms then |
226 :tag("fin", { xmlns = xmlns_mam, queryid = qid }) | 232 :tag("fin", { xmlns = xmlns_mam, queryid = qid }) |
227 :add_child(rsm.generate { | 233 :add_child(rsm.generate { |
228 first = first, last = last, count = count })); | 234 first = first, last = last, count = count })); |
229 end); | 235 end); |
230 | 236 |
237 module:hook("muc-get-history", function (event) | |
238 local room = event.room; | |
239 if not logging_enabled(room) then return end | |
240 local room_jid = room.jid; | |
241 local maxstanzas = event.maxstanzas; | |
242 local maxchars = event.maxchars; | |
243 local since = event.since; | |
244 local to = event.to; | |
245 | |
246 -- Load all the data! | |
247 local query = { | |
248 limit = m_min(maxstanzas or 20, max_history_length); | |
249 start = since; | |
250 reverse = true; | |
251 with = "message<groupchat"; | |
252 } | |
253 module:log("debug", require"util.serialization".serialize(query)) | |
254 local data, err = archive:find(jid_split(room_jid), query); | |
255 | |
256 if not data then | |
257 module:log("error", "Could not fetch history: %s", tostring(err)); | |
258 return | |
259 end | |
260 | |
261 local chars = 0; | |
262 local history, i = {}, 1; | |
263 | |
264 for id, item, when in data do | |
265 item.attr.to = to; | |
266 item:tag("delay", { xmlns = "urn:xmpp:delay", from = room_jid, stamp = timestamp(when) }):up(); -- XEP-0203 | |
267 if maxchars then | |
268 chars = #tostring(item); | |
269 if chars + charcount > maxchars then | |
270 break | |
271 end | |
272 charcount = charcount + chars; | |
273 end | |
274 history[i], i = item, i+1; | |
275 -- module:log("debug", tostring(item)); | |
276 end | |
277 function event:next_stanza() | |
278 i = i - 1; | |
279 return history[i]; | |
280 end | |
281 return true; | |
282 end, 1); | |
283 | |
231 function send_history(self, to, stanza) | 284 function send_history(self, to, stanza) |
232 local maxchars, maxstanzas, seconds, since; | 285 local maxchars, maxstanzas, seconds, since; |
233 local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history") | 286 local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history") |
234 if history_tag then | 287 if history_tag then |
235 module:log("debug", tostring(history_tag)); | 288 module:log("debug", tostring(history_tag)); |
244 if seconds then | 297 if seconds then |
245 since = math.max(os.time() - seconds, since or 0); | 298 since = math.max(os.time() - seconds, since or 0); |
246 end | 299 end |
247 end | 300 end |
248 | 301 |
249 -- Load all the data! | 302 local event = { |
250 local data, err = archive:find(jid_split(self.jid), { | 303 room = self; |
251 limit = m_min(maxstanzas or 20, max_history_length); | 304 to = to; -- `to` is required to calculate the character count for `maxchars` |
252 start = since; | 305 maxchars = maxchars, maxstanzas = maxstanzas, since = since; |
253 reverse = true; | 306 next_stanza = function() end; -- events should define this iterator |
254 with = "message<groupchat"; | 307 }; |
255 }); | 308 |
256 | 309 module:fire_event("muc-get-history", event); |
257 if not data then | 310 |
258 module:log("error", "Could not fetch history: %s", tostring(err)); | 311 for msg in event.next_stanza, event do |
259 return | 312 self:_route_stanza(msg); |
260 end | |
261 | |
262 local to_send = {}; | |
263 local charcount = 0; | |
264 local chars; | |
265 for id, item, when in data do | |
266 item.attr.to = to; | |
267 item:tag("delay", { xmlns = "urn:xmpp:delay", from = self.jid, stamp = timestamp(when) }):up(); -- XEP-0203 | |
268 if maxchars then | |
269 chars = #tostring(item); | |
270 if chars + charcount > maxchars then break end | |
271 charcount = charcount + chars; | |
272 end | |
273 to_send[1+#to_send] = item; | |
274 end | |
275 for i = #to_send,1,-1 do | |
276 self:_route_stanza(to_send[i]); | |
277 end | 313 end |
278 end | 314 end |
279 | 315 |
280 -- Handle messages | 316 -- Handle messages |
281 function save_to_history(self, stanza) | 317 function save_to_history(self, stanza) |
292 with = with .. "<" .. stanza.attr.type | 328 with = with .. "<" .. stanza.attr.type |
293 end | 329 end |
294 archive:append(room, nil, time_now(), with, stanza); | 330 archive:append(room, nil, time_now(), with, stanza); |
295 end | 331 end |
296 | 332 |
333 module:hook("muc-broadcast-message", function (event) | |
334 local room, stanza = event.room, event.stanza; | |
335 if stanza:get_child("body") then | |
336 save_to_history(room, stanza); | |
337 end | |
338 end); | |
339 | |
297 module:hook("muc-room-destroyed", function(event) | 340 module:hook("muc-room-destroyed", function(event) |
298 local username = jid_split(event.room.jid); | 341 local username = jid_split(event.room.jid); |
299 archive:delete(username); | 342 archive:delete(username); |
300 end); | 343 end); |
301 | 344 |