Changeset

191:e64c8a44060f

Fix s2s once and for all - Moved dialback to the new mod_dialback (mostly). - Modules can now supply a list of origins to handle to add_handler - Modules can now handle and process any stanza, overriding the core - Modules handle non-jabber:client/jabber:server xmlns'd stanzas
author Matthew Wild <mwild1@gmail.com>
date Sat, 01 Nov 2008 21:07:14 +0000
parents 190:1e993b7deae7
children 192:71c389c6fc2e
files core/modulemanager.lua core/s2smanager.lua core/stanza_router.lua plugins/mod_dialback.lua
diffstat 4 files changed, 89 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/core/modulemanager.lua	Sat Nov 01 18:28:46 2008 +0000
+++ b/core/modulemanager.lua	Sat Nov 01 21:07:14 2008 +0000
@@ -31,19 +31,29 @@
 	end
 end
 
-function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
-	if not (origin_type and tag and xmlns and handler) then return false; end
+local function _add_handler(module, origin_type, tag, xmlns, handler)
 	handlers[origin_type] = handlers[origin_type] or {};
 	if not handlers[origin_type][tag] then
 		handlers[origin_type][tag] = handlers[origin_type][tag] or {};
 		handlers[origin_type][tag][xmlns]= handler;
-		handler_info[handler] = getfenv(2).module;
-		log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
+		handler_info[handler] = module;
+		log("debug", "mod_%s now handles tag '%s'", module.name, tag);
 	elseif handler_info[handlers[origin_type][tag]] then
-		log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", getfenv(2).module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
+		log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
 	end
 end
 
+function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
+	if not (origin_type and tag and xmlns and handler) then return false; end
+	if type(origin_type) == "table" then
+		for _, origin_type in ipairs(origin_type) do
+			_add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
+		end
+		return;
+	end
+	_add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
+end
+
 function loadall()
 	load("saslauth");
 	load("legacyauth");
@@ -53,6 +63,7 @@
 	load("vcard");
 	load("private");
 	load("version");
+	load("dialback");
 end
 
 function load(name)
--- a/core/s2smanager.lua	Sat Nov 01 18:28:46 2008 +0000
+++ b/core/s2smanager.lua	Sat Nov 01 21:07:14 2008 +0000
@@ -188,7 +188,6 @@
 	
 	
 	if session.direction == "outgoing" then
-		hosts[to] = session;
 		if sendq then
 			session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host);
 			for i, data in ipairs(sendq) do
--- a/core/stanza_router.lua	Sat Nov 01 18:28:46 2008 +0000
+++ b/core/stanza_router.lua	Sat Nov 01 21:07:14 2008 +0000
@@ -16,6 +16,9 @@
 
 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
+
+local modules_handle_stanza = require "core.modulemanager".handle_stanza;
+
 local format = string.format;
 local tostring = tostring;
 
@@ -57,6 +60,8 @@
 		core_handle_stanza(origin, stanza);
 	elseif stanza.name == "iq" and not select(3, jid_split(to)) then
 		core_handle_stanza(origin, stanza);
+	elseif stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
+		modules_handle_stanza(origin, stanza);
 	elseif origin.type == "c2s" or origin.type == "s2sin" then
 		core_route_stanza(origin, stanza);
 	end
@@ -66,6 +71,7 @@
 -- that is, they are handled by this server
 function core_handle_stanza(origin, stanza)
 	-- Handlers
+	if modules_handle_stanza(origin, stanza) then return; end
 	if origin.type == "c2s" or origin.type == "c2s_unauthed" then
 		local session = origin;
 		
@@ -108,62 +114,6 @@
 			else
 				-- TODO error, bad type
 			end
