File Coverage

blib/lib/App/Alice/Commands.pm
Criterion Covered Total %
statement 6 32 18.7
branch 0 10 0.0
condition 0 6 0.0
subroutine 2 8 25.0
pod 1 6 16.6
total 9 62 14.5


line stmt bran cond sub pod time code
1             package App::Alice::Commands;
2              
3 4     4   23 use Any::Moose;
  4         7  
  4         29  
4 4     4   1910 use Encode;
  4         7  
  4         2266  
5              
6             has 'handlers' => (
7             is => 'rw',
8             isa => 'ArrayRef',
9             default => sub {[]},
10             );
11              
12             has 'app' => (
13             is => 'ro',
14             isa => 'App::Alice',
15             weak_ref => 1,
16             required => 1,
17             );
18              
19             sub BUILD {
20 0     0 1   my $self = shift;
21 0           $self->reload_handlers;
22             }
23              
24             sub reload_handlers {
25 0     0 0   my $self = shift;
26 0           my $commands_file = $self->app->config->assetdir . "/commands.pl";
27 0 0         if (-e $commands_file) {
28 0           my $commands = do $commands_file;
29 0 0 0       if ($commands and ref $commands eq "ARRAY") {
30 0 0         $self->handlers($commands) if $commands;
31             }
32             else {
33 0           warn "$!\n";
34             }
35             }
36             }
37              
38             sub handle {
39 0     0 0   my ($self, $command, $window) = @_;
40 0           for my $handler (@{$self->handlers}) {
  0            
41 0           my $re = $handler->{re};
42 0 0         if ($command =~ /$re/) {
43 0           my @args = grep {defined $_} ($5, $4, $3, $2, $1); # up to 5 captures
  0            
44 0 0 0       if ($handler->{in_channel} and !$window->is_channel) {
45 0           $self->reply($window, "$command can only be used in a channel");
46             }
47             else {
48 0           $handler->{code}->($self, $window, @args);
49             }
50 0           return;
51             }
52             }
53             }
54              
55             sub show {
56 0     0 0   my ($self, $window, $message) = @_;
57 0           $self->broadcast($window->format_message($window->nick, $message));
58             }
59              
60             sub reply {
61 0     0 0   my ($self, $window, $message) = @_;
62 0           $self->broadcast($window->format_announcement($message));
63             }
64              
65             sub broadcast {
66 0     0 0   my ($self, @messages) = @_;
67 0           $self->app->broadcast(@messages);
68             }
69              
70             __PACKAGE__->meta->make_immutable;
71             1;