Comparison

plugins/mod_offline.lua @ 8024:8eec715c13a6

mod_offline: Switch to using archive store via the storagemanager API
author Kim Alvefur <zash@zash.se>
date Fri, 31 Mar 2017 17:50:19 +0200
parent 6834:750a97b45f88
child 8030:bd3527198308
comparison
equal deleted inserted replaced
8023:5a9d491cc714 8024:8eec715c13a6
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9
10 local datamanager = require "util.datamanager";
11 local st = require "util.stanza";
12 local datetime = require "util.datetime"; 10 local datetime = require "util.datetime";
13 local ipairs = ipairs;
14 local jid_split = require "util.jid".split; 11 local jid_split = require "util.jid".split;
12
13 local offline_messages = module:open_store("offline", "archive");
15 14
16 module:add_feature("msgoffline"); 15 module:add_feature("msgoffline");
17 16
18 module:hook("message/offline/handle", function(event) 17 module:hook("message/offline/handle", function(event)
19 local origin, stanza = event.origin, event.stanza; 18 local origin, stanza = event.origin, event.stanza;
20 local to = stanza.attr.to; 19 local to = stanza.attr.to;
21 local node, host; 20 local node;
22 if to then 21 if to then
23 node, host = jid_split(to) 22 node = jid_split(to)
24 else 23 else
25 node, host = origin.username, origin.host; 24 node = origin.username;
26 end 25 end
27 26
28 stanza.attr.stamp, stanza.attr.stamp_legacy = datetime.datetime(), datetime.legacy(); 27 return offline_messages:append(node, nil, stanza);
29 local result = datamanager.list_append(node, host, "offline", st.preserialize(stanza));
30 stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil;
31
32 return result;
33 end, -1); 28 end, -1);
34 29
35 module:hook("message/offline/broadcast", function(event) 30 module:hook("message/offline/broadcast", function(event)
36 local origin = event.origin; 31 local origin = event.origin;
37 32
38 local node, host = origin.username, origin.host; 33 local node, host = origin.username, origin.host;
39 34
40 local data = datamanager.list_load(node, host, "offline"); 35 local data = offline_messages:find(node);
41 if not data then return true; end 36 if not data then return true; end
42 for _, stanza in ipairs(data) do 37 for _, stanza, when in data do
43 stanza = st.deserialize(stanza); 38 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = host, stamp = datetime.datetime(when)}):up(); -- XEP-0203
44 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = host, stamp = stanza.attr.stamp}):up(); -- XEP-0203
45 stanza:tag("x", {xmlns = "jabber:x:delay", from = host, stamp = stanza.attr.stamp_legacy}):up(); -- XEP-0091 (deprecated)
46 stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil;
47 origin.send(stanza); 39 origin.send(stanza);
48 end 40 end
49 datamanager.list_store(node, host, "offline", nil); 41 offline_messages:delete(node);
50 return true; 42 return true;
51 end, -1); 43 end, -1);