1 package Plack
::App
::Gruntmaster
;
5 our $VERSION = '5999.000_001';
7 use Encode qw
/encode decode/;
8 use File
::Slurp qw
/read_file/;
9 use JSON
::MaybeXS qw
/encode_json/;
11 use Scope
::Upper qw
/unwind SUB UP/;
14 use Gruntmaster
::Data
;
15 use Plack
::App
::Gruntmaster
::HTML
;
17 use Email
::Sender
::Simple qw
/sendmail/;
20 use warnings NONFATAL
=> 'all';
21 no warnings
'illegalproto';
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', # ?
33 hs
=> 'text/x-haskell',
34 java
=> 'text/x-java',
35 pas
=> 'text/x-pascal',
37 py
=> 'text/x-python',
38 rb
=> 'application/x-ruby',
42 use constant FORMAT_EXTENSION
=> {
58 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
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, 'Cache-Control' => 'public, max-age=86400', '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 // 3600)}, __PACKAGE__
.'::Response'
91 $privacy = 'private' if $condition;
92 return if !$condition || admin
;
93 unwind
$env->{authcomplex
}->unauthorized, SUB UP
101 sub (/robots
.txt
) { NOT_FOUND
},
104 return NOT_FOUND
if !job
;
105 my $isowner = remote_user
&& remote_user
->id eq job
->rawowner;
106 my $private = job
->private || job
->problem->private || job
->contest && job
->contest->is_running;
107 forbid
!$isowner && $private;
108 my $privacy = $private ?
'private' : 'public';
109 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES
->{job
->extension});
110 push @headers, (Vary
=> 'Authorization') if $private;
111 [200, \
@headers, [job
->source]]
115 my $format = lc ($_{format
} // '');
118 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
119 my $vary = 'X-Static';
120 $vary .= ', Authorization' if $privacy eq 'private';
121 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}", Vary
=> $vary);
122 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
123 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}}, maybe static
=> $env->{HTTP_X_STATIC
};
124 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
129 response st
=> 'Standings', {
130 st
=> [ contest
->standings ],
132 map { [$_->id, $_->name] }
133 sort { $a->value <=> $b->value }
134 map { $_->problem } contest
->contest_problems],
139 forbid
!contest
->is_finished;
140 my $pblist = db
->problem_list(contest
=> $_{contest
}, solution
=> 1);
141 response ed
=> 'Editorial of ' . contest
->name, {%$pblist, editorial
=> contest
->editorial}, contest
->is_finished(time - 86400) ?
60 : ();
146 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
149 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
151 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
152 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_), 300 },
153 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
155 response
log => 'Job list', db
->job_list(%_), 5
157 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
159 forbid contest
&& contest
->is_pending;
160 response pb
=> 'Problems', db
->problem_list(%_)
163 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
164 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}), 60 },
167 response log_entry
=> "Job $_{job}", db
->job_entry($_{job
}), 10
169 sub (/pb/:problem
+ ?contest
~) {
170 my (undef, undef, $contest) = @_;
171 $_{contest
} = $contest;
172 return NOT_FOUND
if contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
173 forbid problem
->private && !contest
;
175 return redirect
"/pb/$_{problem}" unless contest
->is_running;
177 $privacy = 'private';
179 response pb_entry
=> problem
->name, db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id), $_{contest
} ?
10 : ();
181 sub (/sol/:problem
) {
182 forbid problem
->private;
183 response sol
=> 'Solution of ' . problem
->name, {solution
=> db
->problem($_{problem
})->solution};
186 sub (/) { redispatch_to '/index
' },
187 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
188 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=3600', 'X
-Forever
' => 1, Vary => 'X
-Static
'], [render_article $_{article}, 'en
', maybe static => $env->{HTTP_X_STATIC}]] }
192 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
193 my (undef, undef, $prog) = @_;
195 my $private = (problem->private && !contest) ? 1 : 0;
196 return reply 'This contest has finished
' if contest && contest->is_finished;
197 return reply 'This contest has
not yet started
' if contest && contest->is_pending;
198 return reply 'This problem is private
' if !admin && $private;
199 return reply 'This problem does
not belong to this contest
' if contest && !db->contest_problems->find($_{contest}, $_{problem});
200 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
201 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->lastjob + 30;
202 remote_user->update({lastjob => time});
204 my $source = $prog ? read_file $prog->path : $_{source_code};
205 unlink $prog->path if $prog;
206 my $newjob = db->jobs->create({
207 maybe contest => $_{contest},
210 extension => FORMAT_EXTENSION->{$_{prog_format}},
211 format => $_{prog_format},
212 problem => $_{problem},
214 owner => remote_user->id,
217 [303, [Location => '/log/' . $newjob->id], []]