Software /
code /
verse
Annotate
plugins/pubsub.lua @ 175:4d2a5d02fdfa
plugins.disco: Fix missing xmlns variable.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 31 Dec 2010 00:30:37 +0100 |
parent | 165:8c67ea868c06 |
child | 193:fa6e1e65cb3c |
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); |
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 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 function pubsub:subscribe(server, node, jid, callback) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 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
|
32 :tag("pubsub", { xmlns = xmlns_pubsub }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 :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
|
34 , function (result) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 if callback then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if result.attr.type == "result" then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 callback(true); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 else |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 callback(false, result:get_error()); |
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 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 ); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 function pubsub:publish(server, node, id, item, 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("publish", { node = node }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 :tag("item", { id = id }) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 :add_child(item) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 , function (result) |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if callback then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if result.attr.type == "result" then |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 callback(true); |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 else |
a4dc96890729
plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 callback(false, result:get_error()); |
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 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 end |