mod_tls: Merged duplicate code.
[prosody.git] / net / xmppclient_listener.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 init_xmlhandlers = require "core.xmlhandlers"
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 function stream_callbacks.error(session, error, data)
37         if error == "no-stream" then
38                 session.log("debug", "Invalid opening stream header");
39                 session:close("invalid-namespace");
40         elseif session.close then
41                 (session.log or log)("debug", "Client XML parse error: %s", tostring(error));
42                 session:close("xml-not-well-formed");
43         end
44 end
45
46 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), debug.traceback()); end
47 function stream_callbacks.handlestanza(a, b)
48         xpcall(function () core_process_stanza(a, b) end, handleerr);
49 end
50
51 local sessions = {};
52 local xmppclient = { default_port = 5222, default_mode = "*a" };
53
54 -- These are session methods --
55
56 local function session_reset_stream(session)
57         -- Reset stream
58                 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
59                 session.parser = parser;
60                 
61                 session.notopen = true;
62                 
63                 function session.data(conn, data)
64                         local ok, err = parser:parse(data);
65                         if ok then return; end
66                         log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
67                         session:close("xml-not-well-formed");
68                 end
69                 
70                 return true;
71 end
72
73 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
74 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
75 local function session_close(session, reason)
76         local log = session.log or log;
77         if session.conn then
78                 if session.notopen then
79                         session.send("<?xml version='1.0'?>");
80                         session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
81                 end
82                 if reason then
83                         if type(reason) == "string" then -- assume stream error
84                                 log("info", "Disconnecting client, <stream:error> is: %s", reason);
85                                 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
86                         elseif type(reason) == "table" then
87                                 if reason.condition then
88                                         local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
89                                         if reason.text then
90                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
91                                         end
92                                         if reason.extra then
93                                                 stanza:add_child(reason.extra);
94                                         end
95                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
96                                         session.send(stanza);
97                                 elseif reason.name then -- a stanza
98                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
99                                         session.send(reason);
100                                 end
101                         end
102                 end
103                 session.send("</stream:stream>");
104                 session.conn:close();
105                 xmppclient.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
106         end
107 end
108
109
110 -- End of session methods --
111
112 function xmppclient.onincoming(conn, data)
113         local session = sessions[conn];
114         if not session then
115                 session = sm_new_session(conn);
116                 sessions[conn] = session;
117
118                 session.log("info", "Client connected");
119                 
120                 -- Client is using legacy SSL (otherwise mod_tls sets this flag)
121                 if conn:ssl() then
122                         session.secure = true;
123                 end
124                 
125                 if opt_keepalives ~= nil then
126                         conn:setoption("keepalive", opt_keepalives);
127                 end
128                 
129                 session.reset_stream = session_reset_stream;
130                 session.close = session_close;
131                 
132                 session_reset_stream(session); -- Initialise, ready for use
133                 
134                 session.dispatch_stanza = stream_callbacks.handlestanza;
135         end
136         if data then
137                 session.data(conn, data);
138         end
139 end
140         
141 function xmppclient.ondisconnect(conn, err)
142         local session = sessions[conn];
143         if session then
144                 (session.log or log)("info", "Client disconnected: %s", err);
145                 sm_destroy_session(session, err);
146                 sessions[conn]  = nil;
147                 session = nil;
148         end
149 end
150
151 connlisteners_register("xmppclient", xmppclient);