Changeset

2501:f521e89539da

Merge with trunk.
author Waqas Hussain <waqas20@gmail.com>
date Tue, 26 Jan 2010 02:10:25 +0500
parents 2500:bffdaeb7ab5e (diff) 2497:810fb77b6fe6 (current diff)
children 2502:ec3eaf54bbd4
files
diffstat 1 files changed, 37 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_privacy.lua	Mon Jan 25 20:58:15 2010 +0000
+++ b/plugins/mod_privacy.lua	Tue Jan 26 02:10:25 2010 +0500
@@ -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 @@
 			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 @@
 
 		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 @@
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 		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 @@
 	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 @@
 		end
 		return true;
 	end
-	return false;
-end, 500);
+end);
 
 function checkIfNeedToBeBlocked(e, session)
 	local origin, stanza = e.origin, e.stanza;
@@ -397,15 +383,15 @@
 	
 	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 @@
 			end
 		end
 	end
-	return;
 end
 
 function preCheckIncoming(e)
@@ -517,10 +502,9 @@
 		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 @@
 	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("presence/full", preCheckIncoming, 500);
 module:hook("presence/bare", preCheckIncoming, 500);
 module:hook("presence/host", preCheckIncoming, 500);
-
-module:log("info", "mod_privacy loaded ...");