Comparison

plugins/mod_external_services.lua @ 11755:ae565e49289a

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
parent 11754:21a9b3f2a728
child 11756:a0e17b7c8b05
comparison
equal deleted inserted replaced
11754:21a9b3f2a728 11755:ae565e49289a
120 __newindex = function (self, i, v) 120 __newindex = function (self, i, v)
121 rawset(self, i, assert(prepare(v), "Invalid service entry added")); 121 rawset(self, i, assert(prepare(v), "Invalid service entry added"));
122 end; 122 end;
123 } 123 }
124 124
125 function get_services()
126 local extras = module:get_host_items("external_service");
127 local services = ( configured_services + extras ) / prepare;
128
129 setmetatable(services, services_mt);
130
131 return services;
132 end
133
125 local function handle_services(event) 134 local function handle_services(event)
126 local origin, stanza = event.origin, event.stanza; 135 local origin, stanza = event.origin, event.stanza;
127 local action = stanza.tags[1]; 136 local action = stanza.tags[1];
128 137
129 local user_bare = jid.bare(stanza.attr.from); 138 local user_bare = jid.bare(stanza.attr.from);
132 origin.send(st.error_reply(stanza, "auth", "forbidden")); 141 origin.send(st.error_reply(stanza, "auth", "forbidden"));
133 return true; 142 return true;
134 end 143 end
135 144
136 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns }); 145 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns });
137 local extras = module:get_host_items("external_service"); 146 local services = get_services();
138 local services = ( configured_services + extras ) / prepare;
139 147
140 local requested_type = action.attr.type; 148 local requested_type = action.attr.type;
141 if requested_type then 149 if requested_type then
142 services:filter(function(item) 150 services:filter(function(item)
143 return item.type == requested_type; 151 return item.type == requested_type;
144 end); 152 end);
145 end 153 end
146
147 setmetatable(services, services_mt);
148 154
149 module:fire_event("external_service/services", { 155 module:fire_event("external_service/services", {
150 origin = origin; 156 origin = origin;
151 stanza = stanza; 157 stanza = stanza;
152 reply = reply; 158 reply = reply;
179 origin.send(st.error_reply(stanza, "auth", "forbidden", "The 'port' and 'type' attributes are required.")); 185 origin.send(st.error_reply(stanza, "auth", "forbidden", "The 'port' and 'type' attributes are required."));
180 return true; 186 return true;
181 end 187 end
182 188
183 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); 189 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns });
184 local extras = module:get_host_items("external_service"); 190 local services = get_services();
185 local services = ( configured_services + extras ) / prepare;
186 services:filter(function (item) 191 services:filter(function (item)
187 return item.restricted; 192 return item.restricted;
188 end) 193 end)
189 194
190 local requested_credentials = set.new(); 195 local requested_credentials = set.new();
195 end 200 end
196 201
197 requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host, 202 requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host,
198 tonumber(service.attr.port) or 0)); 203 tonumber(service.attr.port) or 0));
199 end 204 end
200
201 setmetatable(services, services_mt);
202 205
203 module:fire_event("external_service/credentials", { 206 module:fire_event("external_service/credentials", {
204 origin = origin; 207 origin = origin;
205 stanza = stanza; 208 stanza = stanza;
206 reply = reply; 209 reply = reply;