-		else
-			log("debug", "Routing stanza to local");
-			handle_stanza(session, stanza);
-		end
-	elseif origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
-		if stanza.attr.xmlns == "jabber:server:dialback" then
-			if stanza.name == "verify" then
-				-- We are being asked to verify the key, to ensure it was generated by us
-				log("debug", "verifying dialback key...");
-				local attr = stanza.attr;
-				print(tostring(attr.to), tostring(attr.from))
-				print(tostring(origin.to_host), tostring(origin.from_host))
-				-- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
-				--if attr.from ~= origin.to_host then error("invalid-from"); end
-				local type;
-				if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
-					type = "valid"
-				else
-					type = "invalid"
-					log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
-				end
-				origin.sends2s(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));
-			elseif stanza.name == "result" and origin.type == "s2sin_unauthed" then
-				-- he wants to be identified through dialback
-				-- We need to check the key with the Authoritative server
-				local attr = stanza.attr;
-				origin.from_host = attr.from;
-				origin.to_host = attr.to;
-				origin.dialback_key = stanza[1];
-				log("debug", "asking %s if key %s belongs to them", origin.from_host, origin.dialback_key);
-				send_s2s(origin.to_host, origin.from_host, format("<db:verify from='%s' to='%s' id='%s'>%s</db:verify>", origin.to_host, origin.from_host, origin.streamid, origin.dialback_key));
-				hosts[origin.from_host].dialback_verifying = origin;
-			end
-		end
-	elseif origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
-		if stanza.attr.xmlns == "jabber:server:dialback" then
-			if stanza.name == "result" then
-				if stanza.attr.type == "valid" then
-					s2s_make_authenticated(origin);
-				else
-					-- FIXME
-					error("dialback failed!");
-				end
-			elseif stanza.name == "verify" and origin.dialback_verifying then
-				local valid;
-				local attr = stanza.attr;
-				if attr.type == "valid" then
-					s2s_make_authenticated(origin.dialback_verifying);
-					valid = "valid";
-				else
-					-- Warn the original connection that is was not verified successfully
-					log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed");
-					valid = "invalid";
-				end
-				origin.dialback_verifying.sends2s(format("<db:result from='%s' to='%s' id='%s' type='%s'>%s</db:result>", attr.from, attr.to, attr.id, valid, origin.dialback_verifying.dialback_key));
-			end
 		end
 	else
 		log("warn", "Unhandled origin: %s", origin.type);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_dialback.lua	Sat Nov 01 21:07:14 2008 +0000
@@ -0,0 +1,67 @@
+
+local format = string.format;
+local send_s2s = require "core.s2smanager".send_to_host;
+local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
+local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
+
+local log = require "util.logger".init("mod_dialback");
+
+local xmlns_dialback = "jabber:server:dialback";
+
+add_handler({"s2sin_unauthed", "s2sin"}, "verify", xmlns_dialback,
+		function (origin, stanza)
+			-- We are being asked to verify the key, to ensure it was generated by us
+			log("debug", "verifying dialback key...");
+			local attr = stanza.attr;
+			-- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
+			--if attr.from ~= origin.to_host then error("invalid-from"); end
+			local type;
+			if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
+				type = "valid"
+			else
+				type = "invalid"
+				log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
+			end
+			origin.sends2s(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));		
+		end);
+
+add_handler("s2sin_unauthed", "result", xmlns_dialback,
+		function (origin, stanza)
+			-- he wants to be identified through dialback
+			-- We need to check the key with the Authoritative server
+			local attr = stanza.attr;
+			local attr = stanza.attr;
+			origin.from_host = attr.from;
+			origin.to_host = attr.to;
+			origin.dialback_key = stanza[1];
+			log("debug", "asking %s if key %s belongs to them", origin.from_host, origin.dialback_key);
+			send_s2s(origin.to_host, origin.from_host, format("<db:verify from='%s' to='%s' id='%s'>%s</db:verify>", origin.to_host, origin.from_host, origin.streamid, origin.dialback_key));
+			hosts[origin.from_host].dialback_verifying = origin;
+		end);
+
+add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback,
+		function (origin, stanza)
+			if origin.dialback_verifying then
+				local valid;
+				local attr = stanza.attr;
+				if attr.type == "valid" then
+					s2s_make_authenticated(origin.dialback_verifying);
+					valid = "valid";
+				else
+					-- Warn the original connection that is was not verified successfully
+					log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed");
+					valid = "invalid";
+				end
+				origin.dialback_verifying.sends2s(format("<db:result from='%s' to='%s' id='%s' type='%s'>%s</db:result>", attr.from, attr.to, attr.id, valid, origin.dialback_verifying.dialback_key));
+			end
+		end);
+
+add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback,
+		function (origin, stanza)
+			if stanza.attr.type == "valid" then
+				s2s_make_authenticated(origin);
+			else
+				-- FIXME
+				error("dialback failed!");
+			end
+		end);
\ No newline at end of file