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 function bind_resource(session, resource)
72 if not session.username then return false, "auth"; end
73 if session.resource then return false, "constraint"; end -- We don't support binding multiple resources
74 resource = resource or uuid_generate();
75 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
77 if not hosts[session.host].sessions[session.username] then
78 hosts[session.host].sessions[session.username] = { sessions = {} };
80 if hosts[session.host].sessions[session.username].sessions[resource] then
82 return false, "conflict"; -- TODO kick old resource
86 session.resource = resource;
87 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
88 hosts[session.host].sessions[session.username].sessions[resource] = session;
90 session.roster = rm_load_roster(session.username, session.host);
95 function streamopened(session, attr)
96 local send = session.send;
97 session.host = attr.to or error("Client failed to specify destination hostname");
98 session.version = tonumber(attr.version) or 0;
99 session.streamid = m_random(1000000, 99999999);
100 print(session, session.host, "Client opened stream");
101 send("<?xml version='1.0'?>");
102 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));
105 modulemanager.fire_event("stream-features", session, features);
107 send("<stream:features>");
109 for _, feature in ipairs(features) do
110 send_to_session(session, tostring(feature));
113 send("</stream:features>");
114 log("info", "Stream opened successfully");
115 session.notopen = nil;
118 function send_to_available_resources(user, host, stanza)
120 local to = stanza.attr.to;
121 stanza.attr.to = nil;
122 local h = hosts[host];
123 if h and h.type == "local" then
124 local u = h.sessions[user];
126 for k, session in pairs(u.sessions) do
127 if session.presence then
128 session.send(stanza);