Software /
code /
prosody-modules
Comparison
mod_mam_muc/mod_mam_muc.lua @ 1279:2118a2eeb1d5
mod_mam_muc: Override method for storing history messages instead of hooking stanza events
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 18 Jan 2014 21:16:27 +0100 |
parent | 1278:40f077b18dfe |
child | 1311:27b2a357c73c |
comparison
equal
deleted
inserted
replaced
1278:40f077b18dfe | 1279:2118a2eeb1d5 |
---|---|
63 end | 63 end |
64 end); | 64 end); |
65 end | 65 end |
66 | 66 |
67 local _send_history = room_mt.send_history; | 67 local _send_history = room_mt.send_history; |
68 local _save_to_history = room_mt.save_to_history; | |
68 function module.unload() | 69 function module.unload() |
69 room_mt.send_history = _send_history; | 70 room_mt.send_history = _send_history; |
71 room_mt.save_to_history = _save_to_history; | |
70 end | 72 end |
71 | 73 |
72 -- Handle archive queries | 74 -- Handle archive queries |
73 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) | 75 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) |
74 local origin, stanza = event.origin, event.stanza; | 76 local origin, stanza = event.origin, event.stanza; |
202 self:_route_stanza(to_send[i]); | 204 self:_route_stanza(to_send[i]); |
203 end | 205 end |
204 end | 206 end |
205 | 207 |
206 -- Handle messages | 208 -- Handle messages |
207 local function message_handler(event) | 209 function room_mt:save_to_history(stanza) |
208 local stanza = event.stanza; | |
209 local orig_type = stanza.attr.type or "normal"; | |
210 local orig_to = stanza.attr.to; | 210 local orig_to = stanza.attr.to; |
211 local orig_from = stanza.attr.from; | 211 local room = jid_split(self.jid); |
212 | 212 |
213 -- Only store groupchat messages | 213 -- Policy check |
214 if not (orig_type == "groupchat" and (stanza:get_child("body") or stanza:get_child("subject"))) then | |
215 return; | |
216 -- Chat states and other non-content messages, what TODO? | |
217 end | |
218 | |
219 local room = jid_split(orig_to); | |
220 local room_obj = rooms[orig_to] | |
221 if not room_obj then return end -- No such room | |
222 | |
223 if not ( log_all_rooms == true -- Logging forced on all rooms | 214 if not ( log_all_rooms == true -- Logging forced on all rooms |
224 or (room_obj._data.logging == nil and log_by_default == true) | 215 or (self._data.logging == nil and log_by_default == true) |
225 or room_obj._data.logging ) then return end -- Don't log | 216 or self._data.logging ) then return end -- Don't log |
226 | 217 |
227 local nick = room_obj._jid_nick[orig_from]; | 218 module:log("debug", "We're logging this") |
228 if not nick then return end -- Message from someone not in the room? | |
229 | |
230 stanza.attr.from = nick; | |
231 -- And stash it | 219 -- And stash it |
232 local ok, id = archive:append(room, time_now(), "", stanza); | 220 local ok, id = archive:append(room, time_now(), "", stanza); |
233 stanza.attr.from = orig_from; | |
234 if ok and advertise_archive then | 221 if ok and advertise_archive then |
235 stanza:tag("archived", { xmlns = xmlns_mam, by = jid_bare(orig_to), id = id }):up(); | 222 stanza:tag("archived", { xmlns = xmlns_mam, by = jid_bare(orig_to), id = id }):up(); |
236 end | 223 end |
237 end | 224 end |
238 | |
239 module:hook("message/bare", message_handler, 2); | |
240 | 225 |
241 module:hook("muc-room-destroyed", function(event) | 226 module:hook("muc-room-destroyed", function(event) |
242 archive:delete(jid_split(event.room.jid)); | 227 archive:delete(jid_split(event.room.jid)); |
243 end); | 228 end); |
244 | 229 |