Diff

core/stanza_dispatch.lua @ 1:b8787e859fd2

Switched to new connection framework, courtesy of the luadch project Now supports SSL on 5223 Beginning support for presence (aka. the proper routing of stanzas)
author matthew
date Sun, 24 Aug 2008 01:51:02 +0000
parent 0:3e3171b59028
child 2:9bb397205f26
line wrap: on
line diff
--- a/core/stanza_dispatch.lua	Fri Aug 22 21:09:04 2008 +0000
+++ b/core/stanza_dispatch.lua	Sun Aug 24 01:51:02 2008 +0000
@@ -12,14 +12,17 @@
 	local session_log = session.log;
 	local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end
 	local send = session.send;
-
-
+	local send_to;
+	do
+		local _send_to = session.send_to;
+		send_to = function (...) _send_to(session, ...); end
+	end
 
 	iq_handlers["jabber:iq:auth"] = 
 		function (stanza)
-			local username = stanza[1]:child_with_name("username");
-			local password = stanza[1]:child_with_name("password");
-			local resource = stanza[1]:child_with_name("resource");
+			local username = stanza.tags[1]:child_with_name("username");
+			local password = stanza.tags[1]:child_with_name("password");
+			local resource = stanza.tags[1]:child_with_name("resource");
 			if not (username and password and resource) then
 				local reply = st.reply(stanza);
 				send(reply:query("jabber:iq:auth")
@@ -78,24 +81,52 @@
 
 	return	function (stanza)
 			log("info", "--> "..tostring(stanza));
-			if stanza.name == "iq" then
-				if not stanza[1] then log("warn", "<iq> without child is invalid"); return; end
-				if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
-				local xmlns = stanza[1].attr.xmlns;
-				if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
-				if (((not stanza.attr.to) or stanza.attr.to == session.host or stanza.attr.to:match("@[^/]+$")) and (stanza.attr.type == "get" or stanza.attr.type == "set")) then -- Stanza sent to us
-					if iq_handlers[xmlns] then
-						if iq_handlers[xmlns](stanza) then return; end;
+			if (not stanza.attr.to) or (hosts[stanza.attr.to] and hosts[stanza.attr.to].type == "local") then
+				if stanza.name == "iq" then
+					if not stanza.tags[1] then log("warn", "<iq> without child is invalid"); return; end
+					if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
+					local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns) or nil;
+					if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
+					if (((not stanza.attr.to) or stanza.attr.to == session.host or stanza.attr.to:match("@[^/]+$")) and (stanza.attr.type == "get" or stanza.attr.type == "set")) then -- Stanza sent to us
+						if iq_handlers[xmlns] then
+							if iq_handlers[xmlns](stanza) then return; end;
+						end
+						log("warn", "Unhandled namespace: "..xmlns);
+						send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
+						return;
 					end
-					log("warn", "Unhandled namespace: "..xmlns);
-					send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
+				elseif stanza.name == "presence" then
+					if session.roster then
+						-- Broadcast presence and probes
+						local broadcast = st.presence({ from = session.username.."@"..session.host.."/"..session.resource });
+						local probe = st.presence { from = broadcast.attr.from, type = "probe" };
+
+						for child in stanza:children() do
+							broadcast:tag(child.name, child.attr);
+						end
+						for contact in pairs(session.roster) do
+							broadcast.attr.to = contact;
+							send_to(contact, broadcast);
+							--local host = jid.host(contact);
+							--if hosts[host] and hosts[host].type == "local" then
+								--local node, host = jid.split(contact);
+								--if host[host].sessions[node]
+								--local pres = st.presence { from = con
+							--else
+							--	probe.attr.to = contact;
+							--	send_to(contact, probe);
+							--end
+						end
+						
+						-- Probe for our contacts' presence
+					end
 				end
-			
-			end
-							-- Need to route stanza
-			if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
+			else
+			--end				
+			--if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
+				-- Need to route stanza
 				stanza.attr.from = session.username.."@"..session.host;
-				session.send_to(stanza.attr.to, stanza);
+				session:send_to(stanza.attr.to, stanza);
 			end
 		end