File

plugins/tls.lua @ 172:1a32e9ae79d0

plugins.disco: Fix disco_local_services() to call callback with empty array in case of disco error
author Matthew Wild <mwild1@gmail.com>
date Thu, 30 Dec 2010 20:19:47 +0000
parent 67:8154b72591d5
child 197:7e98cf2c1d8d
line wrap: on
line source

local st = require "util.stanza";
local xmlns_tls = "urn:ietf:params:xml:ns:xmpp-tls";

function verse.plugins.tls(stream)
	local function handle_features(features_stanza)
		if stream.authenticated then return; end
		if features_stanza:get_child("starttls", xmlns_tls) and stream.conn.starttls then
			stream:debug("Negotiating TLS...");
			stream:send(st.stanza("starttls", { xmlns = xmlns_tls }));
			return true;
		elseif not stream.conn.starttls and not stream.secure then
			stream:warn("SSL libary (LuaSec) not loaded, so TLS not available");
		elseif not stream.secure then
			stream:debug("Server doesn't offer TLS :(");
		end
	end
	local function handle_tls(tls_status)
		if tls_status.name == "proceed" then
			stream:debug("Server says proceed, handshake starting...");
			stream.conn:starttls({mode="client", protocol="sslv23", options="no_sslv2"}, true);
		end
	end
	local function handle_status(new_status)
		if new_status == "ssl-handshake-complete" then
			stream.secure = true;
			stream:debug("Re-opening stream...");
			stream:reopen();
		end
	end
	stream:hook("stream-features", handle_features, 400);
	stream:hook("stream/"..xmlns_tls, handle_tls);
	stream:hook("status", handle_status, 400);
	
	return true;
end