net/xmppclient_listener.lua
changeset 99 ba08b8a4eeef
child 123 ebd65feb188c
equal deleted inserted replaced
98:3a2d327c4856 99:ba08b8a4eeef
       
     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 
       
     7 local connlisteners_register = require "net.connlisteners".register;
       
     8 
       
     9 local t_insert = table.insert;
       
    10 local t_concat = table.concat;
       
    11 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
       
    12 local m_random = math.random;
       
    13 local format = string.format;
       
    14 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
       
    15 local sm_streamopened = sessionmanager.streamopened;
       
    16 local st = stanza;
       
    17 
       
    18 local sessions = {};
       
    19 local xmppclient = { default_port = 5222 };
       
    20 
       
    21 -- These are session methods --
       
    22 
       
    23 local function session_reset_stream(session)
       
    24 	-- Reset stream
       
    25 		local parser = lxp.new(init_xmlhandlers(session, sm_streamopened), ":");
       
    26 		session.parser = parser;
       
    27 		
       
    28 		session.notopen = true;
       
    29 		
       
    30 		function session.data(conn, data)
       
    31 			parser:parse(data);
       
    32 		end
       
    33 		return true;
       
    34 end
       
    35 
       
    36 -- End of session methods --
       
    37 
       
    38 function xmppclient.listener(conn, data)
       
    39 	local session = sessions[conn];
       
    40 	if not session then
       
    41 		session = sm_new_session(conn);
       
    42 		sessions[conn] = session;
       
    43 
       
    44 		-- Logging functions --
       
    45 
       
    46 		local mainlog, log = log;
       
    47 		do
       
    48 			local conn_name = tostring(conn):match("[a-f0-9]+$");
       
    49 			log = logger.init(conn_name);
       
    50 		end
       
    51 		local print = function (...) log("info", t_concatall({...}, "\t")); end
       
    52 		session.log = log;
       
    53 
       
    54 		print("Client connected");
       
    55 		
       
    56 		session.reset_stream = session_reset_stream;
       
    57 		
       
    58 		session_reset_stream(session); -- Initialise, ready for use
       
    59 		
       
    60 		-- TODO: Below function should be session,stanza - and xmlhandlers should use :method() notation to call,
       
    61 		-- this will avoid the useless indirection we have atm
       
    62 		-- (I'm on a mission, no time to fix now)
       
    63 		session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
       
    64 
       
    65 	end
       
    66 	if data then
       
    67 		session.data(conn, data);
       
    68 	end
       
    69 end
       
    70 	
       
    71 function xmppclient.disconnect(conn)
       
    72 end
       
    73 
       
    74 connlisteners_register("xmppclient", xmppclient);