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");
9 local sessions = sessions;
11 local modulemanager = require "core.modulemanager";
12 local log = require "util.logger".init("sessionmanager");
14 local uuid_generate = require "util.uuid".generate;
15 local rm_load_roster = require "core.rostermanager".load_roster;
17 local newproxy = newproxy;
18 local getmetatable = getmetatable;
20 module "sessionmanager"
22 local open_sessions = 0;
24 function new_session(conn)
25 local session = { conn = conn, priority = 0, type = "c2s_unauthed" };
27 session.trace = newproxy(true);
28 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end;
30 open_sessions = open_sessions + 1;
32 session.send = function (t) w(tostring(t)); end
36 function destroy_session(session)
37 (session.log or log)("info", "Destroying session");
38 if session.host and session.username then
39 if session.resource then
40 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
42 if hosts[session.host] and hosts[session.host].sessions[session.username] then
43 if not next(hosts[session.host].sessions[session.username].sessions) then
44 log("debug", "All resources of %s are now offline", session.username);
45 hosts[session.host].sessions[session.username] = nil;
50 session.disconnect = nil;
51 for k in pairs(session) do
58 function send_to_session(session, data)
59 log("debug", "Sending: %s", tostring(data));
60 session.conn.write(tostring(data));
63 function make_authenticated(session, username)
64 session.username = username;
65 if session.type == "c2s_unauthed" then
71 -- returns true, nil on success
72 -- returns nil, err_type, err, err_message on failure
73 function bind_resource(session, resource)
74 if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
75 if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
76 -- We don't support binding multiple resources
78 resource = resource or uuid_generate();
79 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
81 if not hosts[session.host].sessions[session.username] then
82 hosts[session.host].sessions[session.username] = { sessions = {} };
84 if hosts[session.host].sessions[session.username].sessions[resource] then
86 return nil, "cancel", "conflict", "Resource already exists"; -- TODO kick old resource
90 session.resource = resource;
91 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
92 hosts[session.host].sessions[session.username].sessions[resource] = session;
94 session.roster = rm_load_roster(session.username, session.host);
99 function streamopened(session, attr)
100 local send = session.send;
101 session.host = attr.to or error("Client failed to specify destination hostname");
102 session.version = tonumber(attr.version) or 0;
103 session.streamid = m_random(1000000, 99999999);
104 print(session, session.host, "Client opened stream");
105 send("<?xml version='1.0'?>");
106 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));
109 modulemanager.fire_event("stream-features", session, features);
111 send("<stream:features>");
113 for _, feature in ipairs(features) do
114 send_to_session(session, tostring(feature));
117 send("</stream:features>");
118 log("info", "Stream opened successfully");
119 session.notopen = nil;
122 function send_to_available_resources(user, host, stanza)
124 local to = stanza.attr.to;
125 stanza.attr.to = nil;
126 local h = hosts[host];
127 if h and h.type == "local" then
128 local u = h.sessions[user];
130 for k, session in pairs(u.sessions) do
131 if session.presence then
132 session.send(stanza);