Changeset

201:bb1b3201bab5

merge from waqas
author Matthew Wild <mwild1@gmail.com>
date Sun, 02 Nov 2008 02:02:41 +0000
parents 198:e4755408d40b (current diff) 200:5e8b3cce798f (diff)
children 202:6178f6ba47c8
files
diffstat 3 files changed, 57 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/core/s2smanager.lua	Sun Nov 02 01:30:36 2008 +0000
+++ b/core/s2smanager.lua	Sun Nov 02 02:02:41 2008 +0000
@@ -57,7 +57,7 @@
 local open_sessions = 0;
 
 function new_incoming(conn)
-	local session = { conn = conn,  priority = 0, type = "s2sin_unauthed", direction = "incoming" };
+	local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming" };
 	if true then
 		session.trace = newproxy(true);
 		getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("s2s session got collected, now "..open_sessions.." s2s sessions are allocated") end;
--- a/core/stanza_router.lua	Sun Nov 02 01:30:36 2008 +0000
+++ b/core/stanza_router.lua	Sun Nov 02 02:02:41 2008 +0000
@@ -21,6 +21,10 @@
 
 local format = string.format;
 local tostring = tostring;
+local t_concat = table.concat;
+local t_insert = table.insert;
+local tonumber = tonumber;
+local s_find = string.find;
 
 local jid_split = require "util.jid".split;
 local print = print;
@@ -91,7 +95,7 @@
 						core_route_stanza(origin, stanza);
 					end
 				end
-				if not origin.presence then -- presence probes on initial presence -- FIXME does unavailable qualify as initial presence?
+				if stanza.attr.type == nil and not origin.presence then -- initial presence
 					local probe = st.presence({from = origin.full_jid, type = "probe"});
 					for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
 						local subscription = origin.roster[jid].subscription;
@@ -100,16 +104,42 @@
 							core_route_stanza(origin, probe);
 						end
 					end
-					for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all resources
-						if res ~= origin and stanza.attr.type ~= "unavailable" and res.presence then -- FIXME does unavailable qualify as initial presence?
+					for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
+						if res ~= origin and res.presence then
 							res.presence.attr.to = origin.full_jid;
 							core_route_stanza(res, res.presence);
 							res.presence.attr.to = nil;
 						end
 					end
-					-- TODO resend subscription requests
+					if origin.roster.pending then -- resend incoming subscription requests
+						for jid in pairs(origin.roster.pending) do
+							origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
+						end
+					end
+					local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
+					for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
+						if item.ask then
+							request.attr.to = jid;
+							core_route_stanza(origin, request);
+						end
+					end
 				end
-				origin.presence = stanza;
+				origin.priority = 0;
+				if stanza.attr.type == "unavailable" then
+					origin.presence = nil;
+				else
+					origin.presence = stanza;
+					local priority = stanza:child_with_name("priority");
+					if priority and #priority > 0 then
+						priority = t_concat(priority);
+						if s_find(priority, "^[+-]?[0-9]+$") then
+							priority = tonumber(priority);
+							if priority < -128 then priority = -128 end
+							if priority > 127 then priority = 127 end
+							origin.priority = priority;
+						end
+					end
+				end
 				stanza.attr.to = nil; -- reset it
 			else
 				-- TODO error, bad type
@@ -189,6 +219,7 @@
 	local st_from, st_to = stanza.attr.from, stanza.attr.to;
 	stanza.attr.from, stanza.attr.to = from_bare, to_bare;
 	if stanza.attr.type == "probe" then
+		log("debug", "inbound probe from "..from_bare.." for "..to_bare);
 		if rostermanager.is_contact_subscribed(node, host, from_bare) then
 			if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
 				-- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
@@ -252,22 +283,28 @@
 					if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
 						handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
 					else -- sender is available or unavailable
-						for k in pairs(user.sessions) do -- presence broadcast to all user resources. FIXME should this be just for available resources? Do we need to check subscription?
-							if user.sessions[k].full_jid then
-								stanza.attr.to = user.sessions[k].full_jid; -- reset at the end of function
-								user.sessions[k].send(stanza);
+						for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
+							if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
+								stanza.attr.to = session.full_jid; -- reset at the end of function
+								session.send(stanza);
 							end
 						end
 					end
 				elseif stanza.name == "message" then -- select a resource to recieve message
-					for k in pairs(user.sessions) do
-						if user.sessions[k].full_jid then
-							res = user.sessions[k];
-							break;
+					local priority = 0;
+					local recipients = {};
+					for _, session in pairs(user.sessions) do -- find resource with greatest priority
+						local p = session.priority;
+						if p > priority then
+							priority = p;
+							recipients = {session};
+						elseif p == priority then
+							t_insert(recipients, session);
 						end
 					end
-					-- TODO find resource with greatest priority
-					res.send(stanza);
+					for _, session in pairs(recipient) do
+						session.send(stanza);
+					end
 				else
 					-- TODO send IQ error
 				end
--- a/doc/session.txt	Sun Nov 02 01:30:36 2008 +0000
+++ b/doc/session.txt	Sun Nov 02 02:02:41 2008 +0000
@@ -13,8 +13,8 @@
 	host -- the host part of the client's jid (not defined before stream initiation)
 	resource -- the resource part of the client's full jid (not defined before resource binding)
 	full_jid -- convenience for the above 3 as string in username@host/resource form (not defined before resource binding)
-	priority -- the resource priority, default: 0 (not defined before initial presence)
-	presence -- the last non-directed presence. initially nil.
+	priority -- the resource priority, default: 0
+	presence -- the last non-directed presence with no type attribute. initially nil. reset to nil on unavailable presence.
 	interested -- true if the resource requested the roster. Interested resources recieve roster updates. Initially nil.
 	roster -- the user's roster. Loaded as soon as the resource is bound (session becomes a connected resource).
 	
@@ -24,5 +24,5 @@
 }
 
 if session.full_jid (also session.roster and session.resource) then this is a "connected resource"
-if session.presence then this is an "available resource"
-if session.interested then this is an "interested resource"
+if session.presence then this is an "available resource" (all available resources are connected resources)
+if session.interested then this is an "interested resource" (all interested resources are connected resources)