Abdullah Diab’s Blog

Google Talk Status Countdown

A few days ago I was counting down to a presentation in my faculty. As I was setting counters all over my world (Side Bar Gadget, To Do List) I thought of a way to change my Google Talk status to a counter also.

Since I know that Google Talk uses XMPP protocol, and since my friends and I implemented XMPP last year in our faculty I tried to make a program to set my status to a counter; and here is the result:

XMPP Status Changer

The basic idea behind status changing

Since Google Talk uses XMPP then if you can connect using an XMPP client to your Google account you can use the client instead of Google talk. But indeed that’s not what we want here, we want to use Google Talk while changing the status automatically.

I searched to see if there were an API to communicate with Google Talk from any other program but I had no luck.

So the solution was to use XMPP’s Presence Priority attribute.

In XMPP, a user can connect from multiple clients with the same ID, each client will have a unique `resource` so messages sent from one client get their replies to the same client. But what about status? XMPP has an attribute sent with each status (Status is called Presence in XMPP) which is `Priority`, this attribute describes the priority of this resource presence. In Google Talk the highest priority is 24. So if you can change the priority of the automatic presence to 24 it will be considered high priority and its status will appear instead of the old one.

Programming it

I used Java for developing the program. There is no direct reason for using Java but that’s what I used 🙂

To be able to use XMPP I used the Smack XMPP Library because it is simple and serves me well.

The whole mechanism is too simple:

  1. Login.
  2. Calculate difference between now and the targeted date.
  3. Send the presence.

Using Smack makes the whole process so easy.

Login

import org.jivesoftware.smack.XMPPConnection;

public void connect() {
    XMPPConnection connection = new XMPPConnection(server); //Server is gmail.com for Google Talk.
    connection.connect();
    connection.login(username, password); //Username and password.
}

Calculate difference between now and the targeted date

This process is done using Java Calendar and Date objects:

import java.util.Calendar;
import java.util.Date;

{
        Calendar calendar1 = Calendar.getInstance();
        Date d = new Date();
        calendar1.setTime(d);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(endLine); //End line is the date we're counting to.

        long milliseconds1 = calendar1.getTimeInMillis();
        long milliseconds2 = calendar2.getTimeInMillis();
        long diff = milliseconds2 - milliseconds1;

        long diffDays = diff / (24 * 60 * 60 * 1000);
        diff = diff % (24 * 60 * 60 * 1000);

        long diffHours = diff / (60 * 60 * 1000);
        diff = diff % (60 * 60 * 1000);

        long diffMinutes = diff / (60 * 1000);
        diff = diff % (60 * 1000);
}

This code calculates the difference between the two dates in days, hours and minutes.

Send the presence

After calculating the difference all we have to do is to send the presence:

import org.jivesoftware.smack.packet.Presence;

{
         String remaining = Long.toString(diffDays) + " day(s), " + Long.toString(diffHours) + " hour(s), " + Long.toString(diffMinutes) + " minute(s) " + message; //Message is usually: Until "something".

        Presence presence = new Presence(Presence.Type.available);
        presence.setStatus(remaining);
        presence.setPriority(24); //Highest priority in Google Talk
        presence.setMode(presenceMode); //This is one of XMPP modes (Available, Chat, DND, Away, XA).
        connection.sendPacket(presence);
}

After this point people will see your new status instead of the one in Google Talk. (Notice that you won’t be able to see the change inside Google Talk but rest assured it is changed ;)).

Testing

XMPP Status Changer Test

Last note

The GUI is self explanatory, just make sure you enter a big value for “Update Interval” because when you’re talking to others and your status changes they’ll have a red line with the new status in the conversation window and certainly you don’t want them to be distracted by the counter from the real conversation.

And BTW, you can use this program with any XMPP account, not just Google Talk. And it’s cross platform 😉

Download

Google Talk Status Countdown on GitHub

Smack library is attached to the files already, no need to download it separately.

Hope you use it 🙂