Software /
code /
verse
Annotate
plugins/pubsub.lua @ 221:efb4f60ba36e
plugins.pubsub: implement node creation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 11 Oct 2011 13:26:11 +0200 |
parent | 193:fa6e1e65cb3c |
child | 222:3c257afd68e7 |
rev | line source |
---|---|
154
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local jid_bare = require "util.jid".bare; |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event"; |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors"; |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local pubsub = {}; |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local pubsub_mt = { __index = pubsub }; |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 function verse.plugins.pubsub(stream) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 stream.pubsub = setmetatable({ stream = stream }, pubsub_mt); |
159
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
12 stream:hook("message", function (message) |
193
fa6e1e65cb3c
plugins.pubsub: Fix to use :childtags() for iterating through items in a pubsub notification (:matching_tags() was removed from Prosody)
Matthew Wild <mwild1@gmail.com>
parents:
165
diff
changeset
|
13 for pubsub_event in message:childtags("event", xmlns_pubsub_event) do |
159
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
14 local items = pubsub_event:get_child("items"); |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
15 if items then |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
16 local node = items.attr.node; |
193
fa6e1e65cb3c
plugins.pubsub: Fix to use :childtags() for iterating through items in a pubsub notification (:matching_tags() was removed from Prosody)
Matthew Wild <mwild1@gmail.com>
parents:
165
diff
changeset
|
17 for item in items:childtags("item") do |
159
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
18 stream:event("pubsub/event", { |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
19 from = message.attr.from; |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
20 node = node; |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
21 item = item; |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
22 }); |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
23 end |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
24 end |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
25 end |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
26 end); |
165
8c67ea868c06
plugins.pubsub: Return true to indicate success loading
Matthew Wild <mwild1@gmail.com>
parents:
159
diff
changeset
|
27 return true; |
154
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
221
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
30 function pubsub:create(server, node, callback) |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
31 local create = verse.iq({ to = server, type = "set" }) |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
32 :tag("pubsub", { xmlns = xmlns_pubsub }) |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
33 :tag("create", { node = node }):up() |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
34 self.stream:send_iq(create, function (result) |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
35 if callback then |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
36 if result.attr.type == "result" then |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
37 callback(true); |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
38 else |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
39 callback(false, result:get_error()); |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
40 end |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
41 end |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
42 end |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
43 ); |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
44 end |
efb4f60ba36e
plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents:
193
diff
changeset
|
45 |
154
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 function pubsub:subscribe(server, node, jid, callback) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 self.stream:send_iq(verse.iq({ to = server, type = "set" }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 :tag("pubsub", { xmlns = xmlns_pubsub }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 :tag("subscribe", { node = node, jid = jid or jid_bare(self.stream.jid) }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 , function (result) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if callback then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if result.attr.type == "result" then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 callback(true); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 else |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 callback(false, result:get_error()); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 ); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 function pubsub:publish(server, node, id, item, callback) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 self.stream:send_iq(verse.iq({ to = server, type = "set" }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 :tag("pubsub", { xmlns = xmlns_pubsub }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 :tag("publish", { node = node }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 :tag("item", { id = id }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 :add_child(item) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 , function (result) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 if callback then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 if result.attr.type == "result" then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 callback(true); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 else |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 callback(false, result:get_error()); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 ); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end |