b257e532a3fe2f8ba9947f05410196b8f496378f
[prosody.git] / core / sessionmanager.lua
1
2 local tonumber, tostring = tonumber, tostring;
3 local ipairs, print= ipairs, print;
4
5 local m_random = import("math", "random");
6 local format = import("string", "format");
7
8 local hosts = hosts;
9
10 local modulemanager = require "core.modulemanager";
11 local log = require "util.logger".init("sessionmanager");
12 local error = error;
13 local uuid_generate = require "util.uuid".uuid_generate;
14 module "sessionmanager"
15
16 function new_session(conn)
17         local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" };
18         local w = conn.write;
19         session.send = function (t) w(tostring(t)); end
20         return session;
21 end
22
23 function destroy_session(session)
24 end
25
26 function send_to_session(session, data)
27         log("debug", "Sending: %s", tostring(data));
28         session.conn.write(tostring(data));
29 end
30
31 function make_authenticated(session, username)
32         session.username = username;
33         session.resource = resource;
34         if session.type == "c2s_unauthed" then
35                 session.type = "c2s";
36         end
37 end
38
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
44         
45         if not hosts[session.host].sessions[session.username] then
46                 hosts[session.host].sessions[session.username] = { sessions = {} };
47         else
48                 if hosts[session.host].sessions[session.username].sessions[resource] then
49                         -- Resource conflict
50                         return false, "conflict";
51                 end
52         end
53         
54         session.resource = resource;
55         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
56         hosts[session.host].sessions[session.username].sessions[resource] = session;
57         
58         return true;
59 end
60
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));
69                                                 
70                                                 local features = {};
71                                                 modulemanager.fire_event("stream-features", session, features);
72                                                 
73                                                 send("<stream:features>");
74                                                 
75                                                 for _, feature in ipairs(features) do
76                                                         send_to_session(session, tostring(feature));
77                                                 end
78  
79                                                 send("</stream:features>");
80                                                 log("info", "Stream opened successfully");
81                                                 session.notopen = nil;
82 end
83
84 return _M;