# HG changeset patch # User Matthew Wild # Date 1228534941 0 # Node ID b2d665689df7805f3950e952f1cf0d86235ec43e # Parent 5216efe6088bfdd95f69c6b322c7c058cb8a31dd# Parent 3303fe83cd70a63e09898279b596306694c7111f Merge from waqas/myself diff -r 3303fe83cd70 -r b2d665689df7 core/eventmanager.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/eventmanager.lua Sat Dec 06 03:42:21 2008 +0000 @@ -0,0 +1,25 @@ + +local t_insert = table.insert; +local ipairs = ipairs; + +module "eventmanager" + +local event_handlers = {}; + +function add_event_hook(name, handler) + if not event_handlers[name] then + event_handlers[name] = {}; + end + t_insert(event_handlers[name] , handler); +end + +function fire_event(name, ...) + local event_handlers = event_handlers[name]; + if event_handlers then + for name, handler in ipairs(event_handlers) do + handler(...); + end + end +end + +return _M; \ No newline at end of file diff -r 3303fe83cd70 -r b2d665689df7 core/hostmanager.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/hostmanager.lua Sat Dec 06 03:42:21 2008 +0000 @@ -0,0 +1,49 @@ + +hosts = {}; + +local hosts = hosts; +local configmanager = require "core.configmanager"; +local eventmanager = require "core.eventmanager"; + +local pairs = pairs; + +module "hostmanager" + +local function load_enabled_hosts(config) + local defined_hosts = config or configmanager.getconfig(); + + for host, host_config in pairs(defined_hosts) do + if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then + activate(host, host_config); + end + end +end + +eventmanager.add_event_hook("server-starting", load_enabled_hosts); + +function activate(host, host_config) + hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} }; + + eventmanager.fire_event("host-activated", host, host_config); +end + +function deactivate(host) + local host_session = hosts[host]; + + eventmanager.fire_event("host-deactivating", host, host_session); + + -- Disconnect local users, s2s connections + for user, session_list in pairs(host_session.sessions) do + for resource, session in pairs(session_list) do + session:close("host-gone"); + end + end + -- Components? + + hosts[host] = nil; + eventmanager.fire_event("host-deactivated", host); +end + +function getconfig(name) +end + diff -r 3303fe83cd70 -r b2d665689df7 core/modulemanager.lua --- a/core/modulemanager.lua Sat Dec 06 02:27:34 2008 +0000 +++ b/core/modulemanager.lua Sat Dec 06 03:42:21 2008 +0000 @@ -24,6 +24,8 @@ local logger = require "util.logger"; local log = logger.init("modulemanager"); local addDiscoInfoHandler = require "core.discomanager".addDiscoInfoHandler; +local eventmanager = require "core.eventmanager"; + local loadfile, pcall = loadfile, pcall; local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv; @@ -182,28 +184,7 @@ end); end - -do - local event_handlers = {}; - - function api:add_event_hook(name, handler) - if not event_handlers[name] then - event_handlers[name] = {}; - end - t_insert(event_handlers[name] , handler); - self:log("debug", "Subscribed to %s", name); - end - - function fire_event(name, ...) - local event_handlers = event_handlers[name]; - if event_handlers then - for name, handler in ipairs(event_handlers) do - handler(...); - end - end - end -end - +api.add_event_hook = eventmanager.add_event_hook; local function _add_handler(module, origin_type, tag, xmlns, handler) local handlers = stanza_handlers[module.host]; diff -r 3303fe83cd70 -r b2d665689df7 core/sessionmanager.lua --- a/core/sessionmanager.lua Sat Dec 06 02:27:34 2008 +0000 +++ b/core/sessionmanager.lua Sat Dec 06 03:42:21 2008 +0000 @@ -35,6 +35,10 @@ 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,7 +49,7 @@ 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; @@ -109,6 +113,8 @@ if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end -- We don't support binding multiple resources + session.conntimetotal = gettime()-session.conntime; + resource = resource or uuid_generate(); --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing @@ -175,7 +181,7 @@ local features = st.stanza("stream:features"); - modulemanager.fire_event("stream-features", session, features); + fire_event("stream-features", session, features); send(features); diff -r 3303fe83cd70 -r b2d665689df7 prosody --- a/prosody Sat Dec 06 02:27:34 2008 +0000 +++ b/prosody Sat Dec 06 03:42:21 2008 +0000 @@ -68,6 +68,44 @@ end end +require "util.datamanager".set_data_path(data_path); + +local server = require "net.server" + +require "util.dependencies" + +-- Maps connections to sessions -- +sessions = {}; +hosts = {}; + + +-- Load and initialise core modules -- + +require "util.import" +require "core.xmlhandlers" +require "core.rostermanager" +require "core.offlinemessage" +require "core.eventmanager" +require "core.hostmanager" +require "core.modulemanager" +require "core.usermanager" +require "core.sessionmanager" +require "core.stanza_router" + +--[[ +pcall(require, "remdebug.engine"); +if remdebug then remdebug.engine.start() end +]] + +local cl = require "net.connlisteners"; + +require "util.stanza" +require "util.jid" + +------------------------------------------------------------------------ + +------------- Begin code without a home --------------------- + local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data"; local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end local _mkdir = {} @@ -91,46 +129,11 @@ end mkdir(data_path); -require "util.datamanager".set_data_path(data_path); - -local server = require "net.server" - -require "util.dependencies" - --- Maps connections to sessions -- -sessions = {}; -hosts = {}; - -local defined_hosts = config.getconfig(); - -for host, host_config in pairs(defined_hosts) do - if host ~= "*" and (host_config.core.enabled == nil or host_config.core.enabled) then - hosts[host] = {type = "local", connected = true, sessions = {}, host = host, s2sout = {} }; - mkdirs(host); - end -end +eventmanager.add_event_hook("host-activated", mkdirs); --- Load and initialise core modules -- - -require "util.import" -require "core.xmlhandlers" -require "core.rostermanager" -require "core.offlinemessage" -require "core.modulemanager" -require "core.usermanager" -require "core.sessionmanager" -require "core.stanza_router" +----------- End of out-of-place code -------------- ---[[ -pcall(require, "remdebug.engine"); -if remdebug then remdebug.engine.start() end -]] - -local start = require "net.connlisteners".start; -require "util.stanza" -require "util.jid" - ------------------------------------------------------------------------- +eventmanager.fire_event("server-starting"); -- Initialise modules @@ -159,13 +162,17 @@ end -- start listening on sockets -start("xmppclient", { ssl = global_ssl_ctx }) -start("xmppserver", { ssl = global_ssl_ctx }) +cl.start("xmppclient", { ssl = global_ssl_ctx }) +cl.start("xmppserver", { ssl = global_ssl_ctx }) if config.get("*", "core", "console_enabled") then - start("console") + if cl.get("console") then + cl.start("console") + else + log("error", "Console is enabled, but the console module appears not to be loaded"); + end end -modulemanager.fire_event("server-started"); +eventmanager.fire_event("server-started"); server.loop(); diff -r 3303fe83cd70 -r b2d665689df7 tests/test.lua --- a/tests/test.lua Sat Dec 06 02:27:34 2008 +0000 +++ b/tests/test.lua Sat Dec 06 03:42:21 2008 +0000 @@ -21,6 +21,7 @@ function run_all_tests() dotest "util.jid" + dotest "util.multitable" dotest "core.stanza_router" dotest "core.s2smanager" dotest "core.configmanager" @@ -40,14 +41,34 @@ return setmetatable(t or {}, env_mt); end -function assert_equal(a, b, message) +function assert_equal(a, b, message, level) if not (a == b) then - error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), 2); + error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), (level or 1) + 1); elseif verbosity >= 4 then print("assert_equal succeeded: "..tostring(a).." == "..tostring(b)); end end +function assert_table(a, message, level) + assert_equal(type(a), "table", message, (level or 1) + 1); +end +function assert_function(a, message, level) + assert_equal(type(a), "function", message, (level or 1) + 1); +end +function assert_string(a, message, level) + assert_equal(type(a), "string", message, (level or 1) + 1); +end +function assert_boolean(a, message) + assert_equal(type(a), "boolean", message); +end +function assert_is(a, message) + assert_equal(not not a, true, message); +end +function assert_is_not(a, message) + assert_equal(not not a, false, message); +end + + function dosingletest(testname, fname) local tests = setmetatable({}, { __index = _G }); tests.__unit = testname; diff -r 3303fe83cd70 -r b2d665689df7 util/multitable.lua --- a/util/multitable.lua Sat Dec 06 02:27:34 2008 +0000 +++ b/util/multitable.lua Sat Dec 06 03:42:21 2008 +0000 @@ -44,7 +44,7 @@ if not tab then tab = {}; t[key] = tab; end t = tab; end - t_insert(t, select(count, ...)); + t_insert(t, (select(count, ...))); end local function r(t, n, _end, ...)