diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/moduleapi.lua | 20 | ||||
-rw-r--r-- | core/modulemanager.lua | 23 | ||||
-rw-r--r-- | core/portmanager.lua | 69 |
3 files changed, 87 insertions, 25 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 7a4d1fa6..a577c07a 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -22,6 +22,7 @@ local tonumber, tostring = tonumber, tostring; local prosody = prosody; local hosts = prosody.hosts; +local core_post_stanza = prosody.core_post_stanza; -- Registry of shared module data local shared_data = setmetatable({}, { __mode = "v" }); @@ -50,6 +51,7 @@ function api:set_global() local _log = logger.init("mod_"..self.name); self.log = function (self, ...) return _log(...); end; self._log = _log; + self.global = true; end function api:add_feature(xmlns) @@ -291,4 +293,22 @@ function api:handle_items(type, added_cb, removed_cb, existing) end end +function api:provides(name, item) + if not item then item = self.environment; end + if not item.name then + local item_name = module.name; + -- Strip a provider prefix to find the item name + -- (e.g. "auth_foo" -> "foo" for an auth provider) + if item_name:find(name.."_", 1, true) == 1 then + item_name = item_name:sub(#name+2); + end + item.name = item_name; + end + self:add_item(name, item); +end + +function api:send(stanza) + return core_post_stanza(hosts[self.host], stanza); +end + return api; diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 0ca37105..f9f3a8b8 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -101,11 +101,12 @@ local function do_unload_module(host, name) end if mod.module.items then -- remove items + local events = (host == "*" and prosody.events) or hosts[host].events; for key,t in pairs(mod.module.items) do for i = #t,1,-1 do local value = t[i]; t[i] = nil; - hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value}); + events.fire_event("item-removed/"..key, {source = mod.module, item = value}); end end end @@ -122,6 +123,7 @@ local function do_load_module(host, module_name) if not modulemap[host] then modulemap[host] = {}; + hosts[host].modules = modulemap[host]; end if modulemap[host][module_name] then @@ -147,8 +149,6 @@ local function do_load_module(host, module_name) api_instance.environment = pluginenv; setfenv(mod, pluginenv); - hosts[host].modules = modulemap[host]; - modulemap[host][module_name] = pluginenv; local ok, err = pcall(mod); if ok then @@ -160,15 +160,18 @@ local function do_load_module(host, module_name) end end - -- Use modified host, if the module set one - if api_instance.host == "*" and host ~= "*" then - modulemap[host][module_name] = nil; - modulemap["*"][module_name] = pluginenv; - api_instance:set_global(); + modulemap[pluginenv.module.host][module_name] = pluginenv; + if pluginenv.module.host == "*" then + if not pluginenv.module.global then -- COMPAT w/pre-0.9 + log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name); + api_instance:set_global(); + end + else + hosts[host].modules[module_name] = pluginenv; end - else + end + if not ok then log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil"); - do_unload_module(api_instance.host, module_name); -- Ignore error, module may be partially-loaded end return ok and pluginenv, err; end diff --git a/core/portmanager.lua b/core/portmanager.lua index 103c5d44..914a8e2f 100644 --- a/core/portmanager.lua +++ b/core/portmanager.lua @@ -13,8 +13,8 @@ end --- Private state --- service_name -> service_info -local services = {}; +-- service_name -> { service_info, ... } +local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end }); -- service_name, interface (string), port (number) local active_services = multitable.new(); @@ -52,28 +52,42 @@ end module("portmanager", package.seeall); +prosody.events.add_handler("item-added/net-provider", function (event) + local item = event.item; + register_service(item.name, item); +end); +prosody.events.add_handler("item-removed/net-provider", function (event) + local item = event.item; + unregister_service(item.name, item); +end); + --- Public API function activate_service(service_name) - local service_info = services[service_name]; + local service_info = services[service_name][1]; if not service_info then return nil, "Unknown service: "..service_name; end + + local listener = service_info.listener; + + local config_prefix = (service_info.config_prefix or service_name).."_"; + if config_prefix == "_" then + config_prefix = ""; + end - local bind_interfaces = set.new(config.get("*", service_name.."_interfaces") - or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9 + local bind_interfaces = set.new(config.get("*", config_prefix.."interfaces") + or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9 or (service_info.private and default_local_interfaces) or config.get("*", "interfaces") or config.get("*", "interface") -- COMPAT w/pre-0.9 - or service_info.default_interface -- COMPAT w/pre0.9 + or listener.default_interface -- COMPAT w/pre0.9 or default_interfaces); - local bind_ports = set.new(config.get("*", service_name.."_ports") - or (service_info.multiplex and config.get("*", "ports")) + local bind_ports = set.new(config.get("*", config_prefix.."ports") or service_info.default_ports - or {service_info.default_port}); + or {listener.default_port}); -- COMPAT w/pre-0.9 - local listener = service_info.listener; local mode = listener.default_mode or "*a"; local ssl; if service_info.encryption == "ssl" then @@ -85,8 +99,8 @@ function activate_service(service_name) for interface in bind_interfaces do for port in bind_ports do - if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then - log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name); + if #active_services:search(nil, interface, port) > 0 then + log("error", "Multiple services configured to listen on the same port ([%s]:%d): %s, %s", interface, port, active_services:search(nil, interface, port)[1][1].service.name or "<unnamed>", service_name or "<unnamed>"); else local handler, err = server.addserver(interface, port, listener, mode, ssl); if not handler then @@ -102,6 +116,7 @@ function activate_service(service_name) end end log("info", "Activated service '%s'", service_name); + return true; end function deactivate(service_name) @@ -118,16 +133,36 @@ function deactivate(service_name) end function register_service(service_name, service_info) - services[service_name] = service_info; + table.insert(services[service_name], service_info); - if not active_services[service_name] then - activate_service(service_name); + if not active_services:get(service_name) then + log("debug", "No active service for %s, activating...", service_name); + local ok, err = activate_service(service_name); + if not ok then + log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error"); + end end fire_event("service-added", { name = service_name, service = service_info }); return true; end +function unregister_service(service_name, service_info) + local service_info_list = services[service_name]; + for i, service in ipairs(service_info_list) do + if service == service_info then + table.remove(service_info_list, i); + end + end + if active_services[service_name] == service_info then + deactivate(service_name); + if #service_info_list > 0 then -- Other services registered with this name + activate(service_name); -- Re-activate with the next available one + end + end + fire_event("service-removed", { name = service_name, service = service_info }); +end + function get_service(service_name) return services[service_name]; end @@ -136,4 +171,8 @@ function get_active_services(...) return active_services; end +function get_registered_services() + return services; +end + return _M; |