Comparison

mod_smacks/mod_smacks.lua @ 1343:7dbde05b48a9

all the things: Remove trailing whitespace
author Florian Zeitz <florob@babelmonkeys.de>
date Tue, 11 Mar 2014 18:44:01 +0100
parent 1325:b21236b6b8d8
child 1405:26a2092e289f
comparison
equal deleted inserted replaced
1342:0ae065453dc9 1343:7dbde05b48a9
26 local c2s_sessions = module:shared("/*/c2s/sessions"); 26 local c2s_sessions = module:shared("/*/c2s/sessions");
27 local session_registry = {}; 27 local session_registry = {};
28 28
29 local function can_do_smacks(session, advertise_only) 29 local function can_do_smacks(session, advertise_only)
30 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end 30 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end
31 31
32 local session_type = session.type; 32 local session_type = session.type;
33 if session_type == "c2s" then 33 if session_type == "c2s" then
34 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm 34 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm
35 return false, "unexpected-request", "Client must bind a resource before enabling stream management"; 35 return false, "unexpected-request", "Client must bind a resource before enabling stream management";
36 end 36 end
77 session.outgoing_stanza_queue = queue; 77 session.outgoing_stanza_queue = queue;
78 session.last_acknowledged_stanza = 0; 78 session.last_acknowledged_stanza = 0;
79 else 79 else
80 queue = session.outgoing_stanza_queue; 80 queue = session.outgoing_stanza_queue;
81 end 81 end
82 82
83 local _send = session.sends2s or session.send; 83 local _send = session.sends2s or session.send;
84 local function new_send(stanza) 84 local function new_send(stanza)
85 local attr = stanza.attr; 85 local attr = stanza.attr;
86 if attr and not attr.xmlns then -- Stanza in default stream namespace 86 if attr and not attr.xmlns then -- Stanza in default stream namespace
87 local cached_stanza = st.clone(stanza); 87 local cached_stanza = st.clone(stanza);
88 88
89 if cached_stanza and cached_stanza:get_child("delay", xmlns_delay) == nil then 89 if cached_stanza and cached_stanza:get_child("delay", xmlns_delay) == nil then
90 cached_stanza = cached_stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()}); 90 cached_stanza = cached_stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()});
91 end 91 end
92 92
93 queue[#queue+1] = cached_stanza; 93 queue[#queue+1] = cached_stanza;
94 end 94 end
95 if session.hibernating then 95 if session.hibernating then
96 -- The session is hibernating, no point in sending the stanza 96 -- The session is hibernating, no point in sending the stanza
97 -- over a dead connection. It will be delivered upon resumption. 97 -- over a dead connection. It will be delivered upon resumption.
102 session.awaiting_ack = true; 102 session.awaiting_ack = true;
103 return _send(st.stanza("r", sm_attr)); 103 return _send(st.stanza("r", sm_attr));
104 end 104 end
105 return ok, err; 105 return ok, err;
106 end 106 end
107 107
108 if session.sends2s then 108 if session.sends2s then
109 session.sends2s = new_send; 109 session.sends2s = new_send;
110 else 110 else
111 session.send = new_send; 111 session.send = new_send;
112 end 112 end
117 session_registry[session.resumption_token] = nil; 117 session_registry[session.resumption_token] = nil;
118 session.resumption_token = nil; 118 session.resumption_token = nil;
119 end 119 end
120 return session_close(...); 120 return session_close(...);
121 end 121 end
122 122
123 if not resume then 123 if not resume then
124 session.handled_stanza_count = 0; 124 session.handled_stanza_count = 0;
125 add_filter(session, "stanzas/in", function (stanza) 125 add_filter(session, "stanzas/in", function (stanza)
126 if not stanza.attr.xmlns then 126 if not stanza.attr.xmlns then
127 session.handled_stanza_count = session.handled_stanza_count + 1; 127 session.handled_stanza_count = session.handled_stanza_count + 1;
142 return true; 142 return true;
143 end 143 end
144 144
145 module:log("debug", "Enabling stream management"); 145 module:log("debug", "Enabling stream management");
146 session.smacks = true; 146 session.smacks = true;
147 147
148 wrap_session(session, false, xmlns_sm); 148 wrap_session(session, false, xmlns_sm);
149 149
150 local resume_token; 150 local resume_token;
151 local resume = stanza.attr.resume; 151 local resume = stanza.attr.resume;
152 if resume == "true" or resume == "1" then 152 if resume == "true" or resume == "1" then
153 resume_token = uuid_generate(); 153 resume_token = uuid_generate();
154 session_registry[resume_token] = session; 154 session_registry[resume_token] = session;
161 module:hook_stanza(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); 161 module:hook_stanza(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100);
162 162
163 function handle_enabled(session, stanza, xmlns_sm) 163 function handle_enabled(session, stanza, xmlns_sm)
164 module:log("debug", "Enabling stream management"); 164 module:log("debug", "Enabling stream management");
165 session.smacks = true; 165 session.smacks = true;
166 166
167 wrap_session(session, false, xmlns_sm); 167 wrap_session(session, false, xmlns_sm);
168 168
169 -- FIXME Resume? 169 -- FIXME Resume?
170 170
171 return true; 171 return true;
172 end 172 end
173 module:hook_stanza(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100); 173 module:hook_stanza(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100);
174 module:hook_stanza(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100); 174 module:hook_stanza(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100);
175 175
267 session.log("debug", "Session resumed before hibernation timeout, all is well") 267 session.log("debug", "Session resumed before hibernation timeout, all is well")
268 end 268 end
269 end); 269 end);
270 return true; -- Postpone destruction for now 270 return true; -- Postpone destruction for now
271 end 271 end
272 272
273 end 273 end
274 end); 274 end);
275 275
276 function handle_resume(session, stanza, xmlns_sm) 276 function handle_resume(session, stanza, xmlns_sm)
277 if session.full_jid then 277 if session.full_jid then
323 -- Similar for connlisteners 323 -- Similar for connlisteners
324 c2s_sessions[session.conn] = original_session; 324 c2s_sessions[session.conn] = original_session;
325 325
326 session.send(st.stanza("resumed", { xmlns = xmlns_sm, 326 session.send(st.stanza("resumed", { xmlns = xmlns_sm,
327 h = original_session.handled_stanza_count, previd = id })); 327 h = original_session.handled_stanza_count, previd = id }));
328 328
329 -- Fake an <a> with the h of the <resume/> from the client 329 -- Fake an <a> with the h of the <resume/> from the client
330 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm, 330 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm,
331 h = stanza.attr.h })); 331 h = stanza.attr.h }));
332 332
333 -- Ok, we need to re-send any stanzas that the client didn't see 333 -- Ok, we need to re-send any stanzas that the client didn't see
334 -- ...they are what is now left in the outgoing stanza queue 334 -- ...they are what is now left in the outgoing stanza queue
335 local queue = original_session.outgoing_stanza_queue; 335 local queue = original_session.outgoing_stanza_queue;
336 for i=1,#queue do 336 for i=1,#queue do
337 session.send(queue[i]); 337 session.send(queue[i]);