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