Merge presence/subscription support from waqas
[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 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 or log)("info", "Destroying session");
38         if session.host and session.username then
39                 if session.resource then
40                         hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
41                 end
42                 if hosts[session.host] and hosts[session.host].sessions[session.username] then
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         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 end
57
58 function send_to_session(session, data)
59         log("debug", "Sending: %s", tostring(data));
60         session.conn.write(tostring(data));
61 end
62
63 function make_authenticated(session, username)
64         session.username = username;
65         if session.type == "c2s_unauthed" then
66                 session.type = "c2s";
67         end
68         return true;
69 end
70
71 function bind_resource(session, resource)
72         if not session.username then return false, "auth"; end
73         if session.resource then return false, "constraint"; end -- We don't support binding multiple resources
74         resource = resource or uuid_generate();
75         --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
76         
77         if not hosts[session.host].sessions[session.username] then
78                 hosts[session.host].sessions[session.username] = { sessions = {} };
79         else
80                 if hosts[session.host].sessions[session.username].sessions[resource] then
81                         -- Resource conflict
82                         return false, "conflict"; -- TODO kick old resource
83                 end
84         end
85         
86         session.resource = resource;
87         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
88         hosts[session.host].sessions[session.username].sessions[resource] = session;
89         
90         session.roster = rm_load_roster(session.username, session.host);
91         
92         return true;
93 end
94
95 function streamopened(session, attr)
96                                                 local send = session.send;
97                                                 session.host = attr.to or error("Client failed to specify destination hostname");
98                                                 session.version = tonumber(attr.version) or 0;
99                                                 session.streamid = m_random(1000000, 99999999);
100                                                 print(session, session.host, "Client opened stream");
101                                                 send("<?xml version='1.0'?>");
102                                                 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));
103                                                 
104                                                 local features = {};
105                                                 modulemanager.fire_event("stream-features", session, features);
106                                                 
107                                                 send("<stream:features>");
108                                                 
109                                                 for _, feature in ipairs(features) do
110                                                         send_to_session(session, tostring(feature));
111                                                 end
112  
113                                                 send("</stream:features>");
114                                                 log("info", "Stream opened successfully");
115                                                 session.notopen = nil;
116 end
117
118 function send_to_available_resources(user, host, stanza)
119         local count = 0;
120         local to = stanza.attr.to;
121         stanza.attr.to = nil;
122         local h = hosts[host];
123         if h and h.type == "local" then
124                 local u = h.sessions[user];
125                 if u then
126                         for k, session in pairs(u.sessions) do
127                                 if session.presence then
128                                         session.send(stanza);
129                                         count = count + 1;
130                                 end
131                         end
132                 end
133         end
134         stanza.attr.to = to;
135         return count;
136 end
137
138 return _M;