| 1 | package Plack::App::Gruntmaster; |
| 2 | |
| 3 | use 5.014000; |
| 4 | use strict; |
| 5 | our $VERSION = '5999.000_001'; |
| 6 | |
| 7 | use Encode qw/encode decode/; |
| 8 | use File::Slurp qw/read_file/; |
| 9 | use JSON::MaybeXS qw/encode_json/; |
| 10 | use PerlX::Maybe; |
| 11 | use Scope::Upper qw/unwind SUB UP/; |
| 12 | use Web::Simple; |
| 13 | |
| 14 | use Gruntmaster::Data; |
| 15 | use Plack::App::Gruntmaster::HTML; |
| 16 | |
| 17 | use warnings NONFATAL => 'all'; |
| 18 | no warnings 'illegalproto'; |
| 19 | |
| 20 | ################################################## |
| 21 | |
| 22 | use constant USER_REGEX => qr/^\w{2,20}$/a; |
| 23 | |
| 24 | use constant FORMAT_EXTENSION => { |
| 25 | C => 'c', |
| 26 | CPP => 'cpp', |
| 27 | GCCGO => 'go', |
| 28 | GOLANG => 'go', |
| 29 | GOLFSCRIPT => 'gs', |
| 30 | HASKELL => 'hs', |
| 31 | MONO => 'cs', |
| 32 | JAVA => 'java', |
| 33 | PASCAL => 'pas', |
| 34 | PERL => 'pl', |
| 35 | PYTHON => 'py', |
| 36 | RUBY => 'rb', |
| 37 | SBCL => 'l', |
| 38 | }; |
| 39 | |
| 40 | use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']]; |
| 41 | |
| 42 | my ($env, $privacy); |
| 43 | |
| 44 | sub remote_user { |
| 45 | unless ($env->{'gruntmaster.user'}) { |
| 46 | my $user = $env->{REMOTE_USER}; |
| 47 | $user &&= user_entry $user; |
| 48 | $env->{'gruntmaster.user'} = $user; |
| 49 | } |
| 50 | $env->{'gruntmaster.user'} |
| 51 | } |
| 52 | |
| 53 | sub admin { remote_user && remote_user->{admin} } |
| 54 | |
| 55 | sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] } |
| 56 | sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] } |
| 57 | sub response { |
| 58 | my ($template, $title, $params, $maxage) = @_; |
| 59 | unless ($params) { |
| 60 | $params = $title; |
| 61 | $title = 'No title'; |
| 62 | } |
| 63 | $params->{time} = time; |
| 64 | $params->{args} = {%_}; |
| 65 | bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response' |
| 66 | } |
| 67 | |
| 68 | sub forbid { |
| 69 | my ($condition) = @_; |
| 70 | $privacy = 'private' if $condition; |
| 71 | return if !$condition || admin; |
| 72 | unwind $env->{authcomplex}->unauthorized, SUB UP |
| 73 | } |
| 74 | |
| 75 | sub dispatch_request{ |
| 76 | $env = $_[PSGI_ENV]; |
| 77 | $privacy = 'public'; |
| 78 | |
| 79 | sub (GET) { |
| 80 | sub (/robots.txt) { NOT_FOUND }, |
| 81 | |
| 82 | sub (/src/:job) { |
| 83 | my $job = job_full $_{job}; |
| 84 | return NOT_FOUND if !$job; |
| 85 | my $isowner = remote_user && remote_user->{id} eq $job->{owner}; |
| 86 | my $contest = $job->{contest} && contest_entry $job->{contest}; |
| 87 | my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished}); |
| 88 | forbid !$isowner && $private; |
| 89 | my $privacy = $private ? 'private' : 'public'; |
| 90 | my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain'); |
| 91 | push @headers, (Vary => 'Authorization') if $private; |
| 92 | [200, \@headers, [$job->{source}]] |
| 93 | }, |
| 94 | |
| 95 | sub (?:format~) { |
| 96 | my $format = lc ($_{format} // ''); |
| 97 | response_filter { |
| 98 | my ($r) = @_; |
| 99 | return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; |
| 100 | my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}"); |
| 101 | push @hdrs, Vary => 'Authorization' if $privacy eq 'private'; |
| 102 | return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; |
| 103 | my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; |
| 104 | [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]] |
| 105 | }, |
| 106 | }, |
| 107 | |
| 108 | sub (/st/:contest) { response st => 'Standings', standings($_{contest}), 10 }, |
| 109 | |
| 110 | sub (/ed/:contest) { |
| 111 | my $contest = contest_full $_{contest}; |
| 112 | forbid !$contest->{finished}; |
| 113 | my $pblist = problem_list contest => $_{contest}, solution => 1; |
| 114 | response ed => 'Editorial of ' . $contest->{name}, {%$pblist, editorial => $contest->{editorial}} |
| 115 | }, |
| 116 | |
| 117 | sub (/login) { |
| 118 | forbid !remote_user; |
| 119 | [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]] |
| 120 | }, |
| 121 | |
| 122 | sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, |
| 123 | |
| 124 | sub (/us/) { response us => 'Users', {us => user_list} }, |
| 125 | sub (/ct/ + ?:owner~) { response ct => 'Contests', contest_list(%_), 300 }, |
| 126 | sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) { |
| 127 | forbid $_{private}; |
| 128 | response log => 'Job list', job_list(%_), 5 |
| 129 | }, |
| 130 | sub (/pb/ + ?:owner~&:contest~&:private~) { |
| 131 | forbid $_{private}; |
| 132 | my $pending = $_{contest} && !contest_entry($_{contest})->{started}; |
| 133 | forbid $pending; |
| 134 | response pb => 'Problems', problem_list %_ |
| 135 | }, |
| 136 | |
| 137 | sub (/us/:user) { |
| 138 | my $user = user_entry $_{user}; |
| 139 | response us_entry => $user->{name}, $user |
| 140 | }, |
| 141 | sub (/ct/:contest) { |
| 142 | my $contest = contest_entry $_{contest}; |
| 143 | response ct_entry => $contest->{name}, $contest, 60 |
| 144 | }, |
| 145 | sub (/log/:job) { |
| 146 | my $job = job_entry $_{job}; |
| 147 | forbid $job->{private}; |
| 148 | response log_entry => "Job $_{job}", $job, 10 |
| 149 | }, |
| 150 | sub (/pb/:problem + ?contest~) { |
| 151 | my (undef, undef, $contest) = @_; |
| 152 | $_{contest} = $contest; |
| 153 | $contest = $contest && contest_entry $_{contest}; |
| 154 | return NOT_FOUND if $contest && !contest_has_problem $_{contest}, $_{problem}; |
| 155 | my $problem = problem_entry $_{problem}, $_{contest}; |
| 156 | forbid $problem->{private} && !$contest; |
| 157 | if ($contest) { |
| 158 | return redirect "/pb/$_{problem}" if !$contest->{started} || $contest->{finished}; |
| 159 | forbid !remote_user; |
| 160 | $privacy = 'private'; |
| 161 | } |
| 162 | response pb_entry => $problem->{name}, $problem, $_{contest} ? 10 : (); |
| 163 | }, |
| 164 | sub (/sol/:problem) { |
| 165 | my $problem = problem_entry $_{problem}; |
| 166 | forbid $problem->{private}; |
| 167 | response sol => 'Solution of ' . $problem->{name}, {solution => $problem->{solution}}; |
| 168 | }, |
| 169 | |
| 170 | sub (/) { redispatch_to '/index' }, |
| 171 | sub (/favicon.ico) { redirect '/static/favicon.ico' }, |
| 172 | sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] } |
| 173 | }, |
| 174 | |
| 175 | sub (POST) { |
| 176 | sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) { |
| 177 | my (undef, undef, $prog) = @_; |
| 178 | forbid !remote_user; |
| 179 | my $problem = problem_entry $_{problem}; |
| 180 | my $private = $problem->{private} ? 1 : 0; |
| 181 | if ($_{contest}) { |
| 182 | $private = 0; |
| 183 | my $contest = contest_entry $_{contest}; |
| 184 | return reply 'This contest has not yet started' if !$contest->{started}; |
| 185 | return reply 'This contest has finished' if $contest->{finished}; |
| 186 | return reply 'This problem is private' if !admin && $private; |
| 187 | return reply 'This problem does not belong to this contest' unless contest_has_problem $_{contest}, $_{problem}; |
| 188 | } |
| 189 | return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024; |
| 190 | return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30; |
| 191 | |
| 192 | my $source = $prog ? read_file $prog->path : $_{source_code}; |
| 193 | unlink $prog->path if $prog; |
| 194 | my $id = create_job( |
| 195 | maybe contest => $_{contest}, |
| 196 | private => $private, |
| 197 | date => time, |
| 198 | extension => FORMAT_EXTENSION->{$_{prog_format}}, |
| 199 | format => $_{prog_format}, |
| 200 | problem => $_{problem}, |
| 201 | source => $source, |
| 202 | owner => remote_user->{id}, |
| 203 | ); |
| 204 | |
| 205 | [303, [Location => '/log/' . $id], []] |
| 206 | }, |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | 1; |
| 212 | __END__ |