Comparison

plugins/mod_pep.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:plugins/mod_pep_plus.lua@82dd435c942c
child 9077:aefb96a52f5f
comparison
equal deleted inserted replaced
9073:a5daf3f6d588 9074:0462405b1cfb
1 -- Prosody IM 1 local pubsub = require "util.pubsub";
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local jid_bare = require "util.jid".bare; 2 local jid_bare = require "util.jid".bare;
11 local jid_split = require "util.jid".split; 3 local jid_split = require "util.jid".split;
4 local jid_join = require "util.jid".join;
5 local set_new = require "util.set".new;
12 local st = require "util.stanza"; 6 local st = require "util.stanza";
7 local calculate_hash = require "util.caps".calculate_hash;
13 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed; 8 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
14 local pairs = pairs; 9 local cache = require "util.cache";
15 local next = next; 10 local set = require "util.set";
16 local type = type;
17 local calculate_hash = require "util.caps".calculate_hash;
18 local core_post_stanza = prosody.core_post_stanza;
19 local bare_sessions = prosody.bare_sessions;
20 11
21 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; 12 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
22 13 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
23 -- Used as canonical 'empty table' 14 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
24 local NULL = {}; 15
25 -- data[user_bare_jid][node] = item_stanza 16 local lib_pubsub = module:require "pubsub";
26 local data = {}; 17
27 --- recipients[user_bare_jid][contact_full_jid][subscribed_node] = true 18 local empty_set = set_new();
19
20 local services = {};
28 local recipients = {}; 21 local recipients = {};
29 -- hash_map[hash][subscribed_nodes] = true
30 local hash_map = {}; 22 local hash_map = {};
31 23
32 module.save = function() 24 local host = module.host;
33 return { data = data, recipients = recipients, hash_map = hash_map }; 25
34 end 26 local node_config = module:open_store("pep", "map");
35 module.restore = function(state) 27 local known_nodes = module:open_store("pep");
36 data = state.data or {}; 28
37 recipients = state.recipients or {}; 29 function module.save()
38 hash_map = state.hash_map or {}; 30 return { services = services };
39 end 31 end
40 32
41 local function subscription_presence(user_bare, recipient) 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);
42 local recipient_bare = jid_bare(recipient); 43 local recipient_bare = jid_bare(recipient);
43 if (recipient_bare == user_bare) then return true end 44 if (recipient_bare == user_bare) then return true; end
44 local username, host = jid_split(user_bare);
45 return is_contact_subscribed(username, host, recipient_bare); 45 return is_contact_subscribed(username, host, recipient_bare);
46 end 46 end
47 47
48 module:hook("pep-publish-item", function (event) 48 local function nodestore(username)
49 local session, bare, node, id, item = event.session, event.user, event.node, event.id, event.item; 49 -- luacheck: ignore 212/self
50 item.attr.xmlns = nil; 50 local store = {};
51 local disable = #item.tags ~= 1 or #item.tags[1] == 0; 51 function store:get(node)
52 if #item.tags == 0 then item.name = "retract"; end 52 local data, err = node_config:get(username, node)
53 local stanza = st.message({from=bare, type='headline'}) 53 if data == true then
54 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'}) 54 -- COMPAT Previously stored only a boolean representing 'persist_items'
55 :tag('items', {node=node}) 55 data = {
56 :add_child(item) 56 name = node;
57 :up() 57 config = {};
58 :up(); 58 subscribers = {};
59 59 affiliations = {};
60 -- store for the future 60 };
61 local user_data = data[bare]; 61 end
62 if disable then 62 return data, err;
63 if user_data then 63 end
64 user_data[node] = nil; 64 function store:set(node, data)
65 if not next(user_data) then data[bare] = nil; end 65 if data then
66 end 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);
67 else 249 else
68 if not user_data then user_data = {}; data[bare] = user_data; end 250 module:log("debug", "No known nodes");
69 user_data[node] = {id, item}; 251 end
70 end 252 services[username] = service;
71 253 module:add_item("pep-service", { service = service, jid = user_bare });
72 -- broadcast 254 return service;
73 for recipient, notify in pairs(recipients[bare] or NULL) do 255 end
74 if notify[node] then 256
75 stanza.attr.to = recipient; 257 function handle_pubsub_iq(event)
76 core_post_stanza(session, stanza); 258 local origin, stanza = event.origin, event.stanza;
77 end 259 local service_name = origin.username;
78 end 260 if stanza.attr.to ~= nil then
79 end); 261 service_name = jid_split(stanza.attr.to);
80 262 end
81 local function publish_all(user, recipient, session) 263 local service = get_pep_service(service_name);
82 local d = data[user]; 264
83 local notify = recipients[user] and recipients[user][recipient]; 265 return lib_pubsub.handle_pubsub_iq(event, service)
84 if d and notify then 266 end
85 for node in pairs(notify) do 267
86 if d[node] then 268 module:hook("iq/bare/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
87 local id, item = unpack(d[node]); 269 module:hook("iq/bare/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
88 session.send(st.message({from=user, to=recipient, type='headline'}) 270
89 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'}) 271 module:add_identity("pubsub", "pep", module:get_option_string("name", "Prosody"));
90 :tag('items', {node=node}) 272 module:add_feature("http://jabber.org/protocol/pubsub#publish");
91 :add_child(item)
92 :up()
93 :up());
94 end
95 end
96 end
97 end
98 273
99 local function get_caps_hash_from_presence(stanza, current) 274 local function get_caps_hash_from_presence(stanza, current)
100 local t = stanza.attr.type; 275 local t = stanza.attr.type;
101 if not t then 276 if not t then
102 for _, child in pairs(stanza.tags) do 277 local child = stanza:get_child("c", "http://jabber.org/protocol/caps");
103 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then 278 if child then
104 local attr = child.attr; 279 local attr = child.attr;
105 if attr.hash then -- new caps 280 if attr.hash then -- new caps
106 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end 281 if attr.hash == 'sha-1' and attr.node and attr.ver then
107 else -- legacy caps 282 return attr.ver, attr.node.."#"..attr.ver;
108 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
109 end 283 end
110 return; -- bad caps format 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
111 end 288 end
112 end 289 end
290 return; -- no or bad caps
113 elseif t == "unavailable" or t == "error" then 291 elseif t == "unavailable" or t == "error" then
114 return; 292 return;
115 end 293 end
116 return current; -- no caps, could mean caps optimization, so return current 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;
117 end 337 end
118 338
119 module:hook("presence/bare", function(event) 339 module:hook("presence/bare", function(event)
120 -- inbound presence to bare JID received 340 -- inbound presence to bare JID received
121 local origin, stanza = event.origin, event.stanza; 341 local origin, stanza = event.origin, event.stanza;
122 local user = stanza.attr.to or (origin.username..'@'..origin.host);
123 local t = stanza.attr.type; 342 local t = stanza.attr.type;
124 local self = not stanza.attr.to; 343 local is_self = not stanza.attr.to;
125 344 local username = jid_split(stanza.attr.to);
126 -- Only cache subscriptions if user is online 345 local user_bare = jid_bare(stanza.attr.to);
127 if not bare_sessions[user] then return; end 346 if is_self then
347 username = origin.username;
348 user_bare = jid_join(username, host);
349 end
128 350
129 if not t then -- available presence 351 if not t then -- available presence
130 if self or subscription_presence(user, stanza.attr.from) then 352 if is_self or subscription_presence(username, stanza.attr.from) then
131 local recipient = stanza.attr.from; 353 local recipient = stanza.attr.from;
132 local current = recipients[user] and recipients[user][recipient]; 354 local current = recipients[username] and recipients[username][recipient];
133 local hash = get_caps_hash_from_presence(stanza, current); 355 local hash, query_node = get_caps_hash_from_presence(stanza, current);
134 if current == hash or (current and current == hash_map[hash]) then return; end 356 if current == hash or (current and current == hash_map[hash]) then return; end
135 if not hash then 357 if not hash then
136 if recipients[user] then recipients[user][recipient] = nil; end 358 update_subscriptions(recipient, username);
137 else 359 else
138 recipients[user] = recipients[user] or {}; 360 recipients[username] = recipients[username] or {};
139 if hash_map[hash] then 361 if hash_map[hash] then
140 recipients[user][recipient] = hash_map[hash]; 362 update_subscriptions(recipient, username, hash_map[hash]);
141 publish_all(user, recipient, origin);
142 else 363 else
143 recipients[user][recipient] = hash; 364 recipients[username][recipient] = hash;
144 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host; 365 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
145 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then 366 if is_self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
146 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute 367 -- COMPAT from ~= stanza.attr.to because OneTeam can't deal with missing from attribute
147 origin.send( 368 origin.send(
148 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"}) 369 st.stanza("iq", {from=user_bare, to=stanza.attr.from, id="disco", type="get"})
149 :query("http://jabber.org/protocol/disco#info") 370 :tag("query", {xmlns = "http://jabber.org/protocol/disco#info", node = query_node})
150 ); 371 );
151 end 372 end
152 end 373 end
153 end 374 end
154 end 375 end
155 elseif t == "unavailable" then 376 elseif t == "unavailable" then
156 if recipients[user] then recipients[user][stanza.attr.from] = nil; end 377 update_subscriptions(stanza.attr.from, username);
157 elseif not self and t == "unsubscribe" then 378 elseif not is_self and t == "unsubscribe" then
158 local from = jid_bare(stanza.attr.from); 379 local from = jid_bare(stanza.attr.from);
159 local subscriptions = recipients[user]; 380 local subscriptions = recipients[username];
160 if subscriptions then 381 if subscriptions then
161 for subscriber in pairs(subscriptions) do 382 for subscriber in pairs(subscriptions) do
162 if jid_bare(subscriber) == from then 383 if jid_bare(subscriber) == from then
163 recipients[user][subscriber] = nil; 384 update_subscriptions(subscriber, username);
164 end 385 end
165 end 386 end
166 end 387 end
167 end 388 end
168 end, 10); 389 end, 10);
169 390
170 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event) 391 module:hook("iq-result/bare/disco", function(event)
171 local session, stanza = event.origin, event.stanza; 392 local origin, stanza = event.origin, event.stanza;
172 local payload = stanza.tags[1]; 393 local disco = stanza:get_child("query", "http://jabber.org/protocol/disco#info");
173 394 if not disco then
174 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then 395 return;
175 payload = payload.tags[1]; -- <publish node='http://jabber.org/protocol/tune'> 396 end
176 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then 397
177 local node = payload.attr.node; 398 -- Process disco response
178 payload = payload.tags[1]; 399 local is_self = stanza.attr.to == nil;
179 if payload and payload.name == "item" then -- <item> 400 local user_bare = jid_bare(stanza.attr.to);
180 local id = payload.attr.id or "1"; 401 local username = jid_split(stanza.attr.to);
181 payload.attr.id = id; 402 if is_self then
182 session.send(st.reply(stanza)); 403 username = origin.username;
183 module:fire_event("pep-publish-item", { 404 user_bare = jid_join(username, host);
184 node = node, user = jid_bare(session.full_jid), actor = session.jid, 405 end
185 id = id, session = session, item = st.clone(payload); 406 local contact = stanza.attr.from;
186 }); 407 local current = recipients[username] and recipients[username][contact];
187 return true; 408 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
188 else 409 local ver = current;
189 module:log("debug", "Payload is missing the <item>", node); 410 if not string.find(current, "#") then
190 end 411 ver = calculate_hash(disco.tags); -- calculate hash
191 else 412 end
192 module:log("debug", "Unhandled payload: %s", payload and payload:top_tag() or "(no payload)"); 413 local notify = set_new();
193 end 414 for _, feature in pairs(disco.tags) do
194 elseif stanza.attr.type == 'get' then 415 if feature.name == "feature" and feature.attr.var then
195 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host; 416 local nfeature = feature.attr.var:match("^(.*)%+notify$");
196 if subscription_presence(user, stanza.attr.from) then 417 if nfeature then notify:add(nfeature); end
197 local user_data = data[user]; 418 end
198 local node, requested_id; 419 end
199 payload = payload.tags[1]; 420 hash_map[ver] = notify; -- update hash map
200 if payload and payload.name == 'items' then 421 if is_self then
201 node = payload.attr.node; 422 -- Optimization: Fiddle with other local users
202 local item = payload.tags[1]; 423 for jid, item in pairs(origin.roster) do -- for all interested contacts
203 if item and item.name == "item" then 424 if jid then
204 requested_id = item.attr.id; 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);
205 end 428 end
206 end 429 end
207 if node and user_data and user_data[node] then -- Send the last item 430 end
208 local id, item = unpack(user_data[node]); 431 end
209 if not requested_id or id == requested_id then 432 update_subscriptions(contact, username, notify);
210 local reply_stanza = st.reply(stanza)
211 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
212 :tag('items', {node=node})
213 :add_child(item)
214 :up()
215 :up();
216 session.send(reply_stanza);
217 return true;
218 else -- requested item doesn't exist
219 local reply_stanza = st.reply(stanza)
220 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
221 :tag('items', {node=node})
222 :up();
223 session.send(reply_stanza);
224 return true;
225 end
226 elseif node then -- node doesn't exist
227 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
228 module:log("debug", "Item '%s' not found", node)
229 return true;
230 else --invalid request
231 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
232 module:log("debug", "Invalid request: %s", tostring(payload));
233 return true;
234 end
235 else --no presence subscription
236 session.send(st.error_reply(stanza, 'auth', 'not-authorized')
237 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
238 module:log("debug", "Unauthorized request: %s", tostring(payload));
239 return true;
240 end
241 end
242 end); 433 end);
243 434
244 module:hook("iq-result/bare/disco", function(event) 435 module:hook("account-disco-info-node", function(event)
245 local session, stanza = event.origin, event.stanza; 436 local stanza, origin = event.stanza, event.origin;
246 if stanza.attr.type == "result" then 437 local service_name = origin.username;
247 local disco = stanza.tags[1]; 438 if stanza.attr.to ~= nil then
248 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then 439 service_name = jid_split(stanza.attr.to);
249 -- Process disco response 440 end
250 local self = not stanza.attr.to; 441 local service = get_pep_service(service_name);
251 local user = stanza.attr.to or (session.username..'@'..session.host); 442 return lib_pubsub.handle_disco_info_node(event, service);
252 local contact = stanza.attr.from;
253 local current = recipients[user] and recipients[user][contact];
254 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
255 local ver = current;
256 if not string.find(current, "#") then
257 ver = calculate_hash(disco.tags); -- calculate hash
258 end
259 local notify = {};
260 for _, feature in pairs(disco.tags) do
261 if feature.name == "feature" and feature.attr.var then
262 local nfeature = feature.attr.var:match("^(.*)%+notify$");
263 if nfeature then notify[nfeature] = true; end
264 end
265 end
266 hash_map[ver] = notify; -- update hash map
267 if self then
268 for jid, item in pairs(session.roster) do -- for all interested contacts
269 if item.subscription == "both" or item.subscription == "from" then
270 if not recipients[jid] then recipients[jid] = {}; end
271 recipients[jid][contact] = notify;
272 publish_all(jid, contact, session);
273 end
274 end
275 end
276 recipients[user][contact] = notify; -- set recipient's data to calculated data
277 -- send messages to recipient
278 publish_all(user, contact, session);
279 end
280 end
281 end); 443 end);
282 444
283 module:hook("account-disco-info", function(event) 445 module:hook("account-disco-info", function(event)
284 local reply = event.reply; 446 local origin, reply = event.origin, event.reply;
447
285 reply:tag('identity', {category='pubsub', type='pep'}):up(); 448 reply:tag('identity', {category='pubsub', type='pep'}):up();
286 reply:tag('feature', {var=xmlns_pubsub}):up(); 449
287 local features = { 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
288 "access-presence", 455 "access-presence",
289 "auto-create",
290 "auto-subscribe", 456 "auto-subscribe",
291 "filtered-notifications", 457 "filtered-notifications",
292 "item-ids",
293 "last-published", 458 "last-published",
459 "persistent-items",
294 "presence-notifications", 460 "presence-notifications",
295 "presence-subscribe", 461 "presence-subscribe",
296 "publish",
297 "retract-items",
298 "retrieve-items",
299 }; 462 };
300 for _, feature in ipairs(features) do 463
464 for feature in supported_features do
301 reply:tag('feature', {var=xmlns_pubsub.."#"..feature}):up(); 465 reply:tag('feature', {var=xmlns_pubsub.."#"..feature}):up();
302 end 466 end
303 end); 467 end);
304 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
305 module:hook("account-disco-items", function(event) 480 module:hook("account-disco-items", function(event)
306 local reply = event.reply; 481 local reply, stanza, origin = event.reply, event.stanza, event.origin;
307 local bare = reply.attr.to; 482
308 local user_data = data[bare]; 483 local is_self = stanza.attr.to == nil;
309 484 local user_bare = jid_bare(stanza.attr.to);
310 if user_data then 485 local username = jid_split(stanza.attr.to);
311 for node, _ in pairs(user_data) do 486 if is_self then
312 reply:tag('item', {jid=bare, node=node}):up(); 487 username = origin.username;
313 end 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();
314 end 497 end
315 end); 498 end);
316
317 module:hook("account-disco-info-node", function (event)
318 local stanza, node = event.stanza, event.node;
319 local user = stanza.attr.to;
320 local user_data = data[user];
321 if user_data and user_data[node] then
322 event.exists = true;
323 event.reply:tag('identity', {category='pubsub', type='leaf'}):up();
324 end
325 end);
326
327 module:hook("resource-unbind", function (event)
328 local user_bare_jid = event.session.username.."@"..event.session.host;
329 if not bare_sessions[user_bare_jid] then -- User went offline
330 -- We don't need this info cached anymore, clear it.
331 recipients[user_bare_jid] = nil;
332 end
333 end);