core/sessionmanager.lua

changeset 38
3fdfd6e0cb4e
parent 30
bcf539295f2d
child 40
2c0147bbd81a
equal deleted inserted replaced
37:06eadafafefa 38:3fdfd6e0cb4e
1 1
2 local tostring = tostring; 2 local tostring = tostring;
3
4 local print = print;
5
6 local hosts = hosts;
3 7
4 local log = require "util.logger".init("sessionmanager"); 8 local log = require "util.logger".init("sessionmanager");
5 9
6 module "sessionmanager" 10 module "sessionmanager"
7 11
10 local w = conn.write; 14 local w = conn.write;
11 session.send = function (t) w(tostring(t)); end 15 session.send = function (t) w(tostring(t)); end
12 return session; 16 return session;
13 end 17 end
14 18
19 function destroy_session(session)
20 end
21
15 function send_to_session(session, data) 22 function send_to_session(session, data)
16 log("debug", "Sending...", tostring(data)); 23 log("debug", "Sending: %s", tostring(data));
17 session.conn.write(tostring(data)); 24 session.conn.write(tostring(data));
18 end 25 end
19 26
27 function make_authenticated(session, username)
28 session.username = username;
29 session.resource = resource;
30 if session.type == "c2s_unauthed" then
31 session.type = "c2s";
32 end
33 end
34
35 function bind_resource(session, resource)
36 if not session.username then return false, "auth"; end
37 if session.resource then return false, "constraint"; end -- We don't support binding multiple resources
38 resource = resource or math.random(100000, 99999999); -- FIXME: Clearly we have issues :)
39 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
40
41 if not hosts[session.host].sessions[session.username] then
42 hosts[session.host].sessions[session.username] = { sessions = {} };
43 else
44 if hosts[session.host].sessions[session.username].sessions[resource] then
45 -- Resource conflict
46 return false, "conflict";
47 end
48 end
49
50 session.resource = resource;
51 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
52 hosts[session.host].sessions[session.username].sessions[resource] = session;
53
54 return true;
55 end
56
20 return _M; 57 return _M;

mercurial