fc22d4338cd172c1ef190ec18714904a75c3911e
[prosody.git] / core / sessionmanager.lua
1
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");
7
8 local hosts = hosts;
9 local sessions = sessions;
10
11 local modulemanager = require "core.modulemanager";
12 local log = require "util.logger".init("sessionmanager");
13 local error = error;
14 local uuid_generate = require "util.uuid".uuid_generate;
15 local rm_load_roster = require "core.rostermanager".load_roster;
16
17 local newproxy = newproxy;
18 local getmetatable = getmetatable;
19
20 module "sessionmanager"
21
22 function new_session(conn)
23         local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" };
24         if true then
25                 session.trace = newproxy(true);
26                 getmetatable(session.trace).__gc = function () print("Session got collected") end;
27         end
28         local w = conn.write;
29         session.send = function (t) w(tostring(t)); end
30         return session;
31 end
32
33 function destroy_session(session)
34         if not (session and session.disconnect) then return; end 
35         log("debug", "Destroying session...");
36         session.disconnect();
37         if session.username then
38                 if session.resource then
39                         hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
40                 end
41                 local nomore = true;
42                 for res, ssn in pairs(hosts[session.host].sessions[session.username]) do
43                         nomore = false;
44                 end
45                 if nomore then
46                         hosts[session.host].sessions[session.username] = nil;
47                 end
48         end
49         session.conn = nil;
50         session.disconnect = nil;
51         for k in pairs(session) do
52                 if k ~= "trace" then
53                         session[k] = nil;
54                 end
55         end
56         collectgarbage("collect");
57         collectgarbage("collect");
58         collectgarbage("collect");
59         collectgarbage("collect");
60         collectgarbage("collect");
61 end
62
63 function send_to_session(session, data)
64         log("debug", "Sending: %s", tostring(data));
65         session.conn.write(tostring(data));
66 end
67
68 function make_authenticated(session, username)
69         session.username = username;
70         if session.type == "c2s_unauthed" then
71                 session.type = "c2s";
72         end
73         return true;
74 end
75
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
81         
82         if not hosts[session.host].sessions[session.username] then
83                 hosts[session.host].sessions[session.username] = { sessions = {} };
84         else
85                 if hosts[session.host].sessions[session.username].sessions[resource] then
86                         -- Resource conflict
87                         return false, "conflict"; -- TODO kick old resource
88                 end
89         end
90         
91         session.resource = resource;
92         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
93         hosts[session.host].sessions[session.username].sessions[resource] = session;
94         
95         session.roster = rm_load_roster(session.username, session.host);
96         
97         return true;
98 end
99
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));
108                                                 
109                                                 local features = {};
110                                                 modulemanager.fire_event("stream-features", session, features);
111                                                 
112                                                 send("<stream:features>");
113                                                 
114                                                 for _, feature in ipairs(features) do
115                                                         send_to_session(session, tostring(feature));
116                                                 end
117  
118                                                 send("</stream:features>");
119                                                 log("info", "Stream opened successfully");
120                                                 session.notopen = nil;
121 end
122
123 return _M;