Mercurial > prosody-modules
comparison mod_external_services/mod_external_services.lua @ 4667:1990611691cf
mod_external_services: Factor out public function returning current services
This way you get the _prepared_ services and don't have to do that mapping
yourself.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 30 Aug 2021 00:11:58 +0200 |
parents | dbc7ba3cc27c |
children | ede9682c2022 |
comparison
equal
deleted
inserted
replaced
4666:dbc7ba3cc27c | 4667:1990611691cf |
---|---|
113 __newindex = function (self, i, v) | 113 __newindex = function (self, i, v) |
114 rawset(self, i, assert(prepare(v), "Invalid service entry added")); | 114 rawset(self, i, assert(prepare(v), "Invalid service entry added")); |
115 end; | 115 end; |
116 } | 116 } |
117 | 117 |
118 function get_services() | |
119 local extras = module:get_host_items("external_service"); | |
120 local services = ( configured_services + extras ) / prepare; | |
121 | |
122 setmetatable(services, services_mt); | |
123 | |
124 return services; | |
125 end | |
126 | |
118 local function handle_services(event) | 127 local function handle_services(event) |
119 local origin, stanza = event.origin, event.stanza; | 128 local origin, stanza = event.origin, event.stanza; |
120 local action = stanza.tags[1]; | 129 local action = stanza.tags[1]; |
121 | 130 |
122 local user_bare = jid.bare(stanza.attr.from); | 131 local user_bare = jid.bare(stanza.attr.from); |
125 origin.send(st.error_reply(stanza, "auth", "forbidden")); | 134 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
126 return true; | 135 return true; |
127 end | 136 end |
128 | 137 |
129 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns }); | 138 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns }); |
130 local extras = module:get_host_items("external_service"); | 139 local services = get_services(); |
131 local services = ( configured_services + extras ) / prepare; | |
132 | 140 |
133 local requested_type = action.attr.type; | 141 local requested_type = action.attr.type; |
134 if requested_type then | 142 if requested_type then |
135 services:filter(function(item) | 143 services:filter(function(item) |
136 return item.type == requested_type; | 144 return item.type == requested_type; |
137 end); | 145 end); |
138 end | 146 end |
139 | |
140 setmetatable(services, services_mt); | |
141 | 147 |
142 module:fire_event("external_service/services", { | 148 module:fire_event("external_service/services", { |
143 origin = origin; | 149 origin = origin; |
144 stanza = stanza; | 150 stanza = stanza; |
145 reply = reply; | 151 reply = reply; |
172 origin.send(st.error_reply(stanza, "auth", "forbidden", "The 'port' and 'type' attributes are required.")); | 178 origin.send(st.error_reply(stanza, "auth", "forbidden", "The 'port' and 'type' attributes are required.")); |
173 return true; | 179 return true; |
174 end | 180 end |
175 | 181 |
176 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); | 182 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); |
177 local extras = module:get_host_items("external_service"); | 183 local services = get_services(); |
178 local services = ( configured_services + extras ) / prepare; | |
179 services:filter(function (item) | 184 services:filter(function (item) |
180 return item.restricted; | 185 return item.restricted; |
181 end) | 186 end) |
182 | 187 |
183 local requested_credentials = set.new(); | 188 local requested_credentials = set.new(); |
188 end | 193 end |
189 | 194 |
190 requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host, | 195 requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host, |
191 tonumber(service.attr.port) or 0)); | 196 tonumber(service.attr.port) or 0)); |
192 end | 197 end |
193 | |
194 setmetatable(services, services_mt); | |
195 | 198 |
196 module:fire_event("external_service/credentials", { | 199 module:fire_event("external_service/credentials", { |
197 origin = origin; | 200 origin = origin; |
198 stanza = stanza; | 201 stanza = stanza; |
199 reply = reply; | 202 reply = reply; |