util/xmppstream.lua

Tue, 04 Jan 2011 00:23:51 +0000

author
Daurnimator <quae@daurnimator.com>
date
Tue, 04 Jan 2011 00:23:51 +0000
changeset 3995
7214dc7a5642
parent 3989
692d221ef9bd
child 4002
2b53b4b5d46e
permissions
-rw-r--r--

util.xmppstream, util.xmllex: Basic test passes

-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
-- 
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local tblinsert = table.insert

local lex = require "util.xmllex";
local index = lex.index
local tagindex_to_tree  = lex.tagindex_to_tree
local process_starttag = lex.process_starttag

local default_log = require "util.logger".init("xmppstream");

local xmlns_streams = "http://etherx.jabber.org/streams";

local function new(session, stream_callbacks)
	local partial
	local openindexes = { }
	local lastindex = 0
	
	return {
		reset = function ( )
			partial = nil
			openindexes = { }
		end ;
		feed = function ( stream , data )
			while true do
				partial = index ( data , partial )
				for i = lastindex + 1 , #partial - 1 do
					local v = partial [ i ]
					
					if v.finish then
						if v.type == "open" then
							tblinsert ( openindexes , i )
							if #openindexes == 1 then
								local name , attr = process_starttag ( v )
								stream_callbacks.streamopened ( session , attr )
							end
						elseif v.type == "close" then
							if #openindexes == 2 then -- If closing level 2...
								local stanza = tagindex_to_tree ( partial , openindexes [ #openindexes ] , i )
								stream_callbacks.handlestanza ( session , stanza )
							elseif #openindexes == 1 then
								stream_callbacks.streamclosed ( session )
							end
							--print ( "Closed level " .. #openindexes )
							openindexes [ #openindexes ] = nil
						end
					end
				end
				lastindex = #partial - 1
				break
			end
		end ;
		set_session = function ( newsession ) session = newsession end
	};
end

return {
	new = new ;
}

mercurial