net.httpserver: Removed an unused function.
[prosody.git] / core / sessionmanager.lua
index a2c6ed9557931d162a71e9cd0d6a3e4138ec5274..d36591bfb34c2218c01c84388796409dbda57a88 100644 (file)
@@ -1,6 +1,6 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -25,8 +25,10 @@ local rm_load_roster = require "core.rostermanager".load_roster;
 local config_get = require "core.configmanager".get;
 local nameprep = require "util.encodings".stringprep.nameprep;
 local resourceprep = require "util.encodings".stringprep.resourceprep;
+local nodeprep = require "util.encodings".stringprep.nodeprep;
 
-local fire_event = require "core.eventmanager".fire_event;
+local initialize_filters = require "util.filters".initialize;
+local fire_event = prosody.events.fire_event;
 local add_task = require "util.timer".add_task;
 local gettime = require "socket".gettime;
 
@@ -49,8 +51,20 @@ function new_session(conn)
        end
        open_sessions = open_sessions + 1;
        log("debug", "open sessions now: ".. open_sessions);
+       
+       local filter = initialize_filters(session);
        local w = conn.write;
-       session.send = function (t) w(conn, tostring(t)); end
+       session.send = function (t)
+               if t.name then
+                       t = filter("stanzas/out", t);
+               end
+               if t then
+                       t = filter("bytes/out", tostring(t));
+                       if t then
+                               return w(conn, t);
+                       end
+               end
+       end
        session.ip = conn:ip();
        local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
        session.log = logger.init(conn_name);
@@ -68,6 +82,11 @@ end
 
 local resting_session = { -- Resting, not dead
                destroyed = true;
+               type = "c2s_destroyed";
+               close = function (session)
+                       session.log("debug", "Attempt to close already-closed session");
+               end;
+               filter = function (type, data) return data; end;
        }; resting_session.__index = resting_session;
 
 function retire_session(session)
@@ -85,6 +104,7 @@ end
 
 function destroy_session(session, err)
        (session.log or log)("info", "Destroying session for %s (%s@%s)", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)");
+       if session.destroyed then return; end
        
        -- Remove session/resource from user's session list
        if session.full_jid then
@@ -104,6 +124,8 @@ function destroy_session(session, err)
 end
 
 function make_authenticated(session, username)
+       username = nodeprep(username);
+       if not username or #username == 0 then return nil, "Invalid username"; end
        session.username = username;
        if session.type == "c2s_unauthed" then
                session.type = "c2s";
@@ -131,7 +153,7 @@ function bind_resource(session, resource)
                local sessions = hosts[session.host].sessions[session.username].sessions;
                local limit = config_get(session.host, "core", "max_resources") or 10;
                if #sessions >= limit then
-                       return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed";
+                       return nil, "cancel", "resource-constraint", "Resource limit reached; only "..limit.." resources allowed";
                end
                if sessions[resource] then
                        -- Resource conflict
@@ -169,7 +191,19 @@ function bind_resource(session, resource)
        hosts[session.host].sessions[session.username].sessions[resource] = session;
        full_sessions[session.full_jid] = session;
        
-       session.roster = rm_load_roster(session.username, session.host);
+       local err;
+       session.roster, err = rm_load_roster(session.username, session.host);
+       if err then
+               full_sessions[session.full_jid] = nil;
+               hosts[session.host].sessions[session.username].sessions[resource] = nil;
+               session.full_jid = nil;
+               session.resource = nil;
+               if next(bare_sessions[session.username..'@'..session.host].sessions) == nil then
+                       bare_sessions[session.username..'@'..session.host] = nil;
+                       hosts[session.host].sessions[session.username] = nil;
+               end
+               return nil, "cancel", "internal-server-error", "Error loading roster";
+       end
        
        hosts[session.host].events.fire_event("resource-bind", {session=session});
        
@@ -178,7 +212,12 @@ end
 
 function streamopened(session, attr)
        local send = session.send;
-       session.host = attr.to or error("Client failed to specify destination hostname");
+       session.host = attr.to;
+       if not session.host then
+               session:close{ condition = "improper-addressing",
+                       text = "A 'to' attribute is required on stream headers" };
+               return;
+       end
        session.host = nameprep(session.host);
        session.version = tonumber(attr.version) or 0;
        session.streamid = uuid_generate();
@@ -211,8 +250,8 @@ function streamopened(session, attr)
 end
 
 function streamclosed(session)
-       session.send("</stream:stream>");
-       session.notopen = true;
+       session.log("debug", "Received </stream:stream>");
+       session:close();
 end
 
 function send_to_available_resources(user, host, stanza)