Merge 0.9->trunk
[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) or uuid_generate();
194         local ok, ret = service:publish(node, stanza.attr.from, id, item);
195         local reply;
196         if ok then
197                 reply = st.reply(stanza)
198                         :tag("pubsub", { xmlns = xmlns_pubsub })
199                                 :tag("publish", { node = node })
200                                         :tag("item", { id = id });
201         else
202                 reply = pubsub_error_reply(stanza, ret);
203         end
204         return origin.send(reply);
205 end
206
207 function handlers.set_retract(origin, stanza, retract)
208         local node, notify = retract.attr.node, retract.attr.notify;
209         notify = (notify == "1") or (notify == "true");
210         local item = retract:get_child("item");
211         local id = item and item.attr.id
212         if not (node and id) then
213                 return origin.send(pubsub_error_reply(stanza, node and "item-not-found" or "nodeid-required"));
214         end
215         local reply, notifier;
216         if notify then
217                 notifier = st.stanza("retract", { id = id });
218         end
219         local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
220         if ok then
221                 reply = st.reply(stanza);
222         else
223                 reply = pubsub_error_reply(stanza, ret);
224         end
225         return origin.send(reply);
226 end
227
228 function handlers.set_purge(origin, stanza, purge)
229         local node, notify = purge.attr.node, purge.attr.notify;
230         notify = (notify == "1") or (notify == "true");
231         local reply;
232         if not node then
233                 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
234         end
235         local ok, ret = service:purge(node, stanza.attr.from, notify);
236         if ok then
237                 reply = st.reply(stanza);
238         else
239                 reply = pubsub_error_reply(stanza, ret);
240         end
241         return origin.send(reply);
242 end
243
244 function simple_broadcast(kind, node, jids, item)
245         if item then
246                 item = st.clone(item);
247                 item.attr.xmlns = nil; -- Clear the pubsub namespace
248         end
249         local message = st.message({ from = module.host, type = "headline" })
250                 :tag("event", { xmlns = xmlns_pubsub_event })
251                         :tag(kind, { node = node })
252                                 :add_child(item);
253         for jid in pairs(jids) do
254                 module:log("debug", "Sending notification to %s", jid);
255                 message.attr.to = jid;
256                 module:send(message);
257         end
258 end
259
260 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
261 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
262
263 local disco_info;
264
265 local feature_map = {
266         create = { "create-nodes", "instant-nodes", "item-ids" };
267         retract = { "delete-items", "retract-items" };
268         purge = { "purge-nodes" };
269         publish = { "publish", autocreate_on_publish and "auto-create" };
270         delete = { "delete-nodes" };
271         get_items = { "retrieve-items" };
272         add_subscription = { "subscribe" };
273         get_subscriptions = { "retrieve-subscriptions" };
274 };
275
276 local function add_disco_features_from_service(disco, service)
277         for method, features in pairs(feature_map) do
278                 if service[method] then
279                         for _, feature in ipairs(features) do
280                                 if feature then
281                                         disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
282                                 end
283                         end
284                 end
285         end
286         for affiliation in pairs(service.config.capabilities) do
287                 if affiliation ~= "none" and affiliation ~= "owner" then
288                         disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
289                 end
290         end
291 end
292
293 local function build_disco_info(service)
294         local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
295                 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up()
296                 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
297         add_disco_features_from_service(disco_info, service);
298         return disco_info;
299 end
300
301 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
302         local origin, stanza = event.origin, event.stanza;
303         local node = stanza.tags[1].attr.node;
304         if not node then
305                 return origin.send(st.reply(stanza):add_child(disco_info));
306         else
307                 local ok, ret = service:get_nodes(stanza.attr.from);
308                 if ok and not ret[node] then
309                         ok, ret = false, "item-not-found";
310                 end
311                 if not ok then
312                         return origin.send(pubsub_error_reply(stanza, ret));
313                 end
314                 local reply = st.reply(stanza)
315                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
316                                 :tag("identity", { category = "pubsub", type = "leaf" });
317                 return origin.send(reply);
318         end
319 end);
320
321 local function handle_disco_items_on_node(event)
322         local stanza, origin = event.stanza, event.origin;
323         local query = stanza.tags[1];
324         local node = query.attr.node;
325         local ok, ret = service:get_items(node, stanza.attr.from);
326         if not ok then
327                 return origin.send(pubsub_error_reply(stanza, ret));
328         end
329         
330         local reply = st.reply(stanza)
331                 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
332         
333         for id, item in pairs(ret) do
334                 reply:tag("item", { jid = module.host, name = id }):up();
335         end
336         
337         return origin.send(reply);
338 end
339
340
341 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
342         if event.stanza.tags[1].attr.node then
343                 return handle_disco_items_on_node(event);
344         end
345         local ok, ret = service:get_nodes(event.stanza.attr.from);
346         if not ok then
347                 event.origin.send(pubsub_error_reply(event.stanza, ret));
348         else
349                 local reply = st.reply(event.stanza)
350                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
351                 for node, node_obj in pairs(ret) do
352                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
353                 end
354                 event.origin.send(reply);
355         end
356         return true;
357 end);
358
359 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
360 local function get_affiliation(jid)
361         local bare_jid = jid_bare(jid);
362         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
363                 return admin_aff;
364         end
365 end
366
367 function set_service(new_service)
368         service = new_service;
369         module.environment.service = service;
370         disco_info = build_disco_info(service);
371 end
372
373 function module.save()
374         return { service = service };
375 end
376
377 function module.restore(data)
378         set_service(data.service);
379 end
380
381 set_service(pubsub.new({
382         capabilities = {
383                 none = {
384                         create = false;
385                         publish = false;
386                         retract = false;
387                         get_nodes = true;
388                         
389                         subscribe = true;
390                         unsubscribe = true;
391                         get_subscription = true;
392                         get_subscriptions = true;
393                         get_items = true;
394                         
395                         subscribe_other = false;
396                         unsubscribe_other = false;
397                         get_subscription_other = false;
398                         get_subscriptions_other = false;
399                         
400                         be_subscribed = true;
401                         be_unsubscribed = true;
402                         
403                         set_affiliation = false;
404                 };
405                 publisher = {
406                         create = false;
407                         publish = true;
408                         retract = true;
409                         get_nodes = true;
410                         
411                         subscribe = true;
412                         unsubscribe = true;
413                         get_subscription = true;
414                         get_subscriptions = true;
415                         get_items = true;
416                         
417                         subscribe_other = false;
418                         unsubscribe_other = false;
419                         get_subscription_other = false;
420                         get_subscriptions_other = false;
421                         
422                         be_subscribed = true;
423                         be_unsubscribed = true;
424                         
425                         set_affiliation = false;
426                 };
427                 owner = {
428                         create = true;
429                         publish = true;
430                         retract = true;
431                         delete = true;
432                         get_nodes = true;
433                         
434                         subscribe = true;
435                         unsubscribe = true;
436                         get_subscription = true;
437                         get_subscriptions = true;
438                         get_items = true;
439                         
440                         
441                         subscribe_other = true;
442                         unsubscribe_other = true;
443                         get_subscription_other = true;
444                         get_subscriptions_other = true;
445                         
446                         be_subscribed = true;
447                         be_unsubscribed = true;
448                         
449                         set_affiliation = true;
450                 };
451         };
452         
453         autocreate_on_publish = autocreate_on_publish;
454         autocreate_on_subscribe = autocreate_on_subscribe;
455         
456         broadcaster = simple_broadcast;
457         get_affiliation = get_affiliation;
458         
459         normalize_jid = jid_bare;
460 }));