Irc Bot - Perl

CanYouHack.it
Programming 1

#!/usr/local/bin/perl -w
# irc.pl
# A simple IRC robot.
# Usage: perl irc.pl

use strict;
use warnings;
use v5.14.2;

# We will use a raw socket to connect to the IRC server.
use IO::Socket;

# The server to connect to and our details.
my $server = "irc.idlemonkeys.net";
my $nick = "bz";
my $login = "bz";

# The channel which the bot will join.
my $channel = "#canyouhack.it";

# Connect to the IRC server.
my $sock = new IO::Socket::INET(PeerAddr => $server,
                                PeerPort => 6667,
                                Proto => 'tcp') or
                                    die "Can't connect\n";

# Log on to the server.
print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n";

# Read lines from the server until it tells us we have connected.
while (my $input = <$sock>) {
    # Check the numerical responses from the server.
    if ($input =~ /004/) {
        # We are now logged in.
        last;
    }
    elsif ($input =~ /433/) {
        die "Nickname is already in use.";
    }
}

# Join the channel.
print "**********************\n";
print $sock "PRIVMSG [CYH]Bot !C1\r\n";

# Keep reading lines from the server.
while (my $input = <$sock>) {
    chop $input;
    if ($input =~ /^PING(.*)$/i) {
        # We must respond to PINGs to avoid being disconnected.
        print $sock "PONG $1\r\n";
    }
    else {
        # Print the raw line received by the bot.
        print "*\n";
        print "$input\n";
        if ($input =~ /PRIVMSG/) {
            my @all_nums;
            say $input; 
            @all_nums = $input =~ /(\d+)/g; 
            #print $out;
            if ($all_nums[3]) {
            print $sock "PRIVMSG [CYH]Bot !A1 $all_nums[3]\n\r";
        }

            }

    }
    #print "input\n";
}


Categories: Share

Leave a Reply