Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
11039:ec6919401790 | 11040:c560531d9a6e |
---|---|
2 local dt = require "util.datetime"; | 2 local dt = require "util.datetime"; |
3 local base64 = require "util.encodings".base64; | 3 local base64 = require "util.encodings".base64; |
4 local hashes = require "util.hashes"; | 4 local hashes = require "util.hashes"; |
5 local st = require "util.stanza"; | 5 local st = require "util.stanza"; |
6 local jid = require "util.jid"; | 6 local jid = require "util.jid"; |
7 local array = require "util.array"; | |
7 | 8 |
8 local default_host = module:get_option_string("external_service_host", module.host); | 9 local default_host = module:get_option_string("external_service_host", module.host); |
9 local default_port = module:get_option_number("external_service_port"); | 10 local default_port = module:get_option_number("external_service_port"); |
10 local default_secret = module:get_option_string("external_service_secret"); | 11 local default_secret = module:get_option_string("external_service_secret"); |
11 local default_ttl = module:get_option_number("external_service_ttl", 86400); | 12 local default_ttl = module:get_option_number("external_service_ttl", 86400); |
103 if #services == 0 then | 104 if #services == 0 then |
104 module:log("warn", "No services configured or all had errors"); | 105 module:log("warn", "No services configured or all had errors"); |
105 end | 106 end |
106 end | 107 end |
107 | 108 |
109 -- Ensure only valid items are added in events | |
110 local services_mt = { | |
111 __index = getmetatable(array()).__index; | |
112 __newindex = function (self, i, v) | |
113 rawset(self, i, assert(prepare(v), "Invalid service entry added")); | |
114 end; | |
115 } | |
116 | |
108 local function handle_services(event) | 117 local function handle_services(event) |
109 local origin, stanza = event.origin, event.stanza; | 118 local origin, stanza = event.origin, event.stanza; |
110 local action = stanza.tags[1]; | 119 local action = stanza.tags[1]; |
111 | 120 |
112 local user_bare = jid.bare(stanza.attr.from); | 121 local user_bare = jid.bare(stanza.attr.from); |
124 if requested_type then | 133 if requested_type then |
125 services:filter(function(item) | 134 services:filter(function(item) |
126 return item.type == requested_type; | 135 return item.type == requested_type; |
127 end); | 136 end); |
128 end | 137 end |
138 | |
139 setmetatable(services, services_mt); | |
129 | 140 |
130 module:fire_event("external_service/services", { | 141 module:fire_event("external_service/services", { |
131 origin = origin; | 142 origin = origin; |
132 stanza = stanza; | 143 stanza = stanza; |
133 reply = reply; | 144 reply = reply; |
174 type = service.attr.type; | 185 type = service.attr.type; |
175 host = service.attr.host; | 186 host = service.attr.host; |
176 port = tonumber(service.attr.port); | 187 port = tonumber(service.attr.port); |
177 }); | 188 }); |
178 end | 189 end |
190 | |
191 setmetatable(services, services_mt); | |
192 setmetatable(requested_credentials, services_mt); | |
179 | 193 |
180 module:fire_event("external_service/credentials", { | 194 module:fire_event("external_service/credentials", { |
181 origin = origin; | 195 origin = origin; |
182 stanza = stanza; | 196 stanza = stanza; |
183 reply = reply; | 197 reply = reply; |