# Date 1512100463 -3600
# Node ID 4e6710b4f873f4a07b4b3fd39431ee0263a90f8c
# Parent 293dbb08427b65768f0f42a585ba10b8a0c6bffe# Parent 3d21c63ec03f82145bea3c71f40650444b07c639
Merge 0.10->trunk
diff -r 3d21c63ec03f -r 4e6710b4f873 .hgignore
--- a/.hgignore Fri Dec 01 04:50:20 2017 +0100
+++ b/.hgignore Fri Dec 01 04:54:23 2017 +0100
@@ -15,7 +15,6 @@
*.rej
*.save
*~
-*.report
*.o
*.so
*.install
@@ -27,3 +26,6 @@
*.exp
*.lib
*.obj
+luacov.report.out
+luacov.report.out.index
+luacov.stats.out
\ No newline at end of file
diff -r 3d21c63ec03f -r 4e6710b4f873 .luacheckrc
--- a/.luacheckrc Fri Dec 01 04:50:20 2017 +0100
+++ b/.luacheckrc Fri Dec 01 04:54:23 2017 +0100
@@ -13,6 +13,10 @@
read_globals = { "prosody", "hosts" };
globals = { "prosody.hosts.?", "hosts.?" };
}
+files["util/"] = {
+ -- Ignore unwrapped license text
+ max_comment_line_length = false;
+}
files["plugins/"] = {
read_globals = {
-- Module instance
@@ -89,16 +93,6 @@
"module.environment",
};
}
-files["tests/"] = {
- read_globals = {
- "testlib_new_env",
- "assert_equal",
- "assert_table",
- "assert_function",
- "assert_string",
- "assert_boolean",
- "assert_is",
- "assert_is_not",
- "runtest",
- };
+files["spec/"] = {
+ std = "+busted"
}
diff -r 3d21c63ec03f -r 4e6710b4f873 CHANGES
--- a/CHANGES Fri Dec 01 04:50:20 2017 +0100
+++ b/CHANGES Fri Dec 01 04:54:23 2017 +0100
@@ -1,3 +1,17 @@
+trunk
+=====
+
+**YYYY-MM-DD**
+
+New features
+------------
+
+- Rewritten more extensible MUC module
+ - Store inactive rooms to disk
+ - Store rooms to disk on shutdown
+- mod\_pep\_plus
+- Asynchronous operations
+
0.10.0
=====================
diff -r 3d21c63ec03f -r 4e6710b4f873 HACKERS
--- a/HACKERS Fri Dec 01 04:50:20 2017 +0100
+++ b/HACKERS Fri Dec 01 04:54:23 2017 +0100
@@ -2,11 +2,11 @@
This project accepts and *encourages* contributions. If you would like to get
involved you can join us on our mailing list and discussion rooms. More
-information on these at http://prosody.im/discuss
+information on these at https://prosody.im/discuss
Patches are welcome, though before sending we would appreciate if you read
docs/coding_style.txt for guidelines on how to format your code, and other tips.
-Documentation for developers can be found at http://prosody.im/doc/developers
+Documentation for developers can be found at https://prosody.im/doc/developers
Have fun :)
diff -r 3d21c63ec03f -r 4e6710b4f873 INSTALL
--- a/INSTALL Fri Dec 01 04:50:20 2017 +0100
+++ b/INSTALL Fri Dec 01 04:54:23 2017 +0100
@@ -1,5 +1,5 @@
(This file was created from
-http://prosody.im/doc/installing_from_source on 2013-03-31)
+https://prosody.im/doc/installing_from_source on 2013-03-31)
====== Installing from source ======
==== Dependencies ====
diff -r 3d21c63ec03f -r 4e6710b4f873 Makefile
--- a/Makefile Fri Dec 01 04:50:20 2017 +0100
+++ b/Makefile Fri Dec 01 04:54:23 2017 +0100
@@ -65,8 +65,7 @@
$(MAKE) clean -C util-src
test:
- cd tests && $(RUNWITH) test.lua 0
- # Skipping: cd tests && RUNWITH=$(RUNWITH) ./test_util_json.sh
+ busted --lua=$(RUNWITH)
util/%.so:
$(MAKE) install -C util-src
diff -r 3d21c63ec03f -r 4e6710b4f873 README
--- a/README Fri Dec 01 04:50:20 2017 +0100
+++ b/README Fri Dec 01 04:54:23 2017 +0100
@@ -9,29 +9,29 @@
## Useful links
-Homepage: http://prosody.im/
-Download: http://prosody.im/download
-Documentation: http://prosody.im/doc/
+Homepage: https://prosody.im/
+Download: https://prosody.im/download
+Documentation: https://prosody.im/doc/
Jabber/XMPP Chat:
Address:
prosody@conference.prosody.im
Web interface:
- http://prosody.im/webchat
+ https://prosody.im/webchat
Mailing lists:
User support and discussion:
- http://groups.google.com/group/prosody-users
+ https://groups.google.com/group/prosody-users
Development discussion:
- http://groups.google.com/group/prosody-dev
+ https://groups.google.com/group/prosody-dev
Issue tracker changes:
- http://groups.google.com/group/prosody-issues
+ https://groups.google.com/group/prosody-issues
## Installation
See the accompanying INSTALL file for help on building Prosody from source. Alternatively
-see our guide at http://prosody.im/doc/install
+see our guide at https://prosody.im/doc/install
diff -r 3d21c63ec03f -r 4e6710b4f873 certs/localhost.cnf
--- a/certs/localhost.cnf Fri Dec 01 04:50:20 2017 +0100
+++ b/certs/localhost.cnf Fri Dec 01 04:54:23 2017 +0100
@@ -11,7 +11,7 @@
[distinguished_name]
countryName = GB
organizationName = Prosody IM
-organizationalUnitName = http://prosody.im/doc/certificates
+organizationalUnitName = https://prosody.im/doc/certificates
commonName = Example certificate
[req]
diff -r 3d21c63ec03f -r 4e6710b4f873 core/configmanager.lua
--- a/core/configmanager.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/core/configmanager.lua Fri Dec 01 04:54:23 2017 +0100
@@ -7,8 +7,8 @@
--
local _G = _G;
-local setmetatable, rawget, rawset, io, error, dofile, type, pairs, table =
- setmetatable, rawget, rawset, io, error, dofile, type, pairs, table;
+local setmetatable, rawget, rawset, io, error, dofile, type, pairs =
+ setmetatable, rawget, rawset, io, error, dofile, type, pairs;
local format, math_max = string.format, math.max;
local fire_event = prosody and prosody.events.fire_event or function () end;
@@ -27,7 +27,7 @@
_M.resolve_relative_path = resolve_relative_path; -- COMPAT
-local parsers = {};
+local parser = nil;
local config_mt = { __index = function (t, _) return rawget(t, "*"); end};
local config = setmetatable({ ["*"] = { } }, config_mt);
@@ -77,11 +77,11 @@
function _M.load(filename, config_format)
config_format = config_format or filename:match("%w+$");
- if parsers[config_format] and parsers[config_format].load then
+ if config_format == "lua" then
local f, err = io.open(filename);
if f then
local new_config = setmetatable({ ["*"] = { } }, config_mt);
- local ok, err = parsers[config_format].load(f:read("*a"), filename, new_config);
+ local ok, err = parser.load(f:read("*a"), filename, new_config);
f:close();
if ok then
config = new_config;
@@ -103,26 +103,11 @@
end
end
-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 config_format in pairs(parsers) do
- table.insert(p, config_format);
- end
- return p;
-end
-
-- Built-in Lua parser
do
local pcall = _G.pcall;
- parsers.lua = {};
- function parsers.lua.load(data, config_file, config_table)
+ parser = {};
+ function parser.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({
@@ -211,7 +196,7 @@
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);
+ local ret, err = parser.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
diff -r 3d21c63ec03f -r 4e6710b4f873 core/hostmanager.lua
--- a/core/hostmanager.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/core/hostmanager.lua Fri Dec 01 04:54:23 2017 +0100
@@ -12,8 +12,6 @@
local disco_items = require "util.multitable".new();
local NULL = {};
-local jid_split = require "util.jid".split;
-
local log = require "util.logger".init("hostmanager");
local hosts = prosody.hosts;
@@ -24,7 +22,7 @@
local incoming_s2s = _G.prosody.incoming_s2s;
local core_route_stanza = _G.prosody.core_route_stanza;
-local pairs, select, rawget = pairs, select, rawget;
+local pairs, rawget = pairs, rawget;
local tostring, type = tostring, type;
local setmetatable = setmetatable;
@@ -71,13 +69,6 @@
prosody_events.add_handler("server-starting", load_enabled_hosts);
local function host_send(stanza)
- 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));
- return;
- end
core_route_stanza(nil, stanza);
end
diff -r 3d21c63ec03f -r 4e6710b4f873 core/moduleapi.lua
--- a/core/moduleapi.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/core/moduleapi.lua Fri Dec 01 04:54:23 2017 +0100
@@ -20,9 +20,10 @@
local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
local error, setmetatable, type = error, setmetatable, type;
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 pack = table.pack or function(...) return {n=select("#",...), ...}; end -- table.pack is only in 5.2
+local unpack = table.unpack or unpack; --luacheck: ignore 113 -- renamed in 5.2
local prosody = prosody;
local hosts = prosody.hosts;
@@ -381,11 +382,29 @@
end
end
-function api:add_timer(delay, callback)
- return timer.add_task(delay, function (t)
- if self.loaded == false then return; end
- return callback(t);
- end);
+local timer_methods = { }
+local timer_mt = {
+ __index = timer_methods;
+}
+function timer_methods:stop( )
+ timer.stop(self.id);
+end
+timer_methods.disarm = timer_methods.stop
+function timer_methods:reschedule(delay)
+ timer.reschedule(self.id, delay)
+end
+
+local function timer_callback(now, id, t) --luacheck: ignore 212/id
+ if t.module_env.loaded == false then return; end
+ return t.callback(now, unpack(t, 1, t.n));
+end
+
+function api:add_timer(delay, callback, ...)
+ local t = pack(...)
+ t.module_env = self;
+ t.callback = callback;
+ t.id = timer.add_task(delay, timer_callback, t);
+ return setmetatable(t, timer_mt);
end
local path_sep = package.config:sub(1,1);
diff -r 3d21c63ec03f -r 4e6710b4f873 core/s2smanager.lua
--- a/core/s2smanager.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/core/s2smanager.lua Fri Dec 01 04:54:23 2017 +0100
@@ -64,6 +64,7 @@
function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); 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 };
session.sends2s = session.send;
return setmetatable(session, resting_session);
end
diff -r 3d21c63ec03f -r 4e6710b4f873 core/sessionmanager.lua
--- a/core/sessionmanager.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/core/sessionmanager.lua Fri Dec 01 04:54:23 2017 +0100
@@ -73,6 +73,7 @@
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
diff -r 3d21c63ec03f -r 4e6710b4f873 core/storagemanager.lua
--- a/core/storagemanager.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/core/storagemanager.lua Fri Dec 01 04:54:23 2017 +0100
@@ -137,7 +137,7 @@
};
}
-local open;
+local open; -- forward declaration
local function create_map_shim(host, store)
local keyval_store, err = open(host, store, "keyval");
diff -r 3d21c63ec03f -r 4e6710b4f873 doc/session.txt
--- a/doc/session.txt Fri Dec 01 04:50:20 2017 +0100
+++ b/doc/session.txt Fri Dec 01 04:54:23 2017 +0100
@@ -20,7 +20,7 @@
-- methods --
send(x) -- converts x to a string, and writes it to the connection
- disconnect(x) -- Disconnect the user and clean up the session, best call sessionmanager.destroy_session() instead of this in most cases
+ close(x) -- Disconnect the user and clean up the session, best call sessionmanager.destroy_session() instead of this in most cases
}
if session.full_jid (also session.roster and session.resource) then this is a "connected resource"
diff -r 3d21c63ec03f -r 4e6710b4f873 net/connlisteners.lua
--- a/net/connlisteners.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/net/connlisteners.lua Fri Dec 01 04:54:23 2017 +0100
@@ -5,7 +5,7 @@
local _ENV = nil;
local function fail()
- log("error", "Attempt to use legacy connlisteners API. For more info see http://prosody.im/doc/developers/network");
+ log("error", "Attempt to use legacy connlisteners API. For more info see https://prosody.im/doc/developers/network");
log("error", "Legacy connlisteners API usage, %s", traceback("", 2));
end
diff -r 3d21c63ec03f -r 4e6710b4f873 net/cqueues.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/net/cqueues.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,74 @@
+-- Prosody IM
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+-- This module allows you to use cqueues with a net.server mainloop
+--
+
+local server = require "net.server";
+local cqueues = require "cqueues";
+assert(cqueues.VERSION >= 20150113, "cqueues newer than 20150113 required")
+
+-- Create a single top level cqueue
+local cq;
+
+if server.cq then -- server provides cqueues object
+ cq = server.cq;
+elseif server.get_backend() == "select" and server._addtimer then -- server_select
+ cq = cqueues.new();
+ local function step()
+ assert(cq:loop(0));
+ end
+
+ -- Use wrapclient (as wrapconnection isn't exported) to get server_select to watch cq fd
+ local handler = server.wrapclient({
+ getfd = function() return cq:pollfd(); end;
+ settimeout = function() end; -- Method just needs to exist
+ close = function() end; -- Need close method for 'closeall'
+ }, nil, nil, {});
+
+ -- Only need to listen for readable; cqueues handles everything under the hood
+ -- readbuffer is called when `select` notes an fd as readable
+ handler.readbuffer = step;
+
+ -- Use server_select low lever timer facility,
+ -- this callback gets called *every* time there is a timeout in the main loop
+ server._addtimer(function(current_time)
+ -- This may end up in extra step()'s, but cqueues handles it for us.
+ step();
+ return cq:timeout();
+ end);
+elseif server.event and server.base then -- server_event
+ cq = cqueues.new();
+ -- Only need to listen for readable; cqueues handles everything under the hood
+ local EV_READ = server.event.EV_READ;
+ -- Convert a cqueues timeout to an acceptable timeout for luaevent
+ local function luaevent_safe_timeout(cq)
+ local t = cq:timeout();
+ -- if you give luaevent 0 or nil, it re-uses the previous timeout.
+ if t == 0 then
+ t = 0.000001; -- 1 microsecond is the smallest that works (goes into a `struct timeval`)
+ elseif t == nil then -- pick something big if we don't have one
+ t = 0x7FFFFFFF; -- largest 32bit int
+ end
+ return t
+ end
+ local event_handle;
+ event_handle = server.base:addevent(cq:pollfd(), EV_READ, function(e)
+ -- Need to reference event_handle or this callback will get collected
+ -- This creates a circular reference that can only be broken if event_handle is manually :close()'d
+ local _ = event_handle;
+ -- Run as many cqueues things as possible (with a timeout of 0)
+ -- If an error is thrown, it will break the libevent loop; but prosody resumes after logging a top level error
+ assert(cq:loop(0));
+ return EV_READ, luaevent_safe_timeout(cq);
+ end, luaevent_safe_timeout(cq));
+else
+ error "NYI"
+end
+
+return {
+ cq = cq;
+}
diff -r 3d21c63ec03f -r 4e6710b4f873 net/dns.lua
--- a/net/dns.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/net/dns.lua Fri Dec 01 04:54:23 2017 +0100
@@ -15,6 +15,7 @@
local socket = require "socket";
local timer = require "util.timer";
local new_ip = require "util.ip".new_ip;
+local have_util_net, util_net = pcall(require, "util.net");
local _, windows = pcall(require, "util.windows");
local is_windows = (_ and windows) or os.getenv("WINDIR");
@@ -382,6 +383,12 @@
rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
end
+if have_util_net and util_net.ntop then
+ function resolver:A(rr)
+ rr.a = util_net.ntop(self:sub(4));
+ end
+end
+
function resolver:AAAA(rr)
local addr = {};
for _ = 1, rr.rdlength, 2 do
@@ -402,6 +409,12 @@
rr.aaaa = addr:gsub(zeros[1], "::", 1):gsub("^0::", "::"):gsub("::0$", "::");
end
+if have_util_net and util_net.ntop then
+ function resolver:AAAA(rr)
+ rr.aaaa = util_net.ntop(self:sub(16));
+ end
+end
+
function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
rr.cname = self:name();
end
diff -r 3d21c63ec03f -r 4e6710b4f873 net/http.lua
diff -r 3d21c63ec03f -r 4e6710b4f873 net/httpserver.lua
--- a/net/httpserver.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/net/httpserver.lua Fri Dec 01 04:54:23 2017 +0100
@@ -5,7 +5,7 @@
local _ENV = nil;
function fail()
- log("error", "Attempt to use legacy HTTP API. For more info see http://prosody.im/doc/developers/legacy_http");
+ log("error", "Attempt to use legacy HTTP API. For more info see https://prosody.im/doc/developers/legacy_http");
log("error", "Legacy HTTP API usage, %s", traceback("", 2));
end
diff -r 3d21c63ec03f -r 4e6710b4f873 net/server.lua
--- a/net/server.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/net/server.lua Fri Dec 01 04:54:23 2017 +0100
@@ -6,25 +6,75 @@
-- COPYING file in the source package for more information.
--
-local use_luaevent = prosody and require "core.configmanager".get("*", "use_libevent");
+local server_type = prosody and require "core.configmanager".get("*", "network_backend") or "select";
+if prosody and require "core.configmanager".get("*", "use_libevent") then
+ server_type = "event";
+end
-if use_luaevent then
- use_luaevent = pcall(require, "luaevent.core");
- if not use_luaevent then
+if server_type == "event" then
+ if not pcall(require, "luaevent.core") then
log("error", "libevent not found, falling back to select()");
+ server_type = "select"
end
end
local server;
-
-if use_luaevent then
+local set_config;
+if server_type == "event" then
server = require "net.server_event";
- -- Overwrite signal.signal() because we need to ask libevent to
- -- handle them instead
- local ok, signal = pcall(require, "util.signal");
- if ok and signal then
- local _signal_signal = signal.signal;
+ local defaults = {};
+ for k,v in pairs(server.cfg) do
+ defaults[k] = v;
+ end
+ function set_config(settings)
+ local event_settings = {
+ ACCEPT_DELAY = settings.accept_retry_interval;
+ ACCEPT_QUEUE = settings.tcp_backlog;
+ CLEAR_DELAY = settings.event_clear_interval;
+ CONNECT_TIMEOUT = settings.connect_timeout;
+ DEBUG = settings.debug;
+ HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
+ MAX_CONNECTIONS = settings.max_connections;
+ MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
+ MAX_READ_LENGTH = settings.max_receive_buffer_size;
+ MAX_SEND_LENGTH = settings.max_send_buffer_size;
+ READ_TIMEOUT = settings.read_timeout;
+ WRITE_TIMEOUT = settings.send_timeout;
+ };
+
+ for k,default in pairs(defaults) do
+ server.cfg[k] = event_settings[k] or default;
+ end
+ end
+elseif server_type == "select" then
+ server = require "net.server_select";
+
+ local defaults = {};
+ for k,v in pairs(server.getsettings()) do
+ defaults[k] = v;
+ end
+ function set_config(settings)
+ local select_settings = {};
+ for k,default in pairs(defaults) do
+ select_settings[k] = settings[k] or default;
+ end
+ server.changesettings(select_settings);
+ end
+else
+ server = require("net.server_"..server_type);
+ set_config = server.set_config;
+ if not server.get_backend then
+ function server.get_backend()
+ return server_type;
+ end
+ end
+end
+
+-- If server.hook_signal exists, replace signal.signal()
+local has_signal, signal = pcall(require, "util.signal");
+if has_signal then
+ if server.hook_signal then
function signal.signal(signal_id, handler)
if type(signal_id) == "string" then
signal_id = signal[signal_id:upper()];
@@ -34,46 +84,22 @@
end
return server.hook_signal(signal_id, handler);
end
+ else
+ server.hook_signal = signal.signal;
end
else
- use_luaevent = false;
- server = require "net.server_select";
+ if not server.hook_signal then
+ server.hook_signal = function()
+ return false, "signal hooking not supported"
+ end
+ end
end
-if prosody then
+if prosody and set_config then
local config_get = require "core.configmanager".get;
- local defaults = {};
- for k,v in pairs(server.cfg or server.getsettings()) do
- defaults[k] = v;
- end
local function load_config()
local settings = config_get("*", "network_settings") or {};
- if use_luaevent then
- local event_settings = {
- ACCEPT_DELAY = settings.accept_retry_interval;
- ACCEPT_QUEUE = settings.tcp_backlog;
- CLEAR_DELAY = settings.event_clear_interval;
- CONNECT_TIMEOUT = settings.connect_timeout;
- DEBUG = settings.debug;
- HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
- MAX_CONNECTIONS = settings.max_connections;
- MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
- MAX_READ_LENGTH = settings.max_receive_buffer_size;
- MAX_SEND_LENGTH = settings.max_send_buffer_size;
- READ_TIMEOUT = settings.read_timeout;
- WRITE_TIMEOUT = settings.send_timeout;
- };
-
- for k,default in pairs(defaults) do
- server.cfg[k] = event_settings[k] or default;
- end
- else
- local select_settings = {};
- for k,default in pairs(defaults) do
- select_settings[k] = settings[k] or default;
- end
- server.changesettings(select_settings);
- end
+ return set_config(settings);
end
load_config();
prosody.events.add_handler("config-reloaded", load_config);
diff -r 3d21c63ec03f -r 4e6710b4f873 net/server_epoll.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/net/server_epoll.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,696 @@
+-- Prosody IM
+-- Copyright (C) 2016 Kim Alvefur
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+-- server_epoll
+-- Server backend based on https://luarocks.org/modules/zash/lua-epoll
+
+local t_sort = table.sort;
+local t_insert = table.insert;
+local t_remove = table.remove;
+local t_concat = table.concat;
+local setmetatable = setmetatable;
+local tostring = tostring;
+local pcall = pcall;
+local next = next;
+local pairs = pairs;
+local log = require "util.logger".init("server_epoll");
+local epoll = require "epoll";
+local socket = require "socket";
+local luasec = require "ssl";
+local gettime = require "util.time".now;
+local createtable = require "util.table".create;
+local _SOCKETINVALID = socket._SOCKETINVALID or -1;
+
+assert(socket.tcp6 and socket.tcp4, "Incompatible LuaSocket version");
+
+local _ENV = nil;
+
+local default_config = { __index = {
+ read_timeout = 900;
+ write_timeout = 7;
+ tcp_backlog = 128;
+ accept_retry_interval = 10;
+ read_retry_delay = 1e-06;
+ connect_timeout = 20;
+ handshake_timeout = 60;
+ max_wait = 86400;
+ min_wait = 1e-06;
+}};
+local cfg = default_config.__index;
+
+local fds = createtable(10, 0); -- FD -> conn
+
+-- Timer and scheduling --
+
+local timers = {};
+
+local function noop() end
+local function closetimer(t)
+ t[1] = 0;
+ t[2] = noop;
+end
+
+-- Set to true when timers have changed
+local resort_timers = false;
+
+-- Add absolute timer
+local function at(time, f)
+ local timer = { time, f, close = closetimer };
+ t_insert(timers, timer);
+ resort_timers = true;
+ return timer;
+end
+
+-- Add relative timer
+local function addtimer(timeout, f)
+ return at(gettime() + timeout, f);
+end
+
+-- Run callbacks of expired timers
+-- Return time until next timeout
+local function runtimers(next_delay, min_wait)
+ -- Any timers at all?
+ if not timers[1] then
+ return next_delay;
+ end
+
+ if resort_timers then
+ -- Sort earliest timers to the end
+ t_sort(timers, function (a, b) return a[1] > b[1]; end);
+ resort_timers = false;
+ end
+
+ -- Iterate from the end and remove completed timers
+ for i = #timers, 1, -1 do
+ local timer = timers[i];
+ local t, f = timer[1], timer[2];
+ -- Get time for every iteration to increase accuracy
+ local now = gettime();
+ if t > now then
+ -- This timer should not fire yet
+ local diff = t - now;
+ if diff < next_delay then
+ next_delay = diff;
+ end
+ break;
+ end
+ local new_timeout = f(now);
+ if new_timeout then
+ -- Schedule for 'delay' from the time actually scheduled,
+ -- not from now, in order to prevent timer drift.
+ timer[1] = t + new_timeout;
+ resort_timers = true;
+ else
+ t_remove(timers, i);
+ end
+ end
+
+ if resort_timers or next_delay < min_wait then
+ -- Timers may be added from within a timer callback.
+ -- Those would not be considered for next_delay,
+ -- and we might sleep for too long, so instead
+ -- we return a shorter timeout so we can
+ -- properly sort all new timers.
+ next_delay = min_wait;
+ end
+
+ return next_delay;
+end
+
+-- Socket handler interface
+
+local interface = {};
+local interface_mt = { __index = interface };
+
+function interface_mt:__tostring()
+ if self.sockname and self.peername then
+ return ("FD %d (%s, %d, %s, %d)"):format(self:getfd(), self.peername, self.peerport, self.sockname, self.sockport);
+ elseif self.sockname or self.peername then
+ return ("FD %d (%s, %d)"):format(self:getfd(), self.sockname or self.peername, self.sockport or self.peerport);
+ end
+ return ("%s FD %d"):format(tostring(self.conn), self:getfd());
+end
+
+-- Replace the listener and tell the old one
+function interface:setlistener(listeners)
+ self:on("detach");
+ self.listeners = listeners;
+end
+
+-- Call a listener callback
+function interface:on(what, ...)
+ if not self.listeners then
+ log("error", "%s has no listeners", self);
+ return;
+ end
+ local listener = self.listeners["on"..what];
+ if not listener then
+ -- log("debug", "Missing listener 'on%s'", what); -- uncomment for development and debugging
+ return;
+ end
+ local ok, err = pcall(listener, self, ...);
+ if not ok then
+ log("error", "Error calling on%s: %s", what, err);
+ end
+ return err;
+end
+
+-- Return the file descriptor number
+function interface:getfd()
+ if self.conn then
+ return self.conn:getfd();
+ end
+ return _SOCKETINVALID;
+end
+
+function interface:server()
+ return self._server or self;
+end
+
+-- Get IP address
+function interface:ip()
+ return self.peername or self.sockname;
+end
+
+-- Get a port number, doesn't matter which
+function interface:port()
+ return self.sockport or self.peerport;
+end
+
+-- Get local port number
+function interface:clientport()
+ return self.sockport;
+end
+
+-- Get remote port
+function interface:serverport()
+ if self.sockport then
+ return self.sockport;
+ elseif self._server then
+ self._server:port();
+ end
+end
+
+-- Return underlying socket
+function interface:socket()
+ return self.conn;
+end
+
+function interface:set_mode(new_mode)
+ self._pattern = new_mode;
+end
+
+function interface:setoption(k, v)
+ -- LuaSec doesn't expose setoption :(
+ if self.conn.setoption then
+ self.conn:setoption(k, v);
+ end
+end
+
+-- Timeout for detecting dead or idle sockets
+function interface:setreadtimeout(t)
+ if t == false then
+ if self._readtimeout then
+ self._readtimeout:close();
+ self._readtimeout = nil;
+ end
+ return
+ end
+ t = t or cfg.read_timeout;
+ if self._readtimeout then
+ self._readtimeout[1] = gettime() + t;
+ resort_timers = true;
+ else
+ self._readtimeout = addtimer(t, function ()
+ if self:on("readtimeout") then
+ return cfg.read_timeout;
+ else
+ self:on("disconnect", "read timeout");
+ self:destroy();
+ end
+ end);
+ end
+end
+
+-- Timeout for detecting dead sockets
+function interface:setwritetimeout(t)
+ if t == false then
+ if self._writetimeout then
+ self._writetimeout:close();
+ self._writetimeout = nil;
+ end
+ return
+ end
+ t = t or cfg.write_timeout;
+ if self._writetimeout then
+ self._writetimeout[1] = gettime() + t;
+ resort_timers = true;
+ else
+ self._writetimeout = addtimer(t, function ()
+ self:on("disconnect", "write timeout");
+ self:destroy();
+ end);
+ end
+end
+
+-- lua-epoll flag for currently requested poll state
+function interface:flags()
+ if self._wantread then
+ if self._wantwrite then
+ return "rw";
+ end
+ return "r";
+ elseif self._wantwrite then
+ return "w";
+ end
+end
+
+-- Add or remove sockets or modify epoll flags
+function interface:setflags(r, w)
+ if r ~= nil then self._wantread = r; end
+ if w ~= nil then self._wantwrite = w; end
+ local flags = self:flags();
+ local currentflags = self._flags;
+ if flags == currentflags then
+ return true;
+ end
+ local fd = self:getfd();
+ if fd < 0 then
+ self._wantread, self._wantwrite = nil, nil;
+ return nil, "invalid fd";
+ end
+ local op = "mod";
+ if not flags then
+ op = "del";
+ elseif not currentflags then
+ op = "add";
+ end
+ local ok, err = epoll.ctl(op, fd, flags);
+-- log("debug", "epoll_ctl(%q, %d, %q) -> %s" .. (err and ", %q" or ""),
+-- op, fd, flags or "", tostring(ok), err);
+ if not ok then return ok, err end
+ if op == "add" then
+ fds[fd] = self;
+ elseif op == "del" then
+ fds[fd] = nil;
+ end
+ self._flags = flags;
+ return true;
+end
+
+-- Called when socket is readable
+function interface:onreadable()
+ local data, err, partial = self.conn:receive(self._pattern);
+ if data then
+ self:on("incoming", data);
+ else
+ if partial then
+ self:on("incoming", partial, err);
+ end
+ if err == "wantread" then
+ self:setflags(true, nil);
+ elseif err == "wantwrite" then
+ self:setflags(nil, true);
+ elseif err ~= "timeout" then
+ self:on("disconnect", err);
+ self:destroy()
+ return;
+ end
+ end
+ if not self.conn then return; end
+ if self.conn:dirty() then
+ self:setreadtimeout(false);
+ self:pausefor(cfg.read_retry_delay);
+ else
+ self:setreadtimeout();
+ end
+end
+
+-- Called when socket is writable
+function interface:onwriteable()
+ local buffer = self.writebuffer;
+ local data = t_concat(buffer);
+ local ok, err, partial = self.conn:send(data);
+ if ok then
+ for i = #buffer, 1, -1 do
+ buffer[i] = nil;
+ end
+ self:setflags(nil, false);
+ self:setwritetimeout(false);
+ self:ondrain(); -- Be aware of writes in ondrain
+ return;
+ end
+ if partial then
+ buffer[1] = data:sub(partial+1);
+ for i = #buffer, 2, -1 do
+ buffer[i] = nil;
+ end
+ self:setwritetimeout();
+ end
+ if err == "wantwrite" or err == "timeout" then
+ self:setflags(nil, true);
+ elseif err == "wantread" then
+ self:setflags(true, nil);
+ elseif err ~= "timeout" then
+ self:on("disconnect", err);
+ self:destroy();
+ end
+end
+
+-- The write buffer has been successfully emptied
+function interface:ondrain()
+ return self:on("drain");
+end
+
+-- Add data to write buffer and set flag for wanting to write
+function interface:write(data)
+ local buffer = self.writebuffer;
+ if buffer then
+ t_insert(buffer, data);
+ else
+ self.writebuffer = { data };
+ end
+ self:setwritetimeout();
+ self:setflags(nil, true);
+ return #data;
+end
+interface.send = interface.write;
+
+-- Close, possibly after writing is done
+function interface:close()
+ if self.writebuffer and self.writebuffer[1] then
+ self:setflags(false, true); -- Flush final buffer contents
+ self.write, self.send = noop, noop; -- No more writing
+ log("debug", "Close %s after writing", tostring(self));
+ self.ondrain = interface.close;
+ else
+ log("debug", "Close %s now", tostring(self));
+ self.write, self.send = noop, noop;
+ self.close = noop;
+ self:on("disconnect");
+ self:destroy();
+ end
+end
+
+function interface:destroy()
+ self:setflags(false, false);
+ self:setwritetimeout(false);
+ self:setreadtimeout(false);
+ self.onreadable = noop;
+ self.onwriteable = noop;
+ self.destroy = noop;
+ self.close = noop;
+ self.on = noop;
+ self.conn:close();
+ self.conn = nil;
+end
+
+function interface:ssl()
+ return self._tls;
+end
+
+function interface:starttls(ctx)
+ if ctx then self.tls = ctx; end
+ if self.writebuffer and self.writebuffer[1] then
+ log("debug", "Start TLS on %s after write", tostring(self));
+ self.ondrain = interface.starttls;
+ self.starttls = false;
+ self:setflags(nil, true); -- make sure wantwrite is set
+ else
+ log("debug", "Start TLS on %s now", tostring(self));
+ self:setflags(false, false);
+ local conn, err = luasec.wrap(self.conn, ctx or self.tls);
+ if not conn then
+ self:on("disconnect", err);
+ self:destroy();
+ return conn, err;
+ end
+ conn:settimeout(0);
+ self.conn = conn;
+ self.ondrain = nil;
+ self.onwriteable = interface.tlshandskake;
+ self.onreadable = interface.tlshandskake;
+ self:setflags(true, true);
+ self:setwritetimeout(cfg.handshake_timeout);
+ end
+end
+
+function interface:tlshandskake()
+ self:setwritetimeout(false);
+ self:setreadtimeout(false);
+ local ok, err = self.conn:dohandshake();
+ if ok then
+ log("debug", "TLS handshake on %s complete", tostring(self));
+ self.onwriteable = nil;
+ self.onreadable = nil;
+ self._tls = true;
+ self:on("status", "ssl-handshake-complete");
+ self:init();
+ elseif err == "wantread" then
+ log("debug", "TLS handshake on %s to wait until readable", tostring(self));
+ self:setflags(true, false);
+ self:setreadtimeout(cfg.handshake_timeout);
+ elseif err == "wantwrite" then
+ log("debug", "TLS handshake on %s to wait until writable", tostring(self));
+ self:setflags(false, true);
+ self:setwritetimeout(cfg.handshake_timeout);
+ else
+ log("debug", "TLS handshake error on %s: %s", tostring(self), err);
+ self:on("disconnect", err);
+ self:destroy();
+ end
+end
+
+local function wrapsocket(client, server, pattern, listeners, tls) -- luasocket object -> interface object
+ client:settimeout(0);
+ local conn = setmetatable({
+ conn = client;
+ _server = server;
+ created = gettime();
+ listeners = listeners;
+ _pattern = pattern or (server and server._pattern);
+ writebuffer = {};
+ tls = tls;
+ }, interface_mt);
+
+ if client.getpeername then
+ conn.peername, conn.peerport = client:getpeername();
+ end
+ if client.getsockname then
+ conn.sockname, conn.sockport = client:getsockname();
+ end
+ return conn;
+end
+
+-- A server interface has new incoming connections waiting
+-- This replaces the onreadable callback
+function interface:onacceptable()
+ local conn, err = self.conn:accept();
+ if not conn then
+ log("debug", "Error accepting new client: %s, server will be paused for %ds", err, cfg.accept_retry_interval);
+ self:pausefor(cfg.accept_retry_interval);
+ return;
+ end
+ local client = wrapsocket(conn, self, nil, self.listeners, self.tls);
+ log("debug", "New connection %s", tostring(client));
+ client:init();
+end
+
+-- Initialization
+function interface:init()
+ if self.tls and not self._tls then
+ return self:starttls();
+ else
+ self.onwriteable = interface.onconnect;
+ self:setwritetimeout();
+ return self:setflags(false, true);
+ end
+end
+
+function interface:pause()
+ return self:setflags(false);
+end
+
+function interface:resume()
+ return self:setflags(true);
+end
+
+-- Pause connection for some time
+function interface:pausefor(t)
+ if self._pausefor then
+ self._pausefor:close();
+ end
+ if t == false then return; end
+ self:setflags(false);
+ self._pausefor = addtimer(t, function ()
+ self._pausefor = nil;
+ if self.conn and self.conn:dirty() then
+ self:onreadable();
+ end
+ self:setflags(true);
+ end);
+end
+
+-- Connected!
+function interface:onconnect()
+ self.onwriteable = nil;
+ self:setflags(true, false);
+ if not self._connected then
+ self._connected = true;
+ self:on("connect");
+ end
+ return self:onwriteable();
+end
+
+local function addserver(addr, port, listeners, pattern, tls)
+ local conn, err = socket.bind(addr, port, cfg.tcp_backlog);
+ if not conn then return conn, err; end
+ conn:settimeout(0);
+ local server = setmetatable({
+ conn = conn;
+ created = gettime();
+ listeners = listeners;
+ _pattern = pattern;
+ onreadable = interface.onacceptable;
+ tls = tls;
+ sockname = addr;
+ sockport = port;
+ }, interface_mt);
+ server:setflags(true, false);
+ return server;
+end
+
+-- COMPAT
+local function wrapclient(conn, addr, port, listeners, pattern, tls)
+ local client = wrapsocket(conn, nil, pattern, listeners, tls);
+ if not client.peername then
+ client.peername, client.peerport = addr, port;
+ end
+ client:init();
+ return client;
+end
+
+-- New outgoing TCP connection
+local function addclient(addr, port, listeners, pattern, tls)
+ local conn, err = socket.tcp();
+ if not conn then return conn, err; end
+ conn:settimeout(0);
+ conn:connect(addr, port);
+ local client = wrapsocket(conn, nil, pattern, listeners, tls)
+ client:init();
+ return client, conn;
+end
+
+-- Dump all data from one connection into another
+local function link(from, to)
+ from.listeners = setmetatable({
+ onincoming = function (_, data)
+ from:pause();
+ to:write(data);
+ end,
+ }, {__index=from.listeners});
+ to.listeners = setmetatable({
+ ondrain = function ()
+ from:resume();
+ end,
+ }, {__index=to.listeners});
+ from:setflags(true, nil);
+ to:setflags(nil, true);
+end
+
+-- XXX What uses this?
+-- net.adns
+function interface:set_send(new_send)
+ self.send = new_send;
+end
+
+-- Close all connections and servers
+local function closeall()
+ for fd, conn in pairs(fds) do -- luacheck: ignore 213/fd
+ conn:close();
+ end
+end
+
+local quitting = nil;
+
+-- Signal main loop about shutdown via above upvalue
+local function setquitting(quit)
+ if quit then
+ quitting = "quitting";
+ closeall();
+ else
+ quitting = nil;
+ end
+end
+
+-- Main loop
+local function loop(once)
+ repeat
+ local t = runtimers(cfg.max_wait, cfg.min_wait);
+ local fd, r, w = epoll.wait(t);
+ if fd then
+ local conn = fds[fd];
+ if conn then
+ if r then
+ conn:onreadable();
+ end
+ if w then
+ conn:onwriteable();
+ end
+ else
+ log("debug", "Removing unknown fd %d", fd);
+ epoll.ctl("del", fd);
+ end
+ elseif r ~= "timeout" then
+ log("debug", "epoll_wait error: %s", tostring(r));
+ end
+ until once or (quitting and next(fds) == nil);
+ return quitting;
+end
+
+return {
+ get_backend = function () return "epoll"; end;
+ addserver = addserver;
+ addclient = addclient;
+ add_task = addtimer;
+ at = at;
+ loop = loop;
+ closeall = closeall;
+ setquitting = setquitting;
+ wrapclient = wrapclient;
+ link = link;
+ set_config = function (newconfig)
+ cfg = setmetatable(newconfig, default_config);
+ end;
+
+ -- libevent emulation
+ event = { EV_READ = "r", EV_WRITE = "w", EV_READWRITE = "rw", EV_LEAVE = -1 };
+ addevent = function (fd, mode, callback)
+ local function onevent(self)
+ local ret = self:callback();
+ if ret == -1 then
+ self:setflags(false, false);
+ elseif ret then
+ self:setflags(mode == "r" or mode == "rw", mode == "w" or mode == "rw");
+ end
+ end
+
+ local conn = setmetatable({
+ getfd = function () return fd; end;
+ callback = callback;
+ onreadable = onevent;
+ onwriteable = onevent;
+ close = function (self)
+ self:setflags(false, false);
+ fds[fd] = nil;
+ end;
+ }, interface_mt);
+ local ok, err = conn:setflags(mode == "r" or mode == "rw", mode == "w" or mode == "rw");
+ if not ok then return ok, err; end
+ return conn;
+ end;
+};
diff -r 3d21c63ec03f -r 4e6710b4f873 net/server_event.lua
--- a/net/server_event.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/net/server_event.lua Fri Dec 01 04:54:23 2017 +0100
@@ -223,7 +223,8 @@
_ = self.eventsession and self.eventsession:close( )
_ = self.eventwritetimeout and self.eventwritetimeout:close( )
_ = self.eventreadtimeout and self.eventreadtimeout:close( )
- _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) -- call ondisconnect listener (wont be the case if handshake failed on connect)
+ -- call ondisconnect listener (wont be the case if handshake failed on connect)
+ _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror)
_ = self.conn and self.conn:close( ) -- close connection
_ = self._server and self._server:counter(-1);
self.eventread, self.eventwrite = nil, nil
@@ -773,7 +774,7 @@
end
local function get_backend()
- return base:method();
+ return "libevent " .. base:method();
end
-- We need to hold onto the events to stop them
@@ -811,6 +812,20 @@
sender:set_mode("*a");
end
+local function add_task(delay, callback)
+ local event_handle;
+ event_handle = base:addevent(nil, 0, function ()
+ local ret = callback(socket_gettime());
+ if ret then
+ return 0, ret;
+ elseif event_handle then
+ return -1;
+ end
+ end
+ , delay);
+ return event_handle;
+end
+
return {
cfg = cfg,
base = base,
@@ -826,6 +841,7 @@
closeall = closeallservers,
get_backend = get_backend,
hook_signal = hook_signal,
+ add_task = add_task,
__NAME = SCRIPT_NAME,
__DATE = LAST_MODIFIED,
diff -r 3d21c63ec03f -r 4e6710b4f873 net/server_select.lua
--- a/net/server_select.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/net/server_select.lua Fri Dec 01 04:54:23 2017 +0100
@@ -40,6 +40,7 @@
local math_min = math.min
local math_huge = math.huge
local table_concat = table.concat
+local table_insert = table.insert
local string_sub = string.sub
local coroutine_wrap = coroutine.wrap
local coroutine_yield = coroutine.yield
@@ -55,7 +56,6 @@
local ssl_wrap = ( has_luasec and luasec.wrap )
local socket_bind = luasocket.bind
-local socket_sleep = luasocket.sleep
local socket_select = luasocket.select
--// functions //--
@@ -100,7 +100,6 @@
local _readtraffic
local _selecttimeout
-local _sleeptime
local _tcpbacklog
local _accepretry
@@ -114,8 +113,6 @@
local _sendtimeout
local _readtimeout
-local _timer
-
local _maxselectlen
local _maxfd
@@ -141,7 +138,6 @@
_readtraffic = 0
_selecttimeout = 1 -- timeout of socket.select
-_sleeptime = 0 -- time to wait at the end of every loop
_tcpbacklog = 128 -- some kind of hint to the OS
_accepretry = 10 -- seconds to wait until the next attempt of a full server to accept
@@ -301,7 +297,6 @@
local bufferqueuelen = 0 -- end of buffer array
local toclose
- local fatalerror
local needtls
local bufferlen = 0
@@ -517,7 +512,6 @@
return dispatch( handler, buffer, err )
else -- connections was closed or fatal error
out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) )
- fatalerror = true
_ = handler and handler:force_close( err )
return false
end
@@ -557,7 +551,6 @@
return true
else -- connection was closed during sending or fatal error
out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) )
- fatalerror = true
_ = handler and handler:force_close( err )
return false
end
@@ -806,7 +799,6 @@
getsettings = function( )
return {
select_timeout = _selecttimeout;
- select_sleep_time = _sleeptime;
tcp_backlog = _tcpbacklog;
max_send_buffer_size = _maxsendlen;
max_receive_buffer_size = _maxreadlen;
@@ -825,7 +817,6 @@
return nil, "invalid settings table"
end
_selecttimeout = tonumber( new.select_timeout ) or _selecttimeout
- _sleeptime = tonumber( new.select_sleep_time ) or _sleeptime
_maxsendlen = tonumber( new.max_send_buffer_size ) or _maxsendlen
_maxreadlen = tonumber( new.max_receive_buffer_size ) or _maxreadlen
_checkinterval = tonumber( new.select_idle_check_interval ) or _checkinterval
@@ -848,6 +839,49 @@
return true
end
+local add_task do
+ local data = {};
+ local new_data = {};
+
+ function add_task(delay, callback)
+ local current_time = luasocket_gettime();
+ delay = delay + current_time;
+ if delay >= current_time then
+ table_insert(new_data, {delay, callback});
+ else
+ local r = callback(current_time);
+ if r and type(r) == "number" then
+ return add_task(r, callback);
+ end
+ end
+ end
+
+ addtimer(function(current_time)
+ if #new_data > 0 then
+ for _, d in pairs(new_data) do
+ table_insert(data, d);
+ end
+ new_data = {};
+ end
+
+ local next_time = math_huge;
+ for i, d in pairs(data) do
+ local t, callback = d[1], d[2];
+ if t <= current_time then
+ data[i] = nil;
+ local r = callback(current_time);
+ if type(r) == "number" then
+ add_task(r, callback);
+ next_time = math_min(next_time, r);
+ end
+ else
+ next_time = math_min(next_time, t - current_time);
+ end
+ end
+ return next_time;
+ end);
+end
+
stats = function( )
return _readtraffic, _sendtraffic, _readlistlen, _sendlistlen, _timerlistlen
end
@@ -861,8 +895,15 @@
loop = function(once) -- this is the main loop of the program
if quitting then return "quitting"; end
if once then quitting = "once"; end
+ _currenttime = luasocket_gettime( )
+ repeat
+ -- Fire timers
local next_timer_time = math_huge;
- repeat
+ for i = 1, _timerlistlen do
+ local t = _timerlist[ i ]( _currenttime ) -- fire timers
+ if t then next_timer_time = math_min(next_timer_time, t); end
+ end
+
local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) )
for _, socket in ipairs( write ) do -- send data waiting in writequeues
local handler = _socketlist[ socket ]
@@ -910,27 +951,12 @@
end
end
- -- Fire timers
- if _currenttime - _timer >= math_min(next_timer_time, 1) then
- next_timer_time = math_huge;
- for i = 1, _timerlistlen do
- local t = _timerlist[ i ]( _currenttime ) -- fire timers
- if t then next_timer_time = math_min(next_timer_time, t); end
- end
- _timer = _currenttime
- else
- next_timer_time = next_timer_time - (_currenttime - _timer);
- end
-
for server, paused_time in pairs( _fullservers ) do
if _currenttime - paused_time > _accepretry then
_fullservers[ server ] = nil;
server.resume();
end
end
-
- -- wait some time (0 by default)
- socket_sleep( _sleeptime )
until quitting;
if once and quitting == "once" then quitting = nil; return; end
closeall();
@@ -977,16 +1003,14 @@
elseif sslctx and not has_luasec then
err = "luasec not found"
end
- if not typ then
+ if getaddrinfo and not typ then
local addrinfo, err = getaddrinfo(address)
if not addrinfo then return nil, err end
if addrinfo[1] and addrinfo[1].family == "inet6" then
typ = "tcp6"
- else
- typ = "tcp"
end
end
- local create = luasocket[typ]
+ local create = luasocket[typ or "tcp"]
if type( create ) ~= "function" then
err = "invalid socket type"
end
@@ -1002,22 +1026,19 @@
end
client:settimeout( 0 )
local ok, err = client:connect( address, port )
- if ok or err == "timeout" then
+ if ok or err == "timeout" or err == "Operation already in progress" then
return wrapclient( client, address, port, listeners, pattern, sslctx )
else
return nil, err
end
end
---// EXPERIMENTAL //--
-
----------------------------------// BEGIN //--
use "setmetatable" ( _socketlist, { __mode = "k" } )
use "setmetatable" ( _readtimes, { __mode = "k" } )
use "setmetatable" ( _writetimes, { __mode = "k" } )
-_timer = luasocket_gettime( )
_starttime = luasocket_gettime( )
local function setlogger(new_logger)
@@ -1032,6 +1053,7 @@
return {
_addtimer = addtimer,
+ add_task = add_task;
addclient = addclient,
wrapclient = wrapclient,
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_admin_telnet.lua
--- a/plugins/mod_admin_telnet.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_admin_telnet.lua Fri Dec 01 04:54:23 2017 +0100
@@ -335,6 +335,43 @@
return true, "OK";
end
+def_env.timer = {};
+
+function def_env.timer:info()
+ local socket = require "socket";
+ local print = self.session.print;
+ local add_task = require"util.timer".add_task;
+ local h, params = add_task.h, add_task.params;
+ if h then
+ print("-- util.timer");
+ for i, id in ipairs(h.ids) do
+ if not params[id] then
+ print(os.date("%F %T", h.priorities[i]), h.items[id]);
+ elseif not params[id].callback then
+ print(os.date("%F %T", h.priorities[i]), h.items[id], unpack(params[id]));
+ else
+ print(os.date("%F %T", h.priorities[i]), params[id].callback, unpack(params[id]));
+ end
+ end
+ end
+ if server.event_base then
+ local count = 0;
+ for k, v in pairs(debug.getregistry()) do
+ if type(v) == "function" and v.callback and v.callback == add_task._on_timer then
+ count = count + 1;
+ end
+ end
+ print(count .. " libevent callbacks");
+ end
+ if h then
+ local next_time = h:peek();
+ if next_time then
+ return true, os.date("Next event at %F %T (in %%.6fs)", next_time):format(next_time - socket.gettime());
+ end
+ end
+ return true;
+end
+
def_env.module = {};
local function get_hosts_set(hosts, module)
@@ -960,7 +997,7 @@
return room_name, host;
end
if not room_name then return nil, host end
- if hosts[host].modules.muc.rooms[room_jid] then return nil, "Room exists already" end
+ if hosts[host].modules.muc.get_room_from_jid(room_jid) then return nil, "Room exists already" end
return hosts[host].modules.muc.create_room(room_jid);
end
@@ -969,7 +1006,7 @@
if not room_name then
return room_name, host;
end
- local room_obj = hosts[host].modules.muc.rooms[room_jid];
+ local room_obj = hosts[host].modules.muc.get_room_from_jid(room_jid);
if not room_obj then
return nil, "No such room: "..room_jid;
end
@@ -983,8 +1020,8 @@
end
local print = self.session.print;
local c = 0;
- for name in keys(host_session.modules.muc.rooms) do
- print(name);
+ for room in host_session.modules.muc.each_room() do
+ print(room.jid);
c = c + 1;
end
return true, c.." rooms";
@@ -1154,7 +1191,7 @@
if option == "short" or option == "full" then
session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
session.print("You may find more help on using this console in our online documentation at ");
- session.print("http://prosody.im/doc/console\n");
+ session.print("https://prosody.im/doc/console\n");
end
if option ~= "short" and option ~= "full" and option ~= "graphic" then
session.print(option);
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_bosh.lua
--- a/plugins/mod_bosh.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_bosh.lua Fri Dec 01 04:54:23 2017 +0100
@@ -19,8 +19,9 @@
local log = logger.init("mod_bosh");
local initialize_filters = require "util.filters".initialize;
local math_min = math.min;
-local xpcall, tostring, type = xpcall, tostring, type;
+local tostring, type = tostring, type;
local traceback = debug.traceback;
+local runner = require"util.async".runner;
local nameprep = require "util.encodings".stringprep.nameprep;
local xmlns_streams = "http://etherx.jabber.org/streams";
@@ -68,13 +69,11 @@
local os_time = os.time;
-- All sessions, and sessions that have no requests open
-local sessions, inactive_sessions = module:shared("sessions", "inactive_sessions");
+local sessions = module:shared("sessions");
-- Used to respond to idle sessions (those with waiting requests)
-local waiting_requests = module:shared("waiting_requests");
function on_destroy_request(request)
log("debug", "Request destroyed: %s", tostring(request));
- waiting_requests[request] = nil;
local session = sessions[request.context.sid];
if session then
local requests = session.requests;
@@ -88,9 +87,24 @@
-- If this session now has no requests open, mark it as inactive
local max_inactive = session.bosh_max_inactive;
if max_inactive and #requests == 0 then
- inactive_sessions[session] = os_time() + max_inactive;
+ if session.inactive_timer then
+ session.inactive_timer:stop();
+ end
+ session.inactive_timer = module:add_timer(max_inactive, check_inactive, session, request.context,
+ "BOSH client silent for over "..max_inactive.." seconds");
(session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive);
end
+ if session.bosh_wait_timer then
+ session.bosh_wait_timer:stop();
+ session.bosh_wait_timer = nil;
+ end
+ end
+end
+
+function check_inactive(now, session, context, reason)
+ if not session.destroyed then
+ sessions[context.sid] = nil;
+ sm_destroy_session(session, reason);
end
end
@@ -124,7 +138,7 @@
local headers = response.headers;
headers.content_type = "text/xml; charset=utf-8";
- if cross_domain and event.request.headers.origin then
+ if cross_domain and request.headers.origin then
set_cross_domain_headers(response);
end
@@ -148,8 +162,14 @@
if session then
-- Session was marked as inactive, since we have
-- a request open now, unmark it
- if inactive_sessions[session] and #session.requests > 0 then
- inactive_sessions[session] = nil;
+ if session.inactive_timer and #session.requests > 0 then
+ session.inactive_timer:stop();
+ session.inactive_timer = nil;
+ end
+
+ if session.bosh_wait_timer then
+ session.bosh_wait_timer:stop();
+ session.bosh_wait_timer = nil;
end
local r = session.requests;
@@ -177,9 +197,6 @@
if not response.finished then
-- We're keeping this request open, to respond later
log("debug", "Have nothing to say, so leaving request unanswered for now");
- if session.bosh_wait then
- waiting_requests[response] = os_time() + session.bosh_wait;
- end
end
if session.bosh_terminate then
@@ -187,6 +204,10 @@
session:close();
return nil;
else
+ if session.bosh_wait and #session.requests > 0 then
+ session.bosh_wait_timer = module:add_timer(session.bosh_wait, after_bosh_wait, session.requests[1], session)
+ end
+
return true; -- Inform http server we shall reply later
end
elseif response.finished then
@@ -198,6 +219,11 @@
return tostring(close_reply) .. "\n";
end
+function after_bosh_wait(now, request, session) -- luacheck: ignore 212
+ if request.conn then
+ session.send("");
+ end
+end
local function bosh_reset_stream(session) session.notopen = true; end
@@ -237,10 +263,11 @@
held_request:send(response_body);
end
sessions[session.sid] = nil;
- inactive_sessions[session] = nil;
sm_destroy_session(session);
end
+local runner_callbacks = { };
+
-- Handle the tag in the request payload.
function stream_callbacks.streamopened(context, attr)
local request, response = context.request, context.response;
@@ -291,6 +318,10 @@
};
sessions[sid] = session;
+ session.thread = runner(function (stanza)
+ session:dispatch_stanza(stanza);
+ end, runner_callbacks, session);
+
local filter = initialize_filters(session);
session.log("debug", "BOSH session created for request from %s", session.ip);
@@ -404,6 +435,11 @@
end
local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(tostring(err), 2)); end
+
+function runner_callbacks:error(err) -- luacheck: ignore 212/self
+ return handleerr(err);
+end
+
function stream_callbacks.handlestanza(context, stanza)
if context.ignore then return; end
log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
@@ -417,9 +453,7 @@
t_insert(session.bosh_deferred, stanza);
else
stanza = session.filter("stanzas/in", stanza);
- if stanza then
- return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
- end
+ session.thread:run(stanza);
end
else
log("debug", "No session for this stanza! (sid: %s)", context.sid or "none!");
@@ -469,51 +503,13 @@
end
end
-local dead_sessions = module:shared("dead_sessions");
-function on_timer()
- -- log("debug", "Checking for requests soon to timeout...");
- -- Identify requests timing out within the next few seconds
- local now = os_time() + 3;
- for request, reply_before in pairs(waiting_requests) do
- if reply_before <= now then
- log("debug", "%s was soon to timeout (at %d, now %d), sending empty response", tostring(request), reply_before, now);
- -- Send empty response to let the
- -- client know we're still here
- if request.conn then
- sessions[request.context.sid].send("");
- end
- end
- end
-
- now = now - 3;
- local n_dead_sessions = 0;
- for session, close_after in pairs(inactive_sessions) do
- if close_after < now then
- (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
- sessions[session.sid] = nil;
- inactive_sessions[session] = nil;
- n_dead_sessions = n_dead_sessions + 1;
- dead_sessions[n_dead_sessions] = session;
- end
- end
-
- for i=1,n_dead_sessions do
- local session = dead_sessions[i];
- dead_sessions[i] = nil;
- sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
- end
- return 1;
-end
-module:add_timer(1, on_timer);
-
-
local GET_response = {
headers = {
content_type = "text/html";
};
body = [[
It works! Now point your BOSH client to this URL to connect to Prosody.
- For more information see Prosody: Setting up BOSH.
+ For more information see Prosody: Setting up BOSH.
]];
};
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_c2s.lua
--- a/plugins/mod_c2s.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_c2s.lua Fri Dec 01 04:54:23 2017 +0100
@@ -15,9 +15,9 @@
local st = require "util.stanza";
local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
local uuid_generate = require "util.uuid".generate;
+local runner = require "util.async".runner;
-local xpcall, tostring, type = xpcall, tostring, type;
-local traceback = debug.traceback;
+local tostring, type = tostring, type;
local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
@@ -35,6 +35,7 @@
local stream_callbacks = { default_ns = "jabber:client" };
local listener = {};
+local runner_callbacks = {};
module:hook("stats-update", function ()
local count = 0;
@@ -127,12 +128,9 @@
end
end
-local function handleerr(err) log("error", "Traceback[c2s]: %s", traceback(tostring(err), 2)); end
function stream_callbacks.handlestanza(session, stanza)
stanza = session.filter("stanzas/in", stanza);
- if stanza then
- return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
- end
+ session.thread:run(stanza);
end
--- Session methods
@@ -213,6 +211,18 @@
end
end, 200);
+function runner_callbacks:ready()
+ self.data.conn:resume();
+end
+
+function runner_callbacks:waiting()
+ self.data.conn:pause();
+end
+
+function runner_callbacks:error(err)
+ (self.data.log or log)("error", "Traceback[c2s]: %s", err);
+end
+
--- Port listener
function listener.onconnect(conn)
local session = sm_new_session(conn);
@@ -249,6 +259,10 @@
session.stream:reset();
end
+ session.thread = runner(function (stanza)
+ core_process_stanza(session, stanza);
+ end, runner_callbacks, session);
+
local filter = session.filter;
function session.data(data)
-- Parse the data, which will store stanzas in session.pending_stanzas
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_component.lua
--- a/plugins/mod_component.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_component.lua Fri Dec 01 04:54:23 2017 +0100
@@ -38,7 +38,7 @@
function module.add_host(module)
if module:get_host_type() ~= "component" then
- error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0);
+ error("Don't load mod_component manually, it should be for a component, please see https://prosody.im/doc/components", 0);
end
local env = module.environment;
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_http.lua
--- a/plugins/mod_http.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_http.lua Fri Dec 01 04:54:23 2017 +0100
@@ -50,6 +50,9 @@
local function redir_handler(event)
event.response.headers.location = event.request.path.."/";
+ if event.request.url.query then
+ event.response.headers.location = event.response.headers.location .. "?" .. event.request.url.query
+ end
return 301;
end
@@ -120,7 +123,7 @@
module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
end
else
- module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
+ module:log("error", "Invalid route in %s, %q. See https://prosody.im/doc/developers/http#routes", app_name, key);
end
end
local services = portmanager.get_active_services();
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_muc_unique.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_muc_unique.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,12 @@
+-- XEP-0307: Unique Room Names for Multi-User Chat
+local st = require "util.stanza";
+local uuid_gen = require "util.uuid".generate;
+module:add_feature "http://jabber.org/protocol/muc#unique"
+module:hook("iq-get/host/http://jabber.org/protocol/muc#unique:unique", function(event)
+ local origin, stanza = event.origin, event.stanza;
+ origin.send(st.reply(stanza)
+ :tag("unique", {xmlns = "http://jabber.org/protocol/muc#unique"})
+ :text(uuid_gen()) -- FIXME Random UUIDs can theoretically have collisions
+ );
+ return true;
+end,-1);
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_pep.lua
--- a/plugins/mod_pep.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_pep.lua Fri Dec 01 04:54:23 2017 +0100
@@ -18,6 +18,8 @@
local core_post_stanza = prosody.core_post_stanza;
local bare_sessions = prosody.bare_sessions;
+local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
+
-- Used as canonical 'empty table'
local NULL = {};
-- data[user_bare_jid][node] = item_stanza
@@ -36,9 +38,6 @@
hash_map = state.hash_map or {};
end
-module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
-module:add_feature("http://jabber.org/protocol/pubsub#publish");
-
local function subscription_presence(user_bare, recipient)
local recipient_bare = jid_bare(recipient);
if (recipient_bare == user_bare) then return true end
@@ -284,7 +283,23 @@
module:hook("account-disco-info", function(event)
local reply = event.reply;
reply:tag('identity', {category='pubsub', type='pep'}):up();
- reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
+ reply:tag('feature', {var=xmlns_pubsub}):up();
+ local features = {
+ "access-presence",
+ "auto-create",
+ "auto-subscribe",
+ "filtered-notifications",
+ "item-ids",
+ "last-published",
+ "presence-notifications",
+ "presence-subscribe",
+ "publish",
+ "retract-items",
+ "retrieve-items",
+ };
+ for _, feature in ipairs(features) do
+ reply:tag('feature', {var=xmlns_pubsub.."#"..feature}):up();
+ end
end);
module:hook("account-disco-items", function(event)
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_pep_plus.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_pep_plus.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,471 @@
+local pubsub = require "util.pubsub";
+local jid_bare = require "util.jid".bare;
+local jid_split = require "util.jid".split;
+local jid_join = require "util.jid".join;
+local set_new = require "util.set".new;
+local st = require "util.stanza";
+local calculate_hash = require "util.caps".calculate_hash;
+local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
+local cache = require "util.cache";
+local set = require "util.set";
+
+local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
+local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
+local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
+
+local lib_pubsub = module:require "pubsub";
+
+local empty_set = set_new();
+
+local services = {};
+local recipients = {};
+local hash_map = {};
+
+local host = module.host;
+
+local known_nodes_map = module:open_store("pep", "map");
+local known_nodes = module:open_store("pep");
+
+function module.save()
+ return { services = services };
+end
+
+function module.restore(data)
+ services = data.services;
+end
+
+local function subscription_presence(username, recipient)
+ local user_bare = jid_join(username, host);
+ local recipient_bare = jid_bare(recipient);
+ if (recipient_bare == user_bare) then return true; end
+ return is_contact_subscribed(username, host, recipient_bare);
+end
+
+local function simple_itemstore(username)
+ return function (config, node)
+ if config["persist_items"] then
+ module:log("debug", "Creating new persistent item store for user %s, node %q", username, node);
+ known_nodes_map:set(username, node, true);
+ local archive = module:open_store("pep_"..node, "archive");
+ return lib_pubsub.archive_itemstore(archive, config, username, node, false);
+ else
+ module:log("debug", "Creating new ephemeral item store for user %s, node %q", username, node);
+ known_nodes_map:set(username, node, nil);
+ return cache.new(tonumber(config["max_items"]));
+ end
+ end
+end
+
+local function get_broadcaster(username)
+ local user_bare = jid_join(username, host);
+ local function simple_broadcast(kind, node, jids, item)
+ if item then
+ item = st.clone(item);
+ item.attr.xmlns = nil; -- Clear the pubsub namespace
+ end
+ local message = st.message({ from = user_bare, type = "headline" })
+ :tag("event", { xmlns = xmlns_pubsub_event })
+ :tag(kind, { node = node })
+ :add_child(item);
+ for jid in pairs(jids) do
+ module:log("debug", "Sending notification to %s from %s: %s", jid, user_bare, tostring(item));
+ message.attr.to = jid;
+ module:send(message);
+ end
+ end
+ return simple_broadcast;
+end
+
+function get_pep_service(username)
+ module:log("debug", "get_pep_service(%q)", username);
+ local user_bare = jid_join(username, host);
+ local service = services[username];
+ if service then
+ return service;
+ end
+ service = pubsub.new({
+ capabilities = {
+ none = {
+ create = false;
+ publish = false;
+ retract = false;
+ get_nodes = false;
+
+ subscribe = false;
+ unsubscribe = false;
+ get_subscription = false;
+ get_subscriptions = false;
+ get_items = false;
+
+ subscribe_other = false;
+ unsubscribe_other = false;
+ get_subscription_other = false;
+ get_subscriptions_other = false;
+
+ be_subscribed = true;
+ be_unsubscribed = true;
+
+ set_affiliation = false;
+ };
+ subscriber = {
+ create = false;
+ publish = false;
+ retract = false;
+ get_nodes = true;
+
+ subscribe = true;
+ unsubscribe = true;
+ get_subscription = true;
+ get_subscriptions = true;
+ get_items = true;
+
+ subscribe_other = false;
+ unsubscribe_other = false;
+ get_subscription_other = false;
+ get_subscriptions_other = false;
+
+ be_subscribed = true;
+ be_unsubscribed = true;
+
+ set_affiliation = false;
+ };
+ publisher = {
+ create = false;
+ publish = true;
+ retract = true;
+ get_nodes = true;
+
+ subscribe = true;
+ unsubscribe = true;
+ get_subscription = true;
+ get_subscriptions = true;
+ get_items = true;
+
+ subscribe_other = false;
+ unsubscribe_other = false;
+ get_subscription_other = false;
+ get_subscriptions_other = false;
+
+ be_subscribed = true;
+ be_unsubscribed = true;
+
+ set_affiliation = false;
+ };
+ owner = {
+ create = true;
+ publish = true;
+ retract = true;
+ delete = true;
+ get_nodes = true;
+ configure = true;
+
+ subscribe = true;
+ unsubscribe = true;
+ get_subscription = true;
+ get_subscriptions = true;
+ get_items = true;
+
+
+ subscribe_other = true;
+ unsubscribe_other = true;
+ get_subscription_other = true;
+ get_subscriptions_other = true;
+
+ be_subscribed = true;
+ be_unsubscribed = true;
+
+ set_affiliation = true;
+ };
+ };
+
+ node_defaults = {
+ ["max_items"] = 1;
+ ["persist_items"] = true;
+ };
+
+ autocreate_on_publish = true;
+ autocreate_on_subscribe = true;
+
+ itemstore = simple_itemstore(username);
+ broadcaster = get_broadcaster(username);
+ get_affiliation = function (jid)
+ if jid_bare(jid) == user_bare then
+ return "owner";
+ elseif subscription_presence(username, jid) then
+ return "subscriber";
+ end
+ end;
+
+ normalize_jid = jid_bare;
+ });
+ local nodes, err = known_nodes:get(username);
+ if nodes then
+ module:log("debug", "Restoring nodes for user %s", username);
+ for node in pairs(nodes) do
+ module:log("debug", "Restoring node %q", node);
+ service:create(node, true);
+ end
+ elseif err then
+ module:log("error", "Could not restore nodes for %s: %s", username, err);
+ else
+ module:log("debug", "No known nodes");
+ end
+ services[username] = service;
+ module:add_item("pep-service", { service = service, jid = user_bare });
+ return service;
+end
+
+function handle_pubsub_iq(event)
+ local origin, stanza = event.origin, event.stanza;
+ local service_name = origin.username;
+ if stanza.attr.to ~= nil then
+ service_name = jid_split(stanza.attr.to);
+ end
+ local service = get_pep_service(service_name);
+
+ return lib_pubsub.handle_pubsub_iq(event, service)
+end
+
+module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
+module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
+
+module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
+module:add_feature("http://jabber.org/protocol/pubsub#publish");
+
+local function get_caps_hash_from_presence(stanza, current)
+ local t = stanza.attr.type;
+ if not t then
+ local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
+ if child then
+ local attr = child.attr;
+ if attr.hash then -- new caps
+ if attr.hash == 'sha-1' and attr.node and attr.ver then
+ return attr.ver, attr.node.."#"..attr.ver;
+ end
+ else -- legacy caps
+ if attr.node and attr.ver then
+ return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver;
+ end
+ end
+ end
+ return; -- no or bad caps
+ elseif t == "unavailable" or t == "error" then
+ return;
+ end
+ return current; -- no caps, could mean caps optimization, so return current
+end
+
+local function resend_last_item(jid, node, service)
+ local ok, id, item = service:get_last_item(node, jid);
+ if not ok then return; end
+ if not id then return; end
+ service.config.broadcaster("items", node, { [jid] = true }, item);
+end
+
+local function update_subscriptions(recipient, service_name, nodes)
+ local service = get_pep_service(service_name);
+ nodes = nodes or empty_set;
+
+ local service_recipients = recipients[service_name];
+ if not service_recipients then
+ service_recipients = {};
+ recipients[service_name] = service_recipients;
+ end
+
+ local current = service_recipients[recipient];
+ if not current or type(current) ~= "table" then
+ current = empty_set;
+ end
+
+ if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then
+ return;
+ end
+
+ for node in current - nodes do
+ service:remove_subscription(node, recipient, recipient);
+ end
+
+ for node in nodes - current do
+ service:add_subscription(node, recipient, recipient);
+ resend_last_item(recipient, node, service);
+ end
+
+ if nodes == empty_set or nodes:empty() then
+ nodes = nil;
+ end
+
+ service_recipients[recipient] = nodes;
+end
+
+module:hook("presence/bare", function(event)
+ -- inbound presence to bare JID recieved
+ local origin, stanza = event.origin, event.stanza;
+ local t = stanza.attr.type;
+ local is_self = not stanza.attr.to;
+ local username = jid_split(stanza.attr.to);
+ local user_bare = jid_bare(stanza.attr.to);
+ if is_self then
+ username = origin.username;
+ user_bare = jid_join(username, host);
+ end
+
+ if not t then -- available presence
+ if is_self or subscription_presence(username, stanza.attr.from) then
+ local recipient = stanza.attr.from;
+ local current = recipients[username] and recipients[username][recipient];
+ local hash, query_node = get_caps_hash_from_presence(stanza, current);
+ if current == hash or (current and current == hash_map[hash]) then return; end
+ if not hash then
+ update_subscriptions(recipient, username);
+ else
+ recipients[username] = recipients[username] or {};
+ if hash_map[hash] then
+ update_subscriptions(recipient, username, hash_map[hash]);
+ else
+ recipients[username][recipient] = hash;
+ local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
+ if is_self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
+ -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
+ origin.send(
+ st.stanza("iq", {from=user_bare, to=stanza.attr.from, id="disco", type="get"})
+ :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
+ );
+ end
+ end
+ end
+ end
+ elseif t == "unavailable" then
+ update_subscriptions(stanza.attr.from, username);
+ elseif not is_self and t == "unsubscribe" then
+ local from = jid_bare(stanza.attr.from);
+ local subscriptions = recipients[username];
+ if subscriptions then
+ for subscriber in pairs(subscriptions) do
+ if jid_bare(subscriber) == from then
+ update_subscriptions(subscriber, username);
+ end
+ end
+ end
+ end
+end, 10);
+
+module:hook("iq-result/bare/disco", function(event)
+ local origin, stanza = event.origin, event.stanza;
+ local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
+ if not disco then
+ return;
+ end
+
+ -- Process disco response
+ local is_self = stanza.attr.to == nil;
+ local user_bare = jid_bare(stanza.attr.to);
+ local username = jid_split(stanza.attr.to);
+ if is_self then
+ username = origin.username;
+ user_bare = jid_join(username, host);
+ end
+ local contact = stanza.attr.from;
+ local current = recipients[username] and recipients[username][contact];
+ if type(current) ~= "string" then return; end -- check if waiting for recipient's response
+ local ver = current;
+ if not string.find(current, "#") then
+ ver = calculate_hash(disco.tags); -- calculate hash
+ end
+ local notify = set_new();
+ for _, feature in pairs(disco.tags) do
+ if feature.name == "feature" and feature.attr.var then
+ local nfeature = feature.attr.var:match("^(.*)%+notify$");
+ if nfeature then notify:add(nfeature); end
+ end
+ end
+ hash_map[ver] = notify; -- update hash map
+ if is_self then
+ -- Optimization: Fiddle with other local users
+ for jid, item in pairs(origin.roster) do -- for all interested contacts
+ if jid then
+ local contact_node, contact_host = jid_split(jid);
+ if contact_host == host and item.subscription == "both" or item.subscription == "from" then
+ update_subscriptions(user_bare, contact_node, notify);
+ end
+ end
+ end
+ end
+ update_subscriptions(contact, username, notify);
+end);
+
+module:hook("account-disco-info-node", function(event)
+ local reply, stanza, origin = event.reply, event.stanza, event.origin;
+ local service_name = origin.username;
+ if stanza.attr.to ~= nil then
+ service_name = jid_split(stanza.attr.to);
+ end
+ local service = get_pep_service(service_name);
+ local node = event.node;
+ local ok = service:get_items(node, jid_bare(stanza.attr.from) or true);
+ if not ok then return; end
+ event.exists = true;
+ reply:tag('identity', {category='pubsub', type='leaf'}):up();
+end);
+
+module:hook("account-disco-info", function(event)
+ local origin, reply = event.origin, event.reply;
+
+ reply:tag('identity', {category='pubsub', type='pep'}):up();
+
+ local username = jid_split(reply.attr.from) or origin.username;
+ local service = get_pep_service(username);
+
+ local supported_features = lib_pubsub.get_feature_set(service) + set.new{
+ -- Features not covered by the above
+ "access-presence",
+ "auto-subscribe",
+ "filtered-notifications",
+ "last-published",
+ "persistent-items",
+ "presence-notifications",
+ "presence-subscribe",
+ };
+
+ for feature in supported_features do
+ reply:tag('feature', {var=xmlns_pubsub.."#"..feature}):up();
+ end
+end);
+
+module:hook("account-disco-items-node", function(event)
+ local reply, stanza, origin = event.reply, event.stanza, event.origin;
+ local node = event.node;
+ local is_self = stanza.attr.to == nil;
+ local user_bare = jid_bare(stanza.attr.to);
+ local username = jid_split(stanza.attr.to);
+ if is_self then
+ username = origin.username;
+ user_bare = jid_join(username, host);
+ end
+ local service = get_pep_service(username);
+ local ok, ret = service:get_items(node, jid_bare(stanza.attr.from) or true);
+ if not ok then return; end
+ event.exists = true;
+ for _, id in ipairs(ret) do
+ reply:tag("item", { jid = user_bare, name = id }):up();
+ end
+end);
+
+module:hook("account-disco-items", function(event)
+ local reply, stanza, origin = event.reply, event.stanza, event.origin;
+
+ local is_self = stanza.attr.to == nil;
+ local user_bare = jid_bare(stanza.attr.to);
+ local username = jid_split(stanza.attr.to);
+ if is_self then
+ username = origin.username;
+ user_bare = jid_join(username, host);
+ end
+ local service = get_pep_service(username);
+
+ local ok, ret = service:get_nodes(jid_bare(stanza.attr.from));
+ if not ok then return; end
+
+ for node, node_obj in pairs(ret) do
+ reply:tag("item", { jid = user_bare, node = node, name = node_obj.config.name }):up();
+ end
+end);
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_posix.lua
--- a/plugins/mod_posix.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_posix.lua Fri Dec 01 04:54:23 2017 +0100
@@ -61,7 +61,7 @@
if not suid or suid == 0 or suid == "root" then
if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then
module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
- module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
+ module:log("error", "For more information on running Prosody as root, see https://prosody.im/doc/root");
prosody.shutdown("Refusing to run as root");
end
end
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_private.lua
--- a/plugins/mod_private.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_private.lua Fri Dec 01 04:54:23 2017 +0100
@@ -9,7 +9,7 @@
local st = require "util.stanza"
-local private_storage = module:open_store();
+local private_storage = module:open_store("private", "map");
module:add_feature("jabber:iq:private");
@@ -22,28 +22,23 @@
end
local tag = query.tags[1];
local key = tag.name..":"..tag.attr.xmlns;
- local data, err = private_storage:get(origin.username);
- if err then
- origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
- return true;
- end
if stanza.attr.type == "get" then
- if data and data[key] then
- origin.send(st.reply(stanza):query("jabber:iq:private"):add_child(st.deserialize(data[key])));
- return true;
+ local data, err = private_storage:get(origin.username, key);
+ if data then
+ origin.send(st.reply(stanza):query("jabber:iq:private"):add_child(st.deserialize(data)));
+ elseif err then
+ origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
else
origin.send(st.reply(stanza):add_child(query));
- return true;
end
+ return true;
else -- type == set
- if not data then data = {}; end;
- if #tag == 0 then
- data[key] = nil;
- else
- data[key] = st.preserialize(tag);
+ local data;
+ if #tag ~= 0 then
+ data = st.preserialize(tag);
end
-- TODO delete datastore if empty
- local ok, err = private_storage:set(origin.username, data);
+ local ok, err = private_storage:set(origin.username, key, data);
if not ok then
origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
return true;
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_pubsub/mod_pubsub.lua
--- a/plugins/mod_pubsub/mod_pubsub.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_pubsub/mod_pubsub.lua Fri Dec 01 04:54:23 2017 +0100
@@ -12,31 +12,32 @@
local pubsub_disco_name = module:get_option_string("name", "Prosody PubSub Service");
local expose_publisher = module:get_option_boolean("expose_publisher", false)
+local enable_persistence = module:get_option_boolean("experimental_pubsub_item_persistence", false);
+
local service;
local lib_pubsub = module:require "pubsub";
-local handlers = lib_pubsub.handlers;
-local pubsub_error_reply = lib_pubsub.pubsub_error_reply;
module:depends("disco");
module:add_identity("pubsub", "service", pubsub_disco_name);
module:add_feature("http://jabber.org/protocol/pubsub");
function handle_pubsub_iq(event)
- local origin, stanza = event.origin, event.stanza;
- local pubsub = stanza.tags[1];
- local action = pubsub.tags[1];
- if not action then
- origin.send(st.error_reply(stanza, "cancel", "bad-request"));
- return true;
- end
- local handler = handlers[stanza.attr.type.."_"..action.name];
- if handler then
- handler(origin, stanza, action, service);
- return true;
- end
+ return lib_pubsub.handle_pubsub_iq(event, service);
+end
+
+local function simple_itemstore(config, node)
+ local archive = module:open_store("pubsub_"..node, "archive");
+ return lib_pubsub.archive_itemstore(archive, config, nil, node);
end
+if enable_persistence then
+ module:log("warn", "Item persistence is an experimental feature. Note that ownership information is lost on restart.")
+else
+ simple_itemstore = nil;
+end
+
+
function simple_broadcast(kind, node, jids, item, actor)
if item then
item = st.clone(item);
@@ -59,38 +60,14 @@
module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
-local feature_map = {
- create = { "create-nodes", "instant-nodes", "item-ids" };
- retract = { "delete-items", "retract-items" };
- purge = { "purge-nodes" };
- publish = { "publish", autocreate_on_publish and "auto-create" };
- delete = { "delete-nodes" };
- get_items = { "retrieve-items" };
- add_subscription = { "subscribe" };
- get_subscriptions = { "retrieve-subscriptions" };
- set_configure = { "config-node" };
- get_default = { "retrieve-default" };
-};
-
local function add_disco_features_from_service(service)
- for method, features in pairs(feature_map) do
- if service[method] then
- for _, feature in ipairs(features) do
- if feature then
- module:add_feature(xmlns_pubsub.."#"..feature);
- end
- end
- end
- end
- for affiliation in pairs(service.config.capabilities) do
- if affiliation ~= "none" and affiliation ~= "owner" then
- module:add_feature(xmlns_pubsub.."#"..affiliation.."-affiliation");
- end
+ for feature in lib_pubsub.get_feature_set(service) do
+ module:add_feature(xmlns_pubsub.."#"..feature);
end
end
module:hook("host-disco-info-node", function (event)
- local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
+ local stanza, reply, node = event.stanza, event.reply, event.node;
local ok, ret = service:get_nodes(stanza.attr.from);
if not ok or not ret[node] then
return;
@@ -100,7 +77,7 @@
end);
module:hook("host-disco-items-node", function (event)
- local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
+ local stanza, reply, node = event.stanza, event.reply, event.node;
local ok, ret = service:get_items(node, stanza.attr.from);
if not ok then
return;
@@ -114,8 +91,8 @@
module:hook("host-disco-items", function (event)
- local stanza, origin, reply = event.stanza, event.origin, event.reply;
- local ok, ret = service:get_nodes(event.stanza.attr.from);
+ local stanza, reply = event.stanza, event.reply;
+ local ok, ret = service:get_nodes(stanza.attr.from);
if not ok then
return;
end
@@ -225,6 +202,7 @@
autocreate_on_publish = autocreate_on_publish;
autocreate_on_subscribe = autocreate_on_subscribe;
+ itemstore = simple_itemstore;
broadcaster = simple_broadcast;
get_affiliation = get_affiliation;
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_pubsub/pubsub.lib.lua
--- a/plugins/mod_pubsub/pubsub.lib.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_pubsub/pubsub.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -1,4 +1,9 @@
+local t_unpack = table.unpack or unpack; -- luacheck: ignore 113
+local time_now = os.time;
+
+local set = require "util.set";
local st = require "util.stanza";
+local it = require "util.iterators";
local uuid_generate = require "util.uuid".generate;
local dataform = require"util.dataforms".new;
@@ -23,7 +28,7 @@
};
local function pubsub_error_reply(stanza, error)
local e = pubsub_errors[error];
- local reply = st.error_reply(stanza, unpack(e, 1, 3));
+ local reply = st.error_reply(stanza, t_unpack(e, 1, 3));
if e[4] then
reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
end
@@ -31,7 +36,7 @@
end
_M.pubsub_error_reply = pubsub_error_reply;
-local node_config_form = require"util.dataforms".new {
+local node_config_form = dataform {
{
type = "hidden";
name = "FORM_TYPE";
@@ -42,18 +47,89 @@
name = "pubsub#max_items";
label = "Max # of items to persist";
};
+ {
+ type = "boolean";
+ name = "pubsub#persist_items";
+ label = "Persist items to storage";
+ };
};
+local service_method_feature_map = {
+ add_subscription = { "subscribe" };
+ create = { "create-nodes", "instant-nodes", "item-ids", "create-and-configure" };
+ delete = { "delete-nodes" };
+ get_items = { "retrieve-items" };
+ get_subscriptions = { "retrieve-subscriptions" };
+ node_defaults = { "retrieve-default" };
+ publish = { "publish" };
+ purge = { "purge-nodes" };
+ retract = { "delete-items", "retract-items" };
+ set_node_config = { "config-node" };
+};
+local service_config_feature_map = {
+ autocreate_on_publish = { "auto-create" };
+};
+
+function _M.get_feature_set(service)
+ local supported_features = set.new();
+
+ for method, features in pairs(service_method_feature_map) do
+ if service[method] then
+ for _, feature in ipairs(features) do
+ if feature then
+ supported_features:add(feature);
+ end
+ end
+ end
+ end
+
+ for option, features in pairs(service_config_feature_map) do
+ if service.config[option] then
+ for _, feature in ipairs(features) do
+ if feature then
+ supported_features:add(feature);
+ end
+ end
+ end
+ end
+
+ for affiliation in pairs(service.config.capabilities) do
+ if affiliation ~= "none" and affiliation ~= "owner" then
+ supported_features:add(affiliation.."-affiliation");
+ end
+ end
+
+ return supported_features;
+end
+
+function _M.handle_pubsub_iq(event, service)
+ local origin, stanza = event.origin, event.stanza;
+ local pubsub_tag = stanza.tags[1];
+ local action = pubsub_tag.tags[1];
+ if not action then
+ return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
+ end
+ local prefix = "";
+ if pubsub_tag.attr.xmlns == xmlns_pubsub_owner then
+ prefix = "owner_";
+ end
+ local handler = handlers[prefix..stanza.attr.type.."_"..action.name];
+ if handler then
+ handler(origin, stanza, action, service);
+ return true;
+ end
+end
+
function handlers.get_items(origin, stanza, items, service)
local node = items.attr.node;
local item = items:get_child("item");
- local id = item and item.attr.id;
+ local item_id = item and item.attr.id;
if not node then
origin.send(pubsub_error_reply(stanza, "nodeid-required"));
return true;
end
- local ok, results = service:get_items(node, stanza.attr.from, id);
+ local ok, results = service:get_items(node, stanza.attr.from, item_id);
if not ok then
origin.send(pubsub_error_reply(stanza, results));
return true;
@@ -95,8 +171,26 @@
function handlers.set_create(origin, stanza, create, service)
local node = create.attr.node;
local ok, ret, reply;
+ local config;
+ local configure = stanza.tags[1]:get_child("configure");
+ if configure then
+ local config_form = configure:get_child("x", "jabber:x:data");
+ if not config_form then
+ origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing dataform"));
+ return true;
+ end
+ local form_data, err = node_config_form:data(config_form);
+ if not form_data then
+ origin.send(st.error_reply(stanza, "modify", "bad-request", err));
+ return true;
+ end
+ config = {
+ ["max_items"] = tonumber(form_data["pubsub#max_items"]);
+ ["persist_items"] = form_data["pubsub#persist_items"];
+ };
+ end
if node then
- ok, ret = service:create(node, stanza.attr.from);
+ ok, ret = service:create(node, stanza.attr.from, config);
if ok then
reply = st.reply(stanza);
else
@@ -105,7 +199,7 @@
else
repeat
node = uuid_generate();
- ok, ret = service:create(node, stanza.attr.from);
+ ok, ret = service:create(node, stanza.attr.from, config);
until ok or ret ~= "conflict";
if ok then
reply = st.reply(stanza)
@@ -119,10 +213,10 @@
return true;
end
-function handlers.set_delete(origin, stanza, delete, service)
+function handlers.owner_set_delete(origin, stanza, delete, service)
local node = delete.attr.node;
- local reply, notifier;
+ local reply;
if not node then
origin.send(pubsub_error_reply(stanza, "nodeid-required"));
return true;
@@ -203,6 +297,9 @@
local ok, ret = service:publish(node, stanza.attr.from, id, item);
local reply;
if ok then
+ if type(ok) == "string" then
+ id = ok;
+ end
reply = st.reply(stanza)
:tag("pubsub", { xmlns = xmlns_pubsub })
:tag("publish", { node = node })
@@ -237,7 +334,7 @@
return true;
end
-function handlers.set_purge(origin, stanza, purge, service)
+function handlers.owner_set_purge(origin, stanza, purge, service)
local node, notify = purge.attr.node, purge.attr.notify;
notify = (notify == "1") or (notify == "true");
local reply;
@@ -255,7 +352,7 @@
return true;
end
-function handlers.get_configure(origin, stanza, config, service)
+function handlers.owner_get_configure(origin, stanza, config, service)
local node = config.attr.node;
if not node then
origin.send(pubsub_error_reply(stanza, "nodeid-required"));
@@ -273,15 +370,20 @@
return true;
end
+ local node_config = node_obj.config;
+ local pubsub_form_data = {
+ ["pubsub#max_items"] = tostring(node_config["max_items"]);
+ ["pubsub#persist_items"] = node_config["persist_items"]
+ }
local reply = st.reply(stanza)
:tag("pubsub", { xmlns = xmlns_pubsub_owner })
:tag("configure", { node = node })
- :add_child(node_config_form:form(node_obj.config));
+ :add_child(node_config_form:form(pubsub_form_data));
origin.send(reply);
return true;
end
-function handlers.set_configure(origin, stanza, config, service)
+function handlers.owner_set_configure(origin, stanza, config, service)
local node = config.attr.node;
if not node then
origin.send(pubsub_error_reply(stanza, "nodeid-required"));
@@ -291,11 +393,20 @@
origin.send(pubsub_error_reply(stanza, "forbidden"));
return true;
end
- local new_config, err = node_config_form:data(config.tags[1]);
- if not new_config then
+ local config_form = config:get_child("x", "jabber:x:data");
+ if not config_form then
+ origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing dataform"));
+ return true;
+ end
+ local form_data, err = node_config_form:data(config_form);
+ if not form_data then
origin.send(st.error_reply(stanza, "modify", "bad-request", err));
return true;
end
+ local new_config = {
+ ["max_items"] = tonumber(form_data["pubsub#max_items"]);
+ ["persist_items"] = form_data["pubsub#persist_items"];
+ };
local ok, err = service:set_node_config(node, stanza.attr.from, new_config);
if not ok then
origin.send(pubsub_error_reply(stanza, err));
@@ -305,13 +416,97 @@
return true;
end
-function handlers.get_default(origin, stanza, default, service)
+function handlers.owner_get_default(origin, stanza, default, service) -- luacheck: ignore 212/default
+ local pubsub_form_data = {
+ ["pubsub#max_items"] = tostring(service.node_defaults["max_items"]);
+ ["pubsub#persist_items"] = service.node_defaults["persist_items"]
+ }
local reply = st.reply(stanza)
:tag("pubsub", { xmlns = xmlns_pubsub_owner })
:tag("default")
- :add_child(node_config_form:form(service.node_defaults));
+ :add_child(node_config_form:form(pubsub_form_data));
origin.send(reply);
return true;
end
+local function create_encapsulating_item(id, payload)
+ local item = st.stanza("item", { id = id, xmlns = xmlns_pubsub });
+ item:add_child(payload);
+ return item;
+end
+
+local function archive_itemstore(archive, config, user, node)
+ module:log("debug", "Creation of itemstore for node %s with config %s", node, config);
+ local get_set = {};
+ function get_set:items() -- luacheck: ignore 212/self
+ local data, err = archive:find(user, {
+ limit = tonumber(config["max_items"]);
+ reverse = true;
+ });
+ if not data then
+ module:log("error", "Unable to get items: %s", err);
+ return true;
+ end
+ module:log("debug", "Listed items %s", data);
+ return it.reverse(function()
+ local id, payload, when, publisher = data();
+ if id == nil then
+ return;
+ end
+ local item = create_encapsulating_item(id, payload, publisher);
+ return id, item;
+ end);
+ end
+ function get_set:get(key) -- luacheck: ignore 212/self
+ local data, err = archive:find(user, {
+ key = key;
+ -- Get the last item with that key, if the archive doesn't deduplicate
+ reverse = true,
+ limit = 1;
+ });
+ if not data then
+ module:log("error", "Unable to get item: %s", err);
+ return nil, err;
+ end
+ local id, payload, when, publisher = data();
+ module:log("debug", "Get item %s (published at %s by %s)", id, when, publisher);
+ if id == nil then
+ return nil;
+ end
+ return create_encapsulating_item(id, payload, publisher);
+ end
+ function get_set:set(key, value) -- luacheck: ignore 212/self
+ local data, err;
+ if value ~= nil then
+ local publisher = value.attr.publisher;
+ local payload = value.tags[1];
+ data, err = archive:append(user, key, payload, time_now(), publisher);
+ else
+ data, err = archive:delete(user, { key = key; });
+ end
+ if not data then
+ module:log("error", "Unable to set item: %s", err);
+ return nil, err;
+ end
+ return data;
+ end
+ function get_set:clear() -- luacheck: ignore 212/self
+ return archive:delete(user);
+ end
+ function get_set:resize(size)
+ return archive:delete(user, {
+ truncate = size;
+ });
+ end
+ function get_set:tail()
+ -- This should conveniently return the last item
+ local item = self:get(nil);
+ if item then
+ return item.attr.id, item;
+ end
+ end
+ return setmetatable(get_set, archive);
+end
+_M.archive_itemstore = archive_itemstore;
+
return _M;
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_s2s/mod_s2s.lua
--- a/plugins/mod_s2s/mod_s2s.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_s2s/mod_s2s.lua Fri Dec 01 04:54:23 2017 +0100
@@ -26,6 +26,7 @@
local s2s_destroy_session = require "core.s2smanager".destroy_session;
local uuid_gen = require "util.uuid".generate;
local fire_global_event = prosody.events.fire_event;
+local runner = require "util.async".runner;
local s2sout = module:require("s2sout");
@@ -41,6 +42,8 @@
local sessions = module:shared("sessions");
+local runner_callbacks = {};
+
local log = module._log;
module:hook("stats-update", function ()
@@ -64,6 +67,9 @@
(session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", traceback());
end;
dummy = true;
+ close = function ()
+ (session.log or log)("error", "Attempting to close the dummy origin of s2s error replies, please report this! Traceback: %s", traceback());
+ end;
};
for i, data in ipairs(sendq) do
local reply = data[2];
@@ -151,7 +157,7 @@
function module.add_host(module)
if module:get_option_boolean("disallow_s2s", false) then
- module:log("warn", "The 'disallow_s2s' config option is deprecated, please see http://prosody.im/doc/s2s#disabling");
+ module:log("warn", "The 'disallow_s2s' config option is deprecated, please see https://prosody.im/doc/s2s#disabling");
return nil, "This host has disallow_s2s set";
end
module:hook("route/remote", route_to_existing_session, -1);
@@ -264,11 +270,21 @@
--- XMPP stream event handlers
-local stream_callbacks = { default_ns = "jabber:server", handlestanza = core_process_stanza };
+local stream_callbacks = { default_ns = "jabber:server" };
+
+function stream_callbacks.handlestanza(session, stanza)
+ stanza = session.filter("stanzas/in", stanza);
+ session.thread:run(stanza);
+end
local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
function stream_callbacks.streamopened(session, attr)
+ -- run _streamopened in async context
+ session.thread:run({ attr = attr });
+end
+
+function stream_callbacks._streamopened(session, attr)
session.version = tonumber(attr.version) or 0;
-- TODO: Rename session.secure to session.encrypted
@@ -440,14 +456,6 @@
end
end
-local function handleerr(err) log("error", "Traceback[s2s]: %s", traceback(tostring(err), 2)); end
-function stream_callbacks.handlestanza(session, stanza)
- stanza = session.filter("stanzas/in", stanza);
- if stanza then
- return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
- end
-end
-
local listener = {};
--- Session methods
@@ -522,6 +530,15 @@
-- Session initialization logic shared by incoming and outgoing
local function initialize_session(session)
local stream = new_xmpp_stream(session, stream_callbacks);
+
+ session.thread = runner(function (stanza)
+ if stanza.name == nil then
+ stream_callbacks._streamopened(session, stanza.attr);
+ else
+ core_process_stanza(session, stanza);
+ end
+ end, runner_callbacks, session);
+
local log = session.log or log;
session.stream = stream;
@@ -585,6 +602,20 @@
end);
end
+function runner_callbacks:ready()
+ self.data.log("debug", "Runner %s ready (%s)", self.thread, coroutine.status(self.thread));
+ self.data.conn:resume();
+end
+
+function runner_callbacks:waiting()
+ self.data.log("debug", "Runner %s waiting (%s)", self.thread, coroutine.status(self.thread));
+ self.data.conn:pause();
+end
+
+function runner_callbacks:error(err)
+ (self.data.log or log)("error", "Traceback[s2s]: %s", err);
+end
+
function listener.onconnect(conn)
conn:setoption("keepalive", opt_keepalives);
local session = sessions[conn];
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_storage_internal.lua
--- a/plugins/mod_storage_internal.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_storage_internal.lua Fri Dec 01 04:54:23 2017 +0100
@@ -44,17 +44,36 @@
driver.archive = { __index = archive };
function archive:append(username, key, value, when, with)
- key = key or id();
when = when or now();
if not st.is_stanza(value) then
return nil, "unsupported-datatype";
end
value = st.preserialize(st.clone(value));
- value.key = key;
value.when = when;
value.with = with;
value.attr.stamp = datetime.datetime(when);
value.attr.stamp_legacy = datetime.legacy(when);
+
+ if key then
+ local items, err = datamanager.list_load(username, host, self.store);
+ if not items and err then return items, err; end
+ if items then
+ items = array(items);
+ items:filter(function (item)
+ return item.key ~= key;
+ end);
+ value.key = key;
+ items:push(value);
+ local ok, err = datamanager.list_store(username, host, self.store, items);
+ if not ok then return ok, err; end
+ return key;
+ end
+ else
+ key = id();
+ end
+
+ value.key = key;
+
local ok, err = datamanager.list_append(username, host, self.store, value);
if not ok then return ok, err; end
return key;
@@ -141,9 +160,6 @@
if not query or next(query) == nil then
return datamanager.list_store(username, host, self.store, nil);
end
- for k in pairs(query) do
- if k ~= "end" then return nil, "unsupported-query-field"; end
- end
local items, err = datamanager.list_load(username, host, self.store);
if not items then
if err then
@@ -154,10 +170,48 @@
end
items = array(items);
local count_before = #items;
- items:filter(function (item)
- return item.when > query["end"];
- end);
+ if query then
+ if query.key then
+ items:filter(function (item)
+ return item.key ~= query.key;
+ end);
+ end
+ if query.with then
+ items:filter(function (item)
+ return item.with ~= query.with;
+ end);
+ end
+ if query.start then
+ items:filter(function (item)
+ return item.when < query.start;
+ end);
+ end
+ if query["end"] then
+ items:filter(function (item)
+ return item.when > query["end"];
+ end);
+ end
+ if query.truncate then
+ if query.reverse then
+ -- Before: { 1, 2, 3, 4, 5, }
+ -- After: { 1, 2, 3 }
+ for i = #items, query.truncate + 1, -1 do
+ items[i] = nil;
+ end
+ else
+ -- Before: { 1, 2, 3, 4, 5, }
+ -- After: { 3, 4, 5 }
+ local offset = #items - query.truncate;
+ for i = 1, #items do
+ items[i] = items[i+offset];
+ end
+ end
+ end
+ end
local count = count_before - #items;
+ if count == 0 then
+ return 0; -- No changes, skip write
+ end
local ok, err = datamanager.list_store(username, host, self.store, items);
if not ok then return ok, err; end
return count;
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_storage_sql.lua
--- a/plugins/mod_storage_sql.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_storage_sql.lua Fri Dec 01 04:54:23 2017 +0100
@@ -374,7 +374,35 @@
end
archive_where(query, args, where);
archive_where_id_range(query, args, where);
- sql_query = sql_query:format(t_concat(where, " AND "));
+ if query.truncate == nil then
+ sql_query = sql_query:format(t_concat(where, " AND "));
+ else
+ args[#args+1] = query.truncate;
+ local unlimited = "ALL";
+ if engine.params.driver == "SQLite3" then
+ sql_query = [[
+ DELETE FROM "prosodyarchive"
+ WHERE %s
+ ORDER BY "sort_id" %s
+ LIMIT %s OFFSET ?;
+ ]];
+ unlimited = "-1";
+ else
+ sql_query = [[
+ DELETE FROM "prosodyarchive"
+ WHERE "sort_id" IN (
+ SELECT "sort_id" FROM "prosodyarchive"
+ WHERE %s
+ ORDER BY "sort_id" %s
+ LIMIT %s OFFSET ?
+ );]];
+ if engine.params.driver == "MySQL" then
+ unlimited = "18446744073709551615";
+ end
+ end
+ sql_query = string.format(sql_query, t_concat(where, " AND "),
+ query.reverse and "ASC" or "DESC", unlimited);
+ end
return engine:delete(sql_query, unpack(args));
end);
return ok and stmt:affected(), stmt;
@@ -477,7 +505,7 @@
end);
if not success then
module:log("error", "Failed to check/upgrade database schema (%s), please see "
- .."http://prosody.im/doc/mysql for help",
+ .."https://prosody.im/doc/mysql for help",
err or "unknown error");
return false;
end
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_storage_sql1.lua
--- a/plugins/mod_storage_sql1.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_storage_sql1.lua Fri Dec 01 04:54:23 2017 +0100
@@ -130,7 +130,7 @@
module:log("info", "Database table automatically upgraded");
else
module:log("error", "Failed to upgrade database schema (%s), please see "
- .."http://prosody.im/doc/mysql for help",
+ .."https://prosody.im/doc/mysql for help",
err or "unknown error");
end
end
@@ -139,7 +139,7 @@
end
elseif params.driver ~= "SQLite3" then -- SQLite normally fails to prepare for existing table
module:log("warn", "Prosody was not able to automatically check/create the database table (%s), "
- .."see http://prosody.im/doc/modules/mod_storage_sql#table_management for help.",
+ .."see https://prosody.im/doc/modules/mod_storage_sql#table_management for help.",
err or "unknown error");
end
end
@@ -151,7 +151,7 @@
if not ok then
package.loaded["DBI"] = {};
module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI);
- module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi");
+ module:log("error", "More information on installing LuaDBI can be found at https://prosody.im/doc/depends#luadbi");
end
prosody.lock_globals();
if not ok or not DBI.Connect then
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_storage_xep0227.lua
--- a/plugins/mod_storage_xep0227.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_storage_xep0227.lua Fri Dec 01 04:54:23 2017 +0100
@@ -164,10 +164,84 @@
end;
};
+handlers.roster = {
+ get = function(self, user)
+ user = getUserElement(getXml(user, self.host));
+ if user then
+ local roster = user:get_child("query", "jabber:iq:roster");
+ if roster then
+ local r = {
+ [false] = {
+ version = roster.attr.version;
+ pending = {};
+ }
+ };
+ for item in roster:childtags("item") do
+ r[item.attr.jid] = {
+ jid = item.attr.jid,
+ subscription = item.attr.subscription,
+ ask = item.attr.ask,
+ name = item.attr.name,
+ groups = {};
+ };
+ for group in item:childtags("group") do
+ r[item.attr.jid].groups[group:get_text()] = true;
+ end
+ for pending in user:childtags("presence", "jabber:client") do
+ r[false].pending[pending.attr.from] = true;
+ end
+ end
+ return r;
+ end
+ end
+ end;
+ set = function(self, user, data)
+ local xml = getXml(user, self.host);
+ local usere = xml and getUserElement(xml);
+ if usere then
+ local roster = usere:get_child("query", 'jabber:iq:roster');
+ if roster then removeStanzaChild(usere, roster); end
+ usere:maptags(function (tag)
+ if tag.attr.xmlns == "jabber:client" and tag.name == "presence" and tag.attr.type == "subscribe" then
+ return nil;
+ end
+ return tag;
+ end);
+ if data and next(data) ~= nil then
+ roster = st.stanza("query", {xmlns='jabber:iq:roster'});
+ usere:add_child(roster);
+ for jid, item in pairs(data) do
+ if jid then
+ roster:tag("item", {
+ jid = jid,
+ subscription = item.subscription,
+ ask = item.ask,
+ name = item.name,
+ });
+ for group in pairs(item.groups) do
+ roster:tag("group"):text(group):up();
+ end
+ roster:up(); -- move out from item
+ else
+ roster.attr.version = item.version;
+ for pending_jid in pairs(item.pending) do
+ usere:add_child(st.presence({ from = pending_jid, type = "subscribe" }));
+ end
+ end
+ end
+ end
+ return setXml(user, self.host, xml);
+ end
+ return true;
+ end;
+};
+
+
-----------------------------
local driver = {};
-function driver:open(datastore, typ)
+function driver:open(datastore, typ) -- luacheck: ignore 212/self
+ if typ and typ ~= "keyval" then return nil, "unsupported-store"; end
local handler = handlers[datastore];
if not handler then return nil, "unsupported-datastore"; end
local instance = setmetatable({ host = module.host; datastore = datastore; }, { __index = handler });
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/mod_uptime.lua
--- a/plugins/mod_uptime.lua Fri Dec 01 04:50:20 2017 +0100
+++ b/plugins/mod_uptime.lua Fri Dec 01 04:54:23 2017 +0100
@@ -39,7 +39,7 @@
minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
end
-function uptime_command_handler (self, data, state)
+function uptime_command_handler ()
return { info = uptime_text(), status = "completed" };
end
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/muc/affiliation_notify.lib.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/affiliation_notify.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,64 @@
+-- Prosody IM
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+--[[
+Out of courtesy, a MUC service MAY send an out-of-room
+if a user's affiliation changes while the user is not in the room;
+the message SHOULD be sent from the room to the user's bare JID,
+MAY contain a element describing the affiliation change,
+and MUST contain a status code of 101.
+]]
+
+
+local st = require "util.stanza";
+
+local function get_affiliation_notify(room)
+ return room._data.affiliation_notify;
+end
+
+local function set_affiliation_notify(room, affiliation_notify)
+ affiliation_notify = affiliation_notify and true or nil;
+ if room._data.affiliation_notify == affiliation_notify then return false; end
+ room._data.affiliation_notify = affiliation_notify;
+ return true;
+end
+
+module:hook("muc-config-form", function(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_affiliationnotify";
+ type = "boolean";
+ label = "Notify absent users when their affiliation changes?";
+ value = get_affiliation_notify(event.room);
+ });
+end, 100-11);
+
+module:hook("muc-config-submitted/muc#roomconfig_affiliationnotify", function(event)
+ if set_affiliation_notify(event.room, event.value) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+module:hook("muc-set-affiliation", function(event)
+ local room = event.room;
+ if not event.in_room and get_affiliation_notify(room) then
+ local body = string.format("Your affiliation in room %s is now %s.", room.jid, event.affiliation);
+ local stanza = st.message({
+ type = "headline";
+ from = room.jid;
+ to = event.jid;
+ }, body)
+ :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
+ :tag("status", {code="101"}):up()
+ :up();
+ room:route_stanza(stanza);
+ end
+end);
+
+return {
+ get = get_affiliation_notify;
+ set = set_affiliation_notify;
+};
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/muc/description.lib.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/description.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,41 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local function get_description(room)
+ return room._data.description;
+end
+
+local function set_description(room, description)
+ if description == "" then description = nil; end
+ if get_description(room) == description then return false; end
+ room._data.description = description;
+ return true;
+end
+
+local function add_form_option(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_roomdesc";
+ type = "text-single";
+ label = "Description";
+ value = get_description(event.room) or "";
+ });
+end
+module:hook("muc-disco#info", add_form_option);
+module:hook("muc-config-form", add_form_option, 100-2);
+
+module:hook("muc-config-submitted/muc#roomconfig_roomdesc", function(event)
+ if set_description(event.room, event.value) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+return {
+ get = get_description;
+ set = set_description;
+};
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/muc/hidden.lib.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/hidden.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,43 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local function get_hidden(room)
+ return room._data.hidden;
+end
+
+local function set_hidden(room, hidden)
+ hidden = hidden and true or nil;
+ if get_hidden(room) == hidden then return false; end
+ room._data.hidden = hidden;
+ return true;
+end
+
+module:hook("muc-config-form", function(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_publicroom";
+ type = "boolean";
+ label = "Make Room Publicly Searchable?";
+ value = not get_hidden(event.room);
+ });
+end, 100-5);
+
+module:hook("muc-config-submitted/muc#roomconfig_publicroom", function(event)
+ if set_hidden(event.room, not event.value) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+module:hook("muc-disco#info", function(event)
+ event.reply:tag("feature", {var = get_hidden(event.room) and "muc_hidden" or "muc_public"}):up();
+end);
+
+return {
+ get = get_hidden;
+ set = set_hidden;
+};
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/muc/history.lib.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/history.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,171 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local gettime = os.time;
+local datetime = require "util.datetime";
+local st = require "util.stanza";
+
+local default_history_length = 20;
+local max_history_length = module:get_option_number("max_history_messages", math.huge);
+
+local function set_max_history_length(_max_history_length)
+ max_history_length = _max_history_length or math.huge;
+end
+
+local function get_historylength(room)
+ return math.min(room._data.history_length or default_history_length, max_history_length);
+end
+
+local function set_historylength(room, length)
+ if length then
+ length = assert(tonumber(length), "Length not a valid number");
+ end
+ if length == default_history_length then length = nil; end
+ room._data.history_length = length;
+ return true;
+end
+
+module:hook("muc-config-form", function(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_historylength";
+ type = "text-single";
+ label = "Maximum Number of History Messages Returned by Room";
+ value = tostring(get_historylength(event.room));
+ });
+end, 100-10);
+
+module:hook("muc-config-submitted/muc#roomconfig_historylength", function(event)
+ if set_historylength(event.room, event.value) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+local function parse_history(stanza)
+ local x_tag = stanza:get_child("x", "http://jabber.org/protocol/muc");
+ local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc");
+ if not history_tag then
+ return nil, default_history_length, nil;
+ end
+
+ local maxchars = tonumber(history_tag.attr.maxchars);
+
+ local maxstanzas = tonumber(history_tag.attr.maxstanzas);
+
+ -- messages received since the UTC datetime specified
+ local since = history_tag.attr.since;
+ if since then
+ since = datetime.parse(since);
+ end
+
+ -- messages received in the last "X" seconds.
+ local seconds = tonumber(history_tag.attr.seconds);
+ if seconds then
+ seconds = gettime() - seconds;
+ if since then
+ since = math.max(since, seconds);
+ else
+ since = seconds;
+ end
+ end
+
+ return maxchars, maxstanzas, since;
+end
+
+module:hook("muc-get-history", function(event)
+ local room = event.room;
+ local history = room._history; -- send discussion history
+ if not history then return nil end
+ local history_len = #history;
+
+ local to = event.to;
+ local maxchars = event.maxchars;
+ local maxstanzas = event.maxstanzas or history_len;
+ local since = event.since;
+ local n = 0;
+ local charcount = 0;
+ for i=history_len,1,-1 do
+ local entry = history[i];
+ if maxchars then
+ if not entry.chars then
+ entry.stanza.attr.to = "";
+ entry.chars = #tostring(entry.stanza);
+ end
+ charcount = charcount + entry.chars + #to;
+ if charcount > maxchars then break; end
+ end
+ if since and since > entry.timestamp then break; end
+ if n + 1 > maxstanzas then break; end
+ n = n + 1;
+ end
+
+ local i = history_len-n+1
+ function event.next_stanza()
+ if i > history_len then return nil end
+ local entry = history[i];
+ local msg = entry.stanza;
+ msg.attr.to = to;
+ i = i + 1;
+ return msg;
+ end
+ return true;
+end, -1);
+
+local function send_history(room, stanza)
+ local maxchars, maxstanzas, since = parse_history(stanza);
+ local event = {
+ room = room;
+ stanza = stanza;
+ to = stanza.attr.from; -- `to` is required to calculate the character count for `maxchars`
+ maxchars = maxchars, maxstanzas = maxstanzas, since = since;
+ next_stanza = function() end; -- events should define this iterator
+ };
+ module:fire_event("muc-get-history", event);
+ for msg in event.next_stanza, event do
+ room:route_stanza(msg);
+ end
+end
+
+-- Send history on join
+module:hook("muc-occupant-session-new", function(event)
+ send_history(event.room, event.stanza);
+end, 50); -- Before subject(20)
+
+-- add to history
+module:hook("muc-add-history", function(event)
+ local historic = event.stanza:get_child("body");
+ if historic then
+ local room = event.room
+ local history = room._history;
+ if not history then history = {}; room._history = history; end
+ local stanza = st.clone(event.stanza);
+ stanza.attr.to = "";
+ local ts = gettime();
+ local stamp = datetime.datetime(ts);
+ stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = module.host, stamp = stamp}):up(); -- XEP-0203
+ stanza:tag("x", {xmlns = "jabber:x:delay", from = module.host, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
+ local entry = { stanza = stanza, timestamp = ts };
+ table.insert(history, entry);
+ while #history > get_historylength(room) do table.remove(history, 1) end
+ end
+ return true;
+end, -1);
+
+-- Have a single muc-add-history event, so that plugins can mark it
+-- as handled without stopping other muc-broadcast-message handlers
+module:hook("muc-broadcast-message", function(event)
+ module:fire_event("muc-add-history", event);
+end);
+
+return {
+ set_max_length = set_max_history_length;
+ parse_history = parse_history;
+ send = send_history;
+ get_length = get_historylength;
+ set_length = set_historylength;
+};
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/muc/lock.lib.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/lock.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,62 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local st = require "util.stanza";
+
+local lock_rooms = module:get_option_boolean("muc_room_locking", false);
+local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
+
+local function lock(room)
+ module:fire_event("muc-room-locked", {room = room;});
+ room._data.locked = os.time() + lock_room_timeout;
+end
+local function unlock(room)
+ module:fire_event("muc-room-unlocked", {room = room;});
+ room._data.locked = nil;
+end
+local function is_locked(room)
+ local ts = room._data.locked or false;
+ if ts then
+ if ts < os.time() then return true; end
+ unlock(room);
+ end
+ return false;
+end
+
+if lock_rooms then
+ module:hook("muc-room-pre-create", function(event)
+ -- Older groupchat protocol doesn't lock
+ if not event.stanza:get_child("x", "http://jabber.org/protocol/muc") then return end
+ -- Lock room at creation
+ local room = event.room;
+ lock(room);
+ end, 10);
+end
+
+-- Don't let users into room while it is locked
+module:hook("muc-occupant-pre-join", function(event)
+ if not event.is_new_room and is_locked(event.room) then -- Deny entry
+ module:log("debug", "Room is locked, denying entry");
+ event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found"));
+ return true;
+ end
+end, -30);
+
+-- When config is submitted; unlock the room
+module:hook("muc-config-submitted", function(event)
+ if is_locked(event.room) then
+ unlock(event.room);
+ end
+end, -1);
+
+return {
+ lock = lock;
+ unlock = unlock;
+ is_locked = is_locked;
+};
diff -r 3d21c63ec03f -r 4e6710b4f873 plugins/muc/members_only.lib.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/muc/members_only.lib.lua Fri Dec 01 04:54:23 2017 +0100
@@ -0,0 +1,128 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local st = require "util.stanza";
+
+local muc_util = module:require "muc/util";
+local valid_affiliations = muc_util.valid_affiliations;
+
+local function get_members_only(room)
+ return room._data.members_only;
+end
+
+local function set_members_only(room, members_only)
+ members_only = members_only and true or nil;
+ if room._data.members_only == members_only then return false; end
+ room._data.members_only = members_only;
+ if members_only then
+ --[[
+ If as a result of a change in the room configuration the room type is
+ changed to members-only but there are non-members in the room,
+ the service MUST remove any non-members from the room and include a
+ status code of 322 in the presence unavailable stanzas sent to those users
+ as well as any remaining occupants.
+ ]]
+ local occupants_changed = {};
+ for _, occupant in room:each_occupant() do
+ local affiliation = room:get_affiliation(occupant.bare_jid);
+ if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
+ occupant.role = nil;
+ room:save_occupant(occupant);
+ occupants_changed[occupant] = true;
+ end
+ end
+ local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
+ :tag("status", {code="322"}):up();
+ for occupant in pairs(occupants_changed) do
+ room:publicise_occupant_status(occupant, x);
+ module:fire_event("muc-occupant-left", {room = room; nick = occupant.nick; occupant = occupant;});
+ end
+ end
+ return true;
+end
+
+module:hook("muc-disco#info", function(event)
+ event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
+end);
+
+module:hook("muc-config-form", function(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_membersonly";
+ type = "boolean";
+ label = "Make Room Members-Only?";
+ value = get_members_only(event.room);
+ });
+end, 100-6);
+
+module:hook("muc-config-submitted/muc#roomconfig_membersonly", function(event)
+ if set_members_only(event.room, event.value) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+-- No affiliation => role of "none"
+module:hook("muc-get-default-role", function(event)
+ if not event.affiliation and get_members_only(event.room) then
+ return false;
+ end
+end);
+
+-- registration required for entering members-only room
+module:hook("muc-occupant-pre-join", function(event)
+ local room = event.room;
+ if get_members_only(room) then
+ local stanza = event.stanza;
+ local affiliation = room:get_affiliation(stanza.attr.from);
+ if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
+ local reply = st.error_reply(stanza, "auth", "registration-required"):up();
+ reply.tags[1].attr.code = "407";
+ event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
+ return true;
+ end
+ end
+end, -5);
+
+-- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
+-- if a member without privileges to edit the member list attempts to invite another user
+-- the service SHOULD return a