plugins/blocking.lua

1
local verse = require "verse";
2
 
3
local xmlns_blocking = "urn:xmpp:blocking";
4
 
5
function verse.plugins.blocking(stream)
6
	-- FIXME: Disco
7
	stream.blocking = {};
8
	function stream.blocking:block_jid(jid, callback)
9
		stream:send_iq(verse.iq{type="set"}
10
			:tag("block", { xmlns = xmlns_blocking })
11
				:tag("item", { jid = jid })
12
			, function () return callback and callback(true); end
13
			, function () return callback and callback(false); end
14
		);
15
	end
16
	function stream.blocking:unblock_jid(jid, callback)
17
		stream:send_iq(verse.iq{type="set"}
18
			:tag("unblock", { xmlns = xmlns_blocking })
19
				:tag("item", { jid = jid })
20
			, function () return callback and callback(true); end
21
			, function () return callback and callback(false); end
22
		);
23
	end
24
	function stream.blocking:unblock_all_jids(callback)
25
		stream:send_iq(verse.iq{type="set"}
26
			:tag("unblock", { xmlns = xmlns_blocking })
27
			, function () return callback and callback(true); end
28
			, function () return callback and callback(false); end
29
		);
30
	end
31
	function stream.blocking:get_blocked_jids(callback)
32
		stream:send_iq(verse.iq{type="get"}
33
			:tag("blocklist", { xmlns = xmlns_blocking })
34
			, function (result)
35
				local list = result:get_child("blocklist", xmlns_blocking);
36
				if not list then return callback and callback(false); end
37
				local jids = {};
38
				for item in list:childtags() do
39
					jids[#jids+1] = item.attr.jid;
40
				end
41
				return callback and callback(jids);
42
			  end
43
			, function (result) return callback and callback(false); end
44
		);
45
	end
46
end