Webqq/Plugin/GCM.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 228 CC 27 SCAR 65.9
CC
27
Cov
0%
call
756.0
BEGIN
2.0
CRAP 729.1
1 package Mojo::Webqq::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-Webqq';
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 $msgId = $msg->sender->id;
31 $title = $msg->sender->displayname;
32 $message = $msg->content;
33 $senderType = '1';
34 }
35 elsif($msg->type eq 'group_message'){
36 if(!$isAt){
37 if(ref $data->{allow_group_member} eq "ARRAY"){
38 my $id1 = $msg->sender->displayname . "|" . $msg->group->displayname;
39 my $id2 = $msg->sender->uid . "|" . $msg->group->displayname;
40 my $id3 = $msg->sender->displayname . "|" . $msg->group->uid;
41 my $id4 = $msg->sender->uid . "|" . $msg->group->uid;
42 return if !first {$id1 eq $_ or $id2 eq $_ or $id3 eq $_ or $id4 eq $_} @{$data->{allow_group_member}};
43 }
44 else{
45 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}};
46 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}};
47 }
48 }
49 $msgId = $msg->group->id;
50 $title = $msg->group->displayname;
51 $message = $msg->sender->displayname . ": " . $msg->content;
52 $senderType = '2';
53 }
54 elsif($msg->type eq 'discuss_message'){
55 return if ref $data->{ban_discuss} eq "ARRAY" and @{$data->{ban_discuss}} and first {$_=~/^\d+$/?$msg->discuss->uid eq $_:$msg->discuss->displayname eq $_} @{$data->{ban_discuss}};
56 return if ref $data->{allow_discuss} eq "ARRAY" and @{$data->{allow_discuss}} and !first {$_=~/^\d+$/?$msg->discuss->uid eq $_:$msg->discuss->displayname eq $_} @{$data->{allow_discuss}};
57 $msgId= $msg->discuss->id;
58 $title = $msg->discuss->displayname;
59 $message = $msg->sender->displayname . ": " . $msg->content;
60 $senderType = '3';
61 }
62 elsif($msg->type eq 'sess_message'){
63
64 }
65 return if !$title or !$message;
66 $client->http_post($api_url,
67 {'Authorization'=>"key=$api_key",json=>1},
68 json=>{
69 registration_ids=> $registration_ids,
70 $collapse_key?(collapse_key=> $collapse_key):(),
71 priority=> $data->{priority} // 'high',
72 data=>{isAt=>$isAt,type=>$type,title=>$title,message=>$message,msgId=>$msgId,senderType=>$senderType},
73 },
74 sub{
75 #"{"multicast_id":9016211065189210367,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1484103730761325%9b9e6c13f9fd7ecd"}]}"
76 my $json = shift;
77 if(not defined $json){
78 $client->debug("[".__PACKAGE__."]GCM消息推送失败: 返回结果异常");
79 return;
80 }
81 else{
82 $client->debug("[".__PACKAGE__."]GCM消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}");
83 }
84 }
85 );
86 });
87  
88 $client->on(all_event => sub{
89 my($client,$event,@args) =@_;
90 my $type = 'Mojo-Sys';
91 my $message;
92 my $msgId = 1;
93 my $title;
94 if($event eq 'login'){
95 $message = "登录成功";
96 $title = "登录事件";
97 }
98 elsif($event eq 'input_qrcode'){
99 $message = $client->qrcode_upload_url // '获取二维码url失败';
100 $title = "扫描二维码事件";
101 }
102 elsif($event eq 'stop'){
103 $message = "Mojo-Webqq已停止";
104 $title = "停止事件";
105 }
106 else{return}
107 $client->http_post($api_url,
108 { 'Authorization'=>"key=$api_key",
109 json=>1,
110 blocking=>1,
111 ua_connect_timeout=>5,
112 ua_request_timeout=>5,
113 ua_inactivity_timeout=>5,
114 ua_retry_times=>1
115 },
116 json=>{
117 registration_ids=> $registration_ids,
118 $collapse_key?(collapse_key=> $collapse_key):(),
119 priority=> $data->{priority} // 'high',
120 data=>{type=>$type,title=>$title,message=>$message,msgId=>$msgId},
121 },
122 sub{
123 my $json = shift;
124 if(not defined $json){
125 $client->debug("[".__PACKAGE__."]GCM消息推送失败: 返回结果异常");
126 return;
127 }
128 else{
129 $client->debug("[".__PACKAGE__."]GCM消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}");
130 }
131 }
132 );
133 });
134 }
135 1;