]>
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 | ||
e03e380b | 7 | use CSS::Minifier::XS; |
3b69df7a | 8 | use Encode qw/encode decode/; |
f34254b8 | 9 | use File::Slurp qw/read_file/; |
e03e380b | 10 | use JavaScript::Minifier::XS; |
3b69df7a | 11 | use JSON::MaybeXS qw/encode_json/; |
594d53ba MG |
12 | use PerlX::Maybe; |
13 | use Scope::Upper qw/unwind SUB UP/; | |
3b69df7a | 14 | use Web::Simple; |
594d53ba MG |
15 | |
16 | use Gruntmaster::Data; | |
3b69df7a MG |
17 | use Plack::App::Gruntmaster::HTML; |
18 | ||
19 | use warnings NONFATAL => 'all'; | |
20 | no warnings 'illegalproto'; | |
594d53ba | 21 | no if $] >= 5.017011, warnings => 'experimental::smartmatch'; |
f34254b8 | 22 | |
594d53ba MG |
23 | ################################################## |
24 | ||
594d53ba MG |
25 | use constant USER_REGEX => qr/^\w{2,20}$/a; |
26 | ||
e03e380b MG |
27 | use constant CONTENT_TYPES => +{ |
28 | c => 'text/x-csrc', | |
29 | cpp => 'text/x-c++src', | |
30 | cs => 'text/x-csharp', # Used by GNOME. Not in mime.types. | |
a5e355d6 MG |
31 | go => 'text/plain', # ? |
32 | hs => 'text/x-haskell', | |
e03e380b MG |
33 | java => 'text/x-java', |
34 | pas => 'text/x-pascal', | |
35 | pl => 'text/x-perl', | |
36 | py => 'text/x-python', | |
37 | }; | |
594d53ba MG |
38 | |
39 | use constant FORMAT_EXTENSION => { | |
40 | C => 'c', | |
41 | CPP => 'cpp', | |
a5e355d6 MG |
42 | GCCGO => 'go', |
43 | GOLANG => 'go', | |
44 | HASKELL => 'hs', | |
594d53ba MG |
45 | MONO => 'cs', |
46 | JAVA => 'java', | |
47 | PASCAL => 'pas', | |
48 | PERL => 'pl', | |
49 | PYTHON => 'py', | |
e6d1bcd4 | 50 | SBCL => 'l', |
594d53ba MG |
51 | }; |
52 | ||
e03e380b | 53 | use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']]; |
3b69df7a | 54 | use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']]; |
594d53ba | 55 | |
1bb102ef MG |
56 | sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' } |
57 | ||
c039d63e | 58 | my ($env, $privacy); |
594d53ba MG |
59 | |
60 | sub db { $env->{'gruntmaster.dbic'} } | |
61 | ||
62 | sub remote_user { | |
63 | my $user = $env->{'gruntmaster.user'}; | |
64 | $user &&= db->user($user); | |
65 | $user | |
66 | } | |
67 | ||
3b69df7a | 68 | sub admin { remote_user && remote_user->admin } |
594d53ba MG |
69 | sub contest { db->contest ($_{contest}) } |
70 | sub problem { db->problem ($_{problem}) } | |
71 | sub job { db->job ($_{job}) } | |
72 | sub user { db->user ($_{user}) } | |
73 | ||
74 | sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] } | |
75 | sub reply { [200, ['Content-Type' => 'text/plain'], \@_] } | |
76 | sub response { | |
c039d63e | 77 | my ($template, $title, $params, $maxage) = @_; |
594d53ba MG |
78 | unless ($params) { |
79 | $params = $title; | |
80 | $title = 'No title'; | |
81 | } | |
9a4806b3 | 82 | $params->{time} = time; |
39e2d01a | 83 | $params->{args} = {%_}; |
c039d63e | 84 | bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response' |
594d53ba | 85 | } |
3b69df7a | 86 | |
c039d63e MG |
87 | sub forbid { |
88 | my ($condition) = @_; | |
89 | $privacy = 'private' if $condition; | |
90 | return if !$condition || admin; | |
594d53ba MG |
91 | unwind FORBIDDEN, SUB UP |
92 | } | |
31d70015 | 93 | |
594d53ba MG |
94 | sub dispatch_request{ |
95 | $env = $_[PSGI_ENV]; | |
c039d63e MG |
96 | $privacy = 'public'; |
97 | ||
594d53ba MG |
98 | sub (GET) { |
99 | sub (/css/:theme) { | |
100 | my $theme = $_{theme}; | |
101 | return NOT_FOUND unless -e "css/themes/$theme.css"; | |
102 | my $css = read_file "css/themes/$theme.css"; | |
103 | $css .= read_file $_ for <css/*.css>; | |
104 | my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css'); | |
1bb102ef | 105 | [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]] |
594d53ba MG |
106 | }, |
107 | ||
108 | sub (/js.js) { | |
109 | my $js; | |
110 | $js .= read_file $_ for <js/*.js>; | |
111 | my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript'); | |
1bb102ef | 112 | [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]] |
594d53ba MG |
113 | }, |
114 | ||
115 | sub (/src/:job) { | |
116 | return NOT_FOUND if !job; | |
c22928ed | 117 | my $isowner = remote_user && remote_user->id eq job->rawowner; |
e547b147 MG |
118 | my $private = job->private || job->problem->private || job->contest && job->contest->is_running; |
119 | forbid !$isowner && $private; | |
120 | my $privacy = $private ? 'private' : 'public'; | |
121 | my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->format}); | |
122 | push @headers, (Vary => 'Authorization') if $private; | |
594d53ba MG |
123 | [200, \@headers, [job->source]] |
124 | }, | |
125 | ||
126 | sub (?:contest=) { | |
127 | return NOT_FOUND if !contest; | |
128 | forbid contest->is_pending; | |
129 | response_filter { return shift } | |
130 | }, | |
131 | ||
132 | sub (?:problem=) { | |
133 | return NOT_FOUND if !problem; | |
134 | forbid problem->is_private; | |
135 | response_filter { return shift } | |
136 | }, | |
137 | ||
d3892d73 | 138 | sub (?:format~) { |
feaa8f5a | 139 | my $format = lc ($_{format} // ''); |
594d53ba MG |
140 | response_filter { |
141 | my ($r) = @_; | |
142 | return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; | |
c039d63e | 143 | my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}"); |
be6551aa | 144 | push @hdrs, Vary => 'Authorization' if $privacy eq 'private'; |
c039d63e | 145 | return [200, ['Content-Type' => 'application/json', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; |
3b69df7a | 146 | my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; |
c039d63e | 147 | [200, ['Content-Type' => 'text/html', @hdrs], [encode 'UTF-8', $ret]] |
594d53ba MG |
148 | }, |
149 | }, | |
150 | ||
594d53ba MG |
151 | sub (/st/:contest) { |
152 | response st => 'Standings', { | |
7468a6a7 | 153 | st => [ contest->standings ], |
ebca729d MG |
154 | problems => [ |
155 | map { [$_->id, $_->name] } | |
156 | sort { $a->value <=> $b->value } | |
157 | map { $_->problem } contest->contest_problems], | |
c039d63e | 158 | }, 10 |
594d53ba MG |
159 | }, |
160 | ||
645cfb7d MG |
161 | sub (/ed/:contest) { |
162 | forbid contest->is_running; | |
163 | response ed => 'Editorial of ' . contest->name, db->problem_list(contest => $_{contest}, solution => 1); | |
164 | }, | |
165 | ||
462db4aa MG |
166 | sub (/login) { |
167 | forbid !remote_user; | |
168 | ||
4f3b70b7 MG |
169 | my $return = $env->{HTTP_REFERER} // '/'; |
170 | [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []] | |
462db4aa MG |
171 | }, |
172 | ||
594d53ba MG |
173 | sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, |
174 | ||
175 | sub (/us/) { response us => 'Users', {us => db->user_list} }, | |
176 | sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) }, | |
b8a0fa71 MG |
177 | sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) { |
178 | forbid $_{private}; | |
39e2d01a | 179 | response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest},} |
b8a0fa71 | 180 | }, |
87ffd88b | 181 | sub (/pb/ + ?:owner~&:contest~&:private~) { |
b8a0fa71 MG |
182 | forbid $_{private}; |
183 | response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}} | |
184 | }, | |
594d53ba MG |
185 | |
186 | sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) }, | |
187 | sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) }, | |
28e89d6c MG |
188 | sub (/log/:job) { |
189 | forbid job->private; | |
190 | response log_entry => "Job $_{job}", db->job_entry($_{job}) | |
191 | }, | |
84ca7535 MG |
192 | sub (/pb/:problem + ?contest~) { |
193 | my (undef, undef, $contest) = @_; | |
194 | $_{contest} = $contest; | |
594d53ba MG |
195 | return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem}); |
196 | forbid problem->is_private; | |
5b76a57d MG |
197 | if (contest && contest->is_running) { |
198 | forbid !remote_user; | |
199 | $privacy = 'private'; | |
200 | } | |
33ea2780 | 201 | response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}}; |
594d53ba | 202 | }, |
e4d5bdf5 MG |
203 | sub (/sol/:problem) { |
204 | forbid !problem->is_in_archive; | |
205 | response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution}; | |
206 | }, | |
594d53ba MG |
207 | |
208 | sub (/) { redispatch_to '/index' }, | |
cb0122d7 | 209 | sub (/favicon.ico) { redirect '/static/favicon.ico' }, |
c039d63e | 210 | sub (/:article) { [200, ['Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] } |
594d53ba MG |
211 | }, |
212 | ||
213 | sub (POST) { | |
ca0c7ea2 | 214 | sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:country=&:level=) { |
594d53ba MG |
215 | return reply 'Parameter too long' if grep { length > 200 } values %_; |
216 | return reply 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $_{username} =~ USER_REGEX; | |
217 | return reply 'Username already in use' if db->user($_{username}); | |
218 | return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password}; | |
219 | ||
ca0c7ea2 | 220 | db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, country => $_{country}, level => $_{level}}); |
594d53ba MG |
221 | db->user($_{username})->set_passphrase($_{password}); |
222 | ||
c039d63e | 223 | purge '/us/'; |
594d53ba MG |
224 | reply 'Registered successfully'; |
225 | }, | |
226 | ||
227 | sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) { | |
228 | forbid !remote_user; | |
229 | return reply 'Incorrect password' unless remote_user->check_passphrase($_{password}); | |
230 | return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password}; | |
231 | remote_user->set_passphrase($_{new_password}); | |
232 | reply 'Password changed successfully'; | |
233 | }, | |
234 | ||
08794667 MG |
235 | sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) { |
236 | my (undef, undef, $prog) = @_; | |
594d53ba | 237 | forbid !remote_user; |
3b69df7a MG |
238 | return reply 'This contest has finished' if contest && contest->is_finished; |
239 | return reply 'This contest has not yet started' if !admin && contest && contest->is_pending; | |
31f5eb01 | 240 | return reply 'This problem does not belong to this contest' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem}); |
08794667 | 241 | return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024; |
594d53ba MG |
242 | return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30; |
243 | remote_user->update({lastjob => time}); | |
244 | ||
08794667 MG |
245 | my $source = $prog ? read_file $prog->path : $_{source_code}; |
246 | unlink $prog->path if $prog; | |
cb6adaff | 247 | my $private = (problem->private && !$_{contest}) ? 1 : 0; |
b2597e87 | 248 | $private = 1 if contest && contest->is_pending; |
31dc8096 | 249 | my $newjob = db->jobs->create({ |
594d53ba | 250 | maybe contest => $_{contest}, |
cb6adaff | 251 | private => $private, |
594d53ba MG |
252 | date => time, |
253 | extension => FORMAT_EXTENSION->{$_{prog_format}}, | |
254 | format => $_{prog_format}, | |
255 | problem => $_{problem}, | |
08794667 | 256 | source => $source, |
594d53ba MG |
257 | owner => remote_user->id, |
258 | }); | |
259 | ||
c039d63e | 260 | purge '/log/'; |
31dc8096 | 261 | [303, [Location => '/log/' . $newjob->id], []] |
594d53ba MG |
262 | } |
263 | } | |
7dc32473 MG |
264 | } |
265 | ||
594d53ba | 266 | |
7dc32473 MG |
267 | 1; |
268 | __END__ |