Register Adhoc Commands

Hello,

I’m new to adhoc commands using Smack API. Could someone give me an example about how to register a new adhoc command? In particular, how to use the argument clazz in AdHocCommandManager.registerCommand(String node, final String name, Class clazz)?

Thanks a lot in advance.

Green

Reading through the XEP is always helpful: http://xmpp.org/extensions/xep-0050.html#retrieve

The node and the name arguments are from the XEP. Here’s an example list of commands supported by the server:

<query xmlns='http://jabber.org/protocol/disco#items'
         node='http://jabber.org/protocol/commands'>
    <item jid='responder@domain'
          node='list'
          name='List Service Configurations'/>
    <item jid='responder@domain'
          node='config'
          name='Configure Service'/>
    <item jid='responder@domain'
          node='reset'
          name='Reset Service Configuration'/>
    <item jid='responder@domain'
          node='start'
          name='Start Service'/>
    <item jid='responder@domain'
          node='stop'
          name='Stop Service'/>
    <item jid='responder@domain'
          node='restart'
          name='Restart Service'/>

So, make the node be the name of the command and the name be human-readable. The class you pass in must extend LocalCommand. So, let’s say you’re writing a foobar command. You might create a class “public class Foobar extends LocalCommand”. You’d then pass in Foobar.class as an argument. I hope that’s enough to get started!

Thanks a lot! I will try it now.

Hi,

I was trying the code below, but after I added a new adhoc command and tried to execute it, I got an error message as bellow:
feature-not-implemented(501)

Also, the newly registered adhoc command didn’t appear in the Psi client.

Could someone help me out please?

Thanks a lot!

===============================

String server = “jabber.org”;
String user= “agent”;
String pwd= "12345;

        ConnectionConfiguration conf = new ConnectionConfiguration(server , 5222);
        XMPPConnection connection = new XMPPConnection(conf);
        AdHocCommandManager commandManager = AdHocCommandManager.getAddHocCommandsManager(connection);
        connection.connect();
        AccountManager accountManager = connection.getAccountManager();

if(accountManager.supportsAccountCreation()){
try{
accountManager.createAccount(user, pwd);
}
catch(XMPPException ex){
}
}

connection.login(user, pwd);

        commandManager = AdHocCommandManager.getAddHocCommandsManager(connection);

//add new adhoc commands

//myAdhoc extends LocalCommand
commandManager.registerCommand(“mynode”, “myname”, myAdhoc.class);

        DiscoverItems cmds = commandManager.discoverCommands("agent@jabber.org/Smack");

for(Iterator it2 = cmds.getItems(); it2.hasNext(); ){
DiscoverItems.Item item = (DiscoverItems.Item)it2.next();
}

       RemoteCommand cmd = commandManager.getRemoteCommand("agent@jabber.org/Smack", "mynode");

if (cmd.getNode()!=null) {
cmd.execute();
Form answer = cmd.getForm();
for(Iterator it = answer.getFields(); it.hasNext();){
FormField field = (FormField)it.next();
System.out.println(“field”+field.toXML());
}
}
connection.disconnect();

===============================

Hi,

I knwon thread it’s old but I’m in the same situation: I need to register custom LocalCommand to a client JID.

I’ve subclassed LocalCommand, used AdHocCommandManager to register my command and published it with

publishCommands but if I try to browse the jid from Psi I can’t see any commands.

Can someone help me?

many thanks