mod_smacks/mod_smacks.lua
author tmolitor <thilo@eightysoft.de>
Sun, 12 Feb 2017 21:23:22 +0100
changeset 2498 d300ae5dba87
parent 2495 5fbca7de2088
child 2600 ffb6646b4253
permissions -rw-r--r--
mod_smacks: Fix some bugs with smacks-ack-delayed event triggering. The old code had several flaws which are addressed here. First of all this fixes the if statement guarding the event generation There where some timing glitches addressed by this commit as well.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1674
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     1
-- XEP-0198: Stream Management for Prosody IM
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     2
--
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     3
-- Copyright (C) 2010-2015 Matthew Wild
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     4
-- Copyright (C) 2010 Waqas Hussain
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     5
-- Copyright (C) 2012-2015 Kim Alvefur
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     6
-- Copyright (C) 2012 Thijs Alkemade
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     7
-- Copyright (C) 2014 Florian Zeitz
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
     8
-- Copyright (C) 2016-2017 Thilo Molitor
1674
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
     9
--
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
    10
-- This project is MIT/X11 licensed. Please see the
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
    11
-- COPYING file in the source package for more information.
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
    12
--
5f5ff061b316 mod_smacks: Add license header
Kim Alvefur <zash@zash.se>
parents: 1640
diff changeset
    13
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
    14
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
    15
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
    16
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
    17
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
    18
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
    19
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
    20
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
    21
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
    22
local timer = require "util.timer";
598
db2a40cbd6ef Add a <delay> to stanzas that are queued (and don't have one already), so clients can show them with the original timestamp.
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 597
diff changeset
    23
local datetime = require "util.datetime";
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
    24
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    25
local xmlns_sm2 = "urn:xmpp:sm:2";
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    26
local xmlns_sm3 = "urn:xmpp:sm:3";
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
    27
local xmlns_errors = "urn:ietf:params:xml:ns:xmpp-stanzas";
598
db2a40cbd6ef Add a <delay> to stanzas that are queued (and don't have one already), so clients can show them with the original timestamp.
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 597
diff changeset
    28
local xmlns_delay = "urn:xmpp:delay";
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
    29
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    30
local sm2_attr = { xmlns = xmlns_sm2 };
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    31
local sm3_attr = { xmlns = xmlns_sm3 };
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
    32
641
06a78cba6499 mod_smacks: get_option+_number
Matthew Wild <mwild1@gmail.com>
parents: 640
diff changeset
    33
local resume_timeout = module:get_option_number("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
    34
local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false);
1885
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
    35
local s2s_resend = module:get_option_boolean("smacks_s2s_resend", false);
642
842a8a3b0d81 mod_smacks: Make smacks_max_unacked_stanzas configurable
Matthew Wild <mwild1@gmail.com>
parents: 641
diff changeset
    36
local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0);
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    37
local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 60);
754
713c6791fbcc mod_smacks: Import prosody.core_process_stanza()
Kim Alvefur <zash@zash.se>
parents: 642
diff changeset
    38
local core_process_stanza = prosody.core_process_stanza;
757
92c6f84ec446 mod_smacks: Use require to import (thanks a lot, autocomplete)
Kim Alvefur <zash@zash.se>
parents: 756
diff changeset
    39
local sessionmanager = require"core.sessionmanager";
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
    40
640
d87131b29bbd mod_smacks: Remove dependency on connlisteners (use sessions table shared by mod_c2s directly)
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
    41
local c2s_sessions = module:shared("/*/c2s/sessions");
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
    42
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
    43
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    44
local function delayed_ack_function(session)
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    45
	-- fire event only when configured to do so
2498
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
    46
	if delayed_ack_timeout > 0 and session.awaiting_ack and not (session.outgoing_stanza_queue == nil) then
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    47
		session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", #session.outgoing_stanza_queue);
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    48
		module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue});
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    49
	end
2498
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
    50
	session.delayed_ack_timer = nil;
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    51
end
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
    52
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    53
local function can_do_smacks(session, advertise_only)
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    54
	if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
    55
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    56
	local session_type = session.type;
