Get the username in the packet interceptor

Hi,

I use the packet interceptor and I want to get the username for a Message type packet.

what can I do?

thanks

Message was edited by: brasco

Hi brasco,

I’‘m not exactly sure what you mean by get the username but I’‘ve included a sample interceptor that gets both the recipent and sender JID’'s

private class SamplePacketInterceptor implements PacketInterceptor {

public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {

if ((packet instanceof Message) && !processed) {

Message presencePacket = (Message) packet;

JID toJID = presencePacket.getTo();

JID fromJID = presencePacket.getFrom();

}

}

}

/code

Please note that the “XMPP protocol often makes the “from” attribute optional, so it does not always need to be set.”

Hope that helps,

Ryan

Hi

Yes you understand well.

I already make a similar code:

Message message = (Message)packet;

String mess = message.getBody();

String name = message.getTo().toString();

After I want to write in a file the history of the chat with the name and the message:

the string name give me username@namePC.com

I think I must use the regular expression to pick ou just the name

I’'m interested to have many others information.

Can I have the IP adress of the users?

the operatinf system version and the client version?

thanks for the help

Hi Brasco,

the string name give me username@namePC.com

I think I must use the regular expression to pick ou just the name

Or, you could just do:

String name = message.getTo().getNode();

Can I have the IP adress of the users?

Sure, something like the following would work:

String address = XMPPServer.getInstance().getSessionManager().getSession(message.getFrom()).getC onnection().getInetAddress().getHostAddress();

the operatinf system version and the client version?

I don’‘t believe there is a way to get the clients OS but you could get the client version by sending it an Version IQ packet (jabber:iq:version). Sorry I don’'t have any code examples on how this is done but you should be able to find something in the Wildfire/Spark code/javadocs.

Hope that helps,

Ryan

1 Like

Thanks Ryan for your reply

Message was edited by: brasco