Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use POE qw(Component::IRC);
- my $nickname = 'Flibble' . $$;
- my $ircname = 'U-Boat\'s slave';
- my $server = 'irc.freshchat.org'; #this cannot use an ipv6 only domain name or it won't resolve. seems to be a bug in perl
- my @channels = ('#botdev');
- # We create a new PoCo-IRC object
- my $irc = POE::Component::IRC->spawn(
- nick => $nickname,
- ircname => $ircname,
- server => $server,
- usessl => '1',
- port => '6697',
- # useipv6 => '1', don't use this unless you have an IPv6 capable internet connection
- ) or die "Oh noooo! $!";
- POE::Session->create(
- package_states => [main => [ qw(_default _start irc_001 irc_public) ],],
- heap => { irc => $irc },
- );
- $poe_kernel->run();
- sub random_temps { ##generate random temperatures in a range of degrees Celsius. if not Celsius, convert it
- my $temp = rand($_[0]);
- my $to_say;
- my $final;
- my $scale = int(rand(3));
- if ($scale == 0) {
- $to_say = "degrees Celsius";
- $final = int($temp) . " " . $to_say;
- }
- elsif ($scale == 1) {
- $temp = ($temp * (5 / 9)) + 32;
- $to_say = "degrees Fahrenheit";
- $final = int($temp) . " " . $to_say;
- }
- else {
- $temp += 273;
- $to_say="Kelvins";
- $final = int($temp) . " " . $to_say;
- }
- return $final;
- }
- sub random_flavours { ##generate a random flavour for some of the public commands
- my $number = int(rand(9));
- my @flavours = ("vanilla", "chocolate", "strawberry", "pumpkin", "pepperoni", "cheese", "mushroom", "vegetarian", "mystery");
- return $flavours[$number];
- }
- sub _start {
- my $heap = $_[HEAP];
- # retrieve our component's object from the heap where we stashed it
- my $irc = $heap->{irc};
- $irc->yield( register => 'all' );
- $irc->yield( connect => { } );
- return;
- }
- sub irc_001 {
- my $sender = $_[SENDER];
- # Since this is an irc_* event, we can get the component's object by
- # accessing the heap of the sender. Then we register and connect to the
- # specified server.
- my $irc = $sender->get_heap();
- print "Connected to ", $irc->server_name(), "\n";
- $irc->yield( join => $_ ) for @channels;
- return;
- }
- sub irc_public {
- my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
- my $nick = ( split /!/, $who )[0];
- my $channel = $where->[0];
- if (my ($water) = $what =~ /^!water/) {
- my @result = random_temps(100);
- $irc->yield(ctcp => $channel => "ACTION gives $nick some water at $result[0].");
- }
- if ($what =~ /^!ice cream/) {
- my @result = random_temps(-50);
- my $flavour = random_flavours();
- $irc->yield(ctcp => $channel => "ACTION gives $nick some $flavour ice cream chilled to $result[0].");
- }
- if ($what =~ /^!pizza/) {
- my @result = random_temps(1200);
- my $flavour = random_flavours();
- $irc->yield(ctcp => $channel => "ACTION gives $nick some $flavour pizza heated to $result[0].");
- if ($result[0] > 200) {$irc->yield("privmsg" => $channel => "Mmm... Crispy!");}
- }
- if ($what =~ /^!cheesecake/) {
- my @result = random_temps(15);
- my $flavour = random_flavours();
- $irc->yield(ctcp => $channel => "ACTION gives $nick a slice of $flavour cheesecake cooled to $result[0].");
- }
- if ($what =~ /^!beer/) {
- my @result = random_temps(20);
- $irc->yield(ctcp => $channel => "ACTION gives $nick a bottle of beer cooled to $result[0].");
- }
- if ($what =~ /^!joint/) {
- $irc->yield("privmsg" => $channel => "Sorry, marijuana has not been legalized in my jurisdiction");
- }
- if ($what =~ /shut up perlbot/) {
- $irc->yield("kick" => $channel => $nick => "I'll teach you tell me to shut up! :-P");
- }
- if ($what =~ /^!coffee/) {
- my @result = random_temps(100);
- $irc->yield(ctcp => $channel => "ACTION gives $nick a cup of coffee brewed at $result[0].");
- }
- if ($what =~ /^!tea/) {
- my @result = random_temps(100);
- $irc->yield(ctcp => $channel => "ACTION gives $nick a cup of tea brewed at $result[0].");
- }
- if ($what =~ /^!milk/) {
- my @result = random_temps(20);
- $irc->yield(ctcp => $channel => "ACTION gives $nick a glass of milk chilled to $result[0].");
- }
- if ($what =~ /^!help/) {
- $irc->yield("privmsg" => $channel => "You may ask me for the following: beer, coffee, tea, milk, water, pizza, cheesecake, joint, or ice cream.");
- }
- # if ($what =~ /^!voice (.*)/) {$irc->yield(mode => $channel => "+v $1");}
- # if ($what =~ /^!devoice (.*)/) {$irc->yield(mode => $channel => "+-v $1");}
- if ($what =~ /^!die/) {
- $irc->yield("shutdown", "This connection was killed by $nick");
- }
- return;
- }
- # We registered for all events, this will produce some debug info.
- sub _default {
- my ($event, $args) = @_[ARG0 .. $#_];
- my @output = ( "$event: " );
- for my $arg (@$args) {
- if ( ref $arg eq 'ARRAY' ) {
- push( @output, '[' . join(', ', @$arg ) . ']' );
- }
- else {
- push ( @output, "'$arg'" );
- }
- }
- print join ' ', @output, "\n";
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment