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