Fix mod_vcard to use session.send for sending stanzas
[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 -- returns true, nil on success
72 -- returns nil, err_type, err, err_message on failure
73 function bind_resource(session, resource)
74         if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
75         if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
76         -- We don't support binding multiple resources
77
78         resource = resource or uuid_generate();
79         --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
80         
81         if not hosts[session.host].sessions[session.username] then
82                 hosts[session.host].sessions[session.username] = { sessions = {} };
83         else
84                 if hosts[session.host].sessions[session.username].sessions[resource] then
85                         -- Resource conflict
86                         return nil, "cancel", "conflict", "Resource already exists"; -- TODO kick old resource
87                 end
88         end
89         
90         session.resource = resource;
91         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
92         hosts[session.host].sessions[session.username].sessions[resource] = session;
93         
94         session.roster = rm_load_roster(session.username, session.host);
95         
96         return true;
97 end
98
99 function streamopened(session, attr)
100                                                 local send = session.send;
101                                                 session.host = attr.to or error("Client failed to specify destination hostname");
102                                                 session.version = tonumber(attr.version) or 0;
103                                                 session.streamid = m_random(1000000, 99999999);
104                                                 print(session, session.host, "Client opened stream");
105                                                 send("<?xml version='1.0'?>");
106                                                 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));
107                                                 
108                                                 local features = {};
109                                                 modulemanager.fire_event("stream-features", session, features);
110                                                 
111                                                 send("<stream:features>");
112                                                 
113                                                 for _, feature in ipairs(features) do
114                                                         send_to_session(session, tostring(feature));
115                                                 end
116  
117                                                 send("</stream:features>");
118                                                 log("info", "Stream opened successfully");
119                                                 session.notopen = nil;
120 end
121
122 function send_to_available_resources(user, host, stanza)
123         local count = 0;
124         local to = stanza.attr.to;
125         stanza.attr.to = nil;
126         local h = hosts[host];
127         if h and h.type == "local" then
128                 local u = h.sessions[user];
129                 if u then
130                         for k, session in pairs(u.sessions) do
131                                 if session.presence then
132                                         session.send(stanza);
133                                         count = count + 1;
134                                 end
135                         end
136                 end
137         end
138         stanza.attr.to = to;
139         return count;
140 end
141
142 return _M;