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;
7 local connlisteners_register = require "net.connlisteners".register;
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;
19 local xmppclient = { default_port = 5222 };
21 -- These are session methods --
23 local function session_reset_stream(session)
25 local parser = lxp.new(init_xmlhandlers(session, sm_streamopened), "|");
26 session.parser = parser;
28 session.notopen = true;
30 function session.data(conn, data)
36 -- End of session methods --
38 function xmppclient.listener(conn, data)
39 local session = sessions[conn];
41 session = sm_new_session(conn);
42 sessions[conn] = session;
44 -- Logging functions --
46 local mainlog, log = log;
48 local conn_name = tostring(conn):match("[a-f0-9]+$");
49 log = logger.init(conn_name);
51 local print = function (...) log("info", t_concatall({...}, "\t")); end
54 print("Client connected");
56 session.reset_stream = session_reset_stream;
58 session_reset_stream(session); -- Initialise, ready for use
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
67 session.data(conn, data);
71 function xmppclient.disconnect(conn)
72 local session = sessions[conn];
74 if session.last_presence and session.last_presence.attr.type ~= "unavailable" then
75 local pres = st.presence{ type = "unavailable" };
76 if err == "closed" then err = "connection closed"; end
77 pres:tag("status"):text("Disconnected: "..err);
78 session.stanza_dispatch(pres);
80 sm_destroy_session(session);
83 collectgarbage("collect");
87 connlisteners_register("xmppclient", xmppclient);