comparison mod_privacy/mod_privacy.lua @ 42:bbb3d3a90a70

mod_privacy: decrease the log messages count.
author Thilo Cestonaro <thilo@cestona.ro>
date Thu, 15 Oct 2009 11:38:57 +0200
parents 0381d5d38c37
children 3f5bbd7c90d4
comparison
equal deleted inserted replaced
41:0381d5d38c37 42:bbb3d3a90a70
18 local to_number = _G.tonumber; 18 local to_number = _G.tonumber;
19 19
20 function findNamedList (privacy_lists, name) 20 function findNamedList (privacy_lists, name)
21 local ret = nil 21 local ret = nil
22 if privacy_lists.lists == nil then 22 if privacy_lists.lists == nil then
23 module:log("debug", "privacy_lists.list is nil. no lists loaded.")
24 return nil; 23 return nil;
25 end 24 end
26 25
27 module:log("debug", "searching for list: %s", name);
28 for i=1, #privacy_lists.lists do 26 for i=1, #privacy_lists.lists do
29 if privacy_lists.lists[i].name == name then 27 if privacy_lists.lists[i].name == name then
30 ret = i; 28 ret = i;
31 break; 29 break;
32 end 30 end
37 function isListUsed(origin, name, privacy_lists) 35 function isListUsed(origin, name, privacy_lists)
38 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then 36 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
39 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do 37 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
40 if resource ~= origin.resource then 38 if resource ~= origin.resource then
41 if session.activePrivacyList == name then 39 if session.activePrivacyList == name then
42 module:log("debug", "List {0} is in use.", name);
43 return true; 40 return true;
44 elseif session.activePrivacyList == nil and privacy_lists.default == name then 41 elseif session.activePrivacyList == nil and privacy_lists.default == name then
45 module:log("debug", "List {0} is in use.", name);
46 return true; 42 return true;
47 end 43 end
48 end 44 end
49 end 45 end
50 end 46 end
51 module:log("debug", "List {0} is in NOT use.", name);
52 return false; 47 return false;
53 end 48 end
54 49
55 function isAnotherSessionUsingDefaultList(origin) 50 function isAnotherSessionUsingDefaultList(origin)
56 local ret = false 51 local ret = false
57 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then 52 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
58 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do 53 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
59 if resource ~= origin.resource and session.activePrivacyList == nil then 54 if resource ~= origin.resource and session.activePrivacyList == nil then
60 module:log("debug", "Default list is used by another resource.");
61 ret = true; 55 ret = true;
62 break; 56 break;
63 end 57 end
64 end 58 end
65 end 59 end
66 return ret; 60 return ret;
67 end 61 end
68 62
69 function declineList (privacy_lists, origin, stanza, which) 63 function declineList (privacy_lists, origin, stanza, which)
70 module:log("info", "User requests to decline the use of privacy list: %s", which);
71 if which == "default" then 64 if which == "default" then
72 if isAnotherSessionUsingDefaultList(origin) then 65 if isAnotherSessionUsingDefaultList(origin) then
73 return { "cancel", "conflict", "Another session is online and using the default list."}; 66 return { "cancel", "conflict", "Another session is online and using the default list."};
74 end 67 end
75 privacy_lists.default = nil; 68 privacy_lists.default = nil;
82 end 75 end
83 return true; 76 return true;
84 end 77 end
85 78
86 function activateList (privacy_lists, origin, stanza, which, name) 79 function activateList (privacy_lists, origin, stanza, which, name)
87 module:log("info", "User requests to change the privacy list: %s, to be list named %s", which, name);
88 local idx = findNamedList(privacy_lists, name); 80 local idx = findNamedList(privacy_lists, name);
89 81
90 if privacy_lists.default == nil then 82 if privacy_lists.default == nil then
91 privacy_lists.default = ""; 83 privacy_lists.default = "";
92 end 84 end
108 end 100 end
109 return true; 101 return true;
110 end 102 end
111 103
112 function deleteList (privacy_lists, origin, stanza, name) 104 function deleteList (privacy_lists, origin, stanza, name)
113 module:log("info", "User requests to delete privacy list: %s", name);
114 local idx = findNamedList(privacy_lists, name); 105 local idx = findNamedList(privacy_lists, name);
115 106
116 if idx ~= nil then 107 if idx ~= nil then
117 if isListUsed(origin, name, privacy_lists) then 108 if isListUsed(origin, name, privacy_lists) then
118 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."}; 109 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
136 end 127 end
137 return false; 128 return false;
138 end 129 end
139 130
140 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster) 131 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
141 module:log("info", "User requests to create / replace list named %s, item count: %d", name, #entries);
142 local idx = findNamedList(privacy_lists, name); 132 local idx = findNamedList(privacy_lists, name);
143 local bare_jid = origin.username.."@"..origin.host; 133 local bare_jid = origin.username.."@"..origin.host;
144 134
145 if privacy_lists.lists == nil then 135 if privacy_lists.lists == nil then
146 privacy_lists.lists = {}; 136 privacy_lists.lists = {};
235 end 225 end
236 return true; 226 return true;
237 end 227 end
238 228
239 function getList(privacy_lists, origin, stanza, name) 229 function getList(privacy_lists, origin, stanza, name)
240 module:log("info", "User requests list named: %s", name or "nil");
241 local reply = st.reply(stanza); 230 local reply = st.reply(stanza);
242 reply:tag("query", {xmlns="jabber:iq:privacy"}); 231 reply:tag("query", {xmlns="jabber:iq:privacy"});
243 232
244 if name == nil then 233 if name == nil then
245 reply:tag("active", {name=origin.activePrivacyList or ""}):up(); 234 reply:tag("active", {name=origin.activePrivacyList or ""}):up();
249 reply:tag("list", {name=list.name}):up(); 238 reply:tag("list", {name=list.name}):up();
250 end 239 end
251 end 240 end
252 else 241 else
253 local idx = findNamedList(privacy_lists, name); 242 local idx = findNamedList(privacy_lists, name);
254 module:log("debug", "list idx: %d", idx or -1);
255 if idx ~= nil then 243 if idx ~= nil then
256 list = privacy_lists.lists[idx]; 244 list = privacy_lists.lists[idx];
257 reply = reply:tag("list", {name=list.name}); 245 reply = reply:tag("list", {name=list.name});
258 for _,item in ipairs(list.items) do 246 for _,item in ipairs(list.items) do
259 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order}); 247 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
291 end 279 end
292 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list 280 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
293 if #tag.tags == 0 then -- Client removes a privacy list 281 if #tag.tags == 0 then -- Client removes a privacy list
294 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name); 282 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
295 else -- Client edits a privacy list 283 else -- Client edits a privacy list
296 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags); -- TODO check if used! 284 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
297 end 285 end
298 end 286 end
299 end 287 end
300 end 288 end
301 elseif stanza.attr.type == "get" then 289 elseif stanza.attr.type == "get" then
333 function checkIfNeedToBeBlocked(e, session) 321 function checkIfNeedToBeBlocked(e, session)
334 local origin, stanza = e.origin, e.stanza; 322 local origin, stanza = e.origin, e.stanza;
335 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {}; 323 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
336 local bare_jid = session.username.."@"..session.host; 324 local bare_jid = session.username.."@"..session.host;
337 325
338 module:log("debug", "checkIfNeedToBeBlocked: username: %s, host: %s", session.username, session.host);
339 module:log("debug", "stanza: %s, to: %s, form: %s", stanza.name, stanza.attr.to or "nil", stanza.attr.from or "nil"); 326 module:log("debug", "stanza: %s, to: %s, form: %s", stanza.name, stanza.attr.to or "nil", stanza.attr.from or "nil");
340 327
341 if stanza.attr.to ~= nil and stanza.attr.from ~= nil then 328 if stanza.attr.to ~= nil and stanza.attr.from ~= nil then
342 module:log("debug", "privacy_lists.lists: %s", tostring(privacy_lists.lists));
343 module:log("debug", "session.activePrivacyList: %s", tostring(session.activePrivacyList));
344 module:log("debug", "privacy_lists.default: %s", tostring(privacy_lists.default));
345 if privacy_lists.lists == nil or 329 if privacy_lists.lists == nil or
346 (session.activePrivacyList == nil or session.activePrivacyList == "") and 330 (session.activePrivacyList == nil or session.activePrivacyList == "") and
347 (privacy_lists.default == nil or privacy_lists.default == "") 331 (privacy_lists.default == nil or privacy_lists.default == "")
348 then 332 then
349 module:log("debug", "neither active nor default list set (both are nil) or privacy_lists totally nil. So nothing to do => default is Allow All.");
350 return; -- Nothing to block, default is Allow all 333 return; -- Nothing to block, default is Allow all
351 end 334 end
352 if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then 335 if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then
353 module:log("debug", "Never block communications from one of a user's resources to another."); 336 module:log("debug", "Never block communications from one of a user's resources to another.");
354 return; -- from one of a user's resource to another => HANDS OFF! 337 return; -- from one of a user's resource to another => HANDS OFF!
372 return; 355 return;
373 end 356 end
374 for _,item in ipairs(list.items) do 357 for _,item in ipairs(list.items) do
375 local apply = false; 358 local apply = false;
376 local block = false; 359 local block = false;
377 if (stanza.name == "message" and item.message) then 360 if (
378 module:log("debug", "message stanza match."); 361 (stanza.name == "message" and item.message) or
379 apply = true; 362 (stanza.name == "iq" and item.iq) or
380 elseif (stanza.name == "iq" and item.iq) then 363 (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or
381 module:log("debug", "iq stanza match!"); 364 (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or
382 apply = true; 365 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-in"] == false)
383 elseif (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) then 366 ) then
384 module:log("debug", "presence-in stanza match.");
385 apply = true;
386 elseif (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) then
387 module:log("debug", "presence-out stanza match");
388 apply = true;
389 elseif (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-in"] == false) then
390 module:log("debug", "all is false, so apply.");
391 apply = true; 367 apply = true;
392 end 368 end
393 if apply then 369 if apply then
394 local evilJid = {}; 370 local evilJid = {};
395 apply = false; 371 apply = false;
398 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from); 374 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from);
399 else 375 else
400 module:log("debug", "evil jid is (to): %s", stanza.attr.to); 376 module:log("debug", "evil jid is (to): %s", stanza.attr.to);
401 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to); 377 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to);
402 end 378 end
403 module:log("debug", "Item Type: %s", tostring(item.type));
404 module:log("debug", "Item Action: %s", item.action);
405 if item.type == "jid" and 379 if item.type == "jid" and
406 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or 380 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
407 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or 381 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
408 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or 382 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
409 (evilJid.host and item.value == evilJid.host) then 383 (evilJid.host and item.value == evilJid.host) then
410 module:log("debug", "jid matched.");
411 apply = true; 384 apply = true;
412 block = (item.action == "deny"); 385 block = (item.action == "deny");
413 elseif item.type == "group" then 386 elseif item.type == "group" then
414 local roster = load_roster(session.username, session.host); 387 local roster = load_roster(session.username, session.host);
415 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups; 388 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
416 for group in pairs(groups) do 389 for group in pairs(groups) do
417 if group == item.value then 390 if group == item.value then
418 module:log("debug", "group matched.");
419 apply = true; 391 apply = true;
420 block = (item.action == "deny"); 392 block = (item.action == "deny");
421 break; 393 break;
422 end 394 end
423 end 395 end
424 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid 396 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
425 local roster = load_roster(session.username, session.host); 397 local roster = load_roster(session.username, session.host);
426 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then 398 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
427 module:log("debug", "subscription matched.");
428 apply = true; 399 apply = true;
429 block = (item.action == "deny"); 400 block = (item.action == "deny");
430 end 401 end
431 elseif item.type == nil then 402 elseif item.type == nil then
432 module:log("debug", "no item.type, so matched.");
433 apply = true; 403 apply = true;
434 block = (item.action == "deny"); 404 block = (item.action == "deny");
435 end 405 end
436 end 406 end
437 if apply then 407 if apply then