Weixin/Plugin/GCM.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 169 CC 20 SCAR 59.9
CC
20
Cov
0%
call
420.0
BEGIN
2.0
CRAP 400.1
1 package Mojo::Weixin::Plugin::GCM;
2 our $AUTHOR = 'sjdy521@163.com';
3 our $SITE = 'http://www.coolapk.com/apk/com.swjtu.gcmformojo';
4 our $DESC = '接收消息通过谷歌提供的GCM接口发送到android手机';
5 our $PRIORITY = 97;
6 use List::Util qw(first);
7 sub call {
8 my $client = shift;
9 my $data = shift;
10 $client->load("UploadQRcode") if !$client->is_load_plugin('UploadQRcode');
11 my $api_url = $data->{api_url} // 'https://gcm-http.googleapis.com/gcm/send';
12 my $api_key = $data->{api_key} or $client->die("[".__PACKAGE__."]必须指定api_key");
13 my $collapse_key = $data->{collapse_key};
14 my $registration_ids = $data->{registration_ids} // [];
15 if(ref $registration_ids ne 'ARRAY' or @{$registration_ids} == 0){
16 $client->die("[".__PACKAGE__."]registration_ids无效");
17 }
18 $client->on(receive_message=>sub{
19 my($client,$msg) = @_;
20 my $type = 'Mojo-Weixin';
21 my $title;
22 my $message;
23 my $msgId;
24 my $senderType;
25 my $isAt = 0;
26 if($msg->is_at) {
27 $isAt=1;
28 }
29 if($msg->type eq 'friend_message'){
30 return if $data->{is_ban_official} and $msg->sender->category eq '公众号';
31 $msgId = $msg->sender->id;
32 $title = $msg->sender->displayname;
33 $message = $msg->content;
34 $senderType = '1';
35 }
36 elsif($msg->type eq 'group_message'){
37 if(!$isAt) {
38 return if ref $data->{ban_group} eq "ARRAY" and @{$data->{ban_group}} and first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->displayname eq $_} @{$data->{ban_group}};
39 return if ref $data->{allow_group} eq "ARRAY" and @{$data->{allow_group}} and !first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->displayname eq $_} @{$data->{allow_group}};
40 }
41 $msgId = $msg->group->id;
42 $title = $msg->group->displayname;
43 $message = $msg->sender->displayname . ": " . $msg->content;
44 $senderType = '2';
45 }
46 return if !$title or !$message;
47 $client->http_post($api_url,
48 {'Authorization'=>"key=$api_key",json=>1},
49 json=>{
50 registration_ids=> $registration_ids,
51 $collapse_key?(collapse_key=> $collapse_key):(),
52 priority=> $data->{priority} // 'high',
53 data=>{isAt=>$isAt,type=>$type,title=>$title,message=>$message,msgId=>$msgId,senderType=>$senderType},
54 },
55 sub{
56 #"{"multicast_id":9016211065189210367,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1484103730761325%9b9e6c13f9fd7ecd"}]}"
57 my $json = shift;
58 if(not defined $json){
59 $client->debug("[".__PACKAGE__."]GCM消息推送失败: 返回结果异常");
60 return;
61 }
62 else{
63 $client->debug("[".__PACKAGE__."]GCM消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}");
64 }
65 }
66 );
67 });
68  
69 $client->on(all_event => sub{
70 my($client,$event,@args) =@_;
71 my $type = 'Mojo-Sys';
72 my $message;
73 my $msgId = 2;
74 my $title;
75 if($event eq 'login'){
76 $message = "登录成功";
77 $title = "登录事件";
78 }
79 elsif($event eq 'input_qrcode'){
80 $message = $client->qrcode_upload_url // '获取二维码url失败';
81 $title = "扫描二维码事件";
82 }
83 elsif($event eq 'stop'){
84 $message = "Mojo-Weixin已停止";
85 $title = "停止事件";
86 }
87 else{return}
88 $client->http_post($api_url,
89 { 'Authorization'=>"key=$api_key",
90 json=>1,
91 blocking=>1,
92 ua_connect_timeout=>5,
93 ua_request_timeout=>5,
94 ua_inactivity_timeout=>5,
95 ua_retry_times=>1
96 },
97 json=>{
98 registration_ids=> $registration_ids,
99 $collapse_key?(collapse_key=> $collapse_key):(),
100 priority=> $data->{priority} // 'high',
101 data=>{type=>$type,title=>$title,message=>$message,msgId=>$msgId},
102 },
103 sub{
104 my $json = shift;
105 if(not defined $json){
106 $client->debug("[".__PACKAGE__."]GCM消息推送失败: 返回结果异常");
107 return;
108 }
109 else{
110 $client->debug("[".__PACKAGE__."]GCM消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}");
111 }
112 }
113 );
114 });
115 }
116 1;