Software /
code /
verse
Annotate
plugins/smacks.lua @ 188:4678932455a3
plugins.smacks: XEP-0198 support
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 22 Feb 2011 23:51:00 +0000 |
child | 197:7e98cf2c1d8d |
rev | line source |
---|---|
188
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local st = require "util.stanza"; |
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 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 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local function on_disconnect() |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 stream.stream_management_supported = nil; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 if stream.resumption_token then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 stream.authenticated = nil; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 stream:connect(stream.connect_host or stream.host, stream.connect_port or 5222); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 stream:reopen(); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 return true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
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 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local function handle_sm_command(stanza) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if stanza.name == "r" then -- Request for acks for stanzas we received |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 elseif stanza.name == "a" then -- Ack for stanzas we sent |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local new_ack = tonumber(stanza.attr.h); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if new_ack > last_ack then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local old_unacked = #outgoing_queue; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 for i=last_ack+1,new_ack do |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 table.remove(outgoing_queue, 1); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 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
|
42 last_ack = new_ack; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 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
|
45 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 elseif stanza.name == "enabled" then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 stream.smacks = true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 -- Catch outgoing stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local old_send = stream.send; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 function stream.send(stream, stanza) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 stream:warn("SENDING"); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if not stanza.attr.xmlns then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 outgoing_queue[#outgoing_queue+1] = stanza; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local ret = old_send(stream, stanza); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 old_send(stream, verse.stanza("r", { xmlns = xmlns_sm })); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 return ret; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 return old_send(stream, stanza); |
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 -- Catch incoming stanzas |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 stream:hook("stanza", incoming_stanza); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 if stanza.attr.id then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 stream.resumption_token = stanza.attr.id; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 stream:hook("disconnected", on_disconnect, 100); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 elseif stanza.name == "resumed" then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 stream:debug("Resumed successfully"); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 stream:send(verse.message{to="me@matthewwild.co.uk", type="chat"}:tag("body"):text("Hi again!")); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 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
|
72 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local function on_bind_success() |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 if not stream.smacks then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 --stream:unhook("bind-success", on_bind_success); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 stream:send(st.stanza("enable", { xmlns = xmlns_sm, resume = "true" })); |
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 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local function on_features(features) |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 if features:get_child("sm", xmlns_sm) then |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 stream.stream_management_supported = true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 if stream.smacks and stream.bound then -- Already enabled in a previous session - resume |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 stream:send(st.stanza("resume", { xmlns = xmlns_sm, |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 h = handled_stanza_count, previd = stream.resumption_token })); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 else |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 stream:hook("bind-success", on_bind_success); |
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 return true; |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 end |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 stream:hook("stream-features", on_features, 150); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 stream:hook("stream/"..xmlns_sm, handle_sm_command); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 --stream:hook("ready", on_stream_ready, 500); |
4678932455a3
plugins.smacks: XEP-0198 support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end |