Perl IRC Bot

I tried using EggDrop, but couldn’t seem to get that working well (at all). So I decided meh! I’ll just make my own!

So I started. After researching, I found an O’Reilly tutorial on how to make an IRC bot in perl so I’m using that as a template. Here’s what I have so far:

#!/usr/bin/perl

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.dal.net";
my $nick = "PirateBot";

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

# Log on to the server.
print $sock "NICK $nickrn";
print $sock "USER $nick 3 * :Pirate Botrn";

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

# Include the functions here
#include "functions.perl";

# Keep reading lines from the server.
while (my $response = ) {
  chop $response;
  #Always print the responses
  print "$responsen";
  if ($response =~ /^PING (.*)$/i) {
    # We must respond to PINGs to avoid being disconnected.
    print $sock "PONG $1rn";
    print "PONG $1rn";
  } elsif ($response =~ /Any pirates around?/i) { #This is just a random test thing
    print $sock "PRIVMSG #dmu :Hell yes!nr";
    print "PRIVMSG #dmu :Hell yes!nr";
  } elsif ($response =~ /^:(.+)!~.+ JOIN :#dmu/i) { #This just says hello to people as they come into the room
    my $usersNick = $1;
    if ($usersNick eq $nick) {
      print $sock "PRIVMSG #dmu :Ahoy! Did you miss me?rn";
      print "PRIVMSG #dmu :Ahoy! Did you miss me?rn";
    } else {
      print $sock "PRIVMSG #dmu :Ahoy, $usersNick.rn";
      print "PRIVMSG #dmu :Ahoy, $usersNick.rn";
    }
  } elsif ($response =~ /^$nick, seen (.+).*?/i) { #Seen command
    print $sock "PRIVMSG #dmu :I can't do that just yet...rn";
    print "PRIVMSG #dmu :I can't do that just yet...rn";
  }
}

The problem with that is that I can’t do much with it… To do the seen command I want I’d need to do a ULIST -c #dmu and grab that output, but that’d make it go around the loop again… So, I’ll need to rethink that design…

Comments are closed.