Annotate

plugins/mod_pubsub.lua @ 3619:291ae816d800

mod_pubsub: It's aliiiive!
author Matthew Wild <mwild1@gmail.com>
date Sat, 13 Nov 2010 03:16:58 +0000
child 3621:7e1a1dd42088
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local pubsub = require "util.pubsub";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "util.stanza";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local jid_bare = require "util.jid".bare;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local uuid_generate = require "util.uuid".generate;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 require "core.modulemanager".load(module.host, "iq");
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local service;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local handlers = {};
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function handle_pubsub_iq(event)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local origin, stanza = event.origin, event.stanza;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local pubsub = stanza.tags[1];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local action = pubsub.tags[1];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local handler = handlers[stanza.attr.type.."_"..action.name];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if handler then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 handler(origin, stanza, action);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return true;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local pubsub_errors = {
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 ["item-not-found"] = { "cancel", "item-not-found" };
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 };
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function pubsub_error_reply(stanza, error)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local e = pubsub_errors[error];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local reply = st.error_reply(stanza, unpack(e, 1, 3));
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if e[4] then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 return reply;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 function handlers.set_subscribe(origin, stanza, subscribe)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local node, jid = subscribe.attr.node, subscribe.attr.jid;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local ok, ret = service:add_subscription(node, stanza.attr.from, jid);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local reply;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if ok then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 reply = st.reply(stanza)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 :tag("pubsub", { xmlns = xmlns_pubsub })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 :tag("subscription", {
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 node = node,
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 jid = jid,
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 subscription = "subscribed"
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 });
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 else
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 reply = pubsub_error_reply(stanza, ret);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return origin.send(reply);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 function handlers.set_publish(origin, stanza, publish)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local node = publish.attr.node;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local item = publish:get_child("item");
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local id = (item and item.attr.id) or uuid_generate();
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local ok, ret = service:publish(node, stanza.attr.from, id, item);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local reply;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if ok then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 reply = st.reply(stanza)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 :tag("pubsub", { xmlns = xmlns_pubsub })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 :tag("publish", { node = node })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 :tag("item", { id = id });
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 else
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 reply = pubsub_error_reply(stanza, ret);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 return origin.send(reply);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 function simple_broadcast(node, jids, item)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local message = st.message({ from = module.host, type = "headline" })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 :tag("event", { xmlns = xmlns_pubsub_event })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 :tag("items", { node = node })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 :add_child(item);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 for jid in pairs(jids) do
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 module:log("debug", "Sending notification to %s", jid);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 message.attr.to = jid;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 core_post_stanza(hosts[module.host], message);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 service = pubsub.new({
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 broadcaster = simple_broadcast
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 });
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);