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