plugins/bind.lua

1
local verse = require "verse";
2
local jid = require "prosody.util.jid";
3
 
4
local xmlns_bind = "urn:ietf:params:xml:ns:xmpp-bind";
5
 
6
function verse.plugins.bind(stream)
7
	local function handle_features(features)
8
		if stream.bound then return; end
9
		stream:debug("Binding resource...");
10
		stream:send_iq(verse.iq({ id="bind",type = "set" }):tag("bind", {xmlns=xmlns_bind}):tag("resource"):text(stream.resource),
11
			function (reply)
12
				if reply.attr.type == "result" then
13
					local result_jid = reply
14
						:get_child("bind", xmlns_bind)
15
							:get_child_text("jid");
16
					stream.username, stream.host, stream.resource = jid.split(result_jid);
17
					stream.jid, stream.bound = result_jid, true;
18
					stream:event("bind-success", { jid = result_jid });
19
				elseif reply.attr.type == "error" then
20
					local err = reply:child_with_name("error");
21
					local type, condition, text = reply:get_error();
22
					stream:event("bind-failure", { error = condition, text = text, type = type });
23
				end
24
			end);
25
	end
26
	stream:hook("stream-features", handle_features, 200);
27
	return true;
28
end