Use smack for xmpp connection in plugin or?

Hello all,

I have many bots that are not persistently connected to openfire due to resources.

I need to momentarily put one or more of these bots online (if they are offline) and then immediately disconnect them.

I need to accomplish this within a plugin.

Should I use smack api for this or is there another method for creating an xmpp connection within a plugin?

If another method, an example would be great as I cannot seem to find much on this.

If smack then no worries as I have can refer to the documentation for smack.

If I use openfire api then at what point (how many connections) would I run into issues?

Thanks!

BTW, I tried something like this to see if I could fake online presence which is preferable.

if(!presenceManager.isAvailable(user)) {
                        Log.debug(user.getName() + " is not online");
                        Presence presence = new Presence();
                        presence.setPriority(29);
                        presence.setFrom(user.getUsername() + "@" + xmppServer.getServerInfo().getXMPPDomain());
                        presence.setTo(adminJid);
                        presence.setShow(Presence.Show.chat);
                        presence.setType(Presence.Type.probe);
                        xmppServer.getPacketRouter().route(presence);

Ok I was able to get this to work as I needed with some modification to the above code.

Here is what I was trying to accomplish.

I created a plugin that would create sharedGroups (via http request to plugin) based on custom user attributes (set in another plugin) and my requirement was that the sharedGroup show up immediately in the spark roster. I guess you could call it a dynamic roster.

The problem I was having was that the sharedGroup would only appear in the roster if all the members of the group were showing as online. If all members (other than the person creating the sharedGroup) were offline the group would not show up until I restarted Spark. I ended up setting all members to “online” using the below code and it works.

Create the sharedGroup

//set all users in this group to "online" if(!presenceManager.isAvailable(user)) {
    Presence presence = new Presence();
    presence.setPriority(10);
    presence.setFrom(user.getUsername() + "@" + xmppServer.getServerInfo().getXMPPDomain());
    presence.setTo(adminJid);//user we want custom group created for
    presence.setStatus("online");
    presence.setShow(null);
    presence.setType(null);
    user.getRoster().broadcastPresence(presence);
    sessionManager.broadcastPresenceToOtherResources(adminJid, presence);
    presenceManager.userAvailable(presence);
    xmppServer.getPacketRouter().route(presence);
  }
  //Now add the user to the group list.
  group.getMembers().add(new JID(user.getUsername() + "@"
       + xmppServer.getServerInfo().getXMPPDomain()));   rosterManager.getSharedGroups(_userName).add(group);
  Roster roster = rosterManager.getRoster(_userName);
  //Force a Roster IQ packet to this user.
  IQ iq = roster.getReset();
  iq.setType(IQ.Type.set);
  iq.setFrom(adminJid);
  iq.setTo(adminJid);
  iq.setID(String.valueOf(Math.random()));
  try {
      iqRosterHandler.handleIQ(iq);

rcollier I appreciate your responses.!

If you are running a plugin, you don’t need a connection as your code already runs in the server and has access to the internal API’s of the server.

I can’t think ot a use case where you would use Smack in a plugin.

Ok, I figured that. So how do I actually create a connection.

The only thing I found was LocalClientSession but I see no implementation of it.

Are you aware of any examples of using the LocalClientSession?

LocalClientSession localClientSession = sessionManager.createClientSession(Connection???);

Thanks.

I am still not understanding why you are trying to create a “connection” to the server that your plugin is actually a part of.

You should be able to do something like you mentioned in your original post, since you do have access to the server API’s and are able to simply send packets. I don’t know if there is something special about trying to spoof a presence packet though, as I have never tried it.

Good luck.

Ok I was able to get this to work as I needed with some modification to the above code.

Here is what I was trying to accomplish.

I created a plugin that would create sharedGroups (via http request to plugin) based on custom user attributes (set in another plugin) and my requirement was that the sharedGroup show up immediately in the spark roster. I guess you could call it a dynamic roster.

The problem I was having was that the sharedGroup would only appear in the roster if all the members of the group were showing as online. If all members (other than the person creating the sharedGroup) were offline the group would not show up until I restarted Spark. I ended up setting all members to “online” using the below code and it works.

Create the sharedGroup

//set all users in this group to "online" if(!presenceManager.isAvailable(user)) {
    Presence presence = new Presence();
    presence.setPriority(10);
    presence.setFrom(user.getUsername() + "@" + xmppServer.getServerInfo().getXMPPDomain());
    presence.setTo(adminJid);//user we want custom group created for
    presence.setStatus("online");
    presence.setShow(null);
    presence.setType(null);
    user.getRoster().broadcastPresence(presence);
    sessionManager.broadcastPresenceToOtherResources(adminJid, presence);
    presenceManager.userAvailable(presence);
    xmppServer.getPacketRouter().route(presence);
  }
  //Now add the user to the group list.
  group.getMembers().add(new JID(user.getUsername() + "@"
       + xmppServer.getServerInfo().getXMPPDomain()));   rosterManager.getSharedGroups(_userName).add(group);
  Roster roster = rosterManager.getRoster(_userName);
  //Force a Roster IQ packet to this user.
  IQ iq = roster.getReset();
  iq.setType(IQ.Type.set);
  iq.setFrom(adminJid);
  iq.setTo(adminJid);
  iq.setID(String.valueOf(Math.random()));
  try {
      iqRosterHandler.handleIQ(iq);

rcollier I appreciate your responses.!