Webqq/Client/Plugin/Msgstat.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 133 CC 18 SCAR 51.2
CC
18
Cov
0%
call
240.0
Report
20.0
BEGIN
2.0
CRAP 167.5
1 package Webqq::Client::Plugin::Msgstat;
2 use Storable qw(retrieve store);
3 use File::Path qw/mkpath/;
4 use Webqq::Client::Util qw(console console_stderr);
5  
6 if($^O !~ /linux/){
7 console_stderr "Webqq::Client::App::Msgstat只能运行在linux系统上\n";
8 exit;
9 }
10 mkpath "/tmp/webqq/data",{mode=>0711};
11  
12 my $msgstat;
13 my $once = 1;
14 $msgstat=(-e "/tmp/webqq/data/msgstat")?retrieve("/tmp/webqq/data/msgstat"):{};
15 sub call{
16 my ($client,$msg,$time,$group_filter) = @_;
17 $time = "17:30" unless defined $time;
18 return 1 if $msg->{type} ne 'group_message';
19 my $group_code = $msg->group_code;
20 my $group_name = $msg->group_name;
21 my $from_nick = $msg->from_nick;
22 my $from_card = $msg->from_card;
23 my $from_qq = $msg->from_qq;
24
25 return 1 unless $group_name;
26 return 1 unless $from_qq;
27  
28 $msgstat->{$group_name}{$from_qq}{nick}=$from_nick;
29 $msgstat->{$group_name}{$from_qq}{card}=$from_card;
30 $msgstat->{$group_name}{$from_qq}{msg}++;
31 $msgstat->{$group_name}{$from_qq}{sys_img}++ if $msg->{content} =~/\[系统表情\]/;
32 $msgstat->{$group_name}{$from_qq}{other_img}++ if $msg->{content} =~/\[图片\]/;
33
34 if($msg->{content} =~ /^-msgstat$/ and $from_qq == 308165330){
35 my $content = Report($msgstat,$group_name);
36 $client->reply_message($msg,$content) if $content;
37 }
38  
39 if($once){
40 $client->{watchers}{rand()} = AE::timer 60,300,sub{
41 console "消息统计数据存盘\n" if $client->{debug};
42 store($msgstat,"/tmp/webqq/data/msgstat");
43 };
44 #my $group_name = "PERL学习交流";
45 $client->add_job("群发言排行榜",$time,sub{
46 for(@{$client->{qq_database}{group_list}}){
47 if(defined $group_filter){
48 next if $_->{name} ne $group_filter;
49 }
50 my $gid = $_->{gid} ;
51 my $content = Report($msgstat,$_->{name});
52 $content = "群发言排行榜:\n" . $content if $content;
53 if($gid and $content){
54 $client->send_group_message(
55 $client->create_group_msg(
56 to_uin=>$gid,
57 content=>$content,
58 group_code=>$_->{code}
59 )
60 );
61 }
62 }
63 });
64  
65 $client->add_job("消息统计数据清空","23:59",sub{
66 $msgstat = {};
67 });
68 $once=0;
69 }
70 return 1;
71 }
72  
73 sub Report{
74 my $msgstat = shift;
75 my $group_name = shift;
76 my $top = shift;
77 $top>0?($top--):($top=10);
78 my $content = "";
79 my @sort_qq =
80 sort {$msgstat->{$group_name}{$b}{other_img}<=>$msgstat->{$group_name}{$a}{other_img} or $msgstat->{$group_name}{$b}{other_img}/$msgstat->{$group_name}{$b}{msg} <=> $msgstat->{$group_name}{$a}{other_img}/$msgstat->{$group_name}{$a}{msg}}
81 grep {$msgstat->{$group_name}{$_}{msg}!=0}
82 keys %{$msgstat->{$group_name}};
83
84 my @top_qq = @sort_qq[0..$top];
85 for(@top_qq){
86 #next if $msgstat->{$group_name}{$_}{other_img} ==0;
87 next if $msgstat->{$group_name}{$_}{msg} ==0;
88 my $nick = $msgstat->{$group_name}{$_}{card}||$msgstat->{$group_name}{$_}{nick};
89 $content .= sprintf("%4s %4s %4s %s\n",
90 $msgstat->{$group_name}{$_}{msg}+0,
91 $msgstat->{$group_name}{$_}{other_img}+0,
92 sprintf("%.1f",($msgstat->{$group_name}{$_}{other_img})*100/$msgstat->{$group_name}{$_}{msg}),
93 $nick,
94 );
95 }
96 $content = sprintf("%4s %4s %4s %s\n","消息","图片","水度","昵称") . $content if $content;
97 return $content;
98 }
99  
100 1;