util.sasl_cyrus: Protect the call to cyrussasl.server_new properly.
[prosody.git] / plugins / mod_compression.lua
index 8fdf9dcce592983219bf53fa3f7669dd0bff45ec..c2e84f2b022b73892cf062997caac8ae488678d1 100644 (file)
@@ -8,6 +8,8 @@
 local st = require "util.stanza";
 local zlib = require "zlib";
 local pcall = pcall;
+local tostring = tostring;
+
 local xmlns_compression_feature = "http://jabber.org/features/compress"
 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
 local xmlns_stream = "http://etherx.jabber.org/streams";
@@ -25,24 +27,21 @@ if not compression_level or compression_level < 1 or compression_level > 9 then
        return;
 end
 
-module:add_event_hook("stream-features",
-               function (session, features)
-                       if not session.compressed then
-                               -- FIXME only advertise compression support when TLS layer has no compression enabled
-                               features:add_child(compression_stream_feature);
-                       end
-               end
-);
+module:hook("stream-features", function(event)
+       local origin, features = event.origin, event.features;
+       if not origin.compressed then
+               -- FIXME only advertise compression support when TLS layer has no compression enabled
+               features:add_child(compression_stream_feature);
+       end
+end);
 
-module:hook("s2s-stream-features",
-               function (data)
-                       local session, features = data.session, data.features;
-                       -- FIXME only advertise compression support when TLS layer has no compression enabled
-                       if not session.compressed then 
-                               features:add_child(compression_stream_feature);
-                       end
-               end
-);
+module:hook("s2s-stream-features", function(event)
+       local origin, features = event.origin, event.features;
+       -- FIXME only advertise compression support when TLS layer has no compression enabled
+       if not origin.compressed then 
+               features:add_child(compression_stream_feature);
+       end
+end);
 
 -- Hook to activate compression if remote server supports it.
 module:hook_stanza(xmlns_stream, "features",
@@ -74,7 +73,7 @@ local function get_deflate_stream(session)
                local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
                (session.sends2s or session.send)(error_st);
                session.log("error", "Failed to create zlib.deflate filter.");
-               module:log("error", deflate_stream);
+               module:log("error", "%s", tostring(deflate_stream));
                return
        end
        return deflate_stream
@@ -86,8 +85,8 @@ local function get_inflate_stream(session)
        if status == false then
                local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
                (session.sends2s or session.send)(error_st);
-               session.log("error", "Failed to create zlib.deflate filter.");
-               module:log("error", inflate_stream);
+               session.log("error", "Failed to create zlib.inflate filter.");
+               module:log("error", "%s", tostring(inflate_stream));
                return
        end
        return inflate_stream
@@ -107,7 +106,7 @@ local function setup_compression(session, deflate_stream)
                                        text = compressed;
                                        extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
                                });
-                               module:log("warn", compressed);
+                               module:log("warn", "%s", tostring(compressed));
                                return;
                        end
                        session.conn:write(compressed);
@@ -128,7 +127,7 @@ local function setup_decompression(session, inflate_stream)
                                        text = decompressed;
                                        extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
                                });
-                               module:log("warn", decompressed);
+                               module:log("warn", "%s", tostring(decompressed));
                                return;
                        end
                        old_data(conn, decompressed);
@@ -169,15 +168,17 @@ module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress
                function(session, stanza)
                        -- fail if we are already compressed
                        if session.compressed then
-                               local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
+                               local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
                                (session.sends2s or session.send)(error_st);
-                               session.log("warn", "Tried to establish another compression layer.");
+                               session.log("debug", "Client tried to establish another compression layer.");
+                               return;
                        end
                        
                        -- checking if the compression method is supported
-                       local method = stanza:child_with_name("method")[1];
+                       local method = stanza:child_with_name("method");
+                       method = method and (method[1] or "");
                        if method == "zlib" then
-                               session.log("info", method.." compression selected.");
+                               session.log("debug", "zlib compression enabled.");
                                
                                -- create deflate and inflate streams
                                local deflate_stream = get_deflate_stream(session);
@@ -202,10 +203,12 @@ module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress
                                                return true;
                                        end;
                                session.compressed = true;
-                       else
-                               session.log("info", method.." compression selected. But we don't support it.");
+                       elseif method then
+                               session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
                                local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
                                (session.sends2s or session.send)(error_st);
+                       else
+                               (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));
                        end
                end
 );