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
=> {
52 use constant NOT_FOUND
=> [404, ['Content-Type' => 'text/plain'], ['Not found']];
53 use constant FORBIDDEN
=> [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
55 sub development
() { ($ENV{PLACK_ENV
} // 'development') eq 'development' }
59 sub db
{ $env->{'gruntmaster.dbic'} }
62 my $user = $env->{'gruntmaster.user'};
63 $user &&= db
->user($user);
67 sub admin
{ remote_user
&& remote_user
->admin }
68 sub contest
{ db
->contest ($_{contest
}) }
69 sub problem
{ db
->problem ($_{problem
}) }
70 sub job
{ db
->job ($_{job
}) }
71 sub user
{ db
->user ($_{user
}) }
73 sub redirect
{ [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
74 sub reply
{ [200, ['Content-Type' => 'text/plain'], \
@_] }
76 my ($template, $title, $params) = @_;
81 $params->{time} = time;
82 bless {template
=> $template, title
=> $title, params
=> $params}, __PACKAGE__
.'::Response'
86 return if !shift || admin
;
87 unwind FORBIDDEN
, SUB UP
94 my $theme = $_{theme
};
95 return NOT_FOUND
unless -e
"css/themes/$theme.css";
96 my $css = read_file
"css/themes/$theme.css";
97 $css .= read_file
$_ for <css
/*.css
>;
98 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
99 [200, \
@headers, [development ?
$css : CSS
::Minifier
::XS
::minify
$css]]
104 $js .= read_file
$_ for <js
/*.js
>;
105 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
106 [200, \
@headers, [development ?
$js : JavaScript
::Minifier
::XS
::minify
$js]]
110 return NOT_FOUND
if !job
;
111 forbid job
->private || job
->problem->private || job
->contest && job
->contest->private;
112 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => CONTENT_TYPES
->{job
->format});
113 [200, \
@headers, [job
->source]]
117 return NOT_FOUND
if !contest
;
118 forbid contest
->is_pending;
119 response_filter
{ return shift }
123 return NOT_FOUND
if !problem
;
124 forbid problem
->is_private;
125 response_filter
{ return shift }
129 my $format = lc ($_{format
} // '');
132 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
133 return [200, ['Content-Type' => 'application/json'], [encode_json
$r->{params
}]] if $format eq 'json';
134 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
135 [200, ['Content-Type' => 'text/html'], [encode
'UTF-8', $ret]]
140 response st
=> 'Standings', {
141 st
=> [ contest
->standings ],
142 problems
=> [map { $_->problem } contest
->contest_problems],
146 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
148 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
149 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_) },
150 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~) { response
log => 'Job list', {%{db
->job_list(%_)}, maybe contest
=> $_{contest
}} },
151 sub (/pb/ + ?
:owner
~&:contest
~) { response pb
=> 'Problems', {%{db
->problem_list(%_)}, maybe contest
=> $_{contest
}} },
153 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
154 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}) },
155 sub (/log/:job
) { response log_entry
=> "Job $_{job}", db
->job_entry($_{job
}) },
156 sub (/pb/:problem
+ ?contest
~) {
157 my (undef, undef, $contest) = @_;
158 $_{contest
} = $contest;
159 return NOT_FOUND
if !contest
&& !problem
->is_in_archive || contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
160 forbid problem
->is_private;
161 response pb_entry
=> problem
->name, db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id);
163 sub (/sol/:problem
) {
164 forbid
!problem
->is_in_archive;
165 response sol
=> 'Solution of ' . problem
->name, {solution
=> db
->problem($_{problem
})->solution};
168 sub (/) { redispatch_to '/index
' },
169 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
'], [render_article $_{article}, 'en
']] }
173 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:level=) {
174 return reply 'Parameter too long
' if grep { length > 200 } values %_;
175 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;
176 return reply 'Username already
in use' if db->user($_{username});
177 return reply 'The two passwords
do not match
' unless $_{password} eq $_{confirm_password};
179 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, level => $_{level}});
180 db->user($_{username})->set_passphrase($_{password});
182 reply 'Registered successfully
';
185 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
187 return reply 'Incorrect password
' unless remote_user->check_passphrase($_{password});
188 return reply 'The two passwords
do not match
' unless $_{new_password} eq $_{confirm_new_password};
189 remote_user->set_passphrase($_{new_password});
190 reply 'Password changed successfully
';
193 sub (/action/submit + %:problem=&:contest~&:prog_format=&:private~&:source_code~ + *:prog~) {
195 return reply 'This contest has finished
' if contest && contest->is_finished;
196 return reply 'This contest has
not yet started
' if !admin && contest && contest->is_pending;
197 return reply 'Maximum source size is
10KB
' if ($_{prog} ? $_{prog}->size : length $_{source_code}) > 10 * 1024;
198 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->lastjob + 30;
199 remote_user->update({lastjob => time});
201 my $prog = $_{prog} ? read_file $_{prog}->path : $_{source_code};
202 unlink $_{prog}->path if $_{prog};
204 maybe contest => $_{contest},
205 maybe private => $_{private},
207 extension => FORMAT_EXTENSION->{$_{prog_format}},
208 format => $_{prog_format},
209 problem => $_{problem},
211 owner => remote_user->id,
214 redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/';