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