Merge 0.10->trunk
[prosody.git] / core / sessionmanager.lua
index 476de931f7621efa0af6055510f72a2b7d6d704c..6aa0a4f0f17d147a0b6d2930436ec3b5508084c8 100644 (file)
@@ -24,9 +24,9 @@ local uuid_generate = require "util.uuid".generate;
 local initialize_filters = require "util.filters".initialize;
 local gettime = require "socket".gettime;
 
-module "sessionmanager"
+local _ENV = nil;
 
-function new_session(conn)
+local function new_session(conn)
        local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() };
        local filter = initialize_filters(session);
        local w = conn.write;
@@ -37,9 +37,14 @@ function new_session(conn)
                if t then
                        t = filter("bytes/out", tostring(t));
                        if t then
-                               return w(conn, t);
+                               local ret, err = w(conn, t);
+                               if not ret then
+                                       session.log("debug", "Error writing to connection: %s", tostring(err));
+                                       return false, err;
+                               end
                        end
                end
+               return true;
        end
        session.ip = conn:ip();
        local conn_name = "c2s"..tostring(session):match("[a-f0-9]+$");
@@ -54,11 +59,11 @@ local resting_session = { -- Resting, not dead
                close = function (session)
                        session.log("debug", "Attempt to close already-closed session");
                end;
-               filter = function (type, data) return data; end;
+               filter = function (type, data) return data; end; --luacheck: ignore 212/type
        }; resting_session.__index = resting_session;
 
-function retire_session(session)
-       local log = session.log or log;
+local function retire_session(session)
+       local log = session.log or log; --luacheck: ignore 431/log
        for k in pairs(session) do
                if k ~= "log" and k ~= "id" then
                        session[k] = nil;
@@ -71,7 +76,7 @@ function retire_session(session)
        return setmetatable(session, resting_session);
 end
 
-function destroy_session(session, err)
+local function destroy_session(session, err)
        (session.log or log)("debug", "Destroying session for %s (%s@%s)%s", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)", err and (": "..err) or "");
        if session.destroyed then return; end
 
@@ -99,7 +104,7 @@ function destroy_session(session, err)
        retire_session(session);
 end
 
-function make_authenticated(session, username)
+local function make_authenticated(session, username)
        username = nodeprep(username);
        if not username or #username == 0 then return nil, "Invalid username"; end
        session.username = username;
@@ -112,9 +117,9 @@ end
 
 -- returns true, nil on success
 -- returns nil, err_type, err, err_message on failure
-function bind_resource(session, resource)
+local function bind_resource(session, resource)
        if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
-       if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
+       if session.resource then return nil, "cancel", "not-allowed", "Cannot bind multiple resources on a single connection"; end
        -- We don't support binding multiple resources
 
        local event_payload = { session = session, resource = resource };
@@ -193,12 +198,12 @@ function bind_resource(session, resource)
        return true;
 end
 
-function send_to_available_resources(user, host, stanza)
-       local jid = user.."@"..host;
+local function send_to_available_resources(username, host, stanza)
+       local jid = username.."@"..host;
        local count = 0;
        local user = bare_sessions[jid];
        if user then
-               for k, session in pairs(user.sessions) do
+               for _, session in pairs(user.sessions) do
                        if session.presence then
                                session.send(stanza);
                                count = count + 1;
@@ -208,12 +213,12 @@ function send_to_available_resources(user, host, stanza)
        return count;
 end
 
-function send_to_interested_resources(user, host, stanza)
-       local jid = user.."@"..host;
+local function send_to_interested_resources(username, host, stanza)
+       local jid = username.."@"..host;
        local count = 0;
        local user = bare_sessions[jid];
        if user then
-               for k, session in pairs(user.sessions) do
+               for _, session in pairs(user.sessions) do
                        if session.interested then
                                session.send(stanza);
                                count = count + 1;
@@ -223,4 +228,12 @@ function send_to_interested_resources(user, host, stanza)
        return count;
 end
 
-return _M;
+return {
+       new_session = new_session;
+       retire_session = retire_session;
+       destroy_session = destroy_session;
+       make_authenticated = make_authenticated;
+       bind_resource = bind_resource;
+       send_to_available_resources = send_to_available_resources;
+       send_to_interested_resources = send_to_interested_resources;
+};