Project

General

Profile

Is it possible to modify Packet in plugin?

Igor Khomenko
Added almost 5 years ago

I wrote a tigase plugin with this guide http://www.tigase.org/content/writing-plugin-code

Now I need to modify each message packet and add additional params into it.

Is it possible to do in method

@Override
public void process(Packet packet, XMPPResourceConnection session,
            NonAuthUserRepository repo, Queue<Packet> results,
            Map<String, Object> settings) throws XMPPException {

or it should be another way?


Replies (21)

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam almost 5 years ago

Yes it is possible. My suggestion is to look at the Message plugging code. It has simple code with plenty of comments so it should be pretty straightforward. Please note, you can not modify Packet (message) which is passed as argument to the method. Consider Packet object as immutable object. Instead you have to create a new Packet with a copy of the message you want to modify. The copy is then returned in results queue.

Added by Igor Khomenko almost 5 years ago

Thanks Artur,

I looked into Message.java plugin. As I understand it retransmits packets from sender to recipient using this line of code:

results.offer(packet.copyElementOnly());

I have some doubts about your solution.

If I add above line into my plugin - will it mean that recipient will receive 2 messages: origin (processed by Message.java plugin) and modified copy (processed by my plugin)?

Thanks

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Yes, you are correct. This is because Plugins work in parallel. If you want to have only your processed copy passed then you have to disable Message plugin and use only your plugin for processing messages.

Added by Igor Khomenko almost 5 years ago

Yes, but Message plugin is not enabled by default in Tigase 5.2

Instead of it MessagesAmp plugin is enabled by default.

I can't disable MessagesAmp because it does lots of stuff: retransmits packets from sender to recipient, offline storage.

So, according to this info I have a solution, could you please check it:

1) Rewrite my plugin using Message plugin as a basis

2) Fix a bit MessagesAmp plugin, replace links to Message class with my plugin class

Is it a single solution?

Thanks

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Igor Khomenko wrote:

I can't disable MessagesAmp because it does lots of stuff: retransmits packets from sender to recipient, offline storage.

Actually, if you don't care about AMP processing you can disable amp component and enable message and msgoffline plugins which take care of the retransmitting and storing messages to offline users.

Added by Igor Khomenko almost 5 years ago

Thanks,

this sounds great

so, finally, solution is:

1) disable messagesAmp plugin

2) enable msgoffline plugin

3) use message plugin as a basis for your own plugin

4) enable your own plugin, which will work as a message plugin + own logic

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

That is correct.

Added by Igor Khomenko almost 5 years ago

Just a quick question regarding this reconfiguration.

Looks like messagesAmp and msgoffline plugins use different DB tables for offline messages.

As I understand - with this reconfiguration - some offline messages will be lost because msgoffline uses own table and will ignore offline messages from messagesAmp table

Please confirm this

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Yes, as both plugins handle storage/retrieval a bit differently then switching from one to another will cause some messages being lots (well, not delivered to the offline person when he comes back to be precised).

Added by Igor Khomenko almost 5 years ago

Thanks

Added by Igor Khomenko almost 5 years ago

One more question please - if I modify packet this way - does it mean that all components will receive modified copy of a packet as well, e.g. MUC ?

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

Yes, components will only receive the packet that you created in your plugin.

Added by Igor Khomenko almost 5 years ago

I decided to use this solution:

1) disable messagesAmp plugin
2) enable msgoffline plugin
3) use message plugin as a basis for your own plugin 
4) enable your own plugin, which will work as a message plugin + own logic

and now I have one doubt:

As I understand, user will receive a modified packet only if he is online.

If he is offline - OfflineMessages plugin will store original message to the offline storage and this is a problem here.

if this is true - I think we need to modify MessagesAmp plugin and do packet customisation there. After that, this modified packet will go to the Message and OfflineMessages plugins and in both cases (online or offline) user will receive modified packet as expected

Please confirm

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

If you want to modify packet both for outgoing and incomming message then this is indeed problematic. My suggestion would be, to avoid modifying AMP, simply process offline message in your own plugin using OfflineMessgaes plugin (same way AMP is using them).

Added by Igor Khomenko almost 5 years ago

Yes, but as you suggested I need to modify 2 plugins: Message and OfflineMessages

that's a bit complicated cause we need to share same code base for packet modification for 2 plugins

Wouldn't be easier to modify only MessagesAmp class?

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

No, I suggested using parts of OfflineMessages only for the purpose of storing/retrieval messages, and after retrieval you can modify packets before putting them into results queue.

Added by Igor Khomenko almost 5 years ago

One more question please regarding Plugin-Component interaction.

You said that Components will only receive the packet that I created in my plugin.

Does it mean also that if I drop an incoming packet in my plugin - it won't be delivered to all components?

Thanks

Added by Wojciech Kapcia TigaseTeam almost 5 years ago

If you drop packet in your own plugin (and there is no other plugin for that packet) then yes - this would mean the packet is gone.

Added by Julia Zashchitina about 4 years ago

Hello.

We have the same need to modify packet message before delivering it to recipient. Could you please explain why it is better to avoid modifying MessageAmp.java?

Added by Wojciech Kapcia TigaseTeam about 4 years ago

Julia Zashchitina wrote:

Hello.

We have the same need to modify packet message before delivering it to recipient. Could you please explain why it is better to avoid modifying MessageAmp.java?

It's explained in the Packet javadoc

Please note! Even though the Packet object and carried the stanza Element is not unmodifiable it should be treated as such. This particular Packet can be processed concurrently at the same time in different components or plugins of the Tigase server. Modifying it may lead to unexpected and hard to diagnose behaviors. Every time you want to change or update the object you should obtaina a copy of it using one of the utility methods: copyElementOnly(), swapFromTo(...), errorResult(...), okResult(...), swapStanzaFromTo(...)

Added by Julia Zashchitina about 4 years ago

Thank you for the fast reply.

    (1-21/21)