]> iEval git - gruntmaster-page.git/blame - lib/Plack/App/Gruntmaster.pm
Use Plack::Middleware::Auth::Complex for auth
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
CommitLineData
7dc32473
MG
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
7dc32473
MG
5our $VERSION = '5999.000_001';
6
e03e380b 7use CSS::Minifier::XS;
3b69df7a 8use Encode qw/encode decode/;
f34254b8 9use File::Slurp qw/read_file/;
e03e380b 10use JavaScript::Minifier::XS;
3b69df7a 11use JSON::MaybeXS qw/encode_json/;
594d53ba
MG
12use PerlX::Maybe;
13use Scope::Upper qw/unwind SUB UP/;
3b69df7a 14use Web::Simple;
594d53ba
MG
15
16use Gruntmaster::Data;
3b69df7a
MG
17use Plack::App::Gruntmaster::HTML;
18
3c434a02
MG
19use Email::Sender::Simple qw/sendmail/;
20use Email::Simple;
21
3b69df7a
MG
22use warnings NONFATAL => 'all';
23no warnings 'illegalproto';
f34254b8 24
594d53ba
MG
25##################################################
26
594d53ba
MG
27use constant USER_REGEX => qr/^\w{2,20}$/a;
28
e03e380b
MG
29use constant CONTENT_TYPES => +{
30 c => 'text/x-csrc',
31 cpp => 'text/x-c++src',
32 cs => 'text/x-csharp', # Used by GNOME. Not in mime.types.
a5e355d6
MG
33 go => 'text/plain', # ?
34 hs => 'text/x-haskell',
e03e380b
MG
35 java => 'text/x-java',
36 pas => 'text/x-pascal',
37 pl => 'text/x-perl',
38 py => 'text/x-python',
76454bde 39 l => 'text/plain',
e03e380b 40};
594d53ba
MG
41
42use constant FORMAT_EXTENSION => {
43 C => 'c',
44 CPP => 'cpp',
a5e355d6
MG
45 GCCGO => 'go',
46 GOLANG => 'go',
47 HASKELL => 'hs',
594d53ba
MG
48 MONO => 'cs',
49 JAVA => 'java',
50 PASCAL => 'pas',
51 PERL => 'pl',
52 PYTHON => 'py',
e6d1bcd4 53 SBCL => 'l',
594d53ba
MG
54};
55
3ef32174 56use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
594d53ba 57
1bb102ef
MG
58sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
59
c039d63e 60my ($env, $privacy);
594d53ba
MG
61
62sub db { $env->{'gruntmaster.dbic'} }
63
64sub remote_user {
8d725691 65 my $user = $env->{REMOTE_USER};
594d53ba
MG
66 $user &&= db->user($user);
67 $user
68}
69
3b69df7a 70sub admin { remote_user && remote_user->admin }
594d53ba
MG
71sub contest { db->contest ($_{contest}) }
72sub problem { db->problem ($_{problem}) }
73sub job { db->job ($_{job}) }
74sub user { db->user ($_{user}) }
75
76sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
69c01de9 77sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
594d53ba 78sub response {
c039d63e 79 my ($template, $title, $params, $maxage) = @_;
594d53ba
MG
80 unless ($params) {
81 $params = $title;
82 $title = 'No title';
83 }
9a4806b3 84 $params->{time} = time;
39e2d01a 85 $params->{args} = {%_};
c039d63e 86 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
594d53ba 87}
3b69df7a 88
c039d63e
MG
89sub forbid {
90 my ($condition) = @_;
91 $privacy = 'private' if $condition;
92 return if !$condition || admin;
8d725691 93 unwind $env->{authcomplex}->unauthorized, SUB UP
594d53ba 94}
31d70015 95
594d53ba
MG
96sub dispatch_request{
97 $env = $_[PSGI_ENV];
c039d63e
MG
98 $privacy = 'public';
99
594d53ba
MG
100 sub (GET) {
101 sub (/css/:theme) {
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>;
69c01de9 106 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css; charset=utf-8');
1bb102ef 107 [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]]
594d53ba
MG
108 },
109
110 sub (/js.js) {
111 my $js;
112 $js .= read_file $_ for <js/*.js>;
69c01de9 113 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript; charset=utf-8');
1bb102ef 114 [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]]
594d53ba
MG
115 },
116
3ef32174 117 sub (/robots.txt) { NOT_FOUND },
3ef32174 118
594d53ba
MG
119 sub (/src/:job) {
120 return NOT_FOUND if !job;
c22928ed 121 my $isowner = remote_user && remote_user->id eq job->rawowner;
e547b147
MG
122 my $private = job->private || job->problem->private || job->contest && job->contest->is_running;
123 forbid !$isowner && $private;
124 my $privacy = $private ? 'private' : 'public';
1bc7c028 125 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->extension});
e547b147 126 push @headers, (Vary => 'Authorization') if $private;
594d53ba
MG
127 [200, \@headers, [job->source]]
128 },
129
130 sub (?:contest=) {
131 return NOT_FOUND if !contest;
132 forbid contest->is_pending;
133 response_filter { return shift }
134 },
135
136 sub (?:problem=) {
137 return NOT_FOUND if !problem;
138 forbid problem->is_private;
139 response_filter { return shift }
140 },
141
d3892d73 142 sub (?:format~) {
feaa8f5a 143 my $format = lc ($_{format} // '');
594d53ba
MG
144 response_filter {
145 my ($r) = @_;
146 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
c039d63e 147 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
be6551aa 148 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
69c01de9 149 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
3b69df7a 150 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
69c01de9 151 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
152 },
153 },
154
594d53ba
MG
155 sub (/st/:contest) {
156 response st => 'Standings', {
7468a6a7 157 st => [ contest->standings ],
ebca729d
MG
158 problems => [
159 map { [$_->id, $_->name] }
160 sort { $a->value <=> $b->value }
161 map { $_->problem } contest->contest_problems],
c039d63e 162 }, 10
594d53ba
MG
163 },
164
645cfb7d 165 sub (/ed/:contest) {
90f613d3 166 forbid !contest->is_finished;
42243f04
MG
167 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
168 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial};
645cfb7d
MG
169 },
170
462db4aa
MG
171 sub (/login) {
172 forbid !remote_user;
173
4f3b70b7
MG
174 my $return = $env->{HTTP_REFERER} // '/';
175 [303, ['Set-Cookie' => "username=".remote_user->id, Location => $return], []]
462db4aa
MG
176 },
177
594d53ba
MG
178 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
179
180 sub (/us/) { response us => 'Users', {us => db->user_list} },
181 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
b8a0fa71
MG
182 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~) {
183 forbid $_{private};
39e2d01a 184 response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest},}
b8a0fa71 185 },
87ffd88b 186 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71
MG
187 forbid $_{private};
188 response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}}
189 },
594d53ba
MG
190
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}) },
28e89d6c
MG
193 sub (/log/:job) {
194 forbid job->private;
195 response log_entry => "Job $_{job}", db->job_entry($_{job})
196 },
84ca7535
MG
197 sub (/pb/:problem + ?contest~) {
198 my (undef, undef, $contest) = @_;
199 $_{contest} = $contest;
594d53ba
MG
200 return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
201 forbid problem->is_private;
5b76a57d
MG
202 if (contest && contest->is_running) {
203 forbid !remote_user;
204 $privacy = 'private';
205 }
33ea2780 206 response pb_entry => problem->name, {%{db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id)}, maybe contest => $_{contest}};
594d53ba 207 },
e4d5bdf5
MG
208 sub (/sol/:problem) {
209 forbid !problem->is_in_archive;
210 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
211 },
594d53ba
MG
212
213 sub (/) { redispatch_to '/index' },
cb0122d7 214 sub (/favicon.ico) { redirect '/static/favicon.ico' },
69c01de9 215 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
216 },
217
218 sub (POST) {
08794667
MG
219 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
220 my (undef, undef, $prog) = @_;
594d53ba 221 forbid !remote_user;
3b69df7a
MG
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;
31f5eb01 224 return reply 'This problem does not belong to this contest' if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
08794667 225 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
594d53ba
MG
226 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
227 remote_user->update({lastjob => time});
228
08794667
MG
229 my $source = $prog ? read_file $prog->path : $_{source_code};
230 unlink $prog->path if $prog;
cb6adaff 231 my $private = (problem->private && !$_{contest}) ? 1 : 0;
b2597e87 232 $private = 1 if contest && contest->is_pending;
31dc8096 233 my $newjob = db->jobs->create({
594d53ba 234 maybe contest => $_{contest},
cb6adaff 235 private => $private,
594d53ba
MG
236 date => time,
237 extension => FORMAT_EXTENSION->{$_{prog_format}},
238 format => $_{prog_format},
239 problem => $_{problem},
08794667 240 source => $source,
594d53ba
MG
241 owner => remote_user->id,
242 });
243
31dc8096 244 [303, [Location => '/log/' . $newjob->id], []]
3c434a02 245 },
594d53ba 246 }
7dc32473
MG
247}
248
594d53ba 249
7dc32473
MG
2501;
251__END__
This page took 0.08264 seconds and 4 git commands to generate.