core/xmlhandlers.lua
changeset 545 60002993be04
parent 519 cccd610a0ef9
child 557 c9b3ffb08fe3
equal deleted inserted replaced
544:efde848869c5 545:60002993be04
    50 		local curr_ns = "";
    50 		local curr_ns = "";
    51 		local curr_tag;
    51 		local curr_tag;
    52 		local chardata = {};
    52 		local chardata = {};
    53 		local xml_handlers = {};
    53 		local xml_handlers = {};
    54 		local log = session.log or default_log;
    54 		local log = session.log or default_log;
    55 		--local print = function (...) log("info", "xmlhandlers", t_concatall({...}, "\t")); end
    55 
    56 		
       
    57 		local send = session.send;
    56 		local send = session.send;
    58 		
    57 		
    59 		local cb_streamopened = stream_callbacks.streamopened;
    58 		local cb_streamopened = stream_callbacks.streamopened;
    60 		local cb_streamclosed = stream_callbacks.streamclosed;
    59 		local cb_streamclosed = stream_callbacks.streamclosed;
       
    60 		local cb_error = stream_callbacks.error or function (e) error("XML stream error: "..tostring(e)); end;
       
    61 		local cb_handlestanza = stream_callbacks.handlestanza;
    61 		
    62 		
    62 		local stanza
    63 		local stanza
    63 		function xml_handlers:StartElement(name, attr)
    64 		function xml_handlers:StartElement(name, attr)
    64 			if stanza and #chardata > 0 then
    65 			if stanza and #chardata > 0 then
    65 				-- We have some character data in the buffer
    66 				-- We have some character data in the buffer
    90 				if session.notopen then
    91 				if session.notopen then
    91 					if name == "stream" then
    92 					if name == "stream" then
    92 						if cb_streamopened then
    93 						if cb_streamopened then
    93 							cb_streamopened(session, attr);
    94 							cb_streamopened(session, attr);
    94 						end
    95 						end
    95 						return;
    96 					else
       
    97 						-- Garbage before stream?
       
    98 						cb_error("no-stream");
    96 					end
    99 					end
    97 					error("Client failed to open stream successfully");
   100 					return;
    98 				end
   101 				end
    99 				if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
   102 				if curr_ns == "jabber:client" and name ~= "iq" and name ~= "presence" and name ~= "message" then
   100 					error("Client sent invalid top-level stanza");
   103 					cb_error("invalid-top-level-element");
   101 				end
   104 				end
   102 				
   105 				
   103 				stanza = st.stanza(name, attr);
   106 				stanza = st.stanza(name, attr);
   104 				curr_tag = stanza;
   107 				curr_tag = stanza;
   105 			else -- we are inside a stanza, so add a tag
   108 			else -- we are inside a stanza, so add a tag
   117 		end
   120 		end
   118 		function xml_handlers:EndElement(name)
   121 		function xml_handlers:EndElement(name)
   119 			curr_ns,name = name:match("^(.+)|([%w%-]+)$");
   122 			curr_ns,name = name:match("^(.+)|([%w%-]+)$");
   120 			if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
   123 			if (not stanza) or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
   121 				if name == "stream" then
   124 				if name == "stream" then
   122 					log("debug", "Stream closed");
       
   123 					if cb_streamclosed then
   125 					if cb_streamclosed then
   124 						cb_streamclosed(session);
   126 						cb_streamclosed(session);
   125 					end
   127 					end
   126 					return;
   128 					return;
   127 				elseif name == "error" then
   129 				elseif name == "error" then
   128 					error("Stream error: "..tostring(name)..": "..tostring(stanza));
   130 					cb_error("stream-error", stanza);
   129 				else
   131 				else
   130 					error("XML parse error in client stream with element: "..name);
   132 					cb_error("parse-error", "unexpected-element-close", name);
   131 				end
   133 				end
   132 			end
   134 			end
   133 			if stanza and #chardata > 0 then
   135 			if stanza and #chardata > 0 then
   134 				-- We have some character data in the buffer
   136 				-- We have some character data in the buffer
   135 				stanza:text(t_concat(chardata));
   137 				stanza:text(t_concat(chardata));
   136 				chardata = {};
   138 				chardata = {};
   137 			end
   139 			end
   138 			-- Complete stanza
   140 			-- Complete stanza
   139 			if #stanza.last_add == 0 then
   141 			if #stanza.last_add == 0 then
   140 				session.stanza_dispatch(stanza);
   142 				cb_handlestanza(session, stanza);
   141 				stanza = nil;
   143 				stanza = nil;
   142 			else
   144 			else
   143 				stanza:up();
   145 				stanza:up();
   144 			end
   146 			end
   145 		end
   147 		end