Component/IRC/Plugin/BotCommand.pm

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