Mercurial > prosody-modules
comparison mod_mam_muc/mod_mam_muc.lua @ 1278:40f077b18dfe
mod_mam_muc: Override sending of room history and use archives
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 18 Jan 2014 21:14:40 +0100 |
parents | 999891a9ae5d |
children | 2118a2eeb1d5 |
comparison
equal
deleted
inserted
replaced
1277:999891a9ae5d | 1278:40f077b18dfe |
---|---|
10 | 10 |
11 local st = require "util.stanza"; | 11 local st = require "util.stanza"; |
12 local rsm = module:require "mod_mam/rsm"; | 12 local rsm = module:require "mod_mam/rsm"; |
13 local jid_bare = require "util.jid".bare; | 13 local jid_bare = require "util.jid".bare; |
14 local jid_split = require "util.jid".split; | 14 local jid_split = require "util.jid".split; |
15 local room_mt = module:depends"muc".room_mt; | |
15 | 16 |
16 local getmetatable = getmetatable; | 17 local getmetatable = getmetatable; |
17 local function is_stanza(x) | 18 local function is_stanza(x) |
18 return getmetatable(x) == st.stanza_mt; | 19 return getmetatable(x) == st.stanza_mt; |
19 end | 20 end |
21 local tostring = tostring; | 22 local tostring = tostring; |
22 local time_now = os.time; | 23 local time_now = os.time; |
23 local m_min = math.min; | 24 local m_min = math.min; |
24 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; | 25 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; |
25 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); | 26 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); |
27 local max_history_length = module:get_option_number("max_history_messages", 1000); | |
26 | 28 |
27 local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false); | 29 local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false); |
28 local log_by_default = module:get_option_boolean("muc_log_by_default", true); | 30 local log_by_default = module:get_option_boolean("muc_log_by_default", true); |
29 local advertise_archive = module:get_option_boolean("muc_log_advertise", true); | 31 local advertise_archive = module:get_option_boolean("muc_log_advertise", true); |
30 | 32 |
60 end | 62 end |
61 end | 63 end |
62 end); | 64 end); |
63 end | 65 end |
64 | 66 |
67 local _send_history = room_mt.send_history; | |
68 function module.unload() | |
69 room_mt.send_history = _send_history; | |
70 end | |
65 | 71 |
66 -- Handle archive queries | 72 -- Handle archive queries |
67 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) | 73 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) |
68 local origin, stanza = event.origin, event.stanza; | 74 local origin, stanza = event.origin, event.stanza; |
69 local room = stanza.attr.to; | 75 local room = stanza.attr.to; |
147 return origin.send(st.reply(stanza) | 153 return origin.send(st.reply(stanza) |
148 :query(xmlns_mam):add_child(rsm.generate { | 154 :query(xmlns_mam):add_child(rsm.generate { |
149 first = first, last = last, count = count })); | 155 first = first, last = last, count = count })); |
150 end); | 156 end); |
151 | 157 |
158 function room_mt:send_history(to, stanza) | |
159 local maxchars, maxstanzas, seconds, since; | |
160 local history_tag = stanza:find("{http://jabber.org/protocol/muc}x/history") | |
161 if history_tag then | |
162 module:log("debug", tostring(history_tag)); | |
163 local history_attr = history_tag.attr; | |
164 maxchars = tonumber(history_attr.maxchars); | |
165 maxstanzas = tonumber(history_attr.maxstanzas); | |
166 seconds = tonumber(history_attr.seconds); | |
167 since = history_attr.since; | |
168 if since then | |
169 since = timestamp_parse(since); | |
170 end | |
171 if seconds then | |
172 since = math.max(os.time() - seconds, since or 0); | |
173 end | |
174 end | |
175 | |
176 -- Load all the data! | |
177 local data, err = archive:find(jid_split(self.jid), { | |
178 limit = m_min(maxstanzas or 20, max_history_length); | |
179 after = since; | |
180 reverse = true; | |
181 }); | |
182 | |
183 if not data then | |
184 module:log("error", "Could not fetch history: %s", tostring(err)); | |
185 return | |
186 end | |
187 | |
188 local to_send = {}; | |
189 local charcount = 0; | |
190 local chars; | |
191 for id, item, when in data do | |
192 item.attr.to = to; | |
193 item:tag("delay", { xmlns = "urn:xmpp:delay", from = self.jid, stamp = timestamp(when) }):up(); -- XEP-0203 | |
194 if maxchars then | |
195 chars = #tostring(item); | |
196 if chars + charcount > maxchars then break end | |
197 charcount = charcount + chars; | |
198 end | |
199 to_send[1+#to_send] = item; | |
200 end | |
201 for i = #to_send,1,-1 do | |
202 self:_route_stanza(to_send[i]); | |
203 end | |
204 end | |
205 | |
152 -- Handle messages | 206 -- Handle messages |
153 local function message_handler(event) | 207 local function message_handler(event) |
154 local stanza = event.stanza; | 208 local stanza = event.stanza; |
155 local orig_type = stanza.attr.type or "normal"; | 209 local orig_type = stanza.attr.type or "normal"; |
156 local orig_to = stanza.attr.to; | 210 local orig_to = stanza.attr.to; |