<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>XMPP on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/xmpp/</link>
    <description>Recent content in XMPP on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 25 May 2010 19:46:21 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/xmpp/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Google Talk Status Countdown</title>
      <link>https://mpcabd.xyz/google-talk-status-countdown/</link>
      <pubDate>Tue, 25 May 2010 19:46:21 +0000</pubDate>
      <guid>https://mpcabd.xyz/google-talk-status-countdown/</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Since I know that Google Talk uses &lt;a href=&#34;http://www.xmpp.org&#34;&gt;XMPP&lt;/a&gt; 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:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger.png&#34; rel=&#34;lightbox[231]&#34; title=&#34;XMPP Status Changer&#34;&gt;&lt;img src=&#34;http://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger-300x190.png&#34; alt=&#34;XMPP Status Changer&#34; title=&#34;XMPP Status Changer&#34; width=&#34;300&#34; height=&#34;190&#34; class=&#34;alignnone size-medium wp-image-234&#34; srcset=&#34;https://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger-300x190.png 300w, https://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger.png 484w&#34; sizes=&#34;(max-width: 300px) 100vw, 300px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>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.</p>
<p>Since I know that Google Talk uses <a href="http://www.xmpp.org">XMPP</a> 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:</p>
<p><a href="http://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger.png" rel="lightbox[231]" title="XMPP Status Changer"><img src="http://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger-300x190.png" alt="XMPP Status Changer" title="XMPP Status Changer" width="300" height="190" class="alignnone size-medium wp-image-234" srcset="https://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger-300x190.png 300w, https://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger.png 484w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<h1 id="the-basic-idea-behind-status-changing">The basic idea behind status changing</h1>
<p>Since Google Talk uses <a href="http://www.xmpp.org">XMPP</a> 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.</p>
<p>I searched to see if there were an API to communicate with Google Talk from any other program but I had no luck.</p>
<p>So the solution was to use XMPP’s Presence Priority attribute.</p>
<p>In <a href="http://www.xmpp.org">XMPP</a>, 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.</p>
<h1 id="programming-it">Programming it</h1>
<p>I used Java for developing the program. There is no direct reason for using Java but that’s what I used 🙂</p>
<p>To be able to use XMPP I used the <a href="http://www.igniterealtime.org/downloads/index.jsp">Smack XMPP Library</a> because it is simple and serves me well.</p>
<p>The whole mechanism is too simple:</p>
<ol>
<li>Login.</li>
<li>Calculate difference between now and the targeted date.</li>
<li>Send the presence.</li>
</ol>
<p>Using Smack makes the whole process so easy.</p>
<h2 id="login">Login</h2>
<pre class="lang:java decode:true">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.
}
</pre>
<h2 id="calculate-difference-between-now-and-the-targeted-date">Calculate difference between now and the targeted date</h2>
<p>This process is done using Java Calendar and Date objects:</p>
<pre class="lang:java decode:true">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);
}
</pre>
<p>This code calculates the difference between the two dates in days, hours and minutes.</p>
<h2 id="send-the-presence">Send the presence</h2>
<p>After calculating the difference all we have to do is to send the presence:</p>
<pre class="lang:java decode:true">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);
}
</pre>
<p>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 ;)).</p>
<h1 id="testing">Testing</h1>
<p><a href="http://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger2.png" rel="lightbox[231]" title="XMPP Status Changer Test"><img src="http://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger2-300x127.png" alt="XMPP Status Changer Test" title="XMPP Status Changer Test" width="300" height="127" class="alignnone size-medium wp-image-242" srcset="https://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger2-300x127.png 300w, https://mpcabd.xyz/wp-content/uploads/2010/05/XMPPStatusChanger2.png 493w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<h1 id="last-note">Last note</h1>
<p>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.</p>
<p>And BTW, you can use this program with any XMPP account, not just Google Talk. And it’s cross platform 😉</p>
<h1 id="download">Download</h1>
<p>Google Talk Status Countdown on <a href="https://github.com/mpcabd/gtalk-status-countdown">GitHub</a></p>
<p>Smack library is attached to the files already, no need to download it separately.</p>
<ul>
<li><a href="https://github.com/mpcabd/gtalk-status-countdown/tarball/master">Source Code</a></li>
<li><a href="https://github.com/downloads/mpcabd/gtalk-status-countdown/statusChangerExecutable.tar.gz">Executable Jar File</a></li>
</ul>
<p>Hope you use it 🙂</p>]]></content:encoded>
    </item>
  </channel>
</rss>
