Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smack- create entry in roster

I know that this question has been asked before, but only a partial response has been given by mschonaker here. On my website, user can add people to their roster, then the buddy has to accept and finally they are connected. The first person (user a) use the famous

roster.createEntry(jid, name, groups);

which works and add an entry in his roster, but then I am a bit confused to what to do:

  • how do I receive the request on the other end? I tried implementing a PacketListener, override processPacket() and check for packet which types are Presence.Type.subscribe or Presence.Type.subscribed, but it appears that it is only triggered for the user a, but not the one that should listen for subscriptions- user b.

  • then, I have another function that can lookup all requests at login, so if I login again I will see the request, but how do I accept it? at first, I thought that the user b should also add user a in his roster by roster.createEntry(jid, name, groups);

but that didn't work and nothing was happening. I also tried to do

Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(jid);
xMPPConnection.sendPacket(subscribed);

but didn't work either. I'm sure there must be a good and simple way to do it, but I haven't found it so far anywhere, and trying one thing at a time gave me too many headaches. Does anyone know the correct flow for this? thanks in advance!

like image 919
Guillaume Avatar asked Jul 11 '11 14:07

Guillaume


1 Answers

From the Smack documentation: Rosters and presence use a permissions-based model where users must give permission before they are added to someone else's roster. This protects a user's privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry it will be in a pending state until the other user accepts your request. If another user requests a presence subscription so they can add you to their roster, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:

Automatically accept all presence subscription requests.
Automatically reject all presence subscription requests.
Process presence subscription requests manually. 

The mode can be set using the Roster.setSubscriptionMode(Roster.SubscriptionMode) method. Simple clients normally use one of the automated subscription modes, while full-featured clients should manually process subscription requests and let the end-user accept or reject each request. If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of Presence.Type.subscribe.

So, try setting Roster's subscription mode to manual, and then implement PacketListener to listen for Presence.Type.subscribe. Once a packet is received, create a new Packet with Presence.Type.subscribed and send it to the sender.

like image 86
Maggie Avatar answered Sep 20 '22 14:09

Maggie