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