470c23d272ac1f8b91735204c8cbaad824986474
[prosody.git] / net / xmppclient_listener.lua
1 -- Prosody IM v0.1
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 = { streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
40
41 local sessions = {};
42 local xmppclient = { default_port = 5222, default_mode = "*a" };
43
44 -- These are session methods --
45
46 local function session_reset_stream(session)
47         -- Reset stream
48                 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
49                 session.parser = parser;
50                 
51                 session.notopen = true;
52                 
53                 function session.data(conn, data)
54                         parser:parse(data);
55                 end
56                 return true;
57 end
58
59
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;
63         if session.conn then
64                 if reason then
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();
71                                         if reason.text then
72                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
73                                         end
74                                         if reason.extra then
75                                                 stanza:add_child(reason.extra);
76                                         end
77                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
78                                         session.send(stanza);
79                                 elseif reason.name then -- a stanza
80                                         log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
81                                         session.send(reason);
82                                 end
83                         end
84                 end
85                 session.send("</stream:stream>");
86                 session.conn.close();
87                 xmppclient.disconnect(session.conn, "stream error");
88         end
89 end
90
91
92 -- End of session methods --
93
94 function xmppclient.listener(conn, data)
95         local session = sessions[conn];
96         if not session then
97                 session = sm_new_session(conn);
98                 sessions[conn] = session;
99
100                 -- Logging functions --
101
102                 local mainlog, log = log;
103                 do
104                         local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
105                         log = logger.init(conn_name);
106                 end
107                 local print = function (...) log("info", t_concatall({...}, "\t")); end
108                 session.log = log;
109
110                 print("Client connected");
111                 
112                 session.reset_stream = session_reset_stream;
113                 session.close = session_close;
114                 
115                 session_reset_stream(session); -- Initialise, ready for use
116                 
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)
120
121                 -- Debug version --
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
124                 
125         end
126         if data then
127                 session.data(conn, data);
128         end
129 end
130         
131 function xmppclient.disconnect(conn, err)
132         local session = sessions[conn];
133         if session then
134                 (session.log or log)("info", "Client disconnected: %s", err);
135                 sm_destroy_session(session);
136                 sessions[conn]  = nil;
137                 session = nil;
138                 collectgarbage("collect");
139         end
140 end
141
142 connlisteners_register("xmppclient", xmppclient);