diff options
Diffstat (limited to 'lib/IRC/Ebooks.pm')
-rw-r--r-- | lib/IRC/Ebooks.pm | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/IRC/Ebooks.pm b/lib/IRC/Ebooks.pm new file mode 100644 index 0000000..bd5e923 --- /dev/null +++ b/lib/IRC/Ebooks.pm @@ -0,0 +1,95 @@ +# +# Learn from an IRC log, and make conversation. +# +# Author: Tom Ryder <tom@sanctum.geek.nz> +# +package IRC::Ebooks; +use parent qw( Bot::BasicBot ); + +# Force me to write this properly +use warnings; +use strict; +use utf8; +use autodie; + +# Everything's UTF8 +use utf8::all; + +# Require Perl v5.14 +use 5.014; + +# Import required modules +use Any::Moose; # dpkg: libany-moose-perl +use Carp; # dpkg: perl-base +use Const::Fast; # dpkg: libconst-fast-perl + +# More modules, these aren't in Debian packages, use dh-make-perl(1p) +use Hailo; + +# Decree package version +our $VERSION = 0.1; + +# Default probability of talking +const my $CHATTER_DEFAULT => 1; + +# Constructor +sub new { + my ( $class, $config ) = @_; + my $self = $class->SUPER::new( %{ $config->{irc} } ); + + # Read channel from config + $self->{channel} = $config->{irc}->{channels}; + + # Read brain path from config + $self->{brain} = $config->{options}->{brain} + or croak 'brainfile not specified in config'; + + # Figure out whether to try reading from brainfile + $self->{hailo} = Hailo->new( brain => $self->{brain} ) + or croak sprintf 'Failed to load brain at %s', $self->{brain}; + + # Read chatter from config, or use default + $self->{chatter} = $config->{options}->{chatter}; + $self->{chatter} ||= $CHATTER_DEFAULT; + + # Reject impossible chatter level + if ( $self->{chatter} < 0 or $self->{chatter} > 1 ) { + croak sprintf 'Invalid chatter probability %s', $self->{chatter}; + } + + # All constructed, return self + return $self; +} + +# Learn to say something +sub learn { + my ( $self, $text ) = @_; + return $self->{hailo}->learn($text); +} + +# Learn the topic +sub topic { + my ( $self, $change ) = @_; + return ( $self->{topic} = $change->{topic} ); +} + +# Say something random about the topic +sub speak { + my ($self) = @_; + my $text = $self->{hailo}->reply( $self->{topic} ); + $text = lc $text; + $text =~ s{[.]$}{}msx; + return $text; +} + +# Tick event, used for initiating random chatter +sub tick { + my ($self) = @_; + if ( $self->{chatter} > rand ) { + $self->say( channel => $self->{channel}, body => $self->speak ); + } + return 1; +} + +1; + |