2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local add_task = require "util.timer".add_task;
12 local new_xmpp_stream = require "util.xmppstream".new;
13 local nameprep = require "util.encodings".stringprep.nameprep;
14 local sessionmanager = require "core.sessionmanager";
15 local st = require "util.stanza";
16 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
17 local uuid_generate = require "util.uuid".generate;
19 local xpcall, tostring, type = xpcall, tostring, type;
20 local format = string.format;
21 local traceback = debug.traceback;
23 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
25 local log = module._log;
27 local c2s_timeout = module:get_option_number("c2s_timeout");
28 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false);
30 local sessions = module:shared("sessions");
32 local stream_callbacks = { default_ns = "jabber:client", handlestanza = core_process_stanza };
35 --- Stream events handlers
36 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
37 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
39 function stream_callbacks.streamopened(session, attr)
40 local send = session.send;
41 session.host = nameprep(attr.to);
42 if not session.host then
43 session:close{ condition = "improper-addressing",
44 text = "A valid 'to' attribute is required on stream headers" };
47 session.version = tonumber(attr.version) or 0;
48 session.streamid = uuid_generate();
49 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
51 if not hosts[session.host] then
52 -- We don't serve this host...
53 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
57 send("<?xml version='1.0'?>");
58 send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' version='1.0' xml:lang='en'>", session.streamid, session.host));
60 (session.log or log)("debug", "Sent reply <stream:stream> to client");
61 session.notopen = nil;
63 -- If session.secure is *false* (not nil) then it means we /were/ encrypting
64 -- since we now have a new stream header, session is secured
65 if session.secure == false then
66 session.secure = true;
69 local features = st.stanza("stream:features");
70 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
71 module:fire_event("stream-features", session, features);
76 function stream_callbacks.streamclosed(session)
77 session.log("debug", "Received </stream:stream>");
81 function stream_callbacks.error(session, error, data)
82 if error == "no-stream" then
83 session.log("debug", "Invalid opening stream header");
84 session:close("invalid-namespace");
85 elseif error == "parse-error" then
86 (session.log or log)("debug", "Client XML parse error: %s", tostring(data));
87 session:close("not-well-formed");
88 elseif error == "stream-error" then
89 local condition, text = "undefined-condition";
90 for child in data:children() do
91 if child.attr.xmlns == xmlns_xmpp_streams then
92 if child.name ~= "text" then
93 condition = child.name;
95 text = child:get_text();
97 if condition ~= "undefined-condition" and text then
102 text = condition .. (text and (" ("..text..")") or "");
103 session.log("info", "Session closed by remote with error: %s", text);
104 session:close(nil, text);
108 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), traceback()); end
109 function stream_callbacks.handlestanza(session, stanza)
110 stanza = session.filter("stanzas/in", stanza);
112 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
117 local function session_close(session, reason)
118 local log = session.log or log;
120 if session.notopen then
121 session.send("<?xml version='1.0'?>");
122 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
125 if type(reason) == "string" then -- assume stream error
126 log("info", "Disconnecting client, <stream:error> is: %s", reason);
127 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
128 elseif type(reason) == "table" then
129 if reason.condition then
130 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
132 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
135 stanza:add_child(reason.extra);
137 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
138 session.send(stanza);
139 elseif reason.name then -- a stanza
140 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
141 session.send(reason);
145 session.send("</stream:stream>");
146 session.conn:close();
147 listener.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
152 function listener.onconnect(conn)
153 local session = sm_new_session(conn);
154 sessions[conn] = session;
156 session.log("info", "Client connected");
158 -- Client is using legacy SSL (otherwise mod_tls sets this flag)
160 session.secure = true;
163 if opt_keepalives then
164 conn:setoption("keepalive", opt_keepalives);
167 session.close = session_close;
169 local stream = new_xmpp_stream(session, stream_callbacks);
170 session.stream = stream;
171 session.notopen = true;
173 function session.reset_stream()
174 session.notopen = true;
175 session.stream:reset();
178 local filter = session.filter;
179 function session.data(data)
180 data = filter("bytes/in", data);
182 local ok, err = stream:feed(data);
183 if ok then return; end
184 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
185 session:close("not-well-formed");
191 add_task(c2s_timeout, function ()
192 if session.type == "c2s_unauthed" then
193 session:close("connection-timeout");
198 session.dispatch_stanza = stream_callbacks.handlestanza;
201 function listener.onincoming(conn, data)
202 local session = sessions[conn];
208 function listener.ondisconnect(conn, err)
209 local session = sessions[conn];
211 (session.log or log)("info", "Client disconnected: %s", err);
212 sm_destroy_session(session, err);
213 sessions[conn] = nil;
218 function listener.associate_session(conn, session)
219 sessions[conn] = session;
222 module:add_item("net-provider", {
226 encryption = "starttls";
228 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";
232 module:add_item("net-provider", {
237 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";