X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=net%2Fxmppclient_listener.lua;h=ad1fd9489e65106bce7aa39b87f7ea43dd932d60;hb=2f7c6103fa59195b6e86a10400a95540fa796df2;hp=e89a591eb3696685588473133f63cb0dce90c77c;hpb=c5f72005e67cd9fc6c6bd54fd3df02b98bf6292d;p=prosody.git diff --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua index e89a591e..7cffc0ca 100644 --- a/net/xmppclient_listener.lua +++ b/net/xmppclient_listener.lua @@ -1,7 +1,17 @@ +-- Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + + local logger = require "logger"; +local log = logger.init("xmppclient_listener"); local lxp = require "lxp" -local init_xmlhandlers = require "core.xmlhandlers" +local new_xmpp_stream = require "util.xmppstream".new; local sm_new_session = require "core.sessionmanager".new_session; local connlisteners_register = require "net.connlisteners".register; @@ -11,36 +21,69 @@ local t_concat = table.concat; 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 local m_random = math.random; local format = string.format; -local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); +local sessionmanager = require "core.sessionmanager"; +local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; local sm_streamopened = sessionmanager.streamopened; local sm_streamclosed = sessionmanager.streamclosed; -local st = stanza; +local st = require "util.stanza"; + +local config = require "core.configmanager"; +local opt_keepalives = config.get("*", "core", "tcp_keepalives"); + +local stream_callbacks = { default_ns = "jabber:client", + streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza }; + +local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; + +function stream_callbacks.error(session, error, data) + if error == "no-stream" then + session.log("debug", "Invalid opening stream header"); + session:close("invalid-namespace"); + elseif error == "parse-error" then + (session.log or log)("debug", "Client XML parse error: %s", tostring(data)); + session:close("not-well-formed"); + elseif error == "stream-error" then + local condition, text = "undefined-condition"; + for child in data:children() do + if child.attr.xmlns == xmlns_xmpp_streams then + if child.name ~= "text" then + condition = child.name; + else + text = child:get_text(); + end + if condition ~= "undefined-condition" and text then + break; + end + end + end + text = condition .. (text and (" ("..text..")") or ""); + session.log("info", "Session closed by remote with error: %s", text); + session:close(nil, text); + end +end -local stream_callbacks = { streamopened = sm_streamopened, streamclosed = sm_streamclosed }; +local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), debug.traceback()); end +function stream_callbacks.handlestanza(session, stanza) + stanza = session.filter("stanzas/in", stanza); + if stanza then + return xpcall(function () return core_process_stanza(session, stanza) end, handleerr); + end +end local sessions = {}; -local xmppclient = { default_port = 5222 }; +local xmppclient = { default_port = 5222, default_mode = "*a" }; -- These are session methods -- -local function session_reset_stream(session) - -- Reset stream - local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|"); - session.parser = parser; - - session.notopen = true; - - function session.data(conn, data) - parser:parse(data); - end - return true; -end - - local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; -local function session_disconnect(session, reason) +local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; +local function session_close(session, reason) local log = session.log or log; if session.conn then + if session.notopen then + session.send(""); + session.send(st.stanza("stream:stream", default_stream_attr):top_tag()); + end if reason then if type(reason) == "string" then -- assume stream error log("info", "Disconnecting client, is: %s", reason); @@ -63,59 +106,77 @@ local function session_disconnect(session, reason) end end session.send(""); - session.conn.close(); - xmppclient.disconnect(session.conn, "stream error"); + session.conn:close(); + xmppclient.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed"); end end -- End of session methods -- -function xmppclient.listener(conn, data) - local session = sessions[conn]; - if not session then - session = sm_new_session(conn); - sessions[conn] = session; - - -- Logging functions -- - - local mainlog, log = log; - do - local conn_name = tostring(conn):match("[a-f0-9]+$"); - log = logger.init(conn_name); +function xmppclient.onconnect(conn) + local session = sm_new_session(conn); + sessions[conn] = session; + + session.log("info", "Client connected"); + + -- Client is using legacy SSL (otherwise mod_tls sets this flag) + if conn:ssl() then + session.secure = true; + end + + if opt_keepalives ~= nil then + conn:setoption("keepalive", opt_keepalives); + end + + session.close = session_close; + + local stream = new_xmpp_stream(session, stream_callbacks); + session.stream = stream; + + session.notopen = true; + + function session.reset_stream() + session.notopen = true; + session.stream:reset(); + end + + local filter = session.filter; + function session.data(data) + data = filter("bytes/in", data); + if data then + local ok, err = stream:feed(data); + if ok then return; end + log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_")); + session:close("not-well-formed"); end - local print = function (...) log("info", t_concatall({...}, "\t")); end - session.log = log; - - print("Client connected"); - - session.reset_stream = session_reset_stream; - session.disconnect = session_disconnect; - - session_reset_stream(session); -- Initialise, ready for use - - -- TODO: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, - -- this will avoid the useless indirection we have atm - -- (I'm on a mission, no time to fix now) - - -- Debug version -- - local function handleerr(err) print("Traceback:", err, debug.traceback()); end - session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end end - if data then - session.data(conn, data); + + local handlestanza = stream_callbacks.handlestanza; + function session.dispatch_stanza(session, stanza) + return handlestanza(session, stanza); + end +end + +function xmppclient.onincoming(conn, data) + local session = sessions[conn]; + if session then + session.data(data); end end -function xmppclient.disconnect(conn, err) +function xmppclient.ondisconnect(conn, err) local session = sessions[conn]; if session then (session.log or log)("info", "Client disconnected: %s", err); - sm_destroy_session(session); + sm_destroy_session(session, err); sessions[conn] = nil; session = nil; - collectgarbage("collect"); end end +function xmppclient.associate_session(conn, session) + sessions[conn] = session; +end + connlisteners_register("xmppclient", xmppclient);