plugins/pubsub.lua

Mon, 28 Nov 2011 17:15:41 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 28 Nov 2011 17:15:41 +0100
changeset 248
c4b55b0dc6ba
parent 222
3c257afd68e7
child 250
a5ac643a7fd6
permissions
-rw-r--r--

plugins.pubsub: Make the old functions wrap the new.

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;
222
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
2 local t_insert = table.insert;
154
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
222
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
5 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
154
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 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
7 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
8
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local pubsub = {};
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local pubsub_mt = { __index = pubsub };
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function verse.plugins.pubsub(stream)
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 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
15 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
16 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
17 if items then
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
18 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
19 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
20 stream:event("pubsub/event", {
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
21 from = message.attr.from;
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
22 node = node;
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
23 item = item;
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
24 });
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
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
27 end
88cc513e81c8 plugins.pubsub: Fire pubsub/event on incoming pubsub notifications
Matthew Wild <mwild1@gmail.com>
parents: 154
diff changeset
28 end);
165
8c67ea868c06 plugins.pubsub: Return true to indicate success loading
Matthew Wild <mwild1@gmail.com>
parents: 159
diff changeset
29 return true;
154
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
248
c4b55b0dc6ba plugins.pubsub: Make the old functions wrap the new.
Kim Alvefur <zash@zash.se>
parents: 222
diff changeset
32 -- COMPAT
221
efb4f60ba36e plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents: 193
diff changeset
33 function pubsub:create(server, node, callback)
248
c4b55b0dc6ba plugins.pubsub: Make the old functions wrap the new.
Kim Alvefur <zash@zash.se>
parents: 222
diff changeset
34 return self:server(server):node(node):create(nil, callback);
221
efb4f60ba36e plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents: 193
diff changeset
35 end
efb4f60ba36e plugins.pubsub: implement node creation
Kim Alvefur <zash@zash.se>
parents: 193
diff changeset
36
154
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 function pubsub:subscribe(server, node, jid, callback)
248
c4b55b0dc6ba plugins.pubsub: Make the old functions wrap the new.
Kim Alvefur <zash@zash.se>
parents: 222
diff changeset
38 return self:server(server):node(node):subscribe(jid, nil, callback);
154
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
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 function pubsub:publish(server, node, id, item, callback)
248
c4b55b0dc6ba plugins.pubsub: Make the old functions wrap the new.
Kim Alvefur <zash@zash.se>
parents: 222
diff changeset
42 return self:server(server):node(node):publish(id, nil, item, callback);
154
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
222
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
44
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
45 --------------------------------------------------------------------------
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
46 ---------------------New and improved PubSub interface--------------------
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
47 --------------------------------------------------------------------------
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
48
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
49 local pubsub_service = {};
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
50 local pubsub_service_mt = { __index = pubsub_service };
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
51
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
52 -- TODO should the property be named 'jid' instead?
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
53 function pubsub:service(service)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
54 return setmetatable({ stream = self.stream, service = service }, pubsub_service_mt)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
55 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
56
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
57 -- Helper function for iq+pubsub tags
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
58
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
59 local function pubsub_iq(iq_type, to, ns, op, node, jid, item_id)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
60 local st = verse.iq{ type = iq_type or "get", to = to }
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
61 :tag("pubsub", { xmlns = ns or xmlns_pubsub }) -- ns would be ..#owner
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
62 if op then st:tag(op, { node = node, jid = jid }); end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
63 if id then st:tag("item", { id = item_id ~= true and item_id or nil }); end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
64 return st;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
65 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
66
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
67 -- http://xmpp.org/extensions/xep-0060.html#entity-subscriptions
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
68 function pubsub_service:subscriptions(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
69 self.stream:send_iq(pubsub_iq(nil, self.service, nil, "subscriptions")
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
70 , callback and function (result)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
71 if result.attr.type == "result" then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
72 local ps = result:get_child("pubsub", xmlns_pubsub);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
73 local subs = ps and ps:get_child("subscriptions");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
74 local nodes = {};
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
75 if subs then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
76 for sub in subs:childtags("subscription") do
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
77 local node = self:node(sub.attr.node)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
78 node.subscription = sub;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
79 t_insert(nodes, node);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
80 -- FIXME Good enough?
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
81 -- Or how about:
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
82 -- nodes[node] = sub;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
83 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
84 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
85 callback(nodes);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
86 else
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
87 callback(false, result:get_error());
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
88 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
89 end or nil);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
90 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
91
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
92 -- http://xmpp.org/extensions/xep-0060.html#entity-affiliations
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
93 function pubsub_service:affiliations(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
94 self.stream:send_iq(pubsub_iq(nil, self.service, nil, "affiliations")
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
95 , callback and function (result)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
96 if result.attr.type == "result" then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
97 local ps = result:get_child("pubsub", xmlns_pubsub);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
98 local affils = ps and ps:get_child("affiliations") or {};
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
99 local nodes = {};
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
100 if affils then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
101 for affil in affils:childtags("affiliation") do
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
102 local node = self:node(affil.attr.node)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
103 node.affiliation = affil;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
104 t_insert(nodes, node);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
105 -- nodes[node] = affil;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
106 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
107 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
108 callback(nodes);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
109 else
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
110 callback(false, result:get_error());
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
111 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
112 end or nil);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
113 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
114
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
115 -- TODO Listing nodes? It's done with standard disco#items, but should
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
116 -- we have a wrapper here? If so, it could wrap items in pubsub_node objects
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
117
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
118 --[[
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
119 function pubsub_service:nodes(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
120 self.stream:disco_items(...)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
121 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
122 --]]
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
123
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
124 local pubsub_node = {};
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
125 local pubsub_node_mt = { __index = pubsub_node };
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
126
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
127 function pubsub_service:node(node)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
128 return setmetatable({ stream = self.stream, service = self.service, node = node }, pubsub_node_mt)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
129 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
130
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
131 function pubsub_mt:__call(service, node)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
132 local s = self:service(service);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
133 return node and s:node(node) or s;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
134 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
135
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
136 function pubsub_node:hook(callback, prio)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
137 local function hook(event)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
138 -- FIXME service == nil would mean anyone,
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
139 -- publishing would be go to your bare jid.
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
140 -- So if you're only interestied in your own
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
141 -- events, hook your own bare jid.
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
142 if (not event.service or event.from == self.service) and event.node == self.node then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
143 return callback(event)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
144 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
145 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
146 self.stream:hook("pubsub/event", hook, prio);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
147 return hook;
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
148 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
149
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
150 function pubsub_node:unhook(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
151 self.stream:unhook("pubsub/event", callback);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
152 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
153
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
154 function pubsub_node:create(config, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
155 if config ~= nil then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
156 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
157 else
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
158 self.stream:send_iq(pubsub_iq("set", self.service, nil, "create", self.node), callback);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
159 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
160 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
161
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
162 -- <configure/> and <default/> rolled into one
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
163 function pubsub_node:configure(config, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
164 if config ~= nil then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
165 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
166 -- if config == true then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
167 -- fetch form and pass it to the callback
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
168 -- which would process it and pass it back
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
169 -- and then we submit it
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
170 -- elseif type(config) == "table" then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
171 -- it's a form or stanza that we submit
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
172 -- end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
173 -- this would be done for everything that needs a config
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
174 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
175 self.stream:send_iq(pubsub_iq("set", self.service, nil, config == nil and "default" or "configure", self.node), callback);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
176 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
177
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
178 function pubsub_node:publish(id, options, node, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
179 if options ~= nil then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
180 error("Node configuration is not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
181 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
182 self.stream:send_iq(pubsub_iq("set", self.service, nil, "publish", self.node, nil, id)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
183 :add_child(node)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
184 , callback);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
185 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
186
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
187 function pubsub_node:subscribe(jid, options, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
188 if options ~= nil then
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
189 error("Subscription configuration is not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
190 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
191 self.stream:send_iq(pubsub_iq("set", self.service, nil, "subscribe", self.node, jid, id)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
192 , callback);
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
193 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
194
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
195 function pubsub_node:subscription(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
196 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
197 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
198
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
199 function pubsub_node:affiliation(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
200 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
201 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
202
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
203 function pubsub_node:unsubscribe(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
204 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
205 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
206
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
207 function pubsub_node:configure_subscription(options, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
208 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
209 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
210
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
211 function pubsub_node:items(count, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
212 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
213 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
214
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
215 function pubsub_node:item(id, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
216 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
217 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
218
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
219 function pubsub_node:retract(id, callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
220 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
221 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
222
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
223 function pubsub_node:purge(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
224 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
225 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
226
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
227 function pubsub_node:delete(callback)
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
228 error("Not implemented yet.");
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
229 end
3c257afd68e7 plugins.pubsub: New, OOP-ish, PubSub interface. Beware of stubs.
Kim Alvefur <zash@zash.se>
parents: 221
diff changeset
230

mercurial