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