Bot/Client.pm

filter: Statementstmt 46.5% 88 / 189 Branchbran 29.6% 16 / 54 Conditioncond 43.3% 13 / 30 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 53.1% 17 / 32 Podpod 0.0% 0 / 8 totaltotal 41.7% 139 / 333 CC 42 SCAR 28.0
CC
42
Cov
43%
_refetch_conversations
72.0
_refetch_users
30.0
_listen
18.0
CRAP 16.4
« Bot.pm ↑ Summary Bot/Information.pm »
1 package Slack::RTM::Bot::Client;
2
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use HTTP::Request::Common qw(POST GET);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 8 ▶use Protocol::WebSocket::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use IO::Socket::SSL qw/SSL_VERIFY_NONE/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 8 ▶use Slack::RTM::Bot::Information;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Slack::RTM::Bot::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 8 my $ua = LWP::UserAgent->new(
20 ssl_opts => {
21 verify_hostname => 0,
22 SSL_verify_mode => SSL_VERIFY_NONE
23 }
24 );
25 8 $ua->agent('Slack::RTM::Bot');
26
27 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
28 3 my $pkg = shift;
29 3 my $self = {
30 @_
31 };
32 3 ▶ die "token is required." unless $self->{token};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"token"} 0 3 3
33 3 return bless $self, $pkg;
34 }
35
36 1 ▶sub connect {
Subroutine connect: called CC=3 SCAR=12.5
Pod undocumented
37 1 my $self = shift;
38 1 my ($token) = @_;
39
40 1 my $res = $ua->request(POST 'https://slack.com/api/rtm.connect', [ token => $token ]);
41 1 my $content;
42 1 eval {
43 1 $content = JSON::from_json($res->content);
44 };
45 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
46 0 die 'connect response fail:'.Dumper $res->content;
47 }
48 1 ▶ die 'connect response fail: '.$res->content unless ($content->{ok});
Branch: 1 branch
BranchTrueFalseTotal
unless $content->{"ok"} 1 0 1
49
50 0 $self->{info} = Slack::RTM::Bot::Information->new(%{$content});
51 0 $self->_connect;
52 }
53
54 0 ▶sub _connect {
Subroutine _connect: not called CC=3 SCAR=24.8
55 0 my $self = shift;
56 0 my ($host) = $self->{info}->{url} =~ m{wss://(.+)/websocket};
57 0 my $socket = IO::Socket::SSL->new(
58 SSL_verify_mode => SSL_VERIFY_NONE,
59 PeerHost => $host,
60 PeerPort => 443
61 );
62 0 $socket->blocking(0);
63 0 $socket->connect;
64
65 0 my $ws_client = Protocol::WebSocket::Client->new(url => $self->{info}->{url});
66 0 ▶ $ws_client->{hs}->req->{max_message_size} = $self->{options}->{max_message_size} if $self->{options}->{max_message_size};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"max_message_size"} 0 0 0
67 0 ▶ $ws_client->{hs}->res->{max_message_size} = $self->{options}->{max_message_size} if $self->{options}->{max_message_size};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"max_message_size"} 0 0 0
68 0 ▶ $ws_client->on(read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my ($cli, $buffer) = @_;
70 0 $self->_listen($buffer);
71 0 });
72 0 ▶ $ws_client->on(write => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my ($cli, $buffer) = @_;
74 0 syswrite $socket, $buffer;
75 0 });
76 0 ▶ $ws_client->on(connect => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
77 0 ▶ print "RTM (re)connected.\n" if ($self->{options}->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"debug"} 0 0 0
78 0 });
79 0 ▶ $ws_client->on(error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 my ($cli, $error) = @_;
81 0 print STDERR 'error: '. $error;
82 0 });
83 0 $ws_client->connect;
84
85 0 $self->{ws_client} = $ws_client;
86 0 $self->{socket} = $socket;
87 }
88
89 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod undocumented
90 0 my $self = shift;
91 0 $self->{ws_client}->disconnect;
92 0 undef $self;
93 }
94
95 0 ▶sub read {
Subroutine read: not called CC=3 SCAR=24.8
Pod undocumented
96 0 my $self = shift;
97 0 my $data = '';
98 0 ▶ while (my $line = readline $self->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $self->{"socket"})) 0 0 0
99 0 $data .= $line;
100 }
101 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
102 0 $self->{ws_client}->read($data);
103 0 return $data =~ /.*hello.*/;
104 }
105 }
106
107 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my $self = shift;
109 0 $self->{ws_client}->write(JSON::encode_json({@_}));
110 }
111
112 0 ▶sub find_conversation_id {
Subroutine find_conversation_id: not called CC=3 SCAR=24.8
Pod undocumented
113 0 my $self = shift;
114 0 my ($name) = @_;
115 0 my $id = $self->{info}->_find_conversation_id($name);
116 0 ▶ $id ||= $self->_refetch_conversation_id($name) or die "There are no conversations of such name: $name";
Branch: 1 branch
BranchTrueFalseTotal
$id ||= $self->_refetch_conversation_id($name) or die "There are no conversations of such name: $name" 0 0 0
Condition0%0/3
$id ||= $self->_refetch_conversation_id($name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$id ||= $self->_refetch_conversation_id($name)
$id $self->_refetch_conversation_id($name)
Truth table0%0/3
A ||= B
A= $idB= $self->_refetch_conversation_id($name)
ABresult
1X1
011
000
117 0 return $id;
118 }
119
120 0 ▶sub _refetch_conversation_id {
Subroutine _refetch_conversation_id: not called CC=1 SCAR=6.9
121 0 my $self = shift;
122 0 my ($name) = @_;
123 0 $self->_refetch_conversations;
124 0 return $self->{info}->_find_conversation_id($name);
125 }
126
127 1 ▶sub find_conversation_name {
Subroutine find_conversation_name: called CC=4 SCAR=15.5
Pod undocumented
128 1 my $self = shift;
129 1 my ($id) = @_;
130 1 my $name = $self->{info}->_find_conversation_name($id);
131 1 ▶ $name ||= $self->_refetch_conversation_name($id) or warn "There are no conversations of such id: $id";
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $self->_refetch_conversation_name($id) or warn "There are no conversations of such id: $id" 1 0 1
Condition33%1/3
$name ||= $self->_refetch_conversation_name($id)
l!l&&r!l&&!r
100
MC/DC0%0/2
$name ||= $self->_refetch_conversation_name($id)
$name $self->_refetch_conversation_name($id)
Truth table33%1/3
A ||= B
A= $nameB= $self->_refetch_conversation_name($id)
ABresult
1X1
011
000
132 1 ▶ $name ||= $id;
Condition33%1/3
$name ||= $id
l!l&&r!l&&!r
100
MC/DC0%0/2
$name ||= $id
$name $id
Truth table33%1/3
A ||= B
A= $nameB= $id
ABresult
1X1
011
000
133 1 return $name;
134 }
135
136 0 ▶sub _refetch_conversation_name {
Subroutine _refetch_conversation_name: not called CC=1 SCAR=6.9
137 0 my $self = shift;
138 0 my ($id) = @_;
139 0 $self->_refetch_conversations;
140 0 return $self->{info}->_find_conversation_name($id);
141 }
142
143 0 ▶sub _refetch_conversations {
Subroutine _refetch_conversations: not called CC=8 SCAR=42.8
144 0 my $self = shift;
145 0 my $cursor = "";
146 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cursor eq "" or do { do { my $res = $ua->request(POST("https://slack.com/api/conversations.list", ["token", $self->{"token"}, "types", "public_channel,private_channel,im", "cursor", $cursor])); my $content; eval { do { $content = &JSON::decode_json(scalar $res->content) } }; if ($@) { die 'connect response fail:' . Dumper($res->content); } ; die 'connect response fail: ' . $res->content unless $content->{"ok"}; foreach my $channel (@{$$content{"channels"};}) { if ($channel->{"is_im"}) { my $user_id = $channel->{"user"}; my $name = $self->{"info"}->_find_user_name($user_id); $name ||= $self->_refetch_user_name($user_id) or warn "There are no users of such id: $user_id"; $self->{"info"}{"channels"}{$channel->{"id"}} = {%$channel, "name", '@' . $name}; next; } ; $self->{"info"}{"channels"}{$channel->{"id"}} = $channel; } ; $cursor = $content->{"response_metadata"}{"next_cursor"} } } 0 0 0
147 0 my $res = $ua->request(POST 'https://slack.com/api/conversations.list', [ token => $self->{token}, types => "public_channel,private_channel,im", cursor => $cursor ]);
148 0 my $content;
149 0 eval {
150 0 $content = JSON::decode_json($res->content);
151 };
152 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
153 0 die 'connect response fail:' . Dumper $res->content;
154 }
155 0 ▶ die 'connect response fail: ' . $res->content unless ($content->{ok});
Branch: 1 branch
BranchTrueFalseTotal
unless $content->{"ok"} 0 0 0
156
157 0 for my $channel (@{$content->{channels}}) {
158 0 ▶ if ($channel->{is_im}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($channel->{"is_im"}) 0 0 0
159 0 my $user_id = $channel->{user};
160 0 my $name = $self->{info}->_find_user_name($user_id);
161 0 ▶ $name ||= $self->_refetch_user_name($user_id) or warn "There are no users of such id: $user_id";
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $self->_refetch_user_name($user_id) or warn "There are no users of such id: $user_id" 0 0 0
Condition0%0/3
$name ||= $self->_refetch_user_name($user_id)
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= $self->_refetch_user_name($user_id)
$name $self->_refetch_user_name($user_id)
Truth table0%0/3
A ||= B
A= $nameB= $self->_refetch_user_name($user_id)
ABresult
1X1
011
000
162 0 $self->{info}->{channels}->{$channel->{id}} = { %$channel, name => '@'.$name };
163 0 next;
164 }
165 0 $self->{info}->{channels}->{$channel->{id}} = $channel;
166 }
167
168 0 $cursor = $content->{response_metadata}->{next_cursor};
169 } until ($cursor eq "");
170 }
171
172 1 ▶sub find_user_name {
Subroutine find_user_name: called CC=4 SCAR=15.5
Pod undocumented
173 1 my $self = shift;
174 1 my ($id) = @_;
175 1 my $name = $self->{info}->_find_user_name($id);
176 1 ▶ $name ||= $self->_refetch_user_name($id) or warn "There are no users of such id: $id";
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $self->_refetch_user_name($id) or warn "There are no users of such id: $id" 1 0 1
Condition33%1/3
$name ||= $self->_refetch_user_name($id)
l!l&&r!l&&!r
100
MC/DC0%0/2
$name ||= $self->_refetch_user_name($id)
$name $self->_refetch_user_name($id)
Truth table33%1/3
A ||= B
A= $nameB= $self->_refetch_user_name($id)
ABresult
1X1
011
000
177 1 ▶ $name ||= $id;
Condition33%1/3
$name ||= $id
l!l&&r!l&&!r
100
MC/DC0%0/2
$name ||= $id
$name $id
Truth table33%1/3
A ||= B
A= $nameB= $id
ABresult
1X1
011
000
178 1 return $name;
179 }
180
181 0 ▶sub _refetch_user_id {
Subroutine _refetch_user_id: not called CC=1 SCAR=6.9
182 0 my $self = shift;
183 0 my ($name) = @_;
184 0 $self->_refetch_users;
185 0 return $self->{info}->_find_user_id($name);
186 }
187
188 0 ▶sub _refetch_user_name {
Subroutine _refetch_user_name: not called CC=1 SCAR=6.9
189 0 my $self = shift;
190 0 my ($id) = @_;
191 0 $self->_refetch_users;
192 0 return $self->{info}->_find_user_name($id);
193 }
194
195 0 ▶sub _refetch_users {
Subroutine _refetch_users: not called CC=5 SCAR=34.0
196 0 my $self = shift;
197 0 my $res;
198 0 eval {
199 0 my $users = {};
200 0 my $cursor = "";
201 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$cursor eq "" or do { do { $res = $ua->request(GET(qq[https://slack.com/api/users.list?token=$self->{"token"}&cursor=$cursor])); my $args = &JSON::from_json(scalar $res->content); foreach my $user (@{$$args{"members"};}) { $$users{$user->{"id"}} = $user; } ; if (defined $args->{"response_metadata"}{"next_cursor"}) { $cursor = $args->{"response_metadata"}{"next_cursor"}; } } } 0 0 0
202 0 $res = $ua->request(GET "https://slack.com/api/users.list?token=$self->{token}&cursor=$cursor");
203 0 my $args = JSON::from_json($res->content);
204 0 for my $user (@{$args->{members}}) {
205 0 $users->{$user->{id}} = $user;
206 }
207 0 ▶ if (defined($args->{response_metadata}->{next_cursor})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"response_metadata"}{"next_cursor"}) 0 0 0
208 0 $cursor = $args->{response_metadata}->{next_cursor};
209 }
210 } until ($cursor eq "");
211 0 $self->{info}->{users} = $users;
212 };
213 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
214 0 die '_refetch_users response fail:'.Dumper $res->content;
215 }
216 }
217
218 5 ▶sub _listen {
Subroutine _listen: called CC=15 SCAR=28.9
219 5 my $self = shift;
220 5 my ($buffer) = @_;
221 5 my $buffer_obj;
222 5 eval {
223 5 $buffer_obj = JSON::from_json($buffer);
224 };
225 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
226 0 die "response is not json string. : $buffer";
227 }
228 5 ▶ if ($buffer_obj->{type} && $buffer_obj->{type} eq 'reconnect_url') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer_obj->{"type"} and $buffer_obj->{"type"} eq "reconnect_url") 0 5 5
Condition33%1/3
$buffer_obj->{"type"} and $buffer_obj->{"type"} eq "reconnect_url"
!ll&&!rl&&r
050
MC/DC0%0/2
$buffer_obj->{"type"} and $buffer_obj->{"type"} eq "reconnect_url"
$buffer_obj->{"type"} $buffer_obj->{"type"} eq "reconnect_url"
Truth table33%1/3
A and B
A= $buffer_obj->{"type"}B= $buffer_obj->{"type"} eq "reconnect_url"
ABresult
0X0
100
111
229 0 $self->{info}->{url} = $buffer_obj->{url};
230 }
231
232 5 my ($user, $channel);
233 5 ▶ if ($buffer_obj->{user} && !ref($buffer_obj->{user})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer_obj->{"user"} and not ref $buffer_obj->{"user"}) 1 4 5
Condition66%2/3
$buffer_obj->{"user"} and not ref $buffer_obj->{"user"}
!ll&&!rl&&r
401
MC/DC50%1/2
$buffer_obj->{"user"} and not ref $buffer_obj->{"user"}
$buffer_obj->{"user"} not ref $buffer_obj->{"user"}
Truth table66%2/3
A and not(B)
A= $buffer_obj->{"user"}B= not ref $buffer_obj->{"user"}
ABresult
0X0
100
111
234 1 $user = $self->find_user_name($buffer_obj->{user});
235 1 ▶ warn "There are no users of such id: $buffer_obj->{user}" unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 1 1
236 }
237 5 ▶ if ($buffer_obj->{channel} && !ref($buffer_obj->{channel})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer_obj->{"channel"} and not ref $buffer_obj->{"channel"}) 1 4 5
Condition100%3/3
$buffer_obj->{"channel"} and not ref $buffer_obj->{"channel"}
!ll&&!rl&&r
311
MC/DC100%2/2
$buffer_obj->{"channel"} and not ref $buffer_obj->{"channel"}
$buffer_obj->{"channel"} not ref $buffer_obj->{"channel"}
Truth table100%3/3
A and not(B)
A= $buffer_obj->{"channel"}B= not ref $buffer_obj->{"channel"}
ABresult
0X0
100
111
238 1 $channel = $self->find_conversation_name($buffer_obj->{channel});
239 1 ▶ warn "There are no conversations of such id: $buffer_obj->{channel}" unless $channel;
Branch: 1 branch
BranchTrueFalseTotal
unless $channel 0 1 1
240
241 }
242 5 my $response = Slack::RTM::Bot::Response->new(
243 buffer => $buffer_obj,
244 user => $user,
245 channel => $channel
246 );
247 5 ACTION: for my $action(@{$self->{actions}}){
248 10 for my $key(keys %{$action->{events}}){
249 10 my $regex = $action->{events}->{$key};
250 10 ▶ if(!defined $response->{$key} || $response->{$key} !~ $regex){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->{$key} and $response->{$key} =~ /$regex/) 5 5 10
Condition100%3/3
defined $response->{$key} and $response->{$key} =~ /$regex/
!ll&&!rl&&r
145
MC/DC100%2/2
defined $response->{$key} and $response->{$key} =~ /$regex/
defined $response->{$key} $response->{$key} =~ /$regex/
Truth table100%3/3
A and B
A= defined $response->{$key}B= $response->{$key} =~ /$regex/
ABresult
0X0
100
111
251 5 next ACTION;
252 }
253 }
254 5 eval {
255 5 $action->{routine}->($response);
256 };
257 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
258 0 warn $@;
259 0 kill 9, @{$self->{pids}};
260 0 exit(1);
261 }
262 }
263 };
264
265 8 1;
« Bot.pm ↑ Summary Bot/Information.pm »