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 logger = require "logger";
12 local log = logger.init("xmppclient_listener");
13 local lxp = require "lxp"
14 local new_xmpp_stream = require "util.xmppstream".new;
15 local sm_new_session = require "core.sessionmanager".new_session;
17 local connlisteners_register = require "net.connlisteners".register;
19 local t_insert = table.insert;
20 local t_concat = table.concat;
21 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
22 local m_random = math.random;
23 local format = string.format;
24 local sessionmanager = require "core.sessionmanager";
25 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
26 local sm_streamopened = sessionmanager.streamopened;
27 local sm_streamclosed = sessionmanager.streamclosed;
28 local st = require "util.stanza";
30 local config = require "core.configmanager";
31 local opt_keepalives = config.get("*", "core", "tcp_keepalives");
33 local stream_callbacks = { default_ns = "jabber:client",
34 streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
36 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
38 function stream_callbacks.error(session, error, data)
39 if error == "no-stream" then
40 session.log("debug", "Invalid opening stream header");
41 session:close("invalid-namespace");
42 elseif error == "parse-error" then
43 (session.log or log)("debug", "Client XML parse error: %s", tostring(data));
44 session:close("xml-not-well-formed");
45 elseif error == "stream-error" then
46 local condition, text = "undefined-condition";
47 for child in data:children() do
48 if child.attr.xmlns == xmlns_xmpp_streams then
49 if child.name ~= "text" then
50 condition = child.name;
52 text = child:get_text();
54 if condition ~= "undefined-condition" and text then
59 text = condition .. (text and (" ("..text..")") or "");
60 session.log("info", "Session closed by remote with error: %s", text);
61 session:close(nil, text);
65 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), debug.traceback()); end
66 function stream_callbacks.handlestanza(session, stanza)
67 stanza = session.filter("stanzas/in", stanza);
69 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
74 local xmppclient = { default_port = 5222, default_mode = "*a" };
76 -- These are session methods --
78 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
79 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
80 local function session_close(session, reason)
81 local log = session.log or log;
83 if session.notopen then
84 session.send("<?xml version='1.0'?>");
85 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
88 if type(reason) == "string" then -- assume stream error
89 log("info", "Disconnecting client, <stream:error> is: %s", reason);
90 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
91 elseif type(reason) == "table" then
92 if reason.condition then
93 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
95 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
98 stanza:add_child(reason.extra);
100 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
101 session.send(stanza);
102 elseif reason.name then -- a stanza
103 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
104 session.send(reason);
108 session.send("</stream:stream>");
109 session.conn:close();
110 xmppclient.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
115 -- End of session methods --
117 function xmppclient.onconnect(conn)
118 local session = sm_new_session(conn);
119 sessions[conn] = session;
121 session.log("info", "Client connected");
123 -- Client is using legacy SSL (otherwise mod_tls sets this flag)
125 session.secure = true;
128 if opt_keepalives ~= nil then
129 conn:setoption("keepalive", opt_keepalives);
132 session.close = session_close;
134 local stream = new_xmpp_stream(session, stream_callbacks);
135 session.stream = stream;
137 session.notopen = true;
139 function session.reset_stream()
140 session.notopen = true;
141 session.stream:reset();
144 local filter = session.filter;
145 function session.data(data)
146 data = filter("bytes/in", data);
148 local ok, err = stream:feed(data);
149 if ok then return; end
150 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
151 session:close("xml-not-well-formed");
155 local handlestanza = stream_callbacks.handlestanza;
156 function session.dispatch_stanza(session, stanza)
157 return handlestanza(session, stanza);
161 function xmppclient.onincoming(conn, data)
162 local session = sessions[conn];
168 function xmppclient.ondisconnect(conn, err)
169 local session = sessions[conn];
171 (session.log or log)("info", "Client disconnected: %s", err);
172 sm_destroy_session(session, err);
173 sessions[conn] = nil;
178 connlisteners_register("xmppclient", xmppclient);