net.xmppcomponent_listener: Removed unnecessary and problematic cleanup code.
[prosody.git] / net / xmppclient_listener.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local logger = require "logger";
12 local log = logger.init("xmppclient_listener");
13 local lxp = require "lxp"
14 local new_xmpp_stream = require "util.xmppstream".new;
15 local sm_new_session = require "core.sessionmanager".new_session;
16
17 local connlisteners_register = require "net.connlisteners".register;
18
19 local t_insert = table.insert;
20 local t_concat = table.concat;
21 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
22 local m_random = math.random;
23 local format = string.format;
24 local sessionmanager = require "core.sessionmanager";
25 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
26 local sm_streamopened = sessionmanager.streamopened;
27 local sm_streamclosed = sessionmanager.streamclosed;
28 local st = require "util.stanza";
29
30 local config = require "core.configmanager";
31 local opt_keepalives = config.get("*", "core", "tcp_keepalives");
32
33 local stream_callbacks = { default_ns = "jabber:client",
34                 streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
35
36 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
37
38 function stream_callbacks.error(session, error, data)
39         if error == "no-stream" then
40                 session.log("debug", "Invalid opening stream header");
41                 session:close("invalid-namespace");
42         elseif error == "parse-error" then
43                 (session.log or log)("debug", "Client XML parse error: %s", tostring(data));
44                 session:close("xml-not-well-formed");
45         elseif error == "stream-error" then
46                 local condition, text = "undefined-condition";
47                 for child in data:children() do
48                         if child.attr.xmlns == xmlns_xmpp_streams then
49                                 if child.name ~= "text" then
50                                         condition = child.name;
51                                 else
52                                         text = child:get_text();
53                                 end
54                                 if condition ~= "undefined-condition" and text then
55                                         break;
56                                 end
57                         end
58                 end
59                 text = condition .. (text and (" ("..text..")") or "");
60                 session.log("info", "Session closed by remote with error: %s", text);
61                 session:close(nil, text);
62         end
63 end
64
65 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), debug.traceback()); end
66 function stream_callbacks.handlestanza(session, stanza)
67         stanza = session.filter("stanzas/in", stanza);
68         if stanza then
69                 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
70         end
71 end
72
73 local sessions = {};
74 local xmppclient = { default_port = 5222, default_mode = "*a" };
75
76 -- These are session methods --
77
78 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
79 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
80 local function session_close(session, reason)
81         local log = session.log or log;
82         if session.conn then
83                 if session.notopen then
84                         session.send("<?xml version='1.0'?>");
85                         session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
86                 end
87                 if reason then
88                         if type(reason) == "string" then -- assume stream error
89                                 log("info", "Disconnecting client, <stream:error> is: %s", reason);
90                                 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
91                         elseif type(reason) == "table" then
92                                 if reason.condition then
93                                         local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
94                                         if reason.text then
95                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
96                                         end
97                                         if reason.extra then
98                                                 stanza:add_child(reason.extra);
99                                         end
100                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
101                                         session.send(stanza);
102                                 elseif reason.name then -- a stanza
103                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
104                                         session.send(reason);
105                                 end
106                         end
107                 end
108                 session.send("</stream:stream>");
109                 session.conn:close();
110                 xmppclient.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
111         end
112 end
113
114
115 -- End of session methods --
116
117 function xmppclient.onconnect(conn)
118         local session = sm_new_session(conn);
119         sessions[conn] = session;
120         
121         session.log("info", "Client connected");
122         
123         -- Client is using legacy SSL (otherwise mod_tls sets this flag)
124         if conn:ssl() then
125                 session.secure = true;
126         end
127         
128         if opt_keepalives ~= nil then
129                 conn:setoption("keepalive", opt_keepalives);
130         end
131         
132         session.close = session_close;
133         
134         local stream = new_xmpp_stream(session, stream_callbacks);
135         session.stream = stream;
136         
137         session.notopen = true;
138         
139         function session.reset_stream()
140                 session.notopen = true;
141                 session.stream:reset();
142         end
143         
144         local filter = session.filter;
145         function session.data(data)
146                 data = filter("bytes/in", data);
147                 if data then
148                         local ok, err = stream:feed(data);
149                         if ok then return; end
150                         log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
151                         session:close("xml-not-well-formed");
152                 end
153         end
154         
155         local handlestanza = stream_callbacks.handlestanza;
156         function session.dispatch_stanza(session, stanza)
157                 return handlestanza(session, stanza);
158         end
159 end
160
161 function xmppclient.onincoming(conn, data)
162         local session = sessions[conn];
163         if session then
164                 session.data(data);
165         end
166 end
167         
168 function xmppclient.ondisconnect(conn, err)
169         local session = sessions[conn];
170         if session then
171                 (session.log or log)("info", "Client disconnected: %s", err);
172                 sm_destroy_session(session, err);
173                 sessions[conn]  = nil;
174                 session = nil;
175         end
176 end
177
178 function xmppclient.associate_session(conn, session)
179         sessions[conn] = session;
180 end
181
182 connlisteners_register("xmppclient", xmppclient);