Comparison

plugins/pep.lua @ 114:37f5966cff15

verse.plugins.pep: New plugin to add an API for sending and catching PEP events
author Matthew Wild <mwild1@gmail.com>
date Wed, 25 Aug 2010 16:27:30 +0100
child 164:d862093d9f91
comparison
equal deleted inserted replaced
113:769366a8b238 114:37f5966cff15
1
2 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
3 local xmlns_pubsub_event = xmlns_pubsub.."#event";
4
5 function verse.plugins.pep(stream)
6 stream.pep = {};
7
8 stream:hook("message", function (message)
9 local event = message:get_child("event", xmlns_pubsub_event);
10 if not event then return; end
11 local items = event:get_child("items");
12 if not items then return; end
13 local node = items.attr.node;
14 for item in items:childtags() do
15 if item.name == "item" and item.attr.xmlns == xmlns_pubsub_event then
16 stream:event("pep/"..node, {
17 from = message.attr.from,
18 item = item.tags[1],
19 });
20 end
21 end
22 end);
23
24 function stream:hook_pep(node, callback, priority)
25 stream:hook("pep/"..node, callback, priority);
26 stream:add_disco_feature(node.."+notify");
27 end
28
29 function stream:unhook_pep(node, callback)
30 stream:unhook("pep/"..node, callback);
31 local handlers = stream.events._handlers["pep/"..node];
32 if not(handlers) or #handlers == 0 then
33 stream:remove_disco_feature(node.."+notify");
34 end
35 end
36
37 function stream:publish_pep(item, node)
38 local publish = verse.iq({ type = "set" })
39 :tag("pubsub", { xmlns = xmlns_pubsub })
40 :tag("publish", { node = node or item.attr.xmlns })
41 :tag("item")
42 :add_child(item);
43 return stream:send_iq(publish);
44 end
45 end