Changeset

627:f0a4498ae996

Merge from waqas
author Matthew Wild <mwild1@gmail.com>
date Tue, 16 Dec 2008 02:40:29 +0000
parents 625:cad4dcfbf295 (diff) 626:cf1d26fd4d6f (current diff)
children 629:6f9052edb18d
files util/stanza.lua
diffstat 58 files changed, 373 insertions(+), 160 deletions(-) [+]
line wrap: on
line diff
--- a/core/componentmanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/componentmanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/configmanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/configmanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/discomanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/discomanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/modulemanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/modulemanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/offlinemanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/offlinemanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/offlinemessage.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/offlinemessage.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/presencemanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/presencemanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/rostermanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/rostermanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/s2smanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/s2smanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -47,6 +47,9 @@
 
 local dns = require "net.dns";
 
+incoming_s2s = {};
+local incoming_s2s = incoming_s2s;
+
 module "s2smanager"
 
 local function compare_srv_priorities(a,b) return a.priority < b.priority or a.weight < b.weight; end
@@ -91,7 +94,7 @@
 local open_sessions = 0;
 
 function new_incoming(conn)
-	local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming" };
+	local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
 	if true then
 		session.trace = newproxy(true);
 		getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
@@ -99,6 +102,7 @@
 	open_sessions = open_sessions + 1;
 	local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
 	session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end
+	incoming_s2s[session] = true;
 	return session;
 end
 
@@ -239,11 +243,16 @@
 	return key == generate_dialback(id, to, from);
 end
 
-function make_authenticated(session)
+function make_authenticated(session, host)
 	if session.type == "s2sout_unauthed" then
 		session.type = "s2sout";
 	elseif session.type == "s2sin_unauthed" then
 		session.type = "s2sin";
+		if host then
+			session.hosts[host].authed = true;
+		end
+	elseif session.type == "s2sin" and host then
+		session.hosts[host].authed = true;
 	else
 		return false;
 	end
@@ -284,6 +293,8 @@
 	
 	if session.direction == "outgoing" then
 		hosts[session.from_host].s2sout[session.to_host] = nil;
+	elseif session.direction == "incoming" then
+		incoming_s2s[session] = nil;
 	end
 	
 	for k in pairs(session) do
--- a/core/sessionmanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/sessionmanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -113,8 +113,6 @@
 	if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
 	-- We don't support binding multiple resources
 
-	session.conntimetotal = gettime()-session.conntime;
-	
 	resource = resource or uuid_generate();
 	--FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
 	
@@ -209,4 +207,4 @@
 	return count;
 end
 
-return _M;
\ No newline at end of file
+return _M;
--- a/core/stanza_router.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/stanza_router.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -72,26 +72,27 @@
 	if origin.type == "c2s" then
 		stanza.attr.from = origin.full_jid;
 	end
-	local to = stanza.attr.to;
+	local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
 	local node, host, resource = jid_split(to);
 	local to_bare = node and (node.."@"..host) or host; -- bare JID
 	local from = stanza.attr.from;
 	local from_node, from_host, from_resource = jid_split(from);
 	local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
 
-	if origin.type == "s2sin" then
-		if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
-			log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
-			return; -- FIXME what should we do here? does this work with subdomains?
-		end
-	end
 	--[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
 		log("warn", "stanza recieved for a non-local server");
 		return; -- FIXME what should we do here?
 	end]] -- FIXME
 
 	-- FIXME do stanzas not of jabber:client get handled by components?
-	if origin.type == "s2sin" or origin.type == "c2s" then
+	if (origin.type == "s2sin" or origin.type == "c2s") and (not xmlns or xmlns == "jabber:server" or xmlns == "jabber:client") then			
+		if origin.type == "s2sin" then
+			local host_status = origin.hosts[from_host];
+			if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
+				log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from_host, origin.from_host);
+				return; -- FIXME what should we do here? does this work with subdomains?
+			end
+		end
 		if not to then
 			core_handle_stanza(origin, stanza);
 		elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
