Project

General

Profile

Send packet from outside of any context

Daniele Ricci
Added about 2 years ago

Hello Tigase team,

I have a timer with a runnable running repeatedly. This runnable needs to check for last logout of a user and delete it if expired. Before deleting it, I'd like to send an unsubscribed presence to all its contacts in the roster.

I was able to read the roster successfully, but since I'm inside a timer (started in a plugin) and I'm not inside any of the plugin's method such as process, preProcess, etc. I had an idea:

SessionManager sessMan = (SessionManager) XMPPServer.getComponent("sess-man");
sessMan.addPacketNB(Packet.packetInstance(Presence.ELEM_NAME,
    jid.toString(), user.getBareJID().toString(), StanzaType.unsubscribed));

Where jid is the user being deleted and user is the contact currently being iterated from the roster.

This way produces a feature-not-implemented packet being sent back to the sender (the user being deleted):

2017-01-04 21:51:41.132 [in_0-message-router]  MessageRouter.processPacket()  FINEST:   Processing packet: from=sess-man@notebook.casaricci.it, to=null, DATA=<presence type="error" from="4bdd4f929f3a1062253e4e496bafba0bdfb5db75@prime.kontalk.net" to="d96b6d3ba60e3714e98a094990bb6275e83ab46b@prime.kontalk.net"><error code="501" type="cancel"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">The feature is not supported yet.</text></error></presence>, SIZE=378, XMLNS=null, PRIORITY=NORMAL, PERMISSION=AUTH, TYPE=error

Because the packet is processed by the default packet handler which has a check for null resource (because I'm sending to a bare JID) that triggers this feature-not-implemented response.

I wonder if this is a wrong way of using Tigase classes or it's simply a bug. Some help would be greatly appreciated.

Thanks!


Replies (2)

Added by Andrzej Wójcik IoT 1 CloudTigaseTeam about 2 years ago

You can use classes in this way. It is not a great solution (as you need to access component from timer started from processor) but it will work.

There are 2 issues with your soution:

  • you should use addOutPacket() method of SessionManager as you are sending outgoing presence packets

  • you need to set XMLNS of a presence packet. In your case it is not set, which triggers processing by PacketDefaultHandler instead of processing by PresenceSubscription processor due to XMLNS not being set.

Added by Daniele Ricci about 2 years ago

Andrzej Wójcik wrote:

  • you need to set XMLNS of a presence packet. In your case it is not set, which triggers processing by PacketDefaultHandler instead of processing by PresenceSubscription processor due to XMLNS not being set.

That was it! I totally forgot to do it. It work perfectly now, thank you!!

    (1-2/2)