aboutsummaryrefslogtreecommitdiff
path: root/lib/IRC/Ebooks.pm
blob: bd5e923c63a84733ea46a133f09a67a90cd3e8c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;