File

plugins/bind.lua @ 450:e72deac76e0e

plugins.smacks: Change to track enabled state per direction Counting outgoing stanzas should start after <enable> is sent, while counting incoming stanzas should star after receiving <enabled/> This should also help with failed resumptions
author Kim Alvefur <zash@zash.se>
date Sat, 19 Feb 2022 15:57:24 +0100
parent 300:b1d50f9a04c7
child 457:73d4eb93657b
line wrap: on
line source

local verse = require "verse";
local jid = require "util.jid";

local xmlns_bind = "urn:ietf:params:xml:ns:xmpp-bind";

function verse.plugins.bind(stream)
	local function handle_features(features)
		if stream.bound then return; end
		stream:debug("Binding resource...");
		stream:send_iq(verse.iq({ type = "set" }):tag("bind", {xmlns=xmlns_bind}):tag("resource"):text(stream.resource),
			function (reply)
				if reply.attr.type == "result" then
					local result_jid = reply
						:get_child("bind", xmlns_bind)
							:get_child_text("jid");
					stream.username, stream.host, stream.resource = jid.split(result_jid);
					stream.jid, stream.bound = result_jid, true;
					stream:event("bind-success", { jid = result_jid });
				elseif reply.attr.type == "error" then
					local err = reply:child_with_name("error");
					local type, condition, text = reply:get_error();
					stream:event("bind-failure", { error = condition, text = text, type = type });
				end
			end);
	end
	stream:hook("stream-features", handle_features, 200);
	return true;
end