core/stanza_router.lua
changeset 199 eccf66b42bd7
parent 192 71c389c6fc2e
child 200 5e8b3cce798f
--- a/core/stanza_router.lua	Sun Nov 02 02:23:52 2008 +0500
+++ b/core/stanza_router.lua	Sun Nov 02 06:36:42 2008 +0500
@@ -21,6 +21,9 @@
 
 local format = string.format;
 local tostring = tostring;
+local t_concat = table.concat;
+local tonumber = tonumber;
+local s_find = string.find;
 
 local jid_split = require "util.jid".split;
 local print = print;
@@ -91,7 +94,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 +103,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