Software /
code /
prosody
Annotate
plugins/mod_smacks.lua @ 11934:65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 16 Nov 2021 21:15:22 +0100 |
child | 11935:4d0d10fabb82 |
rev | line source |
---|---|
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- XEP-0198: Stream Management for Prosody IM |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Copyright (C) 2010-2015 Matthew Wild |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- Copyright (C) 2010 Waqas Hussain |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- Copyright (C) 2012-2021 Kim Alvefur |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- Copyright (C) 2012 Thijs Alkemade |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- Copyright (C) 2014 Florian Zeitz |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- Copyright (C) 2016-2020 Thilo Molitor |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 -- |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 -- This project is MIT/X11 licensed. Please see the |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 -- COPYING file in the source package for more information. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 -- |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local st = require "util.stanza"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local dep = require "util.dependencies"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local cache = dep.softreq("util.cache"); -- only available in prosody 0.10+ |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local uuid_generate = require "util.uuid".generate; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local jid = require "util.jid"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local t_remove = table.remove; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local math_min = math.min; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local math_max = math.max; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local os_time = os.time; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local tonumber, tostring = tonumber, tostring; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local add_filter = require "util.filters".add_filter; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local timer = require "util.timer"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local datetime = require "util.datetime"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 local xmlns_mam2 = "urn:xmpp:mam:2"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local xmlns_sm2 = "urn:xmpp:sm:2"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 local xmlns_sm3 = "urn:xmpp:sm:3"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local xmlns_errors = "urn:ietf:params:xml:ns:xmpp-stanzas"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local xmlns_delay = "urn:xmpp:delay"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local sm2_attr = { xmlns = xmlns_sm2 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local sm3_attr = { xmlns = xmlns_sm3 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 local resume_timeout = module:get_option_number("smacks_hibernation_time", 600); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", true); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 local s2s_resend = module:get_option_boolean("smacks_s2s_resend", false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 local max_inactive_unacked_stanzas = module:get_option_number("smacks_max_inactive_unacked_stanzas", 256); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 30); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 local max_hibernated_sessions = module:get_option_number("smacks_max_hibernated_sessions", 10); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 local max_old_sessions = module:get_option_number("smacks_max_old_sessions", 10); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 local core_process_stanza = prosody.core_process_stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 local sessionmanager = require"core.sessionmanager"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 assert(max_hibernated_sessions > 0, "smacks_max_hibernated_sessions must be greater than 0"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 assert(max_old_sessions > 0, "smacks_max_old_sessions must be greater than 0"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 local c2s_sessions = module:shared("/*/c2s/sessions"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 local function init_session_cache(max_entries, evict_callback) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 -- old prosody version < 0.10 (no limiting at all!) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 if not cache then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 local store = {}; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 return { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 get = function(user, key) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 if not user then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 if not key then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return store[key]; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 set = function(user, key, value) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 if not user then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if not key then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 store[key] = value; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 -- use per user limited cache for prosody >= 0.10 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 local stores = {}; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 return { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 get = function(user, key) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if not user then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 if not key then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 if not stores[user] then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 stores[user] = cache.new(max_entries, evict_callback); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 return stores[user]:get(key); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 set = function(user, key, value) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 if not user then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 if not key then return nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 if not stores[user] then stores[user] = cache.new(max_entries, evict_callback); end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 stores[user]:set(key, value); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 -- remove empty caches completely |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 if not stores[user]:count() then stores[user] = nil; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 local old_session_registry = init_session_cache(max_old_sessions, nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local session_registry = init_session_cache(max_hibernated_sessions, function(resumption_token, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 if session.destroyed then return true; end -- destroyed session can always be removed from cache |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 session.log("warn", "User has too much hibernated sessions, removing oldest session (token: %s)", resumption_token); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 -- store old session's h values on force delete |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 -- save only actual h value and username/host (for security) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 old_session_registry.set(session.username, resumption_token, { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 h = session.handled_stanza_count, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 username = session.username, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 host = session.host |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 return true; -- allow session to be removed from full cache to make room for new one |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 local function stoppable_timer(delay, callback) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 local stopped = false; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 local timer = module:add_timer(delay, function (t) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 if stopped then return; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 return callback(t); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 if timer and timer.stop then return timer; end -- new prosody api includes stop() function |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 return { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 stop = function(self) stopped = true end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 timer; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 local function delayed_ack_function(session, stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 -- fire event only if configured to do so and our session is not already hibernated or destroyed |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 if delayed_ack_timeout > 0 and session.awaiting_ack |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 and not session.hibernating and not session.destroyed then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 session.outgoing_stanza_queue and #session.outgoing_stanza_queue or 0); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue, stanza = stanza}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 session.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 local function can_do_smacks(session, advertise_only) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 local session_type = session.type; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 if session.username then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 return false, "unexpected-request", "Client must bind a resource before enabling stream management"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 elseif s2s_smacks and (session_type == "s2sin" or session_type == "s2sout") then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 return false, "service-unavailable", "Stream management is not available for this stream"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 module:hook("stream-features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 if can_do_smacks(event.origin, true) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 event.features:tag("sm", sm2_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 event.features:tag("sm", sm3_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 module:hook("s2s-stream-features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 if can_do_smacks(event.origin, true) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 event.features:tag("sm", sm2_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 event.features:tag("sm", sm3_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 local function request_ack_if_needed(session, force, reason, stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 local expected_h = session.last_acknowledged_stanza + #queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 -- session.log("debug", "*** SMACKS(1) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local max_unacked = max_unacked_stanzas; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 if session.state == "inactive" then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 max_unacked = max_inactive_unacked_stanzas; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 if session.awaiting_ack == nil and not session.hibernating then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 -- this check of last_requested_h prevents ack-loops if missbehaving clients report wrong |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 -- stanza counts. it is set when an <r> is really sent (e.g. inside timer), preventing any |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 -- further requests until a higher h-value would be expected. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
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)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 if (#queue > max_unacked and expected_h ~= session.last_requested_h) or force then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 session.log("debug", "Queuing <r> (in a moment) from %s - #queue=%d", reason, #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 session.awaiting_ack = false; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 session.awaiting_ack_timer = stoppable_timer(1e-06, function () |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 -- session.log("debug", "*** SMACKS(3) ***: awaiting_ack=%s, hibernating=%s", tostring(session.awaiting_ack), tostring(session.hibernating)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 -- only request ack if needed and our session is not already hibernated or destroyed |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 if not session.awaiting_ack and not session.hibernating and not session.destroyed then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 if session.destroyed then return end -- sending something can trigger destruction |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 session.awaiting_ack = true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 session.last_requested_h = session.last_acknowledged_stanza + #queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 if not session.delayed_ack_timer then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 session.delayed_ack_timer = stoppable_timer(delayed_ack_timeout, function() |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 delayed_ack_function(session, nil); -- we don't know if this is the only new stanza in the queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 -- Trigger "smacks-ack-delayed"-event if we added new (ackable) stanzas to the outgoing queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 -- and there isn't already a timer for this event running. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 -- If we wouldn't do this, stanzas added to the queue after the first "smacks-ack-delayed"-event |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 -- would not trigger this event (again). |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 if #queue > max_unacked and session.awaiting_ack and session.delayed_ack_timer == nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 session.log("debug", "Calling delayed_ack_function directly (still waiting for ack)"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 delayed_ack_function(session, stanza); -- this is the only new stanza in the queue --> provide it to other modules |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 local function outgoing_stanza_filter(stanza, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 -- XXX: Normally you wouldn't have to check the xmlns for a stanza as it's |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 -- supposed to be nil. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 -- However, when using mod_smacks with mod_websocket, then mod_websocket's |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 -- stanzas/out filter can get called before this one and adds the xmlns. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 local is_stanza = stanza.attr and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 (not stanza.attr.xmlns or stanza.attr.xmlns == 'jabber:client') |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 and not stanza.name:find":"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 if is_stanza and not stanza._cached then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 local cached_stanza = st.clone(stanza); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 cached_stanza._cached = true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 if cached_stanza and cached_stanza.name ~= "iq" and cached_stanza:get_child("delay", xmlns_delay) == nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 cached_stanza = cached_stanza:tag("delay", { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 xmlns = xmlns_delay, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 from = jid.bare(session.full_jid or session.host), |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 stamp = datetime.datetime() |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 queue[#queue+1] = cached_stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 if session.hibernating then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 session.log("debug", "hibernating since %s, stanza queued", datetime.datetime(session.hibernating)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 module:fire_event("smacks-hibernation-stanza-queued", {origin = session, queue = queue, stanza = cached_stanza}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 return nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 request_ack_if_needed(session, false, "outgoing_stanza_filter", stanza); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 return stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 local function count_incoming_stanzas(stanza, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 if not stanza.attr.xmlns then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 session.handled_stanza_count = session.handled_stanza_count + 1; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 return stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 local function wrap_session_out(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 if not resume then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 session.outgoing_stanza_queue = {}; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 session.last_acknowledged_stanza = 0; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 add_filter(session, "stanzas/out", outgoing_stanza_filter, -999); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 local session_close = session.close; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 function session.close(...) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 if session.resumption_token then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 session_registry.set(session.username, session.resumption_token, nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 old_session_registry.set(session.username, session.resumption_token, nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 session.resumption_token = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 -- send out last ack as per revision 1.5.2 of XEP-0198 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 if session.smacks and session.conn and session.handled_stanza_count then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 (session.sends2s or session.send)(st.stanza("a", { xmlns = session.smacks, h = string.format("%d", session.handled_stanza_count) })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 return session_close(...); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 local function wrap_session_in(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 if not resume then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 session.handled_stanza_count = 0; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 add_filter(session, "stanzas/in", count_incoming_stanzas, 999); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 local function wrap_session(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 wrap_session_out(session, resume); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 wrap_session_in(session, resume); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 function handle_enable(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 local ok, err, err_text = can_do_smacks(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 if not ok then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
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? |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 (session.sends2s or session.send)(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors})); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 module:log("debug", "Enabling stream management"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 session.smacks = xmlns_sm; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
299 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 wrap_session(session, false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 local resume_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 local resume = stanza.attr.resume; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 if resume == "true" or resume == "1" then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 resume_token = uuid_generate(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 session_registry.set(session.username, resume_token, session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 session.resumption_token = resume_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 (session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume, max = tostring(resume_timeout) })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
311 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 module:hook_stanza(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
313 module:hook_stanza(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 module:hook_stanza("http://etherx.jabber.org/streams", "features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 function (session, stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 stoppable_timer(1e-6, function () |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 if can_do_smacks(session) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 if stanza:get_child("sm", xmlns_sm3) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
320 session.sends2s(st.stanza("enable", sm3_attr)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
321 session.smacks = xmlns_sm3; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 elseif stanza:get_child("sm", xmlns_sm2) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
323 session.sends2s(st.stanza("enable", sm2_attr)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 session.smacks = xmlns_sm2; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 wrap_session_out(session, false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 function handle_enabled(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 module:log("debug", "Enabling stream management"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 session.smacks = xmlns_sm; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 wrap_session_in(session, false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 -- FIXME Resume? |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 module:hook_stanza(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 module:hook_stanza(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 function handle_r(origin, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 if not origin.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 module:log("debug", "Received ack request from non-smack-enabled session"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 -- Reply with <a> |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = string.format("%d", origin.handled_stanza_count) })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 -- piggyback our own ack request if needed (see request_ack_if_needed() for explanation of last_requested_h) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 local expected_h = origin.last_acknowledged_stanza + #origin.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 if #origin.outgoing_stanza_queue > 0 and expected_h ~= origin.last_requested_h then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 request_ack_if_needed(origin, true, "piggybacked by handle_r", nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
359 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
360 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 module:hook_stanza(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 module:hook_stanza(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
364 function handle_a(origin, stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 if not origin.smacks then return; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 origin.awaiting_ack = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 if origin.awaiting_ack_timer then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
368 origin.awaiting_ack_timer:stop(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 if origin.delayed_ack_timer then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 origin.delayed_ack_timer:stop(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 origin.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 -- Remove handled stanzas from outgoing_stanza_queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 -- origin.log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or ""); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 local h = tonumber(stanza.attr.h); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 if not h then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 origin:close{ condition = "invalid-xml"; text = "Missing or invalid 'h' attribute"; }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 local handled_stanza_count = h-origin.last_acknowledged_stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
382 local queue = origin.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
383 if handled_stanza_count > #queue then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 origin.log("warn", "The client says it handled %d new stanzas, but we only sent %d :)", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 handled_stanza_count, #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
387 for i=1,#queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 origin.log("debug", "Q item %d: %s", i, tostring(queue[i])); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
390 origin:close{ condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server"; }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
391 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
394 for i=1,math_min(handled_stanza_count,#queue) do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 local handled_stanza = t_remove(origin.outgoing_stanza_queue, 1); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
396 module:fire_event("delivery/success", { session = origin, stanza = handled_stanza }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
397 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
398 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
399 origin.log("debug", "#queue = %d", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
400 origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
401 request_ack_if_needed(origin, false, "handle_a", nil) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
402 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
403 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
404 module:hook_stanza(xmlns_sm2, "a", handle_a); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
405 module:hook_stanza(xmlns_sm3, "a", handle_a); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
406 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
407 --TODO: Optimise... incoming stanzas should be handled by a per-session |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
408 -- function that has a counter as an upvalue (no table indexing for increments, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
409 -- and won't slow non-198 sessions). We can also then remove the .handled flag |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
410 -- on stanzas |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
412 local function handle_unacked_stanzas(session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
413 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 local error_attr = { type = "cancel" }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
415 if #queue > 0 then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
416 session.outgoing_stanza_queue = {}; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
417 for i=1,#queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
418 if not module:fire_event("delivery/failure", { session = session, stanza = queue[i] }) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
419 if queue[i].attr.type ~= "error" then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
420 local reply = st.reply(queue[i]); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
421 if reply.attr.to ~= session.full_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
422 reply.attr.type = "error"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
423 reply:tag("error", error_attr) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
424 :tag("recipient-unavailable", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 core_process_stanza(session, reply); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
426 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
427 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
431 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
432 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 -- don't send delivery errors for messages which will be delivered by mam later on |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
434 -- check if stanza was archived --> this will allow us to send back errors for stanzas not archived |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
435 -- because the user configured the server to do so ("no-archive"-setting for one special contact for example) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
436 local function get_stanza_id(stanza, by_jid) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 for tag in stanza:childtags("stanza-id", "urn:xmpp:sid:0") do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
438 if tag.attr.by == by_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
439 return tag.attr.id; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
440 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 return nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
443 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 module:hook("delivery/failure", function(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
445 local session, stanza = event.session, event.stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
446 -- Only deal with authenticated (c2s) sessions |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
447 if session.username then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 if stanza.name == "message" and stanza.attr.xmlns == nil and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
449 ( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
450 -- don't store messages in offline store if they are mam results |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 local mam_result = stanza:get_child("result", xmlns_mam2); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
452 if mam_result ~= nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
453 return true; -- stanza already "handled", don't send an error and don't add it to offline storage |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 -- do nothing here for normal messages and don't send out "message delivery errors", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
456 -- because messages are already in MAM at this point (no need to frighten users) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
457 local stanza_id = get_stanza_id(stanza, jid.bare(session.full_jid)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
458 if session.mam_requested and stanza_id ~= nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
459 session.log("debug", "mod_smacks delivery/failure returning true for mam-handled stanza: mam-archive-id=%s", tostring(stanza_id)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 return true; -- stanza handled, don't send an error |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 -- store message in offline store, if this client does not use mam *and* was the last client online |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 local sessions = prosody.hosts[module.host].sessions[session.username] and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 prosody.hosts[module.host].sessions[session.username].sessions or nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
465 if sessions and next(sessions) == session.resource and next(sessions, session.resource) == nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
466 local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
467 session.log("debug", "mod_smacks delivery/failuere returning %s for offline-handled stanza", tostring(ok)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
468 return ok; -- if stanza was handled, don't send an error |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
469 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
470 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
473 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
474 module:hook("pre-resource-unbind", function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
475 local session, err = event.session, event.error; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 if session.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
477 if not session.resumption_token then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
478 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 if #queue > 0 then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
480 session.log("debug", "Destroying session with %d unacked stanzas", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
481 handle_unacked_stanzas(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
483 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
484 session.log("debug", "mod_smacks hibernating session for up to %d seconds", resume_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
485 local hibernate_time = os_time(); -- Track the time we went into hibernation |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
486 session.hibernating = hibernate_time; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
487 local resumption_token = session.resumption_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
488 module:fire_event("smacks-hibernation-start", {origin = session, queue = session.outgoing_stanza_queue}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
489 timer.add_task(resume_timeout, function () |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
490 session.log("debug", "mod_smacks hibernation timeout reached..."); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
491 -- We need to check the current resumption token for this resource |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
492 -- matches the smacks session this timer is for in case it changed |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
493 -- (for example, the client may have bound a new resource and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
494 -- started a new smacks session, or not be using smacks) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
495 local curr_session = full_sessions[session.full_jid]; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 if session.destroyed then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 session.log("debug", "The session has already been destroyed"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
498 elseif curr_session and curr_session.resumption_token == resumption_token |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
499 -- Check the hibernate time still matches what we think it is, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
500 -- otherwise the session resumed and re-hibernated. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
501 and session.hibernating == hibernate_time then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
502 -- wait longer if the timeout isn't reached because push was enabled for this session |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
503 -- session.first_hibernated_push is the starting point for hibernation timeouts of those push enabled clients |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
504 -- wait for an additional resume_timeout seconds if no push occurred since hibernation at all |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
505 local current_time = os_time(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
506 local timeout_start = math_max(session.hibernating, session.first_hibernated_push or session.hibernating); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
507 if session.push_identifier ~= nil and not session.first_hibernated_push then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
508 session.log("debug", "No push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
509 return resume_timeout; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
510 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
511 if session.push_identifier ~= nil and current_time-timeout_start < resume_timeout then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
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)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
513 return resume_timeout-(current_time-timeout_start); -- time left to wait |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
514 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
515 session.log("debug", "Destroying session for hibernating too long"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
516 session_registry.set(session.username, session.resumption_token, nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
517 -- save only actual h value and username/host (for security) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
518 old_session_registry.set(session.username, session.resumption_token, { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
519 h = session.handled_stanza_count, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
520 username = session.username, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
521 host = session.host |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
522 }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
523 session.resumption_token = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
524 sessionmanager.destroy_session(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
525 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
526 session.log("debug", "Session resumed before hibernation timeout, all is well") |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
527 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
528 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
529 return true; -- Postpone destruction for now |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
530 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
531 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
532 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
533 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
534 local function handle_s2s_destroyed(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
535 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
536 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
537 if queue and #queue > 0 then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
538 session.log("warn", "Destroying session with %d unacked stanzas", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
539 if s2s_resend then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
540 for i = 1, #queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
541 module:send(queue[i]); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
542 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
543 session.outgoing_stanza_queue = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
544 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
545 handle_unacked_stanzas(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
546 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
547 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
548 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
549 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
550 module:hook("s2sout-destroyed", handle_s2s_destroyed); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
551 module:hook("s2sin-destroyed", handle_s2s_destroyed); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
552 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
553 local function get_session_id(session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
554 return session.id or (tostring(session):match("[a-f0-9]+$")); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
555 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
556 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
557 function handle_resume(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
558 if session.full_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 session.log("warn", "Tried to resume after resource binding"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
560 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
561 :tag("unexpected-request", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
562 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
565 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
566 local id = stanza.attr.previd; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
567 local original_session = session_registry.get(session.username, id); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
568 if not original_session then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
569 session.log("debug", "Tried to resume non-existent session with id %s", id); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
570 local old_session = old_session_registry.get(session.username, id); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
571 if old_session and session.username == old_session.username |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
572 and session.host == old_session.host |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
573 and old_session.h then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
574 session.send(st.stanza("failed", { xmlns = xmlns_sm, h = string.format("%d", old_session.h) }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
575 :tag("item-not-found", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
576 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
577 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
578 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
579 :tag("item-not-found", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
580 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
581 end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
582 elseif session.username == original_session.username |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
583 and session.host == original_session.host then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
584 session.log("debug", "mod_smacks resuming existing session %s...", get_session_id(original_session)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 original_session.log("debug", "mod_smacks session resumed from %s...", get_session_id(session)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
586 -- TODO: All this should move to sessionmanager (e.g. session:replace(new_session)) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 if original_session.conn then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
588 original_session.log("debug", "mod_smacks closing an old connection for this session"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
589 local conn = original_session.conn; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
590 c2s_sessions[conn] = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
591 conn:close(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
592 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
593 local migrated_session_log = session.log; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
594 original_session.ip = session.ip; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
595 original_session.conn = session.conn; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
596 original_session.send = session.send; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
597 original_session.close = session.close; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
598 original_session.filter = session.filter; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
599 original_session.filter.session = original_session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
600 original_session.filters = session.filters; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
601 original_session.stream = session.stream; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
602 original_session.secure = session.secure; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
603 original_session.hibernating = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
604 session.log = original_session.log; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
605 session.type = original_session.type; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
606 wrap_session(original_session, true); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
607 -- Inform xmppstream of the new session (passed to its callbacks) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
608 original_session.stream:set_session(original_session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
609 -- Similar for connlisteners |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
610 c2s_sessions[session.conn] = original_session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
611 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
612 original_session.send(st.stanza("resumed", { xmlns = xmlns_sm, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
613 h = string.format("%d", original_session.handled_stanza_count), previd = id })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
614 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
615 -- Fake an <a> with the h of the <resume/> from the client |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
616 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
617 h = stanza.attr.h })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
618 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 -- Ok, we need to re-send any stanzas that the client didn't see |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
620 -- ...they are what is now left in the outgoing stanza queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
621 -- We have to use the send of "session" because we don't want to add our resent stanzas |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
622 -- to the outgoing queue again |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
623 local queue = original_session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
624 session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
625 for i=1,#queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
626 session.send(queue[i]); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
627 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
628 session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
629 function session.send(stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
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)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
631 return false; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
632 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
633 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
634 request_ack_if_needed(original_session, true, "handle_resume", nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
635 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
636 module:log("warn", "Client %s@%s[%s] tried to resume stream for %s@%s[%s]", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
637 session.username or "?", session.host or "?", session.type, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
638 original_session.username or "?", original_session.host or "?", original_session.type); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
639 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
640 :tag("not-authorized", { xmlns = xmlns_errors })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
641 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
642 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
643 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
644 module:hook_stanza(xmlns_sm2, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm2); end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
645 module:hook_stanza(xmlns_sm3, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm3); end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
646 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
647 module:hook("csi-client-active", function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
648 if event.origin.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
649 request_ack_if_needed(event.origin, true, "csi-active", nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
650 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
651 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
652 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
653 module:hook("csi-flushing", function(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
654 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
655 if session.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
656 if not session.awaiting_ack and not session.hibernating and not session.destroyed then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
657 session.log("debug", "Sending <r> (csi-flushing)"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
658 session.awaiting_ack = true; -- The send() call may invoke this event again, so set this first |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
659 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
660 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
661 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
662 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
663 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
664 local function handle_read_timeout(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
665 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
666 if session.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
667 if session.awaiting_ack then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
668 if session.awaiting_ack_timer then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
669 session.awaiting_ack_timer:stop(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
670 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
671 if session.delayed_ack_timer then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
672 session.delayed_ack_timer:stop(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
673 session.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
674 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
675 return false; -- Kick the session |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
676 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
677 session.log("debug", "Sending <r> (read timeout)"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
678 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
679 session.awaiting_ack = true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
680 if not session.delayed_ack_timer then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
681 session.delayed_ack_timer = stoppable_timer(delayed_ack_timeout, function() |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
682 delayed_ack_function(session, nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
683 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
684 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
685 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
686 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
687 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
688 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
689 module:hook("s2s-read-timeout", handle_read_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
690 module:hook("c2s-read-timeout", handle_read_timeout); |