Annotate

plugins/pubsub.lua @ 154:a4dc96890729

plugins.pubsub, squishy: New pubsub plugin (basic)
author Matthew Wild <mwild1@gmail.com>
date Tue, 16 Nov 2010 11:49:42 +0000
child 159:88cc513e81c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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);
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 function pubsub:subscribe(server, node, jid, callback)
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 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
16 :tag("pubsub", { xmlns = xmlns_pubsub })
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 :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
18 , function (result)
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if callback then
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 if result.attr.type == "result" then
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 callback(true);
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 else
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 callback(false, result:get_error());
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 );
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:publish(server, node, id, item, 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("publish", { node = node })
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 :tag("item", { id = id })
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 :add_child(item)
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 , function (result)
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if callback then
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if result.attr.type == "result" then
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 callback(true);
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 else
a4dc96890729 plugins.pubsub, squishy: New pubsub plugin (basic)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 callback(false, result:get_error());
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 end
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 end