--- a/core/usermanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/usermanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/core/xmlhandlers.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/core/xmlhandlers.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -47,29 +47,27 @@
 
 function init_xmlhandlers(session, stream_callbacks)
 		local ns_stack = { "" };
-		local curr_ns = "";
+		local curr_ns, name = "";
 		local curr_tag;
 		local chardata = {};
 		local xml_handlers = {};
 		local log = session.log or default_log;
-
-		local send = session.send;
 		
 		local cb_streamopened = stream_callbacks.streamopened;
 		local cb_streamclosed = stream_callbacks.streamclosed;
 		local cb_error = stream_callbacks.error or function (session, e) error("XML stream error: "..tostring(e)); end;
 		local cb_handlestanza = stream_callbacks.handlestanza;
 		
-		local stream_ns = stream_callbacks.ns;
+		local stream_tag = stream_callbacks.stream_tag;
 		
 		local stanza
-		function xml_handlers:StartElement(name, attr)
+		function xml_handlers:StartElement(tagname, attr)
 			if stanza and #chardata > 0 then
 				-- We have some character data in the buffer
 				stanza:text(t_concat(chardata));
 				chardata = {};
 			end
-			curr_ns,name = name:match("^(.+)|([%w%-]+)$");
+			local curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
 			if curr_ns ~= "jabber:server" then
 				attr.xmlns = curr_ns;
 			end
@@ -91,7 +89,7 @@
 			
 			if not stanza then --if we are not currently inside a stanza
 				if session.notopen then
-					if name == "stream" and curr_ns == stream_ns then
+					if tagname == stream_tag then
 						if cb_streamopened then
 							cb_streamopened(session, attr);
 						end
@@ -120,10 +118,10 @@
 				t_insert(chardata, data);
 			end
 		end
-		function xml_handlers:EndElement(name)
-			curr_ns,name = name:match("^(.+)|([%w%-]+)$");
+		function xml_handlers:EndElement(tagname)
+			curr_ns,name = tagname:match("^(.+)|([%w%-]+)$");
 			if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
-				if name == "stream" then
+				if tagname == stream_tag then
 					if cb_streamclosed then
 						cb_streamclosed(session);
 					end
--- a/net/connlisteners.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/net/connlisteners.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -47,7 +47,8 @@
 function get(name)
 	local h = listeners[name];
 	if not h then
-		pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua");
+		local ok, ret = pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua");
+		if not ok then return nil, ret; end
 		h = listeners[name];
 	end
 	return h;
