summaryrefslogtreecommitdiffstats
path: root/lib/Bot/Games.pm
blob: f306b529eaf89a7a0668b9a01a0f5a9ab85eea56 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package Bot::Games;
use Moose;
use Config::Any;
use IM::Engine;
with 'MooseX::ConfigFromFile';

has '+configfile' => (
    default => "$ENV{HOME}/.botgamesrc",
);

has protocol => (
    is      => 'ro',
    isa     => 'Str',
    default => 'REPL',
);

has name => (
    is  => 'ro',
    isa => 'Str',
);

has password => (
    is  => 'ro',
    isa => 'Str',
);

has server => (
    is  => 'ro',
    isa => 'Str',
);

has port => (
    is      => 'ro',
    isa     => 'Int',
    default => 6667,
);

has channels => (
    is      => 'ro',
    isa     => 'ArrayRef[Str]',
    default => sub { [] },
);

has namespace => (
    is      => 'ro',
    isa     => 'Str',
    default => 'Bot::Games::Game',
);

has exclude_commands => (
    is      => 'ro',
    isa     => 'Str|RegexpRef|ArrayRef[Str|RegexpRef]',
    default => sub { qr/^Bot::Games::Game::Role/ },
);

has only_commands => (
    is  => 'ro',
    isa => 'Str|RegexpRef|ArrayRef[Str|RegexpRef]',
);

has prefix => (
    is  => 'ro',
    isa => 'Str',
);

has alias => (
    is  => 'ro',
    isa => 'HashRef[Str]',
);

has _credentials => (
    is       => 'ro',
    isa      => 'Maybe[HashRef]',
    builder  => '_build_credentials',
    lazy     => 1,
    init_arg => undef,
);

has _plugin_config => (
    is       => 'ro',
    isa      => 'HashRef',
    builder  => '_build_plugin_config',
    lazy     => 1,
    init_arg => undef,
);

has _ime => (
    is       => 'ro',
    isa      => 'IM::Engine',
    builder  => '_build_ime',
    lazy     => 1,
    init_arg => undef,
    handles  => ['run'],
);

sub BUILD {
    my $self = shift;
    $self->_ime;
}

sub _check_required {
    my $self = shift;
    my @attrs = @_;
    my $protocol = $self->protocol;
    for my $attr (@attrs) {
        die "Protocol $protocol requires option $attr to be set"
            unless defined $self->$attr;
    }
}

sub _build_credentials {
    my $self = shift;
    my $protocol = $self->protocol;
    if ($protocol eq 'AIM') {
        $self->_check_required(qw(name password));
        return {
            screenname => $self->name,
            password   => $self->password
        };
    }
    elsif ($protocol eq 'Jabber') {
        $self->_check_required(qw(name password));
        return {
            jid      => $self->name,
            password => $self->password,
        };
    }
    elsif ($protocol eq 'IRC') {
        $self->_check_required(qw(server port channels name));
        return {
            server   => $self->server,
            port     => $self->port,
            channels => $self->channels,
            nick     => $self->name,
        };
    }
    return;
}

sub _build_plugin_config {
    my $self = shift;
    my %config;
    $config{namespace} = $self->namespace
        if defined $self->namespace;
    $config{exclude_commands} = $self->exclude_commands
        if defined $self->exclude_commands;
    $config{only_commands} = $self->only_commands
        if defined $self->only_commands;
    $config{prefix} = $self->prefix
        if defined $self->prefix;
    $config{alias} = $self->alias
        if defined $self->alias;
    return \%config;
}

sub _build_ime {
    my $self = shift;
    my $credentials = $self->_credentials;
    my $plugin_config = $self->_plugin_config;
    IM::Engine->new(
        interface => {
            protocol => $self->protocol,
            $credentials ? (credentials => $credentials) : (),
        },
        plugins => [
            Commands => $plugin_config,
        ],
    );
}

# XXX: cargo-culted from mx-simpleconfig, so i can disable use_ext
sub get_config_from_file {
    my ($class, $file) = @_;

    my $raw_cfany = Config::Any->load_files({
        files   => [ $file ],
        use_ext => 0,
    });

    die q{Specified configfile '} . $file
      . q{' does not exist, is empty, or is not readable}
        unless $raw_cfany->[0]
            && exists $raw_cfany->[0]->{$file};

    my $raw_config = $raw_cfany->[0]->{$file};

    die "configfile must represent a hash structure"
        unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH';

    $raw_config;
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;