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