examples/xep0030.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Sun, 15 Mar 2009 20:45:24 +0200
changeset 24 25552b21d3fb
parent 10 73f4c12b6ffb
child 27 92b254b64360
permissions -rw-r--r--
Arguments parsing


function disco_items ( callback, what, node )
	local request =
		lm.message.create { to = what, mtype = 'iq-get',
			query = { xmlns = 'http://jabber.org/protocol/disco#items' }
		}
	if node then
		request:child( 'query' ):attribute ( 'node', node )
	end
	return lm.connection.bless( main.connection () ):send ( request,
		function ( conn, message )
			if message:child ( 'error' ) then
				callback ( message:child( 'error' ):children():name () )
			else
				local item = message:child( 'query' ):children ()
				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
				callback ( items )
			end
			return true
		end )
end

function disco_info ( callback, what )
	return lm.connection.bless( main.connection () ):send (
		lm.message.create { to = what, mtype = 'iq-get',
			query = { xmlns='http://jabber.org/protocol/disco#info' }
		},
		function ( conn, message )
			local items_supported = false
			if message:child ( 'error' ) then
				callback ( message:child( 'error' ):children():name () )
			else
				local item = message:child( 'query' ):children ()
				local identities = { }
				local features   = { }
				while item do
					if item:name () == 'identity' then
						table.insert ( identities, { category = item:attribute ( 'category' ), type = item:attribute ( 'type' ), name = item:attribute ( 'name' ) } )
					elseif item:name () == 'feature' then
						table.insert ( features, item:attribute ( 'var' ) )
					end
					item = item:next ()
				end
				callback ( identities, features )
			end
			return true
		end )
end

main.command ( 'disco',
	function ( args )
		args = parse_args ( args )
		local who
		if args.t then
			who = args.t
		else
			who = main.full_jid ()
		end
		if args[1] == 'items' then
			local node = args[2]
			disco_items (
				function ( items )
					if type ( items ) == 'string' then
						main.print_info ( who, string.format ( "Service items discovery for %s (%s) failed: %s", who, node or '', items ) )
					else
						main.print_info ( who, string.format ( "Service items discovery result for %s (%s):", who, node or '' ) )
						for index, item in ipairs ( items ) do
							main.print_info ( who, string.format ( "    [%s (%s)] %s", item.jid or '', item.node or '', item.name or '' ) )
						end
					end
				end, who, node )
		else
			disco_info (
				function ( identities, features )
					if type ( identities ) == 'string' then
						main.print_info ( who, string.format ( "Service info discovery for %s failed: %s", who, identities ) )
					else
						main.print_info ( who, string.format ( "Service info discovery result for %s:", who ) )
						main.print_info ( who, "  Identities:" )
						for index, identity in ipairs ( identities ) do
							main.print_info ( who, string.format ( "    [%s (%s)] %s", identity.category or '', identity.type or '', identity.name or '' ) )
						end
						main.print_info ( who, "  Features:" )
						for index, feature in ipairs ( features ) do
							main.print_info ( who, string.format ( "    [%s]", feature or '' ) )
						end
					end
				end, who )
		end
	end, 'jid' )

commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted.\nIf info reveals, that buddy can do items, items request also will be sent."

-- vim: se ts=4: --