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 format = import("string", "format");
16 local full_sessions = full_sessions;
17 local bare_sessions = bare_sessions;
19 local modulemanager = require "core.modulemanager";
20 local logger = require "util.logger";
21 local log = logger.init("sessionmanager");
23 local uuid_generate = require "util.uuid".generate;
24 local rm_load_roster = require "core.rostermanager".load_roster;
25 local config_get = require "core.configmanager".get;
26 local nameprep = require "util.encodings".stringprep.nameprep;
27 local resourceprep = require "util.encodings".stringprep.resourceprep;
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 local function null_data_handler(conn, data) log("debug", "Discarding data from destroyed c2s session: %s", data); end
71 function destroy_session(session, err)
72 (session.log or log)("info", "Destroying session for %s (%s@%s)", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)");
74 -- Remove session/resource from user's session list
75 if session.full_jid then
76 hosts[session.host].events.fire_event("resource-unbind", {session=session, error=err});
78 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
79 full_sessions[session.full_jid] = nil;
81 if not next(hosts[session.host].sessions[session.username].sessions) then
82 log("debug", "All resources of %s are now offline", session.username);
83 hosts[session.host].sessions[session.username] = nil;
84 bare_sessions[session.username..'@'..session.host] = nil;
88 for k in pairs(session) do
93 session.data = null_data_handler;
96 function make_authenticated(session, username)
97 session.username = username;
98 if session.type == "c2s_unauthed" then
101 session.log("info", "Authenticated as %s@%s", username or "(unknown)", session.host or "(unknown)");
105 -- returns true, nil on success
106 -- returns nil, err_type, err, err_message on failure
107 function bind_resource(session, resource)
108 if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
109 if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
110 -- We don't support binding multiple resources
112 resource = resourceprep(resource);
113 resource = resource ~= "" and resource or uuid_generate();
114 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
116 if not hosts[session.host].sessions[session.username] then
117 local sessions = { sessions = {} };
118 hosts[session.host].sessions[session.username] = sessions;
119 bare_sessions[session.username..'@'..session.host] = sessions;
121 local sessions = hosts[session.host].sessions[session.username].sessions;
122 local limit = config_get(session.host, "core", "max_resources") or 10;
123 if #sessions >= limit then
124 return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed";
126 if sessions[resource] then
128 local policy = config_get(session.host, "core", "conflict_resolve");
130 if policy == "random" then
131 resource = uuid_generate();
133 elseif policy == "increment" then
134 increment = true; -- TODO ping old resource
135 elseif policy == "kick_new" then
136 return nil, "cancel", "conflict", "Resource already exists";
137 else -- if policy == "kick_old" then
138 sessions[resource]:close {
139 condition = "conflict";
140 text = "Replaced by new connection";
142 if not next(sessions) then
143 hosts[session.host].sessions[session.username] = { sessions = sessions };
144 bare_sessions[session.username.."@"..session.host] = hosts[session.host].sessions[session.username];
147 if increment and sessions[resource] then
149 while sessions[resource.."#"..count] do
152 resource = resource.."#"..count;
157 session.resource = resource;
158 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
159 hosts[session.host].sessions[session.username].sessions[resource] = session;
160 full_sessions[session.full_jid] = session;
162 session.roster = rm_load_roster(session.username, session.host);
164 hosts[session.host].events.fire_event("resource-bind", {session=session});
169 function streamopened(session, attr)
170 local send = session.send;
171 session.host = attr.to or error("Client failed to specify destination hostname");
172 session.host = nameprep(session.host);
173 session.version = tonumber(attr.version) or 0;
174 session.streamid = uuid_generate();
175 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
177 if not hosts[session.host] then
178 -- We don't serve this host...
179 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
183 send("<?xml version='1.0'?>");
184 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));
186 (session.log or log)("debug", "Sent reply <stream:stream> to client");
187 session.notopen = nil;
189 -- If session.secure is *false* (not nil) then it means we /were/ encrypting
190 -- since we now have a new stream header, session is secured
191 if session.secure == false then
192 session.secure = true;
195 local features = st.stanza("stream:features");
196 fire_event("stream-features", session, features);
202 function streamclosed(session)
203 session.send("</stream:stream>");
204 session.notopen = true;
207 function send_to_available_resources(user, host, stanza)
208 local jid = user.."@"..host;
210 local user = bare_sessions[jid];
212 for k, session in pairs(user.sessions) do
213 if session.presence then
214 session.send(stanza);
222 function send_to_interested_resources(user, host, stanza)
223 local jid = user.."@"..host;
225 local user = bare_sessions[jid];
227 for k, session in pairs(user.sessions) do
228 if session.interested then
229 session.send(stanza);