2 local logger = require "logger";
3 local lxp = require "lxp"
4 local init_xmlhandlers = require "core.xmlhandlers"
5 local sm_new_session = require "core.sessionmanager".new_session;
6 local s2s_new_incoming = require "core.s2smanager".new_incoming;
7 local s2s_streamopened = require "core.s2smanager".streamopened;
8 local s2s_destroy_session = require "core.s2smanager".destroy_session;
10 local connlisteners_register = require "net.connlisteners".register;
12 local t_insert = table.insert;
13 local t_concat = table.concat;
14 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
15 local m_random = math.random;
16 local format = string.format;
17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
21 local xmppserver = { default_port = 5269 };
23 -- These are session methods --
25 local function session_reset_stream(session)
27 local parser = lxp.new(init_xmlhandlers(session, s2s_streamopened), "|");
28 session.parser = parser;
30 session.notopen = true;
32 function session.data(conn, data)
38 -- End of session methods --
40 function xmppserver.listener(conn, data)
41 local session = sessions[conn];
43 session = s2s_new_incoming(conn);
44 sessions[conn] = session;
46 -- Logging functions --
48 local mainlog, log = log;
50 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$");
51 log = logger.init(conn_name);
53 local print = function (...) log("info", t_concatall({...}, "\t")); end
56 print("Incoming s2s connection");
58 session.reset_stream = session_reset_stream;
60 session_reset_stream(session); -- Initialise, ready for use
62 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call,
63 -- this will avoid the useless indirection we have atm
64 -- (I'm on a mission, no time to fix now)
67 local function handleerr() print("Traceback:", debug.traceback()); end
68 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end
70 -- session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
74 session.data(conn, data);
78 function xmppserver.disconnect(conn)
79 local session = sessions[conn];
81 s2s_destroy_session(session);
84 collectgarbage("collect");
88 function xmppserver.register_outgoing(conn, session)
89 session.direction = "outgoing";
90 sessions[conn] = session;
92 session.reset_stream = session_reset_stream;
93 session_reset_stream(session); -- Initialise, ready for use
95 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call,
96 -- this will avoid the useless indirection we have atm
97 -- (I'm on a mission, no time to fix now)
98 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
101 connlisteners_register("xmppserver", xmppserver);
104 -- We need to perform some initialisation when a connection is created
105 -- We also need to perform that same initialisation at other points (SASL, TLS, ...)
107 -- ...and we need to handle data
108 -- ...and record all sessions associated with connections