Let Google Hangouts contacts appear offline
[prosody.git] / plugins / mod_lastactivity.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 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;
13
14 module:add_feature("jabber:iq:last");
15
16 local map = {};
17
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
21                 local t = os.time();
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};
25         end
26 end, 10);
27
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", "";
34                         if map[username] then
35                                 seconds = tostring(os.difftime(os.time(), map[username].t));
36                                 text = map[username].s;
37                         end
38                         origin.send(st.reply(stanza):tag('query', {xmlns='jabber:iq:last', seconds=seconds}):text(text));
39                 else
40                         origin.send(st.error_reply(stanza, 'auth', 'forbidden'));
41                 end
42                 return true;
43         end
44 end);
45
46 module.save = function()
47         return {map = map};
48 end
49 module.restore = function(data)
50         map = data.map or {};
51 end
52