2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local st = require "util.stanza";
10 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
11 local jid_bare = require "util.jid".bare;
12 local jid_split = require "util.jid".split;
14 module:add_feature("jabber:iq:last");
18 module:hook("pre-presence/bare", function(event)
19 local stanza = event.stanza;
20 if not(stanza.attr.to) and stanza.attr.type == "unavailable" then
22 local s = stanza:child_with_name("status");
23 s = s and #s.tags == 0 and s[1] or "";
24 map[event.origin.username] = {s = s, t = t};
28 module:hook("iq/bare/jabber:iq:last:query", function(event)
29 local origin, stanza = event.origin, event.stanza;
30 if stanza.attr.type == "get" then
31 local username = jid_split(stanza.attr.to) or origin.username;
32 if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
33 local seconds, text = "0", "";
35 seconds = tostring(os.difftime(os.time(), map[username].t));
36 text = map[username].s;
38 origin.send(st.reply(stanza):tag('query', {xmlns='jabber:iq:last', seconds=seconds}):text(text));
40 origin.send(st.error_reply(stanza, 'auth', 'forbidden'));
46 module.save = function()
49 module.restore = function(data)