examples/lm/disco.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Mon, 06 Apr 2009 17:21:19 +0300
changeset 70 e43e386c8a33
parent 68 742878c74b8e
permissions -rw-r--r--
Vcard-temp avatar * Docgen handles preformatted text * Forms can add fields * Ping help * Easy way to publish vcard-temp photo * File mime-type detection


-- SERVICE DISCOVERY (XEP-0030)

-- TODO handler

-- library

local iq = require 'lm.iq'

--

local F = { }

function F.items ( conn, to, success, fail, node )
	iq.send ( conn, to, 'get',
		{
			query = { xmlns = 'http://jabber.org/protocol/disco#items', node = node }
		},
		function ( mess )
			local item  = mess:child( 'query' ):child ()
			local items = { }
			while item do
				if item:name () == 'item' then
					table.insert ( items, { jid = item:attribute ( 'jid' ), node = item:attribute ( 'node' ), name = item:attribute ( 'name' ) } )
				end
				item = item:next ()
			end
			success ( items )
		end,
		fail )
end

function F.info ( conn, to, success, fail )
	iq.send ( conn, to, 'get',
		{
			query = { xmlns='http://jabber.org/protocol/disco#info' }
		},
		function ( mess )
			local identities = { }
			local features   = { }
			local item       = mess:child( 'query' ):child ()
			while item do
				local name  = item:name ()
				if name == 'identity' then
					table.insert ( identities, { category = item:attribute ( 'category' ), type = item:attribute ( 'type' ), name = item:attribute ( 'name' ) } )
				elseif name == 'feature' then
					table.insert ( features, item:attribute ( 'var' ) )
				end
				item = item:next ()
			end
			success ( identities, features )
		end, fail )
end

return F

-- vim: se ts=4: --