comparison core/sessionmanager.lua @ 403:da92afa267cf

Merging with main branch.
author Tobias Markmann <tm@ayena.de>
date Sun, 23 Nov 2008 20:44:48 +0100
parents 17bcecb06420
children b1d86f89fa27
comparison
equal deleted inserted replaced
402:50f1c09541cd 403:da92afa267cf
12 local log = require "util.logger".init("sessionmanager"); 12 local log = require "util.logger".init("sessionmanager");
13 local error = error; 13 local error = error;
14 local uuid_generate = require "util.uuid".generate; 14 local uuid_generate = require "util.uuid".generate;
15 local rm_load_roster = require "core.rostermanager".load_roster; 15 local rm_load_roster = require "core.rostermanager".load_roster;
16 16
17 local st = require "util.stanza";
18
17 local newproxy = newproxy; 19 local newproxy = newproxy;
18 local getmetatable = getmetatable; 20 local getmetatable = getmetatable;
19 21
20 module "sessionmanager" 22 module "sessionmanager"
21 23
26 if true then 28 if true then
27 session.trace = newproxy(true); 29 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; 30 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end;
29 end 31 end
30 open_sessions = open_sessions + 1; 32 open_sessions = open_sessions + 1;
33 log("info", "open sessions now: ".. open_sessions);
31 local w = conn.write; 34 local w = conn.write;
32 session.send = function (t) w(tostring(t)); end 35 session.send = function (t) w(tostring(t)); end
33 return session; 36 return session;
34 end 37 end
35 38
36 function destroy_session(session) 39 function destroy_session(session, err)
37 (session.log or log)("info", "Destroying session"); 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
38 if session.host and session.username then 51 if session.host and session.username then
39 if session.resource then 52 if session.resource then
40 hosts[session.host].sessions[session.username].sessions[session.resource] = nil; 53 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
41 end 54 end
42 if hosts[session.host] and hosts[session.host].sessions[session.username] then 55 if hosts[session.host] and hosts[session.host].sessions[session.username] then
44 log("debug", "All resources of %s are now offline", session.username); 57 log("debug", "All resources of %s are now offline", session.username);
45 hosts[session.host].sessions[session.username] = nil; 58 hosts[session.host].sessions[session.username] = nil;
46 end 59 end
47 end 60 end
48 end 61 end
49 session.conn = nil; 62
50 session.disconnect = nil;
51 for k in pairs(session) do 63 for k in pairs(session) do
52 if k ~= "trace" then 64 if k ~= "trace" then
53 session[k] = nil; 65 session[k] = nil;
54 end 66 end
55 end 67 end
94 function streamopened(session, attr) 106 function streamopened(session, attr)
95 local send = session.send; 107 local send = session.send;
96 session.host = attr.to or error("Client failed to specify destination hostname"); 108 session.host = attr.to or error("Client failed to specify destination hostname");
97 session.version = tonumber(attr.version) or 0; 109 session.version = tonumber(attr.version) or 0;
98 session.streamid = m_random(1000000, 99999999); 110 session.streamid = m_random(1000000, 99999999);
99 print(session, session.host, "Client opened stream"); 111 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
100 send("<?xml version='1.0'?>"); 112
113
114 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)); 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));
102 116
103 local features = {}; 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 = st.stanza("stream:features");
104 modulemanager.fire_event("stream-features", session, features); 125 modulemanager.fire_event("stream-features", session, features);
105 126
106 send("<stream:features>"); 127 send(features);
107 128
108 for _, feature in ipairs(features) do 129 (session.log or log)("info", "Sent reply <stream:stream> to client");
109 send(tostring(feature));
110 end
111
112 send("</stream:features>");
113 log("info", "Stream opened successfully");
114 session.notopen = nil; 130 session.notopen = nil;
115 end 131 end
116 132
117 function send_to_available_resources(user, host, stanza) 133 function send_to_available_resources(user, host, stanza)
118 local count = 0; 134 local count = 0;