blib/lib/IkuSan.pm

filter: Statementstmt 22.0% 41 / 186 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 29.5% 13 / 44 Podpod 0.0% 0 / 10 totaltotal 16.4% 54 / 329 CC 35 SCAR 36.4
CC
35
Cov
16%
new
182.0
_respond
42.0
reply
20.0
CRAP 38.0
↑ Summary
1 package IkuSan;
2  
3 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = "0.01";
9  
10 1 ▶use AnySan;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use AnySan::Provider::IRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Twiggy::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Getopt::Long qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 GetOptionsFromString
17 :config posix_default no_ignore_case bundling
18 1 );
19 1 ▶use AnyEvent::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
Pod undocumented
23 0 my $class = shift;
24 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
25 0 my $self = bless \%args, $class;
26  
27 0 ▶ $self->{nickname} //= 'ikusan';
Condition0%0/2
$self->{"nickname"} //= "ikusan"
l!l
00
MC/DC0%0/1
$self->{"nickname"} //= "ikusan"
$self->{"nickname"}
Truth table0%0/2
A
A= $self->{"nickname"}
Aresult
11
00
28 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
29 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
30 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
31 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
32 0 $self->{http_host} = '127.0.0.1'; # 固定
33 0 ▶ $self->{http_port} //= 19300;
Condition0%0/2
$self->{"http_port"} //= 19300
l!l
00
MC/DC0%0/1
$self->{"http_port"} //= 19300
$self->{"http_port"}
Truth table0%0/2
A
A= $self->{"http_port"}
Aresult
11
00
34 0 ▶ $self->{max_workers} //= 10;
Condition0%0/2
$self->{"max_workers"} //= 10
l!l
00
MC/DC0%0/1
$self->{"max_workers"} //= 10
$self->{"max_workers"}
Truth table0%0/2
A
A= $self->{"max_workers"}
Aresult
11
00
35 0 $self->{pid} = $$; # 固定
36 0 ▶ $self->{on_option_error} //= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 my ($e, $receive) = @_;
38 0 warn "on_option_error: $e";
39 0 ▶ };
Condition0%0/3
$self->{"on_option_error"} //= sub { my($e, $receive) = @_; warn "on_option_error: $e"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"on_option_error"} //= sub { my($e, $receive) = @_; warn "on_option_error: $e"; }
$self->{"on_option_error"} sub { my($e, $receive) = @_; warn "on_option_error: $e"; }
Truth table0%0/3
A //= B
A= $self->{"on_option_error"}B= sub { my($e, $receive) = @_; warn "on_option_error: $e"; }
ABresult
1X1
011
000
40 0 ▶ $self->{on_start} //= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 my ($pm, $receive, $sub, $message, @matches) = @_;
42 0 warn "on_start: $message";
43 0 ▶ };
Condition0%0/3
$self->{"on_start"} //= sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_start: $message"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"on_start"} //= sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_start: $message"; }
$self->{"on_start"} sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_start: $message"; }
Truth table0%0/3
A //= B
A= $self->{"on_start"}B= sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_start: $message"; }
ABresult
1X1
011
000
44 0 ▶ $self->{on_error} //= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 my ($e, $pm, $receive, $sub, $message, @matches) = @_;
46 0 warn "on_error: $e";
47 0 ▶ };
Condition0%0/3
$self->{"on_error"} //= sub { my($e, $pm, $receive, $sub, $message, @matches) = @_; warn "on_error: $e"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"on_error"} //= sub { my($e, $pm, $receive, $sub, $message, @matches) = @_; warn "on_error: $e"; }
$self->{"on_error"} sub { my($e, $pm, $receive, $sub, $message, @matches) = @_; warn "on_error: $e"; }
Truth table0%0/3
A //= B
A= $self->{"on_error"}B= sub { my($e, $pm, $receive, $sub, $message, @matches) = @_; warn "on_error: $e"; }
ABresult
1X1
011
000
48 0 ▶ $self->{on_finish} //= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my ($pm, $receive, $sub, $message, @matches) = @_;
50 0 warn "on_finish: $message";
51 0 ▶ };
Condition0%0/3
$self->{"on_finish"} //= sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_finish: $message"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"on_finish"} //= sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_finish: $message"; }
$self->{"on_finish"} sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_finish: $message"; }
Truth table0%0/3
A //= B
A= $self->{"on_finish"}B= sub { my($pm, $receive, $sub, $message, @matches) = @_; warn "on_finish: $message"; }
ABresult
1X1
011
000
52  
53 0 my ($irc, $is_connect, $connector);
54 0 ▶ $connector = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
55 irc
56 $self->{host},
57 port => $self->{port},
58 key => $self->{keyword},
59 password => $self->{password},
60 nickname => $self->{nickname},
61 user => $self->{user},
62 interval => $self->{post_interval},
63 enable_ssl => $self->{enable_ssl},
64 recive_commands => $self->{receive_commands},
65 0 ▶ on_connect => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
66 0 my ($con, $err) = @_;
67 0 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 0 0 0
68 0 warn "connect error: $err\n";
69 0 ▶ exit 1 unless $self->{reconnect_interval};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"reconnect_interval"} 0 0 0
70 0 sleep $self->{reconnect_interval};
71 0 $con->disconnect('try reconnect');
72 } else {
73 0 warn 'connect';
74 0 $is_connect = 1;
75 }
76 },
77 0 ▶ on_disconnect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 warn 'disconnect';
79 # XXX: bad hack...
80 0 undef $irc->{client};
81 0 undef $irc->{SEND_TIMER};
82 0 undef $irc;
83 0 $is_connect = 0;
84 0 $irc = $connector->();
85 },
86 channels => {
87 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
88 };
89 0 };
90 0 $irc = $connector->();
91  
92 0 ▶ my $app = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
93 0 my $req = Plack::Request->new(shift);
94 0 ▶ if ($req->address eq $self->{http_host} && $req->method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->address eq $self->{"http_host"} and $req->method eq "POST") 0 0 0
Condition0%0/3
$req->address eq $self->{"http_host"} and $req->method eq "POST"
!ll&&!rl&&r
000
MC/DC0%0/2
$req->address eq $self->{"http_host"} and $req->method eq "POST"
$req->address eq $self->{"http_host"} $req->method eq "POST"
Truth table0%0/3
A and B
A= $req->address eq $self->{"http_host"}B= $req->method eq "POST"
ABresult
0X0
100
111
95 0 my $message = $req->param('message');
96 0 my $channel = $req->param('channel');
97 0 my $privmsg = $req->param('privmsg');
98 0 my @message = split(/\n/, $message);
99 0 $irc->send_message( $message[0], channel => $channel, privmsg => $privmsg );
100 0 return [ 200, ["Content-Type" => "text/plain"], ["message sent channel: $channel $message"] ]
101 }
102 0 [ 404, ["Content-Type" => "text/plain"], ["not found"] ]
103 0 };
104  
105 0 warn sprintf("starting httpd: http://%s:%s", $self->{http_host}, $self->{http_port});
106 my $twiggy = Twiggy::Server->new(
107 host => $self->{http_host},
108 port => $self->{http_port},
109 0 );
110  
111 0 $twiggy->register_service($app);
112  
113 my $pm = AnyEvent::ForkManager->new(
114 max_workers => $self->{max_workers},
115 0 );
116  
117 AnySan->register_listener(
118 $self->{nickname} => {
119 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
120 0 my $receive = shift;
121 0 $receive->{message} = decode_utf8 $receive->{message};
122 0 $receive->{http_host} = $self->{http_host};
123 0 $receive->{http_port} = $self->{http_port};
124 0 $receive->{pid} = $self->{pid};
125 0 my $respond = [];
126 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 $respond = $self->_respond($receive);
128 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 $self->{on_option_error}->($_, $receive);
130 0 };
131 0 for my $r (@$respond) {
132 0 my ($sub, $message, @matches) = @$r;
133 $pm->start(
134 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 my ($pm, $receive, $sub, $message, @matches) = @_;
136 0 $self->{on_start}->($pm, $receive, $sub, $message, @matches);
137 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 $sub->($pm, $receive, $sub, $message, @matches);
139 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 $self->{on_error}->($_, $pm, $receive, $sub, $message, @matches);
141 0 ▶ } finally {
Subroutine __ANON__: not called CC=2 SCAR=17.9
142 0 ▶ $self->{on_finish}->($pm, $receive, $sub, $message, @matches) unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
143 0 };
144 },
145 0 args => [$receive, $sub, $message, @matches],
146 );
147 }
148 }
149 }
150 0 );
151  
152 0 $self;
153 }
154  
155 0 ▶sub on_message {
Subroutine on_message: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my ($self, @jobs) = @_;
157 0 ▶ while (my ($reg, $sub) = splice @jobs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@jobs, 0, 2)) 0 0 0
158 0 push @{ $self->_reactions }, [$reg, $sub];
159 }
160 }
161  
162 0 ▶sub on_command {
Subroutine on_command: not called CC=2 SCAR=17.9
Pod undocumented
163 0 my ($self, @jobs) = @_;
164 0 ▶ while (my ($command, $sub) = splice @jobs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@jobs, 0, 2)) 0 0 0
165 0 my $reg = _build_command_reg($self->{nickname}, $command);
166 0 push @{ $self->_reactions }, [$reg, $sub, $command];
167 }
168 }
169  
170 0 ▶sub on_option {
Subroutine on_option: not called CC=3 SCAR=24.8
Pod undocumented
171 0 my ($self, @jobs) = @_;
172 0 ▶ while (my ($command, $option, $sub) = splice @jobs, 0, 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@jobs, 0, 3)) 0 0 0
173 0 ▶ die "on_option is require 3 arguments." unless $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
174 0 my $reg = _build_command_reg($self->{nickname}, $command);
175 0 push @{ $self->_reactions }, [$reg, $sub, $command, $option];
176 }
177 }
178  
179 0 ▶sub _build_command_reg {
Subroutine _build_command_reg: not called CC=1 SCAR=6.9
180 0 my ($nick, $command) = @_;
181  
182 0 my $prefix = '^\s*'.quotemeta($nick). '_*[:\s]\s*' . quotemeta($command);
183 }
184  
185 0 ▶sub fever { AnySan->run }
Subroutine fever: not called CC=1 SCAR=6.9
Pod undocumented
186  
187 0 ▶sub respond_all { shift->{respond_all} }
Subroutine respond_all: not called CC=1 SCAR=6.9
Pod undocumented
188  
189 0 ▶sub _reactions {
Subroutine _reactions: not called CC=2 SCAR=17.9
190 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
191 }
192  
193 0 ▶sub _respond {
Subroutine _respond: not called CC=6 SCAR=37.4
194 0 my ($self, $receive) = @_;
195  
196 0 my @result = ();
197 0 my $message = $receive->message;
198 0 $message =~ s/^\s+//; $message =~ s/\s+$//;
199 0 for my $reaction (@{ $self->_reactions }) {
200 0 my ($reg, $sub, $command, $option) = @$reaction;
201 0 ▶ if (my @matches = $message =~ $reg) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matches) = $message =~ /$reg/) 0 0 0
202 0 ▶ if (defined $option) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $option) { } 0 0 0
elsif (defined $command) { } 0 0 0
203 0 @matches = _build_option_args($reg, $message, $option);
204 }
205 elsif (defined $command) {
206 0 @matches = _build_command_args($reg, $message);
207 }
208 0 push @result, [$sub, $message, @matches];
209 0 ▶ return \@result unless $self->respond_all;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->respond_all 0 0 0
210 }
211 }
212  
213 0 return \@result;
214 }
215  
216 0 ▶sub _build_command_args {
Subroutine _build_command_args: not called CC=1 SCAR=6.9
217 0 my ($reg, $mes) = @_;
218 0 $mes =~ s/$reg//;
219 0 $mes =~ s/^\s+//; $mes =~ s/\s+$//;
220 0 split /\s+/, $mes;
221 }
222  
223 0 ▶sub _build_option_args {
Subroutine _build_option_args: not called CC=2 SCAR=17.9
224 0 my ($reg, $mes, $opt) = @_;
225 0 $mes =~ s/$reg//;
226 0 $mes =~ s/^\s+//; $mes =~ s/\s+$//;
227 0 my $warn = "";
228 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 $warn = $_[0]; chomp($warn)
230 0 };
231 0 GetOptionsFromString($mes, \my %opts, @$opt);
232 0 ▶ die $warn if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
233 0 return %opts;
234 }
235  
236 1 1;
237  
238 package # hide from pause
239 AnySan::Receive;
240  
241 1 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 1 ▶use Encode qw/encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 0 ▶sub furl {
Subroutine furl: not called CC=1 SCAR=6.9
Pod undocumented
245 0 my $self = shift;
246 0 $self->{_furl} = Furl->new();
247 0 $self->{_furl};
248 }
249  
250 0 ▶sub notice {
Subroutine notice: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my ($self, $msg) = @_;
252 0 $self->reply($msg)
253 }
254  
255 0 ▶sub privmsg {
Subroutine privmsg: not called CC=1 SCAR=6.9
Pod undocumented
256 0 my ($self, $msg) = @_;
257 0 $self->reply($msg, privmsg => 1)
258 }
259  
260 0 ▶sub reply {
Subroutine reply: not called CC=4 SCAR=30.0
Pod undocumented
261 0 my ($self, $msg, %args) = @_;
262 0 my @msg = split(/\n/, $msg);
263 0 ▶ if ($self->{pid} == $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pid'} == $$) { } 0 0 0
264 0 ▶ $self->attribute('send_command', 'PRIVMSG') if ($args{privmsg});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"privmsg"} 0 0 0
265 0 $self->send_reply($msg[0]);
266 } else {
267 $self->furl->post(
268 sprintf("http://%s:%s", $self->{http_host}, $self->{http_port}), [], [
269 message => encode_utf8 $msg,
270 channel => $self->attribute("channel"),
271 0 ▶ privmsg => $args{privmsg} || 0,
Condition0%0/2
$args{'privmsg'} || 0
l!l
00
MC/DC0%0/1
$args{'privmsg'} || 0
$args{'privmsg'}
Truth table0%0/2
A
A= $args{'privmsg'}
Aresult
11
00
272 ],
273 );
274 }
275 }
276  
277 1 1;
278  
279 __END__
↑ Summary