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 Email
::Sender
::Simple qw
/sendmail/;
22 use warnings NONFATAL
=> 'all';
23 no warnings
'illegalproto';
25 ##################################################
27 use constant USER_REGEX
=> qr/^\w{2,20}$/a;
29 use constant CONTENT_TYPES
=> +{
31 cpp
=> 'text/x-c++src',
32 cs
=> 'text/x-csharp', # Used by GNOME. Not in mime.types.
33 go
=> 'text/plain', # ?
34 hs
=> 'text/x-haskell',
35 java
=> 'text/x-java',
36 pas
=> 'text/x-pascal',
38 py
=> 'text/x-python',
42 use constant FORMAT_EXTENSION
=> {
56 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
58 sub development
() { ($ENV{PLACK_ENV
} // 'development') eq 'development' }
62 sub db
{ $env->{'gruntmaster.dbic'} }
65 my $user = $env->{REMOTE_USER
};
66 $user &&= db
->user($user);
70 sub admin
{ remote_user
&& remote_user
->admin }
71 sub contest
{ db
->contest ($_{contest
}) }
72 sub problem
{ db
->problem ($_{problem
}) }
73 sub job
{ db
->job ($_{job
}) }
74 sub user
{ db
->user ($_{user
}) }
76 sub redirect
{ [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
77 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
79 my ($template, $title, $params, $maxage) = @_;
84 $params->{time} = time;
85 $params->{args
} = {%_};
86 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 1)}, __PACKAGE__
.'::Response'
91 $privacy = 'private' if $condition;
92 return if !$condition || admin
;
93 unwind
$env->{authcomplex
}->unauthorized, SUB UP
102 my $theme = $_{theme
};
103 return NOT_FOUND
unless -e
"css/themes/$theme.css";
104 my $css = read_file
"css/themes/$theme.css";
105 $css .= read_file
$_ for <css
/*.css
>;
106 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css; charset=utf-8');
107 [200, \
@headers, [development ?
$css : CSS
::Minifier
::XS
::minify
$css]]
112 $js .= read_file
$_ for <js
/*.js
>;
113 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript; charset=utf-8');
114 [200, \
@headers, [development ?
$js : JavaScript
::Minifier
::XS
::minify
$js]]
117 sub (/robots
.txt
) { NOT_FOUND
},
120 return NOT_FOUND
if !job
;
121 my $isowner = remote_user
&& remote_user
->id eq job
->rawowner;
122 my $private = job
->private || job
->problem->private || job
->contest && job
->contest->is_running;
123 forbid
!$isowner && $private;
124 my $privacy = $private ?
'private' : 'public';
125 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES
->{job
->extension});
126 push @headers, (Vary
=> 'Authorization') if $private;
127 [200, \
@headers, [job
->source]]
131 return NOT_FOUND
if !contest
;
132 forbid contest
->is_pending;
133 response_filter
{ return shift }
137 return NOT_FOUND
if !problem
;
138 forbid problem
->is_private;
139 response_filter
{ return shift }
143 my $format = lc ($_{format
} // '');
146 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
147 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
148 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
149 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
150 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
151 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
156 response st
=> 'Standings', {
157 st
=> [ contest
->standings ],
159 map { [$_->id, $_->name] }
160 sort { $a->value <=> $b->value }
161 map { $_->problem } contest
->contest_problems],
166 forbid
!contest
->is_finished;
167 my $pblist = db
->problem_list(contest
=> $_{contest
}, solution
=> 1);
168 response ed
=> 'Editorial of ' . contest
->name, {%$pblist, editorial
=> contest
->editorial};
174 my $return = $env->{HTTP_REFERER
} // '/';
175 [303, ['Set-Cookie' => "username=".remote_user
->id, Location
=> $return], []]
178 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
180 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
181 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_) },
182 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~) {
184 response
log => 'Job list', {%{db
->job_list(%_)}, maybe contest
=> $_{contest
},}
186 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
188 response pb
=> 'Problems', {%{db
->problem_list(%_)}, maybe contest
=> $_{contest
}}
191 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
192 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}) },
195 response log_entry
=> "Job $_{job}", db
->job_entry($_{job
})
197 sub (/pb/:problem
+ ?contest
~) {
198 my (undef, undef, $contest) = @_;
199 $_{contest
} = $contest;
200 return NOT_FOUND
if !contest
&& !problem
->is_in_archive || contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
201 forbid problem
->is_private;
202 if (contest
&& contest
->is_running) {
204 $privacy = 'private';
206 response pb_entry
=> problem
->name, {%{db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id)}, maybe contest
=> $_{contest
}};
208 sub (/sol/:problem
) {
209 forbid
!problem
->is_in_archive;
210 response sol
=> 'Solution of ' . problem
->name, {solution
=> db
->problem($_{problem
})->solution};
213 sub (/) { redispatch_to '/index
' },
214 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
215 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=60', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
219 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
220 my (undef, undef, $prog) = @_;
222 return reply 'This contest has finished
' if contest && contest->is_finished;
223 return reply 'This contest has
not yet started
' if !admin && contest && contest->is_pending;
224 return reply 'This problem does
not belong to this contest
' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
225 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
226 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->lastjob + 30;
227 remote_user->update({lastjob => time});
229 my $source = $prog ? read_file $prog->path : $_{source_code};
230 unlink $prog->path if $prog;
231 my $private = (problem->private && !$_{contest}) ? 1 : 0;
232 $private = 1 if contest && contest->is_pending;
233 my $newjob = db->jobs->create({
234 maybe contest => $_{contest},
237 extension => FORMAT_EXTENSION->{$_{prog_format}},
238 format => $_{prog_format},
239 problem => $_{problem},
241 owner => remote_user->id,
244 [303, [Location => '/log/' . $newjob->id], []]