--- a/net/dns.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/net/dns.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,24 +1,6 @@
--- Prosody IM v0.1
--- Copyright (C) 2008 Matthew Wild
--- Copyright (C) 2008 Waqas Hussain
--- 
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public License
--- as published by the Free Software Foundation; either version 2
--- of the License, or (at your option) any later version.
--- 
--- This program is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--- GNU General Public License for more details.
--- 
--- You should have received a copy of the GNU General Public License
--- along with this program; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
---
-
-
-
+-- Prosody IM v0.2
+-- This file is included with Prosody IM. It has modifications,
+-- which are hereby placed in the public domain.
 
 -- public domain 20080404 lua@ztact.com
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/http.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -0,0 +1,172 @@
+
+local socket = require "socket"
+local mime = require "mime"
+local url = require "socket.url"
+
+local server = require "net.server"
+
+local connlisteners_get = require "net.connlisteners".get;
+local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
+
+local t_insert, t_concat = table.insert, table.concat;
+local tonumber, tostring, pairs = tonumber, tostring, pairs;
+local print = function () end
+
+local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
+local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
+
+module "http"
+
+local function expectbody(reqt, code)
+    if reqt.method == "HEAD" then return nil end
+    if code == 204 or code == 304 then return nil end
+    if code >= 100 and code < 200 then return nil end
+    return 1
+end
+
+local function request_reader(request, data, startpos)
+	if not data then
+		if request.body then
+			request.callback(request.code, t_concat(request.body), request);
+		else
+			-- Error.. connection was closed prematurely
+			request.callback(0, "connection-closed", request);
+		end
+		destroy_request(request);
+		return;
+	end
+	if request.state == "body" then
+		print("Reading body...")
+		if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end
+		if startpos then
+			data = data:sub(startpos, -1)
+		end
+		t_insert(request.body, data);
+		if request.bodylength then
+			request.havebodylength = request.havebodylength + #data;
+			if request.havebodylength >= request.bodylength then
+				-- We have the body
+				if request.callback then
+					request.callback(request.code, t_concat(request.body), request);
+				end
+			end
+			print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
+		end
+	elseif request.state == "headers" then
+		print("Reading headers...")
+		local pos = startpos;
+		local headers = request.responseheaders or {};
+		for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do
+			startpos = startpos + #line + 2;
+			local k, v = line:match("(%S+): (.+)");
+			if k and v then
+				headers[k:lower()] = v;
+				print("Header: "..k:lower().." = "..v);
+			elseif #line == 0 then
+				request.responseheaders = headers;
+				break;
+			else
+				print("Unhandled header line: "..line);
+			end
+		end
+		-- Reached the end of the headers
+		request.state = "body";
+		if #data > startpos then
+			return request_reader(request, data, startpos);
+		end
+	elseif request.state == "status" then
+		print("Reading status...")
+		local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos);
+		if not code then
+			return request.callback(0, "invalid-status-line", request);
+		end
+		
+		request.responsecode, request.responseversion = code, http;
+		
+		if request.onlystatus or not expectbody(request, tonumber(code)) then
+			if request.callback then
+				request.callback(code, nil, request);
+			end
+			destroy_request(request);
+			return;
+		end
+		
+		request.state = "headers";
+		
+		if #data > linelen then
+			return request_reader(request, data, linelen);
+		end
+	end
+end
+
+function request(u, callback, ex)
+	local req = url.parse(u);
+	
+	local custom_headers, body;
+	local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
+	
+	
+	if req.userinfo then
+		default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
+	end
+	
+	if ex then
+		custom_headers = ex.custom_headers;
+		req.onlystatus = ex.onlystatus;
+		body = ex.body;
+		if body then
+			req.method = "POST ";
+			default_headers["Content-Length"] = tostring(#body);
+			default_headers["Content-Type"] = "application/x-www-form-urlencoded";
+		end
+		if ex.method then req.method = ex.method; end
+	end
+	
+	req.handler, req.conn = server.wraptcpclient(listener, socket.tcp(), req.host, req.port or 80, 0, "*a");
+	req.write = req.handler.write;
+	req.conn:settimeout(0);
+	local ok, err = req.conn:connect(req.host, req.port or 80);
+	if not ok and err ~= "timeout" then
+		return nil, err;
+	end
+	
+	req.write((req.method or "GET ")..req.path.." HTTP/1.0\r\n");
+	local t = { [2] = ": ", [4] = "\r\n" };
+	if custom_headers then
+		for k, v in pairs(custom_headers) do
+			t[1], t[3] = k, v;
+			req.write(t_concat(t));
+			default_headers[k] = nil;
+		end
+	end
+	
+	for k, v in pairs(default_headers) do
+		t[1], t[3] = k, v;
+		req.write(t_concat(t));
+		default_headers[k] = nil;
+	end
+	req.write("\r\n");
+	
+	if body then
+		req.write(body);
+	end
+	
+	req.callback = callback;
+	req.reader = request_reader;
+	req.state = "status"
+	
+	listener.register_request(req.handler, req);
+
+	return req;
+end
+
+function destroy_request(request)
+	if request.conn then
+		request.handler.close()
+		listener.disconnect(request.conn, "closed");
+	end
+end
+
+_M.urlencode = urlencode;
+
+return _M;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/httpclient_listener.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -0,0 +1,36 @@
+
+local connlisteners_register = require "net.connlisteners".register;
+
+
+local requests = {}; -- Open requests
+local buffers = {}; -- Buffers of partial lines
+
+local httpclient = { default_port = 80, default_mode = "*a" };
+
+function httpclient.listener(conn, data)
+	local request = requests[conn];
+
+	if not request then
+		print("NO REQUEST!! for "..tostring(conn));
+		return;
+	end
+
+	if data and request.reader then
+		request:reader(data);
+	end
+end
+
+function httpclient.disconnect(conn, err)
+	local request = requests[conn];
+	if request then
+		request:reader(nil);
+	end
+	--requests[conn] = nil;
+end
+
+function httpclient.register_request(conn, req)
+	print("Registering a request for "..tostring(conn));
+	requests[conn] = req;
+end
+
+connlisteners_register("httpclient", httpclient);
--- a/net/server.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/net/server.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -587,6 +587,8 @@
 
 	local eol, fatal_send_error
 
+	socket:settimeout(0);
+	
 	local rstat, sstat = 0, 0
 
 	--// local import of socket methods //--
@@ -833,5 +835,6 @@
 	stats = stats,
 	closeall = closeall,
 	addtimer = addtimer,
+	wraptcpclient = wraptcpclient,
 	wraptlsclient = wraptlsclient,
 }
