|
1 |
|
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 |
|
9 local connlisteners_register = require "net.connlisteners".register; |
|
10 |
|
11 local t_insert = table.insert; |
|
12 local t_concat = table.concat; |
|
13 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 |
|
14 local m_random = math.random; |
|
15 local format = string.format; |
|
16 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); |
|
17 local st = stanza; |
|
18 |
|
19 local sessions = {}; |
|
20 local xmppserver = { default_port = 5269 }; |
|
21 |
|
22 -- These are session methods -- |
|
23 |
|
24 local function session_reset_stream(session) |
|
25 -- Reset stream |
|
26 local parser = lxp.new(init_xmlhandlers(session, s2s_streamopened), "|"); |
|
27 session.parser = parser; |
|
28 |
|
29 session.notopen = true; |
|
30 |
|
31 function session.data(conn, data) |
|
32 parser:parse(data); |
|
33 end |
|
34 return true; |
|
35 end |
|
36 |
|
37 -- End of session methods -- |
|
38 |
|
39 function xmppserver.listener(conn, data) |
|
40 local session = sessions[conn]; |
|
41 if not session then |
|
42 session = s2s_new_incoming(conn); |
|
43 sessions[conn] = session; |
|
44 |
|
45 -- Logging functions -- |
|
46 |
|
47 local mainlog, log = log; |
|
48 do |
|
49 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$"); |
|
50 log = logger.init(conn_name); |
|
51 end |
|
52 local print = function (...) log("info", t_concatall({...}, "\t")); end |
|
53 session.log = log; |
|
54 |
|
55 print("Incoming s2s connection"); |
|
56 |
|
57 session.reset_stream = session_reset_stream; |
|
58 |
|
59 session_reset_stream(session); -- Initialise, ready for use |
|
60 |
|
61 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, |
|
62 -- this will avoid the useless indirection we have atm |
|
63 -- (I'm on a mission, no time to fix now) |
|
64 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
|
65 |
|
66 end |
|
67 if data then |
|
68 session.data(conn, data); |
|
69 end |
|
70 end |
|
71 |
|
72 function xmppserver.disconnect(conn) |
|
73 end |
|
74 |
|
75 function xmppserver.register_outgoing(conn, session) |
|
76 session.direction = "outgoing"; |
|
77 sessions[conn] = session; |
|
78 |
|
79 session.reset_stream = session_reset_stream; |
|
80 session_reset_stream(session); -- Initialise, ready for use |
|
81 |
|
82 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, |
|
83 -- this will avoid the useless indirection we have atm |
|
84 -- (I'm on a mission, no time to fix now) |
|
85 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
|
86 end |
|
87 |
|
88 connlisteners_register("xmppserver", xmppserver); |
|
89 |
|
90 |
|
91 -- We need to perform some initialisation when a connection is created |
|
92 -- We also need to perform that same initialisation at other points (SASL, TLS, ...) |
|
93 |
|
94 -- ...and we need to handle data |
|
95 -- ...and record all sessions associated with connections |