plugins/register.lua

1
local verse = require "verse";
2
 
3
local xmlns_register = "jabber:iq:register";
4
 
5
function verse.plugins.register(stream)
6
	local function handle_features(features_stanza)
7
		if features_stanza:get_child("register", "http://jabber.org/features/iq-register") then
8
			local request = verse.iq({ to = stream.host_, type = "set" })
9
				:tag("query", { xmlns = xmlns_register })
10
					:tag("username"):text(stream.username):up()
11
					:tag("password"):text(stream.password):up();
12
			if stream.register_email then
13
				request:tag("email"):text(stream.register_email):up();
14
			end
15
			stream:send_iq(request, function (result)
16
				if result.attr.type == "result" then
17
					stream:event("registration-success");
18
				else
19
					local type, condition, text = result:get_error();
20
					stream:debug("Registration failed: %s", condition);
21
					stream:event("registration-failure", { type = type, condition = condition, text = text });
22
				end
23
			end);
24
		else
25
			stream:debug("In-band registration not offered by server");
26
			stream:event("registration-failure", { condition = "service-unavailable" });
27
		end
28
		stream:unhook("stream-features", handle_features);
29
		return true;
30
	end
31
	stream:hook("stream-features", handle_features, 310);
32
end