--- a/net/xmppclient_listener.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/net/xmppclient_listener.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -36,7 +36,7 @@
 local sm_streamclosed = sessionmanager.streamclosed;
 local st = stanza;
 
-local stream_callbacks = { ns = "http://etherx.jabber.org/streams", streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
+local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
 
 function stream_callbacks.error(session, error, data)
 	if error == "no-stream" then
@@ -47,7 +47,7 @@
 	end
 end
 
-local function handleerr(err) log("error", "Traceback[c2s]:", err, debug.traceback()); end
+local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), debug.traceback()); end
 function stream_callbacks.handlestanza(a, b)
 	xpcall(function () core_process_stanza(a, b) end, handleerr);
 end
--- a/net/xmppserver_listener.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/net/xmppserver_listener.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -28,7 +28,7 @@
 local s2s_streamclosed = require "core.s2smanager".streamclosed;
 local s2s_destroy_session = require "core.s2smanager".destroy_session;
 local s2s_attempt_connect = require "core.s2smanager".attempt_connection;
-local stream_callbacks = { ns = "http://etherx.jabber.org/streams", streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza =  core_process_stanza };
+local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza =  core_process_stanza };
 
 function stream_callbacks.error(session, error, data)
 	if error == "no-stream" then
@@ -39,7 +39,7 @@
 	end
 end
 
-local function handleerr(err) log("error", "Traceback[s2s]:", err, debug.traceback()); end
+local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), debug.traceback()); end
 function stream_callbacks.handlestanza(a, b)
 	xpcall(function () core_process_stanza(a, b) end, handleerr);
 end
--- a/plugins/mod_console.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_console.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_dialback.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_dialback.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -29,6 +29,8 @@
 
 local xmlns_dialback = "jabber:server:dialback";
 
+local dialback_requests = setmetatable({}, { __mode = 'v' });
+
 module: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
@@ -47,50 +49,78 @@
 		origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
 	end);
 
