comparison mod_pep_plus/mod_pep_plus.lua @ 2668:f8fc79b3051a

mod_pep_plus: Import from Prosody trunk ae3c5abb3336
author Matthew Wild <mwild1@gmail.com>
date Tue, 04 Apr 2017 15:09:04 +0100
parents
children cb2342cf3f3c
comparison
equal deleted inserted replaced
2667:0c7802b95820 2668:f8fc79b3051a
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 configure = true;
137
138 subscribe = true;
139 unsubscribe = true;
140 get_subscription = true;
141 get_subscriptions = true;
142 get_items = true;
143
144
145 subscribe_other = true;
146 unsubscribe_other = true;
147 get_subscription_other = true;
148 get_subscriptions_other = true;
149
150 be_subscribed = true;
151 be_unsubscribed = true;
152
153 set_affiliation = true;
154 };
155 };
156
157 node_defaults = {
158 ["pubsub#max_items"] = "1";
159 };
160
161 autocreate_on_publish = true;
162 autocreate_on_subscribe = true;
163
164 broadcaster = get_broadcaster(name);
165 get_affiliation = function (jid)
166 if jid_bare(jid) == name then
167 return "owner";
168 elseif subscription_presence(name, jid) then
169 return "subscriber";
170 end
171 end;
172
173 normalize_jid = jid_bare;
174 });
175 services[name] = service;
176 module:add_item("pep-service", { service = service, jid = name });
177 return service;
178 end
179
180 function handle_pubsub_iq(event)
181 local origin, stanza = event.origin, event.stanza;
182 local pubsub = stanza.tags[1];
183 local action = pubsub.tags[1];
184 if not action then
185 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
186 end
187 local service_name = stanza.attr.to or origin.username.."@"..origin.host
188 local service = get_pep_service(service_name);
189 local handler = handlers[stanza.attr.type.."_"..action.name];
190 if handler then
191 handler(origin, stanza, action, service);
192 return true;
193 end
194 end
195
196 module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
197 module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
198
199 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
200 module:add_feature("http://jabber.org/protocol/pubsub#publish");
201
202 local function get_caps_hash_from_presence(stanza, current)
203 local t = stanza.attr.type;
204 if not t then
205 local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
206 if child then
207 local attr = child.attr;
208 if attr.hash then -- new caps
209 if attr.hash == 'sha-1' and attr.node and attr.ver then
210 return attr.ver, attr.node.."#"..attr.ver;
211 end
212 else -- legacy caps
213 if attr.node and attr.ver then
214 return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver;
215 end
216 end
217 end
218 return; -- no or bad caps
219 elseif t == "unavailable" or t == "error" then
220 return;
221 end
222 return current; -- no caps, could mean caps optimization, so return current
223 end
224
225 local function resend_last_item(jid, node, service)
226 local ok, items = service:get_items(node, jid);
227 if not ok then return; end
228 for i, id in ipairs(items) do
229 service.config.broadcaster("items", node, { [jid] = true }, items[id]);
230 end
231 end
232
233 local function update_subscriptions(recipient, service_name, nodes)
234 local service = get_pep_service(service_name);
235 nodes = nodes or empty_set;
236
237 local service_recipients = recipients[service_name];
238 if not service_recipients then
239 service_recipients = {};
240 recipients[service_name] = service_recipients;
241 end
242
243 local current = service_recipients[recipient];
244 if not current or type(current) ~= "table" then
245 current = empty_set;
246 end
247
248 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then
249 return;
250 end
251
252 for node in current - nodes do
253 service:remove_subscription(node, recipient, recipient);
254 end
255
256 for node in nodes - current do
257 service:add_subscription(node, recipient, recipient);
258 resend_last_item(recipient, node, service);
259 end
260
261 if nodes == empty_set or nodes:empty() then
262 nodes = nil;
263 end
264
265 service_recipients[recipient] = nodes;
266 end
267
268 module:hook("presence/bare", function(event)
269 -- inbound presence to bare JID recieved
270 local origin, stanza = event.origin, event.stanza;
271 local user = stanza.attr.to or (origin.username..'@'..origin.host);
272 local t = stanza.attr.type;
273 local self = not stanza.attr.to;
274 local service = get_pep_service(user);
275
276 if not t then -- available presence
277 if self or subscription_presence(user, stanza.attr.from) then
278 local recipient = stanza.attr.from;
279 local current = recipients[user] and recipients[user][recipient];
280 local hash, query_node = get_caps_hash_from_presence(stanza, current);
281 if current == hash or (current and current == hash_map[hash]) then return; end
282 if not hash then
283 update_subscriptions(recipient, user);
284 else
285 recipients[user] = recipients[user] or {};
286 if hash_map[hash] then
287 update_subscriptions(recipient, user, hash_map[hash]);
288 else
289 recipients[user][recipient] = hash;
290 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
291 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
292 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
293 origin.send(
294 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
295 :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
296 );
297 end
298 end
299 end
300 end
301 elseif t == "unavailable" then
302 update_subscriptions(stanza.attr.from, user);
303 elseif not self and t == "unsubscribe" then
304 local from = jid_bare(stanza.attr.from);
305 local subscriptions = recipients[user];
306 if subscriptions then
307 for subscriber in pairs(subscriptions) do
308 if jid_bare(subscriber) == from then
309 update_subscriptions(subscriber, user);
310 end
311 end
312 end
313 end
314 end, 10);
315
316 module:hook("iq-result/bare/disco", function(event)
317 local origin, stanza = event.origin, event.stanza;
318 local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
319 if not disco then
320 return;
321 end
322
323 -- Process disco response
324 local self = not stanza.attr.to;
325 local user = stanza.attr.to or (origin.username..'@'..origin.host);
326 local contact = stanza.attr.from;
327 local current = recipients[user] and recipients[user][contact];
328 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
329 local ver = current;
330 if not string.find(current, "#") then
331 ver = calculate_hash(disco.tags); -- calculate hash
332 end
333 local notify = set_new();
334 for _, feature in pairs(disco.tags) do
335 if feature.name == "feature" and feature.attr.var then
336 local nfeature = feature.attr.var:match("^(.*)%+notify$");
337 if nfeature then notify:add(nfeature); end
338 end
339 end
340 hash_map[ver] = notify; -- update hash map
341 if self then
342 for jid, item in pairs(origin.roster) do -- for all interested contacts
343 if item.subscription == "both" or item.subscription == "from" then
344 if not recipients[jid] then recipients[jid] = {}; end
345 update_subscriptions(contact, jid, notify);
346 end
347 end
348 end
349 update_subscriptions(contact, user, notify);
350 end);
351
352 module:hook("account-disco-info-node", function(event)
353 local reply, stanza, origin = event.reply, event.stanza, event.origin;
354 local service_name = stanza.attr.to or origin.username.."@"..origin.host
355 local service = get_pep_service(service_name);
356 local node = event.node;
357 local ok = service:get_items(node, jid_bare(stanza.attr.from) or true);
358 if not ok then return; end
359 event.exists = true;
360 reply:tag('identity', {category='pubsub', type='leaf'}):up();
361 end);
362
363 module:hook("account-disco-info", function(event)
364 local reply = event.reply;
365 reply:tag('identity', {category='pubsub', type='pep'}):up();
366 reply:tag('feature', {var='http://jabber.org/protocol/pubsub#publish'}):up();
367 end);
368
369 module:hook("account-disco-items-node", function(event)
370 local reply, stanza, origin = event.reply, event.stanza, event.origin;
371 local node = event.node;
372 local service_name = stanza.attr.to or origin.username.."@"..origin.host
373 local service = get_pep_service(service_name);
374 local ok, ret = service:get_items(node, jid_bare(stanza.attr.from) or true);
375 if not ok then return; end
376 event.exists = true;
377 for _, id in ipairs(ret) do
378 reply:tag("item", { jid = service_name, name = id }):up();
379 end
380 end);
381
382 module:hook("account-disco-items", function(event)
383 local reply, stanza, origin = event.reply, event.stanza, event.origin;
384
385 local service_name = reply.attr.from or origin.username.."@"..origin.host
386 local service = get_pep_service(service_name);
387 local ok, ret = service:get_nodes(jid_bare(stanza.attr.from));
388 if not ok then return; end
389
390 for node, node_obj in pairs(ret) do
391 reply:tag("item", { jid = service_name, node = node, name = node_obj.config.name }):up();
392 end
393 end);