Component/IRC/Plugin/BotCommand.pm

filter: Statementstmt 88.1% 186 / 211 Branchbran 60.4% 81 / 134 Conditioncond 28.8% 17 / 59 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 100% 20 / 20 Podpod 50.0% 5 / 10 totaltotal 66.1% 309 / 467 CC 96 SCAR 37.6
CC
96
Cov
70%
_handle_cmd
88.6
_get_help
57.0
resolve_alias
10.9
CRAP 42.9
↑ Summary Component/IRC/Qnet/State.pm »
1 package POE::Component::IRC::Plugin::BotCommand;
2 # vim: set expandtab ts=4 sw=4 ai:
3 9 $POE::Component::IRC::Plugin::BotCommand::VERSION = '6.95';
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use IRC::Utils qw( parse_user strip_color strip_formatting );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶sub new {
Subroutine new: called CC=7 SCAR=19.9
Pod documented
11 8 my ($package) = shift;
12 8 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 8 8
13 8 my %args = @_;
14  
15 8 ▶ $args{Method} = 'notice' if !defined $args{Method};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"Method"} 8 0 8
16  
17 8 for my $cmd (keys %{ $args{Commands} }) {
18 11 ▶ if (ref $args{Commands}->{$cmd} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"Commands"}{$cmd} eq "HASH") 1 10 11
19 croak "$cmd: no info provided"
20 1 ▶ if !exists $args{Commands}->{$cmd}->{info} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"Commands"}{$cmd}{"info"} 0 1 1
21 $args{Commands}->{lc $cmd}->{handler} =
22 sprintf("irc_botcmd_%s", lc($cmd))
23 1 ▶ if !$args{Commands}->{lc $cmd}->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"Commands"}{lc $cmd}{"handler"} 1 0 1
24 }
25 11 $args{Commands}->{lc $cmd} = delete $args{Commands}->{$cmd};
26 }
27 8 return bless \%args, $package;
28 }
29  
30 8 ▶sub PCI_register {
Subroutine PCI_register: called CC=5 SCAR=16.2
Pod undocumented
31 8 my ($self, $irc) = splice @_, 0, 2;
32  
33 8 ▶ $self->{Addressed} = 1 if !defined $self->{Addressed};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Addressed"} 5 3 8
34 8 ▶ $self->{Prefix} = '!' if !defined $self->{Prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Prefix"} 5 3 8
35 8 ▶ $self->{In_channels} = 1 if !defined $self->{In_channels};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"In_channels"} 8 0 8
36 8 ▶ $self->{In_private} = 1 if !defined $self->{In_private};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"In_private"} 8 0 8
37 8 $self->{rx_cmd_args} = qr/^(\S+)(?:\s+(.+))?$/;
38 8 $self->{irc} = $irc;
39  
40 8 $irc->plugin_register( $self, 'SERVER', qw(msg public) );
41 8 return 1;
42 }
43  
44 8 ▶sub PCI_unregister {
Subroutine PCI_unregister: called CC=1 SCAR=0.0
Pod undocumented
45 8 return 1;
46 }
47  
48 2 ▶sub S_msg {
Subroutine S_msg: called CC=6 SCAR=19.7
Pod undocumented
49 2 my ($self, $irc) = splice @_, 0, 2;
50 2 my $who = ${ $_[0] };
51 2 my $where = parse_user($who);
52 2 my $what = ${ $_[2] };
53  
54 2 ▶ return PCI_EAT_NONE if !$self->{In_private};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"In_private"} 0 2 2
55 2 $what = $self->_normalize($what);
56  
57 2 ▶ if (!$self->{Bare_private}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Bare_private"}) 0 2 2
58 0 ▶ return PCI_EAT_NONE if $what !~ s/^\Q$self->{Prefix}\E//;
Branch: 1 branch
BranchTrueFalseTotal
unless $what =~ s/^\Q$self->{"Prefix"}\E// 0 0 0
59 }
60  
61 2 my ($cmd, $args);
62 2 ▶ if (!(($cmd, $args) = $what =~ $self->{rx_cmd_args})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $what =~ /$self->{"rx_cmd_args"}/) 0 2 2
63 0 return PCI_EAT_NONE;
64 }
65  
66 2 $self->_handle_cmd($who, $where, $cmd, $args);
67 2 ▶ return $self->{Eat} ? PCI_EAT_PLUGIN : PCI_EAT_NONE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Eat'} ? : 0 2 2
68 }
69  
70 16 ▶sub S_public {
Subroutine S_public: called CC=7 SCAR=19.8
Pod undocumented
71 16 my ($self, $irc) = splice @_, 0, 2;
72 16 my $who = ${ $_[0] };
73 16 my $where = ${ $_[1] }->[0];
74 16 my $what = ${ $_[2] };
75 16 my $me = $irc->nick_name();
76  
77 16 ▶ return PCI_EAT_NONE if !$self->{In_channels};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"In_channels"} 0 16 16
78 16 $what = $self->_normalize($what);
79  
80 16 ▶ if ($self->{Addressed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Addressed'}) { } 10 6 16
81 10 ▶ return PCI_EAT_NONE if !(($what) = $what =~ m/^\s*\Q$me\E[:,;.!?~]?\s*(.*)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless ($what) = $what =~ /^\s*\Q$me\E[:,;.!?~]?\s*(.*)$/ 0 10 10
82 }
83 else {
84 6 ▶ return PCI_EAT_NONE if $what !~ s/^\Q$self->{Prefix}\E//;
Branch: 1 branch
BranchTrueFalseTotal
unless $what =~ s/^\Q$self->{"Prefix"}\E// 0 6 6
85 }
86  
87 16 my ($cmd, $args);
88 16 ▶ if (!(($cmd, $args) = $what =~ $self->{rx_cmd_args})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $what =~ /$self->{"rx_cmd_args"}/) 0 16 16
89 0 return PCI_EAT_NONE;
90 }
91  
92 16 $self->_handle_cmd($who, $where, $cmd, $args);
93 16 ▶ return $self->{Eat} ? PCI_EAT_PLUGIN : PCI_EAT_NONE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Eat'} ? : 2 14 16
94 }
95  
96 18 ▶sub _normalize {
Subroutine _normalize: called CC=1 SCAR=0.0
97 18 my ($self, $line) = @_;
98 18 $line = strip_color($line);
99 18 $line = strip_formatting($line);
100 18 return $line;
101 }
102  
103 18 ▶sub _handle_cmd {
Subroutine _handle_cmd: called CC=36 SCAR=44.8
104 18 my ($self, $who, $where, $cmd, $args) = @_;
105 18 my $irc = $self->{irc};
106 18 ▶ my $chantypes = join('', @{ $irc->isupport('CHANTYPES') || ['#', '&']});
Condition50%1/2
$irc->isupport('CHANTYPES') || ['#', '&']
l!l
180
MC/DC0%0/1
$irc->isupport('CHANTYPES') || ['#', '&']
$irc->isupport('CHANTYPES')
Truth table50%1/2
A
A= $irc->isupport('CHANTYPES')
Aresult
11
00
107 18 ▶ my $public = $where =~ /^[$chantypes]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$where =~ /^[$chantypes]/ ? : 16 2 18
108 18 $cmd = lc $cmd;
109  
110 18 my $cmd_unresolved = $cmd;
111  
112 18 ▶ if((my $cmd_resolved = $self->resolve_alias($cmd)))
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd_resolved = $self->resolve_alias($cmd)) 0 18 18
113 {
114 0 $cmd = $cmd_resolved;
115 }
116  
117  
118 18 ▶ if (defined $self->{Commands}->{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Commands"}{$cmd}) 11 7 18
119 11 ▶ if (ref $self->{Commands}->{$cmd} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"Commands"}{$cmd} eq "HASH") 1 10 11
120 1 ▶ my @args_array = defined $args ? split /\s+/, $args : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 1 0 1
121 1 ▶ if (defined($self->{Commands}->{$cmd}->{args}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};} and @args_array < @{$self->{"Commands"}{$cmd}{"args"};} || !defined($self->{"Commands"}{$cmd}{"variable"}) && @args_array > @{$self->{"Commands"}{$cmd}{"args"};}) 0 1 1
Condition33%3/9
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};} and @args_array < @{$self->{"Commands"}{$cmd}{"args"};} || !defined($self->{"Commands"}{$cmd}{"variable"}) && @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
!ll&&!rl&&r
010
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}
!ll&&!rl&&r
001
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/4
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};} and @args_array < @{$self->{"Commands"}{$cmd}{"args"};} || !defined($self->{"Commands"}{$cmd}{"variable"}) && @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
defined $self->{"Commands"}{$cmd}{"args"} ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" @{$self->{"Commands"}{$cmd}{"args"};} @args_array < @{$self->{"Commands"}{$cmd}{"args"};} || !defined($self->{"Commands"}{$cmd}{"variable"}) && @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
Truth table20%1/5
A and B and C and D
A= defined $self->{"Commands"}{$cmd}{"args"}B= ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY"C= @{$self->{"Commands"}{$cmd}{"args"};}D= @args_array < @{$self->{"Commands"}{$cmd}{"args"};} || !defined($self->{"Commands"}{$cmd}{"variable"}) && @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
122 ref($self->{Commands}->{$cmd}->{args}) eq 'ARRAY' &&
123 1 ▶ @{ $self->{Commands}->{$cmd}->{args} } &&
Condition33%1/3
@args_array < @{$self->{"Commands"}{$cmd}{"args"};} or not defined $self->{"Commands"}{$cmd}{"variable"} and @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
l!l&&r!l&&!r
001
MC/DC0%0/2
@args_array < @{$self->{"Commands"}{$cmd}{"args"};} or not defined $self->{"Commands"}{$cmd}{"variable"} and @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
@args_array < @{$self->{"Commands"}{$cmd}{"args"};} not defined $self->{"Commands"}{$cmd}{"variable"} and @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
Truth table33%1/3
A or B
A= @args_array < @{$self->{"Commands"}{$cmd}{"args"};}B= not defined $self->{"Commands"}{$cmd}{"variable"} and @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
ABresult
1X1
011
000
124 1 ▶ (@args_array < @{ $self->{Commands}->{$cmd}->{args} } ||
Condition33%1/3
not defined $self->{"Commands"}{$cmd}{"variable"} and @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
!ll&&!rl&&r
100
MC/DC0%0/2
not defined $self->{"Commands"}{$cmd}{"variable"} and @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
not defined $self->{"Commands"}{$cmd}{"variable"} @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
Truth table33%1/3
not(A) and B
A= not defined $self->{"Commands"}{$cmd}{"variable"}B= @args_array > @{$self->{"Commands"}{$cmd}{"args"};}
ABresult
0X0
100
111
125 (!defined $self->{Commands}->{$cmd}->{variable} &&
126 0 @args_array > @{ $self->{Commands}->{$cmd}->{args} }))
127 ) {
128 0 $irc->yield($self->{Method}, $where,
129 "Not enough or too many arguments. See help for $cmd");
130 0 return;
131 }
132  
133 1 ▶ if(defined $self->{Commands}->{$cmd}->{variable} ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Commands"}{$cmd}{"variable"} or defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}) 1 0 1
Condition11%1/9
defined $self->{"Commands"}{$cmd}{"variable"} or defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}
l!l&&r!l&&!r
100
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}
!ll&&!rl&&r
000
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/4
defined $self->{"Commands"}{$cmd}{"variable"} or defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}
defined $self->{"Commands"}{$cmd}{"variable"} defined $self->{"Commands"}{$cmd}{"args"} ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" @{$self->{"Commands"}{$cmd}{"args"};}
Truth table20%1/5
A or B and C and D
A= defined $self->{"Commands"}{$cmd}{"variable"}B= defined $self->{"Commands"}{$cmd}{"args"}C= ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY"D= @{$self->{"Commands"}{$cmd}{"args"};}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
134 (defined($self->{Commands}->{$cmd}->{args}) &&
135 ref($self->{Commands}->{$cmd}->{args}) eq 'ARRAY' &&
136 0 @{ $self->{Commands}->{$cmd}->{args} }))
137 {
138 1 $args = {};
139 1 ▶ if( defined($self->{Commands}->{$cmd}->{args}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}) 1 0 1
Condition33%2/6
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}
!ll&&!rl&&r
001
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/3
defined $self->{"Commands"}{$cmd}{"args"} and ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd}{"args"};}
defined $self->{"Commands"}{$cmd}{"args"} ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY" @{$self->{"Commands"}{$cmd}{"args"};}
Truth table25%1/4
A and B and C
A= defined $self->{"Commands"}{$cmd}{"args"}B= ref $self->{"Commands"}{$cmd}{"args"} eq "ARRAY"C= @{$self->{"Commands"}{$cmd}{"args"};}
ABCresult
0XX0
10X0
1100
1111
140 ref($self->{Commands}->{$cmd}->{args}) eq 'ARRAY' &&
141 1 @{ $self->{Commands}->{$cmd}->{args} })
142 {
143 1 for (@{ $self->{Commands}->{$cmd}->{args} }) {
144 2 my $in_arg = shift @args_array;
145 2 ▶ if (ref $self->{Commands}->{$cmd}->{$_} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"Commands"}{$cmd}{$_} eq "ARRAY") 1 1 2
146 1 my @values = @{ $self->{Commands}->{$cmd}->{$_} };
147 1 shift @values;
148  
149 9 ▶ use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
150 # Check if argument has one of possible values
151 1 ▶ if (none { $_ eq $in_arg} @values) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_ eq $in_arg; } , @values)) 0 1 1
152 0 $irc->yield($self->{Method}, $where,
153 "$_ can be one of ".join '|', @values);
154 0 return;
155 }
156  
157 }
158 2 $args->{$_} = $in_arg;
159 }
160 }
161  
162 # Process remaining arguments if variable is set
163 1 my $arg_cnt = 0;
164 1 ▶ if (defined $self->{Commands}->{$cmd}->{variable}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Commands"}{$cmd}{"variable"}) 1 0 1
165 1 for (@args_array) {
166 1 $args->{"opt".$arg_cnt++} = $_;
167 }
168 }
169 }
170 }
171 }
172  
173 18 ▶ if (ref $self->{Auth_sub} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"Auth_sub"} eq "CODE") 3 15 18
174 3 my ($authed, $errors) = $self->{Auth_sub}->($self->{irc}, $who, $where, $cmd, $args, $cmd_unresolved);
175  
176 3 ▶ if (!$authed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($authed) 1 2 3
177 1 ▶ my @errors = ref $errors eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $errors eq 'ARRAY' ? : 0 1 1
178 ? @$errors
179 : 'You are not authorized to use this command.';
180 1 ▶ if (!$self->{Ignore_unauthorized}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Ignore_unauthorized"}) 1 0 1
181 1 for my $error (@errors) {
182 1 $irc->yield($self->{Method}, $where, $error);
183 }
184 }
185 1 return;
186 }
187 }
188  
189 17 ▶ if (defined $self->{Commands}->{$cmd}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $self->{'Commands'}{$cmd}) { } 11 6 17
elsif ($cmd =~ /^help$/i) { } 6 0 6
elsif (not $self->{'Ignore_unknown'}) { } 0 0 0
190 11 ▶ my $handler = (ref($self->{Commands}->{$cmd}) eq 'HASH' ? $self->{Commands}->{$cmd}->{handler} : "irc_botcmd_$cmd");
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'Commands'}{$cmd} eq 'HASH' ? : 1 10 11
191 11 $irc->send_event_next($handler => $who, $where, $args, $cmd, $cmd_unresolved);
192 }
193 elsif ($cmd =~ /^help$/i) {
194 6 my @help = $self->_get_help($args, $public);
195 6 $irc->yield($self->{Method} => $where => $_) for @help;
196 }
197 elsif (!$self->{Ignore_unknown}) {
198 0 my @help = $self->_get_help($cmd, $public);
199 0 $irc->yield($self->{Method} => $where => $_) for @help;
200 }
201  
202 17 return;
203 }
204  
205 6 ▶sub _get_help {
Subroutine _get_help: called CC=25 SCAR=40.4
206 6 my ($self, $args, $public) = @_;
207 6 my $irc = $self->{irc};
208 my $p = $self->{Addressed} && $public
209 ? $irc->nick_name().': '
210 6 ▶ : $self->{Prefix};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Addressed'} && $public ? : 6 0 6
Condition33%1/3
$self->{"Addressed"} and $public
!ll&&!rl&&r
006
MC/DC0%0/2
$self->{"Addressed"} and $public
$self->{"Addressed"} $public
Truth table33%1/3
A and B
A= $self->{"Addressed"}B= $public
ABresult
0X0
100
111
211  
212 6 my @help;
213 6 ▶ if (defined $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) { } 3 3 6
214 3 my $cmd = (split /\s+/, $args, 2)[0];
215  
216 3 $cmd = lc $cmd;
217  
218 3 ▶ my $cmd_resolved = $self->resolve_alias($cmd) || $cmd;
Condition33%1/3
$self->resolve_alias($cmd) || $cmd
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->resolve_alias($cmd) || $cmd
$self->resolve_alias($cmd) $cmd
Truth table33%1/3
A || B
A= $self->resolve_alias($cmd)B= $cmd
ABresult
1X1
011
000
219  
220 3 ▶ if (exists $self->{Commands}->{$cmd_resolved}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'Commands'}{$cmd_resolved}) { } 1 2 3
221 1 ▶ if (ref $self->{Commands}->{$cmd_resolved} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Commands'}{$cmd_resolved} eq 'HASH') { } 1 0 1
222 push @help, "Syntax: $p$cmd".
223 ( defined($self->{Commands}->{$cmd_resolved}->{args}) &&
224 ref($self->{Commands}->{$cmd_resolved}->{args}) eq 'ARRAY' ?
225 1 ▶ " ".join ' ', @{ $self->{Commands}->{$cmd_resolved}->{args} } :
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Commands'}{$cmd_resolved}{'variable'} ? : 1 0 1
226 "" ).
227 (defined $self->{Commands}->{$cmd_resolved}->{variable} ?
228 1 ▶ " ..." : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Commands'}{$cmd_resolved}{'args'} && ref $self->{'Commands'}{$cmd_resolved}{'args'} eq 'ARRAY' ? : 1 0 1
Condition33%1/3
defined $self->{"Commands"}{$cmd_resolved}{"args"} and ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"Commands"}{$cmd_resolved}{"args"} and ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY"
defined $self->{"Commands"}{$cmd_resolved}{"args"} ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"Commands"}{$cmd_resolved}{"args"}B= ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY"
ABresult
0X0
100
111
229 push @help, split /\015?\012/,
230 1 "Description: ".$self->{Commands}->{$cmd_resolved}->{info};
231 1 ▶ if( defined($self->{Commands}->{$cmd_resolved}->{args}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Commands"}{$cmd_resolved}{"args"} and ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd_resolved}{"args"};}) 1 0 1
Condition33%2/6
defined $self->{"Commands"}{$cmd_resolved}{"args"} and ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd_resolved}{"args"};}
!ll&&!rl&&r
001
defined $self->{"Commands"}{$cmd_resolved}{"args"} and ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/3
defined $self->{"Commands"}{$cmd_resolved}{"args"} and ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY" and @{$self->{"Commands"}{$cmd_resolved}{"args"};}
defined $self->{"Commands"}{$cmd_resolved}{"args"} ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY" @{$self->{"Commands"}{$cmd_resolved}{"args"};}
Truth table25%1/4
A and B and C
A= defined $self->{"Commands"}{$cmd_resolved}{"args"}B= ref $self->{"Commands"}{$cmd_resolved}{"args"} eq "ARRAY"C= @{$self->{"Commands"}{$cmd_resolved}{"args"};}
ABCresult
0XX0
10X0
1100
1111
232 ref($self->{Commands}->{$cmd_resolved}->{args}) eq 'ARRAY' &&
233 1 @{ $self->{Commands}->{$cmd_resolved}->{args} })
234 {
235 1 push @help, "Arguments:";
236  
237 1 for my $arg (@{ $self->{Commands}->{$cmd_resolved}->{args} }) {
238 2 ▶ next if not defined $self->{Commands}->{$cmd_resolved}->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Commands"}{$cmd_resolved}{$arg} 0 2 2
239 2 ▶ if (ref $self->{Commands}->{$cmd_resolved}->{$arg} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Commands'}{$cmd_resolved}{$arg} eq 'ARRAY') { } 1 1 2
240 1 my @arg_usage = @{$self->{Commands}->{$cmd_resolved}->{$arg}};
241 1 push @help, " $arg: ".$arg_usage[0].
242 " (".(join '|', @arg_usage[1..$#arg_usage]).")"
243 }
244 else {
245 push @help, " $arg: ".
246 1 $self->{Commands}->{$cmd_resolved}->{$arg};
247 }
248 }
249 }
250  
251 push @help, "Alias of: ${p}${cmd_resolved}" .
252 (ref($self->{Commands}->{$cmd_resolved}->{args}) eq 'ARRAY' ?
253 0 ▶ " ".join ' ', @{ $self->{Commands}->{$cmd_resolved}->{args} } :
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Commands'}{$cmd_resolved}{'variable'} ? : 0 0 0
254 "" ).
255 (defined $self->{Commands}->{$cmd_resolved}->{variable} ?
256 1 ▶ " ..." : "")
Branch: 2 branches
BranchTrueFalseTotal
if $cmd_resolved ne $cmd 0 1 1
ref $self->{'Commands'}{$cmd_resolved}{'args'} eq 'ARRAY' ? : 0 0 0
257 if $cmd_resolved ne $cmd;
258  
259 0 my @aliases = grep { $_ ne $cmd } $self->list_aliases($cmd_resolved);
260  
261 1 ▶ if($cmd_resolved ne $cmd)
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_resolved ne $cmd) 0 1 1
262 {
263 0 push @aliases, $cmd_resolved;
264 }
265  
266 1 ▶ push @help, "Aliases: ".join( " ", @aliases) if scalar(@aliases);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @aliases 0 1 1
267 }
268 else {
269 0 @help = split /\015?\012/, $self->{Commands}->{$cmd};
270 }
271 }
272 else {
273 2 push @help, "Unknown command: $cmd";
274 2 push @help, "To get a list of commands, use: ${p}help";
275 }
276 }
277 else {
278 3 ▶ if (keys %{ $self->{Commands} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{'Commands'};}) { } 1 2 3
279 1 push @help, 'Commands: ' . join ', ', sort keys %{ $self->{Commands} };
280 1 push @help, "For more details, use: ${p}help <command>";
281 }
282 else {
283 2 push @help, 'No commands are defined';
284 }
285 }
286  
287 6 ▶ if(ref($self->{'Help_sub'}) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Help_sub'} eq 'CODE') { } 0 6 6
288 {
289 0 ▶ my ($cmd, $args) = (defined $args ? split /\s+/, $args, 2 : ('', ''));
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
290  
291 0 ▶ my $cmd_resolved = $self->resolve_alias($cmd) || $cmd;
Condition0%0/3
$self->resolve_alias($cmd) || $cmd
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->resolve_alias($cmd) || $cmd
$self->resolve_alias($cmd) $cmd
Truth table0%0/3
A || B
A= $self->resolve_alias($cmd)B= $cmd
ABresult
1X1
011
000
292  
293 0 return $self->{'Help_sub'}->($self->{irc}, $cmd, $cmd_resolved, $args, @help);
294 }
295 else
296 {
297 6 return @help;
298 }
299 }
300  
301 8 ▶sub add {
Subroutine add: called CC=5 SCAR=16.7
Pod documented
302 8 my ($self, $cmd, $usage) = @_;
303 8 $cmd = lc $cmd;
304 8 ▶ return if exists $self->{Commands}->{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Commands"}{$cmd} 0 8 8
305  
306 8 ▶ if (ref $usage eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $usage eq "HASH") 1 7 8
307 1 ▶ return if !exists $usage->{info} || !@{ $usage->{args} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $usage->{"info"} and @{$usage->{"args"};} 0 1 1
Condition33%1/3
exists $usage->{"info"} and @{$usage->{"args"};}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $usage->{"info"} and @{$usage->{"args"};}
exists $usage->{"info"} @{$usage->{"args"};}
Truth table33%1/3
A and B
A= exists $usage->{"info"}B= @{$usage->{"args"};}
ABresult
0X0
100
111
308 }
309  
310 8 $self->{Commands}->{$cmd} = $usage;
311 8 return 1;
312 }
313  
314 5 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
315 5 my ($self, $cmd) = @_;
316 5 $cmd = lc $cmd;
317 5 ▶ return if !exists $self->{Commands}->{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Commands"}{$cmd} 0 5 5
318 5 delete $self->{Commands}->{$cmd};
319 5 return 1;
320 }
321  
322 26 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
323 26 my ($self) = @_;
324 26 return %{ $self->{Commands} };
325 }
326  
327 21 ▶sub resolve_alias {
Subroutine resolve_alias: called CC=7 SCAR=23.8
Pod documented
328 21 my ($self, $alias) = @_;
329
330 21 my %cmds = $self->list();
331  
332 #TODO: refactor using smartmatch/Perl6::Junction if feasible
333 21 ▶ while(my ($cmd, $info) = each(%cmds))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %cmds) 32 21 53
334 {
335 32 ▶ next unless ref($info) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info eq "HASH" 26 6 32
336 6 ▶ next unless $info->{aliases} && ref($info->{aliases}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"aliases"} and ref $info->{"aliases"} eq "ARRAY" 6 0 6
Condition33%1/3
$info->{"aliases"} and ref $info->{"aliases"} eq "ARRAY"
!ll&&!rl&&r
600
MC/DC0%0/2
$info->{"aliases"} and ref $info->{"aliases"} eq "ARRAY"
$info->{"aliases"} ref $info->{"aliases"} eq "ARRAY"
Truth table33%1/3
A and B
A= $info->{"aliases"}B= ref $info->{"aliases"} eq "ARRAY"
ABresult
0X0
100
111
337 0 my @aliases = @{$info->{aliases}};
338
339 0 foreach my $cmdalias (@aliases)
340 {
341 0 ▶ return $cmd if $alias eq $cmdalias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias eq $cmdalias 0 0 0
342 }
343 }
344  
345 21 return undef;
346 }
347  
348 sub list_aliases
349 1 ▶{
Subroutine list_aliases: called CC=5 SCAR=19.6
Pod undocumented
350 1 my ($self, $cmd) = @_;
351 1 $cmd = lc $cmd;
352 1 ▶ return if !exists $self->{Commands}->{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Commands"}{$cmd} 0 1 1
353 1 ▶ return unless ref($self->{Commands}->{$cmd}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"Commands"}{$cmd} eq "HASH" 0 1 1
354 1 ▶ return unless exists $self->{Commands}->{$cmd}->{aliases} && ref($self->{Commands}->{$cmd}->{aliases}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Commands"}{$cmd}{"aliases"} and ref $self->{"Commands"}{$cmd}{"aliases"} eq "ARRAY" 1 0 1
Condition33%1/3
exists $self->{"Commands"}{$cmd}{"aliases"} and ref $self->{"Commands"}{$cmd}{"aliases"} eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
exists $self->{"Commands"}{$cmd}{"aliases"} and ref $self->{"Commands"}{$cmd}{"aliases"} eq "ARRAY"
exists $self->{"Commands"}{$cmd}{"aliases"} ref $self->{"Commands"}{$cmd}{"aliases"} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $self->{"Commands"}{$cmd}{"aliases"}B= ref $self->{"Commands"}{$cmd}{"aliases"} eq "ARRAY"
ABresult
0X0
100
111
355 0 return @{$self->{Commands}->{$cmd}->{aliases}};
356  
357 }
358  
359 9 1;
360  
361 =encoding utf8
362  
363 =head1 NAME
364  
365 POE::Component::IRC::Plugin::BotCommand - A PoCo-IRC plugin which handles
366 commands issued to your bot
367  
368 =head1 SYNOPSIS
369  
370 use POE;
371 use POE::Component::Client::DNS;
372 use POE::Component::IRC;
373 use POE::Component::IRC::Plugin::BotCommand;
374  
375 my @channels = ('#channel1', '#channel2');
376 my $dns = POE::Component::Client::DNS->spawn();
377 my $irc = POE::Component::IRC->spawn(
378 nick => 'YourBot',
379 server => 'some.irc.server',
380 );
381  
382 POE::Session->create(
383 package_states => [
384 main => [ qw(_start irc_001 irc_botcmd_slap irc_botcmd_lookup dns_response) ],
385 ],
386 );
387  
388 $poe_kernel->run();
389  
390 sub _start {
391 $irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new(
392 Commands => {
393 slap => 'Takes one argument: a nickname to slap.',
394 lookup => 'Takes two arguments: a record type (optional), and a host.',
395 }
396 ));
397 $irc->yield(register => qw(001 botcmd_slap botcmd_lookup));
398 $irc->yield(connect => { });
399 }
400  
401 # join some channels
402 sub irc_001 {
403 $irc->yield(join => $_) for @channels;
404 return;
405 }
406  
407 # the good old slap
408 sub irc_botcmd_slap {
409 my $nick = (split /!/, $_[ARG0])[0];
410 my ($where, $arg) = @_[ARG1, ARG2];
411 $irc->yield(ctcp => $where, "ACTION slaps $arg");
412 return;
413 }
414  
415 # non-blocking dns lookup
416 sub irc_botcmd_lookup {
417 my $nick = (split /!/, $_[ARG0])[0];
418 my ($where, $arg) = @_[ARG1, ARG2];
419 my ($type, $host) = $arg =~ /^(?:(\w+) )?(\S+)/;
420  
421 my $res = $dns->resolve(
422 event => 'dns_response',
423 host => $host,
424 type => $type,
425 context => {
426 where => $where,
427 nick => $nick,
428 },
429 );
430 $poe_kernel->yield(dns_response => $res) if $res;
431 return;
432 }
433  
434 sub dns_response {
435 my $res = $_[ARG0];
436 my @answers = map { $_->rdatastr } $res->{response}->answer() if $res->{response};
437  
438 $irc->yield(
439 'notice',
440 $res->{context}->{where},
441 $res->{context}->{nick} . (@answers
442 ? ": @answers"
443 : ': no answers for "' . $res->{host} . '"')
444 );
445  
446 return;
447 }
448  
449 =head1 DESCRIPTION
450  
451 POE::Component::IRC::Plugin::BotCommand is a
452 L<POE::Component::IRC|POE::Component::IRC> plugin. It provides you with a
453 standard interface to define bot commands and lets you know when they are
454 issued. Commands are accepted as channel or private messages.
455  
456 The plugin will respond to the 'help' command by default, listing available
457 commands and information on how to use them. However, if you add a help
458 command yourself, that one will be used instead.
459  
460 =head1 METHODS
461  
462 =head2 C<new>
463  
464 B<'Commands'>, a hash reference, with your commands as keys, and usage
465 information as values. If the usage string contains newlines, the plugin
466 will send one message for each line.
467  
468 If a command's value is a HASH ref like this:
469  
470 $irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new(
471 Commands => {
472 slap => {
473 info => 'Slap someone',
474 args => [qw(nickname)],
475 nickname => 'nickname to slap'
476 }
477 }
478 ));
479  
480 The args array reference is than used to validate number of arguments required
481 and to name arguments passed to event handler. Help is than generated from
482 C<info> and other hash keys which represent arguments (they are optional).
483  
484 An optional C<handler> key can be specified inside the HASH ref to override the event handler.
485 The irc_botcmd_ prefix is not automatically prepended to the handler name when overriding it.
486  
487 An optional C<aliases> key can be specified inside the HASH ref containing a array ref with alias names.
488 The aliases can be specified for help and to run the command.
489  
490 =head3 Accepting commands
491  
492 B<'In_channels'>, a boolean value indicating whether to accept commands in
493 channels. Default is true.
494  
495 B<'In_private'>, a boolean value indicating whether to accept commands in
496 private. Default is true.
497  
498 B<'Addressed'>, requires users to address the bot by name in order
499 to issue commands. Default is true.
500  
501 B<'Prefix'>, a string which all commands must be prefixed with (except in
502 channels when B<'Addressed'> is true). Default is '!'. You can set it to ''
503 to allow bare commands.
504  
505 B<'Bare_private'>, a boolean value indicating whether bare commands (without
506 the prefix) are allowed in private messages. Default is false.
507  
508 =head3 Authorization
509  
510 B<'Auth_sub'>, a subroutine reference which, if provided, will be called
511 for every command. The subroutine will be called in list context. If the
512 first value returned is true, the command will be processed as normal. If
513 the value is false, then no events will be generated, and an error message
514 will possibly be sent back to the user.
515  
516 You can override the default error message by returning a second value, an
517 array reference of (zero or more) strings. Each string will be sent as a
518 message to the user.
519  
520 Your subroutine will be called with the following arguments:
521  
522 =over 4
523  
524 =item 1. The IRC component object
525  
526 =item 2. The nick!user@host of the user
527  
528 =item 3. The place where the command was issued (the nickname of the user if
529 it was in private)
530  
531 =item 4. The name of the command
532  
533 =item 5. The command argument string
534  
535 =back
536  
537 B<'Ignore_unauthorized'>, if true, the plugin will ignore unauthorized
538 commands, rather than printing an error message upon receiving them. This is
539 only relevant if B<'Auth_sub'> is also supplied. Default is false.
540  
541 =head3 Help Command
542  
543 B<'Help_sub'>, a subroutine reference which, if provided, will be called upon
544 the end of the predefined help command. The subroutine will be called in list context.
545  
546 Your subroutine will be called with the following arguments:
547  
548 =over 4
549  
550 =item 1. The IRC component object
551  
552 =item 2. The command.
553  
554 =item 3. The resolved command(after alias processing).
555  
556 =item 4. The arguments.
557  
558 =item 5. The generated help text as array.
559  
560  
561 =back
562  
563  
564 =head3 Miscellaneous
565  
566 B<'Ignore_unknown'>, if true, the plugin will ignore undefined commands,
567 rather than printing a help message upon receiving them. Default is false.
568  
569 B<'Method'>, how you want help messages to be delivered. Valid options are
570 'notice' (the default) and 'privmsg'.
571  
572 B<'Eat'>, set to true to make the plugin hide
573 L<C<irc_public>|POE::Component::IRC/irc_public> events from other plugins
574 when they look like commands. Probably only useful when a B<'Prefix'> is
575 defined. Default is false.
576  
577 Returns a plugin object suitable for feeding to
578 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
579  
580 =head2 C<add>
581  
582 Adds a new command. Takes two arguments, the name of the command, and a string
583 or hash reference containing its usage information (see C<new>). Returns false
584 if the command has already been defined or no info or arguments are provided,
585 true otherwise.
586  
587 =head2 C<remove>
588  
589 Removes a command. Takes one argument, the name of the command. Returns false
590 if the command wasn't defined to begin with, true otherwise.
591  
592 =head2 C<list>
593  
594 Takes no arguments. Returns a list of key/value pairs, the keys being the
595 command names and the values being the usage strings or hash references.
596  
597 =head2 C<resolve_alias>
598  
599 Takes one argument, a string to match against command aliases, if no matching
600 command can be found undef is returned.
601  
602 =head1 OUTPUT EVENTS
603  
604 =head2 C<irc_botcmd_*>
605  
606 You will receive an event like this for every valid command issued. E.g. if
607 'slap' were a valid command, you would receive an C<irc_botcmd_slap> event
608 every time someone issued that command. It receives the following arguments:
609  
610 =over 4
611  
612 =item * C<ARG0>: the nick!hostmask of the user who issued the command.
613  
614 =item * C<ARG1> is the name of the channel in which the command was issued,
615 or the sender's nickname if this was a private message.
616  
617 =item * C<ARG2>: a string of arguments to the command, or hash reference with
618 arguments in case you defined command along with arguments, or undef if there
619 were no arguments
620  
621 =back
622  
623 =head1 AUTHOR
624  
625 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
626  
627 =cut
↑ Summary Component/IRC/Qnet/State.pm »