#!/usr/bin/perl -w use strict; use warnings; use lib "./lib"; use Net::YMSG qw(:standard); scalar(@ARGV) == 2 || die "Usage: $0 "; print "Creating object\n"; my $yahoo = new Net::YMSG ( YahooID => lc($ARGV[0]), # TODO: module should lc the ID Password => $ARGV[1], Debug => 1, ANSI => 1, ); print "Object created\n"; print "Setting handlers\n"; $yahoo->setHandlers ( Connected => \&on_connect, List => \&on_list, NewContact => \&on_new_contact, Message => \&on_message, Typing => \&on_typing, Status => \&on_status, ); print "Daring to connect\n"; $yahoo->connect(); print "Entering the main loop\n"; while (1) { $yahoo->do_one_loop(); select(undef,undef,undef,0.01); } sub on_connect { my $self = shift; print "Connected to YMSG!\n\n"; $self->sendMessage ("kirsle", "I'm alive!"); $self->sendMessage ("kirsle", ""); } sub on_list { my ($self,$buddies,$groups) = @_; print "We've received our buddy list! We're friends with:\n" . join (", ", @{$buddies}) . "\n"; } sub on_new_contact { my ($self,$from) = @_; print "$from wants to be our friend!\n"; # Add him to our list! $self->addBuddy($from, "Buddies"); } sub on_message { my ($self,$from,$message) = @_; print "[$from] $message\n"; # Some commands. if ($message =~ /!away (.+?)$/i) { $self->setStatus (YMSG_STATUS_BRB, $1); $self->sendMessage ($from, "Status changed!"); return; } elsif ($message =~ /!online/i) { $self->setStatus (YMSG_STATUS_ONLINE); $self->sendMessage ($from, "Status changed!"); return; } $self->sendTyping ($from, YMSG_TYPING_STARTED); sleep 2; $self->sendMessage ($from, "[I am a robot] You said: $message"); $self->sendTyping ($from, YMSG_TYPING_STOPPED); } sub on_typing { my ($self,$from,$typing) = @_; if ($typing) { print "$from has started typing.\n"; } else { print "$from has stopped typing.\n"; } } sub on_status { my ($self,$from,$status,$message) = @_; # Translate the status. $status = $self->statusToString($status); print "$from is $status: $message\n"; }