2421
5e7badecf7fe mod_smacks: Check if a session is an authenticated c2s session by looking for a username (fix for change in 0.10 9f70d35a1602)
Kim Alvefur <zash@zash.se>
parents: 2398
diff changeset
    57
	if session.username then
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    58
		if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm
597
f9c73c1249cd Update smacks to urn:xmpp:sm:3. Fix typo in can_do_smacks.
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 596
diff changeset
    59
			return false, "unexpected-request", "Client must bind a resource before enabling stream management";
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    60
		end
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    61
		return true;
600
7693724881b3 Fix a typo in mod_smacks (type -> session_type).
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 599
diff changeset
    62
	elseif s2s_smacks and (session_type == "s2sin" or session_type == "s2sout") then
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    63
		return true;
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    64
	end
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    65
	return false, "service-unavailable", "Stream management is not available for this stream";
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    66
end
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    67
263
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
    68
module:hook("stream-features",
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
    69
		function (event)
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    70
			if can_do_smacks(event.origin, true) then
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    71
				event.features:tag("sm", sm2_attr):tag("optional"):up():up();
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    72
				event.features:tag("sm", sm3_attr):tag("optional"):up():up();
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
    73
			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
    74
		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
    75
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
    76
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
    77
		function (event)
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
    78
			if can_do_smacks(event.origin, true) then
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    79
				event.features:tag("sm", sm2_attr):tag("optional"):up():up();
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    80
				event.features:tag("sm", sm3_attr):tag("optional"):up():up();
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
    81
			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
    82
		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
    83
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    84
local function request_ack_if_needed(session)
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    85
	local queue = session.outgoing_stanza_queue;
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    86
	if #queue > max_unacked_stanzas and session.awaiting_ack == nil then
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    87
		session.log("debug", "Queuing <r> (in a moment)");
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    88
		session.awaiting_ack = false;
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    89
		session.awaiting_ack_timer = module:add_timer(1e-06, function ()
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    90
			if not session.awaiting_ack then
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    91
				session.log("debug", "Sending <r> (inside timer, before send)");
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    92
				(session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }))
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    93
				session.log("debug", "Sending <r> (inside timer, after send)");
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    94
				session.awaiting_ack = true;
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    95
				session.delayed_ack_timer = module:add_timer(delayed_ack_timeout, function()
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    96
					delayed_ack_function(session);
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    97
				end);
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    98
			end
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
    99
		end);
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   100
	end
