moduleapi: in module:provides(), add the name of the module in item._provided_by
[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 local usermanager = require "core.usermanager";
6
7 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
8 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
9 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
10 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
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 local pubsub_disco_name = module:get_option("name");
15 if type(pubsub_disco_name) ~= "string" then pubsub_disco_name = "Prosody PubSub Service"; end
16
17 local service;
18
19 local handlers = {};
20
21 function handle_pubsub_iq(event)
22         local origin, stanza = event.origin, event.stanza;
23         local pubsub = stanza.tags[1];
24         local action = pubsub.tags[1];
25         if not action then
26                 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
27         end
28         local handler = handlers[stanza.attr.type.."_"..action.name];
29         if handler then
30                 handler(origin, stanza, action);
31                 return true;
32         end
33 end
34
35 local pubsub_errors = {
36         ["conflict"] = { "cancel", "conflict" };
37         ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
38         ["jid-required"] = { "modify", "bad-request", nil, "jid-required" };
39         ["nodeid-required"] = { "modify", "bad-request", nil, "nodeid-required" };
40         ["item-not-found"] = { "cancel", "item-not-found" };
41         ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
42         ["forbidden"] = { "cancel", "forbidden" };
43 };
44 function pubsub_error_reply(stanza, error)
45         local e = pubsub_errors[error];
46         local reply = st.error_reply(stanza, unpack(e, 1, 3));
47         if e[4] then
48                 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
49         end
50         return reply;
51 end
52
53 function handlers.get_items(origin, stanza, items)
54         local node = items.attr.node;
55         local item = items:get_child("item");
56         local id = item and item.attr.id;
57         
58         if not node then
59                 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
60         end
61         local ok, results = service:get_items(node, stanza.attr.from, id);
62         if not ok then
63                 return origin.send(pubsub_error_reply(stanza, results));
64         end
65         
66         local data = st.stanza("items", { node = node });
67         for _, entry in pairs(results) do
68                 data:add_child(entry);
69         end
70         local reply;
71         if data then
72                 reply = st.reply(stanza)
73                         :tag("pubsub", { xmlns = xmlns_pubsub })
74                                 :add_child(data);
75         else
76                 reply = pubsub_error_reply(stanza, "item-not-found");
77         end
78         return origin.send(reply);
79 end
80
81 function handlers.get_subscriptions(origin, stanza, subscriptions)
82         local node = subscriptions.attr.node;
83         if not node then
84                 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
85         end
86         local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from);
87         if not ok then
88                 return origin.send(pubsub_error_reply(stanza, ret));
89         end
90         local reply = st.reply(stanza)
91                 :tag("pubsub", { xmlns = xmlns_pubsub })
92                         :tag("subscriptions");
93         for _, sub in ipairs(ret) do
94                 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
95         end
96         return origin.send(reply);
97 end
98
99 function handlers.set_create(origin, stanza, create)
100         local node = create.attr.node;
101         local ok, ret, reply;
102         if node then
103                 ok, ret = service:create(node, stanza.attr.from);
104                 if ok then
105                         reply = st.reply(stanza);
106                 else
107                         reply = pubsub_error_reply(stanza, ret);
108                 end
109         else
110                 repeat
111                         node = uuid_generate();
112                         ok, ret = service:create(node, stanza.attr.from);
113                 until ok or ret ~= "conflict";
114                 if ok then
115                         reply = st.reply(stanza)
116                                 :tag("pubsub", { xmlns = xmlns_pubsub })
117                                         :tag("create", { node = node });
118                 else
119                         reply = pubsub_error_reply(stanza, ret);
120                 end
121         end
122         return origin.send(reply);
123 end
124
125 function handlers.set_delete(origin, stanza, delete)
126         local node = delete.attr.node;
127
128         local reply, notifier;
129         if not node then
130                 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
131         end
132         local ok, ret = service:delete(node, stanza.attr.from);
133         if ok then
134                 reply = st.reply(stanza);
135         else
136                 reply = pubsub_error_reply(stanza, ret);
137         end
138         return origin.send(reply);
139 end
140
141 function handlers.set_subscribe(origin, stanza, subscribe)
142         local node, jid = subscribe.attr.node, subscribe.attr.jid;
143         if not (node and jid) then
144                 return origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
145         end
146         --[[
147         local options_tag, options = stanza.tags[1]:get_child("options"), nil;
148         if options_tag then
149                 options = options_form:data(options_tag.tags[1]);
150         end
151         --]]
152         local options_tag, options; -- FIXME
153         local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
154         local reply;
155         if ok then
156                 reply = st.reply(stanza)
157                         :tag("pubsub", { xmlns = xmlns_pubsub })
158                                 :tag("subscription", {
159                                         node = node,
160                                         jid = jid,
161                                         subscription = "subscribed"
162                                 }):up();
163                 if options_tag then
164                         reply:add_child(options_tag);
165                 end
166         else
167                 reply = pubsub_error_reply(stanza, ret);
168         end
169         origin.send(reply);
170 end
171
172 function handlers.set_unsubscribe(origin, stanza, unsubscribe)
173         local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
174         if not (node and jid) then
175                 return origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
176         end
177         local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
178         local reply;
179         if ok then
180                 reply = st.reply(stanza);
181         else
182                 reply = pubsub_error_reply(stanza, ret);
183         end
184         return origin.send(reply);
185 end
186
187 function handlers.set_publish(origin, stanza, publish)
188         local node = publish.attr.node;
189         if not node then
190                 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
191         end
192         local item = publish:get_child("item");
193         local id = (item and item.attr.id);
194         if not id then
195                 id = uuid_generate();
196                 if item then
197                         item.attr.id = id;
198                 end
199         end
200         local ok, ret = service:publish(node, stanza.attr.from, id, item);
201         local reply;
202         if ok then
203                 reply = st.reply(stanza)
204                         :tag("pubsub", { xmlns = xmlns_pubsub })
205                                 :tag("publish", { node = node })
206                                         :tag("item", { id = id });
207         else
208                 reply = pubsub_error_reply(stanza, ret);
209         end
210         return origin.send(reply);
211 end
212
213 function handlers.set_retract(origin, stanza, retract)
214         local node, notify = retract.attr.node, retract.attr.notify;
215         notify = (notify == "1") or (notify == "true");
216         local item = retract:get_child("item");
217         local id = item and item.attr.id
218         if not (node and id) then
219                 return origin.send(pubsub_error_reply(stanza, node and "item-not-found" or "nodeid-required"));
220         end
221         local reply, notifier;
222         if notify then
223                 notifier = st.stanza("retract", { id = id });
224         end
225         local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
226         if ok then
227                 reply = st.reply(stanza);
228         else
229                 reply = pubsub_error_reply(stanza, ret);
230         end
231         return origin.send(reply);
232 end
233
234 function handlers.set_purge(origin, stanza, purge)
235         local node, notify = purge.attr.node, purge.attr.notify;
236         notify = (notify == "1") or (notify == "true");
237         local reply;
238         if not node then
239                 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
240         end
241         local ok, ret = service:purge(node, stanza.attr.from, notify);
242         if ok then
243                 reply = st.reply(stanza);
244         else
245                 reply = pubsub_error_reply(stanza, ret);
246         end
247         return origin.send(reply);
248 end
249
250 function simple_broadcast(kind, node, jids, item)
251         if item then
252                 item = st.clone(item);
253                 item.attr.xmlns = nil; -- Clear the pubsub namespace
254         end
255         local message = st.message({ from = module.host, type = "headline" })
256                 :tag("event", { xmlns = xmlns_pubsub_event })
257                         :tag(kind, { node = node })
258                                 :add_child(item);
259         for jid in pairs(jids) do
260                 module:log("debug", "Sending notification to %s", jid);
261                 message.attr.to = jid;
262                 module:send(message);
263         end
264 end
265
266 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
267 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
268
269 local disco_info;
270
271 local feature_map = {
272         create = { "create-nodes", "instant-nodes", "item-ids" };
273         retract = { "delete-items", "retract-items" };
274         purge = { "purge-nodes" };
275         publish = { "publish", autocreate_on_publish and "auto-create" };
276         delete = { "delete-nodes" };
277         get_items = { "retrieve-items" };
278         add_subscription = { "subscribe" };
279         get_subscriptions = { "retrieve-subscriptions" };
280 };
281
282 local function add_disco_features_from_service(disco, service)
283         for method, features in pairs(feature_map) do
284                 if service[method] then
285                         for _, feature in ipairs(features) do
286                                 if feature then
287                                         disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
288                                 end
289                         end
290                 end
291         end
292         for affiliation in pairs(service.config.capabilities) do
293                 if affiliation ~= "none" and affiliation ~= "owner" then
294                         disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
295                 end
296         end
297 end
298
299 local function build_disco_info(service)
300         local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
301                 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up()
302                 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
303         add_disco_features_from_service(disco_info, service);
304         return disco_info;
305 end
306
307 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
308         local origin, stanza = event.origin, event.stanza;
309         local node = stanza.tags[1].attr.node;
310         if not node then
311                 return origin.send(st.reply(stanza):add_child(disco_info));
312         else
313                 local ok, ret = service:get_nodes(stanza.attr.from);
314                 if ok and not ret[node] then
315                         ok, ret = false, "item-not-found";
316                 end
317                 if not ok then
318                         return origin.send(pubsub_error_reply(stanza, ret));
319                 end
320                 local reply = st.reply(stanza)
321                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
322                                 :tag("identity", { category = "pubsub", type = "leaf" });
323                 return origin.send(reply);
324         end
325 end);
326
327 local function handle_disco_items_on_node(event)
328         local stanza, origin = event.stanza, event.origin;
329         local query = stanza.tags[1];
330         local node = query.attr.node;
331         local ok, ret = service:get_items(node, stanza.attr.from);
332         if not ok then
333                 return origin.send(pubsub_error_reply(stanza, ret));
334         end
335         
336         local reply = st.reply(stanza)
337                 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
338         
339         for id, item in pairs(ret) do
340                 reply:tag("item", { jid = module.host, name = id }):up();
341         end
342         
343         return origin.send(reply);
344 end
345
346
347 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
348         if event.stanza.tags[1].attr.node then
349                 return handle_disco_items_on_node(event);
350         end
351         local ok, ret = service:get_nodes(event.stanza.attr.from);
352         if not ok then
353                 event.origin.send(pubsub_error_reply(event.stanza, ret));
354         else
355                 local reply = st.reply(event.stanza)
356                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
357                 for node, node_obj in pairs(ret) do
358                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
359                 end
360                 event.origin.send(reply);
361         end
362         return true;
363 end);
364
365 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
366 local function get_affiliation(jid)
367         local bare_jid = jid_bare(jid);
368         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
369                 return admin_aff;
370         end
371 end
372
373 function set_service(new_service)
374         service = new_service;
375         module.environment.service = service;
376         disco_info = build_disco_info(service);
377 end
378
379 function module.save()
380         return { service = service };
381 end
382
383 function module.restore(data)
384         set_service(data.service);
385 end
386
387 set_service(pubsub.new({
388         capabilities = {
389                 none = {
390                         create = false;
391                         publish = false;
392                         retract = false;
393                         get_nodes = true;
394                         
395                         subscribe = true;
396                         unsubscribe = true;
397                         get_subscription = true;
398                         get_subscriptions = true;
399                         get_items = true;
400                         
401                         subscribe_other = false;
402                         unsubscribe_other = false;
403                         get_subscription_other = false;
404                         get_subscriptions_other = false;
405                         
406                         be_subscribed = true;
407                         be_unsubscribed = true;
408                         
409                         set_affiliation = false;
410                 };
411                 publisher = {
412                         create = false;
413                         publish = true;
414                         retract = true;
415                         get_nodes = true;
416                         
417                         subscribe = true;
418                         unsubscribe = true;
419                         get_subscription = true;
420                         get_subscriptions = true;
421                         get_items = true;
422                         
423                         subscribe_other = false;
424                         unsubscribe_other = false;
425                         get_subscription_other = false;
426                         get_subscriptions_other = false;
427                         
428                         be_subscribed = true;
429                         be_unsubscribed = true;
430                         
431                         set_affiliation = false;
432                 };
433                 owner = {
434                         create = true;
435                         publish = true;
436                         retract = true;
437                         delete = true;
438                         get_nodes = true;
439                         
440                         subscribe = true;
441                         unsubscribe = true;
442                         get_subscription = true;
443                         get_subscriptions = true;
444                         get_items = true;
445                         
446                         
447                         subscribe_other = true;
448                         unsubscribe_other = true;
449                         get_subscription_other = true;
450                         get_subscriptions_other = true;
451                         
452                         be_subscribed = true;
453                         be_unsubscribed = true;
454                         
455                         set_affiliation = true;
456                 };
457         };
458         
459         autocreate_on_publish = autocreate_on_publish;
460         autocreate_on_subscribe = autocreate_on_subscribe;
461         
462         broadcaster = simple_broadcast;
463         get_affiliation = get_affiliation;
464         
465         normalize_jid = jid_bare;
466 }));