Comparison

plugins/mod_pep_plus.lua @ 9074:0462405b1cfb

mod_pep -> mod_pep_simple, mod_pep_plus -> mod_pep
author Matthew Wild <mwild1@gmail.com>
date Wed, 01 Aug 2018 19:08:09 +0100
parent 9061:82dd435c942c
comparison
equal deleted inserted replaced
9073:a5daf3f6d588 9074:0462405b1cfb
1 local pubsub = require "util.pubsub"; 1 module:log("error", "mod_pep_plus has been renamed to mod_pep, please update your config file. Auto-loading mod_pep...");
2 local jid_bare = require "util.jid".bare; 2 module:depends("pep");
3 local jid_split = require "util.jid".split;
4 local jid_join = require "util.jid".join;
5 local set_new = require "util.set".new;
6 local st = require "util.stanza";
7 local calculate_hash = require "util.caps".calculate_hash;
8 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
9 local cache = require "util.cache";
10 local set = require "util.set";
11
12 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
13 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
14 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
15
16 local lib_pubsub = module:require "pubsub";
17
18 local empty_set = set_new();
19
20 local services = {};
21 local recipients = {};
22 local hash_map = {};
23
24 local host = module.host;
25
26 local node_config = module:open_store("pep", "map");
27 local known_nodes = module:open_store("pep");
28
29 function module.save()
30 return { services = services };
31 end
32
33 function module.restore(data)
34 services = data.services;
35 end
36
37 function is_item_stanza(item)
38 return st.is_stanza(item) and item.attr.xmlns == xmlns_pubsub and item.name == "item";
39 end
40
41 local function subscription_presence(username, recipient)
42 local user_bare = jid_join(username, host);
43 local recipient_bare = jid_bare(recipient);
44 if (recipient_bare == user_bare) then return true; end
45 return is_contact_subscribed(username, host, recipient_bare);
46 end
47
48 local function nodestore(username)
49 -- luacheck: ignore 212/self
50 local store = {};
51 function store:get(node)
52 local data, err = node_config:get(username, node)
53 if data == true then
54 -- COMPAT Previously stored only a boolean representing 'persist_items'
55 data = {
56 name = node;
57 config = {};
58 subscribers = {};
59 affiliations = {};
60 };
61 end
62 return data, err;
63 end
64 function store:set(node, data)
65 if data then
66 -- Save the data without subscriptions
67 -- TODO Save explicit subscriptions maybe?
68 data = {
69 name = data.name;
70 config = data.config;
71 affiliations = data.affiliations;
72 subscribers = {};
73 };
74 end
75 return node_config:set(username, node, data);
76 end
77 function store:users()
78 return pairs(known_nodes:get(username) or {});
79 end
80 return store;
81 end
82
83 local function simple_itemstore(username)
84 return function (config, node)
85 if config["persist_items"] then
86 module:log("debug", "Creating new persistent item store for user %s, node %q", username, node);
87 local archive = module:open_store("pep_"..node, "archive");
88 return lib_pubsub.archive_itemstore(archive, config, username, node, false);
89 else
90 module:log("debug", "Creating new ephemeral item store for user %s, node %q", username, node);
91 return cache.new(tonumber(config["max_items"]));
92 end
93 end
94 end
95
96 local function get_broadcaster(username)
97 local user_bare = jid_join(username, host);
98 local function simple_broadcast(kind, node, jids, item)
99 local message = st.message({ from = user_bare, type = "headline" })
100 :tag("event", { xmlns = xmlns_pubsub_event })
101 :tag(kind, { node = node });
102 if item then
103 item = st.clone(item);
104 item.attr.xmlns = nil; -- Clear the pubsub namespace
105 message:add_child(item);
106 end
107 for jid in pairs(jids) do
108 module:log("debug", "Sending notification to %s from %s: %s", jid, user_bare, tostring(item));
109 message.attr.to = jid;
110 module:send(message);
111 end
112 end
113 return simple_broadcast;
114 end
115
116 function get_pep_service(username)
117 module:log("debug", "get_pep_service(%q)", username);
118 local user_bare = jid_join(username, host);
119 local service = services[username];
120 if service then
121 return service;
122 end
123 service = pubsub.new({
124 capabilities = {
125 none = {
126 create = false;
127 publish = false;
128 retract = false;
129 get_nodes = false;
130
131 subscribe = false;
132 unsubscribe = false;
133 get_subscription = false;
134 get_subscriptions = false;
135 get_items = false;
136
137 subscribe_other = false;
138 unsubscribe_other = false;
139 get_subscription_other = false;
140 get_subscriptions_other = false;
141
142 be_subscribed = true;
143 be_unsubscribed = true;
144
145 set_affiliation = false;
146 };
147 subscriber = {
148 create = false;
149 publish = false;
150 retract = false;
151 get_nodes = true;
152
153 subscribe = true;
154 unsubscribe = true;
155 get_subscription = true;
156 get_subscriptions = true;
157 get_items = true;
158
159 subscribe_other = false;
160 unsubscribe_other = false;
161 get_subscription_other = false;
162 get_subscriptions_other = false;
163
164 be_subscribed = true;
165 be_unsubscribed = true;
166
167 set_affiliation = false;
168 };
169 publisher = {
170 create = false;
171 publish = true;
172 retract = true;
173 get_nodes = true;
174
175 subscribe = true;
176 unsubscribe = true;
177 get_subscription = true;
178 get_subscriptions = true;
179 get_items = true;
180
181 subscribe_other = false;
182 unsubscribe_other = false;
183 get_subscription_other = false;
184 get_subscriptions_other = false;
185
186 be_subscribed = true;
187 be_unsubscribed = true;
188
189 set_affiliation = false;
190 };
191 owner = {
192 create = true;
193 publish = true;
194 retract = true;
195 delete = true;
196 get_nodes = true;
197 configure = true;
198
199 subscribe = true;
200 unsubscribe = true;
201 get_subscription = true;
202 get_subscriptions = true;
203 get_items = true;
204
205
206 subscribe_other = true;
207 unsubscribe_other = true;
208 get_subscription_other = true;
209 get_subscriptions_other = true;
210
211 be_subscribed = true;
212 be_unsubscribed = true;
213
214 set_affiliation = true;
215 };
216 };
217
218 node_defaults = {
219 ["max_items"] = 1;
220 ["persist_items"] = true;
221 };
222
223 autocreate_on_publish = true;
224 autocreate_on_subscribe = true;
225
226 nodestore = nodestore(username);
227 itemstore = simple_itemstore(username);
228 broadcaster = get_broadcaster(username);
229 itemcheck = is_item_stanza;
230 get_affiliation = function (jid)
231 if jid_bare(jid) == user_bare then
232 return "owner";
233 elseif subscription_presence(username, jid) then
234 return "subscriber";
235 end
236 end;
237
238 normalize_jid = jid_bare;
239 });
240 local nodes, err = known_nodes:get(username);
241 if nodes then
242 module:log("debug", "Restoring nodes for user %s", username);
243 for node in pairs(nodes) do
244 module:log("debug", "Restoring node %q", node);
245 service:create(node, true);
246 end
247 elseif err then
248 module:log("error", "Could not restore nodes for %s: %s", username, err);
249 else
250 module:log("debug", "No known nodes");
251 end
252 services[username] = service;
253 module:add_item("pep-service", { service = service, jid = user_bare });
254 return service;
255 end
256
257 function handle_pubsub_iq(event)
258 local origin, stanza = event.origin, event.stanza;
259 local service_name = origin.username;
260 if stanza.attr.to ~= nil then
261 service_name = jid_split(stanza.attr.to);
262 end
263 local service = get_pep_service(service_name);
264
265 return lib_pubsub.handle_pubsub_iq(event, service)
266 end
267
268 module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
269 module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
270
271 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
272 module:add_feature("http://jabber.org/protocol/pubsub#publish");
273
274 local function get_caps_hash_from_presence(stanza, current)
275 local t = stanza.attr.type;
276 if not t then
277 local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
278 if child then
279 local attr = child.attr;
280 if attr.hash then -- new caps
281 if attr.hash == 'sha-1' and attr.node and attr.ver then
282 return attr.ver, attr.node.."#"..attr.ver;
283 end
284 else -- legacy caps
285 if attr.node and attr.ver then
286 return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver;
287 end
288 end
289 end
290 return; -- no or bad caps
291 elseif t == "unavailable" or t == "error" then
292 return;
293 end
294 return current; -- no caps, could mean caps optimization, so return current
295 end
296
297 local function resend_last_item(jid, node, service)
298 local ok, id, item = service:get_last_item(node, jid);
299 if not ok then return; end
300 if not id then return; end
301 service.config.broadcaster("items", node, { [jid] = true }, item);
302 end
303
304 local function update_subscriptions(recipient, service_name, nodes)
305 nodes = nodes or empty_set;
306
307 local service_recipients = recipients[service_name];
308 if not service_recipients then
309 service_recipients = {};
310 recipients[service_name] = service_recipients;
311 end
312
313 local current = service_recipients[recipient];
314 if not current or type(current) ~= "table" then
315 current = empty_set;
316 end
317
318 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then
319 return;
320 end
321
322 local service = get_pep_service(service_name);
323 for node in current - nodes do
324 service:remove_subscription(node, recipient, recipient);
325 end
326
327 for node in nodes - current do
328 service:add_subscription(node, recipient, recipient);
329 resend_last_item(recipient, node, service);
330 end
331
332 if nodes == empty_set or nodes:empty() then
333 nodes = nil;
334 end
335
336 service_recipients[recipient] = nodes;
337 end
338
339 module:hook("presence/bare", function(event)
340 -- inbound presence to bare JID received
341 local origin, stanza = event.origin, event.stanza;
342 local t = stanza.attr.type;
343 local is_self = not stanza.attr.to;
344 local username = jid_split(stanza.attr.to);
345 local user_bare = jid_bare(stanza.attr.to);
346 if is_self then
347 username = origin.username;
348 user_bare = jid_join(username, host);
349 end
350
351 if not t then -- available presence
352 if is_self or subscription_presence(username, stanza.attr.from) then
353 local recipient = stanza.attr.from;
354 local current = recipients[username] and recipients[username][recipient];
355 local hash, query_node = get_caps_hash_from_presence(stanza, current);
356 if current == hash or (current and current == hash_map[hash]) then return; end
357 if not hash then
358 update_subscriptions(recipient, username);
359 else
360 recipients[username] = recipients[username] or {};
361 if hash_map[hash] then
362 update_subscriptions(recipient, username, hash_map[hash]);
363 else
364 recipients[username][recipient] = hash;
365 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
366 if is_self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
367 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
368 origin.send(
369 st.stanza("iq", {from=user_bare, to=stanza.attr.from, id="disco", type="get"})
370 :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
371 );
372 end
373 end
374 end
375 end
376 elseif t == "unavailable" then
377 update_subscriptions(stanza.attr.from, username);
378 elseif not is_self and t == "unsubscribe" then
379 local from = jid_bare(stanza.attr.from);
380 local subscriptions = recipients[username];
381 if subscriptions then
382 for subscriber in pairs(subscriptions) do
383 if jid_bare(subscriber) == from then
384 update_subscriptions(subscriber, username);
385 end
386 end
387 end
388 end
389 end, 10);
390
391 module:hook("iq-result/bare/disco", function(event)
392 local origin, stanza = event.origin, event.stanza;
393 local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
394 if not disco then
395 return;
396 end
397
398 -- Process disco response
399 local is_self = stanza.attr.to == nil;
400 local user_bare = jid_bare(stanza.attr.to);
401 local username = jid_split(stanza.attr.to);
402 if is_self then
403 username = origin.username;
404 user_bare = jid_join(username, host);
405 end
406 local contact = stanza.attr.from;
407 local current = recipients[username] and recipients[username][contact];
408 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
409 local ver = current;
410 if not string.find(current, "#") then
411 ver = calculate_hash(disco.tags); -- calculate hash
412 end
413 local notify = set_new();
414 for _, feature in pairs(disco.tags) do
415 if feature.name == "feature" and feature.attr.var then
416 local nfeature = feature.attr.var:match("^(.*)%+notify$");
417 if nfeature then notify:add(nfeature); end
418 end
419 end
420 hash_map[ver] = notify; -- update hash map
421 if is_self then
422 -- Optimization: Fiddle with other local users
423 for jid, item in pairs(origin.roster) do -- for all interested contacts
424 if jid then
425 local contact_node, contact_host = jid_split(jid);
426 if contact_host == host and (item.subscription == "both" or item.subscription == "from") then
427 update_subscriptions(user_bare, contact_node, notify);
428 end
429 end
430 end
431 end
432 update_subscriptions(contact, username, notify);
433 end);
434
435 module:hook("account-disco-info-node", function(event)
436 local stanza, origin = event.stanza, event.origin;
437 local service_name = origin.username;
438 if stanza.attr.to ~= nil then
439 service_name = jid_split(stanza.attr.to);
440 end
441 local service = get_pep_service(service_name);
442 return lib_pubsub.handle_disco_info_node(event, service);
443 end);
444
445 module:hook("account-disco-info", function(event)
446 local origin, reply = event.origin, event.reply;
447
448 reply:tag('identity', {category='pubsub', type='pep'}):up();
449
450 local username = jid_split(reply.attr.from) or origin.username;
451 local service = get_pep_service(username);
452
453 local supported_features = lib_pubsub.get_feature_set(service) + set.new{
454 -- Features not covered by the above
455 "access-presence",
456 "auto-subscribe",
457 "filtered-notifications",
458 "last-published",
459 "persistent-items",
460 "presence-notifications",
461 "presence-subscribe",
462 };
463
464 for feature in supported_features do
465 reply:tag('feature', {var=xmlns_pubsub.."#"..feature}):up();
466 end
467 end);
468
469 module:hook("account-disco-items-node", function(event)
470 local stanza, origin = event.stanza, event.origin;
471 local is_self = stanza.attr.to == nil;
472 local username = jid_split(stanza.attr.to);
473 if is_self then
474 username = origin.username;
475 end
476 local service = get_pep_service(username);
477 return lib_pubsub.handle_disco_items_node(event, service);
478 end);
479
480 module:hook("account-disco-items", function(event)
481 local reply, stanza, origin = event.reply, event.stanza, event.origin;
482
483 local is_self = stanza.attr.to == nil;
484 local user_bare = jid_bare(stanza.attr.to);
485 local username = jid_split(stanza.attr.to);
486 if is_self then
487 username = origin.username;
488 user_bare = jid_join(username, host);
489 end
490 local service = get_pep_service(username);
491
492 local ok, ret = service:get_nodes(jid_bare(stanza.attr.from));
493 if not ok then return; end
494
495 for node, node_obj in pairs(ret) do
496 reply:tag("item", { jid = user_bare, node = node, name = node_obj.config.name }):up();
497 end
498 end);