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', # ?
32 hs
=> 'text/x-haskell',
33 java
=> 'text/x-java',
34 pas
=> 'text/x-pascal',
36 py
=> 'text/x-python',
40 use constant FORMAT_EXTENSION
=> {
54 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
58 sub db
{ $env->{'gruntmaster.dbic'} }
61 my $user = $env->{REMOTE_USER
};
62 $user &&= db
->user($user);
66 sub admin
{ remote_user
&& remote_user
->admin }
67 sub contest
{ db
->contest ($_{contest
}) }
68 sub problem
{ db
->problem ($_{problem
}) }
69 sub job
{ db
->job ($_{job
}) }
70 sub user
{ db
->user ($_{user
}) }
72 sub redirect
{ [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
73 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
75 my ($template, $title, $params, $maxage) = @_;
80 $params->{time} = time;
81 $params->{args
} = {%_};
82 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 1)}, __PACKAGE__
.'::Response'
87 $privacy = 'private' if $condition;
88 return if !$condition || admin
;
89 unwind
$env->{authcomplex
}->unauthorized, SUB UP
97 sub (/robots
.txt
) { NOT_FOUND
},
100 return NOT_FOUND
if !job
;
101 my $isowner = remote_user
&& remote_user
->id eq job
->rawowner;
102 my $private = job
->private || job
->problem->private || job
->contest && job
->contest->is_running;
103 forbid
!$isowner && $private;
104 my $privacy = $private ?
'private' : 'public';
105 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES
->{job
->extension});
106 push @headers, (Vary
=> 'Authorization') if $private;
107 [200, \
@headers, [job
->source]]
111 my $format = lc ($_{format
} // '');
114 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
115 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
116 push @hdrs, Vary
=> 'Authorization' if $privacy eq 'private';
117 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
118 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}};
119 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
124 response st
=> 'Standings', {
125 st
=> [ contest
->standings ],
127 map { [$_->id, $_->name] }
128 sort { $a->value <=> $b->value }
129 map { $_->problem } contest
->contest_problems],
134 forbid
!contest
->is_finished;
135 my $pblist = db
->problem_list(contest
=> $_{contest
}, solution
=> 1);
136 response ed
=> 'Editorial of ' . contest
->name, {%$pblist, editorial
=> contest
->editorial};
142 my $return = $env->{HTTP_REFERER
} // '/';
143 [303, ['Set-Cookie' => "username=".remote_user
->id, Location
=> $return], []]
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
~&:private
~&:result
~) {
152 response
log => 'Job list', {%{db
->job_list(%_)}, maybe contest
=> $_{contest
},}
154 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
156 forbid contest
&& contest
->is_pending;
157 response pb
=> 'Problems', db
->problem_list(%_)
160 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
161 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}) },
164 response log_entry
=> "Job $_{job}", db
->job_entry($_{job
})
166 sub (/pb/:problem
+ ?contest
~) {
167 my (undef, undef, $contest) = @_;
168 $_{contest
} = $contest;
169 return NOT_FOUND
if contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
170 forbid problem
->private && !contest
;
172 return redirect
"/pb/$_{problem}" unless contest
->is_running;
174 $privacy = 'private';
176 response pb_entry
=> problem
->name, {%{db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id)}, maybe contest
=> $_{contest
}};
178 sub (/sol/:problem
) {
179 forbid problem
->private;
180 response sol
=> 'Solution of ' . problem
->name, {solution
=> db
->problem($_{problem
})->solution};
183 sub (/) { redispatch_to '/index
' },
184 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
185 sub (/:article) { [200, ['Content
-Type
' => 'text
/html
; charset
=utf
-8', 'Cache
-Control
' => 'public
, max
-age
=60', 'X
-Forever
' => 1], [render_article $_{article}, 'en
']] }
189 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
190 my (undef, undef, $prog) = @_;
192 my $private = (problem->private && !contest) ? 1 : 0;
193 return reply 'This contest has finished
' if contest && contest->is_finished;
194 return reply 'This contest has
not yet started
' if contest && contest->is_pending;
195 return reply 'This problem is private
' if !admin && $private;
196 return reply 'This problem does
not belong to this contest
' if contest && !db->contest_problems->find($_{contest}, $_{problem});
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 $source = $prog ? read_file $prog->path : $_{source_code};
202 unlink $prog->path if $prog;
203 my $newjob = db->jobs->create({
204 maybe contest => $_{contest},
207 extension => FORMAT_EXTENSION->{$_{prog_format}},
208 format => $_{prog_format},
209 problem => $_{problem},
211 owner => remote_user->id,
214 [303, [Location => '/log/' . $newjob->id], []]