Move stream opening handling from xmlhandlers to sessionmanager

Fri, 03 Oct 2008 22:18:08 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 03 Oct 2008 22:18:08 +0100
changeset 40
2c0147bbd81a
parent 39
89877d61ac51
child 41
68297fef35ff

Move stream opening handling from xmlhandlers to sessionmanager

core/sessionmanager.lua file | annotate | diff | comparison | revisions
core/xmlhandlers.lua file | annotate | diff | comparison | revisions
--- a/core/sessionmanager.lua	Fri Oct 03 22:17:20 2008 +0100
+++ b/core/sessionmanager.lua	Fri Oct 03 22:18:08 2008 +0100
@@ -1,11 +1,17 @@
 
-local tostring = tostring;
+local tonumber, tostring = tonumber, tostring;
+local ipairs = ipairs;
+
+local m_random = math.random;
+local format = string.format;
 
 local print = print;
 
 local hosts = hosts;
 
+local modulemanager = require "core.modulemanager";
 local log = require "util.logger".init("sessionmanager");
+local error = error;
 
 module "sessionmanager"
 
@@ -54,4 +60,27 @@
 	return true;
 end
 
+function streamopened(session, attr)
+						local send = session.send;
+						session.host = attr.to or error("Client failed to specify destination hostname");
+			                        session.version = tonumber(attr.version) or 0;
+			                        session.streamid = m_random(1000000, 99999999);
+			                        print(session, session.host, "Client opened stream");
+			                        send("<?xml version='1.0'?>");
+			                        send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' version='1.0'>", session.streamid, session.host));
+						
+						local features = {};
+						modulemanager.fire_event("stream-features", session, features);
+						
+						send("<stream:features>");
+						
+						for _, feature in ipairs(features) do
+							send_to_session(session, tostring(features));
+						end
+ 
+        			                send("</stream:features>");
+						log("info", "core", "Stream opened successfully");
+						session.notopen = nil;
+end
+
 return _M;
\ No newline at end of file
--- a/core/xmlhandlers.lua	Fri Oct 03 22:17:20 2008 +0100
+++ b/core/xmlhandlers.lua	Fri Oct 03 22:18:08 2008 +0100
@@ -1,4 +1,5 @@
 
+local sessionmanager_streamopened = require "core.sessionmanager".streamopened;
 require "util.stanza"
 
 local st = stanza;
@@ -27,7 +28,6 @@
 		
 		local stanza
 		function xml_handlers:StartElement(name, attr)
-				log("info", "xmlhandlers", "Start element: " .. name);
 			if stanza and #chardata > 0 then
 				-- We have some character data in the buffer
 				stanza:text(t_concat(chardata));
@@ -37,24 +37,7 @@
 			if not stanza then
 				if session.notopen then
 					if name == "stream" then
-						session.host = attr.to or error("Client failed to specify destination hostname");
-			                        session.version = attr.version or 0;
-			                        session.streamid = m_random(1000000, 99999999);
-			                        print(session, session.host, "Client opened stream");
-			                        send("<?xml version='1.0'?>");
-			                        send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' version='1.0'>", session.streamid, session.host));
-						send("<stream:features>");
-						if not session.username then
-							send("<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
-								send("<mechanism>PLAIN</mechanism>");
-							send("</mechanisms>");
-						else
-							send("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
-						end
-        			                --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
-        			                send("</stream:features>");
-						log("info", "core", "Stream opened successfully");
-						session.notopen = nil;
+						sessionmanager_streamopened(session, attr);
 						return;
 					end
 					error("Client failed to open stream successfully");

mercurial