Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #!perl.exe
- #
- # JavBot 1.0
- # - IRC network Module
- use POE qw(Component::IRC);
- ## ** <SETTINGS> **
- # Remote host
- my $rHost = "irc.toribash.com";
- # Remote port
- my $rPort = 6667;
- # Bot Name
- my $sBot = "JavBot";
- # Default irc channels
- my @aChans = ("#javbot", "#robocode");
- # Owners array.
- my @sOwners = ("avwerk", "junt-wrk");
- # Debug flags
- my $blDbg = 0;
- ## ** </SETTINGS> **
- ## ** <UTIL FUNCTIONS> **
- # Banner function
- sub pBanner {
- print " ___\n";
- print " / /\n";
- print " | |\n";
- print " | | ___ ___ ___\n";
- print " ____ / / / \\ \\ \\ / /\n";
- print " | |__/ / / o \\ \\ \\/ /\n";
- print " \\_______/ /__/ \\__\\ \\____/\n";
- print " ____\n";
- print " | |__ ___ ___\n";
- print " | | / \\ _| |_\n";
- print " | o | | o | |_ _|\n";
- print " |_____| \\___/ |_|\n";
- print "\n";
- print " Version 1.0\n";
- print " by avwave and Juntalis\n\n";
- }
- # Simple print helpers.
- sub pState { print " (Info): @_\n"; } # pState($msg)
- sub pUser { if ($_[0] eq "") { print " (PM) $_[1]: $_[2]\n"; } # pUser("", $user, $msg)
- else { print " ($_[0]) $_[1]: $_[2]\n"; } } # pUser($channel, $user, $msg)
- sub pDbg { if($blDbg) { print " Debug($_[0]): $_[1]\n"; } } # pDbg(event, msg)
- ## ** </UTIL FUNCTIONS> **
- ## ** <OBJECT CREATION> **
- # Print the banner.
- pBanner();
- # Create POE IRC Component
- my $irc = POE::Component::IRC->spawn(
- nick => $sBot,
- username => $sBot,
- ircname => $sBot,
- server => $rHost,
- port => $rPort,
- ) or die " Error: $!";
- # Create and start POE Session
- POE::Session->create(
- package_states => [
- main => [ qw(_default _start irc_001 irc_public irc_nick irc_part irc_msg irc_join irc_quit) ],
- ],
- heap => { irc => $irc },
- );
- $poe_kernel->run();
- ## ** </OBJECT CREATION> **
- ## ** <CALLBACKS> **
- # POE Session Initiated
- 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;
- }
- # IRC Session Started
- sub irc_001 {
- # 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 $sender = $_[SENDER];
- my $irc = $sender->get_heap();
- # Print information.
- pState("Connected to: " . $irc->server_name());
- pState("Logged in as: " . $irc->nick_name());
- # Join channels.
- $irc->yield( join => $_ ) for @aChans;
- # Alert owners.
- foreach $sOwner(@sOwners)
- { $irc->yield(ctcp => $sOwner => 'ONLINE'); }
- return;
- }
- # PRIVMSG from inside a channel
- sub irc_public {
- # Get information.
- my ($sender, $sWho, $sWhere, $sMsg) = @_[SENDER, ARG0 .. ARG2];
- my $sUser = ( split /!/, $sWho )[0];
- my $sChan = $sWhere->[0];
- my $sRegEx = "$sBot:(.+)";
- # Prints what's going on.
- pUser($sChan, $sUser, $sMsg);
- # Check for mention of its name
- if ($sMsg =~ m/$sRegEx/i) {
- $irc->yield( privmsg => $sChan => "$sUser: I heard you talking about me." );
- }
- #if ( my ($rot13) = $sMsg =~ /^rot13 (.+)/ ) {
- # $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
- # $irc->yield( privmsg => $channel => "$sUser: $rot13" );
- #}
- return;
- }
- # PRIVMSG privately
- sub irc_msg {
- # Information
- my ($sender, $sWho, $sMsg) = @_[SENDER, ARG0, ARG2];
- my $sUser = ( split /!/, $sWho )[0];
- # Print information
- pUser("", $sUser, $sMsg);
- }
- # NICK change in a channel.
- sub irc_nick {
- # Information
- my ($sender, $sOld, $sNew) = @_[SENDER, ARG0, ARG1];
- my $sUser = ( split /!/, $sOld)[0];
- # Print information
- pState("$sUser has changed their nick to $sNew.");
- }
- # User parts a channel.
- sub irc_part {
- # Information
- my ($sender, $sNick, $sWhere, $sReason) = @_[SENDER, ARG0, ARG1, ARG2];
- my $sUser = ( split /!/, $sNick)[0];
- # Print information
- pState("$sUser left $sWhere. (Reason: $sReason)");
- }
- # User joins a channel.
- sub irc_join {
- # Information
- my ($sender, $sNick, $sWhere) = @_[SENDER, ARG0, ARG1];
- my $sUser = ( split /!/, $sNick)[0];
- # Print information
- pState("$sUser joined $sWhere.");
- }
- # User quits the server.
- sub irc_quit {
- # Information
- my ($sender, $sNick, $sReason) = @_[SENDER, ARG0, ARG1];
- my $sUser = ( split /!/, $sNick)[0];
- # Print information
- pState("$sUser has disconnected. (Reason: $sReason)");
- }
- # Debug info
- sub _default {
- my ($event, $args) = @_[ARG0 .. $#_];
- my @output = ();
- for my $arg (@$args) {
- if ( ref $arg eq 'ARRAY' ) {
- push(@output, '[' . join(', ', @$arg ) . ']');
- }
- else {
- push (@output, "'$arg'");
- }
- }
- pDbg("$event", join(' ', @output));
- return 0;
- }
- ## ** </CALLBACKS> **
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement