All pastes #497247 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-19 18:33 UTC
rendered paste body
#!/usr/bin/perl

use warnings;
use strict;

use encoding 'utf8';
use DBI;
use POE;
use POE::Component::IRC;
use LWP::Simple;

my $name;
my $guild;
my $lvl;
my $race;
my $class;
my $built;
my $realm;

sub CHANNEL () { "#poeTest" }

# Create the component that will represent an IRC network.
my ($irc) = POE::Component::IRC->spawn();

# Establish database connection
# Not using mysql for mirror
#my $dbh = DBI->connect('dbi:mysql:armory','user','pwd');

# Create the bot session.  The new() call specifies the events the bot
# knows about and the functions that will handle those events.
POE::Session->create(
    inline_states => {
        _start     => \&bot_start,
        irc_001    => \&on_connect,
        irc_public => \&on_public,
		irc_disconnect => \&on_disconnect,
    },
);

# Fill hash
#my $sql = "SELECT name, counter FROM arguments";
#my $sth = $dbh->prepare($sql);
#$sth->execute || 
#			die "Could not execute SQL statement ... maybe invalid?";

#my %hash;

#output database results
#while (my (@row) =$sth->fetchrow_array)
#{
#	$hash{$row[0]} = $row[1];
#}

# The bot session has started.  Register this bot with the "magnet"
# IRC component.  Select a nickname.  Connect to a server.
sub bot_start {
    my $kernel  = $_[KERNEL];
    my $heap    = $_[HEAP];
    my $session = $_[SESSION];

    $irc->yield( register => "all" );

    my $nick = 'ArmoryBot';
    $irc->yield( connect =>
          { Nick => $nick,
            Username => 'ArmoryBot',
            Ircname  => 'ArmoryBot',
            Server   => 'irc.quakenet.org',
            Port     => '6667',
          }
    );
}

# The bot has successfully connected to a server.  Join a channel.
sub on_connect {
    $irc->yield( join => CHANNEL );
}

# The bot has received a public message.  Parse it for commands, and
# respond to interesting things.
sub on_public {
    my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ];
    my $nick = ( split /!/, $who )[0];
    my $channel = $where->[0];

    # Monitor activity
	my $ts = scalar localtime;
    print " [$ts] <$nick:$channel> $msg\n";

    if ( my ($request) = $msg =~ /^!Armory (.+)/i )
	{
		my @split = split(' ', $request);
		
		if ($split[0] eq "quit")
		{
			if ($nick eq "chemosh_")	# If chemosh_ calls quit the bot
			{
				$irc->yield ( shutdown => "Taking a break" );
			}
		}
		else
		{
			# Send armory profile link
			my $url = "http://armory.wow-europe.com/character-sheet.xml?r=Nordrassil&n=$split[0]";
	       	$irc->yield( privmsg => CHANNEL, $url );
			
			# Add comment to database
			#on_command($split[0]);
		}
    }
	elsif ($msg =~ /^!Armory$/i)
	{
		my $response = "Usage info: http://www.jahmaan.com/armorybot.html";
		$irc->yield( privmsg => CHANNEL, $response );
	}
	elsif ( my ($rep) = $msg =~ /^!Armoryrep (.+)/i ) 
	{
		my @split = split(' ', $rep);
		my $url = "http://armory.wow-europe.com/character-reputation.xml?r=Nordrassil&n=$split[0]";

       	# Send a response back to the server.
       	$irc->yield( privmsg => CHANNEL, $url );
		#on_command($split[0]);
	}
	elsif ( my ($tal) = $msg =~ /^!Armorytal (.+)/i )
	{
		my @split = split(' ', $tal);
		my $url = "http://armory.wow-europe.com/character-talents.xml?r=Nordrassil&n=$split[0]";

       	# Send a response back to the server.
       	$irc->yield( privmsg => CHANNEL, $url );
		#on_command($split[0]);
	}
	elsif ( ($msg =~ /^!eat$/i) || ($msg =~ /^!eaten$/i) )
	{
		# idle
	}
	elsif ( my ($eat) = $msg =~ /^!eat (.+)/i )
	{
		my @split = split(' ', $eat);
		my $response = "Oh my god! $nick has eaten $split[0]! Bastard!";

       	# Send a response back to the server.
       	$irc->yield( privmsg => CHANNEL, $response );
		#on_eat($split[0]);
	}
	elsif ( my ($eaten) = $msg =~ /^!eaten (.+)/i )
	{
		my @split = split(' ', $eaten);
		
       	# Show how many times <name> has been eaten
		times_eaten($split[0]);
	}
	elsif ($msg =~ /^!define life/i )
	{
		my $define = "3 (usu. one's life) the period between the birth and death of a living thing, esp. a human being";

       	# Send a response back to the server.
       	$irc->yield( privmsg => CHANNEL, $define );
	}
	elsif ($msg =~ /^!beer$/i )
	{
		my $update_sql = "UPDATE beer SET counter = counter+1 LIMIT 1";

		#$sth = $dbh->prepare($update_sql);
		#$sth->execute || 
	     #      		die "Could not execute SQL statement ... maybe invalid?";
				
       	# Send a response back to the server.
		$poe_kernel->post( $irc => ctcp => $channel => "ACTION gives $nick a bucket of cold beer." );
		
	}
	elsif ($msg =~ /^!beerquantity$/i )
	{
		#my $update_sql = "SELECT counter FROM beer LIMIT 1";

		#$sth = $dbh->prepare($update_sql);
		#$sth->execute || 
	    #       		die "Could not execute SQL statement ... maybe invalid?";
		
		#my @row = $sth->fetchrow_array;
				
       	# Send a response back to the server.
       	$irc->yield( privmsg => CHANNEL, "Database unavailable .. I fail" );
	}
	elsif ( my ($charname) = $msg =~ /^!whois (.+)/i )
	{
		my @input = split(' ', $charname);
		my $charname = $input[0];
		getWhois($charname);
	}
	elsif ( my ($itemstr) = $msg =~ /^!item (.+)/ ) {
		
		my @item = split(' ', $itemstr);
		my $size = @item;
		my $itemlink = "http://www.wowhead.com/?search=";
		
		for (my $i=0; $i<$size; $i++) 
		{
			if($i==0)
			{
		        $itemlink .= "$item[$i]"; 
			}
			else
			{
				$itemlink .= "\+$item[$i]"
			}
		}
		
		$irc->yield( privmsg => CHANNEL, $itemlink );
	}
}


