Simple Perl Bot

From SHellium Wiki
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

How to make a simple perl bot

This is a very simple perl bot , can Join and write a message.

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

use strict;

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.freenode.net";
my $nick = "simpleB0t";
my $login = "simpleb0t";

The channel which the bot will join.

my $channel = "#RabbitHole";

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 * :Cool Master you made it!\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 $sock "JOIN $channel\r\n";

Keep reading lines from the server. (you can make it to write in log file)

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 "$input\n";
    }
}

Write message in main chanel (#ChanelName).

print $sock "PRIVMSG #ChanelName :Greetings people \r\n";

Write message to someone (Nick).

print $sock "PRIVMSG Nick :Yo man \r\n";


Full Code:

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

use strict;
use IO::Socket;

# The server to connect to and our details.
my $server = "irc.freenode.net";
my $nick = "st3v0_b0t";
my $login = "st3v0_b0t";
my $channel = "#bshellz";
my $sock = new IO::Socket::INET(PeerAddr => $server,PeerPort => 6667,Proto => 'tcp') or die "Can't connect\n";


print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :Cool st3v0 you made it!\r\n";


while (my $input = <$sock>) {   
    if ($input =~ /004/) {       
        last;
    }
    elsif ($input =~ /433/) {
        die "Nickname is already in use.";
    }
}

print $sock "JOIN $channel\r\n";

my $ping_counter = 0;

while (my $input = <$sock>) {
    chop $input;
    if ($input =~ /^PING(.*)$/i) {
        print $sock "PONG $1\r\n";
        $ping_counter = $ping_counter + 1        
        if($ping_counter == 100) {    
            print $sock "PRIVMSG BeschBot :!keep st3v0\r\n";
            $ping_counter = 0;
        }
    }
    else {
        print "$input\n";
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox