examples/disco.lua
changeset 68 742878c74b8e
parent 66 542f61e113cb
--- a/examples/disco.lua	Sat Mar 28 19:43:12 2009 +0200
+++ b/examples/disco.lua	Tue Mar 31 18:35:34 2009 +0300
@@ -1,58 +1,62 @@
 
--- SERVICE DISCOVERY (XEP-0030)
-
--- TODO add handler (unused by mcabber)
-
--- library
-
-local iq = require 'iq'
-
---
-
-local F = { }
+local lm    = require 'lm'
+local disco = require 'lm.disco'
 
-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
+main.command ( 'disco',
+	function ( args )
+		local who
+		local conn = lm.connection.bless ( main.connection () )
+		if args.t then
+			who = args.t
+		else
+			who = main.full_jid ()
+		end
+		if args[1] == 'items' then
+			local node = args[2]
+			disco.items ( conn, who,
+				function ( items )
+					local text = ''
+					for index, item in ipairs ( items ) do
+						text = text .. ("\n    [%s (%s)] %s"):format ( item.jid or '', item.node or '', item.name or '' )
+					end
+					if text ~= '' then
+						main.print_info ( who, ("Items service discovery result for %s (%s):%s"):format ( who, node or '', text ) )
+					else
+						main.print_info ( who, ("No items in discovery result for %s (%s)"):format ( who, node or '' ) )
+					end
+				end,
+				function ( mesg )
+					main.print_info ( who, ("Items service discovery for %s (%s) failed: %s"):format ( who, node or '', mesg ) )
+				end, node )
+		else
+			disco.info ( conn, who,
+				function ( identities, features )
+					main.print_info ( who, ("Service info discovery result for %s:"):format ( who ) )
+					local text = ''
+					for index, identity in ipairs ( identities ) do
+						text = text .. ("\n    [%s (%s)] %s"):format ( identity.category or '', identity.type or '', identity.name or '' )
+					end
+					if text ~= '' then
+						main.print_info ( who, "  Identities:" .. text )
+					else
+						main.print_info ( who, "  No identities" )
+					end
+					text = ''
+					for index, feature in ipairs ( features ) do
+						text = text .. ("\n    [%s]"):format ( feature or '' )
+					end
+					if text ~= '' then
+						main.print_info ( who, "  Features:" .. text )
+					else
+						main.print_info ( who, "  No features" )
+					end
+				end,
+				function ( mesg )
+					main.print_info ( who, ("Info service discovery for %s failed: %s"):format ( who, mesg ) )
+				end )
+		end
+	end, true, 'jid' )
 
-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
+commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted."
 
 -- vim: se ts=4: --