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
:get_child_text("status");
23 map
[event
.origin
.username
] = {s
= s
, t
= t
};
27 module
:hook("iq-get/bare/jabber:iq:last:query", function(event
)
28 local origin
, stanza
= event
.origin
, event
.stanza
;
29 local username
= jid_split(stanza
.attr
.to
) or origin
.username
;
30 if not stanza
.attr
.to
or is_contact_subscribed(username
, module
.host
, jid_bare(stanza
.attr
.from
)) then
31 local seconds
, text
= "0", "";
33 seconds
= tostring(os
.difftime(os
.time(), map
[username
].t
));
34 text
= map
[username
].s
;
36 origin
.send(st
.reply(stanza
):tag('query', {xmlns
='jabber:iq:last', seconds
=seconds
}):text(text
));
38 origin
.send(st
.error_reply(stanza
, 'auth', 'forbidden'));
43 module
.save
= function()
46 module
.restore
= function(data
)