plugins/pep.lua

1
local verse = require "verse";
2
 
3
local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
4
local xmlns_pubsub_event = xmlns_pubsub.."#event";
5
 
6
function verse.plugins.pep(stream)
7
	stream:add_plugin("disco");
8
	stream:add_plugin("pubsub");
9
	stream.pep = {};
10
 
11
	stream:hook("pubsub/event", function(event)
12
		return stream:event("pep/"..event.node, { from = event.from, id = event.item.attr.id, item = event.item.tags[1] } );
13
	end);
14
 
15
	function stream:hook_pep(node, callback, priority)
16
		local handlers = stream.events._handlers["pep/"..node];
17
		if not(handlers) or #handlers == 0 then
18
			stream:add_disco_feature(node.."+notify");
19
		end
20
		stream:hook("pep/"..node, callback, priority);
21
	end
22
 
23
	function stream:unhook_pep(node, callback)
24
		stream:unhook("pep/"..node, callback);
25
		local handlers = stream.events._handlers["pep/"..node];
26
		if not(handlers) or #handlers == 0 then
27
			stream:remove_disco_feature(node.."+notify");
28
		end
29
	end
30
 
31
	function stream:publish_pep(item, node, id)
32
		return stream.pubsub:service(nil):node(node or item.attr.xmlns):publish(id or "current", nil, item)
33
	end
34
end