Replace message body in packetinterceptor

hi all.

i’m trying to create a packetinterceptor plugin.

just for a test i’d like to replace the body of all chat messages to the string “test”. i’ve simply tried to do message.setBody(“test”) but it doesnt work: i receive always the original body.

the packet interceptor is correctly registered and receiving notifications.

thank you,

Paolo

ps:

this is my code:

public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException { if(packet instanceof Message) { Log.error("TD Message"); if(((Message)packet).getType() == Message.Type.chat) { if(!processed) { Log.debug("TD Body: " + ((Message)packet).getBody()); if(((Message)packet).getBody() != null) { ((Message)packet).setBody("test"); Log.debug("TD Changing"); } } } } }

I did a quick test and your code is working for me.

thanks for the quick answer…

may be the client? i’m using pidgin

Paolo Chiodi

I’m using Psi 0.12. However, as long as Pidgin is sending type=“chat” I don’t see any problem.

Just to be sure:

You are aware of that you only can affect the message that the recipient gets? You can not modify the message which the sender sees in his chat history, since this message is not going through the server.

i’ve done some more test. it seems that the problem is about pidgin

i’m using pidgin v 2.4.2 and openfire v 3.6.0

if i use psi for sending and either psi or pidgin to receive my code works ok.

i’l do some deepier test to see why is this happening and if it has been solved with newer version of openfire

this is the message received from a pidgin client.

<message type="chat" id="purple3816c2e2" to="receiver@myserver.it" from="pidgin_sender@myserver.it/Home">
<x xmlns="jabber:x:event"><composing/></x>
<active xmlns="http://jabber.org/protocol/chatstates"/>
<body>test</body>
<html xmlns="http://jabber.org/protocol/xhtml-im">
<body xmlns="http://www.w3.org/1999/xhtml">original message</body>
</html>
</message>

it seems that message.setBody modify only the first body tag instead modifying also the one that pidgin create.

i’ don’t know if the pidgin way is standard compliance but it seems that pidgin create two alternative bodies: a standard text body and an html body. openfire read and set the text body while clients (either psi or pidgin) still read the html body.

hope this help

Paolo Chiodi

should be issued as a bug?

Paolo

Hm, maybe. I don’t see why this does happen.

Code in org.xmpp.packet.Message#{get|set}Body(String) looks correct. Also

org.dom4j.tree.AbstractElement#elementText seems to be correct.

sorry, but i have just given a look at the code and it is not correct (as of released 3.6.2 version source)

setBody only update the first “body” element it encounters, adding a new one if it doesn’t exist.

the problem is that pidgin create two element: the first, “body”, contains the text only version; the second is an “html” element that contains another “body” element which is the html version.

Paolo Chiodi

ps: how can i post a new issue on the bug tracker?

Hi Paolo,

This isn’t a bug. When you use the #setBody method it will update the text within the message element. The portion of the message is considered a child element and shouldn’t be updated when the #setBody method is used. Message packets, depending on how they’re being used could have multiple nested elements that could contain different text, so if the #setBody method were to replace the text in all elements there could be some unpredictable behavior. For instance, in theory there could be a message packet that looked something like the following:

average

blond

trim

red

If the #setBody method was called and it were to update all the tags with something like “Hello” it wouldn’t make much sense and could cause problems.

In your packet interceptor you will have to look to see if each message packet has the “html” element with the “http://jabber.org/protocol/xhtml-im” namespace and if it does make the appropriate update.

Hope that helps,
Ryan

thank you Ryan.

i was supposing that the setBody could edit everything that is “the body” of a message, or the existing of a {set/get}HtmlBody.

maybe the problem is my knowledge (or lack of) about the xmpp protocol

Paolo Chiodi

You can access the full packet by using packet.getElement()

The bug I see is that get/setBody() does not see an body-Tag, if it contains a namespace. Probably a bug in dom4j. However, I have not much time at the moment, so it’s possible I missed something.