This Question is Answered (go to answer)

1 "helpful" answer available (5 pts)
1,206 Views 11 Replies Last post: Dec 10, 2008 8:08 AM by Coolcat RSS
chiodi84 Bronze 10 posts since
Dec 9, 2008
Currently Being Moderated

Dec 9, 2008 2:02 AM

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");
 }
 }
 }
 }
 }
Tags: plugins, message, packetinterceptor, body
Coolcat KeyContributor 784 posts since
Mar 19, 2007
Currently Being Moderated
Dec 9, 2008 2:23 AM in response to: chiodi84
Re: replace message body in packetinterceptor

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

Coolcat KeyContributor 784 posts since
Mar 19, 2007
Currently Being Moderated
Dec 9, 2008 2:45 AM in response to: chiodi84
Re: replace message body in packetinterceptor

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.

Coolcat KeyContributor 784 posts since
Mar 19, 2007
Currently Being Moderated
Dec 10, 2008 4:02 AM in response to: chiodi84
Re: replace message body in packetinterceptor

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#element[Text](String) seems to be correct.

Ryan Graham KeyContributor 1,877 posts since
Jan 17, 2003
Currently Being Moderated
Dec 10, 2008 6:38 AM in response to: chiodi84
Re: replace message body in packetinterceptor

Hi Paolo,

 

This isn't a bug. When you use the #setBody method it will update the text within the message <body> element. The <html> 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 <body> elements that could contain different text, so if the #setBody method were to replace the text in all <body> elements there could be some unpredictable behavior. For instance, in theory there could be a message packet that looked something like the following:

 

<message to="jack@example.com" from="jill@example.com">

   <x xmlns='example:x:person'>

      <person>

         <body>average</body>

         <hair>blond</hair>

      </person>

      <person>

         <body>trim</body>

         <hair>red</hair>

      </person>

   </x>

</message>

 

If the #setBody method was called and it were to update all the <body> 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

Coolcat KeyContributor 784 posts since
Mar 19, 2007
Currently Being Moderated
Dec 10, 2008 8:08 AM in response to: chiodi84
Re: replace message body in packetinterceptor

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.

More Like This

  • Retrieving data ...

Bookmarked By (0)