mod_pep_plus: Only broadcast newly added subscriptions
[prosody.git] / plugins / mod_pep_plus.lua
1 local pubsub = require "util.pubsub";
2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local set_new = require "util.set".new;
5 local st = require "util.stanza";
6 local calculate_hash = require "util.caps".calculate_hash;
7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
8
9 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
11 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
12
13 local lib_pubsub = module:require "pubsub";
14 local handlers = lib_pubsub.handlers;
15 local pubsub_error_reply = lib_pubsub.pubsub_error_reply;
16
17 local empty_set = set_new();
18
19 local services = {};
20 local recipients = {};
21 local hash_map = {};
22
23 function module.save()
24         return { services = services };
25 end
26
27 function module.restore(data)
28         services = data.services;
29 end
30
31 local function subscription_presence(user_bare, recipient)
32         local recipient_bare = jid_bare(recipient);
33         if (recipient_bare == user_bare) then return true; end
34         local username, host = jid_split(user_bare);
35         return is_contact_subscribed(username, host, recipient_bare);
36 end
37
38 local function get_broadcaster(name)
39         local function simple_broadcast(kind, node, jids, item)
40                 if item then
41                         item = st.clone(item);
42                         item.attr.xmlns = nil; -- Clear the pubsub namespace
43                 end
44                 local message = st.message({ from = name, type = "headline" })
45                         :tag("event", { xmlns = xmlns_pubsub_event })
46                                 :tag(kind, { node = node })
47                                         :add_child(item);
48                 for jid in pairs(jids) do
49                         module:log("debug", "Sending notification to %s from %s: %s", jid, name, tostring(item));
50                         message.attr.to = jid;
51                         module:send(message);
52                 end
53         end
54         return simple_broadcast;
55 end
56
57 function get_pep_service(name)
58         if services[name] then
59                 return services[name];
60         end
61         services[name] = pubsub.new({
62                 capabilities = {
63                         none = {
64                                 create = false;
65                                 publish = false;
66                                 retract = false;
67                                 get_nodes = false;
68
69                                 subscribe = false;
70                                 unsubscribe = false;
71                                 get_subscription = false;
72                                 get_subscriptions = false;
73                                 get_items = false;
74
75                                 subscribe_other = false;
76                                 unsubscribe_other = false;
77                                 get_subscription_other = false;
78                                 get_subscriptions_other = false;
79
80                                 be_subscribed = true;
81                                 be_unsubscribed = true;
82
83                                 set_affiliation = false;
84                         };
85                         subscriber = {
86                                 create = false;
87                                 publish = false;
88                                 retract = false;
89                                 get_nodes = true;
90
91                                 subscribe = true;
92                                 unsubscribe = true;
93                                 get_subscription = true;
94                                 get_subscriptions = true;
95                                 get_items = true;
96
97                                 subscribe_other = false;
98                                 unsubscribe_other = false;
99                                 get_subscription_other = false;
100                                 get_subscriptions_other = false;
101
102                                 be_subscribed = true;
103                                 be_unsubscribed = true;
104
105                                 set_affiliation = false;
106                         };
107                         publisher = {
108                                 create = false;
109                                 publish = true;
110                                 retract = true;
111                                 get_nodes = true;
112
113                                 subscribe = true;
114                                 unsubscribe = true;
115                                 get_subscription = true;
116                                 get_subscriptions = true;
117                                 get_items = true;
118
119                                 subscribe_other = false;
120                                 unsubscribe_other = false;
121                                 get_subscription_other = false;
122                                 get_subscriptions_other = false;
123
124                                 be_subscribed = true;
125                                 be_unsubscribed = true;
126
127                                 set_affiliation = false;
128                         };
129                         owner = {
130                                 create = true;
131                                 publish = true;
132                                 retract = true;
133                                 delete = true;
134                                 get_nodes = true;
135
136                                 subscribe = true;
137                                 unsubscribe = true;
138                                 get_subscription = true;
139                                 get_subscriptions = true;
140                                 get_items = true;
141
142
143                                 subscribe_other = true;
144                                 unsubscribe_other = true;
145                                 get_subscription_other = true;
146                                 get_subscriptions_other = true;
147
148                                 be_subscribed = true;
149                                 be_unsubscribed = true;
150
151                                 set_affiliation = true;
152                         };
153                 };
154
155                 autocreate_on_publish = true;
156                 autocreate_on_subscribe = true;
157
158                 broadcaster = get_broadcaster(name);
159                 get_affiliation = function (jid)
160                         if jid_bare(jid) == name then
161                                 return "owner";
162                         elseif subscription_presence(name, jid) then
163                                 return "subscriber";
164                         end
165                 end;
166
167                 normalize_jid = jid_bare;
168         });
169         return services[name];
170 end
171
172 function handle_pubsub_iq(event)
173         local origin, stanza = event.origin, event.stanza;
174         local pubsub = stanza.tags[1];
175         local action = pubsub.tags[1];
176         if not action then
177                 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
178         end
179         local service_name = stanza.attr.to or origin.username.."@"..origin.host
180         local service = get_pep_service(service_name);
181         local handler = handlers[stanza.attr.type.."_"..action.name];
182         if handler then
183                 handler(origin, stanza, action, service);
184                 return true;
185         end
186 end
187
188 module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
189 module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
190
191 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
192 module:add_feature("http://jabber.org/protocol/pubsub#publish");
193
194 local function get_caps_hash_from_presence(stanza, current)
195         local t = stanza.attr.type;
196         if not t then
197                 local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
198                 if child then
199                         local attr = child.attr;
200                         if attr.hash then -- new caps
201                                 if attr.hash == 'sha-1' and attr.node and attr.ver then
202                                         return attr.ver, attr.node.."#"..attr.ver;
203                                 end
204                         else -- legacy caps
205                                 if attr.node and attr.ver then
206                                         return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver;
207                                 end
208                         end
209                 end
210                 return; -- no or bad caps
211         elseif t == "unavailable" or t == "error" then
212                 return;
213         end
214         return current; -- no caps, could mean caps optimization, so return current
215 end
216
217 local function resend_last_item(jid, node, service)
218         local ok, items = service:get_items(node, jid);
219         if not ok then return; end
220         for i, id in ipairs(items) do
221                 service.config.broadcaster("items", node, { [jid] = true }, items[id]);
222         end
223 end
224
225 local function update_subscriptions(recipient, service_name, nodes)
226         local service = get_pep_service(service_name);
227         nodes = nodes or empty_set;
228
229         local service_recipients = recipients[service_name];
230         if not service_recipients then
231                 service_recipients = {};
232                 recipients[service_name] = service_recipients;
233         end
234
235         local current = service_recipients[recipient];
236         if not current or type(current) ~= "table" then
237                 current = empty_set;
238         end
239
240         if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then
241                 return;
242         end
243
244         for node in current - nodes do
245                 service:remove_subscription(node, recipient, recipient);
246         end
247
248         for node in nodes - current do
249                 service:add_subscription(node, recipient, recipient);
250                 resend_last_item(recipient, node, service);
251         end
252
253         if nodes == empty_set or nodes:empty() then
254                 nodes = nil;
255         end
256
257         service_recipients[recipient] = nodes;
258 end
259
260 module:hook("presence/bare", function(event)
261         -- inbound presence to bare JID recieved
262         local origin, stanza = event.origin, event.stanza;
263         local user = stanza.attr.to or (origin.username..'@'..origin.host);
264         local t = stanza.attr.type;
265         local self = not stanza.attr.to;
266         local service = get_pep_service(user);
267
268         if not t then -- available presence
269                 if self or subscription_presence(user, stanza.attr.from) then
270                         local recipient = stanza.attr.from;
271                         local current = recipients[user] and recipients[user][recipient];
272                         local hash, query_node = get_caps_hash_from_presence(stanza, current);
273                         if current == hash or (current and current == hash_map[hash]) then return; end
274                         if not hash then
275                                 update_subscriptions(recipient, user);
276                         else
277                                 recipients[user] = recipients[user] or {};
278                                 if hash_map[hash] then
279                                         update_subscriptions(recipient, user, hash_map[hash]);
280                                 else
281                                         recipients[user][recipient] = hash;
282                                         local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
283                                         if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
284                                                 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
285                                                 origin.send(
286                                                         st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
287                                                                 :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
288                                                 );
289                                         end
290                                 end
291                         end
292                 end
293         elseif t == "unavailable" then
294                 update_subscriptions(stanza.attr.from, user);
295         elseif not self and t == "unsubscribe" then
296                 local from = jid_bare(stanza.attr.from);
297                 local subscriptions = recipients[user];
298                 if subscriptions then
299                         for subscriber in pairs(subscriptions) do
300                                 if jid_bare(subscriber) == from then
301                                         update_subscriptions(subscriber, user);
302                                 end
303                         end
304                 end
305         end
306 end, 10);
307
308 module:hook("iq-result/bare/disco", function(event)
309         local origin, stanza = event.origin, event.stanza;
310         local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
311         if not disco then
312                 return;
313         end
314
315         -- Process disco response
316         local self = not stanza.attr.to;
317         local user = stanza.attr.to or (origin.username..'@'..origin.host);
318         local contact = stanza.attr.from;
319         local current = recipients[user] and recipients[user][contact];
320         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
321         local ver = current;
322         if not string.find(current, "#") then
323                 ver = calculate_hash(disco.tags); -- calculate hash
324         end
325         local notify = set_new();
326         for _, feature in pairs(disco.tags) do
327                 if feature.name == "feature" and feature.attr.var then
328                         local nfeature = feature.attr.var:match("^(.*)%+notify$");
329                         if nfeature then notify:add(nfeature); end
330                 end
331         end
332         hash_map[ver] = notify; -- update hash map
333         if self then
334                 for jid, item in pairs(origin.roster) do -- for all interested contacts
335                         if item.subscription == "both" or item.subscription == "from" then
336                                 if not recipients[jid] then recipients[jid] = {}; end
337                                 update_subscriptions(contact, jid, notify);
338                         end
339                 end
340         end
341         update_subscriptions(contact, user, notify);
342 end);
343
344 module:hook("account-disco-info-node", function(event)
345         local reply, stanza, origin = event.reply, event.stanza, event.origin;
346         local service_name = stanza.attr.to or origin.username.."@"..origin.host
347         local service = get_pep_service(service_name);
348         local node = event.node;
349         local ok = service:get_items(node, jid_bare(stanza.attr.from) or true);
350         if not ok then return; end
351         event.exists = true;
352         reply:tag('identity', {category='pubsub', type='leaf'}):up();
353 end);
354
355 module:hook("account-disco-info", function(event)
356         local reply = event.reply;
357         reply:tag('identity', {category='pubsub', type='pep'}):up();
358         reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
359 end);
360
361 module:hook("account-disco-items-node", function(event)
362         local reply, stanza, origin = event.reply, event.stanza, event.origin;
363         local node = event.node;
364         local service_name = stanza.attr.to or origin.username.."@"..origin.host
365         local service = get_pep_service(service_name);
366         local ok, ret = service:get_items(node, jid_bare(stanza.attr.from) or true);
367         if not ok then return; end
368         event.exists = true;
369         for _, id in ipairs(ret) do
370                 reply:tag("item", { jid = service_name, name = id }):up();
371         end
372 end);
373
374 module:hook("account-disco-items", function(event)
375         local reply, stanza, origin = event.reply, event.stanza, event.origin;
376
377         local service_name = reply.attr.from or origin.username.."@"..origin.host
378         local service = get_pep_service(service_name);
379         local ok, ret = service:get_nodes(jid_bare(stanza.attr.from));
380         if not ok then return; end
381
382         for node, node_obj in pairs(ret) do
383                 reply:tag("item", { jid = service_name, node = node, name = node_obj.config.name }):up();
384         end
385 end);