sub getWhois {
	my ($charname) = @_;
	my $link = "http://armory.wow-europe.com/character-sheet.xml?r=Nordrassil&n=$charname";
	my $source = get($link) or die 'Unable to get page';

    print " -> requested link: $link\n";
	print $source;
	if ($source =~ /(.+)(charTitle\W\w*\s?\w*\W\w+\W)(\w+)(\W\w+\W)(.*)(\Wlevel)(.+)/ ) 
	{
		$name = $3;
		$guild = $5;
	}
	
	
	# realm
	if ($source =~ /(.+)(theCharUrl\s\W\s\W\w\W)(\w+\s?\w*)(.+)/ ) 
	{
		$realm = $3;
	}
	# Check lvl, race, class
	if ($source =~ /(.+)(character\Wdescription\Wnoflash>)(\w+)(\S\S)(\d\d?)(\S\S)(\w+\s?\w+)(\S\S)(\w+)(.+)/ )
	{
		$lvl = $5;
		$race = $7;
		$class = $9;
	}
	
	# Built
	if ($source =~ /(<span>)(\d\d?)(\s\/\s)(\d\d?)(\s\/\s)(\d\d?)(<\/span>)/ ) {
		if ($class eq "Druid") {
			my %hash = (balance => $2, feral =>  $4, restoration => $6);
			$built = sortBuilt(%hash);
		}	
		
		if ($class eq "Hunter") {
			my %hash = ("beast mastery" => $2, marksmanship =>  $4, survival => $6);
			$built = sortBuilt(%hash);
		}
	
		if ($class eq "Mage") {
			my %hash = (arcane => $2, fire =>  $4, frost => $6);
			$built = sortBuilt(%hash);
		}
		
		if ($class eq "Paladin") {
			my %hash = (holy => $2, protection =>  $4, retribution => $6);
			$built = sortBuilt(%hash);
		}
		
		if ($class eq "Priest") {
			my %hash = (discipline => $2, holy =>  $4, shadow => $6);
			$built = sortBuilt(%hash);
		}
		
		if ($class eq "Rogue") {
			my %hash = (assassination => $2, combat =>  $4, subtlety => $6);
			$built = sortBuilt(%hash);
		}
		
		if ($class eq "Shaman") {
			my %hash = (elemental => $2, enhancement =>  $4, restoration => $6);
			$built = sortBuilt(%hash);
		}
		
		if ($class eq "Warlock") {
			my %hash = (affliction => $2, demonology =>  $4, destruction => $6);
			$built = sortBuilt(%hash);
		}
		
		if ($class eq "Warrior") {
			my %hash = (arms => $2, fury =>  $4, protection => $6);
			$built = sortBuilt(%hash);
		}
	print_whois();	
	}
}

sub print_whois {
	$name = ucfirst($name);
	$built = ucfirst($built);
	my $whois = "$name <$guild> Level $lvl $race $built $class on $realm\n";
	$irc->yield( privmsg => CHANNEL, $whois );
}

sub sortBuilt {
	my (%hash) = @_;
	my @sorted = sort { $hash{$a} <=> $hash{$b} } keys %hash;
	return $sorted[2];
}


# Run the bot until it is done.
$poe_kernel->run();
exit 0;