Diff

plugins/mod_external_services.lua @ 11040:c560531d9a6e

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.
author Kim Alvefur <zash@zash.se>
date Mon, 17 Aug 2020 00:24:11 +0200
parent 11039:ec6919401790
child 11626:ef62d29c8fdc
line wrap: on
line diff
--- 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;