Fun with Instant Messaging in Ruby

14 Jan 2008

While i was going through Redmine's source code, trying to fix some email notification bugs, I felt it would be good if instant messenger notification was also available.So i decided to give it a try. I was sure it would be cool if i could write my own programs to communicate with my IM friends, even though it may not be very 'useful' :)

  1. How to connect and communicate with IM servers.

    XMPP/Jabber protocol is used for interaction with IM servers. Ruby has a xmpp4r library. I also found a xmpp4r-simple library which was built on top of xmpp4r, very easy to use and would serve my purpose. So i chose xmpp4r-simple.

    sudo gem install xmpp4r sudo gem install xmpp4r-simple

  2. Connecting to GoogleTalk

Since Gtalk supports Jabber, i decided to do my experiments using Gtalk servers.

I started off with a program that connects to google talk, authenticates and sends "Hello" to a predefined user.

chat0.rb

(Replace the username, password and destinationusername in the code with with actual ones. Login to destinationusername' in an IM to see our code running)

3. My first chat program - autoreply

A program that will simply send a predefined message in reply to every message it receives from any user. May be when you are busy, instead of setting status as "busy", you can simply run this program, so that it sends some appropriate reply automatically.

auto_reply.rb

  1. Echo Chat

Program that will simply send back whatever message it receives. Sure this will baffle your IM buddies :)

echo_chat.rb

  1. Normal Chat

normal_chat.rb

User1 sends a message,user2 responds, again user1, then user2 and so on. But if user1 wants to send a message before user2 responds, its not possible here since its a single threaded program, which will block,waiting for user2's response.

Using 2 separate threads for listening and sending solves this issue.

chat_final.rb

6. A remote shell

Well, my next idea was to simulate a remote shell over IM. ie To run commands on a remote machine and get the results through IM.

remote_shell.rb

How good it be, if you could say "geyser ON" on your IM, on your way home, which turns on the geyser at your home, so that you have warm water ready by the time you reach home? :)

Theoretically, we can do it like this:

i. Connect the geyser to your parallel port (ofcourse not directly, there should be some relay/switching device in between), so that a TTL HIGH on one of the parallel port pins, turns on the Geyser.

ii. Write a simple C program named "geyser"(or might be in Ruby itself, i am not sure if we can do it in Ruby), which talks to the parallel port. On running this program with argument ON, it should produce HIGH on one of the parallel port pins, and OFF should make that pin LOW, so that we can use this program to turn the geyser ON and OFF.

iii. Now we can run this "geyser" program through our "IM remote shell". So you say "geyser ON" in your IM, it turns the geyser ON.

  1. IM based dictionary service

You wanted to know the French equivalent for the word 'yesterday'. You have an IM called 'french-bot' , you just send him the word "yesteday" through your IM and he (or she - as you wish) immediately sends you back the French word for "yesterday". Wont it be cool?

Here is your French-Bot :)

french_bot

Thats all for now. Playing with IM was really fun.