Comparison

plugins/mod_mam/mod_mam.lua @ 7909:428d4abee723

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 20 Feb 2017 01:46:54 +0100
parent 7908:dbdaa8487ecd
child 7979:a1e88642411d
comparison
equal deleted inserted replaced
7900:41f783d4e127 7909:428d4abee723
7 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
8 -- 8 --
9 -- XEP-0313: Message Archive Management for Prosody 9 -- XEP-0313: Message Archive Management for Prosody
10 -- 10 --
11 11
12 local xmlns_mam = "urn:xmpp:mam:1"; 12 local xmlns_mam = "urn:xmpp:mam:2";
13 local xmlns_delay = "urn:xmpp:delay"; 13 local xmlns_delay = "urn:xmpp:delay";
14 local xmlns_forward = "urn:xmpp:forward:0"; 14 local xmlns_forward = "urn:xmpp:forward:0";
15 local xmlns_st_id = "urn:xmpp:sid:0"; 15 local xmlns_st_id = "urn:xmpp:sid:0";
16 16
17 local um = require "core.usermanager"; 17 local um = require "core.usermanager";
87 }; 87 };
88 88
89 -- Serve form 89 -- Serve form
90 module:hook("iq-get/self/"..xmlns_mam..":query", function(event) 90 module:hook("iq-get/self/"..xmlns_mam..":query", function(event)
91 local origin, stanza = event.origin, event.stanza; 91 local origin, stanza = event.origin, event.stanza;
92 origin.send(st.reply(stanza):add_child(query_form:form())); 92 origin.send(st.reply(stanza):query(xmlns_mam):add_child(query_form:form()));
93 return true; 93 return true;
94 end); 94 end);
95 95
96 -- Handle archive queries 96 -- Handle archive queries
97 module:hook("iq-set/self/"..xmlns_mam..":query", function(event) 97 module:hook("iq-set/self/"..xmlns_mam..":query", function(event)
131 local qset = rsm.get(query); 131 local qset = rsm.get(query);
132 local qmax = m_min(qset and qset.max or default_max_items, max_max_items); 132 local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
133 local reverse = qset and qset.before or false; 133 local reverse = qset and qset.before or false;
134 local before, after = qset and qset.before, qset and qset.after; 134 local before, after = qset and qset.before, qset and qset.after;
135 if type(before) ~= "string" then before = nil; end 135 if type(before) ~= "string" then before = nil; end
136
137 136
138 -- Load all the data! 137 -- Load all the data!
139 local data, err = archive:find(origin.username, { 138 local data, err = archive:find(origin.username, {
140 start = qstart; ["end"] = qend; -- Time range 139 start = qstart; ["end"] = qend; -- Time range
141 with = qwith; 140 with = qwith;
290 -- Check with the users preferences 289 -- Check with the users preferences
291 if shall_store(store_user, with) then 290 if shall_store(store_user, with) then
292 log("debug", "Archiving stanza: %s", stanza:top_tag()); 291 log("debug", "Archiving stanza: %s", stanza:top_tag());
293 292
294 -- And stash it 293 -- And stash it
295 local ok, id = archive:append(store_user, nil, stanza, time_now(), with); 294 local ok = archive:append(store_user, nil, stanza, time_now(), with);
296 if ok then 295 if ok then
296 local id = ok;
297 stanza:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); 297 stanza:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
298 if cleanup then cleanup[store_user] = true; end 298 if cleanup then cleanup[store_user] = true; end
299 module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id }); 299 module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id });
300 end 300 end
301 else 301 else
304 end 304 end
305 305
306 local function c2s_message_handler(event) 306 local function c2s_message_handler(event)
307 return message_handler(event, true); 307 return message_handler(event, true);
308 end 308 end
309
310 local function strip_stanza_id(event)
311 local strip_by = jid_bare(event.origin.full_jid);
312 event.stanza:maptags(function(tag)
313 if not ( tag.attr.xmlns == xmlns_st_id and tag.attr.by == strip_by ) then
314 return tag;
315 end
316 end);
317 end
318
319 module:hook("pre-message/bare", strip_stanza_id, -1);
320 module:hook("pre-message/full", strip_stanza_id, -1);
309 321
310 local cleanup_after = module:get_option_string("archive_expires_after", "1w"); 322 local cleanup_after = module:get_option_string("archive_expires_after", "1w");
311 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); 323 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);
312 if cleanup_after ~= "never" then 324 if cleanup_after ~= "never" then
313 local day = 86400; 325 local day = 86400;
332 cleanup = {}; 344 cleanup = {};
333 345
334 -- Iterating over users is not supported by all authentication modules 346 -- Iterating over users is not supported by all authentication modules
335 -- Catch and ignore error if not supported 347 -- Catch and ignore error if not supported
336 pcall(function () 348 pcall(function ()
337 -- If this works, then we schedule cleanup for all known known 349 -- If this works, then we schedule cleanup for all known users on startup
338 for user in um.users(module.host) do 350 for user in um.users(module.host) do
339 cleanup[user] = true; 351 cleanup[user] = true;
340 end 352 end
341 end); 353 end);
342 354
358 end 370 end
359 371
360 -- Stanzas sent by local clients 372 -- Stanzas sent by local clients
361 module:hook("pre-message/bare", c2s_message_handler, 0); 373 module:hook("pre-message/bare", c2s_message_handler, 0);
362 module:hook("pre-message/full", c2s_message_handler, 0); 374 module:hook("pre-message/full", c2s_message_handler, 0);
363 -- Stanszas to local clients 375 -- Stanzas to local clients
364 module:hook("message/bare", message_handler, 0); 376 module:hook("message/bare", message_handler, 0);
365 module:hook("message/full", message_handler, 0); 377 module:hook("message/full", message_handler, 0);
366 378
367 module:hook("account-disco-info", function(event) 379 module:hook("account-disco-info", function(event)
368 (event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up(); 380 (event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up();