mod_smacks/mod_smacks.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 23 Sep 2022 22:41:15 +0100
changeset 5058 62480053c87b
parent 4775 e227af629736
permissions -rw-r--r--
mod_cloud_notify_encrypted: Additional debug logging when enabling/skipping
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
4449
e13eb0f851c8 mod_smacks: Explicitly request ack when leaving CSI-inactive mode
Kim Alvefur <zash@zash.se>
parents: 4448
diff changeset
     5
-- Copyright (C) 2012-2021 Kim Alvefur
1674
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
3845
b5d367798570 Fix bug readding stanzas to outgoing_queue on resume
tmolitor <thilo@eightysoft.de>
parents: 3650
diff changeset
     8
-- Copyright (C) 2016-2020 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";
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    15
local dep = require "util.dependencies";
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    16
local cache = dep.softreq("util.cache");	-- only available in prosody 0.10+
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
    17
local uuid_generate = require "util.uuid".generate;
2760
dbba101601b4 mod_smacks: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2748
diff changeset
    18
local jid = require "util.jid";
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
    19
3959
017f60608fc8 mod_smacks: also count outgoing MAM messages
JC Brand <jc@opkode.com>
parents: 3946
diff changeset
    20
local t_remove = 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
    21
local math_min = math.min;
3111
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
    22
local math_max = math.max;
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
    23
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
    24
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
    25
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
    26
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
    27
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
    28
4193
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
    29
local xmlns_mam2 = "urn:xmpp:mam:2";
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    30
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
    31
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
    32
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
    33
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
    34
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
    35
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
    36
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
    37
3971
0957ba6aeb99 mod_smacks: Update documentation and slightly adjust default values
tmolitor <thilo@eightysoft.de>
parents: 3946
diff changeset
    38
local resume_timeout = module:get_option_number("smacks_hibernation_time", 600);
4540
8bdb9805bb73 mod_smacks: Enable on s2s by default to see what breaks
Kim Alvefur <zash@zash.se>
parents: 4467
diff changeset
    39
local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", true);
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
    40
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
    41
local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0);
4448
2f5e52d67928 mod_smacks: Do ask for acks while in CSI inactive mode, but less frequent
Kim Alvefur <zash@zash.se>
parents: 4446
diff changeset
    42
local max_inactive_unacked_stanzas = module:get_option_number("smacks_max_inactive_unacked_stanzas", 256);
3971
0957ba6aeb99 mod_smacks: Update documentation and slightly adjust default values
tmolitor <thilo@eightysoft.de>
parents: 3946
diff changeset
    43
local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 30);
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    44
local max_hibernated_sessions = module:get_option_number("smacks_max_hibernated_sessions", 10);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    45
local max_old_sessions = module:get_option_number("smacks_max_old_sessions", 10);
754
713c6791fbcc mod_smacks: Import prosody.core_process_stanza()
Kim Alvefur <zash@zash.se>
parents: 642
diff changeset
    46
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
    47
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
    48
3497
3d4eefdd950a mod_smacks: Raise error on out-of-range config options (thanks marc0s)
Matthew Wild <mwild1@gmail.com>
parents: 3483
diff changeset
    49
assert(max_hibernated_sessions > 0, "smacks_max_hibernated_sessions must be greater than 0");
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
    50
assert(max_old_sessions > 0, "smacks_max_old_sessions must be greater than 0");
3497
3d4eefdd950a mod_smacks: Raise error on out-of-range config options (thanks marc0s)
Matthew Wild <mwild1@gmail.com>
parents: 3483
diff changeset
    51
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
    52
local c2s_sessions = module:shared("/*/c2s/sessions");
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    53
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    54
local function init_session_cache(max_entries, evict_callback)
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    55
	-- old prosody version < 0.10 (no limiting at all!)
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    56
	if not cache then
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    57
		local store = {};
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    58
		return {
2705
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    59
			get = function(user, key)
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    60
				if not user then return nil; end
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    61
				if not key then return nil; end
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    62
				return store[key];
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    63
			end;
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    64
			set = function(user, key, value)
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    65
				if not user then return nil; end
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    66
				if not key then return nil; end
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    67
				store[key] = value;
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    68
			end;
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    69
		};
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    70
	end
3108
626d2c781c66 mod_smacks: send maximum resumption timeout to client
Jonas Wielicki <jonas@wielicki.name>
parents: 2941
diff changeset
    71
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    72
	-- use per user limited cache for prosody >= 0.10
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    73
	local stores = {};
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    74
	return {
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    75
			get = function(user, key)
2705
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    76
				if not user then return nil; end
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    77
				if not key then return nil; end
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    78
				if not stores[user] then
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    79
					stores[user] = cache.new(max_entries, evict_callback);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    80
				end
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    81
				return stores[user]:get(key);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    82
			end;
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    83
			set = function(user, key, value)
2705
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    84
				if not user then return nil; end
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    85
				if not key then return nil; end
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    86
				if not stores[user] then stores[user] = cache.new(max_entries, evict_callback); end
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    87
				stores[user]:set(key, value);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    88
				-- remove empty caches completely
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    89
				if not stores[user]:count() then stores[user] = nil; end
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    90
			end;
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    91
		};
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    92
end
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    93
local old_session_registry = init_session_cache(max_old_sessions, nil);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    94
local session_registry = init_session_cache(max_hibernated_sessions, function(resumption_token, session)
2705
d96831e46b64 Fix #889
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
    95
	if session.destroyed then return true; end		-- destroyed session can always be removed from cache
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    96
	session.log("warn", "User has too much hibernated sessions, removing oldest session (token: %s)", resumption_token);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    97
	-- store old session's h values on force delete
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    98
	-- save only actual h value and username/host (for security)
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
    99
	old_session_registry.set(session.username, resumption_token, {
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   100
		h = session.handled_stanza_count,
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   101
		username = session.username,
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   102
		host = session.host
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   103
	});
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   104
	return true;	-- allow session to be removed from full cache to make room for new one
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   105
end);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   106
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   107
local function stoppable_timer(delay, callback)
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   108
	local stopped = false;
