Remove version number from copyright headers
[prosody.git] / core / sessionmanager.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 tonumber, tostring = tonumber, tostring;
12 local ipairs, pairs, print, next= ipairs, pairs, print, next;
13 local collectgarbage = collectgarbage;
14 local m_random = import("math", "random");
15 local format = import("string", "format");
16
17 local hosts = hosts;
18 local full_sessions = full_sessions;
19 local bare_sessions = bare_sessions;
20
21 local modulemanager = require "core.modulemanager";
22 local log = require "util.logger".init("sessionmanager");
23 local error = error;
24 local uuid_generate = require "util.uuid".generate;
25 local rm_load_roster = require "core.rostermanager".load_roster;
26 local config_get = require "core.configmanager".get;
27 local nameprep = require "util.encodings".stringprep.nameprep;
28
29 local fire_event = require "core.eventmanager".fire_event;
30
31 local gettime = require "socket".gettime;
32
33 local st = require "util.stanza";
34
35 local newproxy = newproxy;
36 local getmetatable = getmetatable;
37
38 module "sessionmanager"
39
40 local open_sessions = 0;
41
42 function new_session(conn)
43         local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() };
44         if true then
45                 session.trace = newproxy(true);
46                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
47         end
48         open_sessions = open_sessions + 1;
49         log("debug", "open sessions now: ".. open_sessions);
50         local w = conn.write;
51         session.send = function (t) w(tostring(t)); end
52         session.ip = conn.ip();
53         return session;
54 end
55
56 function destroy_session(session, err)
57         (session.log or log)("info", "Destroying session for %s (%s@%s)", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)");
58         
59         -- Remove session/resource from user's session list
60         if session.full_jid then
61                 hosts[session.host].events.fire_event("resource-unbind", {session=session, error=err});
62
63                 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
64                 full_sessions[session.full_jid] = nil;
65                         
66                 if not next(hosts[session.host].sessions[session.username].sessions) then
67                         log("debug", "All resources of %s are now offline", session.username);
68                         hosts[session.host].sessions[session.username] = nil;
69                         bare_sessions[session.username..'@'..session.host] = nil;
70                 end
71         end
72         
73         for k in pairs(session) do
74                 if k ~= "trace" then
75                         session[k] = nil;
76                 end
77         end
78 end
79
80 function make_authenticated(session, username)
81         session.username = username;
82         if session.type == "c2s_unauthed" then
83                 session.type = "c2s";
84         end
85         session.log("info", "Authenticated as %s@%s", username or "(unknown)", session.host or "(unknown)");
86         return true;
87 end
88
89 -- returns true, nil on success
90 -- returns nil, err_type, err, err_message on failure
91 function bind_resource(session, resource)
92         if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
93         if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
94         -- We don't support binding multiple resources
95
96         resource = resource or uuid_generate();
97         --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
98         
99         if not hosts[session.host].sessions[session.username] then
100                 local sessions = { sessions = {} };
101                 hosts[session.host].sessions[session.username] = sessions;
102                 bare_sessions[session.username..'@'..session.host] = sessions;
103         else
104                 local sessions = hosts[session.host].sessions[session.username].sessions;
105                 local limit = config_get(session.host, "core", "max_resources") or 10;
106                 if #sessions >= limit then
107                         return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed";
108                 end
109                 if sessions[resource] then
110                         -- Resource conflict
111                         local policy = config_get(session.host, "core", "conflict_resolve");
112                         local increment;
113                         if policy == "random" then
114                                 resource = uuid_generate();
115                                 increment = true;
116                         elseif policy == "increment" then
117                                 increment = true; -- TODO ping old resource
118                         elseif policy == "kick_new" then
119                                 return nil, "cancel", "conflict", "Resource already exists";
120                         else -- if policy == "kick_old" then
121                                 sessions[resource]:close {
122                                         condition = "conflict";
123                                         text = "Replaced by new connection";
124                                 };
125                                 if not next(sessions) then
126                                         hosts[session.host].sessions[session.username] = { sessions = sessions };
127                                         bare_sessions[session.username.."@"..session.host] = hosts[session.host].sessions[session.username];
128                                 end
129                         end
130                         if increment and sessions[resource] then
131                                 local count = 1;
132                                 while sessions[resource.."#"..count] do
133                                         count = count + 1;
134                                 end
135                                 resource = resource.."#"..count;
136                         end
137                 end
138         end
139         
140         session.resource = resource;
141         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
142         hosts[session.host].sessions[session.username].sessions[resource] = session;
143         full_sessions[session.full_jid] = session;
144         
145         session.roster = rm_load_roster(session.username, session.host);
146         
147         hosts[session.host].events.fire_event("resource-bind", {session=session});
148         
149         return true;
150 end
151
152 function streamopened(session, attr)
153         local send = session.send;
154         session.host = attr.to or error("Client failed to specify destination hostname");
155         session.host = nameprep(session.host);
156         session.version = tonumber(attr.version) or 0;
157         session.streamid = m_random(1000000, 99999999);
158         (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
159         
160         send("<?xml version='1.0'?>");
161         send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' version='1.0' xml:lang='en'>", session.streamid, session.host));
162
163         if not hosts[session.host] then
164                 -- We don't serve this host...
165                 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
166                 return;
167         end
168         
169         -- If session.secure is *false* (not nil) then it means we /were/ encrypting
170         -- since we now have a new stream header, session is secured
171         if session.secure == false then
172                 session.secure = true;
173         end
174                                                 
175         local features = st.stanza("stream:features");
176         fire_event("stream-features", session, features);
177         
178         send(features);
179         
180         (session.log or log)("debug", "Sent reply <stream:stream> to client");
181         session.notopen = nil;
182 end
183
184 function streamclosed(session)
185         session.send("</stream:stream>");
186         session.notopen = true;
187 end
188
189 function send_to_available_resources(user, host, stanza)
190         local count = 0;
191         local to = stanza.attr.to;
192         stanza.attr.to = nil;
193         local h = hosts[host];
194         if h and h.type == "local" then
195                 local u = h.sessions[user];
196                 if u then
197                         for k, session in pairs(u.sessions) do
198                                 if session.presence then
199                                         session.send(stanza);
200                                         count = count + 1;
201                                 end
202                         end
203                 end
204         end
205         stanza.attr.to = to;
206         return count;
207 end
208
209 return _M;