Thursday, November 12, 2009

GTalk status message fun!!!

Gtalk uses Jabber protocol for its communcations. So, by accessing the server talk.google.com through this protocol, lot of things can be done like sending mails, sending IM and one of the coolest things that can be done is changing status messages. This is one such example where one can dynamically change status message programmatically.

This uses Smack API for XMPP (jabber, all the same ) communications. Download smack and add smack.jar and smackx.jar to the classpath while executing. Go, have fun....

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

public class statusChange
{
public static void main( String [] args )
{
ConnectionConfiguration config = new ConnectionConfiguration( "talk.google.com" , 5222 , "google.com" );
XMPPConnection connection = new XMPPConnection(config );
Presence presence;
String status;

try
{
connection.connect();
connection.login("username@gmail.com", "password");
status = "I Me and Myself ";

presence = new Presence( Presence.Type.available, status, 24, Presence.Mode.available);
while( true )
{
status = set(status);
presence.setStatus(status);
connection.sendPacket(presence);
Thread.sleep(1000);
}

}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
connection.disconnect();
}
}
private static String set(String input)
{
return input.substring(1) + input.charAt(0);
}
}

PS...

1) When the gtalk user changes the status message, Gtalk adds it to the chat window and it happens every time. So, person who is chatting with you, might get irritated.

2) The status message changes only when the applciation is running, ( as long as the presence packets are being sent). Once it terminates, the previous status message ( the one before the launch of the application ) is retained.


 

3) The dynamics of the status cannot be experienced in the same account. Check into your pal's talk client to enjoy it!


 

4) With your username add "@gmail.com" else the server throws an error.