--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/disco.lua Sat Mar 21 05:05:14 2009 +0200
@@ -0,0 +1,114 @@
+
+-- SERVICE DISCOVERY (XEP-0030)
+
+-- library
+
+require 'lm'
+require 'iq'
+
+-- public
+
+disco = { }
+
+function disco.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' ):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
+ success ( items )
+ end,
+ fail )
+end
+
+function disco.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' ):children ()
+ 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
+
+-- mcabber
+
+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' )
+
+commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted."
+
+-- vim: se ts=4: --
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/remote.lua Sat Mar 21 05:05:14 2009 +0200
@@ -0,0 +1,106 @@
+
+-- REMOTE CONTROLLING CLIENTS (XEP-0146)
+
+-- library
+
+require 'lm'
+require 'iq'
+-- forms
+require 'disco'
+
+-- public
+
+remote = { }
+
+function remote.list ( conn, to, success, fail )
+ disco.items ( conn, to, success, fail, 'http://jabber.org/protocol/commands' )
+end
+
+function remote.command ( conn, to, command, success, fail )
+ iq.send ( conn, to, 'set',
+ {
+ command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
+ },
+ function ( mess )
+ local c = mess:child ( 'command' )
+ if c then
+ local x = c:child ( 'x' )
+ if x then
+ local sid = c:attribute ( 'sessionid' )
+ local id = parse_form ( x ) -- FIXME
+ forms[id].send =
+ function ( form )
+ iq.send ( conn, to, 'set',
+ {
+ command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
+ x = { xmlns = 'jabber:x:data', type = 'form',
+ field = form.val,
+ },
+ },
+ },
+ function ( mess )
+ local c = mess:child ( 'command' )
+ if c and c:attribute ( 'status' ) == 'completed' then
+ success ()
+ main.print_info ( to, 'Now you can run /form del ' .. id .. ' to delete form from list' )
+ form.status = 'acquired'
+ else
+ print ( 'D: FIXME: nonsuccessful remote command: ' .. mess:xml () )
+ end
+ end,
+ function ( mesg )
+ main.print_info ( to, 'Got non-successful response to form: ' .. mesg )
+ form.status = 'rejected'
+ end )
+ form.status = 'sent'
+ end
+ forms[id].status = 'filling'
+ main.print_info ( to, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
+ end
+ end
+ end,
+ fail )
+end
+
+-- mcabber
+
+main.command ( 'remote',
+ function ( args )
+ local who
+ if args.t then
+ who = args.t
+ else
+ who = main.full_jid ()
+ end
+ local action = args[1]
+ local conn = lm.connection.bless ( main.connection () )
+ if action then
+ remote.command ( conn, who, action,
+ function ()
+ main.print_info ( who, ('Command %s executed'):format ( action ) )
+ end,
+ function ( mesg )
+ main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
+ end )
+ else
+ remote.list ( conn, who,
+ function ( items )
+ local text = ''
+ for index, item in ipairs ( items ) do
+ text = text .. '\n - ' .. item.node
+ end
+ if text ~= '' then
+ main.print_info ( who, 'Available commands:' .. text )
+ else
+ main.print_info ( who, 'No commands available.' )
+ end
+ end,
+ function ( mesg )
+ main.print_info ( who, ("Remote commands list for %s failed: %s"):format ( who, mesg ) )
+ end )
+ end
+ end, true, 'jid' )
+
+commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command."
+
+-- vim: se ts=4: --