Sending Message From Plugin

Hey guys,

Right now I’m developing a plugin that reads a message from a user and sends a new message back to the user automatically. Both users will essentially be computers and will not have any human interaction.

Right now my plugin implements Plugin and PacketInterceptor. I use the PacketInterceptor to parse the message.

I am using

XMPPServer.getInstance().getMessageRouter().route(Message m)

To send a message back to the client, but the message never gets sent. I noticed in the Broadcast plugin they implement Component and use componentManager.sendPacket() but the Component interface implementation requires a lot of extra code and I am not sure what is needed and what isn’t.

What interfaces do I need to be able to automatically send messages between two users and which methods should I use to do this?

Thanks,

Max

Hey Max,

I would guess that the problem is in the TO (and may be also in the FROM) values from the message you are trying to send. You can debug the code and see follow the flow to figure out what is going on.

Regards,

– Gato

I’ve been experimenting with that so far. Right now what I am doing is using the JID of the originator as the TO value, so let’s say max@myhost/Customer sends a message, I send one back to max@myhost/Customer and I set the FROM value to be ‘myhost’ but I’ve also tried setting it to the JID of the person max@myhost/Customer is sending the message to (which is just one user that handles all these special messages). I’m not getting any errors or debug output related to the routing system either, so I do not know if the issue is with that or not.

This does work for me:

private XMPPServer xmppServer;
private PacketRouter packetRouter; xmppServer = XMPPServer.getInstance();
packetRouter = xmppServer.getPacketRouter();     public void sendMessage(Message source, String body) {
        Message packet = new Message();
        packet.setType(source.getType());
        packet.setThread(source.getThread());
        packet.setFrom(source.getTo());
        packet.setTo(source.getFrom());
        packet.setBody(body);
        packetRouter.route(packet);
    }

**Be careful: **PacketInterceptor will intercept your own packages, too! This can lead into an endless recursion.

I agree with coolcat - I have done similar sending of messages from my plugin. The one question I have is that I have not been able to successfully send these messages unless the to and from fields have been set to the JIDs of the two parties in the original message. I would love to be be able to set the from field on my plugin generated message to indicate it is coming from the server/plugin rather than as if it is being received from the other user.

Is this possible?

Is this possible?
Sure. Plugins are trusted by the server, you can set the from-field to whatever you want. You can even fake messages from local users, if you find this useful. However, the JID you specify there should exist and be local.

Thanks guys. What was happening was my code was rejecting the message I sent using PacketRejectedException. Now I get the messages I am sending. For parsing messages, is PacketInterceptor better than Component?

-max

For parsing messages, is PacketInterceptor better than Component?
A PacketInterceptor does intercept all packets, a Component does only intercept packets that are addressed to your component. So it depends on what you want to do.