2 local tonumber, tostring = tonumber, tostring;
3 local ipairs, print= ipairs, print;
5 local m_random = import("math", "random");
6 local format = import("string", "format");
10 local modulemanager = require "core.modulemanager";
11 local log = require "util.logger".init("sessionmanager");
13 local uuid_generate = require "util.uuid".uuid_generate;
14 module "sessionmanager"
16 function new_session(conn)
17 local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" };
19 session.send = function (t) w(tostring(t)); end
23 function destroy_session(session)
26 function send_to_session(session, data)
27 log("debug", "Sending: %s", tostring(data));
28 session.conn.write(tostring(data));
31 function make_authenticated(session, username)
32 session.username = username;
33 session.resource = resource;
34 if session.type == "c2s_unauthed" then
39 function bind_resource(session, resource)
40 if not session.username then return false, "auth"; end
41 if session.resource then return false, "constraint"; end -- We don't support binding multiple resources
42 resource = resource or uuid_generate();
43 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
45 if not hosts[session.host].sessions[session.username] then
46 hosts[session.host].sessions[session.username] = { sessions = {} };
48 if hosts[session.host].sessions[session.username].sessions[resource] then
50 return false, "conflict";
54 session.resource = resource;
55 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
56 hosts[session.host].sessions[session.username].sessions[resource] = session;
61 function streamopened(session, attr)
62 local send = session.send;
63 session.host = attr.to or error("Client failed to specify destination hostname");
64 session.version = tonumber(attr.version) or 0;
65 session.streamid = m_random(1000000, 99999999);
66 print(session, session.host, "Client opened stream");
67 send("<?xml version='1.0'?>");
68 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));
71 modulemanager.fire_event("stream-features", session, features);
73 send("<stream:features>");
75 for _, feature in ipairs(features) do
76 send_to_session(session, tostring(feature));
79 send("</stream:features>");
80 log("info", "Stream opened successfully");
81 session.notopen = nil;