mod_pubsub: Send node items to new subscribers
[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 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
7 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
8 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
9
10 local autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false);
11 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false);
12 local pubsub_disco_name = module:get_option("name");
13 if type(pubsub_disco_name) ~= "string" then pubsub_disco_name = "Prosody PubSub Service"; end
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("pubsub", { xmlns = xmlns_pubsub })
78                         :tag("subscriptions");
79         for _, sub in ipairs(ret) do
80                 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
81         end
82         return origin.send(reply);
83 end
84
85 function handlers.set_create(origin, stanza, create)
86         local node = create.attr.node;
87         local ok, ret, reply;
88         if node then
89                 ok, ret = service:create(node, stanza.attr.from);
90                 if ok then
91                         reply = st.reply(stanza);
92                 else
93                         reply = pubsub_error_reply(stanza, ret);
94                 end
95         else
96                 repeat
97                         node = uuid_generate();
98                         ok, ret = service:create(node, stanza.attr.from);
99                 until ok or ret ~= "conflict";
100                 if ok then
101                         reply = st.reply(stanza)
102                                 :tag("pubsub", { xmlns = xmlns_pubsub })
103                                         :tag("create", { node = node });
104                 else
105                         reply = pubsub_error_reply(stanza, ret);
106                 end
107         end
108         return origin.send(reply);
109 end
110
111 function handlers.set_subscribe(origin, stanza, subscribe)
112         local node, jid = subscribe.attr.node, subscribe.attr.jid;
113         local options_tag, options = stanza.tags[1]:get_child("options"), nil;
114         if options_tag then
115                 options = options_form:data(options_tag.tags[1]);
116         end
117         local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
118         local reply;
119         if ok then
120                 reply = st.reply(stanza)
121                         :tag("pubsub", { xmlns = xmlns_pubsub })
122                                 :tag("subscription", {
123                                         node = node,
124                                         jid = jid,
125                                         subscription = "subscribed"
126                                 }):up();
127                 if options_tag then
128                         reply:add_child(options_tag);
129                 end
130         else
131                 reply = pubsub_error_reply(stanza, ret);
132         end
133         origin.send(reply);
134         if ok then
135                 -- Send all current items
136                 local ok, items = service:get_items(node, stanza.attr.from);
137                 if items then
138                         local jids = { [jid] = options or true };
139                         for id, item in pairs(items) do
140                                 service.config.broadcaster(node, jids, item);
141                         end
142                 end
143         end
144 end
145
146 function handlers.set_unsubscribe(origin, stanza, unsubscribe)
147         local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
148         local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
149         local reply;
150         if ok then
151                 reply = st.reply(stanza);
152         else
153                 reply = pubsub_error_reply(stanza, ret);
154         end
155         return origin.send(reply);
156 end
157
158 function handlers.set_publish(origin, stanza, publish)
159         local node = publish.attr.node;
160         local item = publish:get_child("item");
161         local id = (item and item.attr.id) or uuid_generate();
162         local ok, ret = service:publish(node, stanza.attr.from, id, item);
163         local reply;
164         if ok then
165                 reply = st.reply(stanza)
166                         :tag("pubsub", { xmlns = xmlns_pubsub })
167                                 :tag("publish", { node = node })
168                                         :tag("item", { id = id });
169         else
170                 reply = pubsub_error_reply(stanza, ret);
171         end
172         return origin.send(reply);
173 end
174
175 function handlers.set_retract(origin, stanza, retract)
176         local node, notify = retract.attr.node, retract.attr.notify;
177         notify = (notify == "1") or (notify == "true");
178         local item = retract:get_child("item");
179         local id = item and item.attr.id
180         local reply, notifier;
181         if notify then
182                 notifier = st.stanza("retract", { id = id });
183         end
184         local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
185         if ok then
186                 reply = st.reply(stanza);
187         else
188                 reply = pubsub_error_reply(stanza, ret);
189         end
190         return origin.send(reply);
191 end
192
193 function simple_broadcast(node, jids, item)
194         item = st.clone(item);
195         item.attr.xmlns = nil; -- Clear the pubsub namespace
196         local message = st.message({ from = module.host, type = "headline" })
197                 :tag("event", { xmlns = xmlns_pubsub_event })
198                         :tag("items", { node = node })
199                                 :add_child(item);
200         for jid in pairs(jids) do
201                 module:log("debug", "Sending notification to %s", jid);
202                 message.attr.to = jid;
203                 core_post_stanza(hosts[module.host], message);
204         end
205 end
206
207 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
208
209 local disco_info;
210
211 local feature_map = {
212         create = { "create-nodes", autocreate_on_publish and "instant-nodes", "item-ids" };
213         retract = { "delete-items", "retract-items" };
214         publish = { "publish" };
215         get_items = { "retrieve-items" };
216         add_subscription = { "subscribe" };
217         get_subscriptions = { "retrieve-subscriptions" };
218 };
219
220 local function add_disco_features_from_service(disco, service)
221         for method, features in pairs(feature_map) do
222                 if service[method] then
223                         for _, feature in ipairs(features) do
224                                 if feature then
225                                         disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
226                                 end
227                         end
228                 end
229         end
230         for affiliation in pairs(service.config.capabilities) do
231                 if affiliation ~= "none" and affiliation ~= "owner" then
232                         disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
233                 end
234         end
235 end
236
237 local function build_disco_info(service)
238         local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
239                 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up()
240                 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
241         add_disco_features_from_service(disco_info, service);
242         return disco_info;
243 end
244
245 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
246         local origin, stanza = event.origin, event.stanza;
247         local node = stanza.tags[1].attr.node;
248         if not node then
249                 return origin.send(st.reply(stanza):add_child(disco_info));
250         else
251                 local ok, ret = service:get_nodes(stanza.attr.from);
252                 if ok and not ret[node] then
253                         ok, ret = false, "item-not-found";
254                 end
255                 if not ok then
256                         return origin.send(pubsub_error_reply(stanza, ret));
257                 end
258                 local reply = st.reply(stanza)
259                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
260                                 :tag("identity", { category = "pubsub", type = "leaf" });
261                 return origin.send(reply);
262         end
263 end);
264
265 local function handle_disco_items_on_node(event)
266         local stanza, origin = event.stanza, event.origin;
267         local query = stanza.tags[1];
268         local node = query.attr.node;
269         local ok, ret = service:get_items(node, stanza.attr.from);
270         if not ok then
271                 return origin.send(pubsub_error_reply(stanza, ret));
272         end
273         
274         local reply = st.reply(stanza)
275                 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
276         
277         for id, item in pairs(ret) do
278                 reply:tag("item", { jid = module.host, name = id }):up();
279         end
280         
281         return origin.send(reply);
282 end
283
284
285 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
286         if event.stanza.tags[1].attr.node then
287                 return handle_disco_items_on_node(event);
288         end
289         local ok, ret = service:get_nodes(event.stanza.attr.from);
290         if not ok then
291                 event.origin.send(pubsub_error_reply(stanza, ret));
292         else
293                 local reply = st.reply(event.stanza)
294                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
295                 for node, node_obj in pairs(ret) do
296                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
297                 end
298                 event.origin.send(reply);
299         end
300         return true;
301 end);
302
303 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
304 local function get_affiliation(jid)
305         local bare_jid = jid_bare(jid);
306         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
307                 return admin_aff;
308         end
309 end
310
311 function set_service(new_service)
312         service = new_service;
313         module.environment.service = service;
314         disco_info = build_disco_info(service);
315 end
316
317 function module.save()
318         return { service = service };
319 end
320
321 function module.restore(data)
322         set_service(data.service);
323 end
324
325 set_service(pubsub.new({
326         capabilities = {
327                 none = {
328                         create = false;
329                         publish = false;
330                         retract = false;
331                         get_nodes = true;
332                         
333                         subscribe = true;
334                         unsubscribe = true;
335                         get_subscription = true;
336                         get_subscriptions = true;
337                         get_items = true;
338                         
339                         subscribe_other = false;
340                         unsubscribe_other = false;
341                         get_subscription_other = false;
342                         get_subscriptions_other = false;
343                         
344                         be_subscribed = true;
345                         be_unsubscribed = true;
346                         
347                         set_affiliation = false;
348                 };
349                 owner = {
350                         create = true;
351                         publish = true;
352                         retract = true;
353                         get_nodes = true;
354                         
355                         subscribe = true;
356                         unsubscribe = true;
357                         get_subscription = true;
358                         get_subscriptions = true;
359                         get_items = true;
360                         
361                         
362                         subscribe_other = true;
363                         unsubscribe_other = true;
364                         get_subscription_other = true;
365                         get_subscriptions_other = true;
366                         
367                         be_subscribed = true;
368                         be_unsubscribed = true;
369                         
370                         set_affiliation = true;
371                 };
372         };
373         
374         autocreate_on_publish = autocreate_on_publish;
375         autocreate_on_subscribe = autocreate_on_subscribe;
376         
377         broadcaster = simple_broadcast;
378         get_affiliation = get_affiliation;
379         
380         normalize_jid = jid_bare;
381 }));