comparison mod_smacks/mod_smacks.lua @ 591:36003cae2370

mod_smacks: Consolidate logic for deciding whether to advertise or allow smacks for a given session, and fix an issue with not allowing s2s connections to enable smacks because of not binding a resource... (thanks xnyphs)
author Matthew Wild <mwild1@gmail.com>
date Wed, 01 Feb 2012 23:57:07 +0000
parents 40b707d7a809
children f9c73c1249cd
comparison
equal deleted inserted replaced
590:40b707d7a809 591:36003cae2370
17 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false); 17 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false);
18 local max_unacked_stanzas = 0; 18 local max_unacked_stanzas = 0;
19 19
20 local session_registry = {}; 20 local session_registry = {};
21 21
22 local function can_do_smacks(session, advertise_only)
23 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end
24
25 local session_type = session.type;
26 if type == "c2s" then
27 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm
28 return false, "unexpected-request", "Client must bind a resource before enabling stream management"; end
29 end
30 return true;
31 elseif s2s_smacks and (type == "s2sin" or type == "s2sout") then
32 return true;
33 end
34 return false, "service-unavailable", "Stream management is not available for this stream";
35 end
36
22 module:hook("stream-features", 37 module:hook("stream-features",
23 function (event) 38 function (event)
24 local origin = event.origin; 39 if can_do_smacks(event.origin, true) then
25 if not(origin.smacks) and origin.type == "c2s" then
26 event.features:tag("sm", sm_attr):tag("optional"):up():up(); 40 event.features:tag("sm", sm_attr):tag("optional"):up():up();
27 end 41 end
28 end); 42 end);
29 43
30 module:hook("s2s-stream-features", 44 module:hook("s2s-stream-features",
31 function (event) 45 function (event)
32 local origin = event.origin; 46 if can_do_smacks(event.origin, true) then
33 if s2s_smacks and not(origin.smacks) and (origin.type == "s2sin" or origin.type == "s2sout") then
34 event.features:tag("sm", sm_attr):tag("optional"):up():up(); 47 event.features:tag("sm", sm_attr):tag("optional"):up():up();
35 end 48 end
36 end); 49 end);
37 50
38 module:hook_stanza("http://etherx.jabber.org/streams", "features", 51 module:hook_stanza("http://etherx.jabber.org/streams", "features",
39 function (session, stanza) 52 function (session, stanza)
40 if s2s_smacks and (session.type == "s2sin" or session.type == "s2sout") 53 if can_do_smacks(session) and stanza:get_child("sm", xmlns_sm) then
41 and not session.smacks and stanza:get_child("sm", xmlns_sm) then
42 session.sends2s(st.stanza("enable", sm_attr)); 54 session.sends2s(st.stanza("enable", sm_attr));
43 end 55 end
44 end); 56 end);
45 57
46 local function wrap_session(session, resume) 58 local function wrap_session(session, resume)
87 99
88 return session; 100 return session;
89 end 101 end
90 102
91 module:hook_stanza(xmlns_sm, "enable", function (session, stanza) 103 module:hook_stanza(xmlns_sm, "enable", function (session, stanza)
92 local err, err_text; 104 local ok, err, err_text = can_do_smacks(session);
93 if session.smacks then 105 if not ok then
94 err, err_text = "unexpected-request", "Stream management already enabled for this stream";
95 elseif not session.resource then
96 err, err_text = "unexpected-request", "Attempted to enable stream management before resource binding";
97 end
98 if err then
99 session.log("warn", "Failed to enable smacks: %s", err_text); -- TODO: XEP doesn't say we can send error text, should it? 106 session.log("warn", "Failed to enable smacks: %s", err_text); -- TODO: XEP doesn't say we can send error text, should it?
100 session.send(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors})); 107 session.send(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors}));
101 return true; 108 return true;
102 end 109 end
103 110