# HG changeset patch # User Kim Alvefur # Date 1356001956 -3600 # Node ID bec0a995f5dfb4f1b3041fe6850d461757ece089 # Parent 4939788a47ead264867c468b33c94457cfc6a06c mod_last_offline: New module that stores last logut time and attaches this timestamp to unavailable presence. diff -r 4939788a47ea -r bec0a995f5df mod_last_offline/mod_last_offline.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_last_offline/mod_last_offline.lua Thu Dec 20 12:12:36 2012 +0100 @@ -0,0 +1,33 @@ +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); +