plugins/session.lua

1
local verse = require "verse";
2
 
3
local xmlns_session = "urn:ietf:params:xml:ns:xmpp-session";
4
 
5
function verse.plugins.session(stream)
6
 
7
	local function handle_features(features)
8
		local session_feature = features:get_child("session", xmlns_session);
9
		if session_feature and not session_feature:get_child("optional") then
10
			local function handle_binding(jid)
11
				stream:debug("Establishing Session...");
12
				stream:send_iq(verse.iq({ type = "set" }):tag("session", {xmlns=xmlns_session}),
13
					function (reply)
14
						if reply.attr.type == "result" then
15
							stream:event("session-success");
16
						elseif reply.attr.type == "error" then
17
							local type, condition, text = reply:get_error();
18
							stream:event("session-failure", { error = condition, text = text, type = type });
19
						end
20
					end);
21
				return true;
22
			end
23
			stream:hook("bind-success", handle_binding);
24
		end
25
	end
26
	stream:hook("stream-features", handle_features);
27
 
28
	return true;
29
end