plugins/mod_external_services.lua
changeset 11044 c560531d9a6e
parent 11043 ec6919401790
child 11630 ef62d29c8fdc
equal deleted inserted replaced
11043:ec6919401790 11044: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;