tests/test_core_s2smanager.lua: Fix to compare SRV weights correctly
[prosody.git] / plugins / mod_offline.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 \r
10 local datamanager = require "util.datamanager";\r
11 local st = require "util.stanza";\r
12 local datetime = require "util.datetime";\r
13 local ipairs = ipairs;
14 local jid_split = require "util.jid".split;\r
15 \r
16 module:add_feature("msgoffline");\r
17 \r
18 module:hook("message/offline/store", function(event)\r
19         local origin, stanza = event.origin, event.stanza;\r
20         local to = stanza.attr.to;\r
21         local node, host;\r
22         if to then\r
23                 node, host = jid_split(to)\r
24         else\r
25                 node, host = origin.username, origin.host;\r
26         end\r
27         \r
28         stanza.attr.stamp, stanza.attr.stamp_legacy = datetime.datetime(), datetime.legacy();\r
29         local result = datamanager.list_append(node, host, "offline", st.preserialize(stanza));\r
30         stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil;\r
31         \r
32         return true;\r
33 end);\r
34 \r
35 module:hook("message/offline/broadcast", function(event)\r
36         local origin = event.origin;\r
37         local node, host = origin.username, origin.host;\r
38         \r
39         local data = datamanager.list_load(node, host, "offline");\r
40         if not data then return true; end\r
41         for _, stanza in ipairs(data) do\r
42                 stanza = st.deserialize(stanza);\r
43                 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = host, stamp = stanza.attr.stamp}):up(); -- XEP-0203\r
44                 stanza:tag("x", {xmlns = "jabber:x:delay", from = host, stamp = stanza.attr.stamp_legacy}):up(); -- XEP-0091 (deprecated)\r
45                 stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil;\r
46                 origin.send(stanza);\r
47         end\r
48         return true;\r
49 end);\r
50 \r
51 module:hook("message/offline/delete", function(event)\r
52         local origin = event.origin;\r
53         local node, host = origin.username, origin.host;\r
54 \r
55         return datamanager.list_store(node, host, "offline", nil);\r
56 end);\r