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