1 package Plack
::App
::Gruntmaster
;
5 our $VERSION = '5999.000_001';
8 use Encode qw
/encode decode/;
9 use File
::Slurp qw
/read_file/;
10 use JavaScript
::Minifier
::XS
;
11 use JSON
::MaybeXS qw
/encode_json/;
13 use Scope
::Upper qw
/unwind SUB UP/;
16 use Gruntmaster
::Data
;
17 use Plack
::App
::Gruntmaster
::HTML
;
19 use warnings NONFATAL
=> 'all';
20 no warnings
'illegalproto';
21 no if $] >= 5.017011, warnings
=> 'experimental::smartmatch';
23 ##################################################
25 use constant USER_REGEX
=> qr/^\w{2,20}$/a;
27 use constant CONTENT_TYPES
=> +{
29 cpp
=> 'text/x-c++src',
30 cs
=> 'text/x-csharp', # Used by GNOME. Not in mime.types.
31 go
=> 'text/plain', # ?
32 hs
=> 'text/x-haskell',
33 java
=> 'text/x-java',
34 pas
=> 'text/x-pascal',
36 py
=> 'text/x-python',
39 use constant FORMAT_EXTENSION
=> {
53 use constant NOT_FOUND
=> [404, ['Content-Type' => 'text/plain'], ['Not found']];
54 use constant FORBIDDEN
=> [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
56 sub development
() { ($ENV{PLACK_ENV
} // 'development') eq 'development' }
60 sub db
{ $env->{'gruntmaster.dbic'} }
63 my $user = $env->{'gruntmaster.user'};
64 $user &&= db
->user($user);
68 sub admin
{ remote_user
&& remote_user
->admin }
69 sub contest
{ db
->contest ($_{contest
}) }
70 sub problem
{ db
->problem ($_{problem
}) }
71 sub job
{ db
->job ($_{job
}) }
72 sub user
{ db
->user ($_{user
}) }
74 sub redirect
{ [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
75 sub reply
{ [200, ['Content-Type' => 'text/plain'], \
@_] }
77 my ($template, $title, $params, $maxage) = @_;
82 $params->{time} = time;
83 $params->{args
} = {%_};
84 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 1)}, __PACKAGE__
.'::Response'
89 $privacy = 'private' if $condition;
90 return if !$condition || admin
;
91 unwind FORBIDDEN
, SUB UP
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');
105 [200, \
@headers, [development ?
$css : CSS
::Minifier
::XS
::minify
$css]]
110 $js .= read_file
$_ for <js
/*.js
>;
111 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
112 [200, \
@headers, [development ?
$js : JavaScript
::Minifier
::XS
::minify
$js]]
116 return NOT_FOUND
if !job
;
117 my $isowner = remote_user
&& remote_user
->id eq job
->rawowner;
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;
123 [200, \
@headers, [job
->source]]
127 return NOT_FOUND
if !contest
;
128 forbid contest
->is_pending;
129 response_filter
{ return shift }
133 return NOT_FOUND
if !problem
;
134 forbid problem
->is_private;
135 response_filter
{ return shift }
139 my $format = lc ($_{format
} // '');
142 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
143 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
144 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
145 return [200, ['Content-Type' => 'application/json', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
146 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
147 [200, ['Content-Type' => 'text/html', @hdrs], [encode
'UTF-8', $ret]]
152 response st
=> 'Standings', {
153 st
=> [ contest
->standings ],
155 map { [$_->id, $_->name] }
156 sort { $a->value <=> $b->value }
157 map { $_->problem } contest
->contest_problems],
162 forbid contest
->is_running;
163 my $pblist = db
->problem_list(contest
=> $_{contest
}, solution
=> 1);
164 response ed
=> 'Editorial of ' . contest
->name, {%$pblist, editorial
=> contest
->editorial};
170 my $return = $env->{HTTP_REFERER
} // '/';
171 [303, ['Set-Cookie' => "username=".remote_user
->id, Location
=> $return], []]
174 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
176 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
177 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_) },
178 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~) {
180 response
log => 'Job list', {%{db
->job_list(%_)}, maybe contest
=> $_{contest
},}
182 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
184 response pb
=> 'Problems', {%{db
->problem_list(%_)}, maybe contest
=> $_{contest
}}
187 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
188 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}) },
191 response log_entry
=> "Job $_{job}", db
->job_entry($_{job
})
193 sub (/pb/:problem
+ ?contest
~) {
194 my (undef, undef, $contest) = @_;
195 $_{contest
} = $contest;
196 return NOT_FOUND
if !contest
&& !problem
->is_in_archive || contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
197 forbid problem
->is_private;
198 if (contest
&& contest
->is_running) {
200 $privacy = 'private';
202 response pb_entry
=> problem
->name, {%{db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id)}, maybe contest
=> $_{contest
}};
204 sub (/sol/:problem
) {
205 forbid
!problem
->is_in_archive;
206 response sol
=> 'Solution of ' . problem
->name, {solution
=> db
->problem($_{problem
})->solution};
209 sub (/) { redispatch_to '/index
' },
210 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
211 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
', 'Cache
-Control
' => 'public
, max
-age
=60', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
215 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:country=&:level=) {
216 return reply 'Parameter too long
' if grep { length > 200 } values %_;
217 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;
218 return reply 'Username already
in use' if db->user($_{username});
219 return reply 'The two passwords
do not match
' unless $_{password} eq $_{confirm_password};
221 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, country => $_{country}, level => $_{level}});
222 db->user($_{username})->set_passphrase($_{password});
225 reply 'Registered successfully
';
228 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
230 return reply 'Incorrect password
' unless remote_user->check_passphrase($_{password});
231 return reply 'The two passwords
do not match
' unless $_{new_password} eq $_{confirm_new_password};
232 remote_user->set_passphrase($_{new_password});
233 reply 'Password changed successfully
';
236 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
237 my (undef, undef, $prog) = @_;
239 return reply 'This contest has finished
' if contest && contest->is_finished;
240 return reply 'This contest has
not yet started
' if !admin && contest && contest->is_pending;
241 return reply 'This problem does
not belong to this contest
' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
242 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
243 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->lastjob + 30;
244 remote_user->update({lastjob => time});
246 my $source = $prog ? read_file $prog->path : $_{source_code};
247 unlink $prog->path if $prog;
248 my $private = (problem->private && !$_{contest}) ? 1 : 0;
249 $private = 1 if contest && contest->is_pending;
250 my $newjob = db->jobs->create({
251 maybe contest => $_{contest},
254 extension => FORMAT_EXTENSION->{$_{prog_format}},
255 format => $_{prog_format},
256 problem => $_{problem},
258 owner => remote_user->id,
262 [303, [Location => '/log/' . $newjob->id], []]