greeter

basic irc bot

Apr 24th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.81 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use POE qw(Component::IRC);
  6.  
  7. my $nickname = 'Flibble' . $$;
  8. my $ircname  = 'U-Boat\'s slave';
  9. 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
  10.  
  11. my @channels = ('#botdev');
  12.  
  13.  # We create a new PoCo-IRC object
  14. my $irc = POE::Component::IRC->spawn(
  15.     nick => $nickname,
  16.     ircname => $ircname,
  17.     server  => $server,
  18.     usessl => '1',
  19.     port => '6697',
  20. #   useipv6 => '1', don't use this unless you have an IPv6 capable internet connection
  21. ) or die "Oh noooo! $!";
  22.  
  23. POE::Session->create(
  24.     package_states => [main => [ qw(_default _start irc_001 irc_public) ],],
  25.     heap => { irc => $irc },
  26. );
  27.  
  28. $poe_kernel->run();
  29.  
  30. sub random_temps { ##generate random temperatures in a range of degrees Celsius. if not Celsius, convert it
  31.     my $temp = rand($_[0]);
  32.     my $to_say;
  33.     my $final;
  34.     my $scale = int(rand(3));
  35.     if ($scale == 0) {
  36.         $to_say = "degrees Celsius";
  37.         $final = int($temp) . " " . $to_say;
  38.     }
  39.     elsif ($scale == 1) {
  40.         $temp = ($temp * (5 / 9)) + 32;
  41.         $to_say = "degrees Fahrenheit";
  42.         $final = int($temp) . " " . $to_say;
  43.     }
  44.     else {
  45.         $temp += 273;
  46.         $to_say="Kelvins";
  47.         $final = int($temp) . " " . $to_say;
  48.     }
  49.     return $final;
  50. }
  51.  
  52. sub random_flavours { ##generate a random flavour for some of the public commands
  53.     my $number = int(rand(9));
  54.     my @flavours = ("vanilla", "chocolate", "strawberry", "pumpkin", "pepperoni", "cheese", "mushroom", "vegetarian", "mystery");
  55.     return $flavours[$number];
  56. }
  57.  
  58. sub _start {
  59.     my $heap = $_[HEAP];
  60.      # retrieve our component's object from the heap where we stashed it
  61.     my $irc = $heap->{irc};
  62.     $irc->yield( register => 'all' );
  63.     $irc->yield( connect => { } );
  64.     return;
  65. }
  66.  
  67. sub irc_001 {
  68.     my $sender = $_[SENDER];
  69.  
  70.      # Since this is an irc_* event, we can get the component's object by
  71.      # accessing the heap of the sender. Then we register and connect to the
  72.      # specified server.
  73.     my $irc = $sender->get_heap();
  74.     print "Connected to ", $irc->server_name(), "\n";
  75.     $irc->yield( join => $_ ) for @channels;
  76.     return;
  77. }
  78.  
  79.  sub irc_public {
  80.     my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
  81.     my $nick = ( split /!/, $who )[0];
  82.     my $channel = $where->[0];
  83.     if (my ($water) = $what =~ /^!water/) {
  84.         my @result = random_temps(100);
  85.         $irc->yield(ctcp => $channel => "ACTION gives $nick some water at $result[0].");
  86.         }
  87.     if ($what =~ /^!ice cream/) {
  88.         my @result = random_temps(-50);
  89.         my $flavour = random_flavours();
  90.         $irc->yield(ctcp => $channel => "ACTION gives $nick some $flavour ice cream chilled to $result[0].");
  91.         }
  92.     if ($what =~ /^!pizza/) {
  93.         my @result = random_temps(1200);
  94.         my $flavour = random_flavours();
  95.         $irc->yield(ctcp => $channel => "ACTION gives $nick some $flavour pizza heated to $result[0].");
  96.         if ($result[0] > 200) {$irc->yield("privmsg" => $channel => "Mmm... Crispy!");}
  97.         }
  98.     if ($what =~ /^!cheesecake/) {
  99.         my @result = random_temps(15);
  100.         my $flavour = random_flavours();
  101.         $irc->yield(ctcp => $channel => "ACTION gives $nick a slice of $flavour cheesecake cooled to $result[0].");
  102.         }
  103.     if ($what =~ /^!beer/) {
  104.         my @result = random_temps(20);
  105.         $irc->yield(ctcp => $channel => "ACTION gives $nick a bottle of beer cooled to $result[0].");
  106.         }
  107.     if ($what =~ /^!joint/) {
  108.         $irc->yield("privmsg" => $channel => "Sorry, marijuana has not been legalized in my jurisdiction");
  109.         }
  110.     if ($what =~ /shut up perlbot/) {
  111.         $irc->yield("kick" => $channel => $nick => "I'll teach you tell me to shut up! :-P");
  112.         }
  113.     if ($what =~ /^!coffee/) {
  114.         my @result = random_temps(100);
  115.         $irc->yield(ctcp => $channel => "ACTION gives $nick a cup of coffee brewed at $result[0].");
  116.         }
  117.     if ($what =~ /^!tea/) {
  118.         my @result = random_temps(100);
  119.         $irc->yield(ctcp => $channel => "ACTION gives $nick a cup of tea brewed at $result[0].");
  120.         }
  121.     if ($what =~ /^!milk/) {
  122.         my @result = random_temps(20);
  123.         $irc->yield(ctcp => $channel => "ACTION gives $nick a glass of milk chilled to $result[0].");
  124.         }
  125.     if ($what =~ /^!help/) {
  126.         $irc->yield("privmsg"  => $channel => "You may ask me for the following: beer, coffee, tea, milk, water, pizza, cheesecake, joint, or ice cream.");
  127.         }
  128. #   if ($what =~ /^!voice (.*)/) {$irc->yield(mode => $channel => "+v $1");}
  129. #   if ($what =~ /^!devoice (.*)/) {$irc->yield(mode => $channel => "+-v $1");}
  130.     if ($what =~ /^!die/) {
  131.         $irc->yield("shutdown", "This connection was killed by $nick");
  132.     }
  133.     return;
  134. }
  135.  
  136.  # We registered for all events, this will produce some debug info.
  137. sub _default {
  138.     my ($event, $args) = @_[ARG0 .. $#_];
  139.     my @output = ( "$event: " );
  140.     for my $arg (@$args) {
  141.         if ( ref $arg eq 'ARRAY' ) {
  142.         push( @output, '[' . join(', ', @$arg ) . ']' );
  143.             }
  144.     else {
  145.         push ( @output, "'$arg'" );
  146.         }
  147.     }
  148.     print join ' ', @output, "\n";
  149.     return;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment