]>
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', | |
a5e355d6 MG |
27 | GCCGO => 'go', |
28 | GOLANG => 'go', | |
d39978bf | 29 | GOLFSCRIPT => 'gs', |
a5e355d6 | 30 | HASKELL => 'hs', |
594d53ba MG |
31 | MONO => 'cs', |
32 | JAVA => 'java', | |
33 | PASCAL => 'pas', | |
34 | PERL => 'pl', | |
35 | PYTHON => 'py', | |
c83dca87 | 36 | RUBY => 'rb', |
e6d1bcd4 | 37 | SBCL => 'l', |
594d53ba MG |
38 | }; |
39 | ||
3ef32174 | 40 | use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']]; |
594d53ba | 41 | |
c039d63e | 42 | my ($env, $privacy); |
594d53ba MG |
43 | |
44 | sub db { $env->{'gruntmaster.dbic'} } | |
45 | ||
46 | sub remote_user { | |
2a06b02c MG |
47 | unless ($env->{'gruntmaster.user'}) { |
48 | my $user = $env->{REMOTE_USER}; | |
49 | $user &&= db->user_entry($user); | |
50 | $env->{'gruntmaster.user'} = $user; | |
51 | } | |
52 | $env->{'gruntmaster.user'} | |
594d53ba MG |
53 | } |
54 | ||
c5343878 | 55 | sub admin { remote_user && remote_user->{admin} } |
594d53ba MG |
56 | sub contest { db->contest ($_{contest}) } |
57 | sub problem { db->problem ($_{problem}) } | |
594d53ba | 58 | |
54bc2c5c | 59 | sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] } |
69c01de9 | 60 | sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] } |
594d53ba | 61 | sub response { |
c039d63e | 62 | my ($template, $title, $params, $maxage) = @_; |
594d53ba MG |
63 | unless ($params) { |
64 | $params = $title; | |
65 | $title = 'No title'; | |
66 | } | |
9a4806b3 | 67 | $params->{time} = time; |
39e2d01a | 68 | $params->{args} = {%_}; |
54bc2c5c | 69 | bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response' |
594d53ba | 70 | } |
3b69df7a | 71 | |
c039d63e MG |
72 | sub forbid { |
73 | my ($condition) = @_; | |
74 | $privacy = 'private' if $condition; | |
75 | return if !$condition || admin; | |
8d725691 | 76 | unwind $env->{authcomplex}->unauthorized, SUB UP |
594d53ba | 77 | } |
31d70015 | 78 | |
594d53ba MG |
79 | sub dispatch_request{ |
80 | $env = $_[PSGI_ENV]; | |
c039d63e MG |
81 | $privacy = 'public'; |
82 | ||
594d53ba | 83 | sub (GET) { |
3ef32174 | 84 | sub (/robots.txt) { NOT_FOUND }, |
3ef32174 | 85 | |
594d53ba | 86 | sub (/src/:job) { |
02d17b02 | 87 | my $job = db->job_full($_{job}); |
c5343878 MG |
88 | return NOT_FOUND if !$job; |
89 | my $isowner = remote_user && remote_user->{id} eq $job->{owner}; | |
02d17b02 MG |
90 | my $contest = $job->{contest} && db->contest_entry($job->{contest}); |
91 | my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished}); | |
e547b147 MG |
92 | forbid !$isowner && $private; |
93 | my $privacy = $private ? 'private' : 'public'; | |
218d6f7e | 94 | my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain'); |
e547b147 | 95 | push @headers, (Vary => 'Authorization') if $private; |
02d17b02 | 96 | [200, \@headers, [$job->{source}]] |
594d53ba MG |
97 | }, |
98 | ||
d3892d73 | 99 | sub (?:format~) { |
feaa8f5a | 100 | my $format = lc ($_{format} // ''); |
594d53ba MG |
101 | response_filter { |
102 | my ($r) = @_; | |
103 | return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; | |
928a611f MG |
104 | my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}"); |
105 | push @hdrs, Vary => 'Authorization' if $privacy eq 'private'; | |
69c01de9 | 106 | return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; |
928a611f | 107 | my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; |
69c01de9 | 108 | [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]] |
594d53ba MG |
109 | }, |
110 | }, | |
111 | ||
594d53ba MG |
112 | sub (/st/:contest) { |
113 | response st => 'Standings', { | |
7468a6a7 | 114 | st => [ contest->standings ], |
ebca729d MG |
115 | problems => [ |
116 | map { [$_->id, $_->name] } | |
117 | sort { $a->value <=> $b->value } | |
118 | map { $_->problem } contest->contest_problems], | |
c039d63e | 119 | }, 10 |
594d53ba MG |
120 | }, |
121 | ||
645cfb7d | 122 | sub (/ed/:contest) { |
90f613d3 | 123 | forbid !contest->is_finished; |
42243f04 | 124 | my $pblist = db->problem_list(contest => $_{contest}, solution => 1); |
54bc2c5c | 125 | response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : (); |
645cfb7d MG |
126 | }, |
127 | ||
462db4aa MG |
128 | sub (/login) { |
129 | forbid !remote_user; | |
2beb67b4 | 130 | [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]] |
462db4aa MG |
131 | }, |
132 | ||
594d53ba MG |
133 | sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, |
134 | ||
135 | sub (/us/) { response us => 'Users', {us => db->user_list} }, | |
54bc2c5c | 136 | sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 }, |
23e32638 | 137 | sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) { |
b8a0fa71 | 138 | forbid $_{private}; |
7e158d3c | 139 | response log => 'Job list', db->job_list(%_), 5 |
b8a0fa71 | 140 | }, |
87ffd88b | 141 | sub (/pb/ + ?:owner~&:contest~&:private~) { |
b8a0fa71 | 142 | forbid $_{private}; |
2ca1b4a6 | 143 | forbid contest && contest->is_pending; |
ee38c972 | 144 | response pb => 'Problems', db->problem_list(%_) |
b8a0fa71 | 145 | }, |
594d53ba | 146 | |
c5343878 MG |
147 | sub (/us/:user) { |
148 | my $user = db->user_entry($_{user}); | |
149 | response us_entry => $user->{name}, $user | |
150 | }, | |
151 | sub (/ct/:contest) { | |
152 | my $contest = db->contest_entry($_{contest}); | |
153 | response ct_entry => $contest->{name}, $contest, 60 | |
154 | }, | |
28e89d6c | 155 | sub (/log/:job) { |
c5343878 MG |
156 | my $job = db->job_entry($_{job}); |
157 | forbid $job->{private}; | |
158 | response log_entry => "Job $_{job}", $job, 10 | |
28e89d6c | 159 | }, |
84ca7535 MG |
160 | sub (/pb/:problem + ?contest~) { |
161 | my (undef, undef, $contest) = @_; | |
162 | $_{contest} = $contest; | |
8a4acaca MG |
163 | return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem}); |
164 | forbid problem->private && !contest; | |
165 | if (contest) { | |
166 | return redirect "/pb/$_{problem}" unless contest->is_running; | |
5b76a57d MG |
167 | forbid !remote_user; |
168 | $privacy = 'private'; | |
169 | } | |
c5343878 | 170 | response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->{id}), $_{contest} ? 10 : (); |
594d53ba | 171 | }, |
e4d5bdf5 | 172 | sub (/sol/:problem) { |
8a4acaca | 173 | forbid problem->private; |
e4d5bdf5 MG |
174 | response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution}; |
175 | }, | |
594d53ba MG |
176 | |
177 | sub (/) { redispatch_to '/index' }, | |
cb0122d7 | 178 | sub (/favicon.ico) { redirect '/static/favicon.ico' }, |
928a611f | 179 | 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 |
180 | }, |
181 | ||
182 | sub (POST) { | |
08794667 MG |
183 | sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) { |
184 | my (undef, undef, $prog) = @_; | |
594d53ba | 185 | forbid !remote_user; |
8a4acaca | 186 | my $private = (problem->private && !contest) ? 1 : 0; |
3b69df7a | 187 | return reply 'This contest has finished' if contest && contest->is_finished; |
8a4acaca MG |
188 | return reply 'This contest has not yet started' if contest && contest->is_pending; |
189 | return reply 'This problem is private' if !admin && $private; | |
190 | return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem}); | |
08794667 | 191 | return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024; |
c5343878 | 192 | return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30; |
594d53ba | 193 | |
08794667 MG |
194 | my $source = $prog ? read_file $prog->path : $_{source_code}; |
195 | unlink $prog->path if $prog; | |
c5343878 | 196 | my $id = db->create_job( |
594d53ba | 197 | maybe contest => $_{contest}, |
cb6adaff | 198 | private => $private, |
594d53ba MG |
199 | date => time, |
200 | extension => FORMAT_EXTENSION->{$_{prog_format}}, | |
201 | format => $_{prog_format}, | |
202 | problem => $_{problem}, | |
08794667 | 203 | source => $source, |
c5343878 MG |
204 | owner => remote_user->{id}, |
205 | ); | |
594d53ba | 206 | |
c5343878 | 207 | [303, [Location => '/log/' . $id], []] |
3c434a02 | 208 | }, |
594d53ba | 209 | } |
7dc32473 MG |
210 | } |
211 | ||
594d53ba | 212 | |
7dc32473 MG |
213 | 1; |
214 | __END__ |