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