Component/IRC/Plugin/Console.pm

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