Advertisement
cgrunwald

Untitled

Sep 25th, 2010
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.69 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #!perl.exe
  3. #
  4. # JavBot 1.0
  5. # - IRC network Module
  6. use POE qw(Component::IRC);
  7.  
  8. ## ** <SETTINGS> **
  9.     # Remote host
  10.     my $rHost = "irc.toribash.com";
  11.     # Remote port
  12.     my $rPort = 6667;
  13.     # Bot Name
  14.     my $sBot = "JavBot";
  15.     # Default irc channels
  16.     my @aChans = ("#javbot", "#robocode");
  17.     # Owners array.
  18.     my @sOwners = ("avwerk", "junt-wrk");
  19.     # Debug flags
  20.     my $blDbg = 0;
  21. ## ** </SETTINGS> **
  22.  
  23. ## ** <UTIL FUNCTIONS> **
  24.     # Banner function
  25.     sub pBanner {
  26.         print "         ___\n";
  27.         print "        /  /\n";
  28.         print "        |  |\n";
  29.         print "        |  |   ___   ___    ___\n";
  30.         print " ____   /  /  /   \\  \\  \\  /  /\n";
  31.         print " |  |__/  /  /  o  \\  \\  \\/  /\n";
  32.         print " \\_______/  /__/ \\__\\  \\____/\n";
  33.         print "     ____\n";
  34.         print "     |  |__   ___    ___\n";
  35.         print "     |     | /   \\  _| |_\n";
  36.         print "     |  o  | | o | |_   _|\n";
  37.         print "     |_____| \\___/   |_|\n";
  38.         print "\n";
  39.         print " Version 1.0\n";
  40.         print " by avwave and Juntalis\n\n";
  41.     }
  42.    
  43.     # Simple print helpers.
  44.     sub pState { print " (Info): @_\n"; }                # pState($msg)
  45.     sub pUser  { if ($_[0] eq "") { print " (PM) $_[1]: $_[2]\n";  } # pUser("", $user, $msg)
  46.              else { print " ($_[0]) $_[1]: $_[2]\n"; } }     # pUser($channel, $user, $msg)
  47.     sub pDbg   { if($blDbg) { print " Debug($_[0]): $_[1]\n"; } }    # pDbg(event, msg)
  48.    
  49. ## ** </UTIL FUNCTIONS> **
  50.  
  51. ## ** <OBJECT CREATION> **
  52.     # Print the banner.
  53.     pBanner();
  54.  
  55.     # Create POE IRC Component
  56.     my $irc = POE::Component::IRC->spawn(
  57.             nick => $sBot,
  58.             username => $sBot,
  59.             ircname => $sBot,
  60.             server => $rHost,
  61.             port => $rPort,
  62.     ) or die " Error: $!";
  63.  
  64.     # Create and start POE Session
  65.      POE::Session->create(
  66.         package_states => [
  67.         main => [ qw(_default _start irc_001 irc_public irc_nick irc_part irc_msg irc_join irc_quit) ],
  68.         ],
  69.         heap => { irc => $irc },
  70.      );
  71.     $poe_kernel->run();
  72.        
  73. ## ** </OBJECT CREATION> **
  74.  
  75. ## ** <CALLBACKS> **
  76.     # POE Session Initiated
  77.     sub _start {
  78.         my $heap = $_[HEAP];
  79.         # retrieve our component's object from the heap where we stashed it
  80.         my $irc = $heap->{irc};
  81.         $irc->yield( register => 'all' );
  82.         $irc->yield( connect => { } );
  83.         return;
  84.     }
  85.  
  86.     # IRC Session Started
  87.     sub irc_001 {
  88.         # Since this is an irc_* event, we can get the component's object by
  89.         # accessing the heap of the sender. Then we register and connect to the
  90.         # specified server.
  91.         my $sender = $_[SENDER];
  92.         my $irc = $sender->get_heap();
  93.  
  94.         # Print information.
  95.         pState("Connected to: " . $irc->server_name());
  96.         pState("Logged in as: " . $irc->nick_name());
  97.  
  98.         # Join channels.
  99.         $irc->yield( join => $_ ) for @aChans;
  100.        
  101.         # Alert owners.
  102.         foreach $sOwner(@sOwners)
  103.         { $irc->yield(ctcp => $sOwner => 'ONLINE'); }
  104.        
  105.         return;
  106.     }
  107.  
  108.     # PRIVMSG from inside a channel
  109.     sub irc_public {
  110.         # Get information.
  111.         my ($sender, $sWho, $sWhere, $sMsg) = @_[SENDER, ARG0 .. ARG2];
  112.         my $sUser = ( split /!/, $sWho )[0];
  113.         my $sChan = $sWhere->[0];
  114.         my $sRegEx = "$sBot:(.+)";
  115.  
  116.         # Prints what's going on.
  117.         pUser($sChan, $sUser, $sMsg);
  118.        
  119.         # Check for mention of its name
  120.         if ($sMsg =~ m/$sRegEx/i) {
  121.             $irc->yield( privmsg => $sChan => "$sUser: I heard you talking about me." );
  122.         }
  123.         #if ( my ($rot13) = $sMsg =~ /^rot13 (.+)/ ) {
  124.         #   $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
  125.         #   $irc->yield( privmsg => $channel => "$sUser: $rot13" );
  126.         #}
  127.         return;
  128.     }
  129.    
  130.     # PRIVMSG privately
  131.     sub irc_msg {
  132.         # Information
  133.         my ($sender, $sWho, $sMsg) = @_[SENDER, ARG0, ARG2];
  134.         my $sUser = ( split /!/, $sWho )[0];
  135.  
  136.         # Print information
  137.         pUser("", $sUser, $sMsg);
  138.     }
  139.    
  140.     # NICK change in a channel.
  141.     sub irc_nick {
  142.         # Information
  143.         my ($sender, $sOld, $sNew) = @_[SENDER, ARG0, ARG1];
  144.         my $sUser = ( split /!/, $sOld)[0];
  145.        
  146.         # Print information
  147.         pState("$sUser has changed their nick to $sNew.");
  148.     }
  149.    
  150.     # User parts a channel.
  151.     sub irc_part {
  152.         # Information
  153.         my ($sender, $sNick, $sWhere, $sReason) = @_[SENDER, ARG0, ARG1, ARG2];
  154.         my $sUser = ( split /!/, $sNick)[0];
  155.  
  156.         # Print information
  157.         pState("$sUser left $sWhere. (Reason: $sReason)");
  158.     }
  159.    
  160.     # User joins a channel.
  161.     sub irc_join {
  162.         # Information
  163.         my ($sender, $sNick, $sWhere) = @_[SENDER, ARG0, ARG1];
  164.         my $sUser = ( split /!/, $sNick)[0];
  165.  
  166.         # Print information
  167.         pState("$sUser joined $sWhere.");
  168.     }
  169.    
  170.     # User quits the server.
  171.     sub irc_quit {
  172.         # Information
  173.         my ($sender, $sNick, $sReason) = @_[SENDER, ARG0, ARG1];
  174.         my $sUser = ( split /!/, $sNick)[0];
  175.  
  176.         # Print information
  177.         pState("$sUser has disconnected. (Reason: $sReason)");
  178.     }
  179.  
  180.     # Debug info
  181.     sub _default {
  182.         my ($event, $args) = @_[ARG0 .. $#_];
  183.         my @output = ();
  184.  
  185.         for my $arg (@$args) {
  186.             if ( ref $arg eq 'ARRAY' ) {
  187.                 push(@output, '[' . join(', ', @$arg ) . ']');
  188.             }
  189.             else {
  190.                 push (@output, "'$arg'");
  191.             }
  192.         }
  193.        
  194.         pDbg("$event", join(' ', @output));
  195.         return 0;
  196.     }
  197.  
  198. ## ** </CALLBACKS> **
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement