User Tools

Site Tools


user:jpettie:portfolio:hpcproject5

Project: Perl IRC Bot with Regular Expressions Out The Wazzzzoooooo!

A project for HPC Fundamentals 2 by Jacob Pettie during the Spring 2013 Semester.

Objectives

The purpose of this project is to create an dynamic bot system that can be updated easily in terms of any feature needed and quickly, for example: pom:, weather:, location:

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • ability to understand the interactive between Perl the IRC cooperation.
  • ability to use RegEx to check for commands and parse web page information into readable output to the IRC client.
  • Knowledge of Perl syntax, along with concepts like ifs, loops, RegEx, and globals.
  • Knowledge of IRC commands and how to communicate with IRC from PERL interactive.

Background

The end result of this project is a working and adaptable IRC Perl Bot that can be easily updated to add any functionality quickly using its dynamic If/RegEx combination looping.

Execution Steps

Steps to creating a Dynamic Perl IRC Bot:

  1. Parsing html web page resources using RegEx for commands and functions of the bot. (location:, weather:, define:)
  2. Implementing Dynamic If statement structure in order to increase implementation time of functionality added to the bot.
  3. Create useful commands for consumption by usual IRC visitors on their course to enlightenment by the great Matthew Haas.

Reference Code and Working Implementation of RegEx and Perl IRC communication Bot

#!/usr/bin/perl
   use strict;
   use vars qw($VERSION %IRSSI);
   use LWP::Simple;
   use Irssi qw(command_bind signal_add);
   use IO::File;
 
   $VERSION = '0.1';
   %IRSSI = (
       authors        => 'Jacob Pettie',
       contact        => 'jpettie@corning-cc.edu',
       name           => 'IRC Bot',
       description    => 'Many potentially good things.',
       license        => 'IDK',
   );
 
   my $lstate = '0';
   my $sleep = '0';
   my $pom;
 
   sub own_question {
       my ($server, $msg, $target) = @_;
       question($server, $msg, "", $target);
   }
 
   sub public_question {
       my ($server, $msg, $nick, $address, $target) = @_;
       question($server, $msg, $nick, $target);
   }
   sub question($server, $msg, $nick, $target) {
       my ($server, $msg, $nick, $target) = @_;
       $_ = $msg;
 
    #if (/^whois:$/i) {
        #my $uInfo;
        #$uInfo = `$server->command('whois '.$msg)`;
    #}
    if (/^sleep:$/i) {
        if ($nick eq 'Ocean'){
            $sleep = '1';
            $server->command('msg '.$target.' '.'casts Sleep on self.');
        }
    }
    if (/^wake:$/i) {
        if ($nick eq 'Ocean'){
            $sleep = '0';
            $server->command('msg '.$target.' '.'Oh, I see how it is, so now you want me to be around but not all the time....');
        }
    }
     if ($sleep eq '0') {
 
       if (/^roll:$/i) {
           my $ia = int(rand(101));
           $server->command('msg '.$target.' '.$nick.' rolled: '.$ia);
           return 0;
       }
    if(m/\.com/i or m/\.net/i or m/\.org/i){
        my $file = Irssi::get_irssi_dir."/urls.txt";
        open (FILE, '>>', $file) or $server->command('msg '.$target.' '.'Could not open file.');
        print FILE time."\t".$nick."\t".$msg;
        print FILE "\n";
        close (FILE);
    }
    if(/^lc:/i) {
        if ($nick eq 'Ocean' or $nick eq 'wedge'){
                $msg = lc($msg);
                $msg =~ s/lc://;
                $pom = `$msg`;
                $server->command('msg '.$target.' '.$pom);
        }
    }
    if(/^define:/i) {
        $msg = lc($msg);
        #$msg =~ s/\s*//g;
        $msg =~ s/define://;
        my $defined;
        if ($msg eq 'milf'){
                $defined = "Tom's Mom";
                $server->command('msg '.$target.' '.$msg.' is '.$defined);
        }else{
                my $define = get('http://www.dictionary.reference.com/browse/'.$msg);
                if ($define =~ /<div class="dndata">(.[^<]*)</){
                        $defined = $1;
                        $server->command('msg '.$target.' '.$msg.' is '.$defined);
                }else{
                        if ($target eq '#offbyone'){
                                $define = get('http://www.urbandictionary.com/define.php?term='.$msg);
                                if ($define =~ /<div class="definition">(.[^<]*)</){
                                        $defined = $1;
                                        $server->command('msg '.$target.' '.$msg.' is '.$defined);
                                }else{
                                        $server->command('msg '.$target.' '.$msg.' was not defined.');
                                        #$server->command('msg '.$target.' '.$target);
                                }
                        }else{
                                $server->command('msg '.$target.' '.$msg.' was not defined.');
                        }
                }
        }
    }
    if(/^alert:/i) {
        my $alerted;
        my $alert = get('http://www.corning-cc.edu/shared/shared_rootsite/snowday.php');
        if ($alert =~ /<p style="color: red;">(.[^<]*)</) {
                $alerted = $1;
                $alerted =~ s/[\000-\037]*//;
                $server->command('msg '.$target.' '.$alerted);
        }
    }
    if(/^weather:/i) {
        $msg = lc($msg);
        $msg =~ s/\s*//g;
        $msg =~ s/weather://;
        my $weather;
        my $wthr = get('http://www.weather.com/weather/right-now/'.$msg);
        if ($wthr =~ /<p class="wx-text">(.[^<]*)</){
                $weather = $1;
                $weather =~ s/[\000-\037]*//;
                $server->command('msg '.$target.' '.$weather);
        }else{
                $server->command('msg '.$target.' There was a problem finding the weather for '.$msg.'.');
        }
    }
    if(/^location:/i) {
        $msg = lc($msg);
        $msg =~ s/\s*//g;
        $msg =~ s/location://;
        my $location;
        my $loc = get('http://www.findazip.com/search-by-code.html?q='.$msg);
        if ($loc =~ /$msg<\/b> - (.[^<]*)</){
                $location = $1;
                $server->command('msg '.$target.' '.$location);
        }else{
                $server->command('msg '.$target.' There was a problem finding the location of '.$msg.'.');
        }
    }
    if(/^pom:/i) {
        $pom = `pom`;
        $server->command('msg '.$target.' '.$pom);
    }
    if(/^bear:/i) {
        $server->command('msg '.$target.' '.'("")(-_-)("")');
    }
    if(/^light:$/i) {
        if ($nick eq 'Ocean' or $nick eq 'squirrel' or $nick eq 'wedge' or $nick eq 'asowers' or $nick eq 'polloloco'){
            if ($lstate eq '1'){
                $lstate = '0';
                $server->command('msg '.$target.' '.$nick.' turned the light off.');
            }else{
                $lstate = '1';
                $server->command('msg '.$target.' '.$nick.' turned the light on.');
            }
            my $res = get('http://admin:debian@10.80.1.245/Set.cmd?CMD=SetPower+P62='.$lstate);
        }
    }
    if (/^rick:$/i) {
        my $ia = int(rand(100));
        $ia = 'http://www.youtube.com/watch?v=oHg5SJYRHA0';
        $server->command('msg '.$target.' '.$nick.' rolled: '.$ia);
        return 0;
    }
 
    if (/^leek:$/i) {
        my $ia = 'http://www.youtube.com/watch?v=qs8pnY3BKn8';
        $server->command('msg '.$target.' '.$nick.' leeked: '.$ia);
        return 0;
    }
 
    if (/^nyan:$/i) {
        my $ia = 'http://www.nyan.cat';
        $server->command('msg '.$target.' '.$nick.' throws cat: '.$ia);
        return 0;
    }
    if (/^flip:$/i) {
        my $ia = '_|____|_\(`.´)/_|____|_';
        $server->command('msg '.$target.' '.$ia);
        return 0;
    }
    }
    #if (!/^roll:/i) { return 0; }
}
signal_add("message public", "public_question");
signal_add("message own_public", "own_question");
user/jpettie/portfolio/hpcproject5.txt · Last modified: 2013/05/07 01:00 by jpettie