Software /
code /
verse
Annotate
plugins/smacks.lua @ 223:30e66eabffa9
squishy: add vcard, vcard_update, jingle_ibb
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 02 Nov 2011 01:11:35 +0100 |
parent | 203:0f34520f4e26 |
child | 250:a5ac643a7fd6 |
rev | line source |
---|---|
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local xmlns_sm = "urn:xmpp:sm:2"; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 function verse.plugins.smacks(stream) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- State for outgoing stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local outgoing_queue = {}; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local last_ack = 0; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 -- State for incoming stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local handled_stanza_count = 0; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 -- Catch incoming stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local function incoming_stanza(stanza) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 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
|
14 handled_stanza_count = handled_stanza_count + 1; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 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
|
16 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 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
|
20 stream:debug("smacks: connection lost"); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 stream.stream_management_supported = nil; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 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
|
23 stream:debug("smacks: have resumption token, reconnecting in 1s..."); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 stream.authenticated = nil; |
200
4166213cc9bd
plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents:
197
diff
changeset
|
25 verse.add_task(1, function () |
4166213cc9bd
plugins.smacks: Add 1s delay between reconnect attempts
Matthew Wild <mwild1@gmail.com>
parents:
197
diff
changeset
|
26 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
|
27 end); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 local function handle_sm_command(stanza) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 elseif stanza.name == "a" then -- Ack for stanzas we sent |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local new_ack = tonumber(stanza.attr.h); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if new_ack > last_ack then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local old_unacked = #outgoing_queue; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 for i=last_ack+1,new_ack do |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 table.remove(outgoing_queue, 1); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 last_ack = new_ack; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 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
|
47 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 elseif stanza.name == "enabled" then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 stream.smacks = true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 -- Catch outgoing stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local old_send = stream.send; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 function stream.send(stream, stanza) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 stream:warn("SENDING"); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if not stanza.attr.xmlns then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 outgoing_queue[#outgoing_queue+1] = stanza; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local ret = old_send(stream, stanza); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 old_send(stream, verse.stanza("r", { xmlns = xmlns_sm })); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 return ret; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 return old_send(stream, stanza); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 -- Catch incoming stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 stream:hook("stanza", incoming_stanza); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 if stanza.attr.id then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 stream.resumption_token = stanza.attr.id; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 stream:hook("disconnected", on_disconnect, 100); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 elseif stanza.name == "resumed" 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
|
70 --TODO: Check h of the resumed element, discard any acked stanzas from |
0f34520f4e26
plugins.smacks: Scatter some logging and comments through the code for good measure
Matthew Wild <mwild1@gmail.com>
parents:
202
diff
changeset
|
71 -- our queue (to prevent duplicates), then re-send any lost stanzas. |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 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
|
73 stream:event("resumed"); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 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
|
76 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local function on_bind_success() |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 if not stream.smacks then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 --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
|
82 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
|
83 stream:send(verse.stanza("enable", { xmlns = xmlns_sm, resume = "true" })); |
188
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 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 local function on_features(features) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 if features:get_child("sm", xmlns_sm) then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 stream.stream_management_supported = true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 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
|
91 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
|
92 stream:send(verse.stanza("resume", { xmlns = xmlns_sm, |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 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
|
94 return true; |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 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
|
96 stream:hook("bind-success", on_bind_success, 1); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 |
202
05d1a4751251
plugins.smacks: Fix event priority and handling to make the code actually... work
Matthew Wild <mwild1@gmail.com>
parents:
201
diff
changeset
|
101 stream:hook("stream-features", on_features, 250); |
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 stream:hook("stream/"..xmlns_sm, handle_sm_command); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 --stream:hook("ready", on_stream_ready, 500); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end |