util/xmppstream.lua

Wed, 05 Jan 2011 05:14:02 +0000

author
daurnimator <quae@daurnimator.com>
date
Wed, 05 Jan 2011 05:14:02 +0000
changeset 4002
2b53b4b5d46e
parent 3995
7214dc7a5642
permissions
-rw-r--r--

util.xmllex, util.xmppstream: It runs

-- 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 get_name, get_attr = lex.get_name, lex.get_attr;

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 = { }
			lastindex = 0
		end ;
		feed = function ( stream , data )
			--print("FFEED", data)
			partial = index ( data , partial )
			local tmp = lastindex
			lastindex = #partial - 1
			for i = tmp + 1 , #partial - 1 do
				local v = partial [ i ]
				
				if v.finish then
					if v.type == "open" or v.type == "selfclosing" then
						tblinsert ( openindexes , i )
						if #openindexes == 1 then
							local vstr = tostring(v);
							local name = get_name ( vstr )
							if name == "stream:stream" then
								local attr = get_attr ( vstr );
								stream_callbacks.streamopened ( session , attr )
							end
						end
					end
					if v.type == "close" or v.type == "selfclosing" then
						if #openindexes == 2 then -- If closing level 2...
							local attr = get_attr ( tostring(partial[openindexes[1]]) )
							local stanza = tagindex_to_tree ( partial , openindexes [ #openindexes ] , i , {
								attr = attr,
							} )
							if stanza[1].attr.xmlns == attr.xmlns then
								stanza[1].attr.xmlns = false;
							end
							stream_callbacks.handlestanza ( session , stanza[1] )
						elseif #openindexes == 1 then
							local prefix, name = get_name ( tostring(v) )
							if name == "stream" then
								stream_callbacks.streamclosed ( session )
							end
						end
						openindexes [ #openindexes ] = nil
					end
				end
			end
			return true
		end ;
		set_session = function ( newsession ) session = newsession end
	};
end

return {
	new = new ;
}

mercurial