plugins/legacy.lua

1
local verse = require "verse";
2
local new_id = require "prosody.util.id".short;
3
 
4
local xmlns_auth = "jabber:iq:auth";
5
 
6
function verse.plugins.legacy(stream)
7
	local function handle_auth_form(result)
8
		local query = result:get_child("query", xmlns_auth);
9
		if result.attr.type ~= "result" or not query then
10
			local type, cond, text = result:get_error();
11
                       stream:debug("warn", "%s %s: %s", type, cond, text);
12
                       --stream:event("authentication-failure", { condition = cond });
13
                       -- COMPAT continue anyways
14
		end
15
		local auth_data = {
16
			username = stream.username;
17
			password = stream.password;
18
			resource = stream.resource or new_id();
19
			digest = false, sequence = false, token = false;
20
		};
21
		local request = verse.iq({ to = stream.host, type = "set" })
22
			:tag("query", { xmlns = xmlns_auth });
23
               if #query > 0 then
24
		for tag in query:childtags() do
25
			local field = tag.name;
26
			local value = auth_data[field];
27
			if value then
28
				request:tag(field):text(auth_data[field]):up();
29
			elseif value == nil then
30
				local cond = "feature-not-implemented";
31
				stream:event("authentication-failure", { condition = cond });
32
				return false;
33
			end
34
		end
35
               else -- COMPAT for servers not following XEP 78
36
                       for field, value in pairs(auth_data) do
37
                               if value then
38
                                       request:tag(field):text(value):up();
39
                               end
40
                       end
41
               end
42
		stream:send_iq(request, function (response)
43
			if response.attr.type == "result" then
44
				stream.resource = auth_data.resource;
45
				stream.jid = auth_data.username.."@"..stream.host.."/"..auth_data.resource;
46
				stream:event("authentication-success");
47
				stream:event("bind-success", stream.jid);
48
			else
49
				local type, cond, text = response:get_error();
50
				stream:event("authentication-failure", { condition = cond });
51
			end
52
		end);
53
	end
54
 
55
	local function handle_opened(attr)
56
		if not attr.version then
57
			stream:send_iq(verse.iq({type="get"})
58
				:tag("query", { xmlns = "jabber:iq:auth" })
59
					:tag("username"):text(stream.username),
60
				handle_auth_form);
61
		end
62
	end
63
	stream:hook("opened", handle_opened);
64
end