| 1 | package Mojo::Weixin::Client; | ||
| 2 | 1 | ▶ | use POSIX (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_login; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_logout; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_get_qrcode_uuid; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_get_qrcode_image; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_is_need_login; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_synccheck; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Mojo::Weixin::Client::Remote::_sync; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Mojo::Weixin::Message::Handle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Mojo::IOLoop; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use Mojo::IOLoop::Delay; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use base qw(Mojo::Weixin::Request); | ||
| 15 | |||
| 16 | sub login{ | ||
| 17 | my $self = shift; | ||
| 18 | return 1 if $self->login_state eq 'success'; | ||
| 19 | if($self->is_first_login == -1){ | ||
| 20 | $self->is_first_login(1); | ||
| 21 | } | ||
| 22 | elsif($self->is_first_login == 1){ | ||
| 23 | $self->is_first_login(0); | ||
| 24 | } | ||
| 25 | |||
| 26 | #if($self->is_first_login){ | ||
| 27 | # #$self->load_cookie();#转移到new的时候就调用,这里不再需要 | ||
| 28 | #} | ||
| 29 | while(1){ | ||
| 30 | $self->check_controller(); | ||
| 31 | my $ret = $self->_login(); | ||
| 32 | $self->clean_qrcode(); | ||
| 33 | sleep 2; | ||
| 34 | if($ret and $self->login_state eq "success" and $self->model_init()){ | ||
| 35 | $self->emit("login"=>($ret==2?1:0)); | ||
| 36 | return 1; | ||
| 37 | } | ||
| 38 | else{ | ||
| 39 | $self->logout(); | ||
| 40 | $self->login_state("init"); | ||
| 41 | $self->error("登录结果异常,再次尝试..."); | ||
| 42 | next; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | sub relogin{ | ||
| 47 | my $self = shift; | ||
| 48 | my $retcode = shift; | ||
| 49 | $self->info("正在重新登录...\n"); | ||
| 50 | if(defined $self->_synccheck_connection_id){ | ||
| 51 | eval{ | ||
| 52 | $self->ioloop->remove($self->_synccheck_connection_id); | ||
| 53 | $self->_synccheck_running(0); | ||
| 54 | $self->info("停止接收消息..."); | ||
| 55 | }; | ||
| 56 | $self->info("停止接收消息失败: $@") if $@; | ||
| 57 | } | ||
| 58 | $self->logout($retcode); | ||
| 59 | $self->login_state("relogin"); | ||
| 60 | #$self->clear_cookie(); | ||
| 61 | |||
| 62 | $self->sync_key(+{LIST=>[]}); | ||
| 63 | $self->synccheck_key(undef); | ||
| 64 | $self->pass_ticket(''); | ||
| 65 | $self->skey(''); | ||
| 66 | $self->wxsid(''); | ||
| 67 | $self->wxuin(''); | ||
| 68 | |||
| 69 | $self->user(+{}); | ||
| 70 | $self->friend([]); | ||
| 71 | $self->group([]); | ||
| 72 | $self->data(+{}); | ||
| 73 | |||
| 74 | $self->login(); | ||
| 75 | $self->timer(2,sub{ | ||
| 76 | $self->info("重新开始接收消息..."); | ||
| 77 | $self->_synccheck(); | ||
| 78 | }); | ||
| 79 | $self->emit("relogin"); | ||
| 80 | } | ||
| 81 | sub logout{ | ||
| 82 | my $self = shift; | ||
| 83 | my $retcode = shift; | ||
| 84 | #my %type = qw( | ||
| 85 | # 1100 0 | ||
| 86 | # 1101 1 | ||
| 87 | # 1102 1 | ||
| 88 | # 1205 1 | ||
| 89 | #); | ||
| 90 | $self->info("客户端正在注销". (defined $retcode?"($retcode)":"") . "..."); | ||
| 91 | $self->_logout(0); | ||
| 92 | $self->_logout(1); | ||
| 93 | } | ||
| 94 | sub steps { | ||
| 95 | my $self = shift; | ||
| 96 | Mojo::IOLoop::Delay->new(ioloop=>$self->ioloop)->steps(@_)->catch(sub { | ||
| 97 | my ($delay, $err) = @_; | ||
| 98 | $self->error("steps error: $err"); | ||
| 99 | })->wait; | ||
| 100 | $self; | ||
| 101 | } | ||
| 102 | sub ready { | ||
| 103 | my $self = shift; | ||
| 104 | $self->state('loading'); | ||
| 105 | #加载插件 | ||
| 106 | my $plugins = $self->plugins; | ||
| 107 | for( | ||
| 108 | sort {$plugins->{$b}{priority} <=> $plugins->{$a}{priority} } | ||
| 109 | grep {defined $plugins->{$_}{auto_call} and $plugins->{$_}{auto_call} == 1} keys %{$plugins} | ||
| 110 | ){ | ||
| 111 | $self->call($_); | ||
| 112 | } | ||
| 113 | $self->state('loading'); | ||
| 114 | $self->emit("after_load_plugin"); | ||
| 115 | $self->login() if $self->login_state ne 'success'; | ||
| 116 | #接收消息 | ||
| 117 | $self->on(synccheck_over=>sub{ | ||
| 118 | my $self = shift; | ||
| 119 | $self->state('running'); | ||
| 120 | my($retcode,$selector,$status) = @_; | ||
| 121 | if(not $status){#检查消息异常时,强制把检查消息(synccheck)间隔设置的更久,直到获取消息(sync)正常为止 | ||
| 122 | $self->debug("检查消息结果异常"); | ||
| 123 | $self->_synccheck_interval($self->synccheck_interval+$self->synccheck_delay); | ||
| 124 | } | ||
| 125 | $self->_parse_synccheck_data($retcode,$selector); | ||
| 126 | $self->timer($self->_synccheck_interval, sub{$self->_synccheck()}); | ||
| 127 | }); | ||
| 128 | $self->on(sync_over=>sub{ | ||
| 129 | my $self = shift; | ||
| 130 | my ($json,$status) = @_; | ||
| 131 | $self->_synccheck_interval($status?$self->synccheck_interval:$self->synccheck_interval+$self->synccheck_delay); | ||
| 132 | $self->_parse_sync_data($json); | ||
| 133 | }); | ||
| 134 | $self->on(run=>sub{ | ||
| 135 | my $self = shift; | ||
| 136 | $self->timer(2,sub{ | ||
| 137 | $self->info("开始接收消息..."); | ||
| 138 | $self->state('running'); | ||
| 139 | $self->_synccheck()} | ||
| 140 | ); | ||
| 141 | }); | ||
| 142 | $self->is_ready(1); | ||
| 143 | $self->emit("ready"); | ||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | sub run{ | ||
| 147 | my $self = shift; | ||
| 148 | $self->ready() if not $self->is_ready; | ||
| 149 | $self->emit("run"); | ||
| 150 | $self->ioloop->start unless $self->ioloop->is_running; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub multi_run{ | ||
| 154 | Mojo::IOLoop->singleton->start unless Mojo::IOLoop->singleton->is_running; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub clean_qrcode{ | ||
| 158 | my $self = shift; | ||
| 159 | return if not defined $self->qrcode_path; | ||
| 160 | return if not -f $self->qrcode_path; | ||
| 161 | $self->info("清除残留的历史二维码图片"); | ||
| 162 | unlink $self->qrcode_path or $self->warn("删除二维码图片[ " . $self->qrcode_path . " ]失败: $!"); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub timer { | ||
| 166 | my $self = shift; | ||
| 167 | return $self->ioloop->timer(@_); | ||
| 168 | } | ||
| 169 | sub interval{ | ||
| 170 | my $self = shift; | ||
| 171 | return $self->ioloop->recurring(@_); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub exit{ | ||
| 175 | my $self = shift; | ||
| 176 | my $code = shift; | ||
| 177 | $self->state('stop'); | ||
| 178 | $self->emit("stop"); | ||
| 179 | $self->info("客户端已退出"); | ||
| 180 | CORE::exit(defined $code?$code+0:0); | ||
| 181 | } | ||
| 182 | sub stop{ | ||
| 183 | my $self = shift; | ||
| 184 | $self->is_stop(1); | ||
| 185 | $self->state('stop'); | ||
| 186 | $self->emit("stop"); | ||
| 187 | $self->info("客户端停止运行"); | ||
| 188 | CORE::exit(); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub spawn { | ||
| 192 | my $self = shift; | ||
| 193 | my %opt = @_; | ||
| 194 | require Mojo::Weixin::Run; | ||
| 195 | my $is_blocking = delete $opt{is_blocking}; | ||
| 196 | my $run = Mojo::Weixin::Run->new(ioloop=>($is_blocking?Mojo::IOLoop->new:$self->ioloop),log=>$self->log); | ||
| 197 | $run->max_forks(delete $opt{max_forks}) if defined $opt{max_forks}; | ||
| 198 | $run->spawn(%opt); | ||
| 199 | $run->start if $is_blocking; | ||
| 200 | $run; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub mail{ | ||
| 204 | my $self = shift; | ||
| 205 | my $callback ; | ||
| 206 | my $is_blocking = 1; | ||
| 207 | if(ref $_[-1] eq "CODE"){ | ||
| 208 | $callback = pop; | ||
| 209 | $is_blocking = 0; | ||
| 210 | } | ||
| 211 | my %opt = @_; | ||
| 212 | #smtp | ||
| 213 | #port | ||
| 214 | #tls | ||
| 215 | #tls_ca | ||
| 216 | #tls_cert | ||
| 217 | #tls_key | ||
| 218 | #user | ||
| 219 | #pass | ||
| 220 | #from | ||
| 221 | #to | ||
| 222 | #cc | ||
| 223 | #subject | ||
| 224 | #charset | ||
| 225 | #html | ||
| 226 | #text | ||
| 227 | #data MIME::Lite产生的发送数据 | ||
| 228 | eval{ require Mojo::SMTP::Client; } ; | ||
| 229 | if($@){ | ||
| 230 | $self->error("发送邮件,请先安装模块 Mojo::SMTP::Client"); | ||
| 231 | return; | ||
| 232 | } | ||
| 233 | my %new = ( | ||
| 234 | address => $opt{smtp}, | ||
| 235 | port => $opt{port} || 25, | ||
| 236 | autodie => $is_blocking, | ||
| 237 | ); | ||
| 238 | for(qw(tls tls_ca tls_cert tls_key)){ | ||
| 239 | $new{$_} = $opt{$_} if defined $opt{$_}; | ||
| 240 | } | ||
| 241 | $new{tls} = 1 if($new{port} == 465 and !defined $new{tls}); | ||
| 242 | my $smtp = Mojo::SMTP::Client->new(%new); | ||
| 243 | unless(defined $smtp){ | ||
| 244 | $self->error("Mojo::SMTP::Client客户端初始化失败"); | ||
| 245 | return; | ||
| 246 | } | ||
| 247 | my $data; | ||
| 248 | if(defined $opt{data}){$data = $opt{data}} | ||
| 249 | else{ | ||
| 250 | my @data; | ||
| 251 | push @data,("From: $opt{from}","To: $opt{to}"); | ||
| 252 | push @data,"Cc: $opt{cc}" if defined $opt{cc}; | ||
| 253 | require MIME::Base64; | ||
| 254 | my $charset = defined $opt{charset}?$opt{charset}:"UTF-8"; | ||
| 255 | push @data,"Subject: =?$charset?B?" . MIME::Base64::encode_base64($opt{subject},"") . "?="; | ||
| 256 | if(defined $opt{text}){ | ||
| 257 | push @data,("Content-Type: text/plain; charset=$charset",'',$opt{text}); | ||
| 258 | } | ||
| 259 | elsif(defined $opt{html}){ | ||
| 260 | push @data,("Content-Type: text/html; charset=$charset",'',$opt{html}); | ||
| 261 | } | ||
| 262 | $data = join "\r\n",@data; | ||
| 263 | } | ||
| 264 | if(defined $callback){#non-blocking send | ||
| 265 | $smtp->send( | ||
| 266 | auth => {login=>$opt{user},password=>$opt{pass}}, | ||
| 267 | from => $opt{from}, | ||
| 268 | to => $opt{to}, | ||
| 269 | data => $data, | ||
| 270 | quit => 1, | ||
| 271 | sub{ | ||
| 272 | my ($smtp, $resp) = @_; | ||
| 273 | if($resp->error){ | ||
| 274 | $self->error("邮件[ To: $opt{to}|Subject: $opt{subject} ]发送失败: " . $resp->error ); | ||
| 275 | $callback->(0,$resp->error) if ref $callback eq "CODE"; | ||
| 276 | return; | ||
| 277 | } | ||
| 278 | else{ | ||
| 279 | $self->debug("邮件[ To: $opt{to}|Subject: $opt{subject} ]发送成功"); | ||
| 280 | $callback->(1) if ref $callback eq "CODE"; | ||
| 281 | } | ||
| 282 | }, | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | else{#blocking send | ||
| 286 | eval{ | ||
| 287 | $smtp->send( | ||
| 288 | auth => {login=>$opt{user},password=>$opt{pass}}, | ||
| 289 | from => $opt{from}, | ||
| 290 | to => $opt{to}, | ||
| 291 | data => $data, | ||
| 292 | quit => 1, | ||
| 293 | ); | ||
| 294 | }; | ||
| 295 | return $@?(0,$@):(1,); | ||
| 296 | } | ||
| 297 | |||
| 298 | } | ||
| 299 | sub add_job { | ||
| 300 | my $self = shift; | ||
| 301 | require Mojo::Weixin::Client::Cron; | ||
| 302 | $self->Mojo::Weixin::Client::Cron::add_job(@_); | ||
| 303 | } | ||
| 304 | |||
| 305 | sub check_pid { | ||
| 306 | my $self = shift; | ||
| 307 | return if not $self->pid_path; | ||
| 308 | eval{ | ||
| 309 | if(not -f $self->pid_path){ | ||
| 310 | $self->spurt($$,$self->pid_path); | ||
| 311 | } | ||
| 312 | else{ | ||
| 313 | my $pid = $self->slurp($self->pid_path); | ||
| 314 | if( $pid=~/^\d+$/ and kill(0, $pid) ){ | ||
| 315 | # my $p; | ||
| 316 | #if($^O eq 'MSWin32' and Win32::Process::Open($p,$pid,0)){ | ||
| 317 | # $self->warn("检测到该账号有其他运行中的客户端(pid:$pid), 请先将其关闭"); | ||
| 318 | # $self->stop(); | ||
| 319 | #} | ||
| 320 | $self->warn("检测到该账号有其他运行中的客户端(pid:$pid), 请先将其关闭"); | ||
| 321 | $self->stop(); | ||
| 322 | } | ||
| 323 | else{ | ||
| 324 | $self->spurt($$,$self->pid_path); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | }; | ||
| 328 | $self->warn("进程检测遇到异常: $@") if $@; | ||
| 329 | |||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | sub clean_pid { | ||
| 334 | my $self = shift; | ||
| 335 | return if not defined $self->pid_path; | ||
| 336 | return if not -f $self->pid_path; | ||
| 337 | $self->info("清除残留的pid文件"); | ||
| 338 | unlink $self->pid_path or $self->warn("删除pid文件[ " . $self->pid_path . " ]失败: $!"); | ||
| 339 | } | ||
| 340 | sub save_state{ | ||
| 341 | my $self = shift; | ||
| 342 | my($previous_state,$current_state) = @_; | ||
| 343 | my @attr = qw( | ||
| 344 | account | ||
| 345 | version | ||
| 346 | start_time | ||
| 347 | http_debug | ||
| 348 | log_encoding | ||
| 349 | log_path | ||
| 350 | log_level | ||
| 351 | log_console | ||
| 352 | disable_color | ||
| 353 | download_media | ||
| 354 | tmpdir | ||
| 355 | media_dir | ||
| 356 | cookie_path | ||
| 357 | qrcode_path | ||
| 358 | pid_path | ||
| 359 | state_path | ||
| 360 | keep_cookie | ||
| 361 | fix_media_loop | ||
| 362 | synccheck_interval | ||
| 363 | emoji_to_text | ||
| 364 | stop_with_mobile | ||
| 365 | ua_retry_times | ||
| 366 | qrcode_count_max | ||
| 367 | state | ||
| 368 | ); | ||
| 369 | # pid | ||
| 370 | # os | ||
| 371 | eval{ | ||
| 372 | my $json = {plugin => []}; | ||
| 373 | for my $attr (@attr){ | ||
| 374 | $json->{$attr} = $self->$attr; | ||
| 375 | } | ||
| 376 | $json->{previous_state} = $previous_state; | ||
| 377 | $json->{pid} = $$; | ||
| 378 | $json->{os} = $^O; | ||
| 379 | for my $p (keys %{ $self->plugins }){ | ||
| 380 | push @{ $json->{plugin} } , { name=>$self->plugins->{$p}{name},priority=>$self->plugins->{$p}{priority},auto_call=>$self->plugins->{$p}{auto_call},call_on_load=>$self->plugins->{$p}{call_on_load} } ; | ||
| 381 | } | ||
| 382 | $self->spurt($self->to_json($json),$self->state_path); | ||
| 383 | }; | ||
| 384 | $self->warn("客户端状态信息保存失败:$@") if $@; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub is_load_plugin { | ||
| 388 | my $self = shift; | ||
| 389 | my $plugin = shift; | ||
| 390 | if(substr($plugin,0,1) eq '+'){ | ||
| 391 | substr($plugin,0,1) = ""; | ||
| 392 | } | ||
| 393 | else{ | ||
| 394 | $plugin = "Mojo::Weixin::Plugin::$plugin"; | ||
| 395 | } | ||
| 396 | return exists $self->plugins->{$plugin}; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub check_controller { | ||
| 400 | my $self = shift; | ||
| 401 | my $once = shift; | ||
| 402 | if($^O ne 'MSWin32' and defined $self->controller_pid ){ | ||
| 403 | if($once){ | ||
| 404 | $self->info("启用Controller[". $self->controller_pid ."]状态检查"); | ||
| 405 | $self->interval(5=>sub{ | ||
| 406 | $self->check_controller(); | ||
| 407 | }); | ||
| 408 | } | ||
| 409 | else{ | ||
| 410 | my $ppid = POSIX::getppid(); | ||
| 411 | if( $ppid=~/^\d+$/ and $ppid == 1 or $ppid != $self->controller_pid ) { | ||
| 412 | $self->warn("检测到脱离Controller进程管理,程序即将终止"); | ||
| 413 | $self->stop(); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | sub check_notice { | ||
| 420 | my $self = shift; | ||
| 421 | return if not $self->is_fetch_notice; | ||
| 422 | $self->info("获取最新公告信息..."); | ||
| 423 | my $notice = $self->http_get($self->notice_api); | ||
| 424 | if($notice){ | ||
| 425 | $self->info("-" x 40); | ||
| 426 | $self->info({content_color=>'green'},$notice); | ||
| 427 | $self->info("-" x 40); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | 1; | ||