Merging.
[prosody.git] / net / xmppserver_listener.lua
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 local s2s_destroy_session = require "core.s2smanager".destroy_session;
9
10 local connlisteners_register = require "net.connlisteners".register;
11
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");
18 local st = stanza;
19
20 local sessions = {};
21 local xmppserver = { default_port = 5269 };
22
23 -- These are session methods --
24
25 local function session_reset_stream(session)
26         -- Reset stream
27                 local parser = lxp.new(init_xmlhandlers(session, s2s_streamopened), "|");
28                 session.parser = parser;
29                 
30                 session.notopen = true;
31                 
32                 function session.data(conn, data)
33                         parser:parse(data);
34                 end
35                 return true;
36 end
37
38 -- End of session methods --
39
40 function xmppserver.listener(conn, data)
41         local session = sessions[conn];
42         if not session then
43                 session = s2s_new_incoming(conn);
44                 sessions[conn] = session;
45
46                 -- Logging functions --
47
48                 local mainlog, log = log;
49                 do
50                         local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$");
51                         log = logger.init(conn_name);
52                 end
53                 local print = function (...) log("info", t_concatall({...}, "\t")); end
54                 session.log = log;
55
56                 print("Incoming s2s connection");
57                 
58                 session.reset_stream = session_reset_stream;
59                 
60                 session_reset_stream(session); -- Initialise, ready for use
61                 
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)
65
66                 -- Debug version --
67                 local function handleerr(err) print("Traceback:", err, debug.traceback()); end
68                 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr));  end
69
70 --              session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
71
72         end
73         if data then
74                 session.data(conn, data);
75         end
76 end
77         
78 function xmppserver.disconnect(conn)
79         local session = sessions[conn];
80         if session then
81                 s2s_destroy_session(session);
82                 sessions[conn]  = nil;
83                 session = nil;
84                 collectgarbage("collect");
85         end
86 end
87
88 function xmppserver.register_outgoing(conn, session)
89         session.direction = "outgoing";
90         sessions[conn] = session;
91         
92         session.reset_stream = session_reset_stream;    
93         session_reset_stream(session); -- Initialise, ready for use
94         
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
99 end
100
101 connlisteners_register("xmppserver", xmppserver);
102
103
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, ...)
106
107 -- ...and we need to handle data
108 -- ...and record all sessions associated with connections