Bot.pm

filter: Statementstmt 41.4% 51 / 123 Branchbran 18.1% 8 / 44 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 75.0% 12 / 16 Podpod 83.3% 5 / 6 totaltotal 43.2% 86 / 199 CC 26 SCAR 36.6
CC
26
Cov
38%
start_RTM
120.5
__ANON__
30.0
stop_RTM
12.0
CRAP 38.7
↑ Summary Bot/Client.pm »
1 package Slack::RTM::Bot;
2
3 8 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 8 ▶use POSIX qw/sys_wait_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Slack::RTM::Bot::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 8 our $VERSION = "1.15";
13
14 8 pipe(READH, WRITEH);
15 8 select(WRITEH);$|=1;
16 8 pipe(READH2, WRITEH2);
17 8 select(WRITEH2);$|=1;
18 8 select(STDOUT);
19
20 7 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
21 7 my $pkg = shift;
22 7 my $self = {
23 @_
24 };
25 7 ▶ die 'need token!' unless $self->{token};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"token"} 1 6 7
26 6 return bless $self, $pkg;
27 }
28
29 1 ▶sub start_RTM {
Subroutine start_RTM: called CC=11 SCAR=47.9
Pod documented
30 1 my $self = shift;
31 1 my ($sub) = @_;
32 1 $self->_connect($self->{options});
33
34 0 my $parent = $$;
35
36 0 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne 'MSWin32') { } 0 0 0
37 0 my @children = ();
38
39 0 my $pid = fork;
40 0 push @children, $pid;
41 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
42 0 while (1) {
43 0 ▶ unless (kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill 0, $pid) 0 0 0
44 0 kill 9, $pid;
45 0 waitpid($pid, WUNTRACED);
46 0 last;
47 }
48 0 print WRITEH "\n";
49 0 sleep 1;
50 }
51 } else {
52 0 my $pid = fork;
53 0 push @children, $pid;
54 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
55 0 $self->{client}->{pids} = [$parent, @children];
56 0 my $i = 0;
57 0 while (1) {
58 0 ▶ unless (kill 0, $parent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill 0, $parent) 0 0 0
59 0 kill 9, $pid;
60 0 waitpid($pid, WUNTRACED);
61 0 last;
62 }
63 0 ▶ if ($self->{client}->read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}->read) 0 0 0
64 0 print WRITEH2 "\n";
65 }
66 0 (my $buffer = <READH>) =~ s/\n.*$//;
67 0 ▶ if ($buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) 0 0 0
68 $self->{client}->write(
69 0 %{JSON::from_json(Encode::decode_utf8($buffer))}
70 );
71 }
72 0 ▶ if (++$i % 30 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i % 30 == 0) 0 0 0
73 $self->{client}->write(
74 0 id => $i,
75 type => 'ping'
76 );
77 }
78 }
79 } else {
80 0 $self->{children} = \@children;
81 # wait until connected
82 0 <READH2>;
83 0 ▶ &$sub($self) if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 0 0
84 }
85 };
86 } else {
87 0 require threads;
88 0 require Thread::Queue;
89
90 threads->create(
91 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
92 0 ▶ while (kill 0, $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $parent) 0 0 0
93 0 print WRITEH "\n";
94 0 sleep 1;
95 }
96 }
97 0 )->detach;
98
99 threads->create(
100 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
101 0 my $i = 0;
102 0 ▶ while (kill 0, $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $parent) 0 0 0
103 0 ▶ if ($self->{client}->read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}->read) 0 0 0
104 0 print WRITEH2 "\n";
105 }
106 0 (my $buffer = <READH>) =~ s/\n.*$//;
107 0 ▶ if ($buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) 0 0 0
108 $self->{client}->write(
109 0 %{JSON::from_json(Encode::decode_utf8($buffer))}
110 );
111 }
112 0 ▶ if (++$i % 30 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i % 30 == 0) 0 0 0
113 $self->{client}->write(
114 0 id => $i,
115 type => 'ping'
116 );
117 }
118 }
119 }
120 0 )->detach;
121
122 0 $self->{queue} = Thread::Queue->new();
123 0 ▶ $self->{worker} = threads->create(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
124 0 ▶ while (defined(my $req = $self->{queue}->dequeue())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $req = $self->{"queue"}->dequeue)) 0 0 0
125 0 print WRITEH $req;
126 }
127 0 });
128
129 # wait until connected
130 0 <READH2>;
131 0 ▶ &$sub($self) if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 0 0
132 }
133 }
134
135 0 ▶sub stop_RTM {
Subroutine stop_RTM: not called CC=3 SCAR=24.8
Pod documented
136 0 my $self = shift;
137
138 0 sleep 1;
139 0 $self->{client}->disconnect;
140 0 undef $self->{client};
141
142 0 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne 'MSWin32') { } 0 0 0
143 0 for my $child (@{$self->{children}}) {
144 0 kill 9, $child;
145 0 waitpid($child, WUNTRACED);
146 }
147 0 undef $self->{children};
148 } else {
149 0 $self->{queue}->end();
150 0 $self->{worker}->join();
151 }
152 }
153
154 1 ▶sub _connect {
Subroutine _connect: called CC=1 SCAR=0.2
155 1 my $self = shift;
156
157 my $client = Slack::RTM::Bot::Client->new(
158 token => $self->{token},
159 actions => $self->{actions},
160 options => $self->{options}
161 1 );
162 1 $client->connect($self->{token});
163
164 0 $self->{client} = $client;
165 }
166
167 6 ▶sub say {
Subroutine say: called CC=6 SCAR=18.1
Pod documented
168 6 my $self = shift;
169 6 my $args;
170 6 ▶ if(!@_ || scalar @_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or scalar @_ % 2 != 0) 2 4 6
Condition100%3/3
not @_ or scalar @_ % 2 != 0
l!l&&r!l&&!r
114
MC/DC100%2/2
not @_ or scalar @_ % 2 != 0
not @_ scalar @_ % 2 != 0
Truth table100%3/3
not(A) or B
A= not @_B= scalar @_ % 2 != 0
ABresult
1X1
011
000
171 2 die "argument is not a HASH or ARRAY."
172 }
173 4 $args = {@_};
174 4 ▶ if(!defined $args->{text} || !defined $args->{channel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"text"} and defined $args->{"channel"}) 2 2 4
Condition100%3/3
defined $args->{"text"} and defined $args->{"channel"}
!ll&&!rl&&r
112
MC/DC100%2/2
defined $args->{"text"} and defined $args->{"channel"}
defined $args->{"text"} defined $args->{"channel"}
Truth table100%3/3
A and B
A= defined $args->{"text"}B= defined $args->{"channel"}
ABresult
0X0
100
111
175 2 die "argument needs keys 'text' and 'channel'.";
176 }
177
178 2 ▶ die "RTM not started." unless $self->{client};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"client"} 2 0 2
179
180 my $request = JSON::to_json({
181 type => 'message',
182 subtype => 'bot_message',
183 bot_id => $self->{client}->{info}->{self}->{id},
184 %$args,
185 channel => $self->{client}->find_conversation_id($args->{channel})
186 0 })."\n";
187 0 print WRITEH $request;
188 }
189
190 6 ▶sub on {
Subroutine on: called CC=2 SCAR=7.0
Pod documented
191 6 my $self = shift;
192 6 ▶ die "RTM already started." if $self->{info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"} 0 6 6
193 6 my ($events, $routine) = @_;
194 6 push @{$self->{actions}}, {
195 events => $events,
196 routine => $routine
197 };
198 }
199
200 6 ▶sub add_action {
Subroutine add_action: called CC=1 SCAR=0.0
Pod undocumented
201 6 my $self = shift;
202 6 $self->on(@_);
203 }
204
205 8 1;
206 __END__
↑ Summary Bot/Client.pm »