Commit | Line | Data |
---|---|---|
7dc32473 MG |
1 | package Plack::App::Gruntmaster; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
7dc32473 MG |
5 | our $VERSION = '5999.000_001'; |
6 | ||
3b69df7a | 7 | use Encode qw/encode decode/; |
f34254b8 | 8 | use File::Slurp qw/read_file/; |
3b69df7a | 9 | use JSON::MaybeXS qw/encode_json/; |
594d53ba MG |
10 | use PerlX::Maybe; |
11 | use Scope::Upper qw/unwind SUB UP/; | |
3b69df7a | 12 | use Web::Simple; |
594d53ba MG |
13 | |
14 | use Gruntmaster::Data; | |
3b69df7a MG |
15 | use Plack::App::Gruntmaster::HTML; |
16 | ||
17 | use warnings NONFATAL => 'all'; | |
18 | no warnings 'illegalproto'; | |
f34254b8 | 19 | |
594d53ba MG |
20 | ################################################## |
21 | ||
594d53ba MG |
22 | use constant USER_REGEX => qr/^\w{2,20}$/a; |
23 | ||
594d53ba MG |
24 | use constant FORMAT_EXTENSION => { |
25 | C => 'c', | |
26 | CPP => 'cpp', | |
a5e355d6 MG |
27 | GCCGO => 'go', |
28 | GOLANG => 'go', | |
d39978bf | 29 | GOLFSCRIPT => 'gs', |
a5e355d6 | 30 | HASKELL => 'hs', |
594d53ba MG |
31 | MONO => 'cs', |
32 | JAVA => 'java', | |
33 | PASCAL => 'pas', | |
34 | PERL => 'pl', | |
35 | PYTHON => 'py', | |
c83dca87 | 36 | RUBY => 'rb', |
e6d1bcd4 | 37 | SBCL => 'l', |
594d53ba MG |
38 | }; |
39 | ||
3ef32174 | 40 | use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']]; |
594d53ba | 41 | |
c039d63e | 42 | my ($env, $privacy); |
594d53ba | 43 | |
594d53ba | 44 | sub remote_user { |
2a06b02c MG |
45 | unless ($env->{'gruntmaster.user'}) { |
46 | my $user = $env->{REMOTE_USER}; | |
1f64ef28 | 47 | $user &&= user_entry $user; |
2a06b02c MG |
48 | $env->{'gruntmaster.user'} = $user; |
49 | } | |
50 | $env->{'gruntmaster.user'} | |
594d53ba MG |
51 | } |
52 | ||
c5343878 | 53 | sub admin { remote_user && remote_user->{admin} } |
594d53ba | 54 | |
54bc2c5c | 55 | sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] } |
69c01de9 | 56 | sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] } |
594d53ba | 57 | sub response { |
c039d63e | 58 | my ($template, $title, $params, $maxage) = @_; |
594d53ba MG |
59 | unless ($params) { |
60 | $params = $title; | |
61 | $title = 'No title'; | |
62 | } | |
9a4806b3 | 63 | $params->{time} = time; |
39e2d01a | 64 | $params->{args} = {%_}; |
54bc2c5c | 65 | bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response' |
594d53ba | 66 | } |
3b69df7a | 67 | |
c039d63e MG |
68 | sub forbid { |
69 | my ($condition) = @_; | |
70 | $privacy = 'private' if $condition; | |
71 | return if !$condition || admin; | |
8d725691 | 72 | unwind $env->{authcomplex}->unauthorized, SUB UP |
594d53ba | 73 | } |
31d70015 | 74 | |
594d53ba MG |
75 | sub dispatch_request{ |
76 | $env = $_[PSGI_ENV]; | |
c039d63e MG |
77 | $privacy = 'public'; |
78 | ||
594d53ba | 79 | sub (GET) { |
3ef32174 | 80 | sub (/robots.txt) { NOT_FOUND }, |
3ef32174 | 81 | |
594d53ba | 82 | sub (/src/:job) { |
1f64ef28 | 83 | my $job = job_full $_{job}; |
c5343878 MG |
84 | return NOT_FOUND if !$job; |
85 | my $isowner = remote_user && remote_user->{id} eq $job->{owner}; | |
1f64ef28 | 86 | my $contest = $job->{contest} && contest_entry $job->{contest}; |
02d17b02 | 87 | my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished}); |
e547b147 MG |
88 | forbid !$isowner && $private; |
89 | my $privacy = $private ? 'private' : 'public'; | |
218d6f7e | 90 | my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain'); |
e547b147 | 91 | push @headers, (Vary => 'Authorization') if $private; |
02d17b02 | 92 | [200, \@headers, [$job->{source}]] |
594d53ba MG |
93 | }, |
94 | ||
d3892d73 | 95 | sub (?:format~) { |
feaa8f5a | 96 | my $format = lc ($_{format} // ''); |
594d53ba MG |
97 | response_filter { |
98 | my ($r) = @_; | |
99 | return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; | |
928a611f MG |
100 | my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}"); |
101 | push @hdrs, Vary => 'Authorization' if $privacy eq 'private'; | |
69c01de9 | 102 | return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; |
928a611f | 103 | my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; |
69c01de9 | 104 | [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]] |
594d53ba MG |
105 | }, |
106 | }, | |
107 | ||
1f64ef28 | 108 | sub (/st/:contest) { response st => 'Standings', standings($_{contest}), 10 }, |
594d53ba | 109 | |
645cfb7d | 110 | sub (/ed/:contest) { |
1f64ef28 | 111 | my $contest = contest_full $_{contest}; |
a18eef28 | 112 | forbid !$contest->{finished}; |
1f64ef28 | 113 | my $pblist = problem_list contest => $_{contest}, solution => 1; |
a18eef28 | 114 | response ed => 'Editorial of ' . $contest->{name}, {%$pblist, editorial => $contest->{editorial}} |
645cfb7d MG |
115 | }, |
116 | ||
462db4aa MG |
117 | sub (/login) { |
118 | forbid !remote_user; | |
2beb67b4 | 119 | [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]] |
462db4aa MG |
120 | }, |
121 | ||
594d53ba MG |
122 | sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, |
123 | ||
1f64ef28 MG |
124 | sub (/us/) { response us => 'Users', {us => user_list} }, |
125 | sub (/ct/ + ?:owner~) { response ct => 'Contests', contest_list(%_), 300 }, | |
23e32638 | 126 | sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) { |
b8a0fa71 | 127 | forbid $_{private}; |
1f64ef28 | 128 | response log => 'Job list', job_list(%_), 5 |
b8a0fa71 | 129 | }, |
87ffd88b | 130 | sub (/pb/ + ?:owner~&:contest~&:private~) { |
b8a0fa71 | 131 | forbid $_{private}; |
1f64ef28 | 132 | my $pending = $_{contest} && !contest_entry($_{contest})->{started}; |
a18eef28 | 133 | forbid $pending; |
1f64ef28 | 134 | response pb => 'Problems', problem_list %_ |
b8a0fa71 | 135 | }, |
594d53ba | 136 | |
c5343878 | 137 | sub (/us/:user) { |
1f64ef28 | 138 | my $user = user_entry $_{user}; |
c5343878 MG |
139 | response us_entry => $user->{name}, $user |
140 | }, | |
141 | sub (/ct/:contest) { | |
1f64ef28 | 142 | my $contest = contest_entry $_{contest}; |
c5343878 MG |
143 | response ct_entry => $contest->{name}, $contest, 60 |
144 | }, | |
28e89d6c | 145 | sub (/log/:job) { |
1f64ef28 | 146 | my $job = job_entry $_{job}; |
c5343878 MG |
147 | forbid $job->{private}; |
148 | response log_entry => "Job $_{job}", $job, 10 | |
28e89d6c | 149 | }, |
84ca7535 MG |
150 | sub (/pb/:problem + ?contest~) { |
151 | my (undef, undef, $contest) = @_; | |
152 | $_{contest} = $contest; | |
1f64ef28 MG |
153 | $contest = $contest && contest_entry $_{contest}; |
154 | return NOT_FOUND if $contest && !contest_has_problem $_{contest}, $_{problem}; | |
155 | my $problem = problem_entry $_{problem}, $_{contest}; | |
a18eef28 MG |
156 | forbid $problem->{private} && !$contest; |
157 | if ($contest) { | |
158 | return redirect "/pb/$_{problem}" if !$contest->{started} || $contest->{finished}; | |
5b76a57d MG |
159 | forbid !remote_user; |
160 | $privacy = 'private'; | |
161 | } | |
a18eef28 | 162 | response pb_entry => $problem->{name}, $problem, $_{contest} ? 10 : (); |
594d53ba | 163 | }, |
e4d5bdf5 | 164 | sub (/sol/:problem) { |
1f64ef28 | 165 | my $problem = problem_entry $_{problem}; |
a18eef28 MG |
166 | forbid $problem->{private}; |
167 | response sol => 'Solution of ' . $problem->{name}, {solution => $problem->{solution}}; | |
e4d5bdf5 | 168 | }, |
594d53ba MG |
169 | |
170 | sub (/) { redispatch_to '/index' }, | |
cb0122d7 | 171 | sub (/favicon.ico) { redirect '/static/favicon.ico' }, |
928a611f | 172 | sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] } |
594d53ba MG |
173 | }, |
174 | ||
175 | sub (POST) { | |
08794667 MG |
176 | sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) { |
177 | my (undef, undef, $prog) = @_; | |
594d53ba | 178 | forbid !remote_user; |
1f64ef28 | 179 | my $problem = problem_entry $_{problem}; |
a18eef28 MG |
180 | my $private = $problem->{private} ? 1 : 0; |
181 | if ($_{contest}) { | |
182 | $private = 0; | |
1f64ef28 | 183 | my $contest = contest_entry $_{contest}; |
a18eef28 MG |
184 | return reply 'This contest has not yet started' if !$contest->{started}; |
185 | return reply 'This contest has finished' if $contest->{finished}; | |
186 | return reply 'This problem is private' if !admin && $private; | |
1f64ef28 | 187 | return reply 'This problem does not belong to this contest' unless contest_has_problem $_{contest}, $_{problem}; |
a18eef28 | 188 | } |
08794667 | 189 | return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024; |
c5343878 | 190 | return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30; |
594d53ba | 191 | |
08794667 MG |
192 | my $source = $prog ? read_file $prog->path : $_{source_code}; |
193 | unlink $prog->path if $prog; | |
1f64ef28 | 194 | my $id = create_job( |
594d53ba | 195 | maybe contest => $_{contest}, |
cb6adaff | 196 | private => $private, |
594d53ba MG |
197 | date => time, |
198 | extension => FORMAT_EXTENSION->{$_{prog_format}}, | |
199 | format => $_{prog_format}, | |
200 | problem => $_{problem}, | |
08794667 | 201 | source => $source, |
c5343878 MG |
202 | owner => remote_user->{id}, |
203 | ); | |
594d53ba | 204 | |
c5343878 | 205 | [303, [Location => '/log/' . $id], []] |
3c434a02 | 206 | }, |
594d53ba | 207 | } |
7dc32473 MG |
208 | } |
209 | ||
594d53ba | 210 | |
7dc32473 MG |
211 | 1; |
212 | __END__ |