Comparison

plugins/mod_pep.lua @ 10520:225fade2ab4d 0.11

mod_pep: Handle presence subscriptions in filter (fixes #1372) Take two on 045209b41b3a
author Kim Alvefur <zash@zash.se>
date Mon, 10 Jun 2019 13:57:09 +0200
parent 10045:6714578cfd6e
child 10521:cbb2c3f8bb1d
child 10672:657e61531b33
comparison
equal deleted inserted replaced
10519:641e3b7a6a39 10520:225fade2ab4d
88 }; 88 };
89 end 89 end
90 return data, err; 90 return data, err;
91 end 91 end
92 function store:set(node, data) 92 function store:set(node, data)
93 if data then
94 -- Save the data without subscriptions
95 local subscribers = {};
96 for jid, sub in pairs(data.subscribers) do
97 if type(sub) ~= "table" or not sub.presence then
98 subscribers[jid] = sub;
99 end
100 end
101 data = {
102 name = data.name;
103 config = data.config;
104 affiliations = data.affiliations;
105 subscribers = subscribers;
106 };
107 end
108 return node_config:set(username, node, data); 93 return node_config:set(username, node, data);
109 end 94 end
110 function store:users() 95 function store:users()
111 return pairs(known_nodes:get(username) or {}); 96 return pairs(known_nodes:get(username) or {});
112 end 97 end
158 end 143 end
159 end 144 end
160 return simple_broadcast; 145 return simple_broadcast;
161 end 146 end
162 147
163 local function on_node_creation(event) 148 local function get_subscriber_filter(username)
164 local service = event.service; 149 return function (jids, node)
165 local node = event.node; 150 local broadcast_to = {};
166 local username = service.config.pep_username; 151 for jid, opts in pairs(jids) do
167 152 broadcast_to[jid] = opts;
168 local service_recipients = recipients[username]; 153 end
169 if not service_recipients then return; end 154
170 155 local service_recipients = recipients[username];
171 for recipient, nodes in pairs(service_recipients) do 156 if service_recipients then
172 if nodes:contains(node) then 157 local service = services[username];
173 service:add_subscription(node, recipient, recipient, { presence = true }); 158 for recipient, nodes in pairs(service_recipients) do
174 end 159 if nodes:contains(node) and service:may(node, recipient, "subscribe") then
160 broadcast_to[recipient] = true;
161 end
162 end
163 end
164 return broadcast_to;
175 end 165 end
176 end 166 end
177 167
178 function get_pep_service(username) 168 function get_pep_service(username)
179 module:log("debug", "get_pep_service(%q)", username); 169 module:log("debug", "get_pep_service(%q)", username);
194 autocreate_on_subscribe = false; 184 autocreate_on_subscribe = false;
195 185
196 nodestore = nodestore(username); 186 nodestore = nodestore(username);
197 itemstore = simple_itemstore(username); 187 itemstore = simple_itemstore(username);
198 broadcaster = get_broadcaster(username); 188 broadcaster = get_broadcaster(username);
189 subscriber_filter = get_subscriber_filter(username);
199 itemcheck = is_item_stanza; 190 itemcheck = is_item_stanza;
200 get_affiliation = function (jid) 191 get_affiliation = function (jid)
201 if jid_bare(jid) == user_bare then 192 if jid_bare(jid) == user_bare then
202 return "owner"; 193 return "owner";
203 end 194 end
230 end 221 end
231 services[username] = service; 222 services[username] = service;
232 module:add_item("pep-service", { service = service, jid = user_bare }); 223 module:add_item("pep-service", { service = service, jid = user_bare });
233 return service; 224 return service;
234 end 225 end
235
236 module:hook("item-added/pep-service", function (event)
237 local service = event.item.service;
238 module:hook_object_event(service.events, "node-created", on_node_creation);
239 end);
240 226
241 function handle_pubsub_iq(event) 227 function handle_pubsub_iq(event)
242 local origin, stanza = event.origin, event.stanza; 228 local origin, stanza = event.origin, event.stanza;
243 local service_name = origin.username; 229 local service_name = origin.username;
244 if stanza.attr.to ~= nil then 230 if stanza.attr.to ~= nil then
301 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then 287 if (current == empty_set or current:empty()) and (nodes == empty_set or nodes:empty()) then
302 return; 288 return;
303 end 289 end
304 290
305 local service = get_pep_service(service_name); 291 local service = get_pep_service(service_name);
306 for node in current - nodes do
307 service:remove_subscription(node, recipient, recipient);
308 end
309 292
310 for node in nodes - current do 293 for node in nodes - current do
311 if service:add_subscription(node, recipient, recipient, { presence = true }) then 294 if service:may(node, recipient, "subscribe") then
312 resend_last_item(recipient, node, service); 295 resend_last_item(recipient, node, service);
313 end 296 end
314 end 297 end
315 298
316 if nodes == empty_set or nodes:empty() then 299 if nodes == empty_set or nodes:empty() then