mod_offline: Make sure mod_offline's store handler reports success vs failure.
[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 autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false);
13 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false);
14
15 local service;
16
17 local handlers = {};
18
19 function handle_pubsub_iq(event)
20         local origin, stanza = event.origin, event.stanza;
21         local pubsub = stanza.tags[1];
22         local action = pubsub.tags[1];
23         local handler = handlers[stanza.attr.type.."_"..action.name];
24         if handler then
25                 handler(origin, stanza, action);
26                 return true;
27         end
28 end
29
30 local pubsub_errors = {
31         ["conflict"] = { "cancel", "conflict" };
32         ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
33         ["item-not-found"] = { "cancel", "item-not-found" };
34         ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
35         ["forbidden"] = { "cancel", "forbidden" };
36 };
37 function pubsub_error_reply(stanza, error)
38         local e = pubsub_errors[error];
39         local reply = st.error_reply(stanza, unpack(e, 1, 3));
40         if e[4] then
41                 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
42         end
43         return reply;
44 end
45
46 function handlers.get_items(origin, stanza, items)
47         local node = items.attr.node;
48         local item = items:get_child("item");
49         local id = item and item.attr.id;
50         
51         local ok, results = service:get_items(node, stanza.attr.from, id);
52         if not ok then
53                 return origin.send(pubsub_error_reply(stanza, results));
54         end
55         
56         local data = st.stanza("items", { node = node });
57         for _, entry in pairs(results) do
58                 data:add_child(entry);
59         end
60         if data then
61                 reply = st.reply(stanza)
62                         :tag("pubsub", { xmlns = xmlns_pubsub })
63                                 :add_child(data);
64         else
65                 reply = pubsub_error_reply(stanza, "item-not-found");
66         end
67         return origin.send(reply);
68 end
69
70 function handlers.get_subscriptions(origin, stanza, subscriptions)
71         local node = subscriptions.attr.node;
72         local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from);
73         if not ok then
74                 return origin.send(pubsub_error_reply(stanza, ret));
75         end
76         local reply = st.reply(stanza)
77                 :tag("subscriptions", { xmlns = xmlns_pubsub });
78         for _, sub in ipairs(ret) do
79                 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
80         end
81         return origin.send(reply);
82 end
83
84 function handlers.set_create(origin, stanza, create)
85         local node = create.attr.node;
86         local ok, ret, reply;
87         if node then
88                 ok, ret = service:create(node, stanza.attr.from);
89                 if ok then
90                         reply = st.reply(stanza);
91                 else
92                         reply = pubsub_error_reply(stanza, ret);
93                 end
94         else
95                 repeat
96                         node = uuid_generate();
97                         ok, ret = service:create(node, stanza.attr.from);
98                 until ok or ret ~= "conflict";
99                 if ok then
100                         reply = st.reply(stanza)
101                                 :tag("pubsub", { xmlns = xmlns_pubsub })
102                                         :tag("create", { node = node });
103                 else
104                         reply = pubsub_error_reply(stanza, ret);
105                 end
106         end
107         return origin.send(reply);
108 end
109
110 function handlers.set_subscribe(origin, stanza, subscribe)
111         local node, jid = subscribe.attr.node, subscribe.attr.jid;
112         if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
113                 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
114         end
115         local ok, ret = service:add_subscription(node, stanza.attr.from, jid);
116         local reply;
117         if ok then
118                 reply = st.reply(stanza)
119                         :tag("pubsub", { xmlns = xmlns_pubsub })
120                                 :tag("subscription", {
121                                         node = node,
122                                         jid = jid,
123                                         subscription = "subscribed"
124                                 });
125         else
126                 reply = pubsub_error_reply(stanza, ret);
127         end
128         return origin.send(reply);
129 end
130
131 function handlers.set_unsubscribe(origin, stanza, unsubscribe)
132         local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
133         if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
134                 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
135         end
136         local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
137         local reply;
138         if ok then
139                 reply = st.reply(stanza);
140         else
141                 reply = pubsub_error_reply(stanza, ret);
142         end
143         return origin.send(reply);
144 end
145
146 function handlers.set_publish(origin, stanza, publish)
147         local node = publish.attr.node;
148         local item = publish:get_child("item");
149         local id = (item and item.attr.id) or uuid_generate();
150         local ok, ret = service:publish(node, stanza.attr.from, id, item);
151         local reply;
152         if ok then
153                 reply = st.reply(stanza)
154                         :tag("pubsub", { xmlns = xmlns_pubsub })
155                                 :tag("publish", { node = node })
156                                         :tag("item", { id = id });
157         else
158                 reply = pubsub_error_reply(stanza, ret);
159         end
160         return origin.send(reply);
161 end
162
163 function handlers.set_retract(origin, stanza, retract)
164         local node, notify = retract.attr.node, retract.attr.notify;
165         notify = (notify == "1") or (notify == "true");
166         local item = retract:get_child("item");
167         local id = item and item.attr.id
168         local reply, notifier;
169         if notify then
170                 notifier = st.stanza("retract", { id = id });
171         end
172         local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
173         if ok then
174                 reply = st.reply(stanza);
175         else
176                 reply = pubsub_error_reply(stanza, ret);
177         end
178         return origin.send(reply);
179 end
180
181 function simple_broadcast(node, jids, item)
182         item = st.clone(item);
183         item.attr.xmlns = nil; -- Clear the pubsub namespace
184         local message = st.message({ from = module.host, type = "headline" })
185                 :tag("event", { xmlns = xmlns_pubsub_event })
186                         :tag("items", { node = node })
187                                 :add_child(item);
188         for jid in pairs(jids) do
189                 module:log("debug", "Sending notification to %s", jid);
190                 message.attr.to = jid;
191                 core_post_stanza(hosts[module.host], message);
192         end
193 end
194
195 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
196
197 local disco_info;
198
199 local feature_map = {
200         create = { "create-nodes", autocreate_on_publish and "instant-nodes", "item-ids" };
201         retract = { "delete-items", "retract-items" };
202         publish = { "publish" };
203         get_items = { "retrieve-items" };
204         add_subscription = { "subscribe" };
205         get_subscriptions = { "retrieve-subscriptions" };
206 };
207
208 local function add_disco_features_from_service(disco, service)
209         for method, features in pairs(feature_map) do
210                 if service[method] then
211                         for _, feature in ipairs(features) do
212                                 if feature then
213                                         disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
214                                 end
215                         end
216                 end
217         end
218         for affiliation in pairs(service.config.capabilities) do
219                 if affiliation ~= "none" and affiliation ~= "owner" then
220                         disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
221                 end
222         end
223 end
224
225 local function build_disco_info(service)
226         local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
227                 :tag("identity", { category = "pubsub", type = "service" }):up()
228                 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
229         add_disco_features_from_service(disco_info, service);
230         return disco_info;
231 end
232
233 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
234         local origin, stanza = event.origin, event.stanza;
235         local node = stanza.tags[1].attr.node;
236         if not node then
237                 return origin.send(st.reply(stanza):add_child(disco_info));
238         else
239                 local ok, ret = service:get_nodes(stanza.attr.from);
240                 if ok and not ret[node] then
241                         ok, ret = false, "item-not-found";
242                 end
243                 if not ok then
244                         return origin.send(pubsub_error_reply(stanza, ret));
245                 end
246                 local reply = st.reply(stanza)
247                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
248                                 :tag("identity", { category = "pubsub", type = "leaf" });
249                 return origin.send(reply);
250         end
251 end);
252
253 local function handle_disco_items_on_node(event)
254         local stanza, origin = event.stanza, event.origin;
255         local query = stanza.tags[1];
256         local node = query.attr.node;
257         local ok, ret = service:get_items(node, stanza.attr.from);
258         if not ok then
259                 return origin.send(pubsub_error_reply(stanza, ret));
260         end
261         
262         local reply = st.reply(stanza)
263                 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
264         
265         for id, item in pairs(ret) do
266                 reply:tag("item", { jid = module.host, name = id }):up();
267         end
268         
269         return origin.send(reply);
270 end
271
272
273 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
274         if event.stanza.tags[1].attr.node then
275                 return handle_disco_items_on_node(event);
276         end
277         local ok, ret = service:get_nodes(event.stanza.attr.from);
278         if not ok then
279                 event.origin.send(pubsub_error_reply(stanza, ret));
280         else
281                 local reply = st.reply(event.stanza)
282                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
283                 for node, node_obj in pairs(ret) do
284                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
285                 end
286                 event.origin.send(reply);
287         end
288         return true;
289 end);
290
291 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
292 local function get_affiliation(jid)
293         local bare_jid = jid_bare(jid);
294         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
295                 return admin_aff;
296         end
297 end
298
299 function set_service(new_service)
300         service = new_service;
301         module.environment.service = service;
302         disco_info = build_disco_info(service);
303 end
304
305 function module.save()
306         return { service = service };
307 end
308
309 function module.restore(data)
310         set_service(data.service);
311 end
312
313 set_service(pubsub.new({
314         capabilities = {
315                 none = {
316                         create = false;
317                         publish = false;
318                         retract = false;
319                         get_nodes = true;
320                         
321                         subscribe = true;
322                         unsubscribe = true;
323                         get_subscription = true;
324                         get_subscriptions = true;
325                         get_items = true;
326                         
327                         subscribe_other = false;
328                         unsubscribe_other = false;
329                         get_subscription_other = false;
330                         get_subscriptions_other = false;
331                         
332                         be_subscribed = true;
333                         be_unsubscribed = true;
334                         
335                         set_affiliation = false;
336                 };
337                 owner = {
338                         create = true;
339                         publish = true;
340                         retract = true;
341                         get_nodes = true;
342                         
343                         subscribe = true;
344                         unsubscribe = true;
345                         get_subscription = true;
346                         get_subscriptions = true;
347                         get_items = true;
348                         
349                         
350                         subscribe_other = true;
351                         unsubscribe_other = true;
352                         get_subscription_other = true;
353                         get_subscriptions_other = true;
354                         
355                         be_subscribed = true;
356                         be_unsubscribed = true;
357                         
358                         set_affiliation = true;
359                 };
360         };
361         
362         autocreate_on_publish = autocreate_on_publish;
363         autocreate_on_subscribe = autocreate_on_subscribe;
364         
365         broadcaster = simple_broadcast;
366         get_affiliation = get_affiliation;
367         
368         normalize_jid = jid_bare;
369 }));