mod_smacks/mod_smacks.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 01 Feb 2012 23:29:05 +0000
changeset 595 40b707d7a809
parent 593 57ac609444c4
child 596 36003cae2370
permissions -rw-r--r--
mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local st = require "util.stanza";
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
     2
local uuid_generate = require "util.uuid".generate;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local t_insert, t_remove = table.insert, table.remove;
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
     5
local math_min = math.min;
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
     6
local os_time = os.time;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
local tonumber, tostring = tonumber, tostring;
201
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
     8
local add_filter = require "util.filters".add_filter;
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
     9
local timer = require "util.timer";
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local xmlns_sm = "urn:xmpp:sm:2";
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    12
local xmlns_errors = "urn:ietf:params:xml:ns:xmpp-stanzas";
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local sm_attr = { xmlns = xmlns_sm };
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    16
local resume_timeout = module:get_option("smacks_hibernation_time", 300);
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    17
local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false);
200
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
    18
local max_unacked_stanzas = 0;
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
    19
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    20
local session_registry = {};
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    21
263
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
    22
module:hook("stream-features",
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
    23
		function (event)
593
57ac609444c4 mod_smacks: Only advertise stream features when a stream is authenticated, and doesn't already have smacks enabled
Matthew Wild <mwild1@gmail.com>
parents: 591
diff changeset
    24
			local origin = event.origin;
57ac609444c4 mod_smacks: Only advertise stream features when a stream is authenticated, and doesn't already have smacks enabled
Matthew Wild <mwild1@gmail.com>
parents: 591
diff changeset
    25
			if not(origin.smacks) and origin.type == "c2s" then
57ac609444c4 mod_smacks: Only advertise stream features when a stream is authenticated, and doesn't already have smacks enabled
Matthew Wild <mwild1@gmail.com>
parents: 591
diff changeset
    26
				event.features:tag("sm", sm_attr):tag("optional"):up():up();
57ac609444c4 mod_smacks: Only advertise stream features when a stream is authenticated, and doesn't already have smacks enabled
Matthew Wild <mwild1@gmail.com>
parents: 591
diff changeset
    27
			end
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
module:hook("s2s-stream-features",
263
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
    31
		function (event)
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    32
			local origin = event.origin;
593
57ac609444c4 mod_smacks: Only advertise stream features when a stream is authenticated, and doesn't already have smacks enabled
Matthew Wild <mwild1@gmail.com>
parents: 591
diff changeset
    33
			if s2s_smacks and not(origin.smacks) and (origin.type == "s2sin" or origin.type == "s2sout") then
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    34
				event.features:tag("sm", sm_attr):tag("optional"):up():up();
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    35
			end
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
		end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    38
module:hook_stanza("http://etherx.jabber.org/streams", "features",
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		function (session, stanza)
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    40
			if s2s_smacks and (session.type == "s2sin" or session.type == "s2sout")
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    41
					and not session.smacks and stanza:get_child("sm", xmlns_sm) then
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    42
				session.sends2s(st.stanza("enable", sm_attr));
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
			end
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    44
end);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    45
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    46
local function wrap_session(session, resume)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    47
	-- Overwrite process_stanza() and send()
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    48
	local queue;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    49
	if not resume then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    50
		queue = {};
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    51
		session.outgoing_stanza_queue = queue;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    52
		session.last_acknowledged_stanza = 0;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    53
	else
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    54
		queue = session.outgoing_stanza_queue;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    55
	end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    56
	
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    57
	local _send = session.sends2s or session.send;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    58
	local function new_send(stanza)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    59
		local attr = stanza.attr;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    60
		if attr and not attr.xmlns then -- Stanza in default stream namespace
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    61
			queue[#queue+1] = st.clone(stanza);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    62
		end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    63
		local ok, err = _send(stanza);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    64
		if ok and #queue > max_unacked_stanzas and not session.awaiting_ack then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    65
			session.awaiting_ack = true;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    66
			return _send(st.stanza("r", sm_attr));
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    67
		end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    68
		return ok, err;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    69
	end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    70
	
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    71
	if session.sends2s then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    72
		session.sends2s = new_send;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    73
	else
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    74
		session.send = new_send;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    75
	end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    76
	
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    77
	if not resume then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    78
		session.handled_stanza_count = 0;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    79
		add_filter(session, "stanzas/in", function (stanza)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    80
			if not stanza.attr.xmlns then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    81
				session.handled_stanza_count = session.handled_stanza_count + 1;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    82
				session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count);
255
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    83
			end
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    84
			return stanza;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    85
		end);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    86
	end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    87
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    88
	return session;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    89
end
201
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    90
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
    91
module:hook_stanza(xmlns_sm, "enable", function (session, stanza)
595
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    92
	local err, err_text;
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    93
	if session.smacks then
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    94
		err, err_text = "unexpected-request", "Stream management already enabled for this stream";
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    95
	elseif not session.resource then
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    96
		err, err_text = "unexpected-request", "Attempted to enable stream management before resource binding";
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    97
	end
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    98
	if err then
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
    99
		session.log("warn", "Failed to enable smacks: %s", err_text); -- TODO: XEP doesn't say we can send error text, should it?
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
   100
		session.send(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors}));
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
   101
		return true;
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
   102
	end
40b707d7a809 mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents: 593
diff changeset
   103
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   104
	module:log("debug", "Enabling stream management");
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   105
	session.smacks = true;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   106
	
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   107
	wrap_session(session);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   108
	
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   109
	local resume_token;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   110
	local resume = stanza.attr.resume;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   111
	if resume == "true" or resume == "1" then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   112
		resume_token = uuid_generate();
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   113
		session_registry[resume_token] = session;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   114
		session.resumption_token = resume_token;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   115
	end
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   116
	(session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume }));
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   117
	return true;
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   118
end, 100);
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   119
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   120
module:hook_stanza(xmlns_sm, "enabled", function (session, stanza)
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   121
	module:log("debug", "Enabling stream management");
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   122
	session.smacks = true;
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   123
	
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   124
	wrap_session(session);
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   125
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   126
	-- FIXME Resume?
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   127
	
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   128
	return true;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   129
end, 100);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
module:hook_stanza(xmlns_sm, "r", function (origin, stanza)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	if not origin.smacks then
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
		module:log("debug", "Received ack request from non-smack-enabled session");
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
		return;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
	end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
	module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
	-- Reply with <a>
255
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
   138
	(origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = tostring(origin.handled_stanza_count) }));
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
	return true;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
