File Coverage

blib/lib/Weixin/Client.pm
Criterion Covered Total %
statement 21 41 51.2
branch 0 2 0.0
condition 0 3 0.0
subroutine 7 10 70.0
pod 1 1 100.0
total 29 57 50.8


line stmt bran cond sub pod time code
1             package Weixin::Client;
2 1     1   12763 use strict;
  1         2  
  1         27  
3 1     1   3 use File::Spec;
  1         2  
  1         13  
4 1     1   258 use Weixin::Util;
  1         2  
  1         57  
5 1     1   514 use LWP::UserAgent;
  1         29336  
  1         28  
6 1     1   342 use Weixin::UserAgent;
  1         2  
  1         30  
7 1     1   522 use LWP::Protocol::https;
  1         66817  
  1         46  
8              
9 1         365 use base qw(
10             Weixin::Message
11             Weixin::Client::Callback
12             Weixin::Client::Operate
13             Weixin::Client::Friend
14             Weixin::Client::Chatroom
15             Weixin::Client::Request
16             Weixin::Client::Cron
17             Weixin::Client::Plugin
18             Weixin::Client::Base
19 1     1   8 );
  1         1  
20              
21             our $VERSION = "1.8";
22              
23             sub new{
24 0     0 1   my $class = shift;
25 0           my %p = @_;
26 0           my $agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062';
27 0           my $tmpdir = File::Spec->tmpdir();
28 0   0       my $cookie_filename = $p{login_file} || "$tmpdir/weixin_client_login.dat";
29 0           my $self = {
30             cookie_jar => HTTP::Cookies->new(hide_cookie2=>1,file=>$cookie_filename,autosave=>1),
31             debug => $p{debug},
32             _token => {},
33             _watchers => {},
34             _intervals => {},
35             _synccheck_error_count => 0,
36             _synccheck_running => 0,
37             _sync_running => 0,
38             _sync_interval => 1,
39             _synccheck_interval => 1,
40             _send_msg_interval => 4,
41             _last_sync_time => undef,
42             _last_synccheck_time => undef,
43             _send_message_queue => Weixin::Message::Queue->new,
44             _receive_message_queue => Weixin::Message::Queue->new,
45             _data => {
46             user => {},
47             friend => [],
48             chatroom => [],
49             },
50             on_run => undef,
51             on_receive_msg => undef,
52             on_send_msg => undef,
53             is_stop => 0,
54             plugin_num => 0,
55             plugins => {},
56             ua_retry_times => 5,
57             tmpdir => $tmpdir,
58             client_version => $VERSION,
59             };
60 0           $self->{ua} = LWP::UserAgent->new(
61             cookie_jar => $self->{cookie_jar},
62             agent => $agent,
63             timeout => 300,
64             ssl_opts => {verify_hostname => 0},
65             );
66 0           $self->{asyn_ua} = Weixin::UserAgent->new(
67             cookie_jar => $self->{cookie_jar},
68             agent => $agent,
69             request_timeout => 300,
70             inactivity_timeout => 300,
71             );
72              
73 0 0         if($self->{debug}){
74             $self->{ua}->add_handler(request_send => sub {
75 0     0     my($request, $ua, $h) = @_;
76 0           print $request->as_string;
77 0           return;
78 0           });
79             $self->{ua}->add_handler(
80 0     0     response_header => sub { my($response, $ua, $h) = @_;
81 0           print $response->as_string;
82 0           return;
83            
84 0           });
85             #$self->{ua}->add_handler(
86             # response_done => sub { my($response, $ua, $h) = @_;
87             # #print substr($response->content,0,1000),"\n" if $response->header("content-type")=~/^text/;
88             # return;
89             #});
90             }
91 0           bless $self,$class;
92 0           $self->prepare();
93 0           return $self;
94             }
95              
96              
97              
98             1;