Mercurial > prosody-hg
comparison plugins/mod_privacy.lua @ 2523:0c816b271208
mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 29 Jan 2010 03:21:18 +0000 |
| parents | 9f9d605ef65d |
| children | b427f5401ce7 |
comparison
equal
deleted
inserted
replaced
| 2522:9f9d605ef65d | 2523:0c816b271208 |
|---|---|
| 374 | 374 |
| 375 function checkIfNeedToBeBlocked(e, session) | 375 function checkIfNeedToBeBlocked(e, session) |
| 376 local origin, stanza = e.origin, e.stanza; | 376 local origin, stanza = e.origin, e.stanza; |
| 377 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {}; | 377 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {}; |
| 378 local bare_jid = session.username.."@"..session.host; | 378 local bare_jid = session.username.."@"..session.host; |
| 379 | 379 local to = stanza.attr.to; |
| 380 module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from)); | 380 local from = stanza.attr.from; |
| 381 | 381 |
| 382 if stanza.attr.to ~= nil and stanza.attr.from ~= nil then | 382 local to_user = bare_jid == jid_bare(to); |
| 383 if privacy_lists.lists == nil or | 383 local from_user = bare_jid == jid_bare(from); |
| 384 (session.activePrivacyList == nil or session.activePrivacyList == "") and | 384 |
| 385 (privacy_lists.default == nil or privacy_lists.default == "") | 385 module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from)); |
| 386 then | 386 |
| 387 return; -- Nothing to block, default is Allow all | 387 if privacy_lists.lists == nil or |
| 388 end | 388 (session.activePrivacyList == nil or session.activePrivacyList == "") and |
| 389 if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then | 389 (privacy_lists.default == nil or privacy_lists.default == "") |
| 390 module:log("debug", "Never block communications from one of a user's resources to another."); | 390 then |
| 391 return; -- from one of a user's resource to another => HANDS OFF! | 391 return; -- Nothing to block, default is Allow all |
| 392 end | 392 end |
| 393 | 393 if from_user and to_user then |
| 394 local idx; | 394 module:log("debug", "Never block communications from one of a user's resources to another."); |
| 395 local list; | 395 return; -- from one of a user's resource to another => HANDS OFF! |
| 396 local item; | 396 end |
| 397 local listname = session.activePrivacyList; | 397 |
| 398 if listname == nil or listname == "" then | 398 local idx; |
| 399 listname = privacy_lists.default; -- no active list selected, use default list | 399 local list; |
| 400 end | 400 local item; |
| 401 idx = findNamedList(privacy_lists, listname); | 401 local listname = session.activePrivacyList; |
| 402 if idx == nil then | 402 if listname == nil or listname == "" then |
| 403 module:log("error", "given privacy listname not found. name: %s", listname); | 403 listname = privacy_lists.default; -- no active list selected, use default list |
| 404 return; | 404 end |
| 405 end | 405 idx = findNamedList(privacy_lists, listname); |
| 406 list = privacy_lists.lists[idx]; | 406 if idx == nil then |
| 407 if list == nil then | 407 module:log("error", "given privacy listname not found. name: %s", listname); |
| 408 module:log("info", "privacy list index wrong. index: %d", idx); | 408 return; |
| 409 return; | 409 end |
| 410 end | 410 list = privacy_lists.lists[idx]; |
| 411 for _,item in ipairs(list.items) do | 411 if list == nil then |
| 412 local apply = false; | 412 module:log("info", "privacy list index wrong. index: %d", idx); |
| 413 local block = false; | 413 return; |
| 414 if ( | 414 end |
| 415 (stanza.name == "message" and item.message) or | 415 for _,item in ipairs(list.items) do |
| 416 (stanza.name == "iq" and item.iq) or | 416 local apply = false; |
| 417 (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or | 417 local block = false; |
| 418 (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or | 418 if ( |
| 419 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false) | 419 (stanza.name == "message" and item.message) or |
| 420 ) then | 420 (stanza.name == "iq" and item.iq) or |
| 421 (stanza.name == "presence" and to_user and item["presence-in"]) or | |
| 422 (stanza.name == "presence" and from_user and item["presence-out"]) or | |
| 423 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false) | |
| 424 ) then | |
| 425 apply = true; | |
| 426 end | |
| 427 if apply then | |
| 428 local evilJid = {}; | |
| 429 apply = false; | |
| 430 if to_user then | |
| 431 module:log("debug", "evil jid is (from): %s", from); | |
| 432 evilJid.node, evilJid.host, evilJid.resource = jid_split(from); | |
| 433 else | |
| 434 module:log("debug", "evil jid is (to): %s", to); | |
| 435 evilJid.node, evilJid.host, evilJid.resource = jid_split(to); | |
| 436 end | |
| 437 if item.type == "jid" and | |
| 438 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or | |
| 439 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or | |
| 440 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or | |
| 441 (evilJid.host and item.value == evilJid.host) then | |
| 421 apply = true; | 442 apply = true; |
| 422 end | 443 block = (item.action == "deny"); |
| 423 if apply then | 444 elseif item.type == "group" then |
| 424 local evilJid = {}; | 445 local roster = load_roster(session.username, session.host); |
| 425 apply = false; | 446 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups; |
| 426 if jid_bare(stanza.attr.to) == bare_jid then | 447 for group in pairs(groups) do |
| 427 module:log("debug", "evil jid is (from): %s", stanza.attr.from); | 448 if group == item.value then |
| 428 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from); | 449 apply = true; |
| 429 else | 450 block = (item.action == "deny"); |
| 430 module:log("debug", "evil jid is (to): %s", stanza.attr.to); | 451 break; |
| 431 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to); | 452 end |
| 432 end | 453 end |
| 433 if item.type == "jid" and | 454 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid |
| 434 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or | 455 local roster = load_roster(session.username, session.host); |
| 435 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or | 456 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then |
| 436 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or | |
| 437 (evilJid.host and item.value == evilJid.host) then | |
| 438 apply = true; | 457 apply = true; |
| 439 block = (item.action == "deny"); | 458 block = (item.action == "deny"); |
| 440 elseif item.type == "group" then | 459 end |
| 441 local roster = load_roster(session.username, session.host); | 460 elseif item.type == nil then |
| 442 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups; | 461 apply = true; |
| 443 for group in pairs(groups) do | 462 block = (item.action == "deny"); |
| 444 if group == item.value then | 463 end |
| 445 apply = true; | 464 end |
| 446 block = (item.action == "deny"); | 465 if apply then |
| 447 break; | 466 if block then |
| 448 end | 467 module:log("info", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from)); |
| 449 end | 468 if stanza.name == "message" then |
| 450 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid | 469 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
| 451 local roster = load_roster(session.username, session.host); | 470 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then |
| 452 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then | 471 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
| 453 apply = true; | 472 end |
| 454 block = (item.action == "deny"); | 473 return true; -- stanza blocked ! |
| 455 end | 474 else |
| 456 elseif item.type == nil then | 475 module:log("info", "stanza explicit allowed!") |
| 457 apply = true; | 476 return; |
| 458 block = (item.action == "deny"); | |
| 459 end | |
| 460 end | |
| 461 if apply then | |
| 462 if block then | |
| 463 module:log("info", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from)); | |
| 464 if stanza.name == "message" then | |
| 465 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 466 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then | |
| 467 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 468 end | |
| 469 return true; -- stanza blocked ! | |
| 470 else | |
| 471 module:log("info", "stanza explicit allowed!") | |
| 472 return; | |
| 473 end | |
| 474 end | 477 end |
| 475 end | 478 end |
| 476 end | 479 end |
| 477 end | 480 end |
| 478 | 481 |
