Fix for never checking if the first module for a host is already loaded (affects...
[prosody.git] / core / sessionmanager.lua
index d17a035725f70e3192286221175957fe86e40dae..571ce1fe43e6826f650e41a52b061f02c89b1291 100644 (file)
@@ -1,20 +1,9 @@
--- Prosody IM v0.1
--- Copyright (C) 2008 Matthew Wild
--- Copyright (C) 2008 Waqas Hussain
+-- Prosody IM v0.3
+-- Copyright (C) 2008-2009 Matthew Wild
+-- Copyright (C) 2008-2009 Waqas Hussain
 -- 
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public License
--- as published by the Free Software Foundation; either version 2
--- of the License, or (at your option) any later version.
--- 
--- This program is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--- GNU General Public License for more details.
--- 
--- You should have received a copy of the GNU General Public License
--- along with this program; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
 --
 
 
@@ -35,6 +24,10 @@ local uuid_generate = require "util.uuid".generate;
 local rm_load_roster = require "core.rostermanager".load_roster;
 local config_get = require "core.configmanager".get;
 
+local fire_event = require "core.eventmanager".fire_event;
+
+local gettime = require "socket".gettime;
+
 local st = require "util.stanza";
 
 local newproxy = newproxy;
@@ -45,15 +38,16 @@ module "sessionmanager"
 local open_sessions = 0;
 
 function new_session(conn)
-       local session = { conn = conn,  priority = 0, type = "c2s_unauthed" };
+       local session = { conn = conn,  priority = 0, type = "c2s_unauthed", conntime = gettime() };
        if true then
                session.trace = newproxy(true);
-               getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end;
+               getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
        end
        open_sessions = open_sessions + 1;
        log("info", "open sessions now: ".. open_sessions);
        local w = conn.write;
        session.send = function (t) w(tostring(t)); end
+       session.ip = conn.ip();
        return session;
 end
 
@@ -65,19 +59,25 @@ function destroy_session(session, err)
                local pres = st.presence{ type = "unavailable" };
                if (not err) or err == "closed" then err = "connection closed"; end
                pres:tag("status"):text("Disconnected: "..err);
-               session.stanza_dispatch(pres);
+               session:dispatch_stanza(pres);
        end
        
        -- Remove session/resource from user's session list
        if session.host and session.username then
-               if session.resource then
-                       hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
-               end
+               -- FIXME: How can the below ever be nil? (but they sometimes are...)
                if hosts[session.host] and hosts[session.host].sessions[session.username] then
+                       if session.resource then
+                               hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
+                       end
+                               
                        if not next(hosts[session.host].sessions[session.username].sessions) then
                                log("debug", "All resources of %s are now offline", session.username);
                                hosts[session.host].sessions[session.username] = nil;
                        end
+               else
+                       log("error", "host or session table didn't exist, please report this! Host: %s [%s] Sessions: %s [%s]", 
+                                       tostring(hosts[session.host]), tostring(session.host),
+                                       tostring(hosts[session.host].sessions[session.username] ), tostring(session.username));
                end
        end
        
@@ -169,7 +169,7 @@ function streamopened(session, attr)
                                                
                                                
                                                local features = st.stanza("stream:features");
-                                               modulemanager.fire_event("stream-features", session, features);
+                                               fire_event("stream-features", session, features);
                                                
                                                send(features);
                                                
@@ -197,4 +197,4 @@ function send_to_available_resources(user, host, stanza)
        return count;
 end
 
-return _M;
\ No newline at end of file
+return _M;