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