2498
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   101
	-- Trigger "smacks-ack-delayed"-event if we added new (ackable) stanzas to the outgoing queue
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   102
	-- and there isn't already a timer for this event running.
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   103
	-- If we wouldn't do this, stanzas added to the queue after the first "smacks-ack-delayed"-event
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   104
	-- would not trigger this event (again).
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   105
	if #queue > max_unacked_stanzas and session.awaiting_ack and session.delayed_ack_timer == nil then
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   106
		session.log("debug", "Calling delayed_ack_function directly (still waiting for ack)");
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   107
		delayed_ack_function(session);
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   108
	end
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   109
end
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   110
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   111
local function outgoing_stanza_filter(stanza, session)
1601
dc0cf2ba0e1a mod_smacks: Stanzas don't have a ':' in their name
Kim Alvefur <zash@zash.se>
parents: 1539
diff changeset
   112
	local is_stanza = stanza.attr and not stanza.attr.xmlns and not stanza.name:find":";
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   113
	if is_stanza and not stanza._cached then -- Stanza in default stream namespace
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   114
		local queue = session.outgoing_stanza_queue;
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   115
		local cached_stanza = st.clone(stanza);
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   116
		cached_stanza._cached = true;
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   117
1603
8e006226e4c5 mod_smacks: Don't attach timestamps to 'iq' stanzas
Kim Alvefur <zash@zash.se>
parents: 1602
diff changeset
   118
		if cached_stanza and cached_stanza.name ~= "iq" and cached_stanza:get_child("delay", xmlns_delay) == nil then
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   119
			cached_stanza = cached_stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()});
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   120
		end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   121
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   122
		queue[#queue+1] = cached_stanza;
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   123
		session.log("debug", "#queue = %d", #queue);
1523
d4a4ed31567e mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents: 1520
diff changeset
   124
		if session.hibernating then
d4a4ed31567e mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents: 1520
diff changeset
   125
			session.log("debug", "hibernating, stanza queued");
2091
e48dbb640408 mod_smacks: Drop stanzas instead of turning them into the empty string
Kim Alvefur <zash@zash.se>
parents: 1885
diff changeset
   126
			return nil;
1523
d4a4ed31567e mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents: 1520
diff changeset
   127
		end
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   128
		request_ack_if_needed(session);
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   129
	end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   130
	return stanza;
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   131
end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   132
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   133
local function count_incoming_stanzas(stanza, session)
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   134
	if not stanza.attr.xmlns then
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   135
		session.handled_stanza_count = session.handled_stanza_count + 1;
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   136
		session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count);
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   137
	end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   138
	return stanza;
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   139
end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   140
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   141
local function wrap_session_out(session, resume)
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
   142
	if not resume then
1528
5ccb3ee2cf72 mod_smacks: Remove variable not used after 2881d532f385
Kim Alvefur <zash@zash.se>
parents: 1527
diff changeset
   143
		session.outgoing_stanza_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
   144
		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
   145
	end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   146
2126
3f788f18cc10 mod_smacks: Adjust filter priorities to avoid conflict with mod_websocket
Kim Alvefur <zash@zash.se>
parents: 2095
diff changeset
   147
	add_filter(session, "stanzas/out", outgoing_stanza_filter, -999);
988
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   148
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   149
	local session_close = session.close;
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   150
	function session.close(...)
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   151
		if session.resumption_token then
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   152
			session_registry[session.resumption_token] = nil;
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   153
			session.resumption_token = nil;
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   154
		end
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   155
		return session_close(...);
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   156
	end
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   157
	return session;
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   158
end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   159
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   160
local function wrap_session_in(session, resume)
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
   161
	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
   162
		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
   163
	end
2126
3f788f18cc10 mod_smacks: Adjust filter priorities to avoid conflict with mod_websocket
Kim Alvefur <zash@zash.se>
parents: 2095
diff changeset
   164
	add_filter(session, "stanzas/in", count_incoming_stanzas, 999);
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
   165
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
   166
	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
   167
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
   168
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   169
local function wrap_session(session, resume)
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   170
	wrap_session_out(session, resume);
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   171
	wrap_session_in(session, resume);
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   172
	return session;
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   173
end
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   174
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   175
function handle_enable(session, stanza, xmlns_sm)
596
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
   176
	local ok, err, err_text = can_do_smacks(session);
36003cae2370 mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents: 595
diff changeset
   177
	if not ok then
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
   178
		session.log("warn", "Failed to enable smacks: %s", err_text); -- TODO: XEP doesn't say we can send error text, should it?
1527
06ecc5b3ca46 mod_smacks: Send failure correctly on s2s
Kim Alvefur <zash@zash.se>
parents: 1526
diff changeset
   179
		(session.sends2s or session.send)(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors}));
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
   180
		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
   181
	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
   182
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
   183
	module:log("debug", "Enabling stream management");
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   184
	session.smacks = xmlns_sm;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   185
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   186
	wrap_session(session, false);
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   187
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
   188
	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
   189
	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
   190
	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
   191
		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
   192
		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
   193
		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
   194
	end
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   195
	(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
   196
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   197
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   198
module:hook_stanza(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100);
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   199
module:hook_stanza(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100);
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   200
1530
fb7cd669f41b mod_smacks: Initiate outgoing smacks on s2s when sending request and incoming when the remote says enabled
Kim Alvefur <zash@zash.se>
parents: 1529
diff changeset
   201
module:hook_stanza("http://etherx.jabber.org/streams", "features",
fb7cd669f41b mod_smacks: Initiate outgoing smacks on s2s when sending request and incoming when the remote says enabled
Kim Alvefur <zash@zash.se>
parents: 1529
diff changeset
   202
		function (session, stanza)
2093
964b3d649a36 mod_smacks: Increase delay for starting s2s smacks to a non-zero value
Kim Alvefur <zash@zash.se>
parents: 2092
diff changeset
   203
			module:add_timer(1e-6, function ()
1531
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   204
				if can_do_smacks(session) then
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   205
					if stanza:get_child("sm", xmlns_sm3) then
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   206
						session.sends2s(st.stanza("enable", sm3_attr));
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   207
						session.smacks = xmlns_sm3;
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   208
					elseif stanza:get_child("sm", xmlns_sm2) then
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   209
						session.sends2s(st.stanza("enable", sm2_attr));
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   210
						session.smacks = xmlns_sm2;
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   211
					else
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   212
						return;
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   213
					end
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   214
					wrap_session_out(session, false);
1530
fb7cd669f41b mod_smacks: Initiate outgoing smacks on s2s when sending request and incoming when the remote says enabled
Kim Alvefur <zash@zash.se>
parents: 1529
diff changeset
   215
				end
1531
7d86fc477993 mod_smacks: Delay initiation of outgoing smacks on s2s until after queued stanzas are sent
Kim Alvefur <zash@zash.se>
parents: 1530
diff changeset
   216
			end);
1530
fb7cd669f41b mod_smacks: Initiate outgoing smacks on s2s when sending request and incoming when the remote says enabled
Kim Alvefur <zash@zash.se>
parents: 1529
diff changeset
   217
		end);
fb7cd669f41b mod_smacks: Initiate outgoing smacks on s2s when sending request and incoming when the remote says enabled
Kim Alvefur <zash@zash.se>
parents: 1529
diff changeset
   218
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   219
function handle_enabled(session, stanza, xmlns_sm)
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   220
	module:log("debug", "Enabling stream management");
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   221
	session.smacks = xmlns_sm;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   222
1530
fb7cd669f41b mod_smacks: Initiate outgoing smacks on s2s when sending request and incoming when the remote says enabled
Kim Alvefur <zash@zash.se>
parents: 1529
diff changeset
   223
	wrap_session_in(session, false);
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   224
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   225
	-- FIXME Resume?
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   226
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
   227
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   228
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   229
module:hook_stanza(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100);
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   230
module:hook_stanza(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); 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
   231
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   232
function handle_r(origin, stanza, xmlns_sm)
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
   233
	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
   234
		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
   235
		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
   236
	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
   237
	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
   238
	-- Reply with <a>
255
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
   239
	(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
   240
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   241
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   242
module:hook_stanza(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end);
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   243
module:hook_stanza(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); 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
   244
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   245
function handle_a(origin, stanza)
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
   246
	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
   247
	origin.awaiting_ack = nil;
2094
1796a022dd29 mod_smacks: Keep timer object around and stop it when needed (trunk only)
Kim Alvefur <zash@zash.se>
parents: 2093
diff changeset
   248
	if origin.awaiting_ack_timer then
1796a022dd29 mod_smacks: Keep timer object around and stop it when needed (trunk only)
Kim Alvefur <zash@zash.se>
parents: 2093
diff changeset
   249
		origin.awaiting_ack_timer:stop();
1796a022dd29 mod_smacks: Keep timer object around and stop it when needed (trunk only)
Kim Alvefur <zash@zash.se>
parents: 2093
diff changeset
   250
	end
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   251
	if origin.delayed_ack_timer then
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   252
		origin.delayed_ack_timer:stop();
2498
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   253
		origin.delayed_ack_timer = nil;
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   254
	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
   255
	-- Remove handled stanzas from outgoing_stanza_queue
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   256
	log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or "");
1407
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   257
	local h = tonumber(stanza.attr.h);
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   258
	if not h then
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   259
		origin:close{ condition = "invalid-xml"; text = "Missing or invalid 'h' attribute"; };
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   260
	end
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   261
	local handled_stanza_count = 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
   262
	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
   263
	if handled_stanza_count > #queue then
1416
7ddb522d9b28 mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 1408
diff changeset
   264
		origin.log("warn", "The client says it handled %d new stanzas, but we only sent %d :)",
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   265
			handled_stanza_count, #queue);
1416
7ddb522d9b28 mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 1408
diff changeset
   266
		origin.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
   267
		for i=1,#queue do
1416
7ddb522d9b28 mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 1408
diff changeset
   268
			origin.log("debug", "Q item %d: %s", i, tostring(queue[i]));
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   269
		end
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   270
	end
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   271
	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
   272
		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
   273
	end
1406
7d76dd2310ef mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 1405
diff changeset
   274
	origin.log("debug", "#queue = %d", #queue);
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   275
	origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count;
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   276
	request_ack_if_needed(origin)
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
   277
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   278
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   279
module:hook_stanza(xmlns_sm2, "a", handle_a);
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   280
module:hook_stanza(xmlns_sm3, "a", handle_a);
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
   281
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
   282
--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
   283
-- 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
   284
-- 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
   285
-- 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
   286
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
   287
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
   288
	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
   289
	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
   290
	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
   291
		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
   292
		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
   293
			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
   294
			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
   295
				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
   296
				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
   297
					: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
   298
				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
   299
			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
   300
		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
   301
	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
   302
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
   303
622
ce39df945de1 mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents: 600
diff changeset
   304
module:hook("pre-resource-unbind", function (event)
ce39df945de1 mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents: 600
diff changeset
   305
	local session, err = event.session, event.error;
1024
d7655e634c30 mod_smacks: Allow resumption if the TCP connection is closed from our end, c15cea87036f ensures distinction from cleanly closed streams (thanks Lance)
Kim Alvefur <zash@zash.se>
parents: 994
diff changeset
   306
	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
   307
		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
   308
			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
   309
			if #queue > 0 then
1405
26a2092e289f mod_smacks: Log things attached to sessions instead of the module
Kim Alvefur <zash@zash.se>
parents: 1343
diff changeset
   310
				session.log("warn", "Destroying session with %d unacked stanzas", #queue);
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
   311
				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
   312
			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
   313
		else
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   314
			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
   315
			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
   316
			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
   317
			local resumption_token = session.resumption_token;
2144
3a94b3cd31e2 mod_smacks: added new events for hibernation start/end
tmolitor <thilo@eightysoft.de>
parents: 2140
diff changeset
   318
			module:fire_event("smacks-hibernation-start", {origin = session, queue = session.outgoing_stanza_queue});
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
   319
			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
   320
				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
   321
				-- 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
   322
				-- 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
   323
				-- (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
   324
				-- started a new smacks session, or not be using smacks)
810
464ed063a8f2 mod_smacks: Less table indexing!
Kim Alvefur <zash@zash.se>
parents: 757
diff changeset
   325
				local curr_session = full_sessions[session.full_jid];
1602
043d08448d87 mod_smacks: Remove negation of condition, most likely a leftover from debugging
Kim Alvefur <zash@zash.se>
parents: 1601
diff changeset
   326
				if session.destroyed then
987
fabff75bfc3f mod_smacks: If a hibernating session was destroyed before the timeout, don't destroy it again or say that it was resumed
Kim Alvefur <zash@zash.se>
parents: 925
diff changeset
   327
					session.log("debug", "The session has already been destroyed");
fabff75bfc3f mod_smacks: If a hibernating session was destroyed before the timeout, don't destroy it again or say that it was resumed
Kim Alvefur <zash@zash.se>
parents: 925
diff changeset
   328
				elseif curr_session and 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
   329
				-- 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
   330
				-- 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
   331
				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
   332
					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
   333
					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
   334
					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
   335
					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
   336
				else
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   337
					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
   338
				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
   339
			end);
622
ce39df945de1 mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents: 600
diff changeset
   340
			return true; -- 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
   341
		end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   342
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
   343
	end
622
ce39df945de1 mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents: 600
diff changeset
   344
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
   345
1736
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   346
local function handle_s2s_destroyed(event)
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   347
	local session = event.session;
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   348
	local queue = session.outgoing_stanza_queue;
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   349
	if queue and #queue > 0 then
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   350
		session.log("warn", "Destroying session with %d unacked stanzas", #queue);
1885
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   351
		if s2s_resend then
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   352
			for i = 1, #queue do
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   353
				module:send(queue[i]);
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   354
			end
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   355
			session.outgoing_stanza_queue = nil;
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   356
		else
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   357
			handle_unacked_stanzas(session);
3683eb95bc1a mod_smacks: Add experimental resending of unacked messages in s2s queues (disabled by default)
Kim Alvefur <zash@zash.se>
parents: 1737
diff changeset
   358
		end
1736
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   359
	end
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   360
end
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   361
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   362
module:hook("s2sout-destroyed", handle_s2s_destroyed);
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   363
module:hook("s2sin-destroyed", handle_s2s_destroyed);
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   364
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   365
function handle_resume(session, stanza, xmlns_sm)
925
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   366
	if session.full_jid then
994
487cd02aada0 mod_smacks: Complain a little louder about clients trying to resume after resource binding
Kim Alvefur <zash@zash.se>
parents: 988
diff changeset
   367
		session.log("warn", "Tried to resume after resource binding");
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   368
		session.send(st.stanza("failed", { xmlns = xmlns_sm })
925
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   369
			:tag("unexpected-request", { xmlns = xmlns_errors })
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   370
		);
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   371
		return true;
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   372
	end
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   373
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
   374
	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
   375
	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
   376
	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
   377
		session.log("debug", "Tried to resume non-existent session with id %s", id);
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   378
		session.send(st.stanza("failed", { xmlns = xmlns_sm })
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
   379
			: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
   380
		);
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
   381
	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
   382
	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
   383
		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
   384
		-- TODO: All this should move to sessionmanager (e.g. session:replace(new_session))
623
c1f3958695ea mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents: 622
diff changeset
   385
		if original_session.conn then
c1f3958695ea mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents: 622
diff changeset
   386
			session.log("debug", "mod_smacks closing an old connection for this session");
c1f3958695ea mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents: 622
diff changeset
   387
			local conn = original_session.conn;
640
d87131b29bbd mod_smacks: Remove dependency on connlisteners (use sessions table shared by mod_c2s directly)
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
   388
			c2s_sessions[conn] = nil;
623
c1f3958695ea mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents: 622
diff changeset
   389
			conn:close();
c1f3958695ea mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents: 622
diff changeset
   390
		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
   391
		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
   392
		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
   393
		original_session.send = session.send;
1640
ad6694f7b13c mod_smacks: Don't restore filters from original session after resumption, use new filters
Matthew Wild <mwild1@gmail.com>
parents: 1603
diff changeset
   394
		original_session.filter = session.filter;
1710
e4867211cddb mod_smacks: Set session upvalue of filter() to correct session
Kim Alvefur <zash@zash.se>
parents: 1709
diff changeset
   395
		original_session.filter.session = original_session;
1709
6fa220a9f36d mod_smacks: Move set of filters from new session to session being resumed
Kim Alvefur <zash@zash.se>
parents: 1708
diff changeset
   396
		original_session.filters = session.filters;
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
   397
		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
   398
		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
   399
		original_session.hibernating = nil;
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   400
		wrap_session(original_session, true);
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
   401
		-- Inform xmppstream of the new session (passed to its callbacks)
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   402
		original_session.stream:set_session(original_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
   403
		-- Similar for connlisteners
640
d87131b29bbd mod_smacks: Remove dependency on connlisteners (use sessions table shared by mod_c2s directly)
Matthew Wild <mwild1@gmail.com>
parents: 625
diff changeset
   404
		c2s_sessions[session.conn] = original_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
   405
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
   406
		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
   407
			h = original_session.handled_stanza_count, previd = id }));
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   408
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
   409
		-- 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
   410
		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
   411
			h = stanza.attr.h }));
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   412
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
   413
		-- 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
   414
		-- ...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
   415
		local queue = original_session.outgoing_stanza_queue;
