http://qs1969.pair.com?node_id=183586
Category: Miscellaneous
Author/Contact Info Sean Murphy (mrmaam-at-subzion.com)
Description: This is a Google Search AIM bot. You send IMs to it and it returns the first 5 results of what you send it. You must have Net::AIM, the Google Dev Kit, and an AIM screenname.
use Net::AIM;
use SOAP::Lite;

my $key   = "";  # Your Google Dev Kit key
my $query = "";

 *SOAP::XMLSchema1999::Deserializer::as_boolean =
 *SOAP::XMLSchemaSOAP1_1::Deserializer::as_boolean = 
\&SOAP::XMLSchema2001::Deserializer::as_boolean;

my $service = SOAP::Lite -> service('file:./GoogleSearch.wsdl'); # Ref
+ers to the Google Perl API

print "Initializing.. please wait..\n";

$debug = 1;       # Whether debug messages are printed
$commands = 1;    # Whether you can use the non-search commands
$screenname = ""; # The AIM screenname to login to
$password = "";   # The password to the screenname

if (-e "logs" ne 1) { mkdir "logs"; }
if (-e "logs/$screenname" ne 1) { mkdir "logs/$screenname"; }
if (-e "logs/$screenname/temp" ne 1) { mkdir "logs/$screenname/temp"; 
+}
if (-e "logs/$screenname/saidtemp" ne 1) { mkdir "logs/$screenname/sai
+dtemp"; }

use NET::AIM;
use LWP::Simple;
$aim = new Net::AIM;

use File::stat;

$aim->newconn( Screenname => $screenname, Password => $password ) or d
+ie "Can't connect to AIM server.\n";

my $conn = $aim->getconn();
$conn->set_handler('config',   \&on_config);
$conn->set_handler('im_in',    \&on_im);
$conn->set_handler('error',    \&on_error);
$conn->set_handler('eviled',   \&on_evil);

$aim->start;

sub on_evil {
    my $reply;
    my ($self, $event) = @_;
    my ($victim, $friend, $msg) = @{$event->args()};
    $victim = $event->from;
    $victim = lc($victim);
    $victim =~ s/\s//g;

    if ($temp ne "") { $victim = "$temp"; }
    if ($victim eq "") { } else {
        if ($victim ne "") {
            $aim->evil($victim, 0);
            sleep(4);
            $aim->evil($victim, 0);
            $aim->add_deny(1, "buddies", $victim);
        }

        if (-e "logs/$screenname/idiots.html") {
            open (DATA, ">>logs/$screenname/idiots.html") || &fatal("$
+!");
            print DATA ",$victim";
            close(DATA);
        } else {
            open (DATA, ">logs/$screenname/idiots.html") || &fatal("$!
+");
            print DATA "$victim";
            close(DATA);
        }
    }
}

sub on_config { 
    my ($self, $event) = @_;
    print "\nSucessfully started..\n\n";
    $aim->set_info("<font face=\"Verdana\" size=\-1\"><b>&lt;plug&gt;V
+isit <a href=\"http://www.perlmonks.org/\">PerlMonks.Org</a>&lt;/plug
+&gt;</b></font>");
    $aim->send_config();
} 

sub on_error { }

sub on_im {
    my $reply;
    my ($self, $event) = @_;
    my ($victim, $friend, $msg) = @{$event->args()};
    $victim = $event->from;
    $rawvictim = lc($victim);
    $rawvictim =~ s/\s//g;       

    $rawmsg = $msg;
    $msg =~ s/<(.|\n)+?>//g;
    $msg =~ s/[\&\;\`\'\"-\|\*\?\^\(\)\[\]\{\}\$\n\r(\.\.)]//g;
    $msg = lc($msg);
    $reply = "";
    $i = 0;

    $cleanmsg = "$msg";
    $cleanmsg =~ s/ //g;
    $cleanmsg =~ s/\n//g;

    if ($cleanmsg eq "") {
        @reply = ("At least say something to me","You won't get anythi
+ng out of me unless you type at least one letter!","At least type one
+ letter when you talk to me.","Please, talk to me, dont just say that
+!");
        $replylen = scalar(@reply);
        $replynum = int(rand($replylen));
        $reply = "$reply[$replynum]";
    }

    if (-e "logs/$screenname/saidtemp/lastsaid$rawvictim.html") {
        open (FILE, "logs/$screenname/saidtemp/lastsaid$rawvictim.html
+");
        $lastsaid = <FILE>;
        close(FILE);
        if ($msg eq "$lastsaid") {
            @reply = ("Please, don't repeat yourself","Stop repeating 
+yourself","Don't repeat yourself","I will not talk to you unless you 
+stop repeating yourself");
            $replylen = scalar(@reply);
            $replynum = int(rand($replylen));
            $reply = "$reply[$replynum]";
        }
    }

    open (DATA, ">logs/$screenname/saidtemp/lastsaid$rawvictim.html");
    print DATA "$msg";
    close(DATA);

    $justcreated = "";

    if (-e "logs/$screenname/temp/$rawvictim.html" ne 1) {
        $i++;
        $justcreated = "$rawvictim.html";
        open (DATA, ">logs/$screenname/temp/$rawvictim.html") || &fata
+l("$!");
        print DATA "1";
        close(DATA);
        print "Just created $rawvictim.html because it didn't exist..\
+n";
    }

    opendir(DIR, "logs/$screenname/temp");

    foreach $file (sort(readdir(DIR))) {
        $i++;
        $file =~ s/ //g;
        if ($file eq ".") { $i--; }
        elsif($file eq "..") { $i--; }
        elsif($file eq "$justcreated") {
            if ($debug == 1) { print "\n\nSkipping $file because it wa
+s just created\n"; }
            $i--;
        } elsif ($file eq "$rawvictim.html") { } else {
            $full = "logs/$screenname/temp/$file";
            $zs=stat($full);
            $mtime = $zs->mtime;
            $now = time();
            $timetodel = 900;                

            $what = $now - $mtime;
            if ($what > $timetodel) {
                unlink "logs/$screenname/temp/$file";
                if ($debug == 1) { print "\nUnlinked $file.. because i
+t was $what seconds old\n"; }
                $i--;
            } else {
                if ($debug == 1) { print "\nKept $file beacuse it was 
+$what seconds old, not 900\n"; }
            }
        }
    }
    closedir(DIR);

    opendir(DIR, "logs/$screenname/saidtemp");

    foreach $file (sort(readdir(DIR))) {
        $file =~ s/ //g;
        if ($file eq ".") { }
        elsif($file eq "..") { }
        else {
            $full = "logs/$screenname/saidtemp/$file";
            $zs=stat($full);
            $mtime = $zs->mtime;
            $now = time();
            $timetodel = 900;                

            $what = $now - $mtime;
            if ($what > $timetodel) {
                unlink "logs/$screenname/saidtemp/$file";
                if ($debug == 1) { print "\nUnlinked $file.. because i
+t was $what seconds old (lastsaid temp file)\n"; }
            } else {
                if ($debug == 1) { print "\nKept $file beacuse it was 
+$what seconds old, not 900 (lastsaid temp file)\n"; }
            }
        }
    }
    closedir(DIR);

    if ($i == 1) {
        $temp = "$rawvictim";
        if ($debug == 1) { print "\nTemp set to $rawvictim...\n"; }
    } else {
        if ($i == 0) { $i == 1; }
        $temp = "";
        if ($debug == 1) { print "\nTemp cleared, not talking to anyon
+e.\n"; }
    }

    if ($debug == 1) { print "\nI am currently talking to this many pe
+ople: $i..\n\n"; }

    if ($commands == 1) {
        if ($msg =~ /^!block/) {
            $what = "$msg";
            $what =~ s/\!block//i;

            $self->add_deny("Buddies", $msg);
            if (-e "logs/$screenname/idiots.html") {
                open (DATA, ">>logs/$screenname/idiots.html") || &fata
+l("$!");
                print DATA ",$what";
                close(DATA);
            } else {
                open (DATA, ">logs/$screenname/idiots.html") || &fatal
+("$!");
                print DATA "$what";
                close(DATA);
            }
        }
        if ($msg =~ /^!ad/) {
            $what = "$msg";
            $what =~ s/\!ad //i;

            $self->send_im($what, "<font face=\"Verdana\" size=\"2\"><
+b>Hello! I am <u>Google Find</u>! I am the latest in instant messagin
+g searching. I can help you search <u>Google</u> just by sending me a
+ message with your search criteria. Enjoy!</b></font>");
            $reply = "Message Sent.";
        }
    }

    if ($reply eq "") {
        if ($msg eq "bye") { $reply = "Goodbye."; }
        if ($msg eq "goodbye") { $reply = "Goodbye."; }
        if ($msg eq "c ya") { $reply = "Goodbye."; }
        if ($msg eq "cya") { $reply = "Goodbye."; }
        if ($msg eq "goodnight") { $reply = "Goodnight."; }
        if ($msg eq "goodnite") { $reply = "Goodnight."; }
        if ($msg eq "bye bye") { $reply = "Goodbye."; }

        if ($reply eq "") {
            my $result = $service -> doGoogleSearch( $key, $msg, 0, 5,
+ SOAP::Data->type(boolean => "false"), "", SOAP::Data->type(boolean =
+> "false"), "", "latin1", "latin1" );
            if(defined($result->{resultElements})) {
                if (defined($result->{resultElements}->[0]->{title})) 
+{ $reply = "<br><b>1:</b> $result->{resultElements}->[0]->{title}, $r
+esult->{resultElements}->[0]->{URL}<br>"; }
                if (defined($result->{resultElements}->[1]->{title})) 
+{ $reply = $reply . "<b>2:</b> $result->{resultElements}->[1]->{title
+}, $result->{resultElements}->[1]->{URL}<br>"; }
                if (defined($result->{resultElements}->[2]->{title})) 
+{ $reply = $reply . "<b>3:</b> $result->{resultElements}->[2]->{title
+}, $result->{resultElements}->[2]->{URL}<br>"; }
                if (defined($result->{resultElements}->[3]->{title})) 
+{ $reply = $reply . "<b>4:</b> $result->{resultElements}->[3]->{title
+}, $result->{resultElements}->[3]->{URL}<br>"; }
                if (defined($result->{resultElements}->[4]->{title})) 
+{ $reply = $reply . "<b>5:</b> $result->{resultElements}->[4]->{title
+}, $result->{resultElements}->[4]->{URL}<br>"; }
            } else {
                $reply =  "No matching results. Refine your query.";
            }
            if ($reply eq "") { $reply =  "No matching results. Refine
+ your query."; }
        }
    }

    if (-e "logs/$screenname/idiots.html") {
        open (FILE, "logs/$screenname/idiots.html") || &fatal("$!");
        $idiots = <FILE>;
        close(FILE);

        @idiots = split(/,/, $idiots);

        foreach $items (@idiots) { 
            if ($found ne "1") {
                if ($items eq "$rawvictim") {
                    $idiot = 1;
                    $found = 1;
                    undef $reply;
                    $aim->evil($rawvictim, 0);
                    $aim->add_deny('1', 'buddies', $rawvictim);
                    print "\nThis guy is an idiot, So i warned him\n";
                } else {
                    $found = 0;
                    $idiot = 0;
                }
            }
        }
    }

    if (-e "logs/$screenname/$victim.html" ne 1) {
        open (DATA, ">logs/$screenname/$victim.html");
        print DATA "<center><font face=\"Verdana\" size=\"5\"><b>AIM B
+ot Log for $rawvictim</b></font></center><br>&nbsp;<br>";
        close(DATA);
        open (DATA, ">>logs/$screenname/$victim.html");
        print DATA "<br><font face=\"Verdana\" size=\"2\" color=\"#FF0
+000\"><b>$victim:</b></font> <font face=\"Verdana\" size=\"2\" color=
+\"#000000\">$msg</font><br><font face=\"Verdana\" size=\"2\" color=\"
+#0000FF\"><b>$screenname: </b></font><font face=\"Verdana\" size=\"2\
+" color=\"#000000\">$reply</font>";
        close(DATA);
    } else {
        open (DATA, ">>logs/$screenname/$victim.html");
        print DATA "<br><font face=\"Verdana\" size=\"2\" color=\"#FF0
+000\"><b>$victim:</b></font> <font face=\"Verdana\" size=\"2\" color=
+\"#000000\">$msg</font><br><font face=\"Verdana\" size=\"2\" color=\"
+#0000FF\"><b>$screenname: </b></font><font face=\"Verdana\" size=\"2\
+" color=\"#000000\">$reply</font>";
        close(DATA);
    }

    print "$victim: $msg\n";
    print "$screenname: $reply\n\n";

    if ($idiot ne "1") {
        if ($found ne "1") {
            $reply = "<font face=\"Verdana\" size=\"2\"><b>$reply</b><
+/font>";
            $reply =~ s/&lt;/</g;
            $reply =~ s/&gt;/>/g;
            $reply =~ s/&amp;gt;/>/g;
            $reply =~ s/&apos;/'/g;
            sleep(2);
            $self->send_im($victim, "$reply");
        }
    } else { print "\nWarned $rawvictim\n"; }

    undef $found;
    undef $reply;
    undef $i;
    undef @idiots;
    undef $idiots;
    undef $idiot;
}

sub fatal { 
    $reason = shift;
    print "\n Fatal error: $reason\n\n";
    exit;
}