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 = { stream_tag = "http://etherx.jabber.org/streams|stream", streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
41 function stream_callbacks.error(session, error, data)
42 if error == "no-stream" then
43 session:close("invalid-namespace");
45 session.log("debug", "Client XML parse error: %s", tostring(error));
46 session:close("xml-not-well-formed");
50 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), debug.traceback()); end
51 function stream_callbacks.handlestanza(a, b)
52 xpcall(function () core_process_stanza(a, b) end, handleerr);
56 local xmppclient = { default_port = 5222, default_mode = "*a" };
58 -- These are session methods --
60 local function session_reset_stream(session)
62 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
63 session.parser = parser;
65 session.notopen = true;
67 function session.data(conn, data)
68 local ok, err = parser:parse(data);
69 if ok then return; end
70 session:close("xml-not-well-formed");
77 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
78 local function session_close(session, reason)
79 local log = session.log or log;
82 if type(reason) == "string" then -- assume stream error
83 log("info", "Disconnecting client, <stream:error> is: %s", reason);
84 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
85 elseif type(reason) == "table" then
86 if reason.condition then
87 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
89 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
92 stanza:add_child(reason.extra);
94 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
96 elseif reason.name then -- a stanza
97 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
102 session.send("</stream:stream>");
103 session.conn.close();
104 xmppclient.disconnect(session.conn, "stream error");
109 -- End of session methods --
111 function xmppclient.listener(conn, data)
112 local session = sessions[conn];
114 session = sm_new_session(conn);
115 sessions[conn] = session;
117 -- Logging functions --
119 local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
120 session.log = logger.init(conn_name);
122 session.log("info", "Client connected");
124 session.reset_stream = session_reset_stream;
125 session.close = session_close;
127 session_reset_stream(session); -- Initialise, ready for use
129 session.dispatch_stanza = stream_callbacks.handlestanza;
132 session.data(conn, data);
136 function xmppclient.disconnect(conn, err)
137 local session = sessions[conn];
139 (session.log or log)("info", "Client disconnected: %s", err);
140 sm_destroy_session(session);
141 sessions[conn] = nil;
143 collectgarbage("collect");
147 connlisteners_register("xmppclient", xmppclient);