A P2P AIM Robot in Perl | 2 | WebReference

A P2P AIM Robot in Perl | 2

A P2P AIM Robot in Perl

Gaim and Net::AIM

AIM on Linux is Gaim

Some of the sysadmins here at Whirlwind recently pushed me into trying out Gaim. It's an AIM client for Linux which is available over at http://www.marko.net/gaim/. Well, the verdict is... it's a pretty cool app. Ok I admit it. But it's still annoying I think compared to IRC. IM people seem to be a lot pushier that IRC folks. They want an answer right away. In any case, the source distribution came with a little file called PERL-HOWTO in the plugins/ directory. Inside were some goodies describing how to write Gaim plugins in Perl. Neat, sorta like X-Chat (IRC). In fact, if you've ever written an X-Chat Perl script, the syntax is very similar.

The distribution also includes the Net::AIM module in the plugins/ directory which enables you to write Perl scripts that act as AIM clients. Oooh! It includes a sample script that will login to the AIM network and send a random message in response to any message you send.

Installing Net::AIM

The Net::AIM module is available from CPAN, or grab the one that comes with Gaim. You'll need perl 5.004 or greater and the IO::* modules. If you're using a newer version of Perl, you may already have them installed.

I've only tested Net::AIM on a Linux box, but I don't see a reason why it wouldn't work on NT. If someone gets it working on a win32 box, please let me know.

Writing a Simple AIM client

First, a little bit about the Net::AIM module. There isn't any documentation with the module itself, so you'll either have to use the example script to figure things out or read the code. Next, the module does things based on events, so if you want to do anything, you'll need to register subroutines for those events. The first thing you want to do in a script is load the module into memory and create a new instance, connect to the AIM network supplying a valid username and password. if you don't have one, goto http://aim.aol.com.

So let's start by writing a simple script that connects to the AIM network and sends a message to me:

1  use Net::AIM;
2  my $nick = "motherofperlbot";
3  my $aim = new Net::AIM;
4  $aim->debug(1);
5  my $conn = $aim->newconn(Screenname   => 'motherofperlbot',
6  			   Password     => 'motherofperl')
7      or die "Can't connect to AIM server.\n";
8  $conn->add_handler('config',    \&on_config);
9  $aim-start;
11 sub on_config {
12     my ($self, $event) = @_;
13     my ($str) = $event->args;
14     $self->set_config($str);
15     $self->send_im($nick, 'testing...');
16 }

The start method on line 9 initiates the connection with the AIM network and puts itself into an infinite loop waiting for new events. As events are recognized, they're handed off to the subroutines that were registered for the event. It's not required to have a subroutine for each event, though you won't get too far without at least handling config. The first handler that the Net::AIM module usually calls after it connects to an AIM server is config. This sets configuration information in the client that was passed by the AIM server. On line 8, we registered the &on_config subroutine to execute when the config event is called by the module. The subroutine is contained in lines 11-16.

Line 15 in &on_config sends the message to the user specified on line 2. The send_im method sends a message to an AIM user. The first argument is the nick of the AIM user and the second argument is the message itself.


Produced by Jonathan Eisenzopf
All Rights Reserved. Legal Notices.
Created: September 28, 2000
Revised: September 28, 2000

URL: http://www.webreference.com/perl/tutorial/13/2.html