2 local tonumber, tostring = tonumber, tostring;
3 local ipairs, pairs, print= ipairs, pairs, print;
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".uuid_generate;
15 local rm_load_roster = require "core.rostermanager".load_roster;
17 local newproxy = newproxy;
18 local getmetatable = getmetatable;
20 module "sessionmanager"
22 function new_session(conn)
23 local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" };
25 session.trace = newproxy(true);
26 getmetatable(session.trace).__gc = function () print("Session got collected") end;
29 session.send = function (t) w(tostring(t)); end
33 function destroy_session(session)
34 if not (session and session.disconnect) then return; end
35 log("debug", "Destroying session...");
37 if session.username then
38 if session.resource then
39 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
42 for res, ssn in pairs(hosts[session.host].sessions[session.username]) do
46 hosts[session.host].sessions[session.username] = nil;
50 session.disconnect = nil;
51 for k in pairs(session) do
56 collectgarbage("collect");
57 collectgarbage("collect");
58 collectgarbage("collect");
59 collectgarbage("collect");
60 collectgarbage("collect");
63 function send_to_session(session, data)
64 log("debug", "Sending: %s", tostring(data));
65 session.conn.write(tostring(data));
68 function make_authenticated(session, username)
69 session.username = username;
70 if session.type == "c2s_unauthed" then
76 function bind_resource(session, resource)
77 if not session.username then return false, "auth"; end
78 if session.resource then return false, "constraint"; end -- We don't support binding multiple resources
79 resource = resource or uuid_generate();
80 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
82 if not hosts[session.host].sessions[session.username] then
83 hosts[session.host].sessions[session.username] = { sessions = {} };
85 if hosts[session.host].sessions[session.username].sessions[resource] then
87 return false, "conflict"; -- TODO kick old resource
91 session.resource = resource;
92 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
93 hosts[session.host].sessions[session.username].sessions[resource] = session;
95 session.roster = rm_load_roster(session.username, session.host);
100 function streamopened(session, attr)
101 local send = session.send;
102 session.host = attr.to or error("Client failed to specify destination hostname");
103 session.version = tonumber(attr.version) or 0;
104 session.streamid = m_random(1000000, 99999999);
105 print(session, session.host, "Client opened stream");
106 send("<?xml version='1.0'?>");
107 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));
110 modulemanager.fire_event("stream-features", session, features);
112 send("<stream:features>");
114 for _, feature in ipairs(features) do
115 send_to_session(session, tostring(feature));
118 send("</stream:features>");
119 log("info", "Stream opened successfully");
120 session.notopen = nil;