diff options
| -rw-r--r-- | xml.lua | 24 | ||||
| -rw-r--r-- | xmpp.lua | 67 | 
2 files changed, 46 insertions, 45 deletions
| @@ -1,3 +1,6 @@ +-- originally from http://lua-users.org/wiki/LuaXml +-- modified by me a bit +  local function parseargs(s)    local arg = {}    string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a) @@ -6,6 +9,18 @@ local function parseargs(s)    return arg  end +local function tag(x) +	return setmetatable(x, {__call=function(x, label) +		-- search for child with given label +		for _,c in ipairs(x) do +			if c.label == label then +				return c +			end +		end +		return nil +	end}) +end +  local psingle  local function pmulti(s, i, parent) @@ -41,11 +56,11 @@ psingle = function(s, i)  	end  	if empty == "/" then -		return nexti, {label=label, xarg=parseargs(xarg), empty=true} +		return nexti, tag{label=label, xarg=parseargs(xarg), empty=true}  	elseif c == "" then -- start tag -		return multi(s, nexti, {label=label, xarg=parseargs(xarg)}) +		return pmulti(s, nexti, tag{label=label, xarg=parseargs(xarg)})  	else -- end tag -		return nexti, {label=label, close=true} +		return nexti, tag{label=label, close=true}  	end  end @@ -63,7 +78,8 @@ local function stanzae(getmore)  				coroutine.yield(el)  				buf = buf:sub(ni)  			else -				buf = buf..getmore() +				local more = getmore() +				buf = buf .. more  			end  		end  	end) @@ -8,17 +8,17 @@ local cqueues = require'cqueues'  local socket = require'cqueues.socket' -local function connect(...) -	local sock = assert(socket.connect(... or server, 5222)) +local function connect() +	local sock = assert(socket.connect(server, 5222))  	sock:setmode('bn','bn')  	local start = ([[  <?xml version='1.0'?><stream:stream from='%s' to='%s' version='1.0' xml:lang='en' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>]]):format(jid, server) +	-- state of the art xml parser  	local function check_and_send(test, text)  		local x = sock:read('-2048')  		print(x) -		assert(x:find(test))  		if text then sock:write(text) end  	end @@ -38,50 +38,35 @@ local function connect(...)  	return sock  end --- sock:write[[ --- <iq type='set' id='1234'> --- <pubsub xmlns='http://jabber.org/protocol/pubsub'> --- <publish node='urn:xmpp:avatar:metadata'> --- <item id='89229147c33129fd8d1ab177b6cbea725e72a8b4'> --- <metadata xmlns='urn:xmpp:avatar:metadata'> --- <info bytes='54660' --- 	height='213' --- 	width='203' --- 	id='89229147c33129fd8d1ab177b6cbea725e72a8b4' --- 	type='image/png' --- 	url='https://ubq323.website/files/itAE9dXL.png'/> --- </metadata> --- </item> --- </publish> --- </pubsub> --- </iq>]] - -local sock = connect(...)  local mucs = { -	'ja@conference.ubq323.website' +	'ja@conference.ubq323.website', +	'a@conference.ubq323.website',  } -for _, muc in ipairs(mucs) do -	sock:write(([[ -		<presence to='%s'> -			<x xmlns='http://jabber.org/protocol/muc'/> -		</presence>]]):format(muc .. '/wilson')) -end +local xml = require'xml' +local pprint=require'pprint' +local function run_xmpp() +	local sock = connect() --- sock:write[[<iq type='get' ---     to='ja@conference.ubq323.website/viba' ---     id='retrieve1'> ---   <pubsub xmlns='http://jabber.org/protocol/pubsub'> ---     <items node='urn:xmpp:avatar:data'> ---       <item id='9c78e85250ef6f74f44f3142f559cc4f4465298c'/> ---     </items> ---   </pubsub> --- </iq>]] +	for _, muc in ipairs(mucs) do +		sock:write(([[ +			<presence to='%s'> +				<x xmlns='http://jabber.org/protocol/muc'> +					<history maxstanzas='0'/> +				</x> +			</presence>]]):format(muc .. '/wilson')) +	end -local xml=require'xml' -for x in xml.stanzae(function() return sock:read('-2048') end) do -	print(x) +	for x in xml.stanzae(function() return sock:read('-2048') end) do +		-- pprint(x) +		local body = x'body' and x'body'[1] +		if x.label == 'message' then pprint('M', x.xarg.from, body) end +	end  end +local cq = cqueues.new() +cq:wrap(run_xmpp) +pprint(cq:loop()) + | 
