Webqq/Plugin/SmartReply.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 261 CC 36 SCAR 70.9
CC
36
Cov
0%
call
1332.0
BEGIN
2.0
BEGIN
2.0
CRAP 1199.0
1 package Mojo::Webqq::Plugin::SmartReply;
2 use POSIX qw(strftime);
3 use Encode;
4 use List::Util qw(first);
5 use Mojo::Util;
6 my $api = 'http://www.tuling123.com/openapi/api';
7 my %ban;
8 my @limit_reply = (
9 "对不起,请不要这么频繁的艾特我",
10 "对不起,您的艾特次数太多",
11 "说这么多话不累么,请休息几分钟",
12 );
13 sub call{
14 my $client = shift;
15 my $data = shift;
16
17 my $notice_reply = ref $data->{notice_reply} eq "ARRAY"?$data->{notice_reply}:\@limit_reply;
18 my $notice_limit = $data->{notice_limit} || 8;
19 my $warn_limit = $data->{warn_limit} || 10;
20 my $ban_limit = $data->{ban_limit} || 12;
21 my $ban_time = $data->{ban_time} || 1200;
22 my $friend_reply = $data->{friend_reply} // 1;
23 my $group_reply = $data->{group_reply} // 1;
24 my $is_need_at = defined $data->{is_need_at}?$data->{is_need_at}:1;
25  
26 my $counter = $client->new_counter(id=>'SmartReply',period=>$data->{period} || 600);
27 $client->on(login=>sub{%ban = ();$counter->reset();});
28 $client->on(receive_message=>sub{
29 my($client,$msg) = @_;
30 return if not $msg->allow_plugin;
31 return if $msg->type !~ /^friend_message|group_message|sess_message$/;
32 return if !$friend_reply and $msg->type eq 'friend_message';
33 return if !$group_reply and $msg->type eq 'group_message';
34 return if exists $ban{$msg->sender->id};
35  
36 my $sender_nick = $msg->sender->displayname;
37 my $user_nick = $msg->receiver->displayname;
38  
39 my $trigger = 0;
40  
41 if($msg->type eq 'group_message'){
42 return if ref $data->{ban_group} eq "ARRAY" and first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->name eq $_} @{$data->{ban_group}};
43 return if ref $data->{allow_group} eq "ARRAY" and !first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->name eq $_} @{$data->{allow_group}};
44 return if ref $data->{ban_user} eq "ARRAY" and first {$_=~/^\d+$/?$msg->sender->uid eq $_:$sender_nick eq $_} @{$data->{ban_user}};
45 $trigger = 1 if $data->{is_need_at} and $msg->type eq "group_message" and $msg->is_at;
46 }
47 else{
48 return if ref $data->{ban_user} eq "ARRAY" and first {$_=~/^\d+$/?$msg->sender->uid eq $_:$sender_nick eq $_} @{$data->{ban_user}};
49 }
50  
51 if(ref $data->{keyword} eq "ARRAY" and @{$data->{keyword}} > 0){
52 $trigger = 1 if first { $msg->content =~ /\Q$_\E/} @{$data->{keyword}};
53 }
54  
55 return if not $trigger;
56  
57 my $id = ($msg->type eq 'group_message'?$msg->group->id : 'placeholder') . "|" .$msg->sender->id;
58 my $limit = $counter->check( $id );
59 if($limit >= $ban_limit){
60 $ban{$msg->sender->id} = 1;
61 $client->reply_message($msg,"\@$sender_nick " . "您已被列入黑名单,$ban_time秒内提问无视",sub{$_[1]->msg_from("bot")});
62 $counter->clear( $id );
63 $client->timer($ban_time ,sub{delete $ban{$msg->sender->id};});
64 return;
65 }
66 if($limit >= $warn_limit){
67 $client->reply_message($msg,"\@$sender_nick " . "警告,您艾特过于频繁,即将被列入黑名单,请克制",sub{$_[1]->from("bot")});
68 return;
69 }
70 if($limit >= $notice_limit){
71 $client->reply_message($msg,"\@$sender_nick " . $limit_reply->[int rand(@$limit_reply)],sub{$_[1]->from("bot")});
72 return;
73 }
74  
75 $msg->allow_plugin(0);
76  
77 my $input = $msg->content;
78 $input=~s/\@\Q$user_nick\E ?|\[[^\[\]]+\]\x01|\[[^\[\]]+\]//g;
79 return unless $input;
80  
81 my $json = {
82 "key" => $data->{apikey} || "f771372ffd054183bfcdf260d7c7ad5a",
83 "userid" => $msg->sender->id,
84 "info" => $input,
85 };
86  
87 $json->{"loc"} = $msg->sender->city if $msg->type eq "group_message" and $msg->sender->city;
88 $client->http_post($api,{json=>1},json=>$json,sub{
89 my $json = shift;
90 return unless defined $json;
91 return if $json->{code}=~/^4000[1-7]$/;
92 my $reply;
93 if($json->{code} == 100000){
94 return unless $json->{text};
95 $reply = $json->{text};
96 }
97 elsif($json->{code} == 200000){
98 $reply = "$json->{text}$json->{url}";
99 }
100 else{return}
101 $reply=~s#<br(\s*/)?>#\n#g;
102 eval{$reply= Mojo::Util::html_unescape($reply);};
103 $client->warn("html entities unescape fail: $@") if $@;
104 $reply = "\@$sender_nick " . $reply if $msg->type eq 'group_message' and rand(100)>20;
105 $reply = $client->truncate($reply,max_bytes=>500,max_lines=>8) if ($msg->type eq 'group_message' and $data->{is_truncate_reply});
106 $client->reply_message($msg,$reply,sub{$_[1]->from("bot")}) if $reply;
107 });
108  
109 });
110 }
111 1;