Bot/EventHandler.pm

filter: Statementstmt 86.2% 44 / 51 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 50.0% 2 / 4 totaltotal 84.4% 65 / 77 CC 7 SCAR 8.4
CC
7
Cov
86%
handle
4.0
SENDMSG
3.0
new
2.0
CRAP 2.3
↑ Summary Bot.pm »
1 package Net::IPMessenger::Bot::EventHandler;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw/Net::IPMessenger::RecvEventHandler/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 $Term::ANSIColor::AUTORESET = 1;
11  
12 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
13 3 my ($class, %args) = @_;
14  
15 3 my $self = shift->SUPER::new();
16  
17 $self->{handler}
18 = ( ref $args{handler} eq 'ARRAY' )
19 ? $args{handler}
20 3 ▶ : [ qr// => $args{handler} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'handler'} eq 'ARRAY' ? : 1 2 3
21  
22 3 return $self;
23 }
24  
25 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my ( $self, $them, $user ) = @_;
27  
28 0 my $peeraddr = inet_ntoa( $them->socket->peeraddr );
29 0 my $peerport = $them->socket->peerport;
30 0 my $command = $them->messagecommand( $user->command );
31 0 my $modename = $command->modename;
32  
33 0 print CYAN "Received $modename from [$peeraddr:$peerport]";
34 0 print RESET "\n";
35 }
36  
37 5 ▶sub handle {
Subroutine handle: called CC=4 SCAR=13.9
Pod undocumented
38 5 my ( $self, $user ) = @_;
39  
40 5 ▶ return unless ( $self->{handler} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handler"} 0 5 5
41  
42 5 my $msg = Encode::decode( 'shiftjis', $user->get_message );
43 5 my $res;
44  
45 5 ▶ for ( my $i = 0; $i < @{ $self->{handler} }; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"handler"};}) 8 0 8
46 8 my $regex = $self->{handler}->[$i];
47 8 my $handler = $self->{handler}->[ $i + 1 ];
48  
49 8 ▶ if ( $msg =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /$regex/) 5 3 8
50 5 $res = $handler->($user);
51 5 last;
52 }
53 }
54  
55 5 return $res;
56 }
57  
58 5 ▶sub SENDMSG {
Subroutine SENDMSG: called CC=3 SCAR=11.0
Pod documented
59 5 my ($self, $ipmsg, $user ) = @_;
60  
61 5 $ipmsg->message([]); # clear cached-messages
62 5 $self->SUPER::SENDMSG($ipmsg, $user);
63  
64 5 my $command = $ipmsg->messagecommand( $user->command );
65 5 ▶ if ( $command->get_readcheck() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->get_readcheck) 1 4 5
66 1 $ipmsg->send(
67 {
68 command => $ipmsg->messagecommand('READMSG'),
69 option => $user->packet_num,
70 }
71 );
72 }
73  
74 5 ▶ if ( my $res = $self->handle($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->handle($user)) 4 1 5
75 4 $ipmsg->send(
76 {
77 command => $ipmsg->messagecommand('SENDMSG'),
78 option => Encode::encode( 'shiftjis', $res ),
79 }
80 );
81 }
82 }
83  
84 1 1;
↑ Summary Bot.pm »