view mod_last_offline/mod_last_offline.lua @ 993:8b14cdfe0213

mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
author Waqas Hussain <waqas20@gmail.com>
date Tue, 30 Apr 2013 20:46:02 +0500
parents bec0a995f5df
children
line wrap: on
line source

local datamanager = require "util.datamanager";	
local jid_split = require "util.jid".split;
local time = os.time;
local NULL = {};
local host = module.host;

module:hook("resource-unbind", function(event)
	local session = event.session;
	if session.username then
		datamanager.store(session.username, host, "last_online", {
			timestamp = time(),
		});
	end
end);

local function offline_stamp(event)
	local stanza = event.stanza;
	local node, to_host = jid_split(stanza.attr.from);
	if to_host == host and event.origin == hosts[host] and stanza.attr.type == "unavailable" then
		local timestamp = (datamanager.load(node, host, "last_online") or NULL).timestamp;
		if timestamp then
			stanza:tag("delay", {
				xmlns = "urn:xmpp:delay",
				from = host,
				stamp = datetime.datetime(timestamp),
			}):up();
		end
	end
end

module:hook("pre-presence/bare", offline_stamp);
module:hook("pre-presence/full", offline_stamp);