8be7bdec6a1f885d943903c096f5d30a5b71c7a2
[prosody.git] / core / sessionmanager.lua
1
2 local tonumber, tostring = tonumber, tostring;
3 local ipairs, pairs, print, next= ipairs, pairs, print, next;
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".generate;
15 local rm_load_roster = require "core.rostermanager".load_roster;
16
17 local st = require "util.stanza";
18
19 local newproxy = newproxy;
20 local getmetatable = getmetatable;
21
22 module "sessionmanager"
23
24 local open_sessions = 0;
25
26 function new_session(conn)
27         local session = { conn = conn,  priority = 0, type = "c2s_unauthed" };
28         if true then
29                 session.trace = newproxy(true);
30                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end;
31         end
32         open_sessions = open_sessions + 1;
33         log("info", "open sessions now: ".. open_sessions);
34         local w = conn.write;
35         session.send = function (t) w(tostring(t)); end
36         return session;
37 end
38
39 function destroy_session(session, err)
40         (session.log or log)("info", "Destroying session");
41         
42         -- Send unavailable presence
43         if session.presence then
44                 local pres = st.presence{ type = "unavailable" };
45                 if (not err) or err == "closed" then err = "connection closed"; end
46                 pres:tag("status"):text("Disconnected: "..err);
47                 session.stanza_dispatch(pres);
48         end
49         
50         -- Remove session/resource from user's session list
51         if session.host and session.username then
52                 if session.resource then
53                         hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
54                 end
55                 if hosts[session.host] and hosts[session.host].sessions[session.username] then
56                         if not next(hosts[session.host].sessions[session.username].sessions) then
57                                 log("debug", "All resources of %s are now offline", session.username);
58                                 hosts[session.host].sessions[session.username] = nil;
59                         end
60                 end
61         end
62         
63         for k in pairs(session) do
64                 if k ~= "trace" then
65                         session[k] = nil;
66                 end
67         end
68 end
69
70 function make_authenticated(session, username)
71         session.username = username;
72         if session.type == "c2s_unauthed" then
73                 session.type = "c2s";
74         end
75         return true;
76 end
77
78 -- returns true, nil on success
79 -- returns nil, err_type, err, err_message on failure
80 function bind_resource(session, resource)
81         if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
82         if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
83         -- We don't support binding multiple resources
84
85         resource = resource or uuid_generate();
86         --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
87         
88         if not hosts[session.host].sessions[session.username] then
89                 hosts[session.host].sessions[session.username] = { sessions = {} };
90         else
91                 if hosts[session.host].sessions[session.username].sessions[resource] then
92                         -- Resource conflict
93                         return nil, "cancel", "conflict", "Resource already exists"; -- TODO kick old resource
94                 end
95         end
96         
97         session.resource = resource;
98         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
99         hosts[session.host].sessions[session.username].sessions[resource] = session;
100         
101         session.roster = rm_load_roster(session.username, session.host);
102         
103         return true;
104 end
105
106 function streamopened(session, attr)
107                                                 local send = session.send;
108                                                 session.host = attr.to or error("Client failed to specify destination hostname");
109                                                 session.version = tonumber(attr.version) or 0;
110                                                 session.streamid = m_random(1000000, 99999999);
111                                                 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
112                                                 
113                                                 
114                                                 send("<?xml version='1.0'?>");
115                                                 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));
116                                                 
117                                                 if not hosts[session.host] then
118                                                         -- We don't serve this host...
119                                                         session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
120                                                         return;
121                                                 end
122                                                 
123                                                 
124                                                 local features = {};
125                                                 modulemanager.fire_event("stream-features", session, features);
126                                                 
127                                                 -- FIXME: Need to send() this all at once
128                                                 send("<stream:features>");
129                                                 
130                                                 for _, feature in ipairs(features) do
131                                                         send(tostring(feature));
132                                                 end
133  
134                                                 send("</stream:features>");
135                                                 log("info", "Stream opened successfully");
136                                                 session.notopen = nil;
137 end
138
139 function send_to_available_resources(user, host, stanza)
140         local count = 0;
141         local to = stanza.attr.to;
142         stanza.attr.to = nil;
143         local h = hosts[host];
144         if h and h.type == "local" then
145                 local u = h.sessions[user];
146                 if u then
147                         for k, session in pairs(u.sessions) do
148                                 if session.presence then
149                                         session.send(stanza);
150                                         count = count + 1;
151                                 end
152                         end
153                 end
154         end
155         stanza.attr.to = to;
156         return count;
157 end
158
159 return _M;