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