mod_extdisco/mod_extdisco.lua
changeset 3609 452ae6effd02
parent 3608 60df58c5f7dd
child 3610 7811ba467525
equal deleted inserted replaced
3608:60df58c5f7dd 3609:452ae6effd02
     1 local st = require "util.stanza";
     1 local st = require "util.stanza";
     2 
     2 
     3 local services = module:get_option("external_services");
     3 local services = module:get_option("external_services");
     4 
     4 
     5 local xmlns_extdisco = "urn:xmpp:extdisco:1";
     5 local xmlns_extdisco_1 = "urn:xmpp:extdisco:1";
       
     6 local xmlns_extdisco_2 = "urn:xmpp:extdisco:2";
     6 
     7 
     7 module:add_feature(xmlns_extdisco);
     8 module:add_feature(xmlns_extdisco_1);
       
     9 module:add_feature(xmlns_extdisco_2);
     8 
    10 
     9 module:hook("iq-get/host/"..xmlns_extdisco..":services", function (event)
    11 local function handle_services(event)
    10 	local origin, stanza = event.origin, event.stanza;
    12 	local origin, stanza = event.origin, event.stanza;
    11 	local service = stanza:get_child("service", xmlns_extdisco);
    13 	local service = stanza.tags[1];
    12 	local service_type = service and service.attr.type;
    14 	local service_type = service and service.attr.type;
    13 	local reply = st.reply(stanza):tag("services", { xmlns = xmlns_extdisco });
    15 	local reply = st.reply(stanza):tag("services", { xmlns = service.attr.xmlns });
    14 	for host, service_info in pairs(services) do
    16 	for host, service_info in pairs(services) do
    15 		if not(service_type) or service_info.type == service_type then
    17 		if not(service_type) or service_info.type == service_type then
    16 			reply:tag("service", {
    18 			reply:tag("service", {
    17 				host = host;
    19 				host = host;
    18 				port = service_info.port;
    20 				port = service_info.port;
    23 			}):up();
    25 			}):up();
    24 		end
    26 		end
    25 	end
    27 	end
    26 	origin.send(reply);
    28 	origin.send(reply);
    27 	return true;
    29 	return true;
    28 end);
    30 end
       
    31 module:hook("iq-get/host/"..xmlns_extdisco_1..":services", handle_services);
       
    32 module:hook("iq-get/host/"..xmlns_extdisco_2..":services", handle_services);
    29 
    33 
    30 module:hook("iq-get/host/"..xmlns_extdisco..":credentials", function (event)
    34 local function handle_credentials(event)
    31 	local origin, stanza = event.origin, event.stanza;
    35 	local origin, stanza = event.origin, event.stanza;
    32 	local credentials = stanza:get_child("credentials", xmlns_extdisco);
    36 	local credentials = stanza.tags[1];
    33 	local host = credentials and credentials.attr.host;
    37 	local host = credentials and credentials.attr.host;
    34 	if not host then
    38 	if not host then
    35 		origin.send(st.error_reply(stanza, "cancel", "bad-request", "No host specified"));
    39 		origin.send(st.error_reply(stanza, "cancel", "bad-request", "No host specified"));
    36 		return true;
    40 		return true;
    37 	end
    41 	end
    39 	if not service_info then
    43 	if not service_info then
    40 		origin.send(st.error_reply(stanza, "cancel", "item-not-found", "No such service known"));
    44 		origin.send(st.error_reply(stanza, "cancel", "item-not-found", "No such service known"));
    41 		return true;
    45 		return true;
    42 	end
    46 	end
    43 	local reply = st.reply(stanza)
    47 	local reply = st.reply(stanza)
    44 		:tag("credentials", { xmlns = xmlns_extdisco })
    48 		:tag("credentials", { xmlns = credentials.attr.xmlns })
    45 			:tag("service", {
    49 			:tag("service", {
    46 				host = host;
    50 				host = host;
    47 				username = service_info.username;
    51 				username = service_info.username;
    48 				password = service_info.password;
    52 				password = service_info.password;
    49 			}):up();
    53 			}):up();
    50 	origin.send(reply);
    54 	origin.send(reply);
    51 	return true;
    55 	return true;
    52 end);
    56 end
       
    57 module:hook("iq-get/host/"..xmlns_extdisco_1..":credentials", handle_credentials);
       
    58 module:hook("iq-get/host/"..xmlns_extdisco_2..":credentials", handle_credentials);