mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_lastactivity.lua
blob575e66be72a9c5a3e14b4043f605220d9a6497fe
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 --
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");
16 local map = {};
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:get_child_text("status");
23 map[event.origin.username] = {s = s, t = t};
24 end
25 end, 10);
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", "";
32 if map[username] then
33 seconds = tostring(os.difftime(os.time(), map[username].t));
34 text = map[username].s;
35 end
36 origin.send(st.reply(stanza):tag('query', {xmlns='jabber:iq:last', seconds=seconds}):text(text));
37 else
38 origin.send(st.error_reply(stanza, 'auth', 'forbidden'));
39 end
40 return true;
41 end);
43 module.save = function()
44 return {map = map};
45 end
46 module.restore = function(data)
47 map = data.map or {};
48 end