2144
3a94b3cd31e2 mod_smacks: added new events for hibernation start/end
tmolitor <thilo@eightysoft.de>
parents: 2140
diff changeset
   416
		module:fire_event("smacks-hibernation-end", {origin = session, queue = queue});
1406
7d76dd2310ef mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 1405
diff changeset
   417
		session.log("debug", "#queue = %d", #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
   418
		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
   419
			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
   420
		end
1406
7d76dd2310ef mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 1405
diff changeset
   421
		session.log("debug", "#queue = %d -- after send", #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
   422
	else
755
bab7c4ace803 mod_smacks: Fix log statement
Kim Alvefur <zash@zash.se>
parents: 754
diff changeset
   423
		module:log("warn", "Client %s@%s[%s] tried to resume stream for %s@%s[%s]",
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
   424
			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
   425
			original_session.username or "?", original_session.host or "?", original_session.type);
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   426
		session.send(st.stanza("failed", { xmlns = xmlns_sm })
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
   427
			: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
   428
	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
   429
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   430
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   431
module:hook_stanza(xmlns_sm2, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm2); end);
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   432
module:hook_stanza(xmlns_sm3, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm3); end);
1737
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   433
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   434
local function handle_read_timeout(event)
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   435
	local session = event.session;
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   436
	if session.smacks then
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   437
		if session.awaiting_ack then
2094
1796a022dd29 mod_smacks: Keep timer object around and stop it when needed (trunk only)
Kim Alvefur <zash@zash.se>
parents: 2093
diff changeset
   438
			if session.awaiting_ack_timer then
1796a022dd29 mod_smacks: Keep timer object around and stop it when needed (trunk only)
Kim Alvefur <zash@zash.se>
parents: 2093
diff changeset
   439
				session.awaiting_ack_timer:stop();
1796a022dd29 mod_smacks: Keep timer object around and stop it when needed (trunk only)
Kim Alvefur <zash@zash.se>
parents: 2093
diff changeset
   440
			end
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   441
			if session.delayed_ack_timer then
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   442
				session.delayed_ack_timer:stop();
2498
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   443
				session.delayed_ack_timer = nil;
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   444
			end
1737
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   445
			return false; -- Kick the session
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   446
		end
2095
ea95637cf041 mod_smacks: Log when sending <r> from read timeout event (0.10+)
Kim Alvefur <zash@zash.se>
parents: 2094
diff changeset
   447
		session.log("debug", "Sending <r> (read timeout)");
2255
48c3d64a3fc1 mod_smacks: Signal that we're about to send an ack request from read timeout event to prevent a duplicate request from outgoing stanza filter
Kim Alvefur <zash@zash.se>
parents: 2152
diff changeset
   448
		session.awaiting_ack = false;
1737
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   449
		(session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }));
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   450
		session.awaiting_ack = true;
2398
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   451
		session.delayed_ack_timer = module:add_timer(delayed_ack_timeout, function()
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   452
			delayed_ack_function(session);
4c27ebcf4cbd mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
tmolitor <thilo@eightysoft.de>
parents: 2255
diff changeset
   453
		end);
1737
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   454
		return true;
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   455
	end
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   456
end
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   457
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   458
module:hook("s2s-read-timeout", handle_read_timeout);
9abd3dce619a mod_smacks: Handle the read timeout event (0.10+ only) and send an ack request, close the connection the second time if unanswerd
Kim Alvefur <zash@zash.se>
parents: 1736
diff changeset
   459
module:hook("c2s-read-timeout", handle_read_timeout);