aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/certmanager.lua210
-rw-r--r--core/configmanager.lua158
-rw-r--r--core/hostmanager.lua47
-rw-r--r--core/loggingmanager.lua198
-rw-r--r--core/moduleapi.lua116
-rw-r--r--core/modulemanager.lua60
-rw-r--r--core/portmanager.lua107
-rw-r--r--core/rostermanager.lua155
-rw-r--r--core/s2smanager.lua30
-rw-r--r--core/sessionmanager.lua84
-rw-r--r--core/stanza_router.lua10
-rw-r--r--core/statsmanager.lua72
-rw-r--r--core/storagemanager.lua117
-rw-r--r--core/usermanager.lua72
14 files changed, 905 insertions, 531 deletions
diff --git a/core/certmanager.lua b/core/certmanager.lua
index 624bd841..a4c9d891 100644
--- a/core/certmanager.lua
+++ b/core/certmanager.lua
@@ -1,97 +1,173 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
+local softreq = require"util.dependencies".softreq;
+local ssl = softreq"ssl";
+if not ssl then
+ return {
+ create_context = function ()
+ return nil, "LuaSec (required for encryption) was not found";
+ end;
+ reload_ssl_config = function () end;
+ }
+end
+
local configmanager = require "core.configmanager";
local log = require "util.logger".init("certmanager");
-local ssl = ssl;
-local ssl_newcontext = ssl and ssl.newcontext;
-
-local tostring = tostring;
+local ssl_context = ssl.context or softreq"ssl.context";
+local ssl_x509 = ssl.x509 or softreq"ssl.x509";
+local ssl_newcontext = ssl.newcontext;
+local new_config = require"util.sslconfig".new;
+local stat = require "lfs".attributes;
+
+local tonumber, tostring = tonumber, tostring;
+local pairs = pairs;
local type = type;
local io_open = io.open;
+local select = select;
local prosody = prosody;
-local resolve_path = configmanager.resolve_relative_path;
+local resolve_path = require"util.paths".resolve_relative_path;
local config_path = prosody.paths.config;
-local luasec_has_noticket, luasec_has_verifyext, luasec_has_no_compression;
-if ssl then
- local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
- luasec_has_noticket = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=4;
- luasec_has_verifyext = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
- luasec_has_no_compression = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=5;
-end
+local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
+local luasec_version = luasec_major * 100 + luasec_minor;
+local luasec_has = {
+ -- TODO If LuaSec ever starts exposing these things itself, use that instead
+ cipher_server_preference = luasec_version >= 2;
+ no_ticket = luasec_version >= 4;
+ no_compression = luasec_version >= 5;
+ single_dh_use = luasec_version >= 2;
+ single_ecdh_use = luasec_version >= 2;
+};
-module "certmanager"
+local _ENV = nil;
-- Global SSL options if not overridden per-host
-local default_ssl_config = configmanager.get("*", "ssl");
-local default_capath = "/etc/ssl/certs";
-local default_verify = (ssl and ssl.x509 and { "peer", "client_once", }) or "none";
-local default_options = { "no_sslv2", "no_sslv3", "cipher_server_preference", luasec_has_noticket and "no_ticket" or nil };
-local default_verifyext = { "lsec_continue", "lsec_ignore_purpose" };
-
-if ssl and not luasec_has_verifyext and ssl.x509 then
- -- COMPAT mw/luasec-hg
- for i=1,#default_verifyext do -- Remove lsec_ prefix
- default_verify[#default_verify+1] = default_verifyext[i]:sub(6);
+local global_ssl_config = configmanager.get("*", "ssl");
+
+local global_certificates = configmanager.get("*", "certificates") or "certs";
+
+local crt_try = { "", "/%s.crt", "/%s/fullchain.pem", "/%s.pem", };
+local key_try = { "", "/%s.key", "/%s/privkey.pem", "/%s.pem", };
+
+local function find_cert(user_certs, name)
+ local certs = resolve_path(config_path, user_certs or global_certificates);
+ for i = 1, #crt_try do
+ local crt_path = certs .. crt_try[i]:format(name);
+ local key_path = certs .. key_try[i]:format(name);
+
+ if stat(crt_path, "mode") == "file" then
+ if key_path:sub(-4) == ".crt" then
+ key_path = key_path:sub(1, -4) .. "key";
+ if stat(key_path, "mode") == "file" then
+ return { certificate = crt_path, key = key_path };
+ end
+ elseif stat(key_path, "mode") == "file" then
+ return { certificate = crt_path, key = key_path };
+ end
+ end
end
end
-if luasec_has_no_compression and configmanager.get("*", "ssl_compression") ~= true then
- default_options[#default_options+1] = "no_compression";
+
+local function find_host_cert(host)
+ if not host then return nil; end
+ return find_cert(configmanager.get(host, "certificate"), host) or find_host_cert(host:match("%.(.+)$"));
end
-if luasec_has_no_compression then -- Has no_compression? Then it has these too...
- default_options[#default_options+1] = "single_dh_use";
- default_options[#default_options+1] = "single_ecdh_use";
+local function find_service_cert(service, port)
+ local cert_config = configmanager.get("*", service.."_certificate");
+ if type(cert_config) == "table" then
+ cert_config = cert_config[port] or cert_config.default;
+ end
+ return find_cert(cert_config, service);
end
-function create_context(host, mode, user_ssl_config)
- user_ssl_config = user_ssl_config or default_ssl_config;
-
- if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
- if not user_ssl_config then return nil, "No SSL/TLS configuration present for "..host; end
-
- local ssl_config = {
- mode = mode;
- protocol = user_ssl_config.protocol or "sslv23";
- key = resolve_path(config_path, user_ssl_config.key);
- password = user_ssl_config.password or function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
- certificate = resolve_path(config_path, user_ssl_config.certificate);
- capath = resolve_path(config_path, user_ssl_config.capath or default_capath);
- cafile = resolve_path(config_path, user_ssl_config.cafile);
- verify = user_ssl_config.verify or default_verify;
- verifyext = user_ssl_config.verifyext or default_verifyext;
- options = user_ssl_config.options or default_options;
- depth = user_ssl_config.depth;
- curve = user_ssl_config.curve or "secp384r1";
- ciphers = user_ssl_config.ciphers or "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
- dhparam = user_ssl_config.dhparam;
+-- Built-in defaults
+local core_defaults = {
+ capath = "/etc/ssl/certs";
+ depth = 9;
+ protocol = "tlsv1+";
+ verify = (ssl_x509 and { "peer", "client_once", }) or "none";
+ options = {
+ cipher_server_preference = luasec_has.cipher_server_preference;
+ no_ticket = luasec_has.no_ticket;
+ no_compression = luasec_has.no_compression and configmanager.get("*", "ssl_compression") ~= true;
+ single_dh_use = luasec_has.single_dh_use;
+ single_ecdh_use = luasec_has.single_ecdh_use;
};
+ verifyext = { "lsec_continue", "lsec_ignore_purpose" };
+ curve = "secp384r1";
+ ciphers = "HIGH+kEDH:HIGH+kEECDH:HIGH:!PSK:!SRP:!3DES:!aNULL";
+}
+local path_options = { -- These we pass through resolve_path()
+ key = true, certificate = true, cafile = true, capath = true, dhparam = true
+}
+
+if luasec_version < 5 and ssl_x509 then
+ -- COMPAT mw/luasec-hg
+ for i=1,#core_defaults.verifyext do -- Remove lsec_ prefix
+ core_defaults.verify[#core_defaults.verify+1] = core_defaults.verifyext[i]:sub(6);
+ end
+end
+
+local function create_context(host, mode, ...)
+ local cfg = new_config();
+ cfg:apply(core_defaults);
+ local service_name, port = host:match("^(%w+) port (%d+)$");
+ if service_name then
+ cfg:apply(find_service_cert(service_name, tonumber(port)));
+ else
+ cfg:apply(find_host_cert(host));
+ end
+ cfg:apply({
+ mode = mode,
+ -- We can't read the password interactively when daemonized
+ password = function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
+ });
+ cfg:apply(global_ssl_config);
+
+ for i = select('#', ...), 1, -1 do
+ cfg:apply(select(i, ...));
+ end
+ local user_ssl_config = cfg:final();
+
+ if mode == "server" then
+ if not user_ssl_config.key then return nil, "No key present in SSL/TLS configuration for "..host; end
+ if not user_ssl_config.certificate then return nil, "No certificate present in SSL/TLS configuration for "..host; end
+ end
+
+ for option in pairs(path_options) do
+ if type(user_ssl_config[option]) == "string" then
+ user_ssl_config[option] = resolve_path(config_path, user_ssl_config[option]);
+ else
+ user_ssl_config[option] = nil;
+ end
+ end
-- LuaSec expects dhparam to be a callback that takes two arguments.
-- We ignore those because it is mostly used for having a separate
-- set of params for EXPORT ciphers, which we don't have by default.
- if type(ssl_config.dhparam) == "string" then
- local f, err = io_open(resolve_path(config_path, ssl_config.dhparam));
+ if type(user_ssl_config.dhparam) == "string" then
+ local f, err = io_open(user_ssl_config.dhparam);
if not f then return nil, "Could not open DH parameters: "..err end
local dhparam = f:read("*a");
f:close();
- ssl_config.dhparam = function() return dhparam; end
+ user_ssl_config.dhparam = function() return dhparam; end
end
- local ctx, err = ssl_newcontext(ssl_config);
+ local ctx, err = ssl_newcontext(user_ssl_config);
- -- COMPAT: LuaSec 0.4.1 ignores the cipher list from the config, so we have to take
- -- care of it ourselves...
- if ctx and ssl_config.ciphers then
+ -- COMPAT Older LuaSec ignores the cipher list from the config, so we have to take care
+ -- of it ourselves (W/A for #x)
+ if ctx and user_ssl_config.ciphers then
local success;
- success, err = ssl.context.setcipher(ctx, ssl_config.ciphers);
+ success, err = ssl_context.setcipher(ctx, user_ssl_config.ciphers);
if not success then ctx = nil; end
end
@@ -100,9 +176,9 @@ function create_context(host, mode, user_ssl_config)
local file = err:match("^error loading (.-) %(");
if file then
if file == "private key" then
- file = ssl_config.key or "your private key";
+ file = user_ssl_config.key or "your private key";
elseif file == "certificate" then
- file = ssl_config.certificate or "your certificate file";
+ file = user_ssl_config.certificate or "your certificate file";
end
local reason = err:match("%((.+)%)$") or "some reason";
if reason == "Permission denied" then
@@ -121,13 +197,19 @@ function create_context(host, mode, user_ssl_config)
log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
end
end
- return ctx, err;
+ return ctx, err, user_ssl_config;
end
-function reload_ssl_config()
- default_ssl_config = configmanager.get("*", "ssl");
+local function reload_ssl_config()
+ global_ssl_config = configmanager.get("*", "ssl");
+ if luasec_has.no_compression then
+ core_defaults.options.no_compression = configmanager.get("*", "ssl_compression") ~= true;
+ end
end
prosody.events.add_handler("config-reloaded", reload_ssl_config);
-return _M;
+return {
+ create_context = create_context;
+ reload_ssl_config = reload_ssl_config;
+};
diff --git a/core/configmanager.lua b/core/configmanager.lua
index c8aa7b9a..16d4b8e2 100644
--- a/core/configmanager.lua
+++ b/core/configmanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -15,26 +15,31 @@ local fire_event = prosody and prosody.events.fire_event or function () end;
local envload = require"util.envload".envload;
local deps = require"util.dependencies";
+local resolve_relative_path = require"util.paths".resolve_relative_path;
+local glob_to_pattern = require"util.paths".glob_to_pattern;
local path_sep = package.config:sub(1,1);
-local have_encodings, encodings = pcall(require, "util.encodings");
-local nameprep = have_encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
+local encodings = deps.softreq"util.encodings";
+local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
+
+local _M = {};
+local _ENV = nil;
-module "configmanager"
+_M.resolve_relative_path = resolve_relative_path; -- COMPAT
local parsers = {};
-local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
+local config_mt = { __index = function (t, _) return rawget(t, "*"); end};
local config = setmetatable({ ["*"] = { } }, config_mt);
-- When host not found, use global
local host_mt = { __index = function(_, k) return config["*"][k] end }
-function getconfig()
+function _M.getconfig()
return config;
end
-function get(host, key, _oldkey)
+function _M.get(host, key, _oldkey)
if key == "core" then
key = _oldkey; -- COMPAT with code that still uses "core"
end
@@ -50,11 +55,11 @@ function _M.rawget(host, key, _oldkey)
end
end
-local function set(config, host, key, value)
+local function set(config_table, host, key, value)
if host and key then
- local hostconfig = rawget(config, host);
+ local hostconfig = rawget(config_table, host);
if not hostconfig then
- hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
+ hostconfig = rawset(config_table, host, setmetatable({}, host_mt))[host];
end
hostconfig[key] = value;
return true;
@@ -69,55 +74,20 @@ function _M.set(host, key, value, _oldvalue)
return set(config, host, key, value);
end
--- Helper function to resolve relative paths (needed by config)
-do
- function resolve_relative_path(parent_path, path)
- if path then
- -- Some normalization
- parent_path = parent_path:gsub("%"..path_sep.."+$", "");
- path = path:gsub("^%.%"..path_sep.."+", "");
-
- local is_relative;
- if path_sep == "/" and path:sub(1,1) ~= "/" then
- is_relative = true;
- elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then
- is_relative = true;
- end
- if is_relative then
- return parent_path..path_sep..path;
- end
- end
- return path;
- end
-end
+function _M.load(filename, config_format)
+ config_format = config_format or filename:match("%w+$");
--- Helper function to convert a glob to a Lua pattern
-local function glob_to_pattern(glob)
- return "^"..glob:gsub("[%p*?]", function (c)
- if c == "*" then
- return ".*";
- elseif c == "?" then
- return ".";
- else
- return "%"..c;
- end
- end).."$";
-end
-
-function load(filename, format)
- format = format or filename:match("%w+$");
-
- if parsers[format] and parsers[format].load then
+ if parsers[config_format] and parsers[config_format].load then
local f, err = io.open(filename);
if f then
local new_config = setmetatable({ ["*"] = { } }, config_mt);
- local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
+ local ok, err = parsers[config_format].load(f:read("*a"), filename, new_config);
f:close();
if ok then
config = new_config;
fire_event("config-reloaded", {
filename = filename,
- format = format,
+ format = config_format,
config = config
});
end
@@ -126,98 +96,95 @@ function load(filename, format)
return f, "file", err;
end
- if not format then
+ if not config_format then
return nil, "file", "no parser specified";
else
- return nil, "file", "no parser for "..(format);
+ return nil, "file", "no parser for "..(config_format);
end
end
-function save(filename, format)
-end
-
-function addparser(format, parser)
- if format and parser then
- parsers[format] = parser;
+function _M.addparser(config_format, parser)
+ if config_format and parser then
+ parsers[config_format] = parser;
end
end
-- _M needed to avoid name clash with local 'parsers'
function _M.parsers()
local p = {};
- for format in pairs(parsers) do
- table.insert(p, format);
+ for config_format in pairs(parsers) do
+ table.insert(p, config_format);
end
return p;
end
-- Built-in Lua parser
do
- local pcall, setmetatable = _G.pcall, _G.setmetatable;
- local rawget = _G.rawget;
+ local pcall = _G.pcall;
parsers.lua = {};
- function parsers.lua.load(data, config_file, config)
+ function parsers.lua.load(data, config_file, config_table)
local env;
-- The ' = true' are needed so as not to set off __newindex when we assign the functions below
env = setmetatable({
Host = true, host = true, VirtualHost = true,
Component = true, component = true,
Include = true, include = true, RunScript = true }, {
- __index = function (t, k)
+ __index = function (_, k)
return rawget(_G, k);
end,
- __newindex = function (t, k, v)
- set(config, env.__currenthost or "*", k, v);
+ __newindex = function (_, k, v)
+ set(config_table, env.__currenthost or "*", k, v);
end
});
-
+
rawset(env, "__currenthost", "*") -- Default is global
function env.VirtualHost(name)
name = nameprep(name);
- if rawget(config, name) and rawget(config[name], "component_module") then
+ if rawget(config_table, name) and rawget(config_table[name], "component_module") then
error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
- name, config[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
+ name, config_table[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
end
rawset(env, "__currenthost", name);
-- Needs at least one setting to logically exist :)
- set(config, name or "*", "defined", true);
+ set(config_table, name or "*", "defined", true);
return function (config_options)
rawset(env, "__currenthost", "*"); -- Return to global scope
for option_name, option_value in pairs(config_options) do
- set(config, name or "*", option_name, option_value);
+ set(config_table, name or "*", option_name, option_value);
end
end;
end
env.Host, env.host = env.VirtualHost, env.VirtualHost;
-
+
function env.Component(name)
name = nameprep(name);
- if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
+ if rawget(config_table, name) and rawget(config_table[name], "defined") and not rawget(config_table[name], "component_module") then
error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
name, name, name), 0);
end
- set(config, name, "component_module", "component");
+ set(config_table, name, "component_module", "component");
-- Don't load the global modules by default
- set(config, name, "load_global_modules", false);
+ set(config_table, name, "load_global_modules", false);
rawset(env, "__currenthost", name);
local function handle_config_options(config_options)
rawset(env, "__currenthost", "*"); -- Return to global scope
for option_name, option_value in pairs(config_options) do
- set(config, name or "*", option_name, option_value);
+ set(config_table, name or "*", option_name, option_value);
end
end
-
+
return function (module)
if type(module) == "string" then
- set(config, name, "component_module", module);
+ set(config_table, name, "component_module", module);
return handle_config_options;
end
return handle_config_options(module);
end
end
env.component = env.Component;
-
+
function env.Include(file)
+ -- Check whether this is a wildcard Include
if file:match("[*?]") then
local lfs = deps.softreq "lfs";
if not lfs then
@@ -237,38 +204,39 @@ do
env.Include(path..path_sep..f);
end
end
- else
- local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
- local f, err = io.open(file);
- if f then
- local ret, err = parsers.lua.load(f:read("*a"), file, config);
- if not ret then error(err:gsub("%[string.-%]", file), 0); end
- end
- if not f then error("Error loading included "..file..": "..err, 0); end
- return f, err;
+ return;
+ end
+ -- Not a wildcard, so resolve (potentially) relative path and run through config parser
+ file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
+ local f, err = io.open(file);
+ if f then
+ local ret, err = parsers.lua.load(f:read("*a"), file, config_table);
+ if not ret then error(err:gsub("%[string.-%]", file), 0); end
end
+ if not f then error("Error loading included "..file..": "..err, 0); end
+ return f, err;
end
env.include = env.Include;
-
+
function env.RunScript(file)
return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
end
-
+
local chunk, err = envload(data, "@"..config_file, env);
-
+
if not chunk then
return nil, err;
end
-
+
local ok, err = pcall(chunk);
-
+
if not ok then
return nil, err;
end
-
+
return true;
end
-
+
end
return _M;
diff --git a/core/hostmanager.lua b/core/hostmanager.lua
index 06ba72a1..53c1cd4e 100644
--- a/core/hostmanager.lua
+++ b/core/hostmanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -13,7 +13,6 @@ local disco_items = require "util.multitable".new();
local NULL = {};
local jid_split = require "util.jid".split;
-local uuid_gen = require "util.uuid".generate;
local log = require "util.logger".init("hostmanager");
@@ -27,15 +26,31 @@ local core_route_stanza = _G.prosody.core_route_stanza;
local pairs, select, rawget = pairs, select, rawget;
local tostring, type = tostring, type;
+local setmetatable = setmetatable;
+
+local _ENV = nil;
-module "hostmanager"
+local host_mt = { }
+function host_mt:__tostring()
+ if self.type == "component" then
+ local typ = configmanager.get(self.host, "component_module");
+ if typ == "component" then
+ return ("Component %q"):format(self.host);
+ end
+ return ("Component %q %q"):format(self.host, typ);
+ elseif self.type == "local" then
+ return ("VirtualHost %q"):format(self.host);
+ end
+end
local hosts_loaded_once;
+local activate, deactivate;
+
local function load_enabled_hosts(config)
local defined_hosts = config or configmanager.getconfig();
local activated_any_host;
-
+
for host, host_config in pairs(defined_hosts) do
if host ~= "*" and host_config.enabled ~= false then
if not host_config.component_module then
@@ -44,11 +59,11 @@ local function load_enabled_hosts(config)
activate(host, host_config);
end
end
-
+
if not activated_any_host then
log("error", "No active VirtualHost entries in the config file. This may cause unexpected behaviour as no modules will be loaded.");
end
-
+
prosody_events.fire_event("hosts-activated", defined_hosts);
hosts_loaded_once = true;
end
@@ -56,8 +71,8 @@ end
prosody_events.add_handler("server-starting", load_enabled_hosts);
local function host_send(stanza)
- local name, type = stanza.name, stanza.attr.type;
- if type == "error" or (name == "iq" and type == "result") then
+ local name, stanza_type = stanza.name, stanza.attr.type;
+ if stanza_type == "error" or (name == "iq" and stanza_type == "result") then
local dest_host_name = select(2, jid_split(stanza.attr.to));
local dest_host = hosts[dest_host_name] or { type = "unknown" };
log("warn", "Unhandled response sent to %s host %s: %s", dest_host.type, dest_host_name, tostring(stanza));
@@ -74,10 +89,10 @@ function activate(host, host_config)
host = host;
s2sout = {};
events = events_new();
- dialback_secret = configmanager.get(host, "dialback_secret") or uuid_gen();
send = host_send;
modules = {};
};
+ setmetatable(host_session, host_mt);
if not host_config.component_module then -- host
host_session.type = "local";
host_session.sessions = {};
@@ -93,7 +108,7 @@ function activate(host, host_config)
log("warn", "%s: Option '%s' has no effect for virtual hosts - put it in the server-wide section instead", host, option_name);
end
end
-
+
log((hosts_loaded_once and "info") or "debug", "Activated host: %s", host);
prosody_events.fire_event("host-activated", host);
return true;
@@ -104,11 +119,11 @@ function deactivate(host, reason)
if not host_session then return nil, "The host "..tostring(host).." is not activated"; end
log("info", "Deactivating host: %s", host);
prosody_events.fire_event("host-deactivating", { host = host, host_session = host_session, reason = reason });
-
+
if type(reason) ~= "table" then
reason = { condition = "host-gone", text = tostring(reason or "This server has stopped serving "..host) };
end
-
+
-- Disconnect local users, s2s connections
-- TODO: These should move to mod_c2s and mod_s2s (how do they know they're being unloaded and not reloaded?)
if host_session.sessions then
@@ -151,8 +166,12 @@ function deactivate(host, reason)
return true;
end
-function get_children(host)
+local function get_children(host)
return disco_items:get(host) or NULL;
end
-return _M;
+return {
+ activate = activate;
+ deactivate = deactivate;
+ get_children = get_children;
+}
diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua
index c69dede8..e3a83817 100644
--- a/core/loggingmanager.lua
+++ b/core/loggingmanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -10,29 +10,28 @@
local format = string.format;
local setmetatable, rawset, pairs, ipairs, type =
setmetatable, rawset, pairs, ipairs, type;
-local io_open, io_write = io.open, io.write;
+local stdout = io.stdout;
+local io_open = io.open;
local math_max, rep = math.max, string.rep;
local os_date = os.date;
-local getstyle, setstyle = require "util.termcolours".getstyle, require "util.termcolours".setstyle;
-
-if os.getenv("__FLUSH_LOG") then
- local io_flush = io.flush;
- local _io_write = io_write;
- io_write = function(...) _io_write(...); io_flush(); end
-end
+local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
+local tostring = tostring;
+local select = select;
+local unpack = table.unpack or unpack; --luacheck: ignore 113
local config = require "core.configmanager";
local logger = require "util.logger";
local prosody = prosody;
_G.log = logger.init("general");
+prosody.log = logger.init("general");
-module "loggingmanager"
+local _ENV = nil;
-- The log config used if none specified in the config file (see reload_logging for initialization)
local default_logging;
local default_file_logging;
-local default_timestamp = "%b %d %H:%M:%S";
+local default_timestamp = "%b %d %H:%M:%S ";
-- The actual config loggingmanager is using
local logging_config;
@@ -45,16 +44,16 @@ local logging_levels = { "debug", "info", "warn", "error" }
-- This function is called automatically when a new sink type is added [see apply_sink_rules()]
local function add_rule(sink_config)
local sink_maker = log_sink_types[sink_config.to];
- if sink_maker then
- -- Create sink
- local sink = sink_maker(sink_config);
-
- -- Set sink for all chosen levels
- for level in pairs(get_levels(sink_config.levels or logging_levels)) do
- logger.add_level_sink(level, sink);
- end
- else
- -- No such sink type
+ if not sink_maker then
+ return; -- No such sink type
+ end
+
+ -- Create sink
+ local sink = sink_maker(sink_config);
+
+ -- Set sink for all chosen levels
+ for level in pairs(get_levels(sink_config.levels or logging_levels)) do
+ logger.add_level_sink(level, sink);
end
end
@@ -63,7 +62,7 @@ end
-- the log_sink_types table.
function apply_sink_rules(sink_type)
if type(logging_config) == "table" then
-
+
for _, level in ipairs(logging_levels) do
if type(logging_config[level]) == "string" then
local value = logging_config[level];
@@ -82,7 +81,7 @@ function apply_sink_rules(sink_type)
end
end
end
-
+
for _, sink_config in ipairs(logging_config) do
if (type(sink_config) == "table" and sink_config.to == sink_type) then
add_rule(sink_config);
@@ -128,7 +127,7 @@ function get_levels(criteria, set)
end
end
end
-
+
for _, level in ipairs(criteria) do
set[level] = true;
end
@@ -136,14 +135,14 @@ function get_levels(criteria, set)
end
-- Initialize config, etc. --
-function reload_logging()
+local function reload_logging()
local old_sink_types = {};
-
+
for name, sink_maker in pairs(log_sink_types) do
old_sink_types[name] = sink_maker;
log_sink_types[name] = nil;
end
-
+
logger.reset();
local debug_mode = config.get("*", "debug");
@@ -152,15 +151,13 @@ function reload_logging()
default_file_logging = {
{ to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
};
- default_timestamp = "%b %d %H:%M:%S";
logging_config = config.get("*", "log") or default_logging;
-
-
+
for name, sink_maker in pairs(old_sink_types) do
log_sink_types[name] = sink_maker;
end
-
+
prosody.events.fire_event("logging-reloaded");
end
@@ -170,107 +167,98 @@ prosody.events.add_handler("config-reloaded", reload_logging);
--- Definition of built-in logging sinks ---
-- Null sink, must enter log_sink_types *first*
-function log_sink_types.nowhere()
+local function log_to_nowhere()
return function () return false; end;
end
+log_sink_types.nowhere = log_to_nowhere;
+
+local function log_to_file(sink_config, logfile)
+ logfile = logfile or io_open(sink_config.filename, "a+");
+ if not logfile then
+ return log_to_nowhere(sink_config);
+ end
+ local write = logfile.write;
--- Column width for "source" (used by stdout and console)
-local sourcewidth = 20;
+ local timestamps = sink_config.timestamps;
-function log_sink_types.stdout(config)
- local timestamps = config.timestamps;
-
if timestamps == true then
timestamps = default_timestamp; -- Default format
+ elseif timestamps then
+ timestamps = timestamps .. " ";
end
-
- return function (name, level, message, ...)
- sourcewidth = math_max(#name+2, sourcewidth);
- local namelen = #name;
- if timestamps then
- io_write(os_date(timestamps), " ");
- end
- if ... then
- io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
- else
- io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
- end
- end
-end
-do
- local do_pretty_printing = true;
-
- local logstyles = {};
- if do_pretty_printing then
- logstyles["info"] = getstyle("bold");
- logstyles["warn"] = getstyle("bold", "yellow");
- logstyles["error"] = getstyle("bold", "red");
+ if sink_config.buffer_mode ~= false then
+ logfile:setvbuf(sink_config.buffer_mode or "line");
end
- function log_sink_types.console(config)
- -- Really if we don't want pretty colours then just use plain stdout
- if not do_pretty_printing then
- return log_sink_types.stdout(config);
- end
-
- local timestamps = config.timestamps;
- if timestamps == true then
- timestamps = default_timestamp; -- Default format
+ -- Column width for "source" (used by stdout and console)
+ local sourcewidth = sink_config.source_width;
+
+ return function (name, level, message, ...)
+ local n = select('#', ...);
+ if n ~= 0 then
+ local arg = { ... };
+ for i = 1, n do
+ arg[i] = tostring(arg[i]);
+ end
+ message = format(message, unpack(arg, 1, n));
end
- return function (name, level, message, ...)
+ if sourcewidth then
sourcewidth = math_max(#name+2, sourcewidth);
- local namelen = #name;
-
- if timestamps then
- io_write(os_date(timestamps), " ");
- end
- io_write(name, rep(" ", sourcewidth-namelen));
- setstyle(logstyles[level]);
- io_write(level);
- setstyle();
- if ... then
- io_write("\t", format(message, ...), "\n");
- else
- io_write("\t", message, "\n");
- end
+ name = name .. rep(" ", sourcewidth-#name);
+ else
+ name = name .. "\t";
end
+ write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", message, "\n");
end
end
+log_sink_types.file = log_to_file;
-local empty_function = function () end;
-function log_sink_types.file(config)
- local log = config.filename;
- local logfile = io_open(log, "a+");
- if not logfile then
- return empty_function;
+local function log_to_stdout(sink_config)
+ if not sink_config.timestamps then
+ sink_config.timestamps = false;
+ end
+ if sink_config.source_width == nil then
+ sink_config.source_width = 20;
end
- local write, flush = logfile.write, logfile.flush;
+ return log_to_file(sink_config, stdout);
+end
+log_sink_types.stdout = log_to_stdout;
- local timestamps = config.timestamps;
+local do_pretty_printing = true;
- if timestamps == nil or timestamps == true then
- timestamps = default_timestamp; -- Default format
- end
+local logstyles;
+if do_pretty_printing then
+ logstyles = {};
+ logstyles["info"] = getstyle("bold");
+ logstyles["warn"] = getstyle("bold", "yellow");
+ logstyles["error"] = getstyle("bold", "red");
+end
+local function log_to_console(sink_config)
+ -- Really if we don't want pretty colours then just use plain stdout
+ local logstdout = log_to_stdout(sink_config);
+ if not do_pretty_printing then
+ return logstdout;
+ end
return function (name, level, message, ...)
- if timestamps then
- write(logfile, os_date(timestamps), " ");
+ local logstyle = logstyles[level];
+ if logstyle then
+ level = getstring(logstyle, level);
end
- if ... then
- write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
- else
- write(logfile, name, "\t" , level, "\t", message, "\n");
- end
- flush(logfile);
- end;
+ return logstdout(name, level, message, ...);
+ end
end
+log_sink_types.console = log_to_console;
-function register_sink_type(name, sink_maker)
+local function register_sink_type(name, sink_maker)
local old_sink_maker = log_sink_types[name];
log_sink_types[name] = sink_maker;
return old_sink_maker;
end
-return _M;
+return {
+ reload_logging = reload_logging;
+ register_sink_type = register_sink_type;
+}
diff --git a/core/moduleapi.lua b/core/moduleapi.lua
index ed75669b..402c7927 100644
--- a/core/moduleapi.lua
+++ b/core/moduleapi.lua
@@ -1,23 +1,28 @@
-- Prosody IM
-- Copyright (C) 2008-2012 Matthew Wild
-- Copyright (C) 2008-2012 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
local config = require "core.configmanager";
-local modulemanager = require "modulemanager"; -- This is necessary to avoid require loops
local array = require "util.array";
local set = require "util.set";
+local it = require "util.iterators";
local logger = require "util.logger";
local pluginloader = require "util.pluginloader";
local timer = require "util.timer";
+local resolve_relative_path = require"util.paths".resolve_relative_path;
+local measure = require "core.statsmanager".measure;
+local st = require "util.stanza";
local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
local error, setmetatable, type = error, setmetatable, type;
-local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack;
+local ipairs, pairs, select = ipairs, pairs, select;
+local unpack = table.unpack or unpack; --luacheck: ignore 113
local tonumber, tostring = tonumber, tostring;
+local require = require;
local prosody = prosody;
local hosts = prosody.hosts;
@@ -44,14 +49,14 @@ function api:get_host()
end
function api:get_host_type()
- return self.host ~= "*" and hosts[self.host].type or nil;
+ return (self.host == "*" and "global") or hosts[self.host].type or "local";
end
function api:set_global()
self.host = "*";
-- Update the logger
local _log = logger.init("mod_"..self.name);
- self.log = function (self, ...) return _log(...); end;
+ self.log = function (self, ...) return _log(...); end; --luacheck: ignore self
self._log = _log;
self.global = true;
end
@@ -59,8 +64,8 @@ end
function api:add_feature(xmlns)
self:add_item("feature", xmlns);
end
-function api:add_identity(category, type, name)
- self:add_item("identity", {category = category, type = type, name = name});
+function api:add_identity(category, identity_type, name)
+ self:add_item("identity", {category = category, type = identity_type, name = name});
end
function api:add_extension(data)
self:add_item("extension", data);
@@ -71,10 +76,10 @@ function api:has_feature(xmlns)
end
return false;
end
-function api:has_identity(category, type, name)
+function api:has_identity(category, identity_type, name)
for _, id in ipairs(self:get_host_items("identity")) do
- if id.category == category and id.type == type and id.name == name then
- return true;
+ if id.category == category and id.type == identity_type and id.name == name then
+ return true;
end
end
return false;
@@ -90,6 +95,7 @@ function api:hook_object_event(object, event, handler, priority)
end
function api:unhook_object_event(object, event, handler)
+ self.event_handlers:set(object, event, handler, nil);
return object.remove_handler(event, handler);
end
@@ -113,16 +119,30 @@ function api:hook_tag(xmlns, name, handler, priority)
end
api.hook_stanza = api.hook_tag; -- COMPAT w/pre-0.9
+function api:unhook(event, handler)
+ return self:unhook_object_event((hosts[self.host] or prosody).events, event, handler);
+end
+
+function api:wrap_object_event(events_object, event, handler)
+ return self:hook_object_event(assert(events_object.wrappers, "no wrappers"), event, handler);
+end
+
+function api:wrap_event(event, handler)
+ return self:wrap_object_event((hosts[self.host] or prosody).events, event, handler);
+end
+
+function api:wrap_global(event, handler)
+ return self:hook_object_event(prosody.events, event, handler);
+end
+
function api:require(lib)
- local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment);
- if not f then
- f, n = pluginloader.load_code(lib, lib..".lib.lua", self.environment);
- end
+ local f, n = pluginloader.load_code_ext(self.name, lib, "lib.lua", self.environment);
if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
return f();
end
function api:depends(name)
+ local modulemanager = require"core.modulemanager";
if not self.dependencies then
self.dependencies = {};
self:hook("module-reloaded", function (event)
@@ -252,21 +272,21 @@ function api:get_option_array(name, ...)
if value == nil then
return nil;
end
-
+
if type(value) ~= "table" then
return array{ value }; -- Assume any non-list is a single-item list
end
-
+
return array():append(value); -- Clone
end
function api:get_option_set(name, ...)
local value = self:get_option_array(name, ...);
-
+
if value == nil then
return nil;
end
-
+
return set.new(value);
end
@@ -282,6 +302,20 @@ function api:get_option_inherited_set(name, ...)
return value;
end
+function api:get_option_path(name, default, parent)
+ if parent == nil then
+ parent = parent or self:get_directory();
+ elseif prosody.paths[parent] then
+ parent = prosody.paths[parent];
+ end
+ local value = self:get_option_string(name, default);
+ if value == nil then
+ return nil;
+ end
+ return resolve_relative_path(parent, value);
+end
+
+
function api:context(host)
return setmetatable({host=host or "*"}, {__index=self,__newindex=self});
end
@@ -304,15 +338,16 @@ function api:remove_item(key, value)
end
function api:get_host_items(key)
+ local modulemanager = require"core.modulemanager";
local result = modulemanager.get_items(key, self.host) or {};
return result;
end
-function api:handle_items(type, added_cb, removed_cb, existing)
- self:hook("item-added/"..type, added_cb);
- self:hook("item-removed/"..type, removed_cb);
+function api:handle_items(item_type, added_cb, removed_cb, existing)
+ self:hook("item-added/"..item_type, added_cb);
+ self:hook("item-removed/"..item_type, removed_cb);
if existing ~= false then
- for _, item in ipairs(self:get_host_items(type)) do
+ for _, item in ipairs(self:get_host_items(item_type)) do
added_cb({ item = item });
end
end
@@ -343,6 +378,14 @@ function api:send(stanza)
return core_post_stanza(hosts[self.host], stanza);
end
+function api:broadcast(jids, stanza, iter)
+ for jid in (iter or it.values)(jids) do
+ local new_stanza = st.clone(stanza);
+ new_stanza.attr.to = jid;
+ core_post_stanza(hosts[self.host], new_stanza);
+ end
+end
+
function api:add_timer(delay, callback)
return timer.add_task(delay, function (t)
if self.loaded == false then return; end
@@ -356,12 +399,35 @@ function api:get_directory()
end
function api:load_resource(path, mode)
- path = config.resolve_relative_path(self:get_directory(), path);
+ path = resolve_relative_path(self:get_directory(), path);
return io.open(path, mode);
end
-function api:open_store(name, type)
- return storagemanager.open(self.host, name or self.name, type);
+function api:open_store(name, store_type)
+ return require"core.storagemanager".open(self.host, name or self.name, store_type);
+end
+
+function api:measure(name, stat_type)
+ return measure(stat_type, "/"..self.host.."/mod_"..self.name.."/"..name);
+end
+
+function api:measure_object_event(events_object, event_name, stat_name)
+ local m = self:measure(stat_name or event_name, "duration");
+ local function handler(handlers, _event_name, _event_data)
+ local finished = m();
+ local ret = handlers(_event_name, _event_data);
+ finished();
+ return ret;
+ end
+ return self:hook_object_event(events_object, event_name, handler);
+end
+
+function api:measure_event(event_name, stat_name)
+ return self:measure_object_event((hosts[self.host] or prosody).events.wrappers, event_name, stat_name);
+end
+
+function api:measure_global_event(event_name, stat_name)
+ return self:measure_object_event(prosody.events.wrappers, event_name, stat_name);
end
return api;
diff --git a/core/modulemanager.lua b/core/modulemanager.lua
index 4df95069..65542f9a 100644
--- a/core/modulemanager.lua
+++ b/core/modulemanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -13,31 +13,33 @@ local pluginloader = require "util.pluginloader";
local set = require "util.set";
local new_multitable = require "util.multitable".new;
+local api = require "core.moduleapi"; -- Module API container
local hosts = hosts;
local prosody = prosody;
-local pcall, xpcall = pcall, xpcall;
+local xpcall = xpcall;
local setmetatable, rawget = setmetatable, rawget;
local ipairs, pairs, type, tostring, t_insert = ipairs, pairs, type, tostring, table.insert;
local debug_traceback = debug.traceback;
-local unpack, select = unpack, select;
-pcall = function(f, ...)
+local select = select;
+local unpack = table.unpack or unpack; --luacheck: ignore 113
+local pcall = function(f, ...)
local n = select("#", ...);
local params = {...};
return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
end
-local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"};
+local autoload_modules = {prosody.platform, "presence", "message", "iq", "offline", "c2s", "s2s", "s2s_auth_certs"};
local component_inheritable_modules = {"tls", "saslauth", "dialback", "iq", "s2s"};
-- We need this to let modules access the real global namespace
local _G = _G;
-module "modulemanager"
+local _ENV = nil;
-local api = _G.require "core.moduleapi"; -- Module API container
+local load_modules_for_host, load, unload, reload, get_module, get_items, get_modules, is_loaded, module_has_method, call_module_method;
-- [host] = { [module] = module_env }
local modulemap = { ["*"] = {} };
@@ -45,28 +47,28 @@ local modulemap = { ["*"] = {} };
-- Load modules when a host is activated
function load_modules_for_host(host)
local component = config.get(host, "component_module");
-
+
local global_modules_enabled = config.get("*", "modules_enabled");
local global_modules_disabled = config.get("*", "modules_disabled");
local host_modules_enabled = config.get(host, "modules_enabled");
local host_modules_disabled = config.get(host, "modules_disabled");
-
+
if host_modules_enabled == global_modules_enabled then host_modules_enabled = nil; end
if host_modules_disabled == global_modules_disabled then host_modules_disabled = nil; end
-
+
local global_modules = set.new(autoload_modules) + set.new(global_modules_enabled) - set.new(global_modules_disabled);
if component then
global_modules = set.intersection(set.new(component_inheritable_modules), global_modules);
end
local modules = (global_modules + set.new(host_modules_enabled)) - set.new(host_modules_disabled);
-
+
-- COMPAT w/ pre 0.8
if modules:contains("console") then
log("error", "The mod_console plugin has been renamed to mod_admin_telnet. Please update your config.");
modules:remove("console");
modules:add("admin_telnet");
end
-
+
if component then
load(host, component);
end
@@ -84,18 +86,18 @@ end);
local function do_unload_module(host, name)
local mod = get_module(host, name);
if not mod then return nil, "module-not-loaded"; end
-
+
if module_has_method(mod, "unload") then
local ok, err = call_module_method(mod, "unload");
if (not ok) and err then
log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
end
end
-
+
for object, event, handler in mod.module.event_handlers:iter(nil, nil, nil) do
object.remove_handler(event, handler);
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
@@ -117,11 +119,11 @@ local function do_load_module(host, module_name, state)
elseif not hosts[host] and host ~= "*"then
return nil, "unknown-host";
end
-
+
if not modulemap[host] then
modulemap[host] = hosts[host].modules;
end
-
+
if modulemap[host][module_name] then
log("debug", "%s is already loaded for %s, so not loading again", module_name, host);
return nil, "module-already-loaded";
@@ -131,7 +133,7 @@ local function do_load_module(host, module_name, state)
local _log = logger.init(host..":"..module_name);
local host_module_api = setmetatable({
host = host, event_handlers = new_multitable(), items = {};
- _log = _log, log = function (self, ...) return _log(...); end;
+ _log = _log, log = function (self, ...) return _log(...); end; --luacheck: ignore 212/self
},{
__index = modulemap["*"][module_name].module;
});
@@ -147,18 +149,19 @@ local function do_load_module(host, module_name, state)
end
return nil, "global-module-already-loaded";
end
-
+
local _log = logger.init(host..":"..module_name);
local api_instance = setmetatable({ name = module_name, host = host,
- _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable(),
- reloading = not not state, saved_state = state~=true and state or nil }
+ _log = _log, log = function (self, ...) return _log(...); end, --luacheck: ignore 212/self
+ event_handlers = new_multitable(), reloading = not not state,
+ saved_state = state~=true and state or nil }
, { __index = api });
local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
api_instance.environment = pluginenv;
-
+
local mod, err = pluginloader.load_code(module_name, nil, pluginenv);
if not mod then
log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
@@ -316,4 +319,15 @@ function call_module_method(module, method, ...)
end
end
-return _M;
+return {
+ load_modules_for_host = load_modules_for_host;
+ load = load;
+ unload = unload;
+ reload = reload;
+ get_module = get_module;
+ get_items = get_items;
+ get_modules = get_modules;
+ is_loaded = is_loaded;
+ module_has_method = module_has_method;
+ call_module_method = call_module_method;
+};
diff --git a/core/portmanager.lua b/core/portmanager.lua
index 37442a31..60f7731d 100644
--- a/core/portmanager.lua
+++ b/core/portmanager.lua
@@ -9,12 +9,12 @@ local set = require "util.set";
local table = table;
local setmetatable, rawset, rawget = setmetatable, rawset, rawget;
-local type, tonumber, tostring, ipairs, pairs = type, tonumber, tostring, ipairs, pairs;
+local type, tonumber, tostring, ipairs = type, tonumber, tostring, ipairs;
local prosody = prosody;
local fire_event = prosody.events.fire_event;
-module "portmanager";
+local _ENV = nil;
--- Config
@@ -41,7 +41,7 @@ local active_services = multitable.new();
--- Private helpers
-local function error_to_friendly_message(service_name, port, err)
+local function error_to_friendly_message(service_name, port, err) --luacheck: ignore 212/service_name
local friendly_message = err;
if err:match(" in use") then
-- FIXME: Use service_name here
@@ -63,33 +63,14 @@ local function error_to_friendly_message(service_name, port, err)
return friendly_message;
end
-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);
-
-local function duplicate_ssl_config(ssl_config)
- local ssl_config = type(ssl_config) == "table" and ssl_config or {};
-
- local _config = {};
- for k, v in pairs(ssl_config) do
- _config[k] = v;
- end
- return _config;
-end
-
--- Public API
-function activate(service_name)
+local function activate(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).."_";
@@ -105,7 +86,7 @@ function activate(service_name)
or listener.default_interface -- COMPAT w/pre0.9
or default_interfaces
bind_interfaces = set.new(type(bind_interfaces)~="table" and {bind_interfaces} or bind_interfaces);
-
+
local bind_ports = config.get("*", config_prefix.."ports")
or service_info.default_ports
or {service_info.default_port
@@ -115,7 +96,7 @@ function activate(service_name)
local mode, ssl = listener.default_mode or default_mode;
local hooked_ports = {};
-
+
for interface in bind_interfaces do
for port in bind_ports do
local port_number = tonumber(port);
@@ -127,24 +108,15 @@ function activate(service_name)
local err;
-- Create SSL context for this service/port
if service_info.encryption == "ssl" then
- local ssl_config = duplicate_ssl_config((config.get("*", config_prefix.."ssl") and config.get("*", config_prefix.."ssl")[interface])
- or (config.get("*", config_prefix.."ssl") and config.get("*", config_prefix.."ssl")[port])
- or config.get("*", config_prefix.."ssl")
- or (config.get("*", "ssl") and config.get("*", "ssl")[interface])
- or (config.get("*", "ssl") and config.get("*", "ssl")[port])
- or config.get("*", "ssl"));
- -- add default entries for, or override ssl configuration
- if ssl_config and service_info.ssl_config then
- for key, value in pairs(service_info.ssl_config) do
- if not service_info.ssl_config_override and not ssl_config[key] then
- ssl_config[key] = value;
- elseif service_info.ssl_config_override then
- ssl_config[key] = value;
- end
- end
- end
-
- ssl, err = certmanager.create_context(service_info.name.." port "..port, "server", ssl_config);
+ local global_ssl_config = config.get("*", "ssl") or {};
+ local prefix_ssl_config = config.get("*", config_prefix.."ssl") or global_ssl_config;
+ ssl, err = certmanager.create_context(service_info.name.." port "..port, "server",
+ prefix_ssl_config[interface],
+ prefix_ssl_config[port],
+ prefix_ssl_config,
+ service_info.ssl_config or {},
+ global_ssl_config[interface],
+ global_ssl_config[port]);
if not ssl then
log("error", "Error binding encrypted port for %s: %s", service_info.name, error_to_friendly_message(service_name, port_number, err) or "unknown error");
end
@@ -170,8 +142,10 @@ function activate(service_name)
return true;
end
-function deactivate(service_name, service_info)
- for name, interface, port, n, active_service
+local close; -- forward declaration
+
+local function deactivate(service_name, service_info)
+ for name, interface, port, n, active_service --luacheck: ignore 213/name 213/n
in active_services:iter(service_name or service_info and service_info.name, nil, nil, nil) do
if service_info == nil or active_service.service == service_info then
close(interface, port);
@@ -180,7 +154,7 @@ function deactivate(service_name, service_info)
log("info", "Deactivated service '%s'", service_name or service_info.name);
end
-function register_service(service_name, service_info)
+local function register_service(service_name, service_info)
table.insert(services[service_name], service_info);
if not active_services:get(service_name) then
@@ -190,12 +164,12 @@ function register_service(service_name, service_info)
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 function unregister_service(service_name, service_info)
log("debug", "Unregistering service: %s", service_name);
local service_info_list = services[service_name];
for i, service in ipairs(service_info_list) do
@@ -210,12 +184,14 @@ function unregister_service(service_name, service_info)
fire_event("service-removed", { name = service_name, service = service_info });
end
+local get_service_at -- forward declaration
+
function close(interface, port)
- local service, server = get_service_at(interface, port);
+ local service, service_server = get_service_at(interface, port);
if not service then
return false, "port-not-open";
end
- server:close();
+ service_server:close();
active_services:remove(service.name, interface, port);
log("debug", "Removed listening service %s from [%s]:%d", service.name, interface, port);
return true;
@@ -226,16 +202,37 @@ function get_service_at(interface, port)
return data.service, data.server;
end
-function get_service(service_name)
+local function get_service(service_name)
return (services[service_name] or {})[1];
end
-function get_active_services(...)
+local function get_active_services()
return active_services;
end
-function get_registered_services()
+local function get_registered_services()
return services;
end
-return _M;
+-- Event handlers
+
+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);
+
+return {
+ activate = activate;
+ deactivate = deactivate;
+ register_service = register_service;
+ unregister_service = unregister_service;
+ close = close;
+ get_service_at = get_service_at;
+ get_service = get_service;
+ get_active_services = get_active_services;
+ get_registered_services = get_registered_services;
+};
diff --git a/core/rostermanager.lua b/core/rostermanager.lua
index 5e06e3f7..7f6fb82a 100644
--- a/core/rostermanager.lua
+++ b/core/rostermanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -13,21 +13,24 @@ local log = require "util.logger".init("rostermanager");
local pairs = pairs;
local tostring = tostring;
+local type = type;
local hosts = hosts;
-local bare_sessions = bare_sessions;
+local bare_sessions = prosody.bare_sessions;
-local datamanager = require "util.datamanager"
local um_user_exists = require "core.usermanager".user_exists;
local st = require "util.stanza";
+local storagemanager = require "core.storagemanager";
+
+local _ENV = nil;
-module "rostermanager"
+local save_roster; -- forward declaration
-function add_to_roster(session, jid, item)
+local function add_to_roster(session, jid, item)
if session.roster then
local old_item = session.roster[jid];
session.roster[jid] = item;
- if save_roster(session.username, session.host) then
+ if save_roster(session.username, session.host, nil, jid) then
return true;
else
session.roster[jid] = old_item;
@@ -38,11 +41,11 @@ function add_to_roster(session, jid, item)
end
end
-function remove_from_roster(session, jid)
+local function remove_from_roster(session, jid)
if session.roster then
local old_item = session.roster[jid];
session.roster[jid] = nil;
- if save_roster(session.username, session.host) then
+ if save_roster(session.username, session.host, nil, jid) then
return true;
else
session.roster[jid] = old_item;
@@ -53,8 +56,8 @@ function remove_from_roster(session, jid)
end
end
-function roster_push(username, host, jid)
- local roster = jid and jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
+local function roster_push(username, host, jid)
+ local roster = jid and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
if roster then
local item = hosts[host].sessions[username].roster[jid];
local stanza = st.iq({type="set"});
@@ -79,7 +82,22 @@ function roster_push(username, host, jid)
end
end
-function load_roster(username, host)
+local function roster_metadata(roster, err)
+ local metadata = roster[false];
+ if not metadata then
+ metadata = { broken = err or nil };
+ roster[false] = metadata;
+ end
+ if roster.pending and type(roster.pending.subscription) ~= "string" then
+ metadata.pending = roster.pending;
+ roster.pending = nil;
+ elseif not metadata.pending then
+ metadata.pending = {};
+ end
+ return metadata;
+end
+
+local function load_roster(username, host)
local jid = username.."@"..host;
log("debug", "load_roster: asked for: %s", jid);
local user = bare_sessions[jid];
@@ -91,27 +109,28 @@ function load_roster(username, host)
else -- Attempt to load roster for non-loaded user
log("debug", "load_roster: loading for offline user: %s@%s", username, host);
end
- local data, err = datamanager.load(username, host, "roster");
+ local roster_store = storagemanager.open(host, "roster", "keyval");
+ local data, err = roster_store:get(username);
roster = data or {};
if user then user.roster = roster; end
- if not roster[false] then roster[false] = { broken = err or nil }; end
+ roster_metadata(roster, err);
if roster[jid] then
roster[jid] = nil;
log("warn", "roster for %s has a self-contact", jid);
end
if not err then
- hosts[host].events.fire_event("roster-load", username, host, roster);
+ hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
end
return roster, err;
end
-function save_roster(username, host, roster)
+function save_roster(username, host, roster, jid)
if not um_user_exists(username, host) then
log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
return nil;
end
- log("debug", "save_roster: saving roster for %s@%s", username, host);
+ log("debug", "save_roster: saving roster for %s@%s, (%s)", username, host, jid or "all contacts");
if not roster then
roster = hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
--if not roster then
@@ -120,22 +139,24 @@ function save_roster(username, host, roster)
--end
end
if roster then
- local metadata = roster[false];
- if not metadata then
- metadata = {};
- roster[false] = metadata;
- end
+ local metadata = roster_metadata(roster);
if metadata.version ~= true then
metadata.version = (metadata.version or 0) + 1;
end
- if roster[false].broken then return nil, "Not saving broken roster" end
- return datamanager.store(username, host, "roster", roster);
+ if metadata.broken then return nil, "Not saving broken roster" end
+ if jid == nil then
+ local roster_store = storagemanager.open(host, "roster", "keyval");
+ return roster_store:set(username, roster);
+ else
+ local roster_store = storagemanager.open(host, "roster", "map");
+ return roster_store:set_keys(username, { [false] = metadata, [jid] = roster[jid] or roster_store.remove });
+ end
end
log("warn", "save_roster: user had no roster to save");
return nil;
end
-function process_inbound_subscription_approval(username, host, jid)
+local function process_inbound_subscription_approval(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
if item and item.ask then
@@ -145,11 +166,13 @@ function process_inbound_subscription_approval(username, host, jid)
item.subscription = "both";
end
item.ask = nil;
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
-function process_inbound_subscription_cancellation(username, host, jid)
+local is_contact_pending_out -- forward declaration
+
+local function process_inbound_subscription_cancellation(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
local changed = nil;
@@ -167,16 +190,18 @@ function process_inbound_subscription_cancellation(username, host, jid)
end
end
if changed then
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
-function process_inbound_unsubscribe(username, host, jid)
+local is_contact_pending_in -- forward declaration
+
+local function process_inbound_unsubscribe(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
local changed = nil;
if is_contact_pending_in(username, host, jid) then
- roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
+ roster[false].pending[jid] = nil;
changed = true;
end
if item then
@@ -189,7 +214,7 @@ function process_inbound_unsubscribe(username, host, jid)
end
end
if changed then
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
@@ -198,13 +223,13 @@ local function _get_online_roster_subscription(jidA, jidB)
local item = user and (user.roster[jidB] or { subscription = "none" });
return item and item.subscription;
end
-function is_contact_subscribed(username, host, jid)
+local function is_contact_subscribed(username, host, jid)
do
local selfjid = username.."@"..host;
- local subscription = _get_online_roster_subscription(selfjid, jid);
- if subscription then return (subscription == "both" or subscription == "from"); end
- local subscription = _get_online_roster_subscription(jid, selfjid);
- if subscription then return (subscription == "both" or subscription == "to"); end
+ local user_subscription = _get_online_roster_subscription(selfjid, jid);
+ if user_subscription then return (user_subscription == "both" or user_subscription == "from"); end
+ local contact_subscription = _get_online_roster_subscription(jid, selfjid);
+ if contact_subscription then return (contact_subscription == "both" or contact_subscription == "to"); end
end
local roster, err = load_roster(username, host);
local item = roster[jid];
@@ -213,24 +238,23 @@ end
function is_contact_pending_in(username, host, jid)
local roster = load_roster(username, host);
- return roster.pending and roster.pending[jid];
+ return roster[false].pending[jid];
end
-function set_contact_pending_in(username, host, jid, pending)
+local function set_contact_pending_in(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
if item and (item.subscription == "from" or item.subscription == "both") then
return; -- false
end
- if not roster.pending then roster.pending = {}; end
- roster.pending[jid] = true;
- return save_roster(username, host, roster);
+ roster[false].pending[jid] = true;
+ return save_roster(username, host, roster, jid);
end
function is_contact_pending_out(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
return item and item.ask;
end
-function set_contact_pending_out(username, host, jid) -- subscribe
+local function set_contact_pending_out(username, host, jid) -- subscribe
local roster = load_roster(username, host);
local item = roster[jid];
if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
@@ -242,9 +266,9 @@ function set_contact_pending_out(username, host, jid) -- subscribe
end
item.ask = "subscribe";
log("debug", "set_contact_pending_out: saving roster; set %s@%s.roster[%q].ask=subscribe", username, host, jid);
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
-function unsubscribe(username, host, jid)
+local function unsubscribe(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
if not item then return false; end
@@ -257,9 +281,9 @@ function unsubscribe(username, host, jid)
elseif item.subscription == "to" then
item.subscription = "none";
end
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
-function subscribed(username, host, jid)
+local function subscribed(username, host, jid)
if is_contact_pending_in(username, host, jid) then
local roster = load_roster(username, host);
local item = roster[jid];
@@ -272,38 +296,37 @@ function subscribed(username, host, jid)
else -- subscription == to
item.subscription = "both";
end
- roster.pending[jid] = nil;
- -- TODO maybe remove roster.pending if empty
- return save_roster(username, host, roster);
+ roster[false].pending[jid] = nil;
+ return save_roster(username, host, roster, jid);
end -- TODO else implement optional feature pre-approval (ask = subscribed)
end
-function unsubscribed(username, host, jid)
+local function unsubscribed(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
local pending = is_contact_pending_in(username, host, jid);
if pending then
- roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
+ roster[false].pending[jid] = nil;
end
- local subscribed;
+ local is_subscribed;
if item then
if item.subscription == "from" then
item.subscription = "none";
- subscribed = true;
+ is_subscribed = true;
elseif item.subscription == "both" then
item.subscription = "to";
- subscribed = true;
+ is_subscribed = true;
end
end
- local success = (pending or subscribed) and save_roster(username, host, roster);
+ local success = (pending or is_subscribed) and save_roster(username, host, roster, jid);
return success, pending, subscribed;
end
-function process_outbound_subscription_request(username, host, jid)
+local function process_outbound_subscription_request(username, host, jid)
local roster = load_roster(username, host);
local item = roster[jid];
if item and (item.subscription == "none" or item.subscription == "from") then
item.ask = "subscribe";
- return save_roster(username, host, roster);
+ return save_roster(username, host, roster, jid);
end
end
@@ -318,4 +341,22 @@ end]]
-return _M;
+return {
+ add_to_roster = add_to_roster;
+ remove_from_roster = remove_from_roster;
+ roster_push = roster_push;
+ load_roster = load_roster;
+ save_roster = save_roster;
+ process_inbound_subscription_approval = process_inbound_subscription_approval;
+ process_inbound_subscription_cancellation = process_inbound_subscription_cancellation;
+ process_inbound_unsubscribe = process_inbound_unsubscribe;
+ is_contact_subscribed = is_contact_subscribed;
+ is_contact_pending_in = is_contact_pending_in;
+ set_contact_pending_in = set_contact_pending_in;
+ is_contact_pending_out = is_contact_pending_out;
+ set_contact_pending_out = set_contact_pending_out;
+ unsubscribe = unsubscribe;
+ subscribed = subscribed;
+ unsubscribed = unsubscribed;
+ process_outbound_subscription_request = process_outbound_subscription_request;
+};
diff --git a/core/s2smanager.lua b/core/s2smanager.lua
index fb5c4299..a8d399d2 100644
--- a/core/s2smanager.lua
+++ b/core/s2smanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -22,16 +22,16 @@ prosody.incoming_s2s = incoming_s2s;
local incoming_s2s = incoming_s2s;
local fire_event = prosody.events.fire_event;
-module "s2smanager"
+local _ENV = nil;
-function new_incoming(conn)
+local function new_incoming(conn)
local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
session.log = logger_init("s2sin"..tostring(session):match("[a-f0-9]+$"));
incoming_s2s[session] = true;
return session;
end
-function new_outgoing(from_host, to_host)
+local function new_outgoing(from_host, to_host)
local host_session = { to_host = to_host, from_host = from_host, host = from_host,
notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
hosts[from_host].s2sout[to_host] = host_session;
@@ -49,11 +49,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, reason)
- local log = session.log or log;
+local function retire_session(session, reason)
+ local log = session.log or log; --luacheck: ignore 431/log
for k in pairs(session) do
if k ~= "log" and k ~= "id" and k ~= "conn" then
session[k] = nil;
@@ -68,17 +68,17 @@ function retire_session(session, reason)
return setmetatable(session, resting_session);
end
-function destroy_session(session, reason)
+local function destroy_session(session, reason)
if session.destroyed then return; end
(session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
-
+
if session.direction == "outgoing" then
hosts[session.from_host].s2sout[session.to_host] = nil;
session:bounce_sendq(reason);
elseif session.direction == "incoming" then
incoming_s2s[session] = nil;
end
-
+
local event_data = { session = session, reason = reason };
if session.type == "s2sout" then
fire_event("s2sout-destroyed", event_data);
@@ -91,9 +91,15 @@ function destroy_session(session, reason)
hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
end
end
-
+
retire_session(session, reason); -- Clean session until it is GC'd
return true;
end
-return _M;
+return {
+ incoming_s2s = incoming_s2s;
+ new_incoming = new_incoming;
+ new_outgoing = new_outgoing;
+ retire_session = retire_session;
+ destroy_session = destroy_session;
+};
diff --git a/core/sessionmanager.lua b/core/sessionmanager.lua
index 4b014d18..6aa0a4f0 100644
--- a/core/sessionmanager.lua
+++ b/core/sessionmanager.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -10,8 +10,8 @@ local tostring, setmetatable = tostring, setmetatable;
local pairs, next= pairs, next;
local hosts = hosts;
-local full_sessions = full_sessions;
-local bare_sessions = bare_sessions;
+local full_sessions = prosody.full_sessions;
+local bare_sessions = prosody.bare_sessions;
local logger = require "util.logger";
local log = logger.init("sessionmanager");
@@ -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,14 +37,19 @@ 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]+$");
session.log = logger.init(conn_name);
-
+
return session;
end
@@ -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;
@@ -67,25 +72,26 @@ function retire_session(session)
function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); return false; end
function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
+ session.thread = { run = function (_, data) return session.data(data) end };
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
-
+
-- Remove session/resource from user's session list
if session.full_jid then
local host_session = hosts[session.host];
-
+
-- Allow plugins to prevent session destruction
if host_session.events.fire_event("pre-resource-unbind", {session=session, error=err}) then
return;
end
-
+
host_session.sessions[session.username].sessions[session.resource] = nil;
full_sessions[session.full_jid] = nil;
-
+
if not next(host_session.sessions[session.username].sessions) then
log("debug", "All resources of %s are now offline", session.username);
host_session.sessions[session.username] = nil;
@@ -94,11 +100,11 @@ function destroy_session(session, err)
host_session.events.fire_event("resource-unbind", {session=session, error=err});
end
-
+
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;
@@ -111,15 +117,25 @@ 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", "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 };
+ if hosts[session.host].events.fire_event("pre-resource-bind", event_payload) == false then
+ local err = event_payload.error;
+ if err then return nil, err.type, err.condition, err.text; end
+ return nil, "cancel", "not-allowed";
+ else
+ -- In case a plugin wants to poke at it
+ resource = event_payload.resource;
+ end
+
resource = resourceprep(resource);
resource = resource ~= "" and resource or uuid_generate();
--FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
-
+
if not hosts[session.host].sessions[session.username] then
local sessions = { sessions = {} };
hosts[session.host].sessions[session.username] = sessions;
@@ -156,12 +172,12 @@ function bind_resource(session, resource)
end
end
end
-
+
session.resource = resource;
session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
hosts[session.host].sessions[session.username].sessions[resource] = session;
full_sessions[session.full_jid] = session;
-
+
local err;
session.roster, err = rm_load_roster(session.username, session.host);
if err then
@@ -176,18 +192,18 @@ function bind_resource(session, resource)
session.log("error", "Roster loading failed: %s", err);
return nil, "cancel", "internal-server-error", "Error loading roster";
end
-
+
hosts[session.host].events.fire_event("resource-bind", {session=session});
-
+
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;
@@ -197,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;
@@ -212,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;
+};
diff --git a/core/stanza_router.lua b/core/stanza_router.lua
index a2c7b396..55c94bf4 100644
--- a/core/stanza_router.lua
+++ b/core/stanza_router.lua
@@ -1,7 +1,7 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
@@ -30,7 +30,7 @@ deprecated_warning"core_process_stanza";
deprecated_warning"core_route_stanza";
local valid_stanzas = { message = true, presence = true, iq = true };
-local function handle_unhandled_stanza(host, origin, stanza)
+local function handle_unhandled_stanza(host, origin, stanza) --luacheck: ignore 212/host
local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns or "jabber:client", origin.type;
if xmlns == "jabber:client" and valid_stanzas[name] then
-- A normal stanza
@@ -46,7 +46,7 @@ local function handle_unhandled_stanza(host, origin, stanza)
if origin.send then
origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
end
- elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features
+ else
log("warn", "Unhandled %s stream element or stanza: %s; xmlns=%s: %s", origin_type, name, xmlns, tostring(stanza)); -- we didn't handle it
origin:close("unsupported-stanza-type");
end
@@ -199,7 +199,7 @@ function core_route_stanza(origin, stanza)
-- Auto-detect origin if not specified
origin = origin or hosts[from_host];
if not origin then return false; end
-
+
if hosts[host] then
-- old stanza routing code removed
core_post_stanza(origin, stanza);
@@ -221,6 +221,8 @@ function core_route_stanza(origin, stanza)
end
end
end
+
+--luacheck: ignore 122/prosody
prosody.core_process_stanza = core_process_stanza;
prosody.core_post_stanza = core_post_stanza;
prosody.core_route_stanza = core_route_stanza;
diff --git a/core/statsmanager.lua b/core/statsmanager.lua
new file mode 100644
index 00000000..7771a2b3
--- /dev/null
+++ b/core/statsmanager.lua
@@ -0,0 +1,72 @@
+
+local stats = require "util.statistics".new();
+local config = require "core.configmanager";
+local log = require "util.logger".init("stats");
+local timer = require "util.timer";
+local fire_event = prosody.events.fire_event;
+
+local stats_config = config.get("*", "statistics_interval");
+local stats_interval = tonumber(stats_config);
+if stats_config and not stats_interval then
+ log("error", "Invalid 'statistics_interval' setting, statistics will be disabled");
+end
+
+local measure, collect;
+local latest_stats = {};
+local changed_stats = {};
+local stats_extra = {};
+
+if stats_interval then
+ log("debug", "Statistics collection is enabled every %d seconds", stats_interval);
+ function measure(type, name)
+ local f = assert(stats[type], "unknown stat type: "..type);
+ return f(name);
+ end
+
+ local mark_collection_start = measure("times", "stats.collection");
+ local mark_processing_start = measure("times", "stats.processing");
+
+ function collect()
+ local mark_collection_done = mark_collection_start();
+ fire_event("stats-update");
+ changed_stats, stats_extra = {}, {};
+ for stat_name, getter in pairs(stats.get_stats()) do
+ local type, value, extra = getter();
+ local old_value = latest_stats[stat_name];
+ latest_stats[stat_name] = value;
+ if value ~= old_value then
+ changed_stats[stat_name] = value;
+ end
+ if extra then
+ stats_extra[stat_name] = extra;
+ end
+ end
+ mark_collection_done();
+ local mark_processing_done = mark_processing_start();
+ fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra });
+ mark_processing_done();
+ return stats_interval;
+ end
+
+ timer.add_task(stats_interval, collect);
+ prosody.events.add_handler("server-started", function () collect() end, -1);
+else
+ log("debug", "Statistics collection is disabled");
+ -- nop
+ function measure()
+ return measure;
+ end
+ function collect()
+ end
+end
+
+return {
+ measure = measure;
+ collect = collect;
+ get_stats = function ()
+ return latest_stats, changed_stats, stats_extra;
+ end;
+ get = function (name)
+ return latest_stats[name], stats_extra[name];
+ end;
+};
diff --git a/core/storagemanager.lua b/core/storagemanager.lua
index 1c82af6d..4f04e594 100644
--- a/core/storagemanager.lua
+++ b/core/storagemanager.lua
@@ -1,5 +1,5 @@
-local error, type, pairs = error, type, pairs;
+local type, pairs = type, pairs;
local setmetatable = setmetatable;
local config = require "core.configmanager";
@@ -11,11 +11,10 @@ local log = require "util.logger".init("storagemanager");
local prosody = prosody;
-module("storagemanager")
+local _ENV = nil;
local olddm = {}; -- maintain old datamanager, for backwards compatibility
for k,v in pairs(datamanager) do olddm[k] = v; end
-_M.olddm = olddm;
local null_storage_method = function () return false, "no data storage active"; end
local null_storage_driver = setmetatable(
@@ -23,7 +22,7 @@ local null_storage_driver = setmetatable(
name = "null",
open = function (self) return self; end
}, {
- __index = function (self, method)
+ __index = function (self, method) --luacheck: ignore 212
return null_storage_method;
end
}
@@ -31,13 +30,13 @@ local null_storage_driver = setmetatable(
local stores_available = multitable.new();
-function initialize_host(host)
+local function initialize_host(host)
local host_session = hosts[host];
host_session.events.add_handler("item-added/storage-provider", function (event)
local item = event.item;
stores_available:set(host, item.name, item);
end);
-
+
host_session.events.add_handler("item-removed/storage-provider", function (event)
local item = event.item;
stores_available:set(host, item.name, nil);
@@ -45,7 +44,7 @@ function initialize_host(host)
end
prosody.events.add_handler("host-activated", initialize_host, 101);
-function load_driver(host, driver_name)
+local function load_driver(host, driver_name)
if driver_name == "null" then
return null_storage_driver;
end
@@ -58,8 +57,28 @@ function load_driver(host, driver_name)
return stores_available:get(host, driver_name);
end
-function get_driver(host, store)
- local storage = config.get(host, "storage");
+local function get_storage_config(host)
+ -- COMPAT w/ unreleased Prosody 0.10 and the once-experimental mod_storage_sql2 in peoples' config files
+ local storage_config = config.get(host, "storage");
+ local found_sql2;
+ if storage_config == "sql2" then
+ storage_config, found_sql2 = "sql", true;
+ elseif type(storage_config) == "table" then
+ for store_name, driver_name in pairs(storage_config) do
+ if driver_name == "sql2" then
+ storage_config[store_name] = "sql";
+ found_sql2 = true;
+ end
+ end
+ end
+ if found_sql2 then
+ log("error", "The temporary 'sql2' storage module has now been renamed to 'sql', please update your config file: https://prosody.im/doc/modules/mod_storage_sql2");
+ end
+ return storage_config;
+end
+
+local function get_driver(host, store)
+ local storage = get_storage_config(host);
local driver_name;
local option_type = type(storage);
if option_type == "string" then
@@ -70,7 +89,7 @@ function get_driver(host, store)
if not driver_name then
driver_name = config.get(host, "default_storage") or "internal";
end
-
+
local driver = load_driver(host, driver_name);
if not driver then
log("warn", "Falling back to null driver for %s storage on %s", store, host);
@@ -80,11 +99,64 @@ function get_driver(host, store)
return driver, driver_name;
end
+local map_shim_mt = {
+ __index = {
+ get = function(self, username, key)
+ local ret, err = self.keyval_store:get(username);
+ if ret == nil then return nil, err end
+ return ret[key];
+ end;
+ set = function(self, username, key, data)
+ local current, err = self.keyval_store:get(username);
+ if current == nil then
+ if err then
+ return nil, err;
+ else
+ current = {};
+ end
+ end
+ current[key] = data;
+ return self.keyval_store:set(username, current);
+ end;
+ set_keys = function (self, username, keydatas)
+ local current, err = self.keyval_store:get(username);
+ if current == nil then
+ if err then
+ return nil, err;
+ else
+ current = keydatas;
+ end
+ else
+ for k,v in pairs(keydatas) do
+ if v == self.remove then v = nil; end
+ current[k] = v;
+ end
+ end
+ return self.keyval_store:set(username, current);
+ end;
+ remove = {};
+ };
+}
+
+local open;
+
+local function create_map_shim(host, store)
+ local keyval_store, err = open(host, store, "keyval");
+ if keyval_store == nil then return nil, err end
+ return setmetatable({
+ keyval_store = keyval_store;
+ }, map_shim_mt);
+end
+
function open(host, store, typ)
local driver, driver_name = get_driver(host, store);
local ret, err = driver:open(store, typ);
if not ret then
if err == "unsupported-store" then
+ if typ == "map" then -- Use shim on top of keyval store
+ log("debug", "map storage driver unavailable, using shim on top of keyval store.");
+ return create_map_shim(host, store);
+ end
log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver",
driver_name, store, typ or "<nil>");
ret = null_storage_driver;
@@ -94,14 +166,19 @@ function open(host, store, typ)
return ret, err;
end
-function purge(user, host)
- local storage = config.get(host, "storage");
+local function purge(user, host)
+ local storage = get_storage_config(host);
if type(storage) == "table" then
-- multiple storage backends in use that we need to purge
local purged = {};
- for store, driver in pairs(storage) do
- if not purged[driver] then
- purged[driver] = get_driver(host, store):purge(user);
+ for store, driver_name in pairs(storage) do
+ if not purged[driver_name] then
+ local driver = get_driver(host, store);
+ if driver.purge then
+ purged[driver_name] = driver:purge(user);
+ else
+ log("warn", "Storage driver %s does not support removing all user data, you may need to delete it manually", driver_name);
+ end
end
end
end
@@ -132,4 +209,12 @@ function datamanager.purge(username, host)
return purge(username, host);
end
-return _M;
+return {
+ initialize_host = initialize_host;
+ load_driver = load_driver;
+ get_driver = get_driver;
+ open = open;
+ purge = purge;
+
+ olddm = olddm;
+};
diff --git a/core/usermanager.lua b/core/usermanager.lua
index 08343bee..d5132662 100644
--- a/core/usermanager.lua
+++ b/core/usermanager.lua
@@ -10,7 +10,6 @@ local modulemanager = require "core.modulemanager";
local log = require "util.logger".init("usermanager");
local type = type;
local ipairs = ipairs;
-local pairs = pairs;
local jid_bare = require "util.jid".bare;
local jid_prep = require "util.jid".prep;
local config = require "core.configmanager";
@@ -24,22 +23,22 @@ local setmetatable = setmetatable;
local default_provider = "internal_plain";
-module "usermanager"
+local _ENV = nil;
-function new_null_provider()
+local function new_null_provider()
local function dummy() return nil, "method not implemented"; end;
local function dummy_get_sasl_handler() return sasl_new(nil, {}); end
return setmetatable({name = "null", get_sasl_handler = dummy_get_sasl_handler}, {
- __index = function(self, method) return dummy; end
+ __index = function(self, method) return dummy; end --luacheck: ignore 212
});
end
local provider_mt = { __index = new_null_provider() };
-function initialize_host(host)
+local function initialize_host(host)
local host_session = hosts[host];
if host_session.type ~= "local" then return; end
-
+
host_session.events.add_handler("item-added/auth-provider", function (event)
local provider = event.item;
local auth_provider = config.get(host, "authentication") or default_provider;
@@ -51,7 +50,7 @@ function initialize_host(host)
host_session.users = setmetatable(provider, provider_mt);
end
if host_session.users ~= nil and host_session.users.name ~= nil then
- log("debug", "host '%s' now set to use user provider '%s'", host, host_session.users.name);
+ log("debug", "Host '%s' now set to use user provider '%s'", host, host_session.users.name);
end
end);
host_session.events.add_handler("item-removed/auth-provider", function (event)
@@ -69,87 +68,98 @@ function initialize_host(host)
end;
prosody.events.add_handler("host-activated", initialize_host, 100);
-function test_password(username, host, password)
+local function test_password(username, host, password)
return hosts[host].users.test_password(username, password);
end
-function get_password(username, host)
+local function get_password(username, host)
return hosts[host].users.get_password(username);
end
-function set_password(username, password, host)
+local function set_password(username, password, host)
return hosts[host].users.set_password(username, password);
end
-function user_exists(username, host)
+local function user_exists(username, host)
+ if hosts[host].sessions[username] then return true; end
return hosts[host].users.user_exists(username);
end
-function create_user(username, password, host)
+local function create_user(username, password, host)
return hosts[host].users.create_user(username, password);
end
-function delete_user(username, host)
+local function delete_user(username, host)
local ok, err = hosts[host].users.delete_user(username);
if not ok then return nil, err; end
prosody.events.fire_event("user-deleted", { username = username, host = host });
return storagemanager.purge(username, host);
end
-function users(host)
+local function users(host)
return hosts[host].users.users();
end
-function get_sasl_handler(host, session)
+local function get_sasl_handler(host, session)
return hosts[host].users.get_sasl_handler(session);
end
-function get_provider(host)
+local function get_provider(host)
return hosts[host].users;
end
-function is_admin(jid, host)
+local function is_admin(jid, host)
if host and not hosts[host] then return false; end
if type(jid) ~= "string" then return false; end
- local is_admin;
jid = jid_bare(jid);
host = host or "*";
-
+
local host_admins = config.get(host, "admins");
local global_admins = config.get("*", "admins");
-
+
if host_admins and host_admins ~= global_admins then
if type(host_admins) == "table" then
for _,admin in ipairs(host_admins) do
if jid_prep(admin) == jid then
- is_admin = true;
- break;
+ return true;
end
end
elseif host_admins then
log("error", "Option 'admins' for host '%s' is not a list", host);
end
end
-
- if not is_admin and global_admins then
+
+ if global_admins then
if type(global_admins) == "table" then
for _,admin in ipairs(global_admins) do
if jid_prep(admin) == jid then
- is_admin = true;
- break;
+ return true;
end
end
elseif global_admins then
log("error", "Global option 'admins' is not a list");
end
end
-
+
-- Still not an admin, check with auth provider
- if not is_admin and host ~= "*" and hosts[host].users and hosts[host].users.is_admin then
- is_admin = hosts[host].users.is_admin(jid);
+ if host ~= "*" and hosts[host].users and hosts[host].users.is_admin then
+ return hosts[host].users.is_admin(jid);
end
- return is_admin or false;
+ return false;
end
-return _M;
+return {
+ new_null_provider = new_null_provider;
+ initialize_host = initialize_host;
+ test_password = test_password;
+ get_password = get_password;
+ set_password = set_password;
+ user_exists = user_exists;
+ create_user = create_user;
+ delete_user = delete_user;
+ users = users;
+ get_sasl_handler = get_sasl_handler;
+ get_provider = get_provider;
+ is_admin = is_admin;
+};