File Coverage

blib/lib/Webqq/Client/Method/_send_message.pm
Criterion Covered Total %
statement 9 39 23.0
branch 0 20 0.0
condition 0 7 0.0
subroutine 3 5 60.0
pod n/a
total 12 71 16.9


line stmt bran cond sub pod time code
1 1     1   6 use JSON;
  1         1  
  1         7  
2 1     1   238 use Encode;
  1         2  
  1         81  
3 1     1   7 use Storable qw(dclone);
  1         1  
  1         612  
4             sub Webqq::Client::_send_message{
5 0     0     my($self,$msg) = @_;
6             #将整个hash从UTF8还原为unicode
7 0           my $ua = $self->{asyn_ua};
8             my $callback = sub{
9 0     0     my $response = shift;
10 0 0         print $response->content() if $self->{debug};
11 0           my $status = $self->parse_send_status_msg( $response->content() );
12 0 0 0       if(defined $status and $status->{is_success} == 0){
    0          
13 0           $self->send_message($msg);
14 0           return;
15             }
16             elsif(defined $status){
17 0 0         if(ref $msg->{cb} eq 'CODE'){
18 0           $msg->{cb}->(
19             $msg, #msg
20             $status->{is_success}, #is_success
21             $status->{status} #status
22             );
23             }
24 0 0         if(ref $self->{on_send_message} eq 'CODE'){
25 0           $self->{on_send_message}->(
26             $msg, #msg
27             $status->{is_success}, #is_success
28             $status->{status} #status
29             );
30             }
31             }
32 0           };
33 0 0         my $api_url = ($self->{qq_param}{is_https}?'https':'http') . '://d.web2.qq.com/channel/send_buddy_msg2';
34 0 0         my @headers = $self->{type} eq 'webqq'? (Referer=>'http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3')
35             : (Referer=>'http://d.web2.qq.com/proxy.html?v=20130916001&callback=1&id=2')
36             ;
37 0           my $content = [decode("utf8",$msg->{content}),"",[]];
38 0   0       my %s = (
39             to => $msg->{to_uin},
40             face => $self->{qq_database}{user}{face} || 570,
41             content => JSON->new->utf8->encode($content),
42             msg_id => $msg->{msg_id},
43             clientid => $self->{qq_param}{clientid},
44             psessionid => $self->{qq_param}{psessionid},
45             );
46 0           $s{content} = decode("utf8",$s{content});
47            
48 0 0         if($self->{type} eq 'smartqq'){
49 0   0       $s{face} = $self->{qq_database}{user}{face} || "591";
50             }
51 0           my $post_content = [
52             r => JSON->new->utf8->encode(\%s),
53             ];
54 0 0         if($self->{type} eq 'webqq'){
55 0           push @$post_content,(
56             clientid => $self->{qq_param}{clientid},
57             psessionid => $self->{qq_param}{psessionid}
58             );
59             }
60 0 0         if($self->{debug}){
61 0           require URI;
62 0           my $uri = URI->new('http:');
63 0           $uri->query_form($post_content);
64 0           print $api_url,"\n";
65 0           print $uri->query(),"\n";
66             }
67             $ua->post(
68 0           $api_url,
69             $post_content,
70             @headers,
71             $callback,
72             );
73             }
74             1;