30
|
1 |
|
|
2 |
local tostring = tostring; |
|
3 |
|
38
|
4 |
local print = print; |
|
5 |
|
|
6 |
local hosts = hosts; |
|
7 |
|
30
|
8 |
local log = require "util.logger".init("sessionmanager"); |
|
9 |
|
|
10 |
module "sessionmanager" |
|
11 |
|
|
12 |
function new_session(conn) |
|
13 |
local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" }; |
|
14 |
local w = conn.write; |
|
15 |
session.send = function (t) w(tostring(t)); end |
|
16 |
return session; |
|
17 |
end |
|
18 |
|
38
|
19 |
function destroy_session(session) |
|
20 |
end |
|
21 |
|
30
|
22 |
function send_to_session(session, data) |
38
|
23 |
log("debug", "Sending: %s", tostring(data)); |
30
|
24 |
session.conn.write(tostring(data)); |
|
25 |
end |
|
26 |
|
38
|
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 |
|
30
|
57 |
return _M; |