From c899c8d357a046e72d4e88c2e05a893c34af2650 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 8 Dec 2018 16:35:00 +0100 Subject: moduleapi: Use pack from util.table --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 10f9f04d..d2aa1e8c 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -20,7 +20,7 @@ local error, setmetatable, type = error, setmetatable, type; local ipairs, pairs, select = ipairs, pairs, select; 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 pack = table.pack or require "util.table".pack; -- table.pack is only in 5.2 local unpack = table.unpack or unpack; --luacheck: ignore 113 -- renamed in 5.2 local prosody = prosody; -- cgit v1.2.3 From f017415defc1a3764412a1edc0759e1a4b9aeea5 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 28 Dec 2018 20:51:31 +0100 Subject: core.moduleapi: Add a promise-based API for tracking IQ stanzas (fixes #714) --- core/moduleapi.lua | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index d2aa1e8c..f7aa7216 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -361,6 +361,71 @@ function api:send(stanza, origin) return core_post_stanza(origin or hosts[self.host], stanza); end +function api:send_iq(stanza, origin, timeout) + local iq_cache = self._iq_cache; + if not iq_cache then + iq_cache = require "util.cache".new(256, function (_, iq) + iq.reject("evicted"); + self:unhook(iq.result_event, iq.result_handler); + self:unhook(iq.error_event, iq.error_handler); + end); + self._iq_cache = iq_cache; + end + return require "util.promise".new(function (resolve, reject) + local event_type; + if stanza.attr.from == self.host then + event_type = "host"; + else -- assume bare since we can't hook full jids + event_type = "bare"; + end + local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; + local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; + local cache_key = event_type.."/"..stanza.attr.id; + + local function result_handler(event) + if event.stanza.attr.from == stanza.attr.to then + resolve(event); + return true; + end + end + + local function error_handler(event) + if event.stanza.attr.from == stanza.attr.to then + reject(event); + return true; + end + end + + if iq_cache:get(cache_key) then + error("choose another iq stanza id attribute") + end + + self:hook(result_event, result_handler); + self:hook(error_event, error_handler); + + local timeout_handle = self:add_timer(timeout or 120, function () + reject("timeout"); + self:unhook(result_event, result_handler); + self:unhook(error_event, error_handler); + iq_cache:set(cache_key, nil); + end); + + local ok = iq_cache:set(cache_key, { + reject = reject, resolve = resolve, + timeout_handle = timeout_handle, + result_event = result_event, error_event = error_event, + result_handler = result_handler, error_handler = error_handler; + }); + + if not ok then + reject("cache insertion failure"); + return; + end + + self:send(stanza, origin); + end); +end + function api:broadcast(jids, stanza, iter) for jid in (iter or it.values)(jids) do local new_stanza = st.clone(stanza); -- cgit v1.2.3 From 0fe56344ca10575746f969992b63b4173395eed2 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 30 Dec 2018 14:26:58 +0100 Subject: core.moduleapi: Move util imports to top --- core/moduleapi.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index f7aa7216..c7fff11f 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -14,6 +14,8 @@ local pluginloader = require "util.pluginloader"; local timer = require "util.timer"; local resolve_relative_path = require"util.paths".resolve_relative_path; local st = require "util.stanza"; +local cache = require "util.cache"; +local promise = require "util.promise"; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; local error, setmetatable, type = error, setmetatable, type; @@ -364,14 +366,14 @@ end function api:send_iq(stanza, origin, timeout) local iq_cache = self._iq_cache; if not iq_cache then - iq_cache = require "util.cache".new(256, function (_, iq) + iq_cache = cache.new(256, function (_, iq) iq.reject("evicted"); self:unhook(iq.result_event, iq.result_handler); self:unhook(iq.error_event, iq.error_handler); end); self._iq_cache = iq_cache; end - return require "util.promise".new(function (resolve, reject) + return promise.new(function (resolve, reject) local event_type; if stanza.attr.from == self.host then event_type = "host"; -- cgit v1.2.3 From f102941562aa2228e1949261c91045ecbf71c18d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 30 Dec 2018 16:03:15 +0100 Subject: core.moduleapi: Use util.error for :send_iq errors --- core/moduleapi.lua | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index c7fff11f..57aa4e9f 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -15,6 +15,7 @@ local timer = require "util.timer"; local resolve_relative_path = require"util.paths".resolve_relative_path; local st = require "util.stanza"; local cache = require "util.cache"; +local errutil = require "util.error"; local promise = require "util.promise"; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; @@ -367,7 +368,10 @@ function api:send_iq(stanza, origin, timeout) local iq_cache = self._iq_cache; if not iq_cache then iq_cache = cache.new(256, function (_, iq) - iq.reject("evicted"); + iq.reject(errutil.new({ + type = "wait", condition = "resource-constraint", + text = "evicted from iq tracking cache" + })); self:unhook(iq.result_event, iq.result_handler); self:unhook(iq.error_event, iq.error_handler); end); @@ -393,20 +397,29 @@ function api:send_iq(stanza, origin, timeout) local function error_handler(event) if event.stanza.attr.from == stanza.attr.to then - reject(event); + local error_type, condition, text = event.stanza:get_error(); + local err = errutil.new({ type = error_type, condition = condition, text = text }, event); + reject(err); return true; end end if iq_cache:get(cache_key) then - error("choose another iq stanza id attribute") + reject(errutil.new({ + type = "modify", condition = "conflict", + text = "iq stanza id attribute already used", + })); + return; end self:hook(result_event, result_handler); self:hook(error_event, error_handler); local timeout_handle = self:add_timer(timeout or 120, function () - reject("timeout"); + reject(errutil.new({ + type = "wait", condition = "remote-server-timeout", + text = "IQ stanza timed out", + })); self:unhook(result_event, result_handler); self:unhook(error_event, error_handler); iq_cache:set(cache_key, nil); @@ -420,7 +433,10 @@ function api:send_iq(stanza, origin, timeout) }); if not ok then - reject("cache insertion failure"); + reject(errutil.new({ + type = "wait", condition = "internal-server-error", + text = "Could not store IQ tracking data" + })); return; end -- cgit v1.2.3 From a89dd30b7e9e04e3b4fb89efc45245970353608f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 30 Dec 2018 20:35:20 +0100 Subject: core.moduleapi: Use convenience function for creating error object from stanza --- core/moduleapi.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 57aa4e9f..c6193cfd 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -397,9 +397,7 @@ function api:send_iq(stanza, origin, timeout) local function error_handler(event) if event.stanza.attr.from == stanza.attr.to then - local error_type, condition, text = event.stanza:get_error(); - local err = errutil.new({ type = error_type, condition = condition, text = text }, event); - reject(err); + reject(errutil.from_stanza(event.stanza), event); return true; end end -- cgit v1.2.3 From 23577330fd9826da26a2ab0a6a3f1d6b82e5dfb8 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 19 Mar 2019 09:04:40 +0000 Subject: moduleapi: New API for modules to set a status --- core/moduleapi.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index c6193cfd..2db7433a 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -17,6 +17,8 @@ local st = require "util.stanza"; local cache = require "util.cache"; local errutil = require "util.error"; local promise = require "util.promise"; +local time_now = require "util.time".now; +local format = require "util.format".format; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; local error, setmetatable, type = error, setmetatable, type; @@ -513,4 +515,33 @@ function api:measure_global_event(event_name, stat_name) return self:measure_object_event(prosody.events.wrappers, event_name, stat_name); end +local status_priorities = { error = 3, warn = 2, info = 1, core = 0 }; + +function api:set_status(status_type, status_message, override) + local priority = status_priorities[status_type]; + if not priority then + self:log("error", "set_status: Invalid status type '%s', assuming 'info'"); + status_type, priority = "info", status_priorities.info; + end + local current_priority = status_priorities[self.status_type] or 0; + -- By default an 'error' status can only be overwritten by another 'error' status + if (current_priority >= status_priorities.error and priority < current_priority and override ~= true) + or (override == false and current_priority > priority) then + self:log("debug", "Ignoring status"); + return; + end + self.status_type, self.status_message, self.status_time = status_type, status_message, time_now(); + self:log("debug", "New status: %s", status_type); + self:fire_event("module-status/updated", { name = self.name }); +end + +function api:log_status(level, msg, ...) + self:set_status(level, format(msg, ...)); + return self:log(level, msg, ...); +end + +function api:get_status() + return self.status_type, self.status_message, self.status_time; +end + return api; -- cgit v1.2.3 From 3616d69edbd95a27b6edc042fb4fc512b584b71b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 26 Mar 2019 17:22:25 +0000 Subject: moduleapi: Remove overly-verbose debug logging on module status change --- core/moduleapi.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 2db7433a..e9e4c6d3 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -531,7 +531,6 @@ function api:set_status(status_type, status_message, override) return; end self.status_type, self.status_message, self.status_time = status_type, status_message, time_now(); - self:log("debug", "New status: %s", status_type); self:fire_event("module-status/updated", { name = self.name }); end -- cgit v1.2.3 From 36ad587977a55c0042e6aae283b10acbb50a87df Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 26 Mar 2019 17:22:56 +0000 Subject: moduleapi: Log suppressed status priority and message when not overriding --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index e9e4c6d3..b81bbeb2 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -527,7 +527,7 @@ function api:set_status(status_type, status_message, override) -- By default an 'error' status can only be overwritten by another 'error' status if (current_priority >= status_priorities.error and priority < current_priority and override ~= true) or (override == false and current_priority > priority) then - self:log("debug", "Ignoring status"); + self:log("debug", "moduleapi: ignoring status [prio %d override %s]: %s", priority, override, status_message); return; end self.status_type, self.status_message, self.status_time = status_type, status_message, time_now(); -- cgit v1.2.3 From 2dd7b5717587d249dbda1335be3b5975a1c96e59 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 21 Aug 2019 23:15:05 +0200 Subject: core.moduleapi: Uppercase "IQ stanza" for consistency It's written like that elsewhere in the send_iq method --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index b81bbeb2..dcdc41a4 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -407,7 +407,7 @@ function api:send_iq(stanza, origin, timeout) if iq_cache:get(cache_key) then reject(errutil.new({ type = "modify", condition = "conflict", - text = "iq stanza id attribute already used", + text = "IQ stanza id attribute already used", })); return; end -- cgit v1.2.3 From e0125bcb4c3a1c0f58d6fc291580ca4d54d8f331 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 21 Aug 2019 23:18:08 +0200 Subject: core.moduleapi: Restructure send_iq method for more atomic cleanup All cleanup in one spot instead of two, and at the end which fits with cleanup happening afterwards. --- core/moduleapi.lua | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index dcdc41a4..0a8adc36 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -374,22 +374,21 @@ function api:send_iq(stanza, origin, timeout) type = "wait", condition = "resource-constraint", text = "evicted from iq tracking cache" })); - self:unhook(iq.result_event, iq.result_handler); - self:unhook(iq.error_event, iq.error_handler); end); self._iq_cache = iq_cache; end - return promise.new(function (resolve, reject) - local event_type; - if stanza.attr.from == self.host then - event_type = "host"; - else -- assume bare since we can't hook full jids - event_type = "bare"; - end - local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; - local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; - local cache_key = event_type.."/"..stanza.attr.id; + local event_type; + if stanza.attr.from == self.host then + event_type = "host"; + else -- assume bare since we can't hook full jids + event_type = "bare"; + end + local result_event = "iq-result/"..event_type.."/"..stanza.attr.id; + local error_event = "iq-error/"..event_type.."/"..stanza.attr.id; + local cache_key = event_type.."/"..stanza.attr.id; + + local p = promise.new(function (resolve, reject) local function result_handler(event) if event.stanza.attr.from == stanza.attr.to then resolve(event); @@ -420,15 +419,11 @@ function api:send_iq(stanza, origin, timeout) type = "wait", condition = "remote-server-timeout", text = "IQ stanza timed out", })); - self:unhook(result_event, result_handler); - self:unhook(error_event, error_handler); - iq_cache:set(cache_key, nil); end); local ok = iq_cache:set(cache_key, { reject = reject, resolve = resolve, timeout_handle = timeout_handle, - result_event = result_event, error_event = error_event, result_handler = result_handler, error_handler = error_handler; }); @@ -442,6 +437,18 @@ function api:send_iq(stanza, origin, timeout) self:send(stanza, origin); end); + + p:finally(function () + local iq = iq_cache:get(cache_key); + if iq then + self:unhook(result_event, iq.result_handler); + self:unhook(error_event, iq.error_handler); + iq.timeout_handle:stop(); + iq_cache:set(cache_key, nil); + end + end); + + return p; end function api:broadcast(jids, stanza, iter) -- cgit v1.2.3 From cdd6144dcc649f612c86f786c4e7fe8a12653582 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 30 Dec 2019 09:53:10 +0100 Subject: core.moduleapi: Fix error context in :send_iq API It got passed as argument to reject() instead of the util.error function and was lost. --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 0a8adc36..dc1f899c 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -398,7 +398,7 @@ function api:send_iq(stanza, origin, timeout) local function error_handler(event) if event.stanza.attr.from == stanza.attr.to then - reject(errutil.from_stanza(event.stanza), event); + reject(errutil.from_stanza(event.stanza, event)); return true; end end -- cgit v1.2.3 From adc4440fd83a3b9124f91ad38eb8aa2c933284cc Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 30 Dec 2019 09:54:49 +0100 Subject: core.moduleapi: Rename local name for util.error for consistency It's called 'errors' everywhere else except here. --- core/moduleapi.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index dc1f899c..5e8438a8 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -15,7 +15,7 @@ local timer = require "util.timer"; local resolve_relative_path = require"util.paths".resolve_relative_path; local st = require "util.stanza"; local cache = require "util.cache"; -local errutil = require "util.error"; +local errors = require "util.error"; local promise = require "util.promise"; local time_now = require "util.time".now; local format = require "util.format".format; @@ -370,7 +370,7 @@ function api:send_iq(stanza, origin, timeout) local iq_cache = self._iq_cache; if not iq_cache then iq_cache = cache.new(256, function (_, iq) - iq.reject(errutil.new({ + iq.reject(errors.new({ type = "wait", condition = "resource-constraint", text = "evicted from iq tracking cache" })); @@ -398,13 +398,13 @@ function api:send_iq(stanza, origin, timeout) local function error_handler(event) if event.stanza.attr.from == stanza.attr.to then - reject(errutil.from_stanza(event.stanza, event)); + reject(errors.from_stanza(event.stanza, event)); return true; end end if iq_cache:get(cache_key) then - reject(errutil.new({ + reject(errors.new({ type = "modify", condition = "conflict", text = "IQ stanza id attribute already used", })); @@ -415,7 +415,7 @@ function api:send_iq(stanza, origin, timeout) self:hook(error_event, error_handler); local timeout_handle = self:add_timer(timeout or 120, function () - reject(errutil.new({ + reject(errors.new({ type = "wait", condition = "remote-server-timeout", text = "IQ stanza timed out", })); @@ -428,7 +428,7 @@ function api:send_iq(stanza, origin, timeout) }); if not ok then - reject(errutil.new({ + reject(errors.new({ type = "wait", condition = "internal-server-error", text = "Could not store IQ tracking data" })); -- cgit v1.2.3 From 3db27c369600393497d626c7b1a3e8c2c0365338 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 16 Jan 2020 16:30:26 +0100 Subject: core.moduleapi: Hook correct event type in some cases In rare cases, module.host can be a bare JID, in which case this test did the wrong thing. --- core/moduleapi.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 5e8438a8..87c337d6 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -19,6 +19,7 @@ local errors = require "util.error"; local promise = require "util.promise"; local time_now = require "util.time".now; local format = require "util.format".format; +local jid_node = require "util.jid".node; local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; local error, setmetatable, type = error, setmetatable, type; @@ -379,7 +380,7 @@ function api:send_iq(stanza, origin, timeout) end local event_type; - if stanza.attr.from == self.host then + if not jid_node(stanza.attr.from) then event_type = "host"; else -- assume bare since we can't hook full jids event_type = "bare"; -- cgit v1.2.3 From 7e181e31e0fdce18e0ce326f612edb2e9428fc13 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Mar 2020 22:32:26 +0100 Subject: moduleapi: Fix handling of replies to :send_iq from internal modules Unclear exactly why, but replies to some queries to local modules would be discarded by stanza_router. This appears to fix it. --- core/moduleapi.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 87c337d6..71239a0c 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -436,7 +436,16 @@ function api:send_iq(stanza, origin, timeout) return; end - self:send(stanza, origin); + local wrapped_origin = setmetatable({ + -- XXX Needed in some cases for replies to work correctly when sending queries internally. + send = function (stanza) + resolve({ stanza = stanza }); + end; + }, { + __index = origin or hosts[self.host]; + }); + + self:send(stanza, wrapped_origin); end); p:finally(function () -- cgit v1.2.3 From 38742f7b507df3687d7d068ff620611a84d874ca Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 1 Apr 2020 22:32:50 +0200 Subject: moduleapi: Rename argument to silence luacheck --- core/moduleapi.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 71239a0c..021db4c8 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -438,8 +438,8 @@ function api:send_iq(stanza, origin, timeout) local wrapped_origin = setmetatable({ -- XXX Needed in some cases for replies to work correctly when sending queries internally. - send = function (stanza) - resolve({ stanza = stanza }); + send = function (reply) + resolve({ stanza = reply }); end; }, { __index = origin or hosts[self.host]; -- cgit v1.2.3 From 1f80e42aa5522074c5fa09333307a1c3ce50dc58 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 4 Jan 2019 07:00:45 +0100 Subject: core.moduleapi: Allow passing a config table trough :measure --- core/moduleapi.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 021db4c8..1212db5a 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -508,9 +508,9 @@ function api:open_store(name, store_type) return require"core.storagemanager".open(self.host, name or self.name, store_type); end -function api:measure(name, stat_type) +function api:measure(name, stat_type, conf) local measure = require "core.statsmanager".measure; - return measure(stat_type, "/"..self.host.."/mod_"..self.name.."/"..name); + return measure(stat_type, "/"..self.host.."/mod_"..self.name.."/"..name, conf); end function api:measure_object_event(events_object, event_name, stat_name) -- cgit v1.2.3 From 58be93c18484c9e12f276f5bb0f8d49e3d833b47 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 7 Oct 2020 16:10:51 +0200 Subject: core.moduleapi: Use resource path for :load_resource() --- core/moduleapi.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 1212db5a..0f43478c 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -500,7 +500,7 @@ function api:get_directory() end function api:load_resource(path, mode) - path = resolve_relative_path(self:get_directory(), path); + path = resolve_relative_path(self.resource_path or self:get_directory(), path); return io.open(path, mode); end -- cgit v1.2.3 From d53dcee89074430a831a5d8e46c17de61a5aa065 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 9 Oct 2020 16:37:15 +0200 Subject: core.moduleapi: Return resource path from module:get_directory() (API BC) :get_directory has so far returned the base directory of the current module source code. This has worked well so far to load resources which tend to be included in the same directory, but with the plugin installer using LuaRocks, extra resources (e.g. templates and other assets) these are saved in a completely different directory. In be73df6765b9 core.modulemanager gained some code for finding that directory and saving it in module.resource_path but now the question is how this should be reflected in the API. A survey of community modules suggest the vast majority use the :get_directory method for locating templates and other assets, rather than the code (which would use module:require instead). Therefore this commit changes :get_directory to return the resource_path when available. This should work for most modules. --- core/moduleapi.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/moduleapi.lua') diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 0f43478c..82002737 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -496,11 +496,11 @@ end local path_sep = package.config:sub(1,1); function api:get_directory() - return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; + return self.resource_path or self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; end function api:load_resource(path, mode) - path = resolve_relative_path(self.resource_path or self:get_directory(), path); + path = resolve_relative_path(self:get_directory(), path); return io.open(path, mode); end -- cgit v1.2.3