# HG changeset patch # User Matthew Wild # Date 1224693381 -3600 # Node ID ba08b8a4eeef0845c8ee951732bc870eda317f4b # Parent 3a2d327c485666e3faa8da08a009db41783f4bcc Abstract connections with "connection listeners" - Added connlistener for xmppclient - SASL/TLS now use a new session:reset_stream() method - main.lua on its way to being a bit neater diff -r 3a2d327c4856 -r ba08b8a4eeef core/sessionmanager.lua --- a/core/sessionmanager.lua Wed Oct 22 17:27:40 2008 +0100 +++ b/core/sessionmanager.lua Wed Oct 22 17:36:21 2008 +0100 @@ -20,7 +20,7 @@ module "sessionmanager" function new_session(conn) - local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" }; + local session = { conn = conn, priority = 0, type = "c2s_unauthed" }; if true then session.trace = newproxy(true); getmetatable(session.trace).__gc = function () print("Session got collected") end; diff -r 3a2d327c4856 -r ba08b8a4eeef core/xmlhandlers.lua --- a/core/xmlhandlers.lua Wed Oct 22 17:27:40 2008 +0100 +++ b/core/xmlhandlers.lua Wed Oct 22 17:36:21 2008 +0100 @@ -1,5 +1,4 @@ -local sessionmanager_streamopened = require "core.sessionmanager".streamopened; require "util.stanza" local st = stanza; @@ -16,7 +15,7 @@ module "xmlhandlers" -function init_xmlhandlers(session) +function init_xmlhandlers(session, streamopened) local ns_stack = { "" }; local curr_ns = ""; local curr_tag; @@ -38,7 +37,7 @@ if not stanza then if session.notopen then if name == "stream" then - sessionmanager_streamopened(session, attr); + streamopened(session, attr); return; end error("Client failed to open stream successfully"); diff -r 3a2d327c4856 -r ba08b8a4eeef main.lua --- a/main.lua Wed Oct 22 17:27:40 2008 +0100 +++ b/main.lua Wed Oct 22 17:36:21 2008 +0100 @@ -1,9 +1,9 @@ require "luarocks.require" local server = require "net.server" +require "lxp" require "socket" require "ssl" -require "lxp" function log(type, area, message) print(type, area, message); @@ -11,8 +11,11 @@ dofile "lxmppd.cfg" +-- Maps connections to sessions -- sessions = {}; +-- Load and initialise core modules -- + require "util.import" require "core.stanza_dispatch" require "core.xmlhandlers" @@ -22,10 +25,12 @@ require "core.usermanager" require "core.sessionmanager" require "core.stanza_router" -require "net.connhandlers" + +local start = require "net.connlisteners".start; require "util.stanza" require "util.jid" +------------------------------------------------------------------------ -- Locals for faster access -- local t_insert = table.insert; @@ -37,62 +42,9 @@ local st = stanza; ------------------------------ - - local hosts, sessions = hosts, sessions; -function connect_host(host) - hosts[host] = { type = "remote", sendbuffer = {} }; -end - -function handler(conn, data, err) - local session = sessions[conn]; - - if not session then - sessions[conn] = sm_new_session(conn); - session = sessions[conn]; - - -- Logging functions -- - - local mainlog, log = log; - do - local conn_name = tostring(conn):match("%w+$"); - log = function (type, area, message) mainlog(type, conn_name, message); end - --log = function () end - end - local print = function (...) log("info", "core", t_concatall({...}, "\t")); end - session.log = log; - - print("Client connected"); - - session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end - - session.connhandler = connhandlers.new("xmpp-client", session); - - function session.disconnect(err) - if session.last_presence and session.last_presence.attr.type ~= "unavailable" then - local pres = st.presence{ type = "unavailable" }; - if err == "closed" then err = "connection closed"; end - pres:tag("status"):text("Disconnected: "..err); - session.stanza_dispatch(pres); - end - session = nil; - print("Disconnected: "..tostring(err)); - collectgarbage("collect"); - end - end - if data then - session.connhandler:data(data); - end - - --log("info", "core", "Client disconnected, connection closed"); -end - -function disconnect(conn, err) - sm_destroy_session(sessions[conn]); - sessions[conn] = nil; -end - +-- Initialise modules modulemanager.loadall(); setmetatable(_G, { __index = function (t, k) print("WARNING: ATTEMPT TO READ A NIL GLOBAL!!!", k); error("Attempt to read a non-existent global. Naughty boy.", 2); end, __newindex = function (t, k, v) print("ATTEMPT TO SET A GLOBAL!!!!", tostring(k).." = "..tostring(v)); error("Attempt to set a global. Naughty boy.", 2); end }) --]][][[]][]; @@ -101,7 +53,6 @@ local protected_handler = function (conn, data, err) local success, ret = pcall(handler, conn, data, err); if not success then print("ERROR on "..tostring(conn)..": "..ret); conn:close(); end end; local protected_disconnect = function (conn, err) local success, ret = pcall(disconnect, conn, err); if not success then print("ERROR on "..tostring(conn).." disconnect: "..ret); conn:close(); end end; -server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5222, "*", 1, ssl_ctx ) -- server.add will send a status message ---server.add( { listener = protected_handler, disconnect = protected_disconnect }, 5223, "*", 1, ssl_ctx ) -- server.add will send a status message +start("xmppclient", { ssl = ssl_ctx }) server.loop(); diff -r 3a2d327c4856 -r ba08b8a4eeef net/connhandlers.lua --- a/net/connhandlers.lua Wed Oct 22 17:27:40 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ - -local lxp = require "lxp" -local init_xmlhandlers = require "core.xmlhandlers" - -module "connhandlers" - - -function new(name, session) - if name == "xmpp-client" then - local parser = lxp.new(init_xmlhandlers(session), ":"); - local parse = parser.parse; - return { data = function (self, data) return parse(parser, data); end, parser = parser } - end -end - -return _M; \ No newline at end of file diff -r 3a2d327c4856 -r ba08b8a4eeef net/connlisteners.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net/connlisteners.lua Wed Oct 22 17:36:21 2008 +0100 @@ -0,0 +1,40 @@ + +local server_add = require "net.server".add; +local log = require "util.logger".init("connlisteners"); + +local dofile, pcall, error = + dofile, pcall, error + +module "connlisteners" + +local listeners = {}; + +function register(name, listener) + if listeners[name] and listeners[name] ~= listener then + log("warning", "Listener %s is already registered, not registering any more", name); + return false; + end + listeners[name] = listener; + log("info", "Registered connection listener %s", name); + return true; +end + +function deregister(name) + listeners[name] = nil; +end + +function start(name, udata) + local h = listeners[name] + if not h then + pcall(dofile, "net/"..name:gsub("[^%w%-]", "_").."_listener.lua"); + h = listeners[name]; + if not h then + error("No such connection module: "..name, 0); + end + end + return server_add(h, + udata.port or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0), + udata.interface or "*", udata.mode or h.default_mode or 1, udata.ssl ); +end + +return _M; \ No newline at end of file diff -r 3a2d327c4856 -r ba08b8a4eeef net/xmppclient_listener.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net/xmppclient_listener.lua Wed Oct 22 17:36:21 2008 +0100 @@ -0,0 +1,74 @@ + +local logger = require "logger"; +local lxp = require "lxp" +local init_xmlhandlers = require "core.xmlhandlers" +local sm_new_session = require "core.sessionmanager".new_session; + +local connlisteners_register = require "net.connlisteners".register; + +local t_insert = table.insert; +local t_concat = table.concat; +local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end +local m_random = math.random; +local format = string.format; +local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); +local sm_streamopened = sessionmanager.streamopened; +local st = stanza; + +local sessions = {}; +local xmppclient = { default_port = 5222 }; + +-- These are session methods -- + +local function session_reset_stream(session) + -- Reset stream + local parser = lxp.new(init_xmlhandlers(session, sm_streamopened), ":"); + session.parser = parser; + + session.notopen = true; + + function session.data(conn, data) + parser:parse(data); + end + return true; +end + +-- End of session methods -- + +function xmppclient.listener(conn, data) + local session = sessions[conn]; + if not session then + session = sm_new_session(conn); + sessions[conn] = session; + + -- Logging functions -- + + local mainlog, log = log; + do + local conn_name = tostring(conn):match("[a-f0-9]+$"); + log = logger.init(conn_name); + end + local print = function (...) log("info", t_concatall({...}, "\t")); end + session.log = log; + + print("Client connected"); + + session.reset_stream = session_reset_stream; + + session_reset_stream(session); -- Initialise, ready for use + + -- TODO: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, + -- this will avoid the useless indirection we have atm + -- (I'm on a mission, no time to fix now) + session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end + + end + if data then + session.data(conn, data); + end +end + +function xmppclient.disconnect(conn) +end + +connlisteners_register("xmppclient", xmppclient); diff -r 3a2d327c4856 -r ba08b8a4eeef plugins/mod_saslauth.lua --- a/plugins/mod_saslauth.lua Wed Oct 22 17:27:40 2008 +0100 +++ b/plugins/mod_saslauth.lua Wed Oct 22 17:36:21 2008 +0100 @@ -36,8 +36,7 @@ return; end session.sasl_handler = nil; - session.connhandler = new_connhandler("xmpp-client", session); - session.notopen = true; + session:reset_stream(); end, function (reason) -- onFail diff -r 3a2d327c4856 -r ba08b8a4eeef plugins/mod_tls.lua --- a/plugins/mod_tls.lua Wed Oct 22 17:27:40 2008 +0100 +++ b/plugins/mod_tls.lua Wed Oct 22 17:36:21 2008 +0100 @@ -3,6 +3,8 @@ local send = require "core.sessionmanager".send_to_session; local sm_bind_resource = require "core.sessionmanager".bind_resource; +local sessions = sessions; + local usermanager_validate_credentials = require "core.usermanager".validate_credentials; local t_concat, t_insert = table.concat, table.insert; local tostring = tostring; @@ -16,16 +18,15 @@ add_handler("c2s_unauthed", "starttls", xmlns_starttls, function (session, stanza) if session.conn.starttls then - print("Wants to do TLS..."); send(session, st.stanza("proceed", { xmlns = xmlns_starttls })); - session.connhandler = new_connhandler("xmpp-client", session); - session.notopen = true; - if session.conn.starttls() then - print("Done"); - else - print("Failed"); - end - + -- FIXME: I'm commenting the below, not sure why it was necessary + -- sessions[session.conn] = nil; + session:reset_stream(); + session.conn.starttls(); + session.log("info", "TLS negotiation started..."); + else + -- FIXME: What reply? + session.log("warn", "Attempt to start TLS, but TLS is not available on this connection"); end end);