#!/usr/bin/winperl use warnings; use strict; use Net::AIM; my $aim = Net::AIM->new(); $aim->newconn( Screenname => "TestAIMBot", Password => "TestAIMBot's Password", ); my $conn = $aim->getconn() or die "Can't connect to AIM server.\n"; $conn->set_handler('im_in', \&on_im); $aim->start; sub on_im { my ($self, $evt, $from, $to) = @_; my ($nick, $auto, $msg) = @{$evt->args()}; # Normalize removes non-alpha-numeric characters. # Note that if you don't remove non-word characters, # opening a file based on the username becomes a # security risk. Be careful. my $filename = $self->normalize($from) . '.emo'; my $count = readcount($filename); # will match 'sorry', 'srry', 'sry', and 'sory' # Note that it's not anchored to the start of # the string, as the word 'sorry' may appear # anywhere in the message. Change if desired. if($msg =~ /so?r?ry/) { # Increase and save the count. writecount($filename, ++$count); } $self->send_im($from, "You were sorry $count times."); } sub readcount { my $filename = shift; # If the file doesn't already exist, we will # simply return a count of 0 and let writecount() # create the file later on. open my $fh, "< $filename" or return 0; my $count = <$fh>; close $fh; return $count; } sub writecount { my $filename = shift; my $count = shift; # If the file doesn't exist, this will create it. open my $fh, "> $filename" or die "Can't open $filename for write: $!\n"; print $fh $count; close $fh; }