diff options
-rw-r--r-- | core/sessionmanager.lua | 2 | ||||
-rw-r--r-- | core/xmlhandlers.lua | 5 | ||||
-rw-r--r-- | main.lua | 67 | ||||
-rw-r--r-- | net/connhandlers.lua | 16 | ||||
-rw-r--r-- | net/connlisteners.lua | 40 | ||||
-rw-r--r-- | net/xmppclient_listener.lua | 74 | ||||
-rw-r--r-- | plugins/mod_saslauth.lua | 3 | ||||
-rw-r--r-- | plugins/mod_tls.lua | 19 |
8 files changed, 137 insertions, 89 deletions
diff --git a/core/sessionmanager.lua b/core/sessionmanager.lua index 4f8b1913..3a5625cb 100644 --- a/core/sessionmanager.lua +++ b/core/sessionmanager.lua @@ -20,7 +20,7 @@ local getmetatable = getmetatable; 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 --git a/core/xmlhandlers.lua b/core/xmlhandlers.lua index ebc8f91d..b1af299f 100644 --- a/core/xmlhandlers.lua +++ b/core/xmlhandlers.lua @@ -1,5 +1,4 @@ -local sessionmanager_streamopened = require "core.sessionmanager".streamopened; require "util.stanza" local st = stanza; @@ -16,7 +15,7 @@ local error = error; module "xmlhandlers" -function init_xmlhandlers(session) +function init_xmlhandlers(session, streamopened) local ns_stack = { "" }; local curr_ns = ""; local curr_tag; @@ -38,7 +37,7 @@ function init_xmlhandlers(session) 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"); @@ -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 @@ end 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.modulemanager" 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 sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionma 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 @@ setmetatable(_G, { __index = function (t, k) print("WARNING: ATTEMPT TO READ A N 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 --git a/net/connhandlers.lua b/net/connhandlers.lua deleted file mode 100644 index 493f1946..00000000 --- a/net/connhandlers.lua +++ /dev/null @@ -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 --git a/net/connlisteners.lua b/net/connlisteners.lua new file mode 100644 index 00000000..a5c8755f --- /dev/null +++ b/net/connlisteners.lua @@ -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 --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua new file mode 100644 index 00000000..7d5b3abe --- /dev/null +++ b/net/xmppclient_listener.lua @@ -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 --git a/plugins/mod_saslauth.lua b/plugins/mod_saslauth.lua index 8ef1e09d..1376b87b 100644 --- a/plugins/mod_saslauth.lua +++ b/plugins/mod_saslauth.lua @@ -36,8 +36,7 @@ add_handler("c2s_unauthed", "auth", xmlns_sasl, return; end session.sasl_handler = nil; - session.connhandler = new_connhandler("xmpp-client", session); - session.notopen = true; + session:reset_stream(); end, function (reason) -- onFail diff --git a/plugins/mod_tls.lua b/plugins/mod_tls.lua index 22df4b28..ab06b9a5 100644 --- a/plugins/mod_tls.lua +++ b/plugins/mod_tls.lua @@ -3,6 +3,8 @@ local st = require "util.stanza"; 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 @@ local new_connhandler = require "net.connhandlers".new; 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); |