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