module:hook_stanza(xmlns_sm, "a", function (origin, stanza)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
	if not origin.smacks then return; end
200
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
   144
	origin.awaiting_ack = nil;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
	-- Remove handled stanzas from outgoing_stanza_queue
258
36648205b10a mod_smacks: Add commented log statement for future debugging
Matthew Wild <mwild1@gmail.com>
parents: 257
diff changeset
   146
	--log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or "");
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   147
	local handled_stanza_count = tonumber(stanza.attr.h)-origin.last_acknowledged_stanza;
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   148
	local queue = origin.outgoing_stanza_queue;
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   149
	if handled_stanza_count > #queue then
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   150
		module:log("warn", "The client says it handled %d new stanzas, but we only sent %d :)",
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   151
			handled_stanza_count, #queue);
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   152
		module:log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza);
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   153
		for i=1,#queue do
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   154
			module:log("debug", "Q item %d: %s", i, tostring(queue[i]));
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   155
		end
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   156
	end
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   157
	for i=1,math_min(handled_stanza_count,#queue) do
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
		t_remove(origin.outgoing_stanza_queue, 1);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
	end
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   160
	origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
	return true;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
--TODO: Optimise... incoming stanzas should be handled by a per-session
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
-- function that has a counter as an upvalue (no table indexing for increments,
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
-- and won't slow non-198 sessions). We can also then remove the .handled flag
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
-- on stanzas
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
function handle_unacked_stanzas(session)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
	local queue = session.outgoing_stanza_queue;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
	local error_attr = { type = "cancel" };
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
	if #queue > 0 then
202
d11478ae374e mod_smacks: Clean outgoing stanza queue correctly on session close
Matthew Wild <mwild1@gmail.com>
parents: 201
diff changeset
   173
		session.outgoing_stanza_queue = {};
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
		for i=1,#queue do
256
57de4a7840ef mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents: 255
diff changeset
   175
			local reply = st.reply(queue[i]);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
			if reply.attr.to ~= session.full_jid then
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
				reply.attr.type = "error";
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
				reply:tag("error", error_attr)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
					:tag("recipient-unavailable", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"});
256
57de4a7840ef mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents: 255
diff changeset
   180
				core_process_stanza(session, reply);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
			end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
		end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
	end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
local _destroy_session = sessionmanager.destroy_session;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
function sessionmanager.destroy_session(session, err)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
	if session.smacks then
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   189
		if not session.resumption_token then
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   190
			local queue = session.outgoing_stanza_queue;
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   191
			if #queue > 0 then
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   192
				module:log("warn", "Destroying session with %d unacked stanzas:", #queue);
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   193
				for i=1,#queue do
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   194
					module:log("warn", "::%s", tostring(queue[i]));
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   195
				end
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   196
				handle_unacked_stanzas(session);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
			end
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   198
		else
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   199
			session.log("debug", "mod_smacks hibernating session for up to %d seconds", resume_timeout);
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   200
			local hibernate_time = os_time(); -- Track the time we went into hibernation
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   201
			session.hibernating = hibernate_time;
478
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   202
			local resumption_token = session.resumption_token;
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   203
			timer.add_task(resume_timeout, function ()
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   204
				session.log("debug", "mod_smacks hibernation timeout reached...");
478
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   205
				-- We need to check the current resumption token for this resource
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   206
				-- matches the smacks session this timer is for in case it changed
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   207
				-- (for example, the client may have bound a new resource and
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   208
				-- started a new smacks session, or not be using smacks)
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   209
				local curr_session = hosts[session.host].sessions[session.username].sessions[session.resource];
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   210
				if curr_session.resumption_token == resumption_token
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   211
				-- Check the hibernate time still matches what we think it is,
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   212
				-- otherwise the session resumed and re-hibernated.
478
db0f065c4e09 mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents: 345
diff changeset
   213
				and session.hibernating == hibernate_time then
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   214
					session.log("debug", "Destroying session for hibernating too long");
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   215
					session_registry[session.resumption_token] = nil;
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   216
					session.resumption_token = nil;
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   217
					-- This recursion back into our destroy handler is to
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   218
					-- make sure we still handle any queued stanzas
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   219
					sessionmanager.destroy_session(session);
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   220
				else
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   221
					session.log("debug", "Session resumed before hibernation timeout, all is well")
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   222
				end
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   223
			end);
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   224
			return; -- Postpone destruction for now
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
		end
258
36648205b10a mod_smacks: Add commented log statement for future debugging
Matthew Wild <mwild1@gmail.com>
parents: 257
diff changeset
   226
		
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
	end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
	return _destroy_session(session, err);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
end
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   230
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   231
module:hook_stanza(xmlns_sm, "resume", function (session, stanza)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   232
	local id = stanza.attr.previd;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   233
	local original_session = session_registry[id];
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   234
	if not original_session then
591
8042558336b6 mod_smacks: Log message when client tries to resume unknown session
Matthew Wild <mwild1@gmail.com>
parents: 589
diff changeset
   235
		session.log("debug", "Tried to resume non-existent session with id %s", id);
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   236
		session.send(st.stanza("failed", sm_attr)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   237
			:tag("item-not-found", { xmlns = xmlns_errors })
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   238
		);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   239
	elseif session.username == original_session.username
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   240
	and session.host == original_session.host then
589
322a14acd974 mod_smacks: Add log message on resume
Matthew Wild <mwild1@gmail.com>
parents: 587
diff changeset
   241
		session.log("debug", "mod_smacks resuming existing session...");
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   242
		-- TODO: All this should move to sessionmanager (e.g. session:replace(new_session))
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   243
		original_session.ip = session.ip;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   244
		original_session.conn = session.conn;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   245
		original_session.send = session.send;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   246
		original_session.stream = session.stream;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   247
		original_session.secure = session.secure;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   248
		original_session.hibernating = nil;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   249
		local filter = original_session.filter;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   250
		local stream = session.stream;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   251
		local log = session.log;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   252
		function original_session.data(data)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   253
			data = filter("bytes/in", data);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   254
			if data then
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   255
				local ok, err = stream:feed(data);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   256
				if ok then return; end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   257
				log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   258
				original_session:close("xml-not-well-formed");
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   259
			end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   260
		end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   261
		wrap_session(original_session, true);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   262
		-- Inform xmppstream of the new session (passed to its callbacks)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   263
		stream:set_session(original_session);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   264
		-- Similar for connlisteners
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   265
		require "net.connlisteners".get("xmppclient").associate_session(session.conn, original_session);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   266
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   267
		session.send(st.stanza("resumed", { xmlns = xmlns_sm,
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   268
			h = original_session.handled_stanza_count, previd = id }));
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   269
		
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   270
		-- Fake an <a> with the h of the <resume/> from the client
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   271
		original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm,
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   272
			h = stanza.attr.h }));
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   273
		
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   274
		-- Ok, we need to re-send any stanzas that the client didn't see
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   275
		-- ...they are what is now left in the outgoing stanza queue
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   276
		local queue = original_session.outgoing_stanza_queue;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   277
		for i=1,#queue do
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   278
			session.send(queue[i]);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   279
		end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   280
	else
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   281
		log("warn", "Client %s@%s[%s] tried to resume stream for %s@%s[%s]",
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   282
			session.username or "?", session.host or "?", session.type,
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   283
			original_session.username or "?", original_session.host or "?", original_session.type);
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   284
		session.send(st.stanza("failed", sm_attr)
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   285
			:tag("not-authorized", { xmlns = xmlns_errors }));
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   286
	end
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   287
	return true;
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   288
end);