3624
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   109
	local timer = module:add_timer(delay, function (t)
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   110
		if stopped then return; end
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   111
		return callback(t);
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   112
	end);
3625
c2c851722a8a mod_smacks: fix prosody 0.9 compatibility
tmolitor <thilo@eightysoft.de>
parents: 3624
diff changeset
   113
	if timer and timer.stop then return timer; end		-- new prosody api includes stop() function
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   114
	return {
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   115
		stop = function(self) stopped = true end;
3624
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   116
		timer;
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   117
	};
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   118
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
   119
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   120
local function delayed_ack_function(session, stanza)
3650
58047d6f2b89 mod_smacks: fix bug #1405 (prevent timer from running for already destroyed sessions)
tmolitor <thilo@eightysoft.de>
parents: 3644
diff changeset
   121
	-- fire event only if configured to do so and our session is not already hibernated or destroyed
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   122
	if delayed_ack_timeout > 0 and session.awaiting_ack
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   123
	and not session.hibernating and not session.destroyed then
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   124
		session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d",
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   125
			session.outgoing_stanza_queue and #session.outgoing_stanza_queue or 0);
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   126
		module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue, stanza = stanza});
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
   127
	end
2498
d300ae5dba87 mod_smacks: Fix some bugs with smacks-ack-delayed event triggering.
tmolitor <thilo@eightysoft.de>
parents: 2495
diff changeset
   128
	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
   129
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
   130
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
   131
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
   132
	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
   133
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
   134
	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
   135
	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
   136
		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
   137
			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
   138
		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
   139
		return true;
600
7693724881b3 Fix a typo in mod_smacks (type -> session_type).
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 599
diff changeset
   140
	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
   141
		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
   142
	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
   143
	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
   144
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
   145
263
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
   146
