Webqq/Plugin/PostImgVerifycode.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 107 CC 8 SCAR 32.6
CC
8
Cov
0%
call
56.0
BEGIN
6.0
BEGIN
2.0
CRAP 26.1
1 package Mojo::Webqq::Plugin::PostImgVerifycode;
2 our $PRIORITY = 0;
3 our $CALL_ON_LOAD = 1;
4 use MIME::Base64;
5 BEGIN{
6 our $has_mime_lite = 0;
7 eval{require MIME::Lite;};
8 $has_mime_lite = 1 if not $@;
9 }
10 sub call {
11 my $client = shift;
12 my $data = shift;
13 $client->die("插件[". __PACKAGE__ ."]依赖模块 MIME::Lite,请先确认该模块已经正确安装") if not $has_mime_lite;
14 $client->on(input_img_verifycode=>sub{
15 my($client,$filename) = @_;
16 $client->die("插件[".__PACKAGE__."]必须设置提交验证码本机地址") unless defined $data->{post_host};
17 $data->{post_port} = "3000" unless defined $data->{post_port};
18 my $subject = $data->{subject} || "QQ帐号 " . $client->uid . " 登录验证码";
19 my $mime = MIME::Lite->new(
20 Type => 'multipart/mixed',
21 From => $data->{from},
22 To => $data->{to},
23 );
24 $mime->add("Subject"=>"=?UTF-8?B?" . MIME::Base64::encode_base64($subject,"") . "?=");
25 $mime->attach(
26 Type =>'TEXT',
27 Data =>"请点击以下链接输入验证码: http://$data->{post_host}:$data->{post_port}/check_code"
28 );
29 $mime->attach(
30 Path => $filename,
31 Disposition => 'attachment',
32 Type => 'image/jpeg',
33 );
34 my ($is_success,$err) = $client->mail(
35 smtp=>$data->{smtp},
36 port=>$data->{port},
37 user=>$data->{user},
38 pass=>$data->{pass},
39 from=>$data->{from},
40 to =>$data->{to},
41 subject=>$subject,
42 data=>$mime->as_string,
43 );
44 if(not $is_success){
45 $client->error("插件[".__PACKAGE__."]邮件发送失败: $err");
46 return;
47 }
48 package Mojo::Webqq::Plugin::PostImgVerifycode::App;
49 use Encode;
50 use Mojolicious::Lite;
51 use File::Basename qw(basename);
52 my $img_path = basename($filename);
53 my $img_data = '';
54 open my $img_handle,$filename or die $!;
55 while((read $img_handle,my $buf,4096)!=0){
56 $img_data .= $buf;
57 }
58 close $img_handle;
59 get '/check_code' => sub{
60 my $template = <<"TEMPLATE";
61 <html>
62 <head>
63 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
64 </head>
65 <body>
66 <form action="/post_code" method="get">
67 <div><img src="/$img_path"></img></div>
68 <nobr>验证码:</nobr>
69 <input type="text" maxlength="4" size="4" name="code"></input>
70 <input type="submit"></input>
71 </form>
72 </body>
73 </html>
74 TEMPLATE
75 $_[0]->render(text => $template);
76 };
77 get "/$img_path" => sub{$_[0]->render(data=>$img_data,format=>'image/jpg')};
78 get '/post_code' => sub{
79 my $code=$_[0]->param("code") || ""; $client->verifycode($code) if defined $code;
80 $_[0]->render(text => "您的验证码已经提交: $code");
81 $client->debug(encode("utf8","插件[Mojo::Webqq::Plugin::PostImgVerifycode]获取到登录验证码为: $code"));
82 };
83 package Mojo::Webqq::Plugin::PostImgVerifycode;
84 use Mojo::IOLoop;
85 use Mojo::Webqq::Server;
86 my $server = Mojo::Webqq::Server->new(ioloop=>Mojo::IOLoop->new);
87 $server->app($server->build_app("Mojo::Webqq::Plugin::PostImgVerifycode::App"));
88 $server->app->secrets("hello world");
89 $server->app->log($client->log);
90 $server->app->hook(after_render => sub {
91 my ($c, $output, $format) = @_;
92 if($c->req->url->path eq '/post_code'){
93 $server->stop;
94 $server->ioloop->stop;
95 undef $server;
96 }
97 });
98 #$server->listen([{host=>$data->{post_host},port=>$data->{post_port}}]);
99 $server->listen(["http://$data->{post_host}:$data->{post_port}" ,]);
100 $server->run;
101 });
102 }
103 1;