Webqq/Plugin/MiPush.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 222 CC 27 SCAR 65.9
CC
27
Cov
0%
call
756.0
BEGIN
2.0
CRAP 729.1
1 package Mojo::Webqq::Plugin::MiPush;
2  
3 our $AUTHOR = 'heipidage';
4 our $SITE = 'http://www.coolapk.com/apk/com.swjtu.gcmformojo';
5 our $DESC = '接收消息通过小米推送接口发送到android手机';
6 our $PRIORITY = 97;
7 use List::Util qw(first);
8 sub call {
9 my $client = shift;
10 my $data = shift;
11 $client->load("UploadQRcode") if !$client->is_load_plugin('UploadQRcode');
12 my $api_url = $data->{api_url} // 'https://api.xmpush.xiaomi.com/v2/message/regid';
13 my $api_key = 'hdtYlfMarG6GEObzPg+JNg==';
14 my $pkgname = 'com.swjtu.gcmformojo';
15 my $is_transparent = $data->{is_transparent} // 1;
16 my $qq_package_url = $data->{qq_package_url} // 'intent:#Intent;component=com.tencent.mobileqq/.activity.SplashActivity;end';
17 my $registration_id = $data->{registration_ids} // [];
18 if(ref $registration_id ne 'ARRAY' or @{$registration_id} == 0){
19 $client->die("[".__PACKAGE__."]registration_ids无效");
20 }
21
22
23 $client->on(receive_message=>sub{
24 my($client,$msg) = @_;
25 my $type = 'Mojo-Webqq';
26 my $title;
27 my $message;
28 my $msgId;
29 my $senderType;
30 my $isAt = 0;
31
32 if($msg->is_at) {
33 $isAt=1;
34 }
35 if($msg->type eq 'friend_message'){
36 $msgId = $msg->sender->id;
37 $title = $msg->sender->displayname;
38 $message = $msg->content;
39 $senderType = '1';
40 }
41 elsif($msg->type eq 'group_message'){
42 if(!$isAt) {
43 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}};
44 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}};
45 }
46 $msgId = $msg->group->id;
47 $title = $msg->group->displayname;
48 $message = $msg->sender->displayname . ": " . $msg->content;
49 $senderType = '2';
50 }
51 elsif($msg->type eq 'discuss_message'){
52 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}};
53 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}};
54 $msgId= $msg->discuss->id;
55 $title = $msg->discuss->displayname;
56 $message = $msg->sender->displayname . ": " . $msg->content;
57 $senderType = '3';
58 }
59 elsif($msg->type eq 'sess_message'){
60
61 }
62 return if !$title or !$message;
63
64 if($is_transparent eq 1) {
65 $client->http_post($api_url,
66 {'Authorization'=>"key=$api_key",
67 json=>1
68 },
69 form=>{
70 pass_through => 1,
71 registration_id => $registration_id,
72 restricted_package_namee => $pkgname,
73 payload=>$client->to_json({isAt=>$isAt,type=>$type,title=>$title,message=>$message,msgId=>$msgId,senderType=>$senderType}),
74 },
75 sub{
76 my $json = shift;
77 if(not defined $json){
78 $client->debug("[".__PACKAGE__."]小米消息推送失败: 返回结果异常");
79 return;
80 }
81 else{
82 $client->debug("[".__PACKAGE__."]小米消息推送状态:$json->{error}");
83 }
84 }
85 );
86 } else {
87 $client->http_post($api_url,
88 {'Authorization'=>"key=$api_key",
89 json=>1
90 },
91 form=>{
92 pass_through => 0,
93 registration_id => $registration_id,
94 restricted_package_namee => $pkgname,
95 'extra.notify_effect' => 2,
96 'extra.intent_uri' => $qq_package_url,
97 title => $title,
98 description => $message,
99 payload => $message,
100 },
101 sub{
102 my $json = shift;
103 if(not defined $json){
104 $client->debug("[".__PACKAGE__."]小米消息推送失败: 返回结果异常");
105 return;
106 }
107 else{
108 $client->debug("[".__PACKAGE__."]小米消息推送状态:$json->{error}");
109 }
110 }
111 );
112
113 }
114  
115 });
116  
117 $client->on(all_event => sub{
118 my($client,$event,@args) =@_;
119 my $type = 'Mojo-Sys';
120 my $message;
121 my $msgId = 1;
122 my $title;
123 if($event eq 'login'){
124 $message = "登录成功";
125 $title = "登录事件";
126 }
127 elsif($event eq 'input_qrcode'){
128 $message = $client->qrcode_upload_url // '获取二维码url失败';
129 $title = "扫描二维码事件";
130 }
131 elsif($event eq 'stop'){
132 $message = "Mojo-Webqq已停止";
133 $title = "停止事件";
134 }
135 else{return}
136
137
138 if($is_transparent eq 1) {
139 $client->http_post($api_url,
140 { 'Authorization'=>"key=$api_key",
141 blocking=>1,
142 json=>1,
143 ua_connect_timeout=>5,
144 ua_request_timeout=>5,
145 ua_inactivity_timeout=>5,
146 ua_retry_times=>1
147 },
148 form=>{
149 pass_through => 1,
150 registration_id => $registration_id,
151 restricted_package_namee => $pkgname,
152 payload=>$client->to_json({type=>$type,title=>$title,message=>$message,msgId=>$msgId}),
153 },
154 sub{
155 my $json = shift;
156 if(not defined $json){
157 $client->debug("[".__PACKAGE__."]小米消息推送失败: 返回结果异常");
158 return;
159 }
160 else{
161 $client->debug("[".__PACKAGE__."]小米消息推送状态:$json->{error}");
162 }
163 }
164 );
165 }
166 else {
167 $client->http_post($api_url,
168 { 'Authorization'=>"key=$api_key",
169 blocking=>1,
170 json=>1,
171 ua_connect_timeout=>5,
172 ua_request_timeout=>5,
173 ua_inactivity_timeout=>5,
174 ua_retry_times=>1
175 },
176 form=>{
177 pass_through => 0,
178 registration_id => $registration_id,
179 restricted_package_namee => $pkgname,
180 title => $title,
181 description => $message,
182 payload => $message,
183 },
184 sub{
185 my $json = shift;
186 if(not defined $json){
187 $client->debug("[".__PACKAGE__."]小米消息推送失败: 返回结果异常");
188 return;
189 }
190 else{
191 $client->debug("[".__PACKAGE__."]小米消息推送状态:$json->{error}");
192 }
193 }
194 );
195 }
196 });
197 }
198 1;