plugins/smacks.lua

changeset 450
e72deac76e0e
parent 449
c720f331327c
equal deleted inserted replaced
449:c720f331327c 450:e72deac76e0e
3 3
4 local xmlns_sm = "urn:xmpp:sm:3"; 4 local xmlns_sm = "urn:xmpp:sm:3";
5 5
6 function verse.plugins.smacks(stream) 6 function verse.plugins.smacks(stream)
7 -- State for outgoing stanzas 7 -- State for outgoing stanzas
8 local outgoing_queue = {}; 8 local outgoing_queue = nil;
9 local last_ack = 0; 9 local last_ack = nil;
10 local last_stanza_time = now(); 10 local last_stanza_time = nil;
11 local timer_active; 11 local timer_active;
12 12
13 -- State for incoming stanzas 13 -- State for incoming stanzas
14 local handled_stanza_count = 0; 14 local handled_stanza_count = nil;
15 15
16 -- Catch incoming stanzas 16 -- Catch incoming stanzas
17 local function incoming_stanza(stanza) 17 local function incoming_stanza(stanza)
18 if stream.smacks and (stanza.attr.xmlns == "jabber:client" or not stanza.attr.xmlns) then 18 if handled_stanza_count and (stanza.attr.xmlns == "jabber:client" or not stanza.attr.xmlns) then
19 handled_stanza_count = handled_stanza_count + 1; 19 handled_stanza_count = handled_stanza_count + 1;
20 stream:debug("Increasing handled stanzas to %d for %s", handled_stanza_count, stanza:top_tag()); 20 stream:debug("Increasing handled stanzas to %d for %s", handled_stanza_count, stanza:top_tag());
21 end 21 end
22 end 22 end
23 23
24 -- Catch outgoing stanzas 24 -- Catch outgoing stanzas
25 local function outgoing_stanza(stanza) 25 local function outgoing_stanza(stanza)
26 -- NOTE: This will not behave nice if stanzas are serialized before this point 26 -- NOTE: This will not behave nice if stanzas are serialized before this point
27 if stream.smacks and (stanza.name and not stanza.attr.xmlns) then 27 if outgoing_queue and (stanza.name and not stanza.attr.xmlns) then
28 -- serialize stanzas in order to bypass this on resumption 28 -- serialize stanzas in order to bypass this on resumption
29 outgoing_queue[#outgoing_queue+1] = tostring(stanza); 29 outgoing_queue[#outgoing_queue+1] = tostring(stanza);
30 last_stanza_time = now(); 30 last_stanza_time = now();
31 if not timer_active then 31 if not timer_active then
32 timer_active = true; 32 timer_active = true;
81 last_ack = new_ack; 81 last_ack = new_ack;
82 elseif new_ack < last_ack then 82 elseif new_ack < last_ack then
83 stream:warn("Received bad ack for "..new_ack.." when last ack was "..last_ack); 83 stream:warn("Received bad ack for "..new_ack.." when last ack was "..last_ack);
84 end 84 end
85 elseif stanza.name == "enabled" then 85 elseif stanza.name == "enabled" then
86 handled_stanza_count = 0;
86 stream.pre_smacks_features = nil; 87 stream.pre_smacks_features = nil;
87 88
88 if stanza.attr.id then 89 if stanza.attr.id then
89 stream.resumption_token = stanza.attr.id; 90 stream.resumption_token = stanza.attr.id;
90 end 91 end
106 stream:debug("Resumed successfully"); 107 stream:debug("Resumed successfully");
107 stream:event("resumed"); 108 stream:event("resumed");
108 elseif stanza.name == "failed" then 109 elseif stanza.name == "failed" then
109 stream.bound = nil 110 stream.bound = nil
110 stream.smacks = nil 111 stream.smacks = nil
111 last_ack = 0 112 last_ack = nil
112 handled_stanza_count = 0; 113 handled_stanza_count = nil
113 114
114 -- TODO ack using final h value from <failed/> if present 115 -- TODO ack using final h value from <failed/> if present
115 outgoing_queue = {}; -- TODO fire some delivery failures 116 outgoing_queue = {}; -- TODO fire some delivery failures
116 117
117 local features = stream.pre_smacks_features; 118 local features = stream.pre_smacks_features;
126 127
127 local function on_bind_success() 128 local function on_bind_success()
128 if stream.stream_management_supported and not stream.smacks then 129 if stream.stream_management_supported and not stream.smacks then
129 --stream:unhook("bind-success", on_bind_success); 130 --stream:unhook("bind-success", on_bind_success);
130 stream:debug("smacks: sending enable"); 131 stream:debug("smacks: sending enable");
132 outgoing_queue = {};
133 last_ack = 0;
134 last_stanza_time = now();
131 stream:send(verse.stanza("enable", { xmlns = xmlns_sm, resume = "true" })); 135 stream:send(verse.stanza("enable", { xmlns = xmlns_sm, resume = "true" }));
132 stream.smacks = true; 136 stream.smacks = true;
133 end 137 end
134 end 138 end
135 139

mercurial