Software /
code /
verse
Annotate
plugins/smacks.lua @ 449:c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 19 Feb 2022 15:52:10 +0100 |
parent | 448:6d97b42bb1b4 |
child | 450:e72deac76e0e |
rev | line source |
---|---|
250 | 1 local verse = require "verse"; |
394
c2e959b60c13
plugins.smacks: explicitly import luasocket which stops setting globals in new versions
Kim Alvefur <zash@zash.se>
parents:
380
diff
changeset
|
2 local now = require"socket".gettime; |
250 | 3 |
408
635cbd979d7b
plugins.smacks: Update to sm:3 namespace
Kim Alvefur <zash@zash.se>
parents:
395
diff
changeset
|
4 local xmlns_sm = "urn:xmpp:sm:3"; |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 function verse.plugins.smacks(stream) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- State for outgoing stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local outgoing_queue = {}; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local last_ack = 0; |
350
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
10 local last_stanza_time = now(); |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
11 local timer_active; |
380 | 12 |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 -- State for incoming stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local handled_stanza_count = 0; |
380 | 15 |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 -- Catch incoming stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local function incoming_stanza(stanza) |
449
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
18 if stream.smacks and (stanza.attr.xmlns == "jabber:client" or not stanza.attr.xmlns) then |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 handled_stanza_count = handled_stanza_count + 1; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 stream:debug("Increasing handled stanzas to %d for %s", handled_stanza_count, stanza:top_tag()); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
321
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
23 |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
24 -- Catch outgoing stanzas |
395 | 25 local function outgoing_stanza(stanza) |
321
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
26 -- NOTE: This will not behave nice if stanzas are serialized before this point |
449
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
27 if stream.smacks and (stanza.name and not stanza.attr.xmlns) then |
321
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
28 -- serialize stanzas in order to bypass this on resumption |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
29 outgoing_queue[#outgoing_queue+1] = tostring(stanza); |
350
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
30 last_stanza_time = now(); |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
31 if not timer_active then |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
32 timer_active = true; |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
33 stream:debug("Waiting to send ack request..."); |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
34 verse.add_task(1, function() |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
35 if #outgoing_queue == 0 then |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
36 timer_active = false; |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
37 return; |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
38 end |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
39 local time_since_last_stanza = now() - last_stanza_time; |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
40 if time_since_last_stanza < 1 and #outgoing_queue < 10 then |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
41 return 1 - time_since_last_stanza; |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
42 end |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
43 stream:debug("Time up, sending <r>..."); |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
44 timer_active = false; |
321
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
45 stream:send(verse.stanza("r", { xmlns = xmlns_sm })); |
350
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
46 end); |
04049524fcd1
plugins.smacks: Improve logic for sending <r/>
Matthew Wild <mwild1@gmail.com>
parents:
326
diff
changeset
|
47 end |
321
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
48 end |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
49 end |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
50 |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local function on_disconnect() |
203
0f34520f4e26
plugins.smacks: Scatter some logging and comments through the code for good measure
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
52 stream:debug("smacks: connection lost"); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 stream.stream_management_supported = nil; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if stream.resumption_token then |
203
0f34520f4e26
plugins.smacks: Scatter some logging and comments through the code for good measure
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
55 stream:debug("smacks: have resumption token, reconnecting in 1s..."); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 stream.authenticated = nil; |
200
4166213cc9bd
plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents:
197
diff
changeset
|
57 verse.add_task(1, function () |
4166213cc9bd
plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents:
197
diff
changeset
|
58 stream:connect(stream.connect_host or stream.host, stream.connect_port or 5222); |
4166213cc9bd
plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents:
197
diff
changeset
|
59 end); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 return true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
380 | 62 end |
324
dbb3362c1ff3
plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
63 |
dbb3362c1ff3
plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
64 -- Graceful shutdown |
dbb3362c1ff3
plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
65 local function on_close() |
dbb3362c1ff3
plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
66 stream.resumption_token = nil; |
dbb3362c1ff3
plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
67 end |
380 | 68 |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 local function handle_sm_command(stanza) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 if stanza.name == "r" then -- Request for acks for stanzas we received |
203
0f34520f4e26
plugins.smacks: Scatter some logging and comments through the code for good measure
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
71 stream:debug("Ack requested... acking %d handled stanzas", handled_stanza_count); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 stream:send(verse.stanza("a", { xmlns = xmlns_sm, h = tostring(handled_stanza_count) })); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 elseif stanza.name == "a" then -- Ack for stanzas we sent |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local new_ack = tonumber(stanza.attr.h); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 if new_ack > last_ack then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 local old_unacked = #outgoing_queue; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 for i=last_ack+1,new_ack do |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 table.remove(outgoing_queue, 1); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 stream:debug("Received ack: New ack: "..new_ack.." Last ack: "..last_ack.." Unacked stanzas now: "..#outgoing_queue.." (was "..old_unacked..")"); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 last_ack = new_ack; |
445
b119dc4d8bc2
plugins.smacks: Don't warn about zero stanzas acked
Kim Alvefur <zash@zash.se>
parents:
408
diff
changeset
|
82 elseif new_ack < last_ack then |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 stream:warn("Received bad ack for "..new_ack.." when last ack was "..last_ack); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 elseif stanza.name == "enabled" then |
448
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
86 stream.pre_smacks_features = nil; |
324
dbb3362c1ff3
plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
87 |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 if stanza.attr.id then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 stream.resumption_token = stanza.attr.id; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 elseif stanza.name == "resumed" then |
448
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
92 stream.pre_smacks_features = nil; |
321
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
93 local new_ack = tonumber(stanza.attr.h); |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
94 if new_ack > last_ack then |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
95 local old_unacked = #outgoing_queue; |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
96 for i=last_ack+1,new_ack do |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
97 table.remove(outgoing_queue, 1); |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
98 end |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
99 stream:debug("Received ack: New ack: "..new_ack.." Last ack: "..last_ack.." Unacked stanzas now: "..#outgoing_queue.." (was "..old_unacked..")"); |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
100 last_ack = new_ack; |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
101 end |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
102 for i=1,#outgoing_queue do |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
103 stream:send(outgoing_queue[i]); |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
104 end |
369d4638d775
plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
105 outgoing_queue = {}; |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 stream:debug("Resumed successfully"); |
201
1fce24cb2c41
plugins.smacks: Remove some debugging code from resumption success handling, and fire a "resumed" event instead
Matthew Wild <mwild1@gmail.com>
parents:
200
diff
changeset
|
107 stream:event("resumed"); |
448
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
108 elseif stanza.name == "failed" then |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
109 stream.bound = nil |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
110 stream.smacks = nil |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
111 last_ack = 0 |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
112 handled_stanza_count = 0; |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
113 |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
114 -- TODO ack using final h value from <failed/> if present |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
115 outgoing_queue = {}; -- TODO fire some delivery failures |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
116 |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
117 local features = stream.pre_smacks_features; |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
118 stream.pre_smacks_features = nil; |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
119 |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
120 -- should trigger a bind and then a new smacks session |
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
121 stream:event("stream-features", features); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 stream:warn("Don't know how to handle "..xmlns_sm.."/"..stanza.name); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 local function on_bind_success() |
449
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
128 if stream.stream_management_supported and not stream.smacks then |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 --stream:unhook("bind-success", on_bind_success); |
203
0f34520f4e26
plugins.smacks: Scatter some logging and comments through the code for good measure
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
130 stream:debug("smacks: sending enable"); |
197
7e98cf2c1d8d
plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents:
188
diff
changeset
|
131 stream:send(verse.stanza("enable", { xmlns = xmlns_sm, resume = "true" })); |
326
f657ed8f464e
plugins.smacks: Start counting when sending <enable/>
Kim Alvefur <zash@zash.se>
parents:
324
diff
changeset
|
132 stream.smacks = true; |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 local function on_features(features) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 if features:get_child("sm", xmlns_sm) then |
448
6d97b42bb1b4
plugins.smacks: Handle failed resumption by trying to bind instead
Kim Alvefur <zash@zash.se>
parents:
446
diff
changeset
|
138 stream.pre_smacks_features = features; |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 stream.stream_management_supported = true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 if stream.smacks and stream.bound then -- Already enabled in a previous session - resume |
203
0f34520f4e26
plugins.smacks: Scatter some logging and comments through the code for good measure
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
141 stream:debug("Resuming stream with %d handled stanzas", handled_stanza_count); |
197
7e98cf2c1d8d
plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents:
188
diff
changeset
|
142 stream:send(verse.stanza("resume", { xmlns = xmlns_sm, |
446
62fb547bd31a
plugins.smacks: Convert counter to string in resumption
Kim Alvefur <zash@zash.se>
parents:
445
diff
changeset
|
143 h = tostring(handled_stanza_count), previd = stream.resumption_token })); |
202
05d1a4751251
plugins.smacks: Fix event priority and handling to make the code actually... work
Matthew Wild <mwild1@gmail.com>
parents:
201
diff
changeset
|
144 return true; |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 |
202
05d1a4751251
plugins.smacks: Fix event priority and handling to make the code actually... work
Matthew Wild <mwild1@gmail.com>
parents:
201
diff
changeset
|
150 stream:hook("stream-features", on_features, 250); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 stream:hook("stream/"..xmlns_sm, handle_sm_command); |
449
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
152 stream:hook("bind-success", on_bind_success, 1); |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
153 |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
154 -- Catch stanzas |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
155 stream:hook("stanza", incoming_stanza); |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
156 stream:hook("outgoing", outgoing_stanza); |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
157 |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
158 stream:hook("closed", on_close, 100); |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
159 stream:hook("disconnected", on_disconnect, 100); |
c720f331327c
plugins.smacks: Restructure events to avoid double hooks after (failed) resumption
Kim Alvefur <zash@zash.se>
parents:
448
diff
changeset
|
160 |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 --stream:hook("ready", on_stream_ready, 500); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 end |