-module:add_handler("s2sin_unauthed", "result", xmlns_dialback,
+module:add_handler({ "s2sin_unauthed", "s2sin" }, "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,
-			st.stanza("db:verify", { from = origin.to_host, to = origin.from_host, id = origin.streamid }):text(origin.dialback_key));
-		hosts[origin.to_host].s2sout[origin.from_host].dialback_verifying = origin;
+		origin.hosts[attr.from] = { dialback_key = stanza[1] };
+		
+		if not hosts[attr.to] then
+			-- Not a host that we serve
+			log("info", "%s tried to connect to %s, which we don't serve", attr.from, attr.to);
+			origin:close("host-unknown");
+			return;
+		end
+		
+		dialback_requests[attr.from] = origin;
+		
+		if not origin.from_host then
+			-- Just used for friendlier logging
+			origin.from_host = attr.from;
+		end
+		if not origin.to_host then
+			-- Just used for friendlier logging
+			origin.to_host = attr.to;
+		end
+		
+		log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
+		send_s2s(attr.to, attr.from,
+			st.stanza("db:verify", { from = attr.to, to = attr.from, id = origin.streamid }):text(stanza[1]));
 	end);
 
 module:add_handler({ "s2sout_unauthed", "s2sout" }, "verify", xmlns_dialback,
 	function (origin, stanza)
-		if origin.dialback_verifying then
+		local attr = stanza.attr;
+		local dialback_verifying = dialback_requests[attr.from];
+		if dialback_verifying then
 			local valid;
-			local attr = stanza.attr;
 			if attr.type == "valid" then
-				s2s_make_authenticated(origin.dialback_verifying);
+				s2s_make_authenticated(dialback_verifying, attr.from);
 				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");
+				log("warn", "authoritative server for "..(attr.from or "(unknown)").." denied the key");
 				valid = "invalid";
 			end
-			if not origin.dialback_verifying.sends2s then
-				log("warn", "Incoming s2s session %s was closed in the meantime, so we can't notify it of the db result", tostring(origin.dialback_verifying):match("%w+$"));
+			if not dialback_verifying.sends2s then
+				log("warn", "Incoming s2s session %s was closed in the meantime, so we can't notify it of the db result", tostring(dialback_verifying):match("%w+$"));
 			else
-				origin.dialback_verifying.sends2s(
+				dialback_verifying.sends2s(
 						st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
-								:text(origin.dialback_verifying.dialback_key));
+								:text(dialback_verifying.hosts[attr.from].dialback_key));
 			end
+			dialback_requests[attr.from] = nil;
 		end
 	end);
 
 module:add_handler({ "s2sout_unauthed", "s2sout" }, "result", xmlns_dialback,
 	function (origin, stanza)
+		-- Remote server is telling us whether we passed dialback
+		
+		local attr = stanza.attr;
+		if not hosts[attr.to] then
+			origin:close("host-unknown");
+			return;
+		elseif hosts[attr.to].s2sout[attr.from] ~= origin then
+			-- This isn't right
+			origin:close("invalid-id");
+			return;
+		end
 		if stanza.attr.type == "valid" then
-			s2s_make_authenticated(origin);
+			s2s_make_authenticated(origin, attr.from);
 		else
-			-- FIXME
+			-- FIXME: Waiting on #33
 			error("dialback failed!");
 		end
 	end);
--- a/plugins/mod_disco.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_disco.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_legacyauth.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_legacyauth.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_ping.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_ping.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_private.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_private.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_register.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_register.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_roster.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_roster.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_saslauth.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_saslauth.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_selftests.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_selftests.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_time.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_time.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_tls.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_tls.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -33,8 +33,6 @@
 		function (session, stanza)
 			if session.conn.starttls then
 				session.send(st.stanza("proceed", { xmlns = xmlns_starttls }));
-				-- FIXME: I'm commenting the below, not sure why it was necessary
-				-- sessions[session.conn] = nil;
 				session:reset_stream();
 				session.conn.starttls();
 				session.log("info", "TLS negotiation started...");
--- a/plugins/mod_uptime.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_uptime.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_vcard.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_vcard.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/plugins/mod_version.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/plugins/mod_version.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -31,7 +31,7 @@
 	if stanza.attr.type == "get" then
 		session.send(st.reply(stanza):query(xmlns_version)
 			:tag("name"):text("Prosody"):up()
-			:tag("version"):text("0.1"):up()
+			:tag("version"):text("0.2"):up()
 			:tag("os"):text("the best operating system ever!"));
 	end
 end
--- a/prosody	Wed Dec 10 06:58:56 2008 +0500
+++ b/prosody	Tue Dec 16 02:40:29 2008 +0000
@@ -1,5 +1,5 @@
 #!/usr/bin/env lua
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -18,8 +18,6 @@
 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 --
 
-
-
 -- Config here --
 
 CFG_SOURCEDIR=nil;
@@ -47,6 +45,9 @@
 config = require "core.configmanager"
 log = require "util.logger".init("general");
 
+-- Disable log output, needs to read from config
+-- require "util.logger".setwriter(function () end);
+
 do
 	-- TODO: Check for other formats when we add support for them
 	-- Use lfs? Make a new conf/ dir?
