--- a/mod_smacks/mod_smacks.lua Sat Mar 05 18:52:43 2011 +0000
+++ b/mod_smacks/mod_smacks.lua Fri Mar 18 21:26:27 2011 +0000
@@ -1,18 +1,23 @@
local st = require "util.stanza";
+local uuid_generate = require "util.uuid".generate;
local t_insert, t_remove = table.insert, table.remove;
local math_min = math.min;
+local os_time = os.time;
local tonumber, tostring = tonumber, tostring;
local add_filter = require "util.filters".add_filter;
local timer = require "util.timer";
local xmlns_sm = "urn:xmpp:sm:2";
+local xmlns_errors = "urn:ietf:params:xml:ns:xmpp-stanzas";
local sm_attr = { xmlns = xmlns_sm };
-local resume_timeout = 300;
+local resume_timeout = module:get_option("smacks_hibernation_time", 300);
local max_unacked_stanzas = 0;
+local session_registry = {};
+
module:hook("stream-features",
function (event)
event.features:tag("sm", sm_attr):tag("optional"):up():up();
@@ -23,49 +28,74 @@
event.features:tag("sm", sm_attr):tag("optional"):up():up();
end);
-module:hook_stanza(xmlns_sm, "enable",
+module:hook_stanza("http://etherx.jabber.org/streams", "features",
function (session, stanza)
- module:log("debug", "Enabling stream management");
- session.smacks = true;
-
- -- Overwrite process_stanza() and send()
- local queue = {};
- session.outgoing_stanza_queue = queue;
- session.last_acknowledged_stanza = 0;
- local _send = session.sends2s or session.send;
- local function new_send(stanza)
- local attr = stanza.attr;
- if attr and not attr.xmlns then -- Stanza in default stream namespace
- queue[#queue+1] = st.clone(stanza);
- end
- local ok, err = _send(stanza);
- if ok and #queue > max_unacked_stanzas and not session.awaiting_ack then
- session.awaiting_ack = true;
- return _send(st.stanza("r", { xmlns = xmlns_sm }));
- end
- return ok, err;
+ if not session.smacks and stanza:get_child("sm", xmlns_sm) then
+ session.send(st.stanza("enable", sm_attr));
end
-
- if session.sends2s then
- session.sends2s = new_send;
- else
- session.send = new_send;
+end);
+
+local function wrap_session(session, resume)
+ -- Overwrite process_stanza() and send()
+ local queue;
+ if not resume then
+ queue = {};
+ session.outgoing_stanza_queue = queue;
+ session.last_acknowledged_stanza = 0;
+ else
+ queue = session.outgoing_stanza_queue;
+ end
+
+ local _send = session.sends2s or session.send;
+ local function new_send(stanza)
+ local attr = stanza.attr;
+ if attr and not attr.xmlns then -- Stanza in default stream namespace
+ queue[#queue+1] = st.clone(stanza);
+ end
+ local ok, err = _send(stanza);
+ if ok and #queue > max_unacked_stanzas and not session.awaiting_ack then
+ session.awaiting_ack = true;
+ return _send(st.stanza("r", sm_attr));
+ end
+ return ok, err;
+ end
+
+ if session.sends2s then
+ session.sends2s = new_send;
+ else
+ session.send = new_send;
+ end
+
+ if not resume then
+ session.handled_stanza_count = 0;
+ add_filter(session, "stanzas/in", function (stanza)
+ if not stanza.attr.xmlns then
+ session.handled_stanza_count = session.handled_stanza_count + 1;
+ session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count);
end
-
- session.handled_stanza_count = 0;
- add_filter(session, "stanzas/in", function (stanza)
- if not stanza.attr.xmlns then
- session.handled_stanza_count = session.handled_stanza_count + 1;
- session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count);
- end
- return stanza;
- end);
+ return stanza;
+ end);
+ end
+
+ return session;
+end
- if not stanza.attr.resume then -- FIXME: Resumption should be a different spec :/
- _send(st.stanza("enabled", sm_attr));
- return true;
- end
- end, 100);
+module:hook_stanza(xmlns_sm, "enable", function (session, stanza)
+ module:log("debug", "Enabling stream management");
+ session.smacks = true;
+
+ wrap_session(session);
+
+ local resume_token;
+ local resume = stanza.attr.resume;
+ if resume == "true" or resume == "1" then
+ resume_token = uuid_generate();
+ session_registry[resume_token] = session;
+ session.resumption_token = resume_token;
+ end
+ session.send(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume }));
+ return true;
+end, 100);
module:hook_stanza(xmlns_sm, "r", function (origin, stanza)
if not origin.smacks then
@@ -88,6 +118,7 @@
if handled_stanza_count > #queue then
module:log("warn", "The client says it handled %d new stanzas, but we only sent %d :)",
handled_stanza_count, #queue);
+ module:log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza);
for i=1,#queue do
module:log("debug", "Q item %d: %s", i, tostring(queue[i]));
end
@@ -134,11 +165,17 @@
handle_unacked_stanzas(session);
end
else
- session.hibernating = true;
+ local hibernate_time = os_time(); -- Track the time we went into hibernation
+ session.hibernating = hibernate_time;
timer.add_task(resume_timeout, function ()
- if session.hibernating then
+ -- Check the hibernate time still matches what we think it is,
+ -- otherwise the session resumed and re-hibernated.
+ if session.hibernating == hibernate_time then
+ session_registry[session.resumption_token] = nil;
session.resumption_token = nil;
- sessionmanager.destroy_session(session); -- Re-destroy
+ -- This recursion back into our destroy handler is to
+ -- make sure we still handle any queued stanzas
+ sessionmanager.destroy_session(session);
end
end);
return; -- Postpone destruction for now
@@ -147,3 +184,60 @@
end
return _destroy_session(session, err);
end
+
+module:hook_stanza(xmlns_sm, "resume", function (session, stanza)
+ local id = stanza.attr.previd;
+ local original_session = session_registry[id];
+ if not original_session then
+ session.send(st.stanza("failed", sm_attr)
+ :tag("item-not-found", { xmlns = xmlns_errors })
+ );
+ elseif session.username == original_session.username
+ and session.host == original_session.host then
+ -- TODO: All this should move to sessionmanager (e.g. session:replace(new_session))
+ original_session.ip = session.ip;
+ original_session.conn = session.conn;
+ original_session.send = session.send;
+ original_session.stream = session.stream;
+ original_session.secure = session.secure;
+ original_session.hibernating = nil;
+ local filter = original_session.filter;
+ local stream = session.stream;
+ local log = session.log;
+ function original_session.data(data)
+ data = filter("bytes/in", data);
+ if data then
+ local ok, err = stream:feed(data);
+ if ok then return; end
+ log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
+ original_session:close("xml-not-well-formed");
+ end
+ end
+ wrap_session(original_session, true);
+ -- Inform xmppstream of the new session (passed to its callbacks)
+ stream:set_session(original_session);
+ -- Similar for connlisteners
+ require "net.connlisteners".get("xmppclient").associate_session(session.conn, original_session);
+
+ session.send(st.stanza("resumed", { xmlns = xmlns_sm,
+ h = original_session.handled_stanza_count, previd = id }));
+
+ -- Fake an <a> with the h of the <resume/> from the client
+ original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm,
+ h = stanza.attr.h }));
+
+ -- Ok, we need to re-send any stanzas that the client didn't see
+ -- ...they are what is now left in the outgoing stanza queue
+ local queue = original_session.outgoing_stanza_queue;
+ for i=1,#queue do
+ session.send(queue[i]);
+ end
+ else
+ log("warn", "Client %s@%s[%s] tried to resume stream for %s@%s[%s]",
+ session.username or "?", session.host or "?", session.type,
+ original_session.username or "?", original_session.host or "?", original_session.type);
+ session.send(st.stanza("failed", sm_attr)
+ :tag("not-authorized", { xmlns = xmlns_errors }));
+ end
+ return true;
+end);