Software /
code /
verse
Annotate
plugins/pubsub.lua @ 159:88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 03 Dec 2010 04:11:18 +0000 |
parent | 154:a4dc96890729 |
child | 165:8c67ea868c06 |
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) |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
13 for pubsub_event in message:matching_tags("event", xmlns_pubsub_event) do |
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; |
88cc513e81c8
plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents:
154
diff
changeset
|
17 for item in items:matching_tags("item") do |
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); |
154
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 function pubsub:subscribe(server, node, jid, callback) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 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
|
31 :tag("pubsub", { xmlns = xmlns_pubsub }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 :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
|
33 , function (result) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 if callback then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 if result.attr.type == "result" then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 callback(true); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 else |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 callback(false, result:get_error()); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 ); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 function pubsub:publish(server, node, id, item, callback) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 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
|
47 :tag("pubsub", { xmlns = xmlns_pubsub }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 :tag("publish", { node = node }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 :tag("item", { id = id }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 :add_child(item) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 , function (result) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if callback then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if result.attr.type == "result" then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 callback(true); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 else |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 callback(false, result:get_error()); |
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 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 ); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |