mod_component_http/mod_component_http.lua
changeset 2956 e8462d6dbc6d
parent 2954 18e6d437003f
child 2962 13acce68a89c
equal deleted inserted replaced
2955:01ed9eb111d4 2956:e8462d6dbc6d
    40 		body = stanza.name == "message" and stanza:get_child_text("body") or nil;
    40 		body = stanza.name == "message" and stanza:get_child_text("body") or nil;
    41 		stanza = tostring(stanza);
    41 		stanza = tostring(stanza);
    42 	});
    42 	});
    43 	http.request(url, {
    43 	http.request(url, {
    44 		body = request_body;
    44 		body = request_body;
    45 	}, function (response_text, code, req, response)
    45 	}, function (response_text, code, _, response)
    46 		if stanza.attr.type == "error" then return; end -- Avoid error loops, don't reply to error stanzas		
    46 		if stanza.attr.type == "error" then return; end -- Avoid error loops, don't reply to error stanzas
    47 		if code == 200 and response_text and response.headers["content-type"] == "application/json" then
    47 		if code == 200 and response_text and response.headers["content-type"] == "application/json" then
    48 			local response_data = json.decode(response_text);
    48 			local response_data = json.decode(response_text);
    49 			if response_data.stanza then
    49 			if response_data.stanza then
    50 				local reply_stanza = xml.parse(response_data.stanza);
    50 				local reply_stanza = xml.parse(response_data.stanza);
    51 				if reply_stanza then
    51 				if reply_stanza then
    84 	end
    84 	end
    85 end
    85 end
    86 
    86 
    87 -- Simple handler for an always-online JID that allows everyone to subscribe to presence
    87 -- Simple handler for an always-online JID that allows everyone to subscribe to presence
    88 local function default_presence_handler(event)
    88 local function default_presence_handler(event)
    89 	local origin, stanza = event.origin, event.stanza;
    89 	local stanza = event.stanza;
    90 	module:log("debug", "Handling %s", tostring(stanza));
    90 	module:log("debug", "Handling %s", tostring(stanza));
    91 	if stanza.attr.type == "probe" then
    91 	if stanza.attr.type == "probe" then
    92 		module:send(st.presence({ to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    92 		module:send(st.presence({ to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    93 	elseif stanza.attr.type == "subscribe" then
    93 	elseif stanza.attr.type == "subscribe" then
    94 		module:send(st.presence({ type = "subscribed", to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    94 		module:send(st.presence({ type = "subscribed", to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    95 		module:send(st.presence({ to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    95 		module:send(st.presence({ to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    96 	elseif stanza.attr.type == "unsubscribe" then
    96 	elseif stanza.attr.type == "unsubscribe" then
    97 		module:send(st.presence({ type = "unavailable", to = stanza.attr.from, from = stanza.attr.to.."/default" }));	
    97 		module:send(st.presence({ type = "unavailable", to = stanza.attr.from, from = stanza.attr.to.."/default" }));
    98 	end
    98 	end
    99 	return true;
    99 	return true;
   100 end
   100 end
   101 
   101 
   102 module:hook("presence/bare", default_presence_handler, -1);
   102 module:hook("presence/bare", default_presence_handler, -1);