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