f92d27aaf63627f00c628e32a1d4463d9522f47e
[prosody.git] / plugins / mod_pubsub.lua
1 local pubsub = require "util.pubsub";
2 local st = require "util.stanza";
3 local jid_bare = require "util.jid".bare;
4 local uuid_generate = require "util.uuid".generate;
5
6 require "core.modulemanager".load(module.host, "iq");
7
8 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
9 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
11
12 local service;
13
14 local handlers = {};
15
16 function handle_pubsub_iq(event)
17         local origin, stanza = event.origin, event.stanza;
18         local pubsub = stanza.tags[1];
19         local action = pubsub.tags[1];
20         local handler = handlers[stanza.attr.type.."_"..action.name];
21         if handler then
22                 handler(origin, stanza, action);
23                 return true;
24         end
25 end
26
27 local pubsub_errors = {
28         ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
29         ["item-not-found"] = { "cancel", "item-not-found" };
30 };
31 function pubsub_error_reply(stanza, error)
32         local e = pubsub_errors[error];
33         local reply = st.error_reply(stanza, unpack(e, 1, 3));
34         if e[4] then
35                 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
36         end
37         return reply;
38 end
39
40 function handlers.get_items(origin, stanza, items)
41         local node = items.attr.node;
42         local item = items:get_child("item");
43         local id = item and item.attr.id;
44         local data = st.stanza("items", { node = node });
45         for _, entry in pairs(service:get(node, stanza.attr.from, id)) do
46                 data:add_child(entry);
47         end
48         if data then
49                 reply = st.reply(stanza)
50                         :tag("pubsub", { xmlns = xmlns_pubsub })
51                                 :add_child(data);
52         else
53                 reply = st.error_reply(stanza, "cancel", "item-not-found", "Item could not be found in this node");
54         end
55         return origin.send(reply);
56 end
57
58 function handlers.set_create(origin, stanza, create)
59         local node = create.attr.node;
60         local ok, ret, reply;
61         if node then
62                 ok, ret = service:create(node, stanza.attr.from);
63                 if ok then
64                         reply = st.reply(stanza);
65                 else
66                         reply = st.error_reply(stanza, "cancel", ret);
67                 end
68         else
69                 repeat
70                         node = uuid_generate();
71                         ok, ret = service:create(node, stanza.attr.from);
72                 until ok;
73                 reply = st.reply(stanza)
74                         :tag("pubsub", { xmlns = xmlns_pubsub })
75                                 :tag("create", { node = node });
76         end
77         origin.send(reply);
78 end
79
80 function handlers.set_subscribe(origin, stanza, subscribe)
81         local node, jid = subscribe.attr.node, subscribe.attr.jid;
82         if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
83                 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
84         end
85         local ok, ret = service:add_subscription(node, stanza.attr.from, jid);
86         local reply;
87         if ok then
88                 reply = st.reply(stanza)
89                         :tag("pubsub", { xmlns = xmlns_pubsub })
90                                 :tag("subscription", {
91                                         node = node,
92                                         jid = jid,
93                                         subscription = "subscribed"
94                                 });
95         else
96                 reply = pubsub_error_reply(stanza, ret);
97         end
98         return origin.send(reply);
99 end
100
101 function handlers.set_publish(origin, stanza, publish)
102         local node = publish.attr.node;
103         local item = publish:get_child("item");
104         local id = (item and item.attr.id) or uuid_generate();
105         local ok, ret = service:publish(node, stanza.attr.from, id, item);
106         local reply;
107         if ok then
108                 reply = st.reply(stanza)
109                         :tag("pubsub", { xmlns = xmlns_pubsub })
110                                 :tag("publish", { node = node })
111                                         :tag("item", { id = id });
112         else
113                 reply = pubsub_error_reply(stanza, ret);
114         end
115         return origin.send(reply);
116 end
117
118 function simple_broadcast(node, jids, item)
119         local message = st.message({ from = module.host, type = "headline" })
120                 :tag("event", { xmlns = xmlns_pubsub_event })
121                         :tag("items", { node = node })
122                                 :add_child(item);
123         for jid in pairs(jids) do
124                 module:log("debug", "Sending notification to %s", jid);
125                 message.attr.to = jid;
126                 core_post_stanza(hosts[module.host], message);
127         end
128 end
129
130 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
131
132 service = pubsub.new({
133         broadcaster = simple_broadcast
134 });
135 module.environment.service = service;
136