blib/lib/UnazuSan.pm

filter: Statementstmt 31.5% 29 / 92 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 42.8% 9 / 21 Podpod 0.0% 0 / 6 totaltotal 24.0% 38 / 158 CC 18 SCAR 28.7
CC
18
Cov
23%
new
56.0
_respond
30.0
__ANON__
12.0
CRAP 17.6
↑ Summary
1 package UnazuSan;
2 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.03";
7  
8 2 ▶use AnySan;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnySan::Provider::IRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode qw/decode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
13 0 my $class = shift;
14 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
15 0 my $self = bless \%args, $class;
16  
17 0 ▶ $self->{nickname} //= 'unazu_san';
Condition0%0/2
$self->{"nickname"} //= "unazu_san"
l!l
00
MC/DC0%0/1
$self->{"nickname"} //= "unazu_san"
$self->{"nickname"}
Truth table0%0/2
A
A= $self->{"nickname"}
Aresult
11
00
18 0 ▶ $self->{port} ||= 6667;
Condition0%0/2
$self->{"port"} ||= 6667
l!l
00
MC/DC0%0/1
$self->{"port"} ||= 6667
$self->{"port"}
Truth table0%0/2
A
A= $self->{"port"}
Aresult
11
00
19 0 ▶ $self->{post_interval} //= 2;
Condition0%0/2
$self->{"post_interval"} //= 2
l!l
00
MC/DC0%0/1
$self->{"post_interval"} //= 2
$self->{"post_interval"}
Truth table0%0/2
A
A= $self->{"post_interval"}
Aresult
11
00
20 0 ▶ $self->{reconnect_interval} //= 3;
Condition0%0/2
$self->{"reconnect_interval"} //= 3
l!l
00
MC/DC0%0/1
$self->{"reconnect_interval"} //= 3
$self->{"reconnect_interval"}
Truth table0%0/2
A
A= $self->{"reconnect_interval"}
Aresult
11
00
21 0 ▶ $self->{receive_commands} //= ['PRIVMSG'];
Condition0%0/2
$self->{"receive_commands"} //= ["PRIVMSG"]
l!l
00
MC/DC0%0/1
$self->{"receive_commands"} //= ["PRIVMSG"]
$self->{"receive_commands"}
Truth table0%0/2
A
A= $self->{"receive_commands"}
Aresult
11
00
22  
23 0 my ($irc, $is_connect, $connector);
24 0 ▶ $connector = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
25 irc
26 $self->{host},
27 port => $self->{port},
28 key => $self->{keyword},
29 password => $self->{password},
30 nickname => $self->{nickname},
31 user => $self->{user},
32 interval => $self->{post_interval},
33 enable_ssl => $self->{enable_ssl},
34 recive_commands => $self->{receive_commands},
35 0 ▶ on_connect => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
36 0 my ($con, $err) = @_;
37 0 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 0 0 0
38 0 warn "connect error: $err\n";
39 0 ▶ exit 1 unless $self->{reconnect_interval};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"reconnect_interval"} 0 0 0
40 0 sleep $self->{reconnect_interval};
41 0 $con->disconnect('try reconnect');
42 } else {
43 0 warn 'connect';
44 0 $is_connect = 1;
45 }
46 },
47 0 ▶ on_disconnect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 warn 'disconnect';
49 # XXX: bad hack...
50 0 undef $irc->{client};
51 0 undef $irc->{SEND_TIMER};
52 0 undef $irc;
53 0 $is_connect = 0;
54 0 $irc = $connector->();
55 },
56 channels => {
57 0 ▶ map { my $chan = $_; $chan = '#'.$chan unless $chan =~ /^#/; ;($chan => +{}) } @{ $self->{join_channels} || [] },
Branch: 1 branch
BranchTrueFalseTotal
unless $chan =~ /^#/ 0 0 0
Condition0%0/2
$self->{'join_channels'} || []
l!l
00
MC/DC0%0/1
$self->{'join_channels'} || []
$self->{'join_channels'}
Truth table0%0/2
A
A= $self->{'join_channels'}
Aresult
11
00
58 };
59 0 };
60 0 $irc = $connector->();
61 0 $self->{irc} = $irc;
62  
63 AnySan->register_listener(
64 echo => {
65 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my $receive = shift;
67 0 $receive->{message} = decode_utf8 $receive->{message};
68 0 $self->_respond($receive);
69 }
70 }
71 0 );
72  
73 0 $self;
74 }
75  
76 0 ▶sub on_message {
Subroutine on_message: not called CC=2 SCAR=17.9
Pod undocumented
77 0 my ($self, @jobs) = @_;
78 0 ▶ while (my ($reg, $sub) = splice @jobs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@jobs, 0, 2)) 0 0 0
79 0 push @{ $self->_reactions }, [$reg, $sub];
80 }
81 }
82  
83 0 ▶sub on_command {
Subroutine on_command: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my ($self, @jobs) = @_;
85 0 ▶ while (my ($command, $sub) = splice @jobs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@jobs, 0, 2)) 0 0 0
86 0 my $reg = _build_command_reg($self->{nickname}, $command);
87 0 push @{ $self->_reactions }, [$reg, $sub, $command];
88 }
89 }
90  
91 1 ▶sub _build_command_reg {
Subroutine _build_command_reg: called CC=1 SCAR=0.0
92 1 my ($nick, $command) = @_;
93  
94 1 my $prefix = '^\s*'.quotemeta($nick). '_*[:\s]\s*' . quotemeta($command);
95 }
96  
97 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
98 0 AnySan->run;
99 }
100  
101 0 ▶sub respond_all { shift->{respond_all} }
Subroutine respond_all: not called CC=1 SCAR=6.9
Pod undocumented
102  
103 0 ▶sub _reactions {
Subroutine _reactions: not called CC=2 SCAR=17.9
104 0 ▶ shift->{_reactions} ||= [];
Condition0%0/2
+(shift())->{"_reactions"} ||= []
l!l
00
MC/DC0%0/1
+(shift())->{"_reactions"} ||= []
+(shift())->{"_reactions"}
Truth table0%0/2
A
A= +(shift())->{"_reactions"}
Aresult
11
00
105 }
106  
107 0 ▶sub _respond {
Subroutine _respond: not called CC=5 SCAR=34.0
108 0 my ($self, $receive) = @_;
109  
110 0 my $message = $receive->message;
111 0 $message =~ s/^\s+//; $message =~ s/\s+$//;
112 0 for my $reaction (@{ $self->_reactions }) {
113 0 my ($reg, $sub, $command) = @$reaction;
114  
115 0 ▶ if (my @matches = $message =~ $reg) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matches) = $message =~ /$reg/) 0 0 0
116 0 ▶ if (defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $command) 0 0 0
117 0 @matches = _build_command_args($reg, $message);
118 }
119 0 $sub->($receive, @matches);
120 0 ▶ return unless $self->respond_all;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->respond_all 0 0 0
121 }
122 }
123 }
124  
125 2 ▶sub _build_command_args {
Subroutine _build_command_args: called CC=1 SCAR=0.0
126 2 my ($reg, $mes) = @_;
127 2 $mes =~ s/$reg//;
128 2 $mes =~ s/^\s+//; $mes =~ s/\s+$//;
129 2 split /\s+/, $mes;
130 }
131  
132 package # hide from pause
133 AnySan::Receive;
134  
135 2 ▶use Encode qw/encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137 0 ▶sub reply {
Subroutine reply: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self, $msg) = @_;
139 0 $self->send_reply(encode_utf8 $msg);
140 }
141  
142 2 1;
143 __END__
↑ Summary