Component/IRC/Plugin/Console.pm

filter: Statementstmt 54.8% 68 / 124 Branchbran 26.4% 9 / 34 Conditioncond 9.0% 1 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 70.0% 14 / 20 Podpod 50.0% 2 / 4 totaltotal 47.0% 94 / 200 CC 26 SCAR 27.2
CC
26
Cov
46%
_client_input
56.0
_dump
18.0
_default
12.4
CRAP 15.2
« Filter/IRC/Compat.pm ↑ Summary Component/IRC/State.pm »
1 package POE::Component::IRC::Plugin::Console;
2 2 $POE::Component::IRC::Plugin::Console::VERSION = '6.95';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IRC::Utils qw(decode_irc);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::IRCD Filter::Line Filter::Stackable);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
12 1 my $package = shift;
13 1 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 1 1
14 1 my %self = @_;
15 1 return bless \%self, $package;
16 }
17  
18 1 ▶sub PCI_register {
Subroutine PCI_register: called CC=1 SCAR=0.0
Pod undocumented
19 1 my ($self, $irc) = splice @_, 0, 2;
20  
21 1 $self->{irc} = $irc;
22  
23 1 $irc->plugin_register( $self, 'SERVER', qw(all) );
24 1 $irc->plugin_register( $self, 'USER', qw(all) );
25  
26 1 POE::Session->create(
27 object_states => [
28 $self => [ qw(_client_error _client_flush _client_input _listener_accept _listener_failed _start _shutdown) ],
29 ],
30 );
31  
32 1 return 1;
33 }
34  
35 1 ▶sub PCI_unregister {
Subroutine PCI_unregister: called CC=1 SCAR=0.0
Pod undocumented
36 1 my ($self, $irc) = splice @_, 0, 2;
37  
38 1 delete $self->{irc};
39 1 $poe_kernel->post( $self->{SESSION_ID} => '_shutdown' );
40 1 $poe_kernel->refcount_decrement( $self->{SESSION_ID}, __PACKAGE__ );
41 1 return 1;
42 }
43  
44 9 ▶sub _dump {
Subroutine _dump: called CC=8 SCAR=28.9
45 9 my ($arg) = @_;
46  
47 9 ▶ if (ref $arg eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 9 9
elsif (ref $arg eq 'HASH') { } 0 9 9
elsif (ref $arg) { } 4 5 9
elsif (defined $arg) { } 5 0 5
48 0 my @elems;
49 0 for my $elem (@$arg) {
50 0 push @elems, _dump($elem);
51 }
52 0 return '['. join(', ', @elems) .']';
53 }
54 elsif (ref $arg eq 'HASH') {
55 0 my @pairs;
56 0 for my $key (keys %$arg) {
57 0 push @pairs, [$key, _dump($arg->{$key})];
58 }
59 0 return '{'. join(', ', map { "$_->[0] => $_->[1]" } @pairs) .'}';
60 }
61 elsif (ref $arg) {
62 4 require overload;
63 4 return overload::StrVal($arg);
64 }
65 elsif (defined $arg) {
66 5 ▶ return $arg if looks_like_number($arg);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($arg) 0 5 5
67 5 return "'".decode_irc($arg)."'";
68 }
69 else {
70 0 return 'undef';
71 }
72 }
73  
74 5 ▶sub _default {
Subroutine _default: called CC=7 SCAR=25.1
75 5 my ($self, $irc, $event) = splice @_, 0, 3;
76 5 ▶ return PCI_EAT_NONE if $event eq 'S_raw';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "S_raw" 0 5 5
77  
78 5 pop @_;
79 5 my @args = map { $$_ } @_;
80 5 my @output;
81  
82 5 for my $i (0..$#args) {
83 9 push @output, "ARG$i: " . _dump($args[$i]);
84 }
85  
86 5 for my $wheel_id ( keys %{ $self->{wheels} } ) {
87 0 ▶ next if ( $self->{exit}->{ $wheel_id } or ( not defined ( $self->{wheels}->{ $wheel_id } ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"exit"}{$wheel_id} or not defined $self->{"wheels"}{$wheel_id} 0 0 0
Condition0%0/3
$self->{"exit"}{$wheel_id} or not defined $self->{"wheels"}{$wheel_id}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"exit"}{$wheel_id} or not defined $self->{"wheels"}{$wheel_id}
$self->{"exit"}{$wheel_id} not defined $self->{"wheels"}{$wheel_id}
Truth table0%0/3
A or not(B)
A= $self->{"exit"}{$wheel_id}B= not defined $self->{"wheels"}{$wheel_id}
ABresult
1X1
011
000
88 0 ▶ next if !$self->{authed}{ $wheel_id };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"authed"}{$wheel_id} 0 0 0
89 0 $self->{wheels}->{ $wheel_id }->put("$event: ".join(', ', @output));
90 }
91  
92 5 return PCI_EAT_NONE;
93 }
94  
95 1 ▶sub _start {
Subroutine _start: called CC=3 SCAR=11.3
96 1 my ($kernel, $self) = @_[KERNEL, OBJECT];
97  
98 1 $self->{SESSION_ID} = $_[SESSION]->ID();
99 1 $kernel->refcount_increment( $self->{SESSION_ID}, __PACKAGE__ );
100 1 $self->{ircd_filter} = POE::Filter::Stackable->new( Filters => [
101 POE::Filter::Line->new(),
102 POE::Filter::IRCD->new(),
103 ]);
104  
105 $self->{listener} = POE::Wheel::SocketFactory->new(
106 BindAddress => 'localhost',
107 1 ▶ BindPort => $self->{bindport} || 0,
Condition50%1/2
$self->{'bindport'} || 0
l!l
01
MC/DC0%0/1
$self->{'bindport'} || 0
$self->{'bindport'}
Truth table50%1/2
A
A= $self->{'bindport'}
Aresult
11
00
108 SuccessEvent => '_listener_accept',
109 FailureEvent => '_listener_failed',
110 Reuse => 'yes',
111 );
112  
113 1 ▶ if ($self->{listener}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'listener'}) { } 1 0 1
114 1 $self->{irc}->send_event( 'irc_console_service' => $self->{listener}->getsockname() );
115 }
116 else {
117 0 $self->{irc}->plugin_del( $self );
118 }
119  
120 1 return;
121 }
122  
123 0 ▶sub _listener_accept {
Subroutine _listener_accept: not called CC=2 SCAR=17.9
124 0 my ($kernel, $self, $socket, $peeradr, $peerport)
125 = @_[KERNEL, OBJECT, ARG0 .. ARG2];
126  
127 my $wheel = POE::Wheel::ReadWrite->new(
128 Handle => $socket,
129 InputFilter => $self->{ircd_filter},
130 0 OutputFilter => POE::Filter::Line->new(),
131 InputEvent => '_client_input',
132 ErrorEvent => '_client_error',
133 FlushedEvent => '_client_flush',
134 );
135  
136 0 ▶ if ( !defined $wheel ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wheel) 0 0 0
137 0 $self->{irc}->send_event( 'irc_console_rw_fail' => $peeradr => $peerport );
138 0 return;
139 }
140  
141 0 my $wheel_id = $wheel->ID();
142 0 $self->{wheels}->{ $wheel_id } = $wheel;
143 0 $self->{authed}->{ $wheel_id } = 0;
144 0 $self->{exit}->{ $wheel_id } = 0;
145 0 $self->{irc}->send_event( 'irc_console_connect' => $peeradr => $peerport => $wheel_id );
146  
147 0 return;
148 }
149  
150 0 ▶sub _listener_failed {
Subroutine _listener_failed: not called CC=1 SCAR=6.9
151 0 delete $_[OBJECT]->{listener};
152 0 return;
153 }
154  
155 0 ▶sub _client_input {
Subroutine _client_input: not called CC=7 SCAR=40.3
156 0 my ($kernel, $self, $input, $wheel_id) = @_[KERNEL, OBJECT, ARG0, ARG1];
157  
158 0 ▶ if ($self->{authed}->{ $wheel_id } && lc ( $input->{command} ) eq 'exit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"authed"}{$wheel_id} and lc $input->{"command"} eq "exit") 0 0 0
Condition0%0/3
$self->{"authed"}{$wheel_id} and lc $input->{"command"} eq "exit"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"authed"}{$wheel_id} and lc $input->{"command"} eq "exit"
$self->{"authed"}{$wheel_id} lc $input->{"command"} eq "exit"
Truth table0%0/3
A and B
A= $self->{"authed"}{$wheel_id}B= lc $input->{"command"} eq "exit"
ABresult
0X0
100
111
159 0 $self->{exit}->{ $wheel_id } = 1;
160 0 ▶ if (defined $self->{wheels}->{ $wheel_id }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"wheels"}{$wheel_id}) 0 0 0
161 0 $self->{wheels}->{ $wheel_id }->put("ERROR * quiting *");
162 }
163 0 return;
164 }
165  
166 0 ▶ if ( $self->{authed}->{ $wheel_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"authed"}{$wheel_id}) 0 0 0
167 0 $self->{irc}->yield( lc ( $input->{command} ) => @{ $input->{params} } );
168 0 return;
169 }
170  
171 0 ▶ if (lc ( $input->{command} ) eq 'pass' && $input->{params}->[0] eq $self->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $input->{"command"} eq "pass" and $input->{"params"}[0] eq $self->{"password"}) 0 0 0
Condition0%0/3
lc $input->{"command"} eq "pass" and $input->{"params"}[0] eq $self->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
lc $input->{"command"} eq "pass" and $input->{"params"}[0] eq $self->{"password"}
lc $input->{"command"} eq "pass" $input->{"params"}[0] eq $self->{"password"}
Truth table0%0/3
A and B
A= lc $input->{"command"} eq "pass"B= $input->{"params"}[0] eq $self->{"password"}
ABresult
0X0
100
111
172 0 $self->{authed}->{ $wheel_id } = 1;
173 0 $self->{wheels}->{ $wheel_id }->put('NOTICE * Password accepted *');
174 0 $self->{irc}->send_event( 'irc_console_authed' => $wheel_id );
175 0 return;
176 }
177  
178 0 $self->{wheels}->{ $wheel_id }->put('NOTICE * Password required * enter PASS <password> *');
179 0 return;
180 }
181  
182 0 ▶sub _client_flush {
Subroutine _client_flush: not called CC=2 SCAR=17.9
183 0 my ($self, $wheel_id) = @_[OBJECT, ARG0];
184 0 ▶ return if !$self->{exit}->{ $wheel_id };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"exit"}{$wheel_id} 0 0 0
185 0 delete $self->{wheels}->{ $wheel_id };
186 0 return;
187 }
188  
189 0 ▶sub _client_error {
Subroutine _client_error: not called CC=1 SCAR=6.9
190 0 my ($self, $wheel_id) = @_[OBJECT, ARG3];
191  
192 0 delete $self->{wheels}->{ $wheel_id };
193 0 delete $self->{authed}->{ $wheel_id };
194 0 $self->{irc}->send_event( 'irc_console_close' => $wheel_id );
195 0 return;
196 }
197  
198 1 ▶sub _shutdown {
Subroutine _shutdown: called CC=1 SCAR=0.0
199 1 my ($kernel, $self) = @_[KERNEL, OBJECT];
200  
201 1 delete $self->{listener};
202 1 delete $self->{wheels};
203 1 delete $self->{authed};
204 1 return;
205 }
206  
207 0 ▶sub getsockname {
Subroutine getsockname: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209 0 ▶ return if !$self->{listener};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"listener"} 0 0 0
210 0 return $self->{listener}->getsockname();
211 }
212  
213 2 1;
214  
215 =encoding utf8
216  
217 =head1 NAME
218  
219 POE::Component::IRC::Plugin::Console - A PoCo-IRC plugin that provides a
220 lightweight debugging and control console for your bot
221  
222 =head1 SYNOPSIS
223  
224 use POE qw(Component::IRC Component::IRC::Plugin::Console);
225  
226 my $nickname = 'Flibble' . $$;
227 my $ircname = 'Flibble the Sailor Bot';
228 my $ircserver = 'irc.blahblahblah.irc';
229 my $port = 6667;
230 my $bindport = 6969;
231  
232 my @channels = ( '#Blah', '#Foo', '#Bar' );
233  
234 my $irc = POE::Component::IRC->spawn(
235 nick => $nickname,
236 server => $ircserver,
237 port => $port,
238 ircname => $ircname,
239 ) or die "Oh noooo! $!";
240  
241 POE::Session->create(
242 package_states => [
243 main => [ qw(_start irc_001 irc_console_service irc_console_connect
244 irc_console_authed irc_console_close irc_console_rw_fail) ],
245 ],
246 );
247  
248 $poe_kernel->run();
249  
250 sub _start {
251 $irc->plugin_add( 'Console' => POE::Component::IRC::Plugin::Console->new(
252 bindport => $bindport,
253 password => 'opensesame'
254 );
255 $irc->yield( register => 'all' );
256 $irc->yield( connect => { } );
257 return;
258 }
259  
260 sub irc_001 {
261 $irc->yield( join => $_ ) for @channels;
262 return;
263 }
264  
265 sub irc_console_service {
266 my $getsockname = $_[ARG0];
267 return;
268 }
269  
270 sub irc_console_connect {
271 my ($peeradr, $peerport, $wheel_id) = @_[ARG0 .. ARG2];
272 return;
273 }
274  
275 sub irc_console_authed {
276 my $wheel_id = $_[ARG0];
277 return;
278 }
279  
280 sub irc_console_close {
281 my $wheel_id = $_[ARG0];
282 return;
283 }
284  
285 sub irc_console_rw_fail {
286 my ($peeradr, $peerport) = @_[ARG0, ARG1];
287 return;
288 }
289  
290 =head1 DESCRIPTION
291  
292 POE::Component::IRC::Plugin::Console is a L<POE::Component::IRC|POE::Component::IRC>
293 plugin that provides an interactive console running over the loopback network.
294 One connects to the listening socket using a telnet client (or equivalent),
295 authenticate using the applicable password. Once authed one will receive all
296 events that are processed through the component. One may also issue all the
297 documented component commands.
298  
299 =head1 METHODS
300  
301 =head2 C<new>
302  
303 Takes two arguments:
304  
305 B<'password'>, the password to set for *all* console connections;
306  
307 B<'bindport'>, specify a particular port to bind to, defaults to 0, ie. randomly
308 allocated;
309  
310 Returns a plugin object suitable for feeding to
311 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
312  
313 =head2 C<getsockname>
314  
315 Gives access to the underlying listener's C<getsockname> method. See
316 L<POE::Wheel::SocketFactory|POE::Wheel::SocketFactory> for details.
317  
318 =head1 OUTPUT EVENTS
319  
320 The plugin generates the following additional
321 L<POE::Component::IRC|POE::Component::IRC> events:
322  
323 =head2 C<irc_console_service>
324  
325 Emitted when a listener is successfully spawned. C<ARG0> is the result of
326 C<getsockname>, see above for details.
327  
328 =head2 C<irc_console_connect>
329  
330 Emitted when a client connects to the console. C<ARG0> is the peeradr, C<ARG1>
331 is the peer port and C<ARG2> is the wheel id of the connection.
332  
333 =head2 C<irc_console_authed>
334  
335 Emitted when a client has successfully provided a valid password. C<ARG0> is
336 the wheel id of the connection.
337  
338 =head2 C<irc_console_close>
339  
340 Emitted when a client terminates a connection. C<ARG0> is the wheel id of the
341 connection.
342  
343 =head2 C<irc_console_rw_fail>
344  
345 Emitted when a L<POE::Wheel::ReadWrite|POE::Wheel::ReadWrite> could not be
346 created on a socket. C<ARG0> is the peer's address, C<ARG1> is the peer's port.
347  
348 =head1 AUTHOR
349  
350 Chris 'BinGOs' Williams
351  
352 =head1 SEE ALSO
353  
354 L<POE::Component::IRC|POE::Component::IRC>
355  
356 L<POE::Wheel::SocketFactory|POE::Wheel::SocketFactory>
357  
358 =cut
« Filter/IRC/Compat.pm ↑ Summary Component/IRC/State.pm »