--- a/tests/test.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tests/test.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tests/test_core_configmanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tests/test_core_configmanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tests/test_core_s2smanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tests/test_core_s2smanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tests/test_core_stanza_router.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tests/test_core_stanza_router.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tests/test_sasl.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tests/test_sasl.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tests/test_util_jid.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tests/test_util_jid.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tools/ejabberd2prosody.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tools/ejabberd2prosody.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,5 +1,5 @@
 #!/usr/bin/env lua
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/tools/erlparse.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/tools/erlparse.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/datamanager.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/datamanager.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/datetime.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/datetime.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/dependencies.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/dependencies.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/discohelper.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/discohelper.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/import.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/import.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/jid.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/jid.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/logger.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/logger.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/multitable.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/multitable.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/sasl.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/sasl.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- sasl.lua v0.1
+-- sasl.lua v0.2
 -- Copyright (C) 2008 Tobias Markmann
 -- 
 --    All rights reserved.
--- a/util/serialization.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/serialization.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/stanza.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/stanza.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
@@ -21,6 +21,7 @@
 local t_insert      =  table.insert;
 local t_concat      =  table.concat;
 local t_remove      =  table.remove;
+local t_concat      =  table.concat;
 local s_format      = string.format;
 local tostring      =      tostring;
 local setmetatable  =  setmetatable;
@@ -31,7 +32,7 @@
 local print         =         print;
 local unpack        =        unpack;
 local s_gsub        =   string.gsub;
-local os = os;
+local os            =            os;
 
 local do_pretty_printing = not os.getenv("WINDIR");
 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
@@ -117,22 +118,20 @@
 
 local xml_escape = xml_escape;
 
-local function dostring(t, buf, self)
+local function dostring(t, buf, self, xml_escape)
 	t_insert(buf, "<");
 	t_insert(buf, t.name);
-	if t.attr then
-		for k, v in pairs(t.attr) do if type(k) == "string" then
-			t_insert(buf, " ");
-			t_insert(buf, k);
-			t_insert(buf, "='");
-			t_insert(buf, (xml_escape(tostring(v))));
-			t_insert(buf, "'");
-		end end
-	end
+	for k, v in pairs(t.attr) do if type(k) == "string" then
+		t_insert(buf, " ");
+		t_insert(buf, k);
+		t_insert(buf, "='");
+		t_insert(buf, (xml_escape(tostring(v))));
+		t_insert(buf, "'");
+	end end
 	t_insert(buf, ">");
 	for n, child in ipairs(t) do
 		if child.name then 
-			self(child, buf, self);
+			self(child, buf, self, xml_escape);
 		else
 			t_insert(buf, (xml_escape(child)));
 		end
@@ -144,10 +143,11 @@
 
 function stanza_mt.__tostring(t)
 	local buf = {};
-	dostring(t, buf, dostring);
+	dostring(t, buf, dostring, xml_escape);
 	return t_concat(buf);
 end
 
+
 function stanza_mt.top_tag(t)
 	local attr_string = "";
 	if t.attr then
--- a/util/termcolours.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/termcolours.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/uuid.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/uuid.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,4 +1,4 @@
--- Prosody IM v0.1
+-- Prosody IM v0.2
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- a/util/ztact.lua	Wed Dec 10 06:58:56 2008 +0500
+++ b/util/ztact.lua	Tue Dec 16 02:40:29 2008 +0000
@@ -1,24 +1,6 @@
--- Prosody IM v0.1
--- Copyright (C) 2008 Matthew Wild
--- Copyright (C) 2008 Waqas Hussain
--- 
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public License
--- as published by the Free Software Foundation; either version 2
--- of the License, or (at your option) any later version.
--- 
--- This program is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--- GNU General Public License for more details.
--- 
--- You should have received a copy of the GNU General Public License
--- along with this program; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
---
-
-
-
+-- Prosody IM v0.2
+-- This file is included with Prosody IM. It has modifications,
+-- which are hereby placed in the public domain.
 
 -- public domain 20080410 lua@ztact.com