module:hook("stream-features",
41f1cac40560 mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents: 258
diff changeset
   147
		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
   148
			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
   149
				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
   150
				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
   151
			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
   152
		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
   153
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
   154
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
   155
		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
   156
			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
   157
				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
   158
				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
   159
			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
   160
		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
   161
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   162
local function request_ack_if_needed(session, force, reason, stanza)
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   163
	local queue = session.outgoing_stanza_queue;
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   164
	local expected_h = session.last_acknowledged_stanza + #queue;
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   165
	-- session.log("debug", "*** SMACKS(1) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating));
4641
242251ce1036 mod_smacks: Use 'smacks_max_inactive_unacked_stanzas' when inactive and no timer
Kim Alvefur <zash@zash.se>
parents: 4636
diff changeset
   166
	local max_unacked = max_unacked_stanzas;
242251ce1036 mod_smacks: Use 'smacks_max_inactive_unacked_stanzas' when inactive and no timer
Kim Alvefur <zash@zash.se>
parents: 4636
diff changeset
   167
	if session.state == "inactive"  then
242251ce1036 mod_smacks: Use 'smacks_max_inactive_unacked_stanzas' when inactive and no timer
Kim Alvefur <zash@zash.se>
parents: 4636
diff changeset
   168
		max_unacked = max_inactive_unacked_stanzas;
242251ce1036 mod_smacks: Use 'smacks_max_inactive_unacked_stanzas' when inactive and no timer
Kim Alvefur <zash@zash.se>
parents: 4636
diff changeset
   169
	end
4448
2f5e52d67928 mod_smacks: Do ask for acks while in CSI inactive mode, but less frequent
Kim Alvefur <zash@zash.se>
parents: 4446
diff changeset
   170
	if session.awaiting_ack == nil and not session.hibernating then
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   171
		-- this check of last_requested_h prevents ack-loops if missbehaving clients report wrong
3638
915e32d5a147 mod_smacks: fix bug for missbehaving clients sending multiple acks in a row
tmolitor <thilo@eightysoft.de>
parents: 3625
diff changeset
   172
		-- stanza counts. it is set when an <r> is really sent (e.g. inside timer), preventing any
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   173
		-- further requests until a higher h-value would be expected.
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   174
		-- session.log("debug", "*** SMACKS(2) ***: #queue=%s, max_unacked_stanzas=%s, expected_h=%s, last_requested_h=%s", tostring(#queue), tostring(max_unacked_stanzas), tostring(expected_h), tostring(session.last_requested_h));
4448
2f5e52d67928 mod_smacks: Do ask for acks while in CSI inactive mode, but less frequent
Kim Alvefur <zash@zash.se>
parents: 4446
diff changeset
   175
		if (#queue > max_unacked and expected_h ~= session.last_requested_h) or force then
3482
f6319346e2a3 mod_smacks: improved debug logging
Georg Lukas <georg@op-co.de>
parents: 3481
diff changeset
   176
			session.log("debug", "Queuing <r> (in a moment) from %s - #queue=%d", reason, #queue);
2628
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   177
			session.awaiting_ack = false;
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   178
			session.awaiting_ack_timer = stoppable_timer(1e-06, function ()
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   179
				-- session.log("debug", "*** SMACKS(3) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating));
3650
58047d6f2b89 mod_smacks: fix bug #1405 (prevent timer from running for already destroyed sessions)
tmolitor <thilo@eightysoft.de>
parents: 3644
diff changeset
   180
				-- only request ack if needed and our session is not already hibernated or destroyed
58047d6f2b89 mod_smacks: fix bug #1405 (prevent timer from running for already destroyed sessions)
tmolitor <thilo@eightysoft.de>
parents: 3644
diff changeset
   181
				if not session.awaiting_ack and not session.hibernating and not session.destroyed then
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   182
					session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, #queue);
2628
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   183
					(session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }))
4541
53ee391ca689 mod_smacks: Fix traceback due to session being destroyed in send()
Kim Alvefur <zash@zash.se>
parents: 4540
diff changeset
   184
					if session.destroyed then return end -- sending something can trigger destruction
3638
915e32d5a147 mod_smacks: fix bug for missbehaving clients sending multiple acks in a row
tmolitor <thilo@eightysoft.de>
parents: 3625
diff changeset
   185
					session.awaiting_ack = true;
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   186
					-- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile)
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   187
					session.last_requested_h = session.last_acknowledged_stanza + #queue;
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   188
					session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, #queue);
2628
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   189
					if not session.delayed_ack_timer then
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   190
						session.delayed_ack_timer = stoppable_timer(delayed_ack_timeout, function()
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   191
							delayed_ack_function(session, nil);		-- we don't know if this is the only new stanza in the queue
2628
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   192
						end);
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   193
					end
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   194
				end
2628
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   195
			end);
c110b6bfe5d1 mod_smacks: Prevent ack loop on misbehaving clients
tmolitor <thilo@eightysoft.de>
parents: 2627
diff changeset
   196
		end
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   197
	end
3108
626d2c781c66 mod_smacks: send maximum resumption timeout to client
Jonas Wielicki <jonas@wielicki.name>
parents: 2941
diff changeset
   198
2717
eea1d5bac451 mod_smacks: Fix delayed_ack event
tmolitor <thilo@eightysoft.de>
parents: 2705
diff changeset
   199
	-- Trigger "smacks-ack-delayed"-event if we added new (ackable) stanzas to the outgoing queue
eea1d5bac451 mod_smacks: Fix delayed_ack event
tmolitor <thilo@eightysoft.de>
parents: 2705
diff changeset
   200
	-- and there isn't already a timer for this event running.
eea1d5bac451 mod_smacks: Fix delayed_ack event
tmolitor <thilo@eightysoft.de>
parents: 2705
diff changeset
   201
	-- If we wouldn't do this, stanzas added to the queue after the first "smacks-ack-delayed"-event
eea1d5bac451 mod_smacks: Fix delayed_ack event
tmolitor <thilo@eightysoft.de>
parents: 2705
diff changeset
   202
	-- would not trigger this event (again).
4641
242251ce1036 mod_smacks: Use 'smacks_max_inactive_unacked_stanzas' when inactive and no timer
Kim Alvefur <zash@zash.se>
parents: 4636
diff changeset
   203
	if #queue > max_unacked and session.awaiting_ack and session.delayed_ack_timer == nil then
2717
eea1d5bac451 mod_smacks: Fix delayed_ack event
tmolitor <thilo@eightysoft.de>
parents: 2705
diff changeset
   204
		session.log("debug", "Calling delayed_ack_function directly (still waiting for ack)");
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   205
		delayed_ack_function(session, stanza);		-- this is the only new stanza in the queue --> provide it to other modules
2717
eea1d5bac451 mod_smacks: Fix delayed_ack event
tmolitor <thilo@eightysoft.de>
parents: 2705
diff changeset
   206
	end
2495
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   207
end
5fbca7de2088 mod_smacks: Send out more ack requests where needed
tmolitor <thilo@eightysoft.de>
parents: 2421
diff changeset
   208
3960
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   209
local function outgoing_stanza_filter(stanza, session)
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   210
	-- XXX: Normally you wouldn't have to check the xmlns for a stanza as it's
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   211
	-- supposed to be nil.
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   212
	-- However, when using mod_smacks with mod_websocket, then mod_websocket's
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   213
	-- stanzas/out filter can get called before this one and adds the xmlns.
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   214
	local is_stanza = stanza.attr and
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   215
		(not stanza.attr.xmlns or stanza.attr.xmlns == 'jabber:client')
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   216
		and not stanza.name:find":";
3959
017f60608fc8 mod_smacks: also count outgoing MAM messages
JC Brand <jc@opkode.com>
parents: 3946
diff changeset
   217
3960
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   218
	if is_stanza and not stanza._cached 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
   219
		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
   220
		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
   221
		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
   222
1603
8e006226e4c5 mod_smacks: Don't attach timestamps to 'iq' stanzas
Kim Alvefur <zash@zash.se>
parents: 1602
diff changeset
   223
		if cached_stanza and cached_stanza.name ~= "iq" and cached_stanza:get_child("delay", xmlns_delay) == nil then
2760
dbba101601b4 mod_smacks: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2748
diff changeset
   224
			cached_stanza = cached_stanza:tag("delay", {
dbba101601b4 mod_smacks: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2748
diff changeset
   225
				xmlns = xmlns_delay,
dbba101601b4 mod_smacks: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2748
diff changeset
   226
				from = jid.bare(session.full_jid or session.host),
dbba101601b4 mod_smacks: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2748
diff changeset
   227
				stamp = datetime.datetime()
dbba101601b4 mod_smacks: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2748
diff changeset
   228
			});
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   229
		end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   230
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   231
		queue[#queue+1] = cached_stanza;
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
   232
		if session.hibernating then
4588
f6f28ceff53a mod_smacks: Include timestamp in hibernation log message
Kim Alvefur <zash@zash.se>
parents: 4543
diff changeset
   233
			session.log("debug", "hibernating since %s, stanza queued", datetime.datetime(session.hibernating));
3624
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   234
			module:fire_event("smacks-hibernation-stanza-queued", {origin = session, queue = queue, stanza = cached_stanza});
2091
e48dbb640408 mod_smacks: Drop stanzas instead of turning them into the empty string
Kim Alvefur <zash@zash.se>
parents: 1885
diff changeset
   235
			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
   236
		end
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   237
		request_ack_if_needed(session, false, "outgoing_stanza_filter", stanza);
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   238
	end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   239
	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
   240
end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   241
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   242
local function count_incoming_stanzas(stanza, session)
3960
ebc1f1d962c5 mod_stanzas: tighten up stanza check and add explanatory text
JC Brand <jc@opkode.com>
parents: 3959
diff changeset
   243
	if not stanza.attr.xmlns 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
   244
		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
   245
		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
   246
	end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   247
	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
   248
end
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   249
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   250
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
   251
	if not resume then
1528
5ccb3ee2cf72 mod_smacks: Remove variable not used after 2881d532f385
Kim Alvefur <zash@zash.se>
parents: 1527
diff changeset
   252
		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
   253
		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
   254
	end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   255
2126
3f788f18cc10 mod_smacks: Adjust filter priorities to avoid conflict with mod_websocket
Kim Alvefur <zash@zash.se>
parents: 2095
diff changeset
   256
	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
   257
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   258
	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
   259
	function session.close(...)
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   260
		if session.resumption_token then
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   261
			session_registry.set(session.username, session.resumption_token, nil);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   262
			old_session_registry.set(session.username, session.resumption_token, nil);
988
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   263
			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
   264
		end
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   265
		-- send out last ack as per revision 1.5.2 of XEP-0198
4635
921c607617b0 mod_smacks: Fix traceback on missing stanza counter
Kim Alvefur <zash@zash.se>
parents: 4588
diff changeset
   266
		if session.smacks and session.conn and session.handled_stanza_count then
2941
e672d1050529 mod_smacks: Ensure stanza count attributes are always strings (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 2760
diff changeset
   267
			(session.sends2s or session.send)(st.stanza("a", { xmlns = session.smacks, h = string.format("%d", session.handled_stanza_count) }));
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   268
		end
988
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   269
		return session_close(...);
c15cea87036f mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents: 987
diff changeset
   270
	end
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   271
	return session;
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   272
end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   273
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   274
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
   275
	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
   276
		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
   277
	end
2126
3f788f18cc10 mod_smacks: Adjust filter priorities to avoid conflict with mod_websocket
Kim Alvefur <zash@zash.se>
parents: 2095
diff changeset
   278
	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
   279
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   280
	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
   281
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
   282
1529
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   283
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
   284
	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
   285
	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
   286
	return session;
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   287
end
16893646a458 mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents: 1528
diff changeset
   288
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   289
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
   290
	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
   291
	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
   292
		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
   293
		(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
   294
		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
   295
	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
   296
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
   297
	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
   298
	session.smacks = xmlns_sm;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   299
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   300
	wrap_session(session, false);
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   301
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
   302
	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
   303
	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
   304
	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
   305
		resume_token = uuid_generate();
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   306
		session_registry.set(session.username, resume_token, 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
   307
		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
   308
	end
3114
06e07b483805 mod_smacks: Convert max number into a string before inserting it into a stanza.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3111
diff changeset
   309
	(session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume, max = tostring(resume_timeout) }));
576
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   310
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   311
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   312
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
   313
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
   314
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
   315
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
   316
		function (session, stanza)
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   317
			stoppable_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
   318
				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
   319
					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
   320
						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
   321
						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
   322
					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
   323
						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
   324
						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
   325
					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
   326
						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
   327
					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
   328
					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
   329
				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
   330
			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
   331
		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
   332
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   333
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
   334
	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
   335
	session.smacks = xmlns_sm;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   336
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
   337
	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
   338
44b69c3d5351 mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents: 478
diff changeset
   339
	-- FIXME Resume?
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   340
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
   341
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   342
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   343
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
   344
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
   345
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   346
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
   347
	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
   348
		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
   349
		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
   350
	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
   351
	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
   352
	-- Reply with <a>
2941
e672d1050529 mod_smacks: Ensure stanza count attributes are always strings (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 2760
diff changeset
   353
	(origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = string.format("%d", origin.handled_stanza_count) }));
3644
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   354
	-- piggyback our own ack request if needed (see request_ack_if_needed() for explanation of last_requested_h)
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   355
	local expected_h = origin.last_acknowledged_stanza + #origin.outgoing_stanza_queue;
b2f32b3c6ec1 mod_smacks: fix bug in bad client handling introduced by last commit
tmolitor <thilo@eightysoft.de>
parents: 3638
diff changeset
   356
	if #origin.outgoing_stanza_queue > 0 and expected_h ~= origin.last_requested_h then
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   357
		request_ack_if_needed(origin, true, "piggybacked by handle_r", nil);
3624
fb1c8dee2ead mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
tmolitor <thilo@eightysoft.de>
parents: 3497
diff changeset
   358
	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
   359
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   360
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   361
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
   362
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
   363
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   364
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
   365
	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
   366
	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
   367
	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
   368
		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
   369
	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
   370
	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
   371
		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
   372
		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
   373
	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
   374
	-- Remove handled stanzas from outgoing_stanza_queue
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   375
	-- origin.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
   376
	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
   377
	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
   378
		origin:close{ condition = "invalid-xml"; text = "Missing or invalid 'h' attribute"; };
3481
1f2381492c9f mod_smacks: no crash on <a> without @h, fix #1317
Georg Lukas <georg@op-co.de>
parents: 3454
diff changeset
   379
		return;
1407
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   380
	end
b631c8a8b9e7 mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents: 1406
diff changeset
   381
	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
   382
	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
   383
	if handled_stanza_count > #queue then
1416
7ddb522d9b28 mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 1408
diff changeset
   384
		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
   385
			handled_stanza_count, #queue);
1416
7ddb522d9b28 mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 1408
diff changeset
   386
		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
   387
		for i=1,#queue do
1416
7ddb522d9b28 mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 1408
diff changeset
   388
			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
   389
		end
4764
367a8020bd92 mod_smacks: Don't forgive client counting errors
tmolitor <thilo@eightysoft.de>
parents: 4685
diff changeset
   390
        origin:close{ condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server"; };
367a8020bd92 mod_smacks: Don't forgive client counting errors
tmolitor <thilo@eightysoft.de>
parents: 4685
diff changeset
   391
        return;
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   392
	end
3454
9b6206f23151 mod_smacks: Fire event on delivery failure/success for each stanza
Matthew Wild <mwild1@gmail.com>
parents: 3222
diff changeset
   393
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
   394
	for i=1,math_min(handled_stanza_count,#queue) do
3454
9b6206f23151 mod_smacks: Fire event on delivery failure/success for each stanza
Matthew Wild <mwild1@gmail.com>
parents: 3222
diff changeset
   395
		local handled_stanza = t_remove(origin.outgoing_stanza_queue, 1);
9b6206f23151 mod_smacks: Fire event on delivery failure/success for each stanza
Matthew Wild <mwild1@gmail.com>
parents: 3222
diff changeset
   396
		module:fire_event("delivery/success", { session = origin, stanza = handled_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
   397
	end
3454
9b6206f23151 mod_smacks: Fire event on delivery failure/success for each stanza
Matthew Wild <mwild1@gmail.com>
parents: 3222
diff changeset
   398
1406
7d76dd2310ef mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents: 1405
diff changeset
   399
	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
   400
	origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count;
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   401
	request_ack_if_needed(origin, false, "handle_a", nil)
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   402
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   403
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   404
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
   405
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
   406
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
   407
--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
   408
-- 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
   409
-- 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
   410
-- 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
   411
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   412
local function 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
   413
	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
   414
	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
   415
	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
   416
		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
   417
		for i=1,#queue do
3454
9b6206f23151 mod_smacks: Fire event on delivery failure/success for each stanza
Matthew Wild <mwild1@gmail.com>
parents: 3222
diff changeset
   418
			if not module:fire_event("delivery/failure", { session = session, stanza = queue[i] }) then
4013
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   419
				if queue[i].attr.type ~= "error" then
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   420
					local reply = st.reply(queue[i]);
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   421
					if reply.attr.to ~= session.full_jid then
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   422
						reply.attr.type = "error";
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   423
						reply:tag("error", error_attr)
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   424
							:tag("recipient-unavailable", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"});
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   425
						core_process_stanza(session, reply);
362997ededb1 mod_smacks: don't send error replys for error stanzas on session timeout
tmolitor <thilo@eightysoft.de>
parents: 3990
diff changeset
   426
					end
3454
9b6206f23151 mod_smacks: Fire event on delivery failure/success for each stanza
Matthew Wild <mwild1@gmail.com>
parents: 3222
diff changeset
   427
				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
   428
			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
   429
		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
   430
	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
   431
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
   432
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   433
-- don't send delivery errors for messages which will be delivered by mam later on
3990
9ec23a457f9a mod_smacks: remove now unused code
tmolitor <thilo@eightysoft.de>
parents: 3989
diff changeset
   434
-- check if stanza was archived --> this will allow us to send back errors for stanzas not archived
9ec23a457f9a mod_smacks: remove now unused code
tmolitor <thilo@eightysoft.de>
parents: 3989
diff changeset
   435
-- because the user configured the server to do so ("no-archive"-setting for one special contact for example)
3988
4e5fde519116 mod_smacks: better logging for georg
tmolitor <thilo@eightysoft.de>
parents: 3987
diff changeset
   436
local function get_stanza_id(stanza, by_jid)
3987
effe2d93a59c mod_smacks: Fix mam handling
tmolitor <thilo@eightysoft.de>
parents: 3972
diff changeset
   437
	for tag in stanza:childtags("stanza-id", "urn:xmpp:sid:0") do
3988
4e5fde519116 mod_smacks: better logging for georg
tmolitor <thilo@eightysoft.de>
parents: 3987
diff changeset
   438
		if tag.attr.by == by_jid then
4e5fde519116 mod_smacks: better logging for georg
tmolitor <thilo@eightysoft.de>
parents: 3987
diff changeset
   439
			return tag.attr.id;
3987
effe2d93a59c mod_smacks: Fix mam handling
tmolitor <thilo@eightysoft.de>
parents: 3972
diff changeset
   440
		end
effe2d93a59c mod_smacks: Fix mam handling
tmolitor <thilo@eightysoft.de>
parents: 3972
diff changeset
   441
	end
3989
c3e9caa29857 mod_smacks: fix typo in last commit
tmolitor <thilo@eightysoft.de>
parents: 3988
diff changeset
   442
	return nil;
3987
effe2d93a59c mod_smacks: Fix mam handling
tmolitor <thilo@eightysoft.de>
parents: 3972
diff changeset
   443
end
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   444
module:hook("delivery/failure", function(event)
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   445
	local session, stanza = event.session, event.stanza;
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   446
	-- Only deal with authenticated (c2s) sessions
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   447
	if session.username then
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   448
		if stanza.name == "message" and stanza.attr.xmlns == nil and
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   449
				( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) then
4193
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   450
			-- don't store messages in offline store if they are mam results
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   451
			local mam_result = stanza:get_child("result", xmlns_mam2);
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   452
			if mam_result ~= nil then
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   453
				return true;		-- stanza already "handled", don't send an error and don't add it to offline storage
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   454
			end
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   455
			-- do nothing here for normal messages and don't send out "message delivery errors",
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   456
			-- because messages are already in MAM at this point (no need to frighten users)
3988
4e5fde519116 mod_smacks: better logging for georg
tmolitor <thilo@eightysoft.de>
parents: 3987
diff changeset
   457
			local stanza_id = get_stanza_id(stanza, jid.bare(session.full_jid));
4e5fde519116 mod_smacks: better logging for georg
tmolitor <thilo@eightysoft.de>
parents: 3987
diff changeset
   458
			if session.mam_requested and stanza_id ~= nil then
4193
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   459
				session.log("debug", "mod_smacks delivery/failure returning true for mam-handled stanza: mam-archive-id=%s", tostring(stanza_id));
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   460
				return true;		-- stanza handled, don't send an error
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   461
			end
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   462
			-- store message in offline store, if this client does not use mam *and* was the last client online
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   463
			local sessions = prosody.hosts[module.host].sessions[session.username] and
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   464
					prosody.hosts[module.host].sessions[session.username].sessions or nil;
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   465
			if sessions and next(sessions) == session.resource and next(sessions, session.resource) == nil then
4685
edbd84bbd8fb mod_smacks: Include username in offline event, mirroring Prosody 0.10+
Kim Alvefur <zash@zash.se>
parents: 4641
diff changeset
   466
				local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza });
3987
effe2d93a59c mod_smacks: Fix mam handling
tmolitor <thilo@eightysoft.de>
parents: 3972
diff changeset
   467
				session.log("debug", "mod_smacks delivery/failuere returning %s for offline-handled stanza", tostring(ok));
4193
22e7b3d6fcae mod_smacks: don't store mam results in offline store
tmolitor <thilo@eightysoft.de>
parents: 4013
diff changeset
   468
				return ok;			-- if stanza was handled, don't send an error
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   469
			end
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   470
		end
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   471
	end
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   472
end);
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   473
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
   474
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
   475
	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
   476
	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
   477
		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
   478
			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
   479
			if #queue > 0 then
3154
14b6ad2b773f mod_smacks: Reduce unacked stanza log message from warn->debug
Matthew Wild <mwild1@gmail.com>
parents: 3114
diff changeset
   480
				session.log("debug", "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
   481
				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
   482
			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
   483
		else
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   484
			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
   485
			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
   486
			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
   487
			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
   488
			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
   489
			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
   490
				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
   491
				-- 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
   492
				-- 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
   493
				-- (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
   494
				-- 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
   495
				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
   496
				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
   497
					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
   498
				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
   499
				-- 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
   500
				-- 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
   501
				and session.hibernating == hibernate_time then
3111
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   502
					-- wait longer if the timeout isn't reached because push was enabled for this session
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   503
					-- session.first_hibernated_push is the starting point for hibernation timeouts of those push enabled clients
4636
ab7dc5c5c782 mod_smacks: Fix typo in comment (thanks codespell)
Kim Alvefur <zash@zash.se>
parents: 4635
diff changeset
   504
					-- wait for an additional resume_timeout seconds if no push occurred since hibernation at all
3111
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   505
					local current_time = os_time();
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   506
					local timeout_start = math_max(session.hibernating, session.first_hibernated_push or session.hibernating);
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   507
					if session.push_identifier ~= nil and not session.first_hibernated_push then
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   508
						session.log("debug", "No push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout);
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   509
						return resume_timeout;
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   510
					end
4543
4e7aa32f58d9 mod_smacks: Fix calculation of smacks timeout values
tmolitor <thilo@eightysoft.de>
parents: 4541
diff changeset
   511
					if session.push_identifier ~= nil and current_time-timeout_start < resume_timeout then
4e7aa32f58d9 mod_smacks: Fix calculation of smacks timeout values
tmolitor <thilo@eightysoft.de>
parents: 4541
diff changeset
   512
						session.log("debug", "A push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout-(current_time-timeout_start));
4e7aa32f58d9 mod_smacks: Fix calculation of smacks timeout values
tmolitor <thilo@eightysoft.de>
parents: 4541
diff changeset
   513
						return resume_timeout-(current_time-timeout_start);		-- time left to wait
3111
f703cc6e72df mod_smacks: defer timeouts for push enabled clients
tmolitor <thilo@eightysoft.de>
parents: 3108
diff changeset
   514
					end
587
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   515
					session.log("debug", "Destroying session for hibernating too long");
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   516
					session_registry.set(session.username, session.resumption_token, nil);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   517
					-- save only actual h value and username/host (for security)
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   518
					old_session_registry.set(session.username, session.resumption_token, {
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   519
						h = session.handled_stanza_count,
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   520
						username = session.username,
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   521
						host = session.host
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   522
					});
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
   523
					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
   524
					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
   525
				else
f733e7599ed6 mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 576
diff changeset
   526
					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
   527
				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
   528
			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
   529
			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
   530
		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
   531
	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
   532
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
   533
1736
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   534
local function handle_s2s_destroyed(event)
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   535
	local session = event.session;
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   536
	local queue = session.outgoing_stanza_queue;
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   537
	if queue and #queue > 0 then
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   538
		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
   539
		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
   540
			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
   541
				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
   542
			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
   543
			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
   544
		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
   545
			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
   546
		end
1736
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   547
	end
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   548
end
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   549
2f9ee9ed6267 mod_smacks: Handle unacked messages in s2s queues
Kim Alvefur <zash@zash.se>
parents: 1710
diff changeset
   550
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
   551
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
   552
3222
67f1d1f22625 mod_smacks: Improve logging on resume so session changes are more easily tracked
Matthew Wild <mwild1@gmail.com>
parents: 3154
diff changeset
   553
local function get_session_id(session)
67f1d1f22625 mod_smacks: Improve logging on resume so session changes are more easily tracked
Matthew Wild <mwild1@gmail.com>
parents: 3154
diff changeset
   554
	return session.id or (tostring(session):match("[a-f0-9]+$"));
67f1d1f22625 mod_smacks: Improve logging on resume so session changes are more easily tracked
Matthew Wild <mwild1@gmail.com>
parents: 3154
diff changeset
   555
end
67f1d1f22625 mod_smacks: Improve logging on resume so session changes are more easily tracked
Matthew Wild <mwild1@gmail.com>
parents: 3154
diff changeset
   556
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   557
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
   558
	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
   559
		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
   560
		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
   561
			: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
   562
		);
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   563
		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
   564
	end
720b8268778e mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents: 908
diff changeset
   565
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
   566
	local id = stanza.attr.previd;
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   567
	local original_session = session_registry.get(session.username, id);
345
445178d15b51 mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents: 263
diff changeset
   568
	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
   569
		session.log("debug", "Tried to resume non-existent session with id %s", id);
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   570
		local old_session = old_session_registry.get(session.username, id);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   571
		if old_session and session.username == old_session.username
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   572
		and session.host == old_session.host
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   573
		and old_session.h then
2941
e672d1050529 mod_smacks: Ensure stanza count attributes are always strings (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 2760
diff changeset
   574
			session.send(st.stanza("failed", { xmlns = xmlns_sm, h = string.format("%d", old_session.h) })
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   575
				:tag("item-not-found", { xmlns = xmlns_errors })
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   576
			);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   577
		else
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   578
			session.send(st.stanza("failed", { xmlns = xmlns_sm })
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   579
				:tag("item-not-found", { xmlns = xmlns_errors })
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   580
			);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   581
		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
   582
	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
   583
	and session.host == original_session.host then
3222
67f1d1f22625 mod_smacks: Improve logging on resume so session changes are more easily tracked
Matthew Wild <mwild1@gmail.com>
parents: 3154
diff changeset
   584
		session.log("debug", "mod_smacks resuming existing session %s...", get_session_id(original_session));
3482
f6319346e2a3 mod_smacks: improved debug logging
Georg Lukas <georg@op-co.de>
parents: 3481
diff changeset
   585
		original_session.log("debug", "mod_smacks session resumed from %s...", get_session_id(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
   586
		-- 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
   587
		if original_session.conn then
3222
67f1d1f22625 mod_smacks: Improve logging on resume so session changes are more easily tracked
Matthew Wild <mwild1@gmail.com>
parents: 3154
diff changeset
   588
			original_session.log("debug", "mod_smacks closing an old connection for this 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
   589
			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
   590
			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
   591
			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
   592
		end
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   593
		local migrated_session_log = session.log;
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
   594
		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
   595
		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
   596
		original_session.send = session.send;
2627
a65260300708 mod_smacks: Made module more error resistant
tmolitor <thilo@eightysoft.de>
parents: 2612
diff changeset
   597
		original_session.close = session.close;
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
   598
		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
   599
		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
   600
		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
   601
		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
   602
		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
   603
		original_session.hibernating = nil;
3483
9ef309fb501d mod_smacks: apply resumed logger and type to new session, fix #1229
Georg Lukas <georg@op-co.de>
parents: 3482
diff changeset
   604
		session.log = original_session.log;
9ef309fb501d mod_smacks: apply resumed logger and type to new session, fix #1229
Georg Lukas <georg@op-co.de>
parents: 3482
diff changeset
   605
		session.type = original_session.type;
1520
2881d532f385 mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents: 1518
diff changeset
   606
		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
   607
		-- 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
   608
		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
   609
		-- 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
   610
		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
   611
2627
a65260300708 mod_smacks: Made module more error resistant
tmolitor <thilo@eightysoft.de>
parents: 2612
diff changeset
   612
		original_session.send(st.stanza("resumed", { xmlns = xmlns_sm,
2941
e672d1050529 mod_smacks: Ensure stanza count attributes are always strings (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 2760
diff changeset
   613
			h = string.format("%d", original_session.handled_stanza_count), previd = id }));
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   614
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
   615
		-- 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
   616
		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
   617
			h = stanza.attr.h }));
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
   618
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
   619
		-- 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
   620
		-- ...they are what is now left in the outgoing stanza queue
3845
b5d367798570 Fix bug readding stanzas to outgoing_queue on resume
tmolitor <thilo@eightysoft.de>
parents: 3650
diff changeset
   621
		-- We have to use the send of "session" because we don't want to add our resent stanzas
b5d367798570 Fix bug readding stanzas to outgoing_queue on resume
tmolitor <thilo@eightysoft.de>
parents: 3650
diff changeset
   622
		-- to the outgoing queue again
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
   623
		local queue = original_session.outgoing_stanza_queue;
3845
b5d367798570 Fix bug readding stanzas to outgoing_queue on resume
tmolitor <thilo@eightysoft.de>
parents: 3650
diff changeset
   624
		session.log("debug", "resending all unacked stanzas that are still queued after resume, #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
   625
		for i=1,#queue do
3845
b5d367798570 Fix bug readding stanzas to outgoing_queue on resume
tmolitor <thilo@eightysoft.de>
parents: 3650
diff changeset
   626
			session.send(queue[i]);
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
   627
		end
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   628
		session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", #queue);
2627
a65260300708 mod_smacks: Made module more error resistant
tmolitor <thilo@eightysoft.de>
parents: 2612
diff changeset
   629
		function session.send(stanza)
3939
c49fea05772e mod_smacks: handle mam-enabled session internally
tmolitor <thilo@eightysoft.de>
parents: 3845
diff changeset
   630
			migrated_session_log("error", "Tried to send stanza on old session migrated by smacks resume (maybe there is a bug?): %s", tostring(stanza));
2627
a65260300708 mod_smacks: Made module more error resistant
tmolitor <thilo@eightysoft.de>
parents: 2612
diff changeset
   631
			return false;
a65260300708 mod_smacks: Made module more error resistant
tmolitor <thilo@eightysoft.de>
parents: 2612
diff changeset
   632
		end
2748
f70c02c14161 mod_smacks: Trigger event smacks-hibernation-end after queue resend
tmolitor <thilo@eightysoft.de>
parents: 2731
diff changeset
   633
		module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue});
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   634
		request_ack_if_needed(original_session, true, "handle_resume", 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
   635
	else
755
bab7c4ace803 mod_smacks: Fix log statement
Kim Alvefur <zash@zash.se>
parents: 754
diff changeset
   636
		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
   637
			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
   638
			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
   639
		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
   640
			: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
   641
	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
   642
	return true;
1298
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   643
end
659da45a2b4b mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1293
diff changeset
   644
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
   645
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
   646
4449
e13eb0f851c8 mod_smacks: Explicitly request ack when leaving CSI-inactive mode
Kim Alvefur <zash@zash.se>
parents: 4448
diff changeset
   647
module:hook("csi-client-active", function (event)
4462
5234d0c8883d mod_smacks: Only act on CSI state changes if smacks is enabled
Kim Alvefur <zash@zash.se>
parents: 4450
diff changeset
   648
	if event.origin.smacks then
5234d0c8883d mod_smacks: Only act on CSI state changes if smacks is enabled
Kim Alvefur <zash@zash.se>
parents: 4450
diff changeset
   649
		request_ack_if_needed(event.origin, true, "csi-active", nil);
5234d0c8883d mod_smacks: Only act on CSI state changes if smacks is enabled
Kim Alvefur <zash@zash.se>
parents: 4450
diff changeset
   650
	end
4449
e13eb0f851c8 mod_smacks: Explicitly request ack when leaving CSI-inactive mode
Kim Alvefur <zash@zash.se>
parents: 4448
diff changeset
   651
end);
e13eb0f851c8 mod_smacks: Explicitly request ack when leaving CSI-inactive mode
Kim Alvefur <zash@zash.se>
parents: 4448
diff changeset
   652
4775
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   653
module:hook("csi-flushing", function(event)
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   654
	local session = event.session;
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   655
	if session.smacks then
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   656
		if not session.awaiting_ack and not session.hibernating and not session.destroyed then
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   657
			session.log("debug", "Sending <r> (csi-flushing)");
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   658
			session.awaiting_ack = true; -- The send() call may invoke this event again, so set this first
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   659
			(session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }))
e227af629736 mod_smacks: Send <r> immediately from csi-flushing event
Kim Alvefur <zash@zash.se>
parents: 4764
diff changeset
   660
		end
4462
5234d0c8883d mod_smacks: Only act on CSI state changes if smacks is enabled
Kim Alvefur <zash@zash.se>
parents: 4450
diff changeset
   661
	end
4450
3d593b612e07 mod_smacks: Request ack on signal of ongoing CSI flush
Kim Alvefur <zash@zash.se>
parents: 4449
diff changeset
   662
end);
3d593b612e07 mod_smacks: Request ack on signal of ongoing CSI flush
Kim Alvefur <zash@zash.se>
parents: 4449
diff changeset
   663
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
   664
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
   665
	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
   666
	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
   667
		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
   668
			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
   669
				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
   670
			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
   671
			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
   672
				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
   673
				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
   674
			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
   675
			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
   676
		end
2095
ea95637cf041 mod_smacks: Log when sending <r> from read timeout event (0.10+)
Kim Alvefur <zash@zash.se>
parents: 2094
diff changeset
   677
		session.log("debug", "Sending <r> (read timeout)");
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
   678
		(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
   679
		session.awaiting_ack = true;
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   680
		if not session.delayed_ack_timer then
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   681
			session.delayed_ack_timer = stoppable_timer(delayed_ack_timeout, function()
4417
0b9501f82e63 mod_smacks: allow O(1) processing of delayed ack events
tmolitor <thilo@eightysoft.de>
parents: 4193
diff changeset
   682
				delayed_ack_function(session, nil);
2600
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   683
			end);
ffb6646b4253 Implement XEP-0198 revision 1.5.2 and limit number of hibernated sessions per user
tmolitor <thilo@eightysoft.de>
parents: 2498
diff changeset
   684
		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
   685
		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
   686
	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
   687
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
   688
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
   689
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
   690
module:hook("c2s-read-timeout", handle_read_timeout);