Webqq/Client/Plugin/Perldoc.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 140 CC 18 SCAR 57.3
CC
18
Cov
0%
call
342.0
BEGIN
2.0
BEGIN
2.0
CRAP 308.0
1 package Webqq::Client::Plugin::Perldoc;
2 use JSON;
3 use Webqq::Client::Util qw(console_stderr truncate);
4 if($^O !~ /linux/){
5 console_stderr "Webqq::Client::App::Perldoc只能运行在linux系统上\n";
6 exit;
7 }
8 chomp(my $PERLDOC_COMMAND = `/bin/env which perldoc`);
9  
10 my %last_module_time ;
11  
12 sub call{
13 my $client = shift;
14 my $msg = shift;
15 return 1 if time - $msg->{msg_time} > 10;
16 my $perldoc_path = shift;
17 $PERLDOC_COMMAND = $perldoc_path if defined $perldoc_path;
18 if($msg->{content} =~/perldoc\s+-(v|f)\s+([^ ]+)/){
19 $msg->{allow_plugin} = 0;
20 my ($p,$v) = ($1,$2);
21 my $doc = '';
22 my $command;
23 if($v eq q{$'}){
24 $command = qq{$PERLDOC_COMMAND -Tt -$p "$v" 2>&1|};
25 }
26 else{
27 $command = qq{$PERLDOC_COMMAND -Tt -$p '$v' 2>&1|};
28 }
29 open PERLDOC,$command or $doc = '@灰灰 run perldoc fail';
30 while(<PERLDOC>){
31 last if $.>10;
32 $doc .= $_;
33 }
34 close PERLDOC;
35 $doc=~s/\n*$/...\n/;
36 if($p eq 'f'){
37 if($doc=~/^No documentation for perl function/){
38 $doc .= "http://perldoc.perl.org/index-functions.html";
39 }
40 else{
41 $doc .= "See More: http://perldoc.perl.org/functions/$v.html";
42 }
43 }
44 elsif($p eq 'v'){
45 $doc .= "See More: http://perldoc.perl.org/perlvar.html";
46 }
47  
48 $client->reply_message($msg,$doc) if $doc;
49 return 0;
50 }
51  
52 elsif($msg->{content} =~ /perldoc\s+((\w+::)*\w+)/ or $msg->{content} =~ /((\w+::)+\w+)/){
53 $msg->{allow_plugin} = 0;
54 my $module = $1;
55 my $is_perldoc = $msg->{content}=~/perldoc/;
56 if(!$is_perldoc and exists $last_module_time{$msg->{type}}{$msg->{from_uin}}{$module} and time - $last_module_time{$msg->{type}}{$msg->{from_uin}}{$module} < 1800){
57 return 0;
58 }
59 my $metacpan_module_api = 'http://api.metacpan.org/v0/module/';
60 my $metacpan_pod_api = 'http://api.metacpan.org/v0/pod/';
61  
62 my $cache = $client->{cache_for_metacpan}->retrieve($module);
63 if(defined $cache){
64 $client->reply_message($msg,$cache->{doc});
65 $last_module_time{$msg->{type}}{$msg->{from_uin}}{$module} = time;
66 return 0;
67 }
68 $client->{asyn_ua}->get($metacpan_module_api . $module,(),sub{
69 my $response = shift;
70 my $doc;
71 my $json;
72 my $code;
73 if($client->{debug}){
74 print "GET " . $metacpan_module_api . $module,"\n";
75 #print $response->content;
76 }
77 eval{ $json = JSON->new->utf8->decode($response->content);};
78 unless($@){
79 if($json->{code} == 404){
80 return 0;
81 #$doc = "模块名称: $module ($json->{message})" ;
82 #$code = 404;
83  
84 #$client->{cache_for_metacpan}->store($module,{code=>$code,doc=>$doc},604800);
85 #$client->reply_message($msg,$doc) ;
86 #$last_module_time{$msg->{type}}{$msg->{from_uin}}{$module} = time;
87 }
88 else{
89 $code = 200;
90 my $author = $json->{author};
91 my $version = $json->{version};
92 #my $date = $json->{date};
93 my $abstract= $json->{abstract};
94 my $podlink = 'https://metacpan.org/pod/' . $module;
95 $doc =
96 "模块: $module\n" .
97 "版本: $version\n" .
98 "作者: $author\n" .
99 "简述: $abstract\n" .
100 "链接: $podlink\n"
101 ;
102 print "GET " . $metacpan_pod_api . $module,"\n" if $client->{debug};
103 $client->{asyn_ua}->get($metacpan_pod_api . $module,(Accept=>"text/plain"),sub{
104 my $res = shift;
105 my ($SYNOPSIS) = $res->content()=~/^SYNOPSIS$(.*?)^[A-Za-z]+$/ms;
106 if($SYNOPSIS){
107 $doc .= "用法概要: $SYNOPSIS\n" ;
108 $doc=~s/\n+$//;
109 $doc = truncate($doc,max_bytes=>1000,max_lines=>30);
110 }
111 $client->{cache_for_metacpan}->store($module,{code=>$code,doc=>$doc},604800);
112 $client->reply_message($msg,$doc);
113 $last_module_time{$msg->{type}}{$msg->{from_uin}}{$module} = time;
114 });
115 }
116 }
117 });
118
119 return 0;
120 }
121  
122 return 1;
123 }
124  
125 1;