2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local logger = require "logger";
23 local lxp = require "lxp"
24 local init_xmlhandlers = require "core.xmlhandlers"
25 local sm_new_session = require "core.sessionmanager".new_session;
27 local connlisteners_register = require "net.connlisteners".register;
29 local t_insert = table.insert;
30 local t_concat = table.concat;
31 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
32 local m_random = math.random;
33 local format = string.format;
34 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
35 local sm_streamopened = sessionmanager.streamopened;
36 local sm_streamclosed = sessionmanager.streamclosed;
39 local stream_callbacks = { streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
42 local xmppclient = { default_port = 5222, default_mode = "*a" };
44 -- These are session methods --
46 local function session_reset_stream(session)
48 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
49 session.parser = parser;
51 session.notopen = true;
53 function session.data(conn, data)
60 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
61 local function session_close(session, reason)
62 local log = session.log or log;
65 if type(reason) == "string" then -- assume stream error
66 log("info", "Disconnecting client, <stream:error> is: %s", reason);
67 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
68 elseif type(reason) == "table" then
69 if reason.condition then
70 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
72 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
75 stanza:add_child(reason.extra);
77 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
79 elseif reason.name then -- a stanza
80 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
85 session.send("</stream:stream>");
87 xmppclient.disconnect(session.conn, "stream error");
92 -- End of session methods --
94 function xmppclient.listener(conn, data)
95 local session = sessions[conn];
97 session = sm_new_session(conn);
98 sessions[conn] = session;
100 -- Logging functions --
102 local mainlog, log = log;
104 local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
105 log = logger.init(conn_name);
107 local print = function (...) log("info", t_concatall({...}, "\t")); end
110 print("Client connected");
112 session.reset_stream = session_reset_stream;
113 session.close = session_close;
115 session_reset_stream(session); -- Initialise, ready for use
117 -- TODO: Below function should be session,stanza - and xmlhandlers should use :method() notation to call,
118 -- this will avoid the useless indirection we have atm
119 -- (I'm on a mission, no time to fix now)
122 --local function handleerr(err) print("Traceback:", err, debug.traceback()); end
123 --session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end
127 session.data(conn, data);
131 function xmppclient.disconnect(conn, err)
132 local session = sessions[conn];
134 (session.log or log)("info", "Client disconnected: %s", err);
135 sm_destroy_session(session);
136 sessions[conn] = nil;
138 collectgarbage("collect");
142 connlisteners_register("xmppclient", xmppclient);