http://qs1969.pair.com?node_id=191447
Category: Miscellaneous
Author/Contact Info mousey -at- perlmonk -dot- org
Description: This bot runs on the AIM (AOL Instant Messaging) network and when spoken to, runs nmap based on the instructions given to it. You need Net::AIM and nmap installed.
use Net::AIM;
use LWP::Simple;
use File::stat;

print "Initializing...\n";

$debug = 1;         # Tells whether debug messages are printed
$screenname = "";   # The AIM screenname to login with
$password = "";     # The password to the screenname

if (-e "nmap_bot" ne 1) { mkdir "nmap_bot"; }
if (-e "nmap_bot/logs" ne 1) { mkdir "nmap_bot/logs"; }
if (-e "nmap_bot/logs/$screenname" ne 1) { mkdir "nmap_bot/logs/$scree
+nname"; }
if (-e "nmap_bot/logs/$screenname/temp" ne 1) { mkdir "nmap_bot/logs/$
+screenname/temp"; }
if (-e "nmap_bot/logs/$screenname/saidtemp" ne 1) { mkdir "nmap_bot/lo
+gs/$screenname/saidtemp"; }

$aim = new Net::AIM;

$aim->newconn( Screenname => $screenname, Password => $password ) or d
+ie "NMAP Bot: 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('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/shitlist.html") {
            open (DATA, ">>nmap_bot/logs/$screenname/shitlist.html") |
+| &fatal("$!");
            print DATA ",$victim";
            close(DATA);
        } else {
            open (DATA, ">nmap_bot/logs/$screenname/shitlist.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;<
+/b>Visit <a 

href=\"http://www.perlmonks.org/\">PerlMonks.org</a><b>&lt;/plug&gt;</
+b></font>");
    $aim->send_config();
} 

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 = 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!","Type something!","Please, talk to me, dont just say that!");
        $replylen = scalar(@reply);
        $replynum = int(rand($replylen));
        $reply = "$reply[$replynum]";
    }

    if (-e "nmap_bot/logs/$screenname/saidtemp/lastsaid$rawvictim.html
+") {
        open (FILE, "nmap_bot/logs/$screenname/saidtemp/lastsaid$rawvi
+ctim.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, ">nmap_bot/logs/$screenname/saidtemp/lastsaid$rawvicti
+m.html");
    print DATA "$msg";
    close(DATA);

    $justcreated = "";

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

    opendir(DIR, "nmap_bot/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 = "nmap_bot/logs/$screenname/temp/$file";
            $zs=stat($full);
            $mtime = $zs->mtime;
            $now = time();
            $timetodel = 900;                

            $what = $now - $mtime;
            if ($what > $timetodel) {
                unlink "nmap_bot/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, "nmap_bot/logs/$screenname/saidtemp");

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

            $what = $now - $mtime;
            if ($what > $timetodel) {
                unlink "nmap_bot/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 ($reply eq "") {
        if ($cleanmsg =~ /^nmap /i) {
          if ($cleanmsg !~ /localhost|127\.0\.0\.1|255\.255\.255\.255|
+*.\.0\.*./) {
            $cleanmsg = &filter($cleanmsg);
            $reply = `$clanmsg`;
          }
        } else {
          $reply = "You must precede all commands by nmap";
        }
    }

    if (-e "nmap_bot/logs/$screenname/shitlist.html") {
        open (FILE, "nmap_bot/logs/$screenname/shitlist.html") || &fat
+al("$!");
        $shitlist = <FILE>;
        close(FILE);

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

        foreach $items (@shitlist) { 
            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 a buttmunch, So i warned him\
+n";
                } else {
                    $found = 0;
                    $idiot = 0;
                }
            }
        }
    }

    if (-e "nmap_bot/logs/$screenname/$victim.html" ne 1) {
        open (DATA, ">nmap_bot/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, ">>nmap_bot/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 fac
+e=\"Verdana\" size=\"2\" 

color=\"#0000FF\"><b>$screenname: </b></font><font face=\"Verdana\" si
+ze=\"2\" 

color=\"#000000\">$reply</font>";
        close(DATA);
    } else {
        open (DATA, ">>nmap_bot/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 fac
+e=\"Verdana\" size=\"2\" 

color=\"#0000FF\"><b>$screenname: </b></font><font face=\"Verdana\" si
+ze=\"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 @shitlist;
    undef $shitlist;
    undef $idiot;
}

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

sub filter {
    my $string = shift;

    $string =~ s/;/\\;/g;
    $string =~ s/`/\\`/g;
    $string =~ s/'/\\'/g;
    $string =~ s/"/\\"/g;
    $string =~ s/&/\\&/g;
    $string =~ s/\(/\\(/g;
    $string =~ s/\)/\\)/g;

    chomp($string);

    return $string;

}