mod_auth_anonymous: Fixed a syntax error.
[prosody.git] / plugins / mod_compression.lua
1 -- Prosody IM
2 -- Copyright (C) 2009 Tobias Markmann
3 -- 
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
6 --
7
8 local st = require "util.stanza";
9 local zlib = require "zlib";
10 local pcall = pcall;
11 local tostring = tostring;
12
13 local xmlns_compression_feature = "http://jabber.org/features/compress"
14 local xmlns_compression_protocol = "http://jabber.org/protocol/compress"
15 local xmlns_stream = "http://etherx.jabber.org/streams";
16 local compression_stream_feature = st.stanza("compression", {xmlns=xmlns_compression_feature}):tag("method"):text("zlib"):up();
17 local add_filter = require "util.filters".add_filter;
18
19 local compression_level = module:get_option("compression_level");
20 -- if not defined assume admin wants best compression
21 if compression_level == nil then compression_level = 9 end;
22
23
24 compression_level = tonumber(compression_level);
25 if not compression_level or compression_level < 1 or compression_level > 9 then
26         module:log("warn", "Invalid compression level in config: %s", tostring(compression_level));
27         module:log("warn", "Module loading aborted. Compression won't be available.");
28         return;
29 end
30
31 module:hook("stream-features", function(event)
32         local origin, features = event.origin, event.features;
33         if not origin.compressed then
34                 -- FIXME only advertise compression support when TLS layer has no compression enabled
35                 features:add_child(compression_stream_feature);
36         end
37 end);
38
39 module:hook("s2s-stream-features", function(event)
40         local origin, features = event.origin, event.features;
41         -- FIXME only advertise compression support when TLS layer has no compression enabled
42         if not origin.compressed then 
43                 features:add_child(compression_stream_feature);
44         end
45 end);
46
47 -- Hook to activate compression if remote server supports it.
48 module:hook_stanza(xmlns_stream, "features",
49                 function (session, stanza)
50                         if not session.compressed then
51                                 -- does remote server support compression?
52                                 local comp_st = stanza:child_with_name("compression");
53                                 if comp_st then
54                                         -- do we support the mechanism
55                                         for a in comp_st:children() do
56                                                 local algorithm = a[1]
57                                                 if algorithm == "zlib" then
58                                                         session.sends2s(st.stanza("compress", {xmlns=xmlns_compression_protocol}):tag("method"):text("zlib"))
59                                                         session.log("info", "Enabled compression using zlib.")
60                                                         return true;
61                                                 end
62                                         end
63                                         session.log("debug", "Remote server supports no compression algorithm we support.")
64                                 end
65                         end
66                 end
67 , 250);
68
69
70 -- returns either nil or a fully functional ready to use inflate stream
71 local function get_deflate_stream(session)
72         local status, deflate_stream = pcall(zlib.deflate, compression_level);
73         if status == false then
74                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
75                 (session.sends2s or session.send)(error_st);
76                 session.log("error", "Failed to create zlib.deflate filter.");
77                 module:log("error", "%s", tostring(deflate_stream));
78                 return
79         end
80         return deflate_stream
81 end
82
83 -- returns either nil or a fully functional ready to use inflate stream
84 local function get_inflate_stream(session)
85         local status, inflate_stream = pcall(zlib.inflate);
86         if status == false then
87                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
88                 (session.sends2s or session.send)(error_st);
89                 session.log("error", "Failed to create zlib.inflate filter.");
90                 module:log("error", "%s", tostring(inflate_stream));
91                 return
92         end
93         return inflate_stream
94 end
95
96 -- setup compression for a stream
97 local function setup_compression(session, deflate_stream)
98         add_filter(session, "bytes/out", function(t)
99                 session.log(t)
100                 local status, compressed, eof = pcall(deflate_stream, tostring(t), 'sync');
101                 if status == false then
102                         session:close({
103                                 condition = "undefined-condition";
104                                 text = compressed;
105                                 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
106                         });
107                         module:log("warn", "%s", tostring(compressed));
108                         return;
109                 end
110                 return compressed;
111         end);   
112 end
113
114 -- setup decompression for a stream
115 local function setup_decompression(session, inflate_stream)
116         add_filter(session, "bytes/in", function(data)
117                 local status, decompressed, eof = pcall(inflate_stream, data);
118                 if status == false then
119                         session:close({
120                                 condition = "undefined-condition";
121                                 text = decompressed;
122                                 extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
123                         });
124                         module:log("warn", "%s", tostring(decompressed));
125                         return;
126                 end
127                 return decompressed;
128         end);
129 end
130
131 module:add_handler({"s2sout_unauthed", "s2sout"}, "compressed", xmlns_compression_protocol, 
132                 function(session ,stanza)
133                         session.log("debug", "Activating compression...")
134                         -- create deflate and inflate streams
135                         local deflate_stream = get_deflate_stream(session);
136                         if not deflate_stream then return end
137                         
138                         local inflate_stream = get_inflate_stream(session);
139                         if not inflate_stream then return end
140                         
141                         -- setup compression for session.w
142                         setup_compression(session, deflate_stream);
143                                 
144                         -- setup decompression for session.data
145                         setup_decompression(session, inflate_stream);
146                         local session_reset_stream = session.reset_stream;
147                         session.reset_stream = function(session)
148                                         session_reset_stream(session);
149                                         setup_decompression(session, inflate_stream);
150                                         return true;
151                                 end;
152                         session:reset_stream();
153                         local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
154                                                                                 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
155                         session.sends2s("<?xml version='1.0'?>");
156                         session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
157                         session.compressed = true;
158                 end
159 );
160
161 module:add_handler({"c2s_unauthed", "c2s", "s2sin_unauthed", "s2sin"}, "compress", xmlns_compression_protocol,
162                 function(session, stanza)
163                         -- fail if we are already compressed
164                         if session.compressed then
165                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed");
166                                 (session.sends2s or session.send)(error_st);
167                                 session.log("debug", "Client tried to establish another compression layer.");
168                                 return;
169                         end
170                         
171                         -- checking if the compression method is supported
172                         local method = stanza:child_with_name("method");
173                         method = method and (method[1] or "");
174                         if method == "zlib" then
175                                 session.log("debug", "zlib compression enabled.");
176                                 
177                                 -- create deflate and inflate streams
178                                 local deflate_stream = get_deflate_stream(session);
179                                 if not deflate_stream then return end
180                                 
181                                 local inflate_stream = get_inflate_stream(session);
182                                 if not inflate_stream then return end
183                                 
184                                 (session.sends2s or session.send)(st.stanza("compressed", {xmlns=xmlns_compression_protocol}));
185                                 session:reset_stream();
186                                 
187                                 -- setup compression for session.w
188                                 setup_compression(session, deflate_stream);
189                                         
190                                 -- setup decompression for session.data
191                                 setup_decompression(session, inflate_stream);
192                                 
193                                 local session_reset_stream = session.reset_stream;
194                                 session.reset_stream = function(session)
195                                                 session_reset_stream(session);
196                                                 setup_decompression(session, inflate_stream);
197                                                 return true;
198                                         end;
199                                 session.compressed = true;
200                         elseif method then
201                                 session.log("debug", "%s compression selected, but we don't support it.", tostring(method));
202                                 local error_st = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("unsupported-method");
203                                 (session.sends2s or session.send)(error_st);
204                         else
205                                 (session.sends2s or session.send)(st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("setup-failed"));
206                         end
207                 end
208 );
209