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',
37 rb
=> 'application/x-ruby',
41 use constant FORMAT_EXTENSION
=> {
56 use constant NOT_FOUND
=> [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
60 sub db
{ $env->{'gruntmaster.dbic'} }
63 my $user = $env->{REMOTE_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, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
75 sub reply
{ [200, ['Content-Type' => 'text/plain; charset=utf-8'], \
@_] }
77 my ($template, $title, $params, $maxage) = @_;
82 $params->{time} = time;
83 $params->{args
} = {%_};
84 bless {template
=> $template, title
=> $title, params
=> $params, maxage
=> ($maxage // 3600)}, __PACKAGE__
.'::Response'
89 $privacy = 'private' if $condition;
90 return if !$condition || admin
;
91 unwind
$env->{authcomplex
}->unauthorized, SUB UP
99 sub (/robots
.txt
) { NOT_FOUND
},
102 return NOT_FOUND
if !job
;
103 my $isowner = remote_user
&& remote_user
->id eq job
->rawowner;
104 my $private = job
->private || job
->problem->private || job
->contest && job
->contest->is_running;
105 forbid
!$isowner && $private;
106 my $privacy = $private ?
'private' : 'public';
107 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES
->{job
->extension});
108 push @headers, (Vary
=> 'Authorization') if $private;
109 [200, \
@headers, [job
->source]]
113 my $format = lc ($_{format
} // '');
116 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
117 my $vary = 'X-Static';
118 $vary .= ', Authorization' if $privacy eq 'private';
119 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}", Vary
=> $vary);
120 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json
$r->{params
}]] if $format eq 'json';
121 my $ret = render
$r->{template
}, 'en', title
=> $r->{title
}, %{$r->{params
}}, maybe static
=> $env->{HTTP_X_STATIC
};
122 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode
'UTF-8', $ret]]
127 response st
=> 'Standings', {
128 st
=> [ contest
->standings ],
130 map { [$_->id, $_->name] }
131 sort { $a->value <=> $b->value }
132 map { $_->problem } contest
->contest_problems],
137 forbid
!contest
->is_finished;
138 my $pblist = db
->problem_list(contest
=> $_{contest
}, solution
=> 1);
139 response ed
=> 'Editorial of ' . contest
->name, {%$pblist, editorial
=> contest
->editorial}, contest
->is_finished(time - 86400) ?
60 : ();
144 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary
=> 'Authorization'], [$env->{REMOTE_USER
}]]
147 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
149 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
150 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_), 300 },
151 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~&:private
~&:result
~) {
153 response
log => 'Job list', db
->job_list(%_), 5
155 sub (/pb/ + ?
:owner
~&:contest
~&:private
~) {
157 forbid contest
&& contest
->is_pending;
158 response pb
=> 'Problems', db
->problem_list(%_)
161 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
162 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}), 60 },
165 response log_entry
=> "Job $_{job}", db
->job_entry($_{job
}), 10
167 sub (/pb/:problem
+ ?contest
~) {
168 my (undef, undef, $contest) = @_;
169 $_{contest
} = $contest;
170 return NOT_FOUND
if contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
171 forbid problem
->private && !contest
;
173 return redirect
"/pb/$_{problem}" unless contest
->is_running;
175 $privacy = 'private';
177 response pb_entry
=> problem
->name, db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id), $_{contest
} ?
10 : ();
179 sub (/sol/:problem
) {
180 forbid problem
->private;
181 response sol
=> 'Solution of ' . problem
->name, {solution
=> db
->problem($_{problem
})->solution};
184 sub (/) { redispatch_to '/index
' },
185 sub (/favicon.ico) { redirect '/static
/favicon
.ico
' },
186 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}]] }
190 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
191 my (undef, undef, $prog) = @_;
193 my $private = (problem->private && !contest) ? 1 : 0;
194 return reply 'This contest has finished
' if contest && contest->is_finished;
195 return reply 'This contest has
not yet started
' if contest && contest->is_pending;
196 return reply 'This problem is private
' if !admin && $private;
197 return reply 'This problem does
not belong to this contest
' if contest && !db->contest_problems->find($_{contest}, $_{problem});
198 return reply 'Maximum source size is
10KB
' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
199 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->lastjob + 30;
200 remote_user->update({lastjob => time});
202 my $source = $prog ? read_file $prog->path : $_{source_code};
203 unlink $prog->path if $prog;
204 my $newjob = db->jobs->create({
205 maybe contest => $_{contest},
208 extension => FORMAT_EXTENSION->{$_{prog_format}},
209 format => $_{prog_format},
210 problem => $_{problem},
212 owner => remote_user->id,
215 [303, [Location => '/log/' . $newjob->id], []]