Mercurial > prosody-modules
annotate mod_smacks/mod_smacks.lua @ 1529:16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 20 Oct 2014 13:13:48 +0200 |
parents | 5ccb3ee2cf72 |
children | fb7cd669f41b |
rev | line source |
---|---|
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local st = require "util.stanza"; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
2 local uuid_generate = require "util.uuid".generate; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local t_insert, t_remove = table.insert, table.remove; |
220
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
5 local math_min = math.min; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
6 local os_time = os.time; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local tonumber, tostring = tonumber, tostring; |
201
bc24f58a0d39
mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents:
200
diff
changeset
|
8 local add_filter = require "util.filters".add_filter; |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
9 local timer = require "util.timer"; |
593
db2a40cbd6ef
Add a <delay> to stanzas that are queued (and don't have one already), so clients can show them with the original timestamp.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
592
diff
changeset
|
10 local datetime = require "util.datetime"; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
12 local xmlns_sm2 = "urn:xmpp:sm:2"; |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
13 local xmlns_sm3 = "urn:xmpp:sm:3"; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
14 local xmlns_errors = "urn:ietf:params:xml:ns:xmpp-stanzas"; |
593
db2a40cbd6ef
Add a <delay> to stanzas that are queued (and don't have one already), so clients can show them with the original timestamp.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
592
diff
changeset
|
15 local xmlns_delay = "urn:xmpp:delay"; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
17 local sm2_attr = { xmlns = xmlns_sm2 }; |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
18 local sm3_attr = { xmlns = xmlns_sm3 }; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
641
06a78cba6499
mod_smacks: get_option+_number
Matthew Wild <mwild1@gmail.com>
parents:
640
diff
changeset
|
20 local resume_timeout = module:get_option_number("smacks_hibernation_time", 300); |
576
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
21 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false); |
642
842a8a3b0d81
mod_smacks: Make smacks_max_unacked_stanzas configurable
Matthew Wild <mwild1@gmail.com>
parents:
641
diff
changeset
|
22 local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0); |
754
713c6791fbcc
mod_smacks: Import prosody.core_process_stanza()
Kim Alvefur <zash@zash.se>
parents:
642
diff
changeset
|
23 local core_process_stanza = prosody.core_process_stanza; |
757
92c6f84ec446
mod_smacks: Use require to import (thanks a lot, autocomplete)
Kim Alvefur <zash@zash.se>
parents:
756
diff
changeset
|
24 local sessionmanager = require"core.sessionmanager"; |
200
64a573203c20
mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents:
160
diff
changeset
|
25 |
640
d87131b29bbd
mod_smacks: Remove dependency on connlisteners (use sessions table shared by mod_c2s directly)
Matthew Wild <mwild1@gmail.com>
parents:
625
diff
changeset
|
26 local c2s_sessions = module:shared("/*/c2s/sessions"); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
27 local session_registry = {}; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
28 |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
29 local function can_do_smacks(session, advertise_only) |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
30 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
31 |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
32 local session_type = session.type; |
595
7693724881b3
Fix a typo in mod_smacks (type -> session_type).
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
594
diff
changeset
|
33 if session_type == "c2s" then |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
34 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm |
592
f9c73c1249cd
Update smacks to urn:xmpp:sm:3. Fix typo in can_do_smacks.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
591
diff
changeset
|
35 return false, "unexpected-request", "Client must bind a resource before enabling stream management"; |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
36 end |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
37 return true; |
595
7693724881b3
Fix a typo in mod_smacks (type -> session_type).
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
594
diff
changeset
|
38 elseif s2s_smacks and (session_type == "s2sin" or session_type == "s2sout") then |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
39 return true; |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
40 end |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
41 return false, "service-unavailable", "Stream management is not available for this stream"; |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
42 end |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
43 |
263
41f1cac40560
mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents:
258
diff
changeset
|
44 module:hook("stream-features", |
41f1cac40560
mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents:
258
diff
changeset
|
45 function (event) |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
46 if can_do_smacks(event.origin, true) then |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
47 event.features:tag("sm", sm2_attr):tag("optional"):up():up(); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
48 event.features:tag("sm", sm3_attr):tag("optional"):up():up(); |
589
57ac609444c4
mod_smacks: Only advertise stream features when a stream is authenticated, and doesn't already have smacks enabled
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
49 end |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end); |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 module:hook("s2s-stream-features", |
263
41f1cac40560
mod_smacks: Fixed to use the correct events API.
Waqas Hussain <waqas20@gmail.com>
parents:
258
diff
changeset
|
53 function (event) |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
54 if can_do_smacks(event.origin, true) then |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
55 event.features:tag("sm", sm2_attr):tag("optional"):up():up(); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
56 event.features:tag("sm", sm3_attr):tag("optional"):up():up(); |
576
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
57 end |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end); |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
60 module:hook_stanza("http://etherx.jabber.org/streams", "features", |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 function (session, stanza) |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
62 if can_do_smacks(session) then |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
63 if stanza:get_child("sm", xmlns_sm3) then |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
64 session.sends2s(st.stanza("enable", sm3_attr)); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
65 elseif stanza:get_child("sm", xmlns_sm2) then |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
66 session.sends2s(st.stanza("enable", sm2_attr)); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
67 end |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
69 end); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
70 |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
71 local function outgoing_stanza_filter(stanza, session) |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
72 local is_stanza = stanza.attr and not stanza.attr.xmlns; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
73 if is_stanza and not stanza._cached then -- Stanza in default stream namespace |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
74 local queue = session.outgoing_stanza_queue; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
75 local cached_stanza = st.clone(stanza); |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
76 cached_stanza._cached = true; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
77 |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
78 if cached_stanza and cached_stanza:get_child("delay", xmlns_delay) == nil then |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
79 cached_stanza = cached_stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()}); |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
80 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
81 |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
82 queue[#queue+1] = cached_stanza; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
83 session.log("debug", "#queue = %d", #queue); |
1522
d4a4ed31567e
mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents:
1520
diff
changeset
|
84 if session.hibernating then |
d4a4ed31567e
mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents:
1520
diff
changeset
|
85 session.log("debug", "hibernating, stanza queued"); |
d4a4ed31567e
mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents:
1520
diff
changeset
|
86 return ""; -- Hack to make session.send() not return nil |
d4a4ed31567e
mod_smacks: Trick session.send() into not returning nil or mod_message will act as if messages were not delivered
Kim Alvefur <zash@zash.se>
parents:
1520
diff
changeset
|
87 end |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
88 if #queue > max_unacked_stanzas then |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
89 module:add_timer(0, function () |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
90 if not session.awaiting_ack then |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
91 session.awaiting_ack = true; |
1526
120817435151
mod_smacks: Fix sending ack requests on s2s
Kim Alvefur <zash@zash.se>
parents:
1522
diff
changeset
|
92 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })); |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
93 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
94 end); |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
95 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
96 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
97 return stanza; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
98 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
99 |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
100 local function count_incoming_stanzas(stanza, session) |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
101 if not stanza.attr.xmlns then |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
102 session.handled_stanza_count = session.handled_stanza_count + 1; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
103 session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count); |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
104 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
105 return stanza; |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
106 end |
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
107 |
1529
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
108 local function wrap_session_out(session, resume) |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
109 if not resume then |
1528
5ccb3ee2cf72
mod_smacks: Remove variable not used after 2881d532f385
Kim Alvefur <zash@zash.se>
parents:
1527
diff
changeset
|
110 session.outgoing_stanza_queue = {}; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
111 session.last_acknowledged_stanza = 0; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
112 end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
113 |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
114 add_filter(session, "stanzas/out", outgoing_stanza_filter, -1000); |
988
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
115 |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
116 local session_close = session.close; |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
117 function session.close(...) |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
118 if session.resumption_token then |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
119 session_registry[session.resumption_token] = nil; |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
120 session.resumption_token = nil; |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
121 end |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
122 return session_close(...); |
c15cea87036f
mod_smacks: Wrap session:close() and make the session not resumable
Kim Alvefur <zash@zash.se>
parents:
987
diff
changeset
|
123 end |
1529
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
124 return session; |
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
125 end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
126 |
1529
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
127 local function wrap_session_in(session, resume) |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
128 if not resume then |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
129 session.handled_stanza_count = 0; |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
130 add_filter(session, "stanzas/in", count_incoming_stanzas, 1000); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
131 end |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
132 |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
133 return session; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
134 end |
201
bc24f58a0d39
mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents:
200
diff
changeset
|
135 |
1529
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
136 local function wrap_session(session, resume) |
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
137 wrap_session_out(session, resume); |
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
138 wrap_session_in(session, resume); |
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
139 return session; |
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
140 end |
16893646a458
mod_smacks: Move wrap_session() contents into separate direction-specific routines
Kim Alvefur <zash@zash.se>
parents:
1528
diff
changeset
|
141 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
142 function handle_enable(session, stanza, xmlns_sm) |
591
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
143 local ok, err, err_text = can_do_smacks(session); |
36003cae2370
mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
Matthew Wild <mwild1@gmail.com>
parents:
590
diff
changeset
|
144 if not ok then |
590
40b707d7a809
mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents:
589
diff
changeset
|
145 session.log("warn", "Failed to enable smacks: %s", err_text); -- TODO: XEP doesn't say we can send error text, should it? |
1527
06ecc5b3ca46
mod_smacks: Send failure correctly on s2s
Kim Alvefur <zash@zash.se>
parents:
1526
diff
changeset
|
146 (session.sends2s or session.send)(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors})); |
590
40b707d7a809
mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents:
589
diff
changeset
|
147 return true; |
40b707d7a809
mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents:
589
diff
changeset
|
148 end |
40b707d7a809
mod_smacks: Handle a client trying to <enable> twice, or trying to <enable> before resource binding (thanks Thijs Alkemade)
Matthew Wild <mwild1@gmail.com>
parents:
589
diff
changeset
|
149 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
150 module:log("debug", "Enabling stream management"); |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
151 session.smacks = xmlns_sm; |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
152 |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
153 wrap_session(session, false); |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
154 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
155 local resume_token; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
156 local resume = stanza.attr.resume; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
157 if resume == "true" or resume == "1" then |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
158 resume_token = uuid_generate(); |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
159 session_registry[resume_token] = session; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
160 session.resumption_token = resume_token; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
161 end |
576
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
162 (session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume })); |
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
163 return true; |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
164 end |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
165 module:hook_stanza(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
166 module:hook_stanza(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); |
576
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
167 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
168 function handle_enabled(session, stanza, xmlns_sm) |
576
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
169 module:log("debug", "Enabling stream management"); |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
170 session.smacks = xmlns_sm; |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
171 |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
172 wrap_session(session, false); |
576
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
173 |
44b69c3d5351
mod_smacks: Fix smacks on s2s connections, but disable it by default.
Kim Alvefur <zash@zash.se>
parents:
478
diff
changeset
|
174 -- FIXME Resume? |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
175 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
176 return true; |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
177 end |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
178 module:hook_stanza(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
179 module:hook_stanza(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
181 function handle_r(origin, stanza, xmlns_sm) |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 if not origin.smacks then |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 module:log("debug", "Received ack request from non-smack-enabled session"); |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 return; |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 end |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 -- Reply with <a> |
255
9b9b089407b1
mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents:
220
diff
changeset
|
188 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = tostring(origin.handled_stanza_count) })); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 return true; |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
190 end |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
191 module:hook_stanza(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
192 module:hook_stanza(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
194 function handle_a(origin, stanza) |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 if not origin.smacks then return; end |
200
64a573203c20
mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents:
160
diff
changeset
|
196 origin.awaiting_ack = nil; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 -- Remove handled stanzas from outgoing_stanza_queue |
258
36648205b10a
mod_smacks: Add commented log statement for future debugging
Matthew Wild <mwild1@gmail.com>
parents:
257
diff
changeset
|
198 --log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or ""); |
1407
b631c8a8b9e7
mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents:
1406
diff
changeset
|
199 local h = tonumber(stanza.attr.h); |
b631c8a8b9e7
mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents:
1406
diff
changeset
|
200 if not h then |
b631c8a8b9e7
mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents:
1406
diff
changeset
|
201 origin:close{ condition = "invalid-xml"; text = "Missing or invalid 'h' attribute"; }; |
b631c8a8b9e7
mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents:
1406
diff
changeset
|
202 end |
b631c8a8b9e7
mod_smacks: Assert that the 'h' attribute is present and a number
Kim Alvefur <zash@zash.se>
parents:
1406
diff
changeset
|
203 local handled_stanza_count = h-origin.last_acknowledged_stanza; |
220
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
204 local queue = origin.outgoing_stanza_queue; |
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
205 if handled_stanza_count > #queue then |
1416
7ddb522d9b28
mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1408
diff
changeset
|
206 origin.log("warn", "The client says it handled %d new stanzas, but we only sent %d :)", |
220
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
207 handled_stanza_count, #queue); |
1416
7ddb522d9b28
mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1408
diff
changeset
|
208 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza); |
220
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
209 for i=1,#queue do |
1416
7ddb522d9b28
mod_smacks: Fix logging (Thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
1408
diff
changeset
|
210 origin.log("debug", "Q item %d: %s", i, tostring(queue[i])); |
220
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
211 end |
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
212 end |
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
213 for i=1,math_min(handled_stanza_count,#queue) do |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 t_remove(origin.outgoing_stanza_queue, 1); |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 end |
1406
7d76dd2310ef
mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents:
1405
diff
changeset
|
216 origin.log("debug", "#queue = %d", #queue); |
220
263858d40ceb
mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
217 origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 return true; |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
219 end |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
220 module:hook_stanza(xmlns_sm2, "a", handle_a); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
221 module:hook_stanza(xmlns_sm3, "a", handle_a); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 --TODO: Optimise... incoming stanzas should be handled by a per-session |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 -- function that has a counter as an upvalue (no table indexing for increments, |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 -- and won't slow non-198 sessions). We can also then remove the .handled flag |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 -- on stanzas |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 function handle_unacked_stanzas(session) |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 local queue = session.outgoing_stanza_queue; |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 local error_attr = { type = "cancel" }; |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 if #queue > 0 then |
202
d11478ae374e
mod_smacks: Clean outgoing stanza queue correctly on session close
Matthew Wild <mwild1@gmail.com>
parents:
201
diff
changeset
|
232 session.outgoing_stanza_queue = {}; |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 for i=1,#queue do |
256
57de4a7840ef
mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents:
255
diff
changeset
|
234 local reply = st.reply(queue[i]); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 if reply.attr.to ~= session.full_jid then |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 reply.attr.type = "error"; |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 reply:tag("error", error_attr) |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 :tag("recipient-unavailable", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}); |
256
57de4a7840ef
mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents:
255
diff
changeset
|
239 core_process_stanza(session, reply); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 end |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 end |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 end |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 end |
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 |
622
ce39df945de1
mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents:
595
diff
changeset
|
245 module:hook("pre-resource-unbind", function (event) |
ce39df945de1
mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents:
595
diff
changeset
|
246 local session, err = event.session, event.error; |
1024
d7655e634c30
mod_smacks: Allow resumption if the TCP connection is closed from our end, c15cea87036f ensures distinction from cleanly closed streams (thanks Lance)
Kim Alvefur <zash@zash.se>
parents:
994
diff
changeset
|
247 if session.smacks then |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
248 if not session.resumption_token then |
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
249 local queue = session.outgoing_stanza_queue; |
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
250 if #queue > 0 then |
1405
26a2092e289f
mod_smacks: Log things attached to sessions instead of the module
Kim Alvefur <zash@zash.se>
parents:
1343
diff
changeset
|
251 session.log("warn", "Destroying session with %d unacked stanzas", #queue); |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
252 handle_unacked_stanzas(session); |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 end |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
254 else |
586
f733e7599ed6
mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents:
576
diff
changeset
|
255 session.log("debug", "mod_smacks hibernating session for up to %d seconds", resume_timeout); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
256 local hibernate_time = os_time(); -- Track the time we went into hibernation |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
257 session.hibernating = hibernate_time; |
478
db0f065c4e09
mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents:
345
diff
changeset
|
258 local resumption_token = session.resumption_token; |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
259 timer.add_task(resume_timeout, function () |
586
f733e7599ed6
mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents:
576
diff
changeset
|
260 session.log("debug", "mod_smacks hibernation timeout reached..."); |
478
db0f065c4e09
mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents:
345
diff
changeset
|
261 -- We need to check the current resumption token for this resource |
db0f065c4e09
mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents:
345
diff
changeset
|
262 -- matches the smacks session this timer is for in case it changed |
db0f065c4e09
mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents:
345
diff
changeset
|
263 -- (for example, the client may have bound a new resource and |
db0f065c4e09
mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents:
345
diff
changeset
|
264 -- started a new smacks session, or not be using smacks) |
810
464ed063a8f2
mod_smacks: Less table indexing!
Kim Alvefur <zash@zash.se>
parents:
757
diff
changeset
|
265 local curr_session = full_sessions[session.full_jid]; |
987
fabff75bfc3f
mod_smacks: If a hibernating session was destroyed before the timeout, don't destroy it again or say that it was resumed
Kim Alvefur <zash@zash.se>
parents:
925
diff
changeset
|
266 if false and session.destroyed then |
fabff75bfc3f
mod_smacks: If a hibernating session was destroyed before the timeout, don't destroy it again or say that it was resumed
Kim Alvefur <zash@zash.se>
parents:
925
diff
changeset
|
267 session.log("debug", "The session has already been destroyed"); |
fabff75bfc3f
mod_smacks: If a hibernating session was destroyed before the timeout, don't destroy it again or say that it was resumed
Kim Alvefur <zash@zash.se>
parents:
925
diff
changeset
|
268 elseif curr_session and curr_session.resumption_token == resumption_token |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
269 -- Check the hibernate time still matches what we think it is, |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
270 -- otherwise the session resumed and re-hibernated. |
478
db0f065c4e09
mod_smacks: Don't destroy a session that binds the same resource as a hibernating smacks session (thanks xnyhps for tracking down the problem, though I've used a different fix)
Matthew Wild <mwild1@gmail.com>
parents:
345
diff
changeset
|
271 and session.hibernating == hibernate_time then |
586
f733e7599ed6
mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents:
576
diff
changeset
|
272 session.log("debug", "Destroying session for hibernating too long"); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
273 session_registry[session.resumption_token] = nil; |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
274 session.resumption_token = nil; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
275 sessionmanager.destroy_session(session); |
586
f733e7599ed6
mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents:
576
diff
changeset
|
276 else |
f733e7599ed6
mod_smacks: Add logging to hibernation and session destruction (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents:
576
diff
changeset
|
277 session.log("debug", "Session resumed before hibernation timeout, all is well") |
257
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
278 end |
08fa42e1ab06
mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents:
256
diff
changeset
|
279 end); |
622
ce39df945de1
mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents:
595
diff
changeset
|
280 return true; -- Postpone destruction for now |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 end |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
282 |
160
9a7671720dec
mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 end |
622
ce39df945de1
mod_smacks: Use pre-resource-unbind (0.9 feature) to fix reloadability and multiple host support
Matthew Wild <mwild1@gmail.com>
parents:
595
diff
changeset
|
284 end); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
285 |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
286 function handle_resume(session, stanza, xmlns_sm) |
925
720b8268778e
mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents:
908
diff
changeset
|
287 if session.full_jid then |
994
487cd02aada0
mod_smacks: Complain a little louder about clients trying to resume after resource binding
Kim Alvefur <zash@zash.se>
parents:
988
diff
changeset
|
288 session.log("warn", "Tried to resume after resource binding"); |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
289 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
925
720b8268778e
mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents:
908
diff
changeset
|
290 :tag("unexpected-request", { xmlns = xmlns_errors }) |
720b8268778e
mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents:
908
diff
changeset
|
291 ); |
720b8268778e
mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents:
908
diff
changeset
|
292 return true; |
720b8268778e
mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents:
908
diff
changeset
|
293 end |
720b8268778e
mod_smacks: Don't allow steam resumption onto a stream after resource binding
Kim Alvefur <zash@zash.se>
parents:
908
diff
changeset
|
294 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
295 local id = stanza.attr.previd; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
296 local original_session = session_registry[id]; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
297 if not original_session then |
588
8042558336b6
mod_smacks: Log message when client tries to resume unknown session
Matthew Wild <mwild1@gmail.com>
parents:
587
diff
changeset
|
298 session.log("debug", "Tried to resume non-existent session with id %s", id); |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
299 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
300 :tag("item-not-found", { xmlns = xmlns_errors }) |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
301 ); |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
302 elseif session.username == original_session.username |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
303 and session.host == original_session.host then |
587
322a14acd974
mod_smacks: Add log message on resume
Matthew Wild <mwild1@gmail.com>
parents:
586
diff
changeset
|
304 session.log("debug", "mod_smacks resuming existing session..."); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
305 -- TODO: All this should move to sessionmanager (e.g. session:replace(new_session)) |
623
c1f3958695ea
mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents:
622
diff
changeset
|
306 if original_session.conn then |
c1f3958695ea
mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents:
622
diff
changeset
|
307 session.log("debug", "mod_smacks closing an old connection for this session"); |
c1f3958695ea
mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents:
622
diff
changeset
|
308 local conn = original_session.conn; |
640
d87131b29bbd
mod_smacks: Remove dependency on connlisteners (use sessions table shared by mod_c2s directly)
Matthew Wild <mwild1@gmail.com>
parents:
625
diff
changeset
|
309 c2s_sessions[conn] = nil; |
623
c1f3958695ea
mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents:
622
diff
changeset
|
310 conn:close(); |
c1f3958695ea
mod_smacks: If a resumed session still has a connection open, close that connection before resuming
Matthew Wild <mwild1@gmail.com>
parents:
622
diff
changeset
|
311 end |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
312 original_session.ip = session.ip; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
313 original_session.conn = session.conn; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
314 original_session.send = session.send; |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
315 original_session.send.filter = original_session.filter; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
316 original_session.stream = session.stream; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
317 original_session.secure = session.secure; |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
318 original_session.hibernating = nil; |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
319 wrap_session(original_session, true); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
320 -- Inform xmppstream of the new session (passed to its callbacks) |
1520
2881d532f385
mod_smacks: Use filters for queuing outgoing stanzas instead of wrapping session.send()
Kim Alvefur <zash@zash.se>
parents:
1518
diff
changeset
|
321 original_session.stream:set_session(original_session); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
322 -- Similar for connlisteners |
640
d87131b29bbd
mod_smacks: Remove dependency on connlisteners (use sessions table shared by mod_c2s directly)
Matthew Wild <mwild1@gmail.com>
parents:
625
diff
changeset
|
323 c2s_sessions[session.conn] = original_session; |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
324 |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
325 session.send(st.stanza("resumed", { xmlns = xmlns_sm, |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
326 h = original_session.handled_stanza_count, previd = id })); |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
327 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
328 -- Fake an <a> with the h of the <resume/> from the client |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
329 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm, |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
330 h = stanza.attr.h })); |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1325
diff
changeset
|
331 |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
332 -- Ok, we need to re-send any stanzas that the client didn't see |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
333 -- ...they are what is now left in the outgoing stanza queue |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
334 local queue = original_session.outgoing_stanza_queue; |
1406
7d76dd2310ef
mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents:
1405
diff
changeset
|
335 session.log("debug", "#queue = %d", #queue); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
336 for i=1,#queue do |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
337 session.send(queue[i]); |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
338 end |
1406
7d76dd2310ef
mod_smacks: Add more debug logging
Kim Alvefur <zash@zash.se>
parents:
1405
diff
changeset
|
339 session.log("debug", "#queue = %d -- after send", #queue); |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
340 else |
755 | 341 module:log("warn", "Client %s@%s[%s] tried to resume stream for %s@%s[%s]", |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
342 session.username or "?", session.host or "?", session.type, |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
343 original_session.username or "?", original_session.host or "?", original_session.type); |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
344 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
345
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
345 :tag("not-authorized", { xmlns = xmlns_errors })); |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
346 end |
445178d15b51
mod_smacks: Merge mod_fastreconnect (resumption support), fix a number of bugs, refactor the code and add some more comments to explain process
Matthew Wild <mwild1@gmail.com>
parents:
263
diff
changeset
|
347 return true; |
1298
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
348 end |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
349 module:hook_stanza(xmlns_sm2, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm2); end); |
659da45a2b4b
mod_smacks: Handle both version 2 and version 3 namespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1293
diff
changeset
|
350 module:hook_stanza(xmlns_sm3, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm3); end); |