SinaWeibo.pm

filter: Statementstmt 16.6% 97 / 584 Branchbran 0.0% 0 / 268 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 22.6% 17 / 75 Podpod 10.7% 3 / 28 totaltotal 11.4% 117 / 1025 CC 155 SCAR 55.8
CC
155
Cov
11%
login
992.0
parse_im_msg
812.0
__ANON__
156.0
CRAP 264.5
↑ Summary SinaWeibo/Server.pm »
1 package Mojo::SinaWeibo;
2 1 $Mojo::SinaWeibo::VERSION = "1.6";
3 1 $Mojo::SinaWeibo::SEND_INTERVAL = 2;
4 1 $Mojo::SinaWeibo::LAST_DISPATCH_TIME = undef;
5 1 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::Util qw(b64_encode dumper sha1_sum url_escape url_unescape encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::RSA::ES::PKCS1v15;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Crypt::RSA::Key::Public;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Encode::Locale ;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 has 'user';
21 1 has 'pwd';
22 1 has ua_debug => 0;
23 1 has log_level => 'info'; #debug|info|warn|error|fatal
24 1 has log_path => undef;
25 0 ▶has ioloop => sub {Mojo::IOLoop->singleton};
Subroutine __ANON__: not called CC=1 SCAR=1.2
26  
27 1 has max_timeout_count => 5;
28 1 has timeout => 15;
29 1 has timeout_count => 0;
30  
31 0 ▶has log => sub{
Subroutine __ANON__: not called CC=1 SCAR=5.7
32 0 require Mojo::Log;
33 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 0 ▶ *Mojo::Log::append = sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
35 0 my ($self, $msg) = @_;
36 0 ▶ return unless my $handle = $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
unless my $handle = $self->handle 0 0 0
37 0 flock $handle, LOCK_EX;
38 0 ▶ $handle->print(encode("console_out", $msg)) or croak "Can't write to log: $!";
Branch: 1 branch
BranchTrueFalseTotal
$handle->print(encode("console_out", $msg)) or croak("Can't write to log: $!") 0 0 0
39 0 flock $handle, LOCK_UN;
40 0 };
41 0 ▶ Mojo::Log->new(path=>$_[0]->log_path,level=>$_[0]->log_level,format=>sub{
Subroutine __ANON__: not called CC=6 SCAR=37.4
42 0 my ($time, $level, @lines) = @_;
43 0 my $title="";
44 0 ▶ if(ref $lines[0] eq "HASH"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lines[0] eq "HASH") 0 0 0
45 0 my $opt = shift @lines;
46 0 ▶ $time = $opt->{"time"} if defined $opt->{"time"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"time"} 0 0 0
47 0 ▶ $title = (defined $opt->{"title"})?$opt->{title} . " ":"";
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'title'} ? : 0 0 0
48 0 ▶ $level = $opt->{level} if defined $opt->{"level"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"level"} 0 0 0
49 }
50 0 @lines = split /\n/,join "",@lines;
51 0 my $return = "";
52 0 $time = POSIX::strftime('[%y/%m/%d %H:%M:%S]',localtime($time));
53 0 for(@lines){
54 0 $return .=
55 $time
56 . " "
57 . "[$level]"
58 . " "
59 . $title
60 . $_
61 . "\n";
62 }
63 0 return $return;
64 0 });
65 1 };
66 0 ▶has ua => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 local $ENV{MOJO_USERAGENT_DEBUG} = 0;
68 0 require Mojo::UserAgent;
69 0 Mojo::UserAgent->new(
70 request_timeout => 30,
71 inactivity_timeout => 30,
72 max_redirects => 7,
73 transactor => Mojo::UserAgent::Transactor->new(
74 name => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062'
75 ),
76 );
77 1 };
78  
79 1 has 'nick';
80 1 has login_type => "rsa";#wsse
81 1 has api_form => "HTML";#HTML|JSON
82 1 has login_state => "invalid";
83 1 has 'need_pin' => 0;
84 0 ▶has rsa => sub {Crypt::RSA::ES::PKCS1v15->new};
Subroutine __ANON__: not called CC=1 SCAR=1.2
85 1 has 'servertime';
86 1 has 'pcid';
87 1 has 'pubkey';
88 1 has 'nonce';
89 1 has 'rsakv';
90 1 has 'exectime';
91 1 has 'verifycode';
92 1 has 'uid';
93 1 has 'home';
94 1 has 'showpin';
95 1 has 'ticket';
96 1 has 'im_msg_id' => 0;
97 1 has 'im_ack' => -1;
98 1 has 'im';
99 1 has 'im_clientid';
100 1 has 'im_channel';
101 1 has 'im_server';
102 1 has 'im_connect_interval' => 0;
103 1 has 'im_xiaoice_uid' => 5175429989;
104 0 ▶has 'im_client_lag_data' => sub{[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
105 0 ▶has 'im_server_lag_data' => sub{[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
106 1 has 'im_ready' => 0;
107 0 ▶has im_user => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
108 1 has 'im_api_server';
109 0 ▶has im_queue => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
110  
111 0 ▶sub search_im_user{
Subroutine search_im_user: not called CC=4 SCAR=30.0
Pod undocumented
112 0 my $s = shift;
113 0 my %p = @_;
114 0 ▶ return if 0 == grep {defined $p{$_}} keys %p;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep({defined $p{$_};} keys %p) 0 0 0
115 0 ▶ if(wantarray){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
116 0 ▶ return grep {my $f = $_;(first {$p{$_} ne $f->{$_}} grep {defined $p{$_}} keys %p) ? 0 : 1;} @{$s->im_user};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $p{$_} ne $f->{$_}; } , grep({defined $p{$_};} keys %p)) ? : 0 0 0
117 }
118 else{
119 0 ▶ return first {my $f = $_;(first {$p{$_} ne $f->{$_}} grep {defined $p{$_}} keys %p) ? 0 : 1;} @{$s->im_user};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $p{$_} ne $f->{$_}; } , grep({defined $p{$_};} keys %p)) ? : 0 0 0
120 }
121 }
122 0 ▶sub add_im_user{
Subroutine add_im_user: not called CC=7 SCAR=40.3
Pod undocumented
123 0 my $s = shift;
124 0 my $user = shift;
125 0 ▶ $s->die("不支持的数据类型") if ref $user ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $user ne "HASH" 0 0 0
126 0 ▶ $s->die("不支持的数据类型") if not exists $user->{uid} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $user->{"uid"} 0 0 0
127 0 ▶ $s->die("不支持的数据类型") if not exists $user->{nick} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $user->{"nick"} 0 0 0
128 0 my $nocheck = shift;
129 0 ▶ if(@{$s->im_user} == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$s->im_user;} == 0) 0 0 0
130 0 push @{$s->im_user},$user;
131 0 return $s;
132 }
133 0 ▶ if($nocheck){
Branch: 1 branch
BranchTrueFalseTotal
if ($nocheck) 0 0 0
134 0 push @{$s->im_user},$user;
135 0 return $s;
136 }
137 0 my $u = $s->search_im_user(uid => $user->{uid});
138 0 ▶ if(defined $u){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $u) { } 0 0 0
139 0 $u = $user;
140 }
141 else{#new user
142 0 push @{$s->im_user},$user;
143 }
144 0 return $s;
145 }
146  
147 0 ▶sub auth {
Subroutine auth: not called CC=3 SCAR=24.8
Pod undocumented
148 0 my $s = shift;
149 0 ▶ return $s if $s->login_state eq "success";
Branch: 1 branch
BranchTrueFalseTotal
if $s->login_state eq "success" 0 0 0
150 0 $s->prelogin();
151 0 $s->login();
152 0 ▶ if($s->login_state eq "success"){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->login_state eq "success") 0 0 0
153 0 $s->timeout_count(0);
154 0 return $s
155 }
156 0 $s->fatal("授权失败,程序处于离线状态");
157 0 $s->login_state("stop");
158 }
159 0 ▶sub login {
Subroutine login: not called CC=31 SCAR=69.0
Pod undocumented
160 0 my $s = shift;
161 0 $s->info("正在登录...");
162 0 my $api = 'http://login.sina.com.cn/sso/login.php';
163 0 my $sp = "";
164 0 ▶ if($s->login_type eq "rsa"){
Branch: 2 branches
BranchTrueFalseTotal
if ($s->login_type eq 'rsa') { } 0 0 0
elsif ($s->login_type eq 'wsse') { } 0 0 0
165 0 $s->debug("登录使用rsa加密算法");
166 0 my $public = Crypt::RSA::Key::Public->new;
167 0 $public->n("0x" . $s->pubkey);
168 0 $public->e("0x10001");
169 0 $sp =
170 lc join "",unpack "H*",
171 $s->rsa->encrypt(
172 Key=>$public,
173 Message=>$s->servertime . "\t" . $s->nonce . "\n" . $s->pwd
174 );
175 }
176 elsif($s->login_type eq "wsse"){
177 0 $s->debug("登录使用wsse加密算法");
178 0 $sp = sha1_sum( "" . sha1_sum(sha1_sum($s->pwd)) . $s->servertime . $s->nonce );
179 }
180 0 ▶ my $post = {
Branch: 2 branches
BranchTrueFalseTotal
$s->login_type eq 'rsa' ? : 0 0 0
$s->api_form eq 'JSON' ? : 0 0 0
181 entry => "weibo",
182 gateway => 1,
183 from => "",
184 savestate => 7,
185 useticket => 1,
186 pagerefer => '',
187 vsnf => 1,
188 service => "miniblog",
189 pwencode => ($s->login_type eq "rsa"?"rsa2":"wsse"),
190 encoding => "UTF-8",
191 prelt => $s->exectime,
192 url => 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
193 returntype => ($s->api_form eq "JSON"?"TEXT":"META"),
194 servertime => $s->servertime,
195 nonce => $s->nonce,
196 rsakv => $s->rsakv,
197 su => b64_encode(url_escape($s->user),""),
198 sp => $sp,
199 };
200  
201 0 ▶ $post->{door} = $s->verifycode if $s->need_pin;
Branch: 1 branch
BranchTrueFalseTotal
if $s->need_pin 0 0 0
202 0 ▶ $post->{pcid} = $s->pcid if $s->need_pin;
Branch: 1 branch
BranchTrueFalseTotal
if $s->need_pin 0 0 0
203 0 ▶ $post->{sr} = "1366*768" if $s->need_pin;
Branch: 1 branch
BranchTrueFalseTotal
if $s->need_pin 0 0 0
204  
205 0 my $tx = $s->ua->post($api . '?client=ssologin.js%28v1.4.18%29' ,form=>$post);
206 0 ▶ if($s->ua_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->ua_debug) 0 0 0
207 0 print $tx->req->to_string,"\n";
208 0 print $tx->res->to_string,"\n";
209 }
210 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
211 0 my ($retcode,$reason,$feedbackurl,$json);
212 0 ▶ if($post->{returntype} eq "META"){
Branch: 2 branches
BranchTrueFalseTotal
if ($post->{'returntype'} eq 'META') { } 0 0 0
elsif ($post->{'returntype'} eq 'TEXT') { } 0 0 0
213 0 ▶ return unless $tx->res->body =~/location.replace\(['"](.*?)['"]\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->res->body =~ /location.replace\(['"](.*?)['"]\)/u 0 0 0
214 0 $feedbackurl = Mojo::URL->new($1);
215 0 $retcode = $feedbackurl->query->param("retcode");
216 0 ▶ $reason = decode("gb2312",url_unescape($feedbackurl->query->param("reason"))) if defined $feedbackurl->query->param("reason");
Branch: 1 branch
BranchTrueFalseTotal
if defined $feedbackurl->query->param("reason") 0 0 0
217 }
218 elsif($post->{returntype} eq "TEXT"){
219 0 $json = decode_json($tx->res->body);
220 0 $retcode = $json->{retcode};
221 0 ▶ $reason = $json->{reason} if exists $json->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if exists $json->{"reason"} 0 0 0
222 }
223 0 ▶ if($retcode == 0){
Branch: 2 branches
BranchTrueFalseTotal
if ($retcode == 0) { } 0 0 0
elsif ($retcode == 4049) { } 0 0 0
224 0 ▶ if($post->{returntype} eq "TEXT"){
Branch: 2 branches
BranchTrueFalseTotal
if ($post->{'returntype'} eq 'TEXT') { } 0 0 0
elsif ($post->{'returntype'} eq 'META') { } 0 0 0
225 $s->ticket($json->{ticket})
226 ->uid($json->{uid})
227 ->home("http://weibo.com/u/$json->{uid}/home")
228 ->nick($json->{nick})
229 0 ->login_state("success");
230 0 $s->info("登录成功");
231 }
232 elsif($post->{returntype} eq "META"){
233 0 $s->ticket($feedbackurl->query->param("ticket"));
234 0 ▶ if($tx->res->body=~/sinaSSOController\.setCrossDomainUrlList\((.*?)\)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->body =~ /sinaSSOController\.setCrossDomainUrlList\((.*?)\)/u) 0 0 0
235 0 my $json = decode_json($1);
236 0 my $i=0;
237 0 $s->debug("处理跨域访问域名列表...");
238 0 for (@{ $json->{arrURL} }){
239 0 my $url = Mojo::URL->new($_);
240 0 $url->query->merge(
241 callback => "sinaSSOController.doCrossDomainCallBack",
242 scriptId => "ssoscript$i",
243 client => 'ssologin.js(v1.4.18)',
244 _ => $s->time(),
245 );
246 0 my $tx = $s->ua->get($url->to_string);
247 0 ▶ if($s->ua_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->ua_debug) 0 0 0
248 0 print $tx->req->to_string,"\n";
249 0 print $tx->res->to_string,"\n";
250 }
251 0 $i++;
252 }
253 }
254 0 my $tx = $s->ua->get($feedbackurl->to_string);
255 0 ▶ if($s->ua_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->ua_debug) 0 0 0
256 0 print $tx->req->to_string,"\n";
257 0 print $tx->res->to_string,"\n";
258 }
259 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
260 0 ▶ return unless $tx->res->body =~/parent\.sinaSSOController\.feedBackUrlCallBack\((.*?)\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->res->body =~ /parent\.sinaSSOController\.feedBackUrlCallBack\((.*?)\)/u 0 0 0
261 0 $s->debug("获取登录回调参数...");
262 0 my $json = decode_json($1);
263 0 ▶ return unless $json->{result};
Branch: 1 branch
BranchTrueFalseTotal
unless $json->{"result"} 0 0 0
264 0 $s->uid($json->{userinfo}{uniqueid})->home("http://weibo.com/u/$json->{userinfo}{uniqueid}/home");
265 0 $s->debug("进行首页跳转...");
266 0 ▶ if(defined $json->{redirect}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{'redirect'}) { } 0 0 0
267 0 my $tx = $s->ua->get($json->{redirect}) ;
268 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
269 0 $s->login_state("success");
270 0 $s->info("登录成功");
271 }
272 else{
273 0 my $tx = $s->ua->get("http://weibo.com/" . $json->{userinfo}{userdomain});
274 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
275 0 $s->login_state("success");
276 0 $s->info("登录成功");
277 }
278 }
279 }
280 elsif($retcode ==4049){
281 0 ▶ $s->get_pin() && $s->login();
Branch: 1 branch
BranchTrueFalseTotal
$s->get_pin and $s->login 0 0 0
282 }
283 else{
284 0 ▶ $s->error($reason?"登录失败: $retcode($reason)":"登录失败: $retcode");
Branch: 1 branch
BranchTrueFalseTotal
$reason ? : 0 0 0
285 0 return;
286 }
287 }
288  
289 0 ▶sub get_im_info{
Subroutine get_im_info: not called CC=6 SCAR=37.4
Pod undocumented
290 0 my $s = shift;
291 0 ▶ return +{channel=>$s->im_channel,server=>$s->im_server} if (defined $s->im_channel and $s->im_server);
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->im_channel and $s->im_server 0 0 0
Condition0%0/3
defined $s->im_channel and $s->im_server
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s->im_channel and $s->im_server
defined $s->im_channel $s->im_server
Truth table0%0/3
A and B
A= defined $s->im_channelB= $s->im_server
ABresult
0X0
100
111
292 0 my $api = "http://nas.im.api.weibo.com/im/webim.jsp";
293 0 my $callback = "IM_" . $s->time();
294 0 my $query_string = {
295 uid => $s->uid,
296 returntype => "json",
297 v => "1.1",
298 callback => $callback,
299 __rnd => $s->time(),
300 };
301 0 $s->debug("获取私信服务器地址...");
302 0 my $tx = $s->ua->get($api,{Referer=>$s->home},form=>$query_string);
303 0 ▶ if($s->ua_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->ua_debug) 0 0 0
304 0 print $tx->req->to_string,"\n";
305 0 print $tx->res->to_string,"\n";
306 }
307 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
308 0 ▶ return unless $tx->res->body=~/\Q$callback\E\((.*?)\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->res->body =~ /\Q$callback\E\((.*?)\)/u 0 0 0
309 0 my $json = decode_json($1);
310 0 $json->{server} =~s#^http#ws#;
311 0 $json->{server} =~s#/$##;
312 0 $s->debug("私信服务器地址[ " . $json->{server} . $json->{channel} . " ]");
313 0 $json->{server} .= "/im";
314 0 $s->im_server($json->{server})->im_channel($json->{channel});
315 0 return {channel=>$json->{channel},server=>$json->{server}};
316 }
317  
318  
319 0 ▶sub get_pin{
Subroutine get_pin: not called CC=3 SCAR=24.8
Pod undocumented
320 0 my $s = shift;
321 0 $s->info("正在获取验证码图片...");
322 0 my $api = 'http://login.sina.com.cn/cgi/pin.php';
323 0 my $query_string = {
324 r => POSIX::floor(rand() * (10**8)),
325 s => 0,
326 p => $s->pcid,
327 };
328 0 my $tx = $s->ua->get($api,form=>$query_string);
329 0 ▶ if($s->ua_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->ua_debug) 0 0 0
330 0 print $tx->req->to_string,"\n";
331 0 print $tx->res->headers->to_string,"\n";
332 }
333 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
334 0 my ($fh, $filename) = tempfile("sinaweibo_img_verfiy_XXXX",SUFFIX =>".png",TMPDIR => 1);
335 0 binmode $fh;
336 0 print $fh $tx->res->body;
337 0 close $fh;
338 0 my $filename_for_console = decode("locale_fs",$filename);
339 0 my $info = $s->log->format->(CORE::time,"info","请输入图片验证码 [ $filename_for_console ]: ");
340 0 chomp $info;
341 0 $s->log->append($info);
342 0 my $input;
343 0 chomp($input=<STDIN>);
344 0 $s->verifycode($input)->need_pin(1);
345 0 return 1;
346 }
347  
348  
349 0 ▶sub prelogin{
Subroutine prelogin: not called CC=7 SCAR=40.3
Pod undocumented
350 0 my $s = shift;
351 0 $s->info("准备登录微博帐号[ ".$s->user." ]");
352 0 my $api = 'http://login.sina.com.cn/sso/prelogin.php';
353 0 my $query_string = {
354 entry => 'weibo',
355 client => 'ssologin.js(v1.4.18)',
356 callback => 'sinaSSOController.preloginCallBack',
357 su => 'TGVuZGZhdGluZyU0MHNpbmEuY29t',
358 rsakt => 'mod',
359 checkpin => '1',
360 _ => $s->time(),
361 };
362 0 my $tx = $s->ua->get($api,form=>$query_string);
363 0 ▶ if($s->ua_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->ua_debug) 0 0 0
364 0 print $tx->req->to_string,"\n";
365 0 print $tx->res->to_string,"\n";
366 }
367 0 ▶ return unless $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->success 0 0 0
368 0 ▶ return unless $tx->res->body =~ /^sinaSSOController\.preloginCallBack\((.*)\)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->res->body =~ /^sinaSSOController\.preloginCallBack\((.*)\)$/u 0 0 0
369 0 my $json = decode_json($1);
370 0 ▶ return if $json->{retcode}!=0;
Branch: 1 branch
BranchTrueFalseTotal
if $json->{"retcode"} != 0 0 0 0
371 0 for (qw(servertime pcid pubkey nonce rsakv exectime showpin)){
372 0 ▶ $s->$_($json->{$_}) if exists $json->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $json->{$_} 0 0 0
373 }
374 }
375  
376 0 ▶sub gen_im_msg_id {
Subroutine gen_im_msg_id: not called CC=1 SCAR=6.9
Pod undocumented
377 0 my $s = shift;
378 0 my $last_id = $s->im_msg_id;
379 0 $s->im_msg_id(++$last_id);
380 0 return $last_id;
381 }
382 0 ▶sub gen_im_ack{
Subroutine gen_im_ack: not called CC=2 SCAR=17.9
Pod undocumented
383 0 my $s = shift;
384 0 my $last_ack = $s->im_ack;
385 0 ▶ if($last_ack == -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($last_ack == -1) { } 0 0 0
386 0 $s->im_ack(0);
387 0 return $last_ack;
388 }
389 else{
390 0 $s->im_ack(++$last_ack);
391 0 return $last_ack;
392 }
393 }
394  
395 0 ▶sub time{
Subroutine time: not called CC=1 SCAR=6.9
Pod undocumented
396 0 my $s = shift;
397 0 return int(Time::HiRes::time * 1000);
398 }
399 0 ▶sub gmtime_string {
Subroutine gmtime_string: not called CC=2 SCAR=17.9
Pod undocumented
400 0 my $s = shift;
401 0 my $time = shift;
402 0 ▶ $time = CORE::time unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
403 0 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
404 0 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
405 0 my %MoY;
406 0 @MoY{@MoY} = (1..12);
407 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = CORE::gmtime($time);
408 0 sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
409 $DoW[$wday],
410 $mday, $MoY[$mon], $year+1900,
411 $hour, $min, $sec);
412 }
413  
414 0 ▶sub gen_im_msg{
Subroutine gen_im_msg: not called CC=9 SCAR=45.0
Pod undocumented
415 0 my $s = shift;
416 0 my $type = shift;
417 0 my $msg = {};
418 0 ▶ if($type eq "handshake"){
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'handshake') { } 0 0 0
elsif ($type eq 'connect') { } 0 0 0
elsif ($type eq 'subscribe') { } 0 0 0
elsif ($type eq 'cmd') { } 0 0 0
419 0 $msg =
420 {
421 version => "1.0",
422 minimumVersion => "0.9",
423 channel => "/meta/handshake",
424 supportedConnectionTypes=> ["websocket",],#"callback-polling"],
425 advice => {timeout=>60000,interval=>0},
426 id => $s->gen_im_msg_id,
427 ext => {ack => Mojo::JSON->true,timesync=>{tc=>$s->time,l=>0,o=>0}},
428 timestamp => $s->gmtime_string,
429 };
430 }
431 elsif($type eq "connect"){
432 0 $msg =
433 {
434 channel => "/meta/connect",
435 connectionType => "websocket",
436 clientId => $s->im_clientid,
437 id => $s->gen_im_msg_id(),
438 ext => {ack => $s->gen_im_ack(),timesync=>{tc=>$s->time,l=>0,o=>0}},
439 timestamp => $s->gmtime_string,
440 };
441 0 ▶ $msg->{advice} = {timeout=>0,} if $msg->{ext}{ack} == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $msg->{"ext"}{"ack"} == -1 0 0 0
442 }
443 elsif($type eq "subscribe"){
444 0 my %p = @_;
445 $msg =
446 {
447 channel => "/meta/subscribe",
448 subscription => $p{channel},
449 0 id => $s->gen_im_msg_id,
450 clientId => $s->im_clientid,
451 ext => {timesync=>{tc=>$s->time,l=>0,o=>0}},
452 timestamp => $s->gmtime_string,
453 };
454 }
455 elsif($type eq "cmd"){
456 0 my %p = @_;
457 0 my $data ={};
458 0 ▶ $data = {cmd=>"recents"} if $p{cmd} eq "recents";
Branch: 1 branch
BranchTrueFalseTotal
if $p{"cmd"} eq "recents" 0 0 0
459 0 ▶ $data = {cmd=>"usersetting",subcmd=>"get",seq=>"get"} if $p{cmd} eq "usersetting";
Branch: 1 branch
BranchTrueFalseTotal
if $p{"cmd"} eq "usersetting" 0 0 0
460 0 ▶ if($p{cmd} eq "msg"){
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"cmd"} eq "msg") 0 0 0
461 0 $data = {cmd=>"msg",uid=>$p{uid},msg=>$p{msg}} ;
462 }
463 $msg =
464 {
465 0 channel => "/im/req",
466 data => $data,
467 id => $s->gen_im_msg_id,
468 clientId => $s->im_clientid,
469 timestamp => $s->gmtime_string,
470 };
471 }
472 0 return $msg;
473 }
474 0 ▶sub parse_im_msg{
Subroutine parse_im_msg: not called CC=28 SCAR=67.0
Pod undocumented
475 0 my $s = shift;
476 0 my $msg = shift;
477 0 ▶ print encode_json($msg),"\n" if $s->ua_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $s->ua_debug 0 0 0
478 0 for my $m(@{$msg}){
479 0 ▶ if($m->{channel} eq '/meta/handshake'){
Branch: 5 branches
BranchTrueFalseTotal
if ($m->{'channel'} eq '/meta/handshake') { } 0 0 0
elsif ($m->{'channel'} eq '/meta/connect') { } 0 0 0
elsif ($m->{'channel'} eq '/meta/subscribe') { } 0 0 0
elsif ($m->{'channel'} eq '/im/req') { } 0 0 0
elsif ($m->{'channel'} eq $s->im_channel) { } 0 0 0
480 0 $s->debug("收到服务器握手消息");
481 0 ▶ return unless first {$_ eq "websocket"} @{$m->{supportedConnectionTypes}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq "websocket"; } , @{$$m{"supportedConnectionTypes"};}) 0 0 0
482 0 ▶ return unless $m->{successful};
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"successful"} 0 0 0
483 0 $s->debug("服务器握手成功");
484 0 $s->im_clientid($m->{clientId});
485 0 $s->im_send($s->gen_im_msg("subscribe",channel=>$s->im_channel));
486 0 $s->im_send($s->gen_im_msg("connect"));
487 }
488 elsif($m->{channel} eq "/meta/connect"){
489 0 $s->debug("收到服务器心跳响应 ack: ".$m->{ext}{ack});
490 0 ▶ return unless $m->{successful};
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"successful"} 0 0 0
491 0 ▶ if(exists $m->{advice} and exists $m->{advice}{interval}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $m->{"advice"} and exists $m->{"advice"}{"interval"}) 0 0 0
Condition0%0/3
exists $m->{"advice"} and exists $m->{"advice"}{"interval"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $m->{"advice"} and exists $m->{"advice"}{"interval"}
exists $m->{"advice"} exists $m->{"advice"}{"interval"}
Truth table0%0/3
A and B
A= exists $m->{"advice"}B= exists $m->{"advice"}{"interval"}
ABresult
0X0
100
111
492 0 $s->im_connect_interval($m->{advice}{interval}/1000);
493 }
494 0 ▶ $s->timer( $s->im_connect_interval,sub{
Subroutine __ANON__: not called CC=4 SCAR=30.0
495 0 my $msg = $s->gen_im_msg("connect");
496 0 ▶ if(exists $m->{ext}{timesync}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $m->{"ext"}{"timesync"}) 0 0 0
497 0 my $i = $s->time;
498 0 my $k = ($i -$m->{ext}{timesync}{tc} - $m->{ext}{timesync}{p})/2;
499 0 my $l = $m->{ext}{timesync}{ts} - $m->{ext}{timesync}{ts} - $k;
500 0 push @{$s->im_client_lag_data},$k;
501 0 push @{$s->im_server_lag_data},$l;
502 0 ▶ if(10<@{$s->im_server_lag_data}){
Branch: 1 branch
BranchTrueFalseTotal
if (10 < @{$s->im_server_lag_data;}) 0 0 0
503 0 shift @{$s->im_server_lag_data};shift @{$s->im_client_lag_data};
504 }
505 0 my $n=0;
506 0 my $o=0;
507 0 ▶ for(my $p=0;$p<@{$s->im_server_lag_data};$p++){
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @{$s->im_server_lag_data;}) 0 0 0
508 0 $n+=$s->im_client_lag_data->[$p];
509 0 $o+=$s->im_server_lag_data->[$p];
510 }
511  
512 0 my $g = int($n/@{$s->im_server_lag_data});my $h=int($o/@{$s->im_server_lag_data});
513 0 $msg->{ext}{timesync}{l} = $g;
514 0 $msg->{ext}{timesync}{o} = $h;
515 }
516 0 $s->im_send($msg);
517 0 });
518 }
519 elsif($m->{channel} eq "/meta/subscribe"){
520 0 ▶ return unless $m->{successful};
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"successful"} 0 0 0
521 0 $s->debug("收到服务器订阅响应消息");
522 0 ▶ if(@{$s->im_user} == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$s->im_user;} == 0) { } 0 0 0
523 0 $s->im_send($s->gen_im_msg("cmd",cmd=>"usersetting"));
524 0 $s->im_send($s->gen_im_msg("cmd",cmd=>"recents"));
525 }
526 else{
527 0 $s->im_ready(1);
528 0 $s->debug("私信服务器状态准备就绪");
529 0 $s->emit("im_ready");
530 }
531 }
532 elsif($m->{channel} eq "/im/req"){
533 0 ▶ next unless $m->{successful};
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"successful"} 0 0 0
534 }
535 elsif($m->{channel} eq $s->im_channel){
536 0 ▶ return unless exists $m->{data}{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $m->{"data"}{"type"} 0 0 0
537 0 ▶ if($m->{data}{type} eq "recents"){
Branch: 3 branches
BranchTrueFalseTotal
if ($m->{'data'}{'type'} eq 'recents') { } 0 0 0
elsif ($m->{'data'}{'type'} eq 'msg') { } 0 0 0
elsif ($m->{'data'}{'type'} eq 'synchroniz') { } 0 0 0
538 0 $s->im_user([ map {{uid=>$_->[0],nick=>$_->[1]}} @{$m->{data}{recents}} ]);
539 0 ▶ if(!$s->im_ready){
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->im_ready) 0 0 0
540 0 $s->im_ready(1);
541 0 $s->debug("私信服务器状态准备就绪");
542 0 $s->emit("im_ready");
543 }
544 }
545  
546 elsif( $m->{data}{type} eq "msg"){
547 0 for(@{$m->{data}{items}}){
548 0 my($uid,$msg,$time) = @$_[0..2];
549 0 my $u = $s->search_im_user(uid=>$uid);
550 0 ▶ my $nick = defined $u?$u->{nick}:"小冰";
Branch: 1 branch
BranchTrueFalseTotal
defined $u ? : 0 0 0
551 0 $s->emit("receive_message",{uid=>$uid,nick=>$nick,content=>$msg,'time'=>int($time/1000)},{is_success=>1,code=>200,msg=>"正常响应"});
552 }
553 }
554
555 elsif($m->{data}{type} eq "synchroniz" ){
556 0 ▶ return unless exists $m->{data}{syncData};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $m->{"data"}{"syncData"} 0 0 0
557 0 my $syncdata = decode_json(encode("utf8",$m->{data}{syncData}));
558 0 ▶ return unless exists $syncdata->{msg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $syncdata->{"msg"} 0 0 0
559 0 ▶ return unless exists $syncdata->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $syncdata->{"uid"} 0 0 0
560 0 ▶ my $time = exists $syncdata->{'time'}?int($syncdata->{'time'}/1000):CORE::time;
Branch: 1 branch
BranchTrueFalseTotal
exists $syncdata->{'time'} ? : 0 0 0
561 0 my($uid,$msg) = ($syncdata->{uid}, $syncdata->{msg});
562 0 my $u = $s->search_im_user(uid=>$uid);
563 0 ▶ my $nick = defined $u?$u->{nick}:"小冰";
Branch: 1 branch
BranchTrueFalseTotal
defined $u ? : 0 0 0
564 0 $s->emit("send_message",{uid=>$uid,nick=>$nick,content=>$msg,'time'=>$time},"sync");
565 }
566 }
567 }
568
569 }
570  
571 0 ▶sub im_init{
Subroutine im_init: not called CC=3 SCAR=24.8
Pod undocumented
572 0 my $s = shift;
573 0 ▶ return if $s->im_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $s->im_ready 0 0 0
574 0 $s->im_msg_id(0)
575 ->im_ack(-1)
576 ->im_ready(0)
577 ->im(undef)
578 ->im_clientid(undef)
579 ->im_connect_interval(0);
580 0 my $im_info = $s->get_im_info();
581 0 ▶ return unless defined $im_info;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $im_info 0 0 0
582 0 ▶ $s->ua->websocket($im_info->{server},sub{
Subroutine __ANON__: not called CC=7 SCAR=40.3
583 0 my ($ua, $tx) = @_;
584 0 ▶ $s->error("Websocket服务器连接失败") and return unless $tx->is_websocket;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->is_websocket 0 0 0
Condition0%0/5
$tx->is_websocket || $s->error("Websocket\x{670d}\x{52a1}\x{5668}\x{8fde}\x{63a5}\x{5931}\x{8d25}") && (return)
l!l&&r!l&&!r
000
$s->error("Websocket\x{670d}\x{52a1}\x{5668}\x{8fde}\x{63a5}\x{5931}\x{8d25}") and return
!ll
00
MC/DC0%0/2
$tx->is_websocket || $s->error("Websocket\x{670d}\x{52a1}\x{5668}\x{8fde}\x{63a5}\x{5931}\x{8d25}") && (return)
$tx->is_websocket $s->error("Websocket\x{670d}\x{52a1}\x{5668}\x{8fde}\x{63a5}\x{5931}\x{8d25}")
Truth table0%0/3
A || B
A= $tx->is_websocketB= $s->error("Websocket\x{670d}\x{52a1}\x{5668}\x{8fde}\x{63a5}\x{5931}\x{8d25}")
ABresult
1X1
011
000
585 0 $s->im($tx);
586 0 ▶ $s->im->on(finish => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
587 0 my ($tx, $code, $reason) = @_;
588 0 $s->debug("WebSocket服务器关闭($code)");
589 0 $s->im_ready(0);
590 0 $s->debug("私信服务器状态失效");
591 0 });
592 0 ▶ $s->im->on(json=>sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
593 0 my ($tx, $msg) = @_;
594 0 $s->parse_im_msg($msg);
595 0 });
596 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($s->im->can("is_established") and $s->im->is_established or $s->im->can("established") and $s->im->established) 0 0 0
Condition0%0/9
$s->im->can("is_established") and $s->im->is_established or $s->im->can("established") and $s->im->established
l!l&&r!l&&!r
000
$s->im->can("is_established") and $s->im->is_established
!ll&&!rl&&r
000
$s->im->can("established") and $s->im->established
!ll&&!rl&&r
000
MC/DC0%0/4
$s->im->can("is_established") and $s->im->is_established or $s->im->can("established") and $s->im->established
$s->im->can("is_established") $s->im->is_established $s->im->can("established") $s->im->established
Truth table0%0/7
A and B or C and D
A= $s->im->can("is_established")B= $s->im->is_establishedC= $s->im->can("established")D= $s->im->established
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
597 ($s->im->can("is_established") and $s->im->is_established)
598 or ($s->im->can("established") and $s->im->established)
599 ){
600 0 $s->debug("Websocket服务器连接成功");
601 0 $s->im_send($s->gen_im_msg("handshake"));
602 }
603 0 });
604 }
605  
606 0 ▶sub im_speek{
Subroutine im_speek: not called CC=7 SCAR=40.3
Pod undocumented
607 0 my $s = shift;
608 0 my $uid = shift;
609 0 my $content = shift;
610 0 my $callback = pop;
611 0 ▶ $content = decode("utf8",$content) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 0 0 0
612 0 ▶ $s->auth() if $s->login_state eq "invalid";
Branch: 1 branch
BranchTrueFalseTotal
if $s->login_state eq "invalid" 0 0 0
613 0 ▶ if($s->login_state eq "stop"){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->login_state eq "stop") 0 0 0
614 0 ▶ $callback->(undef,{is_success=>0,code=>503,msg=>encode("utf8","响应超时")}) if ref $callback eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback eq "CODE" 0 0 0
615 0 return;
616 }
617 0 my $delay = 0;
618 0 my $now = CORE::time;
619 0 ▶ if(defined $Mojo::SinaWeibo::LAST_DISPATCH_TIME){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Mojo::SinaWeibo::LAST_DISPATCH_TIME) 0 0 0
620 0 ▶ $delay = $now<$Mojo::SinaWeibo::LAST_DISPATCH_TIME+$Mojo::SinaWeibo::SEND_INTERVAL?
Branch: 1 branch
BranchTrueFalseTotal
$now < $Mojo::SinaWeibo::LAST_DISPATCH_TIME + $Mojo::SinaWeibo::SEND_INTERVAL ? : 0 0 0
621 $Mojo::SinaWeibo::LAST_DISPATCH_TIME+$Mojo::SinaWeibo::SEND_INTERVAL-$now
622 : 0;
623 };
624  
625 0 ▶ $s->timer($delay,sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
626 0 ▶ if(ref $callback eq "CODE"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback eq "CODE") 0 0 0
627 0 my $ask = {cb=>$callback,,status=>"wait"};
628 0 ▶ my $id = $s->timer($s->timeout,sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
629 0 ▶ return if $ask->{status} eq "done";
Branch: 1 branch
BranchTrueFalseTotal
if $ask->{"status"} eq "done" 0 0 0
630 0 $ask->{status} = "done";
631 0 ▶ $ask->{cb}->(undef,{is_success=>0,code=>503,msg=>encode("utf8","响应超时")}) if ref $ask->{cb} eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $ask->{"cb"} eq "CODE" 0 0 0
632 0 $s->warn("消息响应超时,放弃等待");
633 0 });
634 0 $ask->{timer} = $id;
635 0 push @{$s->im_queue},$ask;
636 }
637 0 ▶ if($s->im_ready){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->im_ready) { } 0 0 0
638 0 my $msg = $s->gen_im_msg("cmd",cmd=>"msg",uid=>$uid,msg=>$content);
639 0 $s->im_send($msg);
640
641 }
642 else{
643 0 ▶ $s->once(im_ready=>sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
644 0 my $s = shift;
645 0 my $msg = $s->gen_im_msg("cmd",cmd=>"msg",uid=>$uid,msg=>$content);
646 0 $s->im_send($msg);
647 0 });
648 0 $s->im_init();
649 }
650 0 });
651  
652 0 $Mojo::SinaWeibo::LAST_DISPATCH_TIME = $now+$delay;
653  
654 }
655  
656 0 ▶sub ask_xiaoice{
Subroutine ask_xiaoice: not called CC=1 SCAR=6.9
Pod documented
657 0 my $s = shift;
658 0 my $uid = $s->im_xiaoice_uid;
659 0 my $content = shift;
660 0 my $callback = pop;
661 0 $s->im_speek($uid,$content,$callback);
662 }
663 0 ▶sub im_send{
Subroutine im_send: not called CC=1 SCAR=6.9
Pod undocumented
664 0 my $s= shift;
665 0 my $msg = shift;
666 0 ▶ $s->im->send({json=>[$msg]},sub{
Subroutine __ANON__: not called CC=12 SCAR=50.5
667 0 ▶ print encode_json($msg),"\n" if $s->ua_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $s->ua_debug 0 0 0
668 0 ▶ $s->debug("发送usersetting消息") if ($msg->{channel} eq "/im/req" and $msg->{data}{cmd} eq "usersetting");
Branch: 1 branch
BranchTrueFalseTotal
if $msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "usersetting" 0 0 0
Condition0%0/3
$msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "usersetting"
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "usersetting"
$msg->{"channel"} eq "/im/req" $msg->{"data"}{"cmd"} eq "usersetting"
Truth table0%0/3
A and B
A= $msg->{"channel"} eq "/im/req"B= $msg->{"data"}{"cmd"} eq "usersetting"
ABresult
0X0
100
111
669 0 ▶ $s->debug("发送recents消息") if ($msg->{channel} eq "/im/req" and $msg->{data}{cmd} eq "recents");
Branch: 1 branch
BranchTrueFalseTotal
if $msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "recents" 0 0 0
Condition0%0/3
$msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "recents"
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "recents"
$msg->{"channel"} eq "/im/req" $msg->{"data"}{"cmd"} eq "recents"
Truth table0%0/3
A and B
A= $msg->{"channel"} eq "/im/req"B= $msg->{"data"}{"cmd"} eq "recents"
ABresult
0X0
100
111
670 0 ▶ $s->debug("发送握手消息") if $msg->{channel} eq "/meta/handshake";
Branch: 1 branch
BranchTrueFalseTotal
if $msg->{"channel"} eq "/meta/handshake" 0 0 0
671 0 ▶ $s->debug("发送心跳消息 ack: " . $msg->{ext}{ack}) if $msg->{channel} eq "/meta/connect";
Branch: 1 branch
BranchTrueFalseTotal
if $msg->{"channel"} eq "/meta/connect" 0 0 0
672 0 ▶ $s->debug("发送订阅消息") if $msg->{channel} eq "/meta/subscribe";
Branch: 1 branch
BranchTrueFalseTotal
if $msg->{"channel"} eq "/meta/subscribe" 0 0 0
673 0 ▶ if($msg->{channel} eq "/im/req" and $msg->{data}{cmd} eq "msg"){
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "msg") 0 0 0
Condition0%0/3
$msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "msg"
!ll&&!rl&&r
000
MC/DC0%0/2
$msg->{"channel"} eq "/im/req" and $msg->{"data"}{"cmd"} eq "msg"
$msg->{"channel"} eq "/im/req" $msg->{"data"}{"cmd"} eq "msg"
Truth table0%0/3
A and B
A= $msg->{"channel"} eq "/im/req"B= $msg->{"data"}{"cmd"} eq "msg"
ABresult
0X0
100
111
674 0 my $u=$s->search_im_user(uid=>$msg->{data}{uid});
675 $s->emit("send_message"=>{
676 uid=>$msg->{data}{uid},
677 nick=>(defined $u?$u->{nick}:"小冰"),
678 'time'=>CORE::time,
679 content=>$msg->{data}{msg},
680 0 ▶ },"api") ;
Branch: 1 branch
BranchTrueFalseTotal
defined $u ? : 0 0 0
681 }
682 0 });
683 }
684 0 ▶sub start{
Subroutine start: not called CC=10 SCAR=45.5
Pod undocumented
685 0 my $s = shift;
686 0 ▶ my %p = @_ if @_>1 and @_%2==0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 and @_ % 2 == 0 0 0 0
Condition0%0/3
@_ > 1 and @_ % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and @_ % 2 == 0
@_ > 1 @_ % 2 == 0
Truth table0%0/3
A and B
A= @_ > 1B= @_ % 2 == 0
ABresult
0X0
100
111
687 0 ▶ $s->on(im_timeout=>sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
688 0 my $s = shift;
689 0 my $callback = shift;
690 0 $s->warn("私信消息响应超时,放弃等待");
691 0 $callback->(undef,{is_success=>0,code=>503,msg=>encode("utf8","响应超时")});
692 0 my $count = $s->timeout_count;
693 0 $s->timeout_count(++$count);
694 0 ▶ if($s->timeout_count >= $s->max_timeout_count){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->timeout_count >= $s->max_timeout_count) 0 0 0
695 0 $s->im_ready(0);
696 0 $s->login_state("invalid");
697 0 $s->emit("invalid");
698 }
699 0 });
700 0 ▶ $s->on(receive_message=>sub{
Subroutine __ANON__: not called CC=9 SCAR=45.0
701 0 my $s = shift;
702 0 my $msg = shift;
703 0 my $status = shift;
704 0 ▶ return if ref $msg ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $msg ne "HASH" 0 0 0
705 0 $s->info({level=>"私信消息",'time'=>$msg->{'time'},title=>"$msg->{nick} :"},$msg->{content});
706 0 ▶ my $ask = @{$s->im_queue}?shift(@{$s->im_queue}):undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$s->im_queue;} ? : 0 0 0
707 0 ▶ return unless defined $ask;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ask 0 0 0
708 0 ▶ return if $ask->{status} eq "done";
Branch: 1 branch
BranchTrueFalseTotal
if $ask->{"status"} eq "done" 0 0 0
709 0 ▶ if(ref $ask->{cb} eq "CODE" and defined $msg and defined $status){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ask->{"cb"} eq "CODE" and defined $msg and defined $status) 0 0 0
Condition0%0/6
ref $ask->{"cb"} eq "CODE" and defined $msg and defined $status
!ll&&!rl&&r
000
ref $ask->{"cb"} eq "CODE" and defined $msg
!ll&&!rl&&r
000
MC/DC0%0/3
ref $ask->{"cb"} eq "CODE" and defined $msg and defined $status
ref $ask->{"cb"} eq "CODE" defined $msg defined $status
Truth table0%0/4
A and B and C
A= ref $ask->{"cb"} eq "CODE"B= defined $msgC= defined $status
ABCresult
0XX0
10X0
1100
1111
710 $ask->{cb}->({
711 uid=>$msg->{uid},
712 nick=>encode("utf8",$msg->{nick}),
713 content=>encode("utf8",$msg->{content}),
714 'time'=>$msg->{'time'},
715 },
716 {
717 is_success=>$status->{is_success},
718 code=>$status->{code},
719 0 msg=>encode("utf8",$status->{msg}),
720 }
721 );
722 }
723  
724 0 $ask->{status} = "done";
725 0 ▶ $s->ioloop->remove($ask->{timer}) if defined $ask->{timer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ask->{"timer"} 0 0 0
726 0 });
727 0 ▶ $s->on(send_message=>sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
728 0 my $s = shift;
729 0 my $msg = shift;
730 0 my $type = shift;
731 0 ▶ return if ref $msg ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $msg ne "HASH" 0 0 0
732 0 $s->info({level=>"私信消息",'time'=>$msg->{'time'},title=>"我->$msg->{nick} :"},$msg->{content});
733 0 ▶ push @{$s->im_queue},{cb=>undef,status=>"wait"} if $type eq "sync";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sync" 0 0 0
734 0 });
735  
736 0 ▶ $s->on(invalid=>sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
737 0 my $s = shift;
738 0 $s->warn("程序当前状态不可用,尝试重新授权");
739 0 $s->auth();
740 0 });
741  
742 0 ▶ if(exists $p{enable_api_server} and $p{enable_api_server} ==1){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{"enable_api_server"} and $p{"enable_api_server"} == 1) 0 0 0
Condition0%0/3
exists $p{"enable_api_server"} and $p{"enable_api_server"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $p{"enable_api_server"} and $p{"enable_api_server"} == 1
exists $p{"enable_api_server"} $p{"enable_api_server"} == 1
Truth table0%0/3
A and B
A= exists $p{"enable_api_server"}B= $p{"enable_api_server"} == 1
ABresult
0X0
100
111
743 package Mojo::SinaWeibo::Openxiaoice;
744 1 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
745 1 ▶ use Mojolicious::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
746 0 ▶ any [qw(GET POST)] => '/openxiaoice/ask' => sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
747 0 my $c = shift;
748 0 my $q = encode("utf8",$c->param("q"));
749 0 $c->render_later;
750 0 ▶ $s->ask_xiaoice($q,sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
751 0 my($msg,$status) = @_;
752 0 ▶ if($status->{is_success}){
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{'is_success'}) { } 0 0 0
753 0 $c->render(json=>{code=>1,answer=>decode("utf8",$msg->{content})});
754 }
755 else{
756 0 $c->render(json=>{code=>0,answer=>undef,reason=>decode("utf8",$status->{msg})});
757 }
758 0 });
759 0 };
760 0 ▶ any '/*whatever' => sub{whatever=>'',$_[0]->render(text => "request error",status=>403)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
761 package Mojo::SinaWeibo;
762 0 require Mojo::SinaWeibo::Server;
763 0 ▶ my $data = [{host=>$p{host}||"0.0.0.0",port=>$p{port}||3000}] ;
Condition0%0/4
$p{'host'} || '0.0.0.0'
l!l
00
$p{'port'} || 3000
l!l
00
MC/DC0%0/1
$p{'host'} || '0.0.0.0'
$p{'host'}
MC/DC0%0/1
$p{'port'} || 3000
$p{'port'}
Truth table0%0/2
A
A= $p{'host'}
Aresult
11
00
Truth table0%0/2
A
A= $p{'port'}
Aresult
11
00
764 0 my $server = Mojo::SinaWeibo::Server->new();
765 0 $s->im_api_server($server);
766 0 $server->app($server->build_app("Mojo::SinaWeibo::Openxiaoice"));
767 0 $server->app->secrets("hello world");
768 0 $server->app->log($s->log);
769 0 ▶ $server->listen([ map { 'http://' . (defined $_->{host}?$_->{host}:"0.0.0.0") .":" . (defined $_->{port}?$_->{port}:3000)} @$data]) if ref $data eq "ARRAY" ;
Branch: 3 branches
BranchTrueFalseTotal
if ref $data eq "ARRAY" 0 0 0
defined $_->{'host'} ? : 0 0 0
defined $_->{'port'} ? : 0 0 0
770 0 $server->start;
771 }
772 }
773  
774 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
775 0 my $s = shift;
776 0 $s->start(@_);
777 0 ▶ $s->ioloop->start unless $s->ioloop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->ioloop->is_running 0 0 0
778 }
779 0 ▶sub emit_one{
Subroutine emit_one: not called CC=2 SCAR=17.9
Pod undocumented
780 0 my ($s, $name) = (shift, shift);
781 0 ▶ if (my $e = $s->{events}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $s->{"events"}{$name}) 0 0 0
782 0 my $cb = shift @$e;
783 0 $s->$cb(@_);
784 }
785 0 return $s;
786 }
787 0 ▶sub timer{
Subroutine timer: not called CC=1 SCAR=6.9
Pod undocumented
788 0 my $s = shift;
789 0 $s->ioloop->timer(@_);
790 }
791 0 ▶sub interval{
Subroutine interval: not called CC=1 SCAR=6.9
Pod undocumented
792 0 my $s = shift;
793 0 $s->ioloop->recurring(@_);
794 }
795  
796 0 ▶sub die{
Subroutine die: not called CC=1 SCAR=6.9
Pod undocumented
797 0 my $s = shift;
798 0 ▶ local $SIG{__DIE__} = sub{$s->log->fatal(@_);exit -1};
Subroutine __ANON__: not called CC=1 SCAR=6.9
799 0 Carp::confess(@_);
800 }
801 0 ▶sub info{
Subroutine info: not called CC=1 SCAR=6.9
Pod undocumented
802 0 my $s = shift;
803 0 $s->log->info(@_);
804 0 $s;
805 }
806 0 ▶sub warn{
Subroutine warn: not called CC=1 SCAR=6.9
Pod undocumented
807 0 my $s = shift;
808 0 $s->log->warn(@_);
809 0 $s;
810 }
811 0 ▶sub error{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
812 0 my $s = shift;
813 0 $s->log->error(@_);
814 0 $s;
815 }
816 0 ▶sub fatal{
Subroutine fatal: not called CC=1 SCAR=6.9
Pod undocumented
817 0 my $s = shift;
818 0 $s->log->fatal(@_);
819 0 $s;
820 }
821 0 ▶sub debug{
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
822 0 my $s = shift;
823 0 $s->log->debug(@_);
824 0 $s;
825 }
826 1 1;
↑ Summary SinaWeibo/Server.pm »