| 1 | package Mojo::Weixin::Plugin::Perldoc; | ||
| 2 | our $PRIORITY = 96; | ||
| 3 | use Pod::Perldoc; | ||
| 4 | use Term::ANSIColor; | ||
| 5 | use Mojo::Weixin::Cache; | ||
| 6 | my $metacpan_module_api = 'http://api.metacpan.org/v0/module/'; | ||
| 7 | my $metacpan_pod_api = 'http://api.metacpan.org/v0/pod/'; | ||
| 8 | my $metacpan_cache = Mojo::Weixin::Cache->new; | ||
| 9 | sub call{ | ||
| 10 | my $client = shift; | ||
| 11 | my $data = shift; | ||
| 12 | my $callback = sub{ | ||
| 13 | my($client,$msg)=@_; | ||
| 14 | return if not $msg->allow_plugin; | ||
| 15 | if($msg->content =~ /perldoc\s+-(v|f)\s+([^ ]+)/){ | ||
| 16 | $msg->allow_plugin(0); | ||
| 17 | return if $msg->from eq "bot"; | ||
| 18 | my($p,$v) = ("-$1",$2); | ||
| 19 | $client->spawn( | ||
| 20 | cmd =>sub{ | ||
| 21 | local @ARGV=($p,$v); | ||
| 22 | require 5; | ||
| 23 | exit(Pod::Perldoc->run()); | ||
| 24 | }, | ||
| 25 | exec_timeout => 5, | ||
| 26 | exit_cb => sub { | ||
| 27 | my($pid,$res)=@_; | ||
| 28 | my $reply; | ||
| 29 | if($res->{exit_status}==0){ | ||
| 30 | $reply = $client->truncate($res->{stdout},max_lines=>10,max_bytes=>2000); | ||
| 31 | $reply .= "\n查看更多内容: http://perldoc.perl.org/functions/$v.html" if $p eq "-f"; | ||
| 32 | $reply .= "\n查看更多内容: http://perldoc.perl.org/perlvar.html" if $p eq "-v"; | ||
| 33 | } | ||
| 34 | elsif($res->{stderr}=~/exec of coderef failed: (.+?)\s*at /){ | ||
| 35 | $reply = $1; | ||
| 36 | $reply .= "\n查看更多内容: http://perldoc.perl.org/index-functions.html" if $p eq "-f"; | ||
| 37 | $reply .= "\n查看更多内容: http://perldoc.perl.org/perlvar.html" if $p eq "-v"; | ||
| 38 | } | ||
| 39 | eval{$reply = Term::ANSIColor::colorstrip($reply);}; | ||
| 40 | $client->reply_message($msg,$reply,sub{$_[1]->from("bot")}) if $reply; | ||
| 41 | }, | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | elsif($msg->content =~ /perldoc\s+((\w+::)*\w+)/){ | ||
| 45 | $msg->allow_plugin(0); | ||
| 46 | return if $msg->class eq "send" and $msg->from ne "api" and $msg->from ne "irc"; | ||
| 47 | my $module = $1; | ||
| 48 | my $cache = $metacpan_cache->retrieve($module); | ||
| 49 | if(defined $cache){ | ||
| 50 | $client->reply_message($msg,$cache->{doc}); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | $client->http_get($metacpan_module_api . $module,{json=>1},sub{ | ||
| 54 | my $json = shift; | ||
| 55 | return unless defined $json; | ||
| 56 | my $doc; | ||
| 57 | my $code; | ||
| 58 | if(defined $json->{code} and $json->{code} == 404){ | ||
| 59 | $doc = "模块名称: $module ($json->{message})" ; | ||
| 60 | $code = 404; | ||
| 61 | $metacpan_cache->store($module,{code=>$code,doc=>$doc},604800); | ||
| 62 | $client->reply_message($msg,$doc); | ||
| 63 | } | ||
| 64 | else{ | ||
| 65 | $code = 200; | ||
| 66 | my $author = $json->{author}; | ||
| 67 | my $version = $json->{version}; | ||
| 68 | my $abstract= $json->{abstract}; | ||
| 69 | my $podlink = 'https://metacpan.org/pod/' . $module; | ||
| 70 | $doc = | ||
| 71 | "模块: $module\n" . | ||
| 72 | "版本: $version\n" . | ||
| 73 | "作者: $author\n" . | ||
| 74 | "简述: $abstract\n" . | ||
| 75 | "链接: $podlink\n" | ||
| 76 | ; | ||
| 77 | $client->http_get($metacpan_pod_api . $module,{Accept=>"text/plain"},sub{ | ||
| 78 | my $data = shift; | ||
| 79 | return unless defined $data; | ||
| 80 | my ($SYNOPSIS) = $data=~/^SYNOPSIS$(.*?)^[A-Za-z]+$/ms; | ||
| 81 | if($SYNOPSIS){ | ||
| 82 | $doc .= "用法概要: $SYNOPSIS\n" ; | ||
| 83 | $doc=~s/\n+$//; | ||
| 84 | $doc = $client->truncate($doc,max_bytes=>1000,max_lines=>30); | ||
| 85 | } | ||
| 86 | $metacpan_cache->store($module,{code=>$code,doc=>$doc},604800); | ||
| 87 | $client->reply_message($msg,$doc,sub{$_[1]->from("bot")}); | ||
| 88 | }); | ||
| 89 | } | ||
| 90 | }); | ||
| 91 | } | ||
| 92 | }; | ||
| 93 | $client->on(receive_message=>$callback); | ||
| 94 | $client->on(send_message=>$callback); | ||
| 95 | } | ||
| 96 | 1; |