Automated merge with http://waqas.ath.cx:8000/
[prosody.git] / net / xmppclient_listener.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
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.
9 -- 
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.
14 -- 
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.
18 --
19
20
21
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;
26
27 local connlisteners_register = require "net.connlisteners".register;
28
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;
37 local st = stanza;
38
39 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
40
41 function stream_callbacks.error(session, error, data)
42         if error == "no-stream" then
43                 session:close("invalid-namespace");
44         else
45                 session.log("debug", "Client XML parse error: %s", tostring(error));
46                 session:close("xml-not-well-formed");
47         end
48 end
49
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);
53 end
54
55 local sessions = {};
56 local xmppclient = { default_port = 5222, default_mode = "*a" };
57
58 -- These are session methods --
59
60 local function session_reset_stream(session)
61         -- Reset stream
62                 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
63                 session.parser = parser;
64                 
65                 session.notopen = true;
66                 
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");
71                 end
72                 
73                 return true;
74 end
75
76
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;
80         if session.conn then
81                 if reason then
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();
88                                         if reason.text then
89                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
90                                         end
91                                         if reason.extra then
92                                                 stanza:add_child(reason.extra);
93                                         end
94                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
95                                         session.send(stanza);
96                                 elseif reason.name then -- a stanza
97                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
98                                         session.send(reason);
99                                 end
100                         end
101                 end
102                 session.send("</stream:stream>");
103                 session.conn.close();
104                 xmppclient.disconnect(session.conn, "stream error");
105         end
106 end
107
108
109 -- End of session methods --
110
111 function xmppclient.listener(conn, data)
112         local session = sessions[conn];
113         if not session then
114                 session = sm_new_session(conn);
115                 sessions[conn] = session;
116
117                 -- Logging functions --
118
119                 local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
120                 session.log = logger.init(conn_name);
121                 
122                 session.log("info", "Client connected");
123                 
124                 session.reset_stream = session_reset_stream;
125                 session.close = session_close;
126                 
127                 session_reset_stream(session); -- Initialise, ready for use
128                 
129                 session.dispatch_stanza = stream_callbacks.handlestanza;
130         end
131         if data then
132                 session.data(conn, data);
133         end
134 end
135         
136 function xmppclient.disconnect(conn, err)
137         local session = sessions[conn];
138         if session then
139                 (session.log or log)("info", "Client disconnected: %s", err);
140                 sm_destroy_session(session);
141                 sessions[conn]  = nil;
142                 session = nil;
143                 collectgarbage("collect");
144         end
145 end
146
147 connlisteners_register("xmppclient", xmppclient);