Changeset

545:60002993be04

Abstract xmlhandlers a bit more, also add error callbacks
author Matthew Wild <mwild1@gmail.com>
date Thu, 04 Dec 2008 18:47:26 +0000
parents 544:efde848869c5
children 548:c80137472280
files core/xmlhandlers.lua net/xmppclient_listener.lua net/xmppserver_listener.lua
diffstat 3 files changed, 18 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/core/xmlhandlers.lua	Thu Dec 04 17:10:02 2008 +0000
+++ b/core/xmlhandlers.lua	Thu Dec 04 18:47:26 2008 +0000
@@ -52,12 +52,13 @@
 		local chardata = {};
 		local xml_handlers = {};
 		local log = session.log or default_log;
-		--local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
-		
+
 		local send = session.send;
 		
 		local cb_streamopened = stream_callbacks.streamopened;
 		local cb_streamclosed = stream_callbacks.streamclosed;
+		local cb_error = stream_callbacks.error or function (e) error("XML stream error: "..tostring(e)); end;
+		local cb_handlestanza = stream_callbacks.handlestanza;
 		
 		local stanza
 		function xml_handlers:StartElement(name, attr)
@@ -92,12 +93,14 @@
 						if cb_streamopened then
 							cb_streamopened(session, attr);
 						end
-						return;
+					else
+						-- Garbage before stream?
+						cb_error("no-stream");
 					end
-					error("Client failed to open stream successfully");
+					return;
 				end
 				if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
-					error("Client sent invalid top-level stanza");
+					cb_error("invalid-top-level-element");
 				end
 				
 				stanza = st.stanza(name, attr);
@@ -119,15 +122,14 @@
 			curr_ns,name = name:match("^(.+)|([%w%-]+)$");
 			if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
 				if name == "stream" then
-					log("debug", "Stream closed");
 					if cb_streamclosed then
 						cb_streamclosed(session);
 					end
 					return;
 				elseif name == "error" then
-					error("Stream error: "..tostring(name)..": "..tostring(stanza));
+					cb_error("stream-error", stanza);
 				else
-					error("XML parse error in client stream with element: "..name);
+					cb_error("parse-error", "unexpected-element-close", name);
 				end
 			end
 			if stanza and #chardata > 0 then
@@ -137,7 +139,7 @@
 			end
 			-- Complete stanza
 			if #stanza.last_add == 0 then
-				session.stanza_dispatch(stanza);
+				cb_handlestanza(session, stanza);
 				stanza = nil;
 			else
 				stanza:up();
--- a/net/xmppclient_listener.lua	Thu Dec 04 17:10:02 2008 +0000
+++ b/net/xmppclient_listener.lua	Thu Dec 04 18:47:26 2008 +0000
@@ -36,7 +36,7 @@
 local sm_streamclosed = sessionmanager.streamclosed;
 local st = stanza;
 
-local stream_callbacks = { streamopened = sm_streamopened, streamclosed = sm_streamclosed };
+local stream_callbacks = { streamopened = sm_streamopened, streamclosed = sm_streamclosed, handlestanza = core_process_stanza };
 
 local sessions = {};
 local xmppclient = { default_port = 5222, default_mode = "*a" };
@@ -119,8 +119,9 @@
 		-- (I'm on a mission, no time to fix now)
 
 		-- Debug version --
-		local function handleerr(err) print("Traceback:", err, debug.traceback()); end
-		session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr));  end
+		--local function handleerr(err) print("Traceback:", err, debug.traceback()); end
+		--session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr));  end
+		
 	end
 	if data then
 		session.data(conn, data);
--- a/net/xmppserver_listener.lua	Thu Dec 04 17:10:02 2008 +0000
+++ b/net/xmppserver_listener.lua	Thu Dec 04 18:47:26 2008 +0000
@@ -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 = { streamopened = s2s_streamopened, streamclosed = s2s_streamclosed };
+local stream_callbacks = { streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza =  core_process_stanza };
 
 local connlisteners_register = require "net.connlisteners".register;
 
@@ -121,8 +121,8 @@
 		-- (I'm on a mission, no time to fix now)
 
 		-- Debug version --
-		local function handleerr(err) print("Traceback:", err, debug.traceback()); end
-		session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr));  end
+--		local function handleerr(err) print("Traceback:", err, debug.traceback()); end
+--		session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr));  end
 	end
 	if data then
 		session.data(conn, data);