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