diff options
-rw-r--r-- | plugins/mod_privacy.lua | 93 |
1 files changed, 37 insertions, 56 deletions
diff --git a/plugins/mod_privacy.lua b/plugins/mod_privacy.lua index 4578870f..18f7384e 100644 --- a/plugins/mod_privacy.lua +++ b/plugins/mod_privacy.lua @@ -10,31 +10,27 @@ local prosody = prosody; local st = require "util.stanza"; local datamanager = require "util.datamanager"; -local bare_sessions = bare_sessions; +local bare_sessions, full_sessions = bare_sessions, full_sessions; local util_Jid = require "util.jid"; local jid_bare = util_Jid.bare; local jid_split = util_Jid.split; local load_roster = require "core.rostermanager".load_roster; -local to_number = _G.tonumber; +local to_number = tonumber; -function findNamedList (privacy_lists, name) - local ret = nil - if privacy_lists.lists == nil then - return nil; - end - - for i=1, #privacy_lists.lists do - if privacy_lists.lists[i].name == name then - ret = i; - break; +function findNamedList(privacy_lists, name) + if privacy_lists.lists then + for i=1,#privacy_lists.lists do + if privacy_lists.lists[i].name == name then + return i; + end end end - return ret; end -function isListUsed(origin, name, privacy_lists) - if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then - for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do +function isListUsed(origin, name, privacy_lists) + local user = bare_sessions[origin.username.."@"..origin.host]; + if user then + for resource, session in pairs(user.sessions) do if resource ~= origin.resource then if session.activePrivacyList == name then return true; @@ -44,36 +40,34 @@ function isListUsed(origin, name, privacy_lists) end end end - return false; end function isAnotherSessionUsingDefaultList(origin) - local ret = false - if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then - for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do + local user = bare_sessions[origin.username.."@"..origin.host]; + if user then + for resource, session in pairs(user.sessions) do if resource ~= origin.resource and session.activePrivacyList == nil then - ret = true; - break; + return true; end end end - return ret; end -function sendUnavailable(to, from) +function sendUnavailable(origin, to, from) --[[ example unavailable presence stanza <presence from="node@host/resource" type="unavailable" to="node@host" > <status>Logged out</status> </presence> ]]-- - local presence = st.presence({from=from, type="unavailable"}) + local presence = st.presence({from=from, type="unavailable"}); presence:tag("status"):text("Logged out"); local node, host = jid_bare(to); local bare = node .. "@" .. host; - if bare_sessions[bare].sessions ~= nil then - for resource, session in pairs(bare_sessions[bare].sessions) do + local user = bare_sessions[bare]; + if user then + for resource, session in pairs(user.sessions) do presence.attr.to = session.full_jid; module:log("debug", "send unavailable to: %s; from: %s", tostring(presence.attr.to), tostring(presence.attr.from)); origin.send(presence); @@ -99,14 +93,14 @@ function sendNeededUnavailablePersences(origin, listnameOrItem) -- TODO implemen if item["presence-out"] == true then if item.type == "jid" then - sendUnavailable(item.value, origin.full_jid); + sendUnavailable(origin, item.value, origin.full_jid); elseif item.type == "group" then elseif item.type == "subscription" then elseif item.type == nil then end elseif item["presence-in"] == true then if item.type == "jid" then - sendUnavailable(origin.full_jid, item.value); + sendUnavailable(origin, origin.full_jid, item.value); elseif item.type == "group" then elseif item.type == "subscription" then elseif item.type == nil then @@ -117,7 +111,7 @@ function sendNeededUnavailablePersences(origin, listnameOrItem) -- TODO implemen end end -function declineList (privacy_lists, origin, stanza, which) +function declineList(privacy_lists, origin, stanza, which) if which == "default" then if isAnotherSessionUsingDefaultList(origin) then return { "cancel", "conflict", "Another session is online and using the default list."}; @@ -133,7 +127,7 @@ function declineList (privacy_lists, origin, stanza, which) return true; end -function activateList (privacy_lists, origin, stanza, which, name) +function activateList(privacy_lists, origin, stanza, which, name) local idx = findNamedList(privacy_lists, name); if privacy_lists.default == nil then @@ -164,7 +158,7 @@ function activateList (privacy_lists, origin, stanza, which, name) return true; end -function deleteList (privacy_lists, origin, stanza, name) +function deleteList(privacy_lists, origin, stanza, name) local idx = findNamedList(privacy_lists, name); if idx ~= nil then @@ -184,13 +178,6 @@ function deleteList (privacy_lists, origin, stanza, name) return {"modify", "bad-request", "Not existing list specifed to be deleted."}; end -local function sortByOrder(a, b) - if a.order < b.order then - return true; - end - return false; -end - function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster) local idx = findNamedList(privacy_lists, name); local bare_jid = origin.username.."@"..origin.host; @@ -277,12 +264,12 @@ function createOrReplaceList (privacy_lists, origin, stanza, name, entries, rost list.items[#list.items + 1] = tmp; end - table.sort(list, sortByOrder); + table.sort(list, function(a, b) return a.order < b.order; end); privacy_lists.lists[idx] = list; origin.send(st.reply(stanza)); if bare_sessions[bare_jid] ~= nil then - iq = st.iq ( { type = "set", id="push1" } ); + local iq = st.iq ( { type = "set", id="push1" } ); iq:tag ("query", { xmlns = "jabber:iq:privacy" } ); iq:tag ("list", { name = list.name } ):up(); iq:up(); @@ -311,7 +298,7 @@ function getList(privacy_lists, origin, stanza, name) else local idx = findNamedList(privacy_lists, name); if idx ~= nil then - list = privacy_lists.lists[idx]; + local list = privacy_lists.lists[idx]; reply = reply:tag("list", {name=list.name}); for _,item in ipairs(list.items) do reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order}); @@ -385,8 +372,7 @@ module:hook("iq/bare/jabber:iq:privacy:query", function(data) end return true; end - return false; -end, 500); +end); function checkIfNeedToBeBlocked(e, session) local origin, stanza = e.origin, e.stanza; @@ -397,15 +383,15 @@ function checkIfNeedToBeBlocked(e, session) if stanza.attr.to ~= nil and stanza.attr.from ~= nil then if privacy_lists.lists == nil or - (session.activePrivacyList == nil or session.activePrivacyList == "") and - (privacy_lists.default == nil or privacy_lists.default == "") + (session.activePrivacyList == nil or session.activePrivacyList == "") and + (privacy_lists.default == nil or privacy_lists.default == "") then return; -- Nothing to block, default is Allow all end - if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then - module:log("debug", "Never block communications from one of a user's resources to another."); - return; -- from one of a user's resource to another => HANDS OFF! - end + if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then + module:log("debug", "Never block communications from one of a user's resources to another."); + return; -- from one of a user's resource to another => HANDS OFF! + end local idx; local list; @@ -490,7 +476,6 @@ function checkIfNeedToBeBlocked(e, session) end end end - return; end function preCheckIncoming(e) @@ -517,10 +502,9 @@ function preCheckIncoming(e) if session ~= nil then return checkIfNeedToBeBlocked(e, session); else - module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource)) + module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource)); end end - return; end function preCheckOutgoing(e) @@ -534,7 +518,6 @@ function preCheckOutgoing(e) return checkIfNeedToBeBlocked(e, session); end - module:hook("pre-message/full", preCheckOutgoing, 500); module:hook("pre-message/bare", preCheckOutgoing, 500); module:hook("pre-message/host", preCheckOutgoing, 500); @@ -554,5 +537,3 @@ module:hook("iq/host", preCheckIncoming, 500); module:hook("presence/full", preCheckIncoming, 500); module:hook("presence/bare", preCheckIncoming, 500); module:hook("presence/host", preCheckIncoming, 500); - -module:log("info", "mod_privacy loaded ..."); |