2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local tonumber, tostring = tonumber, tostring;
12 local ipairs, pairs, print, next= ipairs, pairs, print, next;
13 local collectgarbage = collectgarbage;
14 local format = import("string", "format");
17 local full_sessions = full_sessions;
18 local bare_sessions = bare_sessions;
20 local modulemanager = require "core.modulemanager";
21 local logger = require "util.logger";
22 local log = logger.init("sessionmanager");
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;
29 local fire_event = require "core.eventmanager".fire_event;
30 local add_task = require "util.timer".add_task;
31 local gettime = require "socket".gettime;
33 local st = require "util.stanza";
35 local c2s_timeout = config_get("*", "core", "c2s_timeout");
37 local newproxy = newproxy;
38 local getmetatable = getmetatable;
40 module "sessionmanager"
42 local open_sessions = 0;
44 function new_session(conn)
45 local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() };
47 session.trace = newproxy(true);
48 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
50 open_sessions = open_sessions + 1;
51 log("debug", "open sessions now: ".. open_sessions);
53 session.send = function (t) w(tostring(t)); end
54 session.ip = conn.ip();
55 local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
56 session.log = logger.init(conn_name);
59 add_task(c2s_timeout, function ()
60 if session.type == "c2s_unauthed" then
61 session:close("connection-timeout");
69 function destroy_session(session, err)
70 (session.log or log)("info", "Destroying session for %s (%s@%s)", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)");
72 -- Remove session/resource from user's session list
73 if session.full_jid then
74 hosts[session.host].events.fire_event("resource-unbind", {session=session, error=err});
76 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
77 full_sessions[session.full_jid] = nil;
79 if not next(hosts[session.host].sessions[session.username].sessions) then
80 log("debug", "All resources of %s are now offline", session.username);
81 hosts[session.host].sessions[session.username] = nil;
82 bare_sessions[session.username..'@'..session.host] = nil;
86 for k in pairs(session) do
93 function make_authenticated(session, username)
94 session.username = username;
95 if session.type == "c2s_unauthed" then
98 session.log("info", "Authenticated as %s@%s", username or "(unknown)", session.host or "(unknown)");
102 -- returns true, nil on success
103 -- returns nil, err_type, err, err_message on failure
104 function bind_resource(session, resource)
105 if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
106 if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
107 -- We don't support binding multiple resources
109 resource = resource or uuid_generate();
110 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
112 if not hosts[session.host].sessions[session.username] then
113 local sessions = { sessions = {} };
114 hosts[session.host].sessions[session.username] = sessions;
115 bare_sessions[session.username..'@'..session.host] = sessions;
117 local sessions = hosts[session.host].sessions[session.username].sessions;
118 local limit = config_get(session.host, "core", "max_resources") or 10;
119 if #sessions >= limit then
120 return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed";
122 if sessions[resource] then
124 local policy = config_get(session.host, "core", "conflict_resolve");
126 if policy == "random" then
127 resource = uuid_generate();
129 elseif policy == "increment" then
130 increment = true; -- TODO ping old resource
131 elseif policy == "kick_new" then
132 return nil, "cancel", "conflict", "Resource already exists";
133 else -- if policy == "kick_old" then
134 sessions[resource]:close {
135 condition = "conflict";
136 text = "Replaced by new connection";
138 if not next(sessions) then
139 hosts[session.host].sessions[session.username] = { sessions = sessions };
140 bare_sessions[session.username.."@"..session.host] = hosts[session.host].sessions[session.username];
143 if increment and sessions[resource] then
145 while sessions[resource.."#"..count] do
148 resource = resource.."#"..count;
153 session.resource = resource;
154 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
155 hosts[session.host].sessions[session.username].sessions[resource] = session;
156 full_sessions[session.full_jid] = session;
158 session.roster = rm_load_roster(session.username, session.host);
160 hosts[session.host].events.fire_event("resource-bind", {session=session});
165 function streamopened(session, attr)
166 local send = session.send;
167 session.host = attr.to or error("Client failed to specify destination hostname");
168 session.host = nameprep(session.host);
169 session.version = tonumber(attr.version) or 0;
170 session.streamid = uuid_generate();
171 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
173 if not hosts[session.host] then
174 -- We don't serve this host...
175 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
179 send("<?xml version='1.0'?>");
180 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));
182 (session.log or log)("debug", "Sent reply <stream:stream> to client");
183 session.notopen = nil;
185 -- If session.secure is *false* (not nil) then it means we /were/ encrypting
186 -- since we now have a new stream header, session is secured
187 if session.secure == false then
188 session.secure = true;
191 local features = st.stanza("stream:features");
192 fire_event("stream-features", session, features);
198 function streamclosed(session)
199 session.send("</stream:stream>");
200 session.notopen = true;
203 function send_to_available_resources(user, host, stanza)
204 local jid = user.."@"..host;
206 local user = bare_sessions[jid];
208 for k, session in pairs(user.sessions) do
209 if session.presence then
210 session.send(stanza);
218 function send_to_interested_resources(user, host, stanza)
219 local jid = user.."@"..host;
221 local user = bare_sessions[jid];
223 for k, session in pairs(user.sessions) do
224 if session.interested then
225 session.send(stanza);