comparison mod_last_offline/mod_last_offline.lua @ 927:a9dfa7232d88

Merge
author Matthew Wild <mwild1@gmail.com>
date Tue, 12 Mar 2013 12:10:25 +0000
parents bec0a995f5df
children
comparison
equal deleted inserted replaced
926:f88381a39c56 927:a9dfa7232d88
1 local datamanager = require "util.datamanager";
2 local jid_split = require "util.jid".split;
3 local time = os.time;
4 local NULL = {};
5 local host = module.host;
6
7 module:hook("resource-unbind", function(event)
8 local session = event.session;
9 if session.username then
10 datamanager.store(session.username, host, "last_online", {
11 timestamp = time(),
12 });
13 end
14 end);
15
16 local function offline_stamp(event)
17 local stanza = event.stanza;
18 local node, to_host = jid_split(stanza.attr.from);
19 if to_host == host and event.origin == hosts[host] and stanza.attr.type == "unavailable" then
20 local timestamp = (datamanager.load(node, host, "last_online") or NULL).timestamp;
21 if timestamp then
22 stanza:tag("delay", {
23 xmlns = "urn:xmpp:delay",
24 from = host,
25 stamp = datetime.datetime(timestamp),
26 }):up();
27 end
28 end
29 end
30
31 module:hook("pre-presence/bare", offline_stamp);
32 module:hook("pre-presence/full", offline_stamp);
33