# HG changeset patch # User Kim Alvefur # Date 1597616651 -7200 # Node ID c560531d9a6e262e33053f78401ad0a7170560f1 # Parent ec6919401790ad3162697bc338956ecc296030ea mod_external_services: Validate services added via events While writing developer documentation it became obvious that i was silly to have one item format for config and items API, and another format for the event API. Then there's the stanza format, but that's a common pattern. This change reduces the possible input formats to two and allows other modules the benefit of the processing and validation performed on items from the config. diff -r ec6919401790 -r c560531d9a6e plugins/mod_external_services.lua --- a/plugins/mod_external_services.lua Sat Jul 25 12:22:03 2020 +0200 +++ b/plugins/mod_external_services.lua Mon Aug 17 00:24:11 2020 +0200 @@ -4,6 +4,7 @@ local hashes = require "util.hashes"; local st = require "util.stanza"; local jid = require "util.jid"; +local array = require "util.array"; local default_host = module:get_option_string("external_service_host", module.host); local default_port = module:get_option_number("external_service_port"); @@ -105,6 +106,14 @@ end end +-- Ensure only valid items are added in events +local services_mt = { + __index = getmetatable(array()).__index; + __newindex = function (self, i, v) + rawset(self, i, assert(prepare(v), "Invalid service entry added")); + end; +} + local function handle_services(event) local origin, stanza = event.origin, event.stanza; local action = stanza.tags[1]; @@ -127,6 +136,8 @@ end); end + setmetatable(services, services_mt); + module:fire_event("external_service/services", { origin = origin; stanza = stanza; @@ -177,6 +188,9 @@ }); end + setmetatable(services, services_mt); + setmetatable(requested_credentials, services_mt); + module:fire_event("external_service/credentials", { origin = origin; stanza = stanza;