Annotate

plugins/smacks.lua @ 326:f657ed8f464e

plugins.smacks: Start counting when sending <enable/>
author Kim Alvefur <zash@zash.se>
date Sun, 10 Feb 2013 03:41:45 +0100
parent 324:dbb3362c1ff3
child 350:04049524fcd1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 203
diff changeset
1 local verse = require "verse";
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 203
diff changeset
2
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local xmlns_sm = "urn:xmpp:sm:2";
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 function verse.plugins.smacks(stream)
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- State for outgoing stanzas
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local outgoing_queue = {};
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local last_ack = 0;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 -- State for incoming stanzas
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local handled_stanza_count = 0;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 -- Catch incoming stanzas
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local function incoming_stanza(stanza)
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 if stanza.attr.xmlns == "jabber:client" or not stanza.attr.xmlns then
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 handled_stanza_count = handled_stanza_count + 1;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 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
18 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
321
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
20
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
21 -- Catch outgoing stanzas
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
22 function outgoing_stanza(stanza)
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
23 -- NOTE: This will not behave nice if stanzas are serialized before this point
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
24 if stanza.name and not stanza.attr.xmlns then
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
25 -- 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
26 outgoing_queue[#outgoing_queue+1] = tostring(stanza);
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
27 verse.add_task(1, function()
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
28 if #outgoing_queue > 0 then
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
29 stream:send(verse.stanza("r", { xmlns = xmlns_sm }));
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
30 end
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
31 end);
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
32 end
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
33 end
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
34
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36 stream:debug("smacks: connection lost");
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 stream.stream_management_supported = nil;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39 stream:debug("smacks: have resumption token, reconnecting in 1s...");
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 stream.authenticated = nil;
200
4166213cc9bd plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents: 197
diff changeset
41 verse.add_task(1, function ()
4166213cc9bd plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents: 197
diff changeset
42 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
43 end);
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return true;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
324
dbb3362c1ff3 plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents: 321
diff changeset
47
dbb3362c1ff3 plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents: 321
diff changeset
48 -- Graceful shutdown
dbb3362c1ff3 plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents: 321
diff changeset
49 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
50 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
51 stream:unhook("disconnected", on_disconnect);
dbb3362c1ff3 plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents: 321
diff changeset
52 end
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local function handle_sm_command(stanza)
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 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
56 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
57 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
58 elseif stanza.name == "a" then -- Ack for stanzas we sent
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local new_ack = tonumber(stanza.attr.h);
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if new_ack > last_ack then
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local old_unacked = #outgoing_queue;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 for i=last_ack+1,new_ack do
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 table.remove(outgoing_queue, 1);
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 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
66 last_ack = new_ack;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 else
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 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
69 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 elseif stanza.name == "enabled" then
324
dbb3362c1ff3 plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents: 321
diff changeset
71
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 if stanza.attr.id then
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 stream.resumption_token = stanza.attr.id;
324
dbb3362c1ff3 plugins.smacks: Don't try to reconnect on gracefull stream closure
Kim Alvefur <zash@zash.se>
parents: 321
diff changeset
74 stream:hook("closed", on_close, 100);
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 stream:hook("disconnected", on_disconnect, 100);
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 elseif stanza.name == "resumed" then
321
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
78 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
79 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
80 local old_unacked = #outgoing_queue;
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
81 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
82 table.remove(outgoing_queue, 1);
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
83 end
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
84 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
85 last_ack = new_ack;
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
86 end
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
87 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
88 stream:send(outgoing_queue[i]);
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
89 end
369d4638d775 plugins.smacks: Re-send unacked outgoing stanzas on resumption
Kim Alvefur <zash@zash.se>
parents: 320
diff changeset
90 outgoing_queue = {};
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 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
92 stream:event("resumed");
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 else
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 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
95 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local function on_bind_success()
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if not stream.smacks then
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 --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
101 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
102 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
103 stream.smacks = true;
f657ed8f464e plugins.smacks: Start counting when sending <enable/>
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
104
f657ed8f464e plugins.smacks: Start counting when sending <enable/>
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
105 -- Catch stanzas
f657ed8f464e plugins.smacks: Start counting when sending <enable/>
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
106 stream:hook("stanza", incoming_stanza);
f657ed8f464e plugins.smacks: Start counting when sending <enable/>
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
107 stream:hook("outgoing", outgoing_stanza);
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 local function on_features(features)
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if features:get_child("sm", xmlns_sm) then
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 stream.stream_management_supported = true;
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 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
115 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
116 stream:send(verse.stanza("resume", { xmlns = xmlns_sm,
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 h = 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
118 return true;
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 else
202
05d1a4751251 plugins.smacks: Fix event priority and handling to make the code actually... work
Matthew Wild <mwild1@gmail.com>
parents: 201
diff changeset
120 stream:hook("bind-success", on_bind_success, 1);
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
202
05d1a4751251 plugins.smacks: Fix event priority and handling to make the code actually... work
Matthew Wild <mwild1@gmail.com>
parents: 201
diff changeset
125 stream:hook("stream-features", on_features, 250);
188
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 stream:hook("stream/"..xmlns_sm, handle_sm_command);
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 --stream:hook("ready", on_stream_ready, 500);
4678932455a3 plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end