Bot-BasicBot

 view release on metacpan or  search on metacpan

lib/Bot/BasicBot.pm  view on Meta::CPAN




sub AUTOLOAD {
    my $self = shift;
    our $AUTOLOAD;
    $AUTOLOAD =~ s/.*:://;
    $poe_kernel->post(
        $self->{IRCNAME},
        $AUTOLOAD,
        $self->charset_encode(@_),
    );
    return;
}

# so it won't get AUTOLOADed
sub DESTROY { return }

sub log {
    my $self = shift;
    for (@_) {
      my $log_entry = $_;
      chomp $log_entry;
      print STDERR "$log_entry\n";
    }
    return;
}

sub ignore_nick {
    local $_ = undef;
    my $self = shift;
    my $nick = shift;
    return grep { $nick eq $_ } @{ $self->{ignore_list} };
}

sub nick_strip {
    my $self = shift;
    my $combined = shift || "";
    my ($nick) = $combined =~ m/(.*?)!/;
    return $nick;
}

sub charset_decode {
    my $self = shift;

    my @r;
    for (@_) {
        if (ref($_) eq 'ARRAY') {
            push @r, [ $self->charset_decode(@$_) ];
        }
        elsif (ref($_) eq "HASH") {
            push @r, { $self->charset_decode(%$_) };
        }
        elsif (ref($_)) {
            die "Can't decode object $_\n";
        }
        else {
            push @r, decode_irc($_);
        }
    }
    #warn Dumper({ decoded => \@r });
    return @r;
}

sub charset_encode {
    my $self = shift;

    my @r;
    for (@_) {
        if (ref($_) eq 'ARRAY') {
            push @r, [ $self->charset_encode(@$_) ];
        }
        elsif (ref($_) eq "HASH") {
            push @r, { $self->charset_encode(%$_) };
        }
        elsif (ref($_)) {
            die "Can't encode object $_\n";
        }
        else {
            push @r, encode($self->charset, $_);
        }
    }
    #warn Dumper({ encoded => \@r });
    return @r;
}

1;

=head1 NAME

Bot::BasicBot - simple irc bot baseclass

=head1 SYNOPSIS

  #!/usr/bin/perl
  use strict;
  use warnings;

  # Subclass Bot::BasicBot to provide event-handling methods.
  package UppercaseBot;
  use base qw(Bot::BasicBot);

  sub said {
      my $self      = shift;
      my $arguments = shift;    # Contains the message that the bot heard.

      # The bot will respond by uppercasing the message and echoing it back.
      $self->say(
          channel => $arguments->{channel},
          body    => uc $arguments->{body},
      );

      # The bot will shut down after responding to a message.
      $self->shutdown('I have done my job here.');
  }

  # Create an object of your Bot::BasicBot subclass and call its run method.
  package main;

  my $bot = UppercaseBot->new(
      server      => 'irc.example.com',



( run in 4.362 seconds using v1.01-cache-2.11-cpan-df04353d9ac )