Final fix for marking user offline when all resources are gone :)
[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".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 local open_sessions = 0;
23
24 function new_session(conn)
25         local session = { conn = conn,  priority = 0, type = "c2s_unauthed" };
26         if true then
27                 session.trace = newproxy(true);
28                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end;
29         end
30         open_sessions = open_sessions + 1;
31         local w = conn.write;
32         session.send = function (t) w(tostring(t)); end
33         return session;
34 end
35
36 function destroy_session(session)
37         session.log("info", "Destroying session");
38         if session.username then
39                 if session.resource then
40                         hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
41                 end
42                 
43                 if not next(hosts[session.host].sessions[session.username].sessions) then
44                         log("debug", "All resources of %s are now offline", session.username);
45                         hosts[session.host].sessions[session.username] = nil;
46                 end
47         end
48         session.conn = nil;
49         session.disconnect = nil;
50         for k in pairs(session) do
51                 if k ~= "trace" then
52                         session[k] = nil;
53                 end
54         end
55 end
56
57 function send_to_session(session, data)
58         log("debug", "Sending: %s", tostring(data));
59         session.conn.write(tostring(data));
60 end
61
62 function make_authenticated(session, username)
63         session.username = username;
64         if session.type == "c2s_unauthed" then
65                 session.type = "c2s";
66         end
67         return true;
68 end
69
70 function bind_resource(session, resource)
71         if not session.username then return false, "auth"; end
72         if session.resource then return false, "constraint"; end -- We don't support binding multiple resources
73         resource = resource or uuid_generate();
74         --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
75         
76         if not hosts[session.host].sessions[session.username] then
77                 hosts[session.host].sessions[session.username] = { sessions = {} };
78         else
79                 if hosts[session.host].sessions[session.username].sessions[resource] then
80                         -- Resource conflict
81                         return false, "conflict"; -- TODO kick old resource
82                 end
83         end
84         
85         session.resource = resource;
86         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
87         hosts[session.host].sessions[session.username].sessions[resource] = session;
88         
89         session.roster = rm_load_roster(session.username, session.host);
90         
91         return true;
92 end
93
94 function streamopened(session, attr)
95                                                 local send = session.send;
96                                                 session.host = attr.to or error("Client failed to specify destination hostname");
97                                                 session.version = tonumber(attr.version) or 0;
98                                                 session.streamid = m_random(1000000, 99999999);
99                                                 print(session, session.host, "Client opened stream");
100                                                 send("<?xml version='1.0'?>");
101                                                 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));
102                                                 
103                                                 local features = {};
104                                                 modulemanager.fire_event("stream-features", session, features);
105                                                 
106                                                 send("<stream:features>");
107                                                 
108                                                 for _, feature in ipairs(features) do
109                                                         send_to_session(session, tostring(feature));
110                                                 end
111  
112                                                 send("</stream:features>");
113                                                 log("info", "Stream opened successfully");
114                                                 session.notopen = nil;
115 end
116
117 return _M;