From e8872af8a09454fbeff06eefbd75d7967d468f9a Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 8 Jul 2017 18:21:45 +0200 Subject: mod_saslauth: Use correct varible name (thanks Roi) --- plugins/mod_saslauth.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mod_saslauth.lua b/plugins/mod_saslauth.lua index d374633e..a23d1f53 100644 --- a/plugins/mod_saslauth.lua +++ b/plugins/mod_saslauth.lua @@ -105,7 +105,7 @@ module:hook_stanza(xmlns_sasl, "failure", function (session, stanza) end end if text and condition then - condition = connection .. ": " .. text; + condition = condition .. ": " .. text; end module:log("info", "SASL EXTERNAL with %s failed: %s", session.to_host, condition); -- cgit v1.2.3 From 255073d6c9ea8b7718704ec4e8a1aeb524363eed Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 25 Jul 2017 13:16:31 +0200 Subject: util.dependencies: Add compatibility code for LuaSocket no longer exporting as a global --- util/dependencies.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util/dependencies.lua b/util/dependencies.lua index 491bfd9b..a259c263 100644 --- a/util/dependencies.lua +++ b/util/dependencies.lua @@ -79,6 +79,9 @@ function check_dependencies() ["Source"] = "http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/"; }); fatal = true; + elseif not _G.socket then + -- COMPAT Code expecting LuaSocket to export as a global + _G.socket = socket; end local lfs, err = softreq "lfs" -- cgit v1.2.3 From 9437d97a37b16df525d134101ec059c19f470fce Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 25 Jul 2017 13:25:49 +0200 Subject: util.dependencies: Add comment about LuaSec compat --- util/dependencies.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/util/dependencies.lua b/util/dependencies.lua index a259c263..7ec56022 100644 --- a/util/dependencies.lua +++ b/util/dependencies.lua @@ -103,6 +103,7 @@ function check_dependencies() ["Source"] = "http://www.inf.puc-rio.br/~brunoos/luasec/"; }, "SSL/TLS support will not be available"); elseif not _G.ssl then + -- COMPAT Code expecting LuaSec to export as a global (see #749) _G.ssl = ssl; _G.ssl.context = require "ssl.context"; _G.ssl.x509 = softreq "ssl.x509"; -- cgit v1.2.3 From 347f3ab6a3e1f7f64a9289ec08c081f79bef9087 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 10 Sep 2017 12:42:05 -0400 Subject: util.format: A string.format wrapper that gracefully handles invalid arguments --- util/format.lua | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 util/format.lua diff --git a/util/format.lua b/util/format.lua new file mode 100644 index 00000000..5f2b12be --- /dev/null +++ b/util/format.lua @@ -0,0 +1,74 @@ +-- +-- A string.format wrapper that gracefully handles invalid arguments +-- + +local tostring = tostring; +local select = select; +local assert = assert; +local unpack = unpack; +local type = type; + +local function format(format, ...) + local args, args_length = { ... }, select('#', ...); + + -- format specifier spec: + -- 1. Start: '%%' + -- 2. Flags: '[%-%+ #0]' + -- 3. Width: '%d?%d?' + -- 4. Precision: '%.?%d?%d?' + -- 5. Option: '[cdiouxXaAeEfgGqs%%]' + -- + -- The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument, whereas q and s expect a string. + -- This function does not accept string values containing embedded zeros, except as arguments to the q option. + -- a and A are only in Lua 5.2+ + + + -- process each format specifier + local i = 0; + format = format:gsub("%%[^cdiouxXaAeEfgGqs%%]*[cdiouxXaAeEfgGqs%%]", function(spec) + if spec ~= "%%" then + i = i + 1; + local arg = args[i]; + if arg == nil then -- special handling for nil + arg = "" + args[i] = ""; + end + + local option = spec:sub(-1); + if option == "q" or option == "s" then -- arg should be string + args[i] = tostring(arg); + elseif type(arg) ~= "number" then -- arg isn't number as expected? + args[i] = tostring(arg); + spec = "[%s]"; + end + end + return spec; + end); + + -- process extra args + while i < args_length do + i = i + 1; + local arg = args[i]; + if arg == nil then + args[i] = ""; + else + args[i] = tostring(arg); + end + format = format .. " [%s]" + end + + return format:format(unpack(args)); +end + +local function test() + assert(format("%s", "hello") == "hello"); + assert(format("%s") == ""); + assert(format("%s", true) == "true"); + assert(format("%d", true) == "[true]"); + assert(format("%%", true) == "% [true]"); +end + +return { + format = format; + test = test; +}; -- cgit v1.2.3 From 00cb31f022a3325e4e182b666f85a6b4e05b7a7e Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 10 Sep 2017 13:05:45 -0400 Subject: loggingmanager, mod_posix: Replace the old inconsistent log formatting with the new util.format --- core/loggingmanager.lua | 13 ++----------- plugins/mod_posix.lua | 9 +++------ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua index 14305588..1b888a7e 100644 --- a/core/loggingmanager.lua +++ b/core/loggingmanager.lua @@ -7,7 +7,7 @@ -- -- luacheck: globals log prosody.log -local format = string.format; +local format = require "util.format"; local setmetatable, rawset, pairs, ipairs, type = setmetatable, rawset, pairs, ipairs, type; local stdout = io.stdout; @@ -195,22 +195,13 @@ local function log_to_file(sink_config, logfile) local sourcewidth = sink_config.source_width; return function (name, level, message, ...) - local n = select('#', ...); - if n ~= 0 then - local arg = { ... }; - for i = 1, n do - arg[i] = tostring(arg[i]); - end - message = format(message, unpack(arg, 1, n)); - end - if sourcewidth then sourcewidth = math_max(#name+2, sourcewidth); name = name .. rep(" ", sourcewidth-#name); else name = name .. "\t"; end - write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", message, "\n"); + write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", format(message, ...), "\n"); end end log_sink_types.file = log_to_file; diff --git a/plugins/mod_posix.lua b/plugins/mod_posix.lua index 32fc27bc..b2984add 100644 --- a/plugins/mod_posix.lua +++ b/plugins/mod_posix.lua @@ -20,6 +20,7 @@ if not have_signal then module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); end +local format = require "util.format"; local lfs = require "lfs"; local stat = lfs.attributes; @@ -118,13 +119,9 @@ function syslog_sink_maker(config) -- luacheck: ignore 212/config pposix.syslog_open("prosody", module:get_option_string("syslog_facility")); syslog_opened = true; end - local syslog, format = pposix.syslog_log, string.format; + local syslog = pposix.syslog_log; return function (name, level, message, ...) - if ... then - syslog(level, name, format(message, ...)); - else - syslog(level, name, message); - end + syslog(level, name, format(message, ...)); end; end require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker); -- cgit v1.2.3 From f4aebb575ef73d5f9bce263af4146196c17cf66b Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 10 Sep 2017 13:13:24 -0400 Subject: loggingmanager: Slight cleanup and optimization of file sink --- core/loggingmanager.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua index 1b888a7e..2e9590fe 100644 --- a/core/loggingmanager.lua +++ b/core/loggingmanager.lua @@ -194,14 +194,15 @@ local function log_to_file(sink_config, logfile) -- Column width for "source" (used by stdout and console) local sourcewidth = sink_config.source_width; - return function (name, level, message, ...) - if sourcewidth then + if sourcewidth then + return function (name, level, message, ...) sourcewidth = math_max(#name+2, sourcewidth); - name = name .. rep(" ", sourcewidth-#name); - else - name = name .. "\t"; + write(logfile, timestamps and os_date(timestamps) or "", name, rep(" ", sourcewidth-#name), level, "\t", format(message, ...), "\n"); + end + else + return function (name, level, message, ...) + write(logfile, timestamps and os_date(timestamps) or "", name, "\t", level, "\t", format(message, ...), "\n"); end - write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", format(message, ...), "\n"); end end log_sink_types.file = log_to_file; -- cgit v1.2.3 From 35717a1e41b6b9364adfb4a6de39418f0f241f57 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 11 Sep 2017 19:32:12 +0200 Subject: loggingmanager, mod_posix: Import util.format correctly (fixes #985) --- core/loggingmanager.lua | 2 +- plugins/mod_posix.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua index 2e9590fe..3d469bed 100644 --- a/core/loggingmanager.lua +++ b/core/loggingmanager.lua @@ -7,7 +7,7 @@ -- -- luacheck: globals log prosody.log -local format = require "util.format"; +local format = require "util.format".format; local setmetatable, rawset, pairs, ipairs, type = setmetatable, rawset, pairs, ipairs, type; local stdout = io.stdout; diff --git a/plugins/mod_posix.lua b/plugins/mod_posix.lua index b2984add..fccc7a2b 100644 --- a/plugins/mod_posix.lua +++ b/plugins/mod_posix.lua @@ -20,7 +20,7 @@ if not have_signal then module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); end -local format = require "util.format"; +local format = require "util.format".format; local lfs = require "lfs"; local stat = lfs.attributes; -- cgit v1.2.3 From a5f43007f15cd3673ec0bf16f9644188624f541a Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 11 Sep 2017 19:32:51 +0200 Subject: core.loggingmanager: Remove now unused locals [luacheck] --- core/loggingmanager.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua index 3d469bed..6dc1c1d1 100644 --- a/core/loggingmanager.lua +++ b/core/loggingmanager.lua @@ -15,9 +15,6 @@ local io_open = io.open; local math_max, rep = math.max, string.rep; local os_date = os.date; local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; -local tostring = tostring; -local select = select; -local unpack = table.unpack or unpack; --luacheck: ignore 113 local config = require "core.configmanager"; local logger = require "util.logger"; -- cgit v1.2.3 From 863374e1643db918d328eac3185dab7b2abb0404 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 12 Sep 2017 14:42:56 +0200 Subject: mod_mam: Use a FIFO queue for scheduling archive expiry --- plugins/mod_mam/mod_mam.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 32dd0169..7511a1bc 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -56,6 +56,13 @@ local use_total = true; local cleanup; +local function schedule_cleanup(username) + if cleanup and not cleanup[username] then + table.insert(cleanup, username); + cleanup[username] = true; + end +end + -- Handle prefs. module:hook("iq/self/"..xmlns_mam..":prefs", function(event) local origin, stanza = event.origin, event.stanza; @@ -97,7 +104,7 @@ module:hook("iq-set/self/"..xmlns_mam..":query", function(event) local query = stanza.tags[1]; local qid = query.attr.queryid; - if cleanup then cleanup[origin.username] = true; end + schedule_cleanup(origin.username); -- Search query parameters local qwith, qstart, qend; @@ -304,7 +311,7 @@ local function message_handler(event, c2s) local id = ok; clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); event.stanza = clone_for_other_handlers; - if cleanup then cleanup[store_user] = true; end + schedule_cleanup(store_user); module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id }); end else @@ -353,13 +360,13 @@ if cleanup_after ~= "never" then pcall(function () -- If this works, then we schedule cleanup for all known users on startup for user in um.users(module.host) do - cleanup[user] = true; + schedule_cleanup(user); end end); -- At odd intervals, delete old messages for one user module:add_timer(math.random(10, 60), function() - local user = next(cleanup); + local user = table.remove(cleanup, 1); if user then module:log("debug", "Removing old messages for user %q", user); local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) -- cgit v1.2.3 From 015bc6ad59c4fec1c353d9af6b4e111a287c2148 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 12 Sep 2017 14:59:52 +0200 Subject: mod_mam: Log a message in case archive deletion is not supported by the storage module in use --- plugins/mod_mam/mod_mam.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 7511a1bc..298c770c 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -333,7 +333,9 @@ module:hook("pre-message/full", strip_stanza_id_after_other_events, -1); local cleanup_after = module:get_option_string("archive_expires_after", "1w"); local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); -if cleanup_after ~= "never" then +if not archive.delete then + module:log("debug", "Selected storage driver does not support deletion, archives will not expire"); +elseif cleanup_after ~= "never" then local day = 86400; local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day }; local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)"); -- cgit v1.2.3 From 12cdea49b391a7b2dea63856f4a689ed5668ea09 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 13 Sep 2017 18:18:57 +0200 Subject: mod_c2s: Iterate over child tags instead of child nodes in stream error (fixes traceback from #987) --- plugins/mod_c2s.lua | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/plugins/mod_c2s.lua b/plugins/mod_c2s.lua index 2bb919f8..fdb3b211 100644 --- a/plugins/mod_c2s.lua +++ b/plugins/mod_c2s.lua @@ -98,16 +98,14 @@ function stream_callbacks.error(session, error, data) session:close("not-well-formed"); elseif error == "stream-error" then local condition, text = "undefined-condition"; - for child in data:children() do - if child.attr.xmlns == xmlns_xmpp_streams then - if child.name ~= "text" then - condition = child.name; - else - text = child:get_text(); - end - if condition ~= "undefined-condition" and text then - break; - end + for child in data:childtags(nil, xmlns_xmpp_streams) do + if child.name ~= "text" then + condition = child.name; + else + text = child:get_text(); + end + if condition ~= "undefined-condition" and text then + break; end end text = condition .. (text and (" ("..text..")") or ""); -- cgit v1.2.3 From 5dee36d8385e36c4d72a97a33cf3c934af3d6633 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 14 Sep 2017 01:27:36 +0200 Subject: mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9) --- plugins/mod_component.lua | 18 ++++++++---------- plugins/mod_s2s/mod_s2s.lua | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/plugins/mod_component.lua b/plugins/mod_component.lua index 11abab79..acd70c60 100644 --- a/plugins/mod_component.lua +++ b/plugins/mod_component.lua @@ -151,16 +151,14 @@ function stream_callbacks.error(session, error, data, data2) session:close("not-well-formed"); elseif error == "stream-error" then local condition, text = "undefined-condition"; - for child in data:children() do - if child.attr.xmlns == xmlns_xmpp_streams then - if child.name ~= "text" then - condition = child.name; - else - text = child:get_text(); - end - if condition ~= "undefined-condition" and text then - break; - end + for child in data:childtags(nil, xmlns_xmpp_streams) do + if child.name ~= "text" then + condition = child.name; + else + text = child:get_text(); + end + if condition ~= "undefined-condition" and text then + break; end end text = condition .. (text and (" ("..text..")") or ""); diff --git a/plugins/mod_s2s/mod_s2s.lua b/plugins/mod_s2s/mod_s2s.lua index e038e5b4..10b81a17 100644 --- a/plugins/mod_s2s/mod_s2s.lua +++ b/plugins/mod_s2s/mod_s2s.lua @@ -416,16 +416,14 @@ function stream_callbacks.error(session, error, data) session:close("not-well-formed"); elseif error == "stream-error" then local condition, text = "undefined-condition"; - for child in data:children() do - if child.attr.xmlns == xmlns_xmpp_streams then - if child.name ~= "text" then - condition = child.name; - else - text = child:get_text(); - end - if condition ~= "undefined-condition" and text then - break; - end + for child in data:childtags(nil, xmlns_xmpp_streams) do + if child.name ~= "text" then + condition = child.name; + else + text = child:get_text(); + end + if condition ~= "undefined-condition" and text then + break; end end text = condition .. (text and (" ("..text..")") or ""); -- cgit v1.2.3