examples/xep0030.lua
changeset 66 542f61e113cb
parent 65 f1be8dbb209c
child 67 d33ca5572e91
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
     1 
       
     2 function disco_items ( connection, callback, what, node )
       
     3 	local request =
       
     4 		lm.message.create { to = what, mtype = 'iq-get',
       
     5 			query = { xmlns = 'http://jabber.org/protocol/disco#items' }
       
     6 		}
       
     7 	if node then
       
     8 		request:child( 'query' ):attribute ( 'node', node )
       
     9 	end
       
    10 	return connection:send ( request,
       
    11 		function ( conn, message )
       
    12 			if message:child ( 'error' ) then
       
    13 				callback ( message:child( 'error' ):children():name () )
       
    14 			else
       
    15 				local item = message:child( 'query' ):children ()
       
    16 				local items = { }
       
    17 				while item do
       
    18 					if item:name () == 'item' then
       
    19 						table.insert ( items, { jid = item:attribute ( 'jid' ), node = item:attribute ( 'node' ), name = item:attribute ( 'name' ) } )
       
    20 					end
       
    21 					item = item:next ()
       
    22 				end
       
    23 				callback ( items )
       
    24 			end
       
    25 			return true
       
    26 		end )
       
    27 end
       
    28 
       
    29 function disco_info ( connection, callback, what )
       
    30 	return connection:send (
       
    31 		lm.message.create { to = what, mtype = 'iq-get',
       
    32 			query = { xmlns='http://jabber.org/protocol/disco#info' }
       
    33 		},
       
    34 		function ( conn, message )
       
    35 			local items_supported = false
       
    36 			if message:child ( 'error' ) then
       
    37 				callback ( message:child( 'error' ):children():name () )
       
    38 			else
       
    39 				local item = message:child( 'query' ):children ()
       
    40 				local identities = { }
       
    41 				local features   = { }
       
    42 				while item do
       
    43 					if item:name () == 'identity' then
       
    44 						table.insert ( identities, { category = item:attribute ( 'category' ), type = item:attribute ( 'type' ), name = item:attribute ( 'name' ) } )
       
    45 					elseif item:name () == 'feature' then
       
    46 						table.insert ( features, item:attribute ( 'var' ) )
       
    47 					end
       
    48 					item = item:next ()
       
    49 				end
       
    50 				callback ( identities, features )
       
    51 			end
       
    52 			return true
       
    53 		end )
       
    54 end
       
    55 
       
    56 main.command ( 'disco',
       
    57 	function ( args )
       
    58 		local who
       
    59 		local conn = lm.connection.bless ( main.connection () )
       
    60 		if args.t then
       
    61 			who = args.t
       
    62 		else
       
    63 			who = main.full_jid ()
       
    64 		end
       
    65 		if args[1] == 'items' then
       
    66 			local node = args[2]
       
    67 			disco_items ( conn,
       
    68 				function ( items )
       
    69 					if type ( items ) == 'string' then
       
    70 						main.print_info ( who, ("Items service discovery for %s (%s) failed: %s"):format ( who, node or '', items ) )
       
    71 					else
       
    72 						local text = ''
       
    73 						for index, item in ipairs ( items ) do
       
    74 							text = text .. ("\n    [%s (%s)] %s"):format ( item.jid or '', item.node or '', item.name or '' )
       
    75 						end
       
    76 						if text ~= '' then
       
    77 							main.print_info ( who, ("Items service discovery result for %s (%s):%s"):format ( who, node or '', text ) )
       
    78 						else
       
    79 							main.print_info ( who, ("No items in discovery result for %s (%s)"):format ( who, node or '' ) )
       
    80 						end
       
    81 					end
       
    82 				end, who, node )
       
    83 		else
       
    84 			disco_info ( conn,
       
    85 				function ( identities, features )
       
    86 					if type ( identities ) == 'string' then
       
    87 						main.print_info ( who, ("Info service discovery for %s failed: %s"):format ( who, identities ) )
       
    88 					else
       
    89 						main.print_info ( who, ("Service info discovery result for %s:"):format ( who ) )
       
    90 						local text = ''
       
    91 						for index, identity in ipairs ( identities ) do
       
    92 							text = text .. ("\n    [%s (%s)] %s"):format ( identity.category or '', identity.type or '', identity.name or '' )
       
    93 						end
       
    94 						if text ~= '' then
       
    95 							main.print_info ( who, "  Identities:" .. text )
       
    96 						else
       
    97 							main.print_info ( who, "  No identities" )
       
    98 						end
       
    99 						text = ''
       
   100 						for index, feature in ipairs ( features ) do
       
   101 							text = text .. ("\n    [%s]"):format ( feature or '' )
       
   102 						end
       
   103 						if text ~= '' then
       
   104 							main.print_info ( who, "  Features:" .. text )
       
   105 						else
       
   106 							main.print_info ( who, "  No features" )
       
   107 						end
       
   108 					end
       
   109 				end, who )
       
   110 		end
       
   111 	end, true, 'jid' )
       
   112 
       
   113 commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted."
       
   114 
       
   115 -- vim: se ts=4: --