comparison mod_privacy_lists/mod_privacy_lists.lua @ 1475:58d48afca54d

mod_privacy_lists: Cache privacy lists for better performance with high traffic
author Matthew Wild <mwild1@gmail.com>
date Sun, 27 Jul 2014 14:38:55 +0100
parents d4233dce479f
children 92b930be261f
comparison
equal deleted inserted replaced
1474:d4233dce479f 1475:58d48afca54d
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2009-2010 Matthew Wild 2 -- Copyright (C) 2009-2014 Matthew Wild
3 -- Copyright (C) 2009-2010 Waqas Hussain 3 -- Copyright (C) 2009-2014 Waqas Hussain
4 -- Copyright (C) 2009 Thilo Cestonaro 4 -- Copyright (C) 2009 Thilo Cestonaro
5 -- 5 --
6 -- This project is MIT/X11 licensed. Please see the 6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
8 -- 8 --
16 local jid_split, jid_join = util_Jid.split, util_Jid.join; 16 local jid_split, jid_join = util_Jid.split, util_Jid.join;
17 local load_roster = require "core.rostermanager".load_roster; 17 local load_roster = require "core.rostermanager".load_roster;
18 local to_number = tonumber; 18 local to_number = tonumber;
19 19
20 local privacy_storage = module:open_store(); 20 local privacy_storage = module:open_store();
21 local user_sessions = hosts[module.host].sessions;
22
23 local function get_lists(username)
24 return user_sessions[username].privacy_lists;
25 end
26
27 local function save_lists(username)
28 local privacy_lists = user_sessions[username].privacy_lists;
29 if privacy_lists.default == nil and next(privacy_lists.lists) == nil then
30 privacy_lists = nil;
31 end
32 return privacy_storage:set(username, privacy_lists);
33 end
34
35 module:hook("resource-bind", function (event)
36 local username = event.session.username;
37 user_sessions[username].privacy_lists = privacy_storage:get(username) or { lists = {} };
38 end);
21 39
22 function isListUsed(origin, name, privacy_lists) 40 function isListUsed(origin, name, privacy_lists)
23 local user = bare_sessions[origin.username.."@"..origin.host]; 41 local user = bare_sessions[origin.username.."@"..origin.host];
24 if user then 42 if user then
25 for resource, session in pairs(user.sessions) do 43 for resource, session in pairs(user.sessions) do
216 local origin, stanza = data.origin, data.stanza; 234 local origin, stanza = data.origin, data.stanza;
217 235
218 if stanza.attr.to == nil then -- only service requests to own bare JID 236 if stanza.attr.to == nil then -- only service requests to own bare JID
219 local query = stanza.tags[1]; -- the query element 237 local query = stanza.tags[1]; -- the query element
220 local valid = false; 238 local valid = false;
221 local privacy_lists = privacy_storage:get(origin.username) or { lists = {} }; 239 local privacy_lists = get_lists(origin.username);
222 240
223 if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8 241 if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
224 module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host); 242 module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
225 local lists = privacy_lists.lists; 243 local lists = privacy_lists.lists;
226 for idx, list in ipairs(lists) do 244 for idx, list in ipairs(lists) do
271 if valid[2] == nil then 289 if valid[2] == nil then
272 valid[2] = "bad-request"; 290 valid[2] = "bad-request";
273 end 291 end
274 origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3])); 292 origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
275 else 293 else
276 privacy_storage:set(origin.username, privacy_lists); 294 save_lists(origin.username);
277 end 295 end
278 return true; 296 return true;
279 end 297 end
280 end); 298 end);
281 299
282 function checkIfNeedToBeBlocked(e, session) 300 function checkIfNeedToBeBlocked(e, session)
283 local origin, stanza = e.origin, e.stanza; 301 local origin, stanza = e.origin, e.stanza;
284 local privacy_lists = privacy_storage:get(session.username) or {}; 302 local user = user_sessions[session.username];
303 local privacy_lists = user and user.privacy_lists;
285 local bare_jid = session.username.."@"..session.host; 304 local bare_jid = session.username.."@"..session.host;
286 local to = stanza.attr.to or bare_jid; 305 local to = stanza.attr.to or bare_jid;
287 local from = stanza.attr.from; 306 local from = stanza.attr.from;
288 307
289 local is_to_user = bare_jid == jid_bare(to); 308 local is_to_user = bare_jid == jid_bare(to);
290 local is_from_user = bare_jid == jid_bare(from); 309 local is_from_user = bare_jid == jid_bare(from);
291 310
292 --module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from)); 311 --module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
293 312
294 if privacy_lists.lists == nil or 313 if not privacy_lists or privacy_lists.lists == nil or
295 not (session.activePrivacyList or privacy_lists.default) 314 not (session.activePrivacyList or privacy_lists.default)
296 then 315 then
297 return; -- Nothing to block, default is Allow all 316 return; -- Nothing to block, default is Allow all
298 end 317 end
299 if is_from_user and is_to_user then 318 if is_from_user and is_to_user then