]>
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 | ||
3c434a02 MG |
17 | use Email::Sender::Simple qw/sendmail/; |
18 | use Email::Simple; | |
19 | ||
3b69df7a MG |
20 | use warnings NONFATAL => 'all'; |
21 | no warnings 'illegalproto'; | |
f34254b8 | 22 | |
594d53ba MG |
23 | ################################################## |
24 | ||
594d53ba MG |
25 | use constant USER_REGEX => qr/^\w{2,20}$/a; |
26 | ||
e03e380b MG |
27 | use constant CONTENT_TYPES => +{ |
28 | c => 'text/x-csrc', | |
29 | cpp => 'text/x-c++src', | |
30 | cs => 'text/x-csharp', # Used by GNOME. Not in mime.types. | |
a5e355d6 MG |
31 | go => 'text/plain', # ? |
32 | hs => 'text/x-haskell', | |
e03e380b MG |
33 | java => 'text/x-java', |
34 | pas => 'text/x-pascal', | |
35 | pl => 'text/x-perl', | |
36 | py => 'text/x-python', | |
c83dca87 | 37 | rb => 'application/x-ruby', |
76454bde | 38 | l => 'text/plain', |
e03e380b | 39 | }; |
594d53ba MG |
40 | |
41 | use constant FORMAT_EXTENSION => { | |
42 | C => 'c', | |
43 | CPP => 'cpp', | |
a5e355d6 MG |
44 | GCCGO => 'go', |
45 | GOLANG => 'go', | |
46 | HASKELL => 'hs', | |
594d53ba MG |
47 | MONO => 'cs', |
48 | JAVA => 'java', | |
49 | PASCAL => 'pas', | |
50 | PERL => 'pl', | |
51 | PYTHON => 'py', | |
c83dca87 | 52 | RUBY => 'rb', |
e6d1bcd4 | 53 | SBCL => 'l', |
594d53ba MG |
54 | }; |
55 | ||
3ef32174 | 56 | use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']]; |
594d53ba | 57 | |
c039d63e | 58 | my ($env, $privacy); |
594d53ba MG |
59 | |
60 | sub db { $env->{'gruntmaster.dbic'} } | |
61 | ||
62 | sub remote_user { | |
8d725691 | 63 | my $user = $env->{REMOTE_USER}; |
594d53ba MG |
64 | $user &&= db->user($user); |
65 | $user | |
66 | } | |
67 | ||
3b69df7a | 68 | sub admin { remote_user && remote_user->admin } |
594d53ba MG |
69 | sub contest { db->contest ($_{contest}) } |
70 | sub problem { db->problem ($_{problem}) } | |
71 | sub job { db->job ($_{job}) } | |
72 | sub user { db->user ($_{user}) } | |
73 | ||
54bc2c5c | 74 | sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] } |
69c01de9 | 75 | sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] } |
594d53ba | 76 | sub response { |
c039d63e | 77 | my ($template, $title, $params, $maxage) = @_; |
594d53ba MG |
78 | unless ($params) { |
79 | $params = $title; | |
80 | $title = 'No title'; | |
81 | } | |
9a4806b3 | 82 | $params->{time} = time; |
39e2d01a | 83 | $params->{args} = {%_}; |
54bc2c5c | 84 | bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response' |
594d53ba | 85 | } |
3b69df7a | 86 | |
c039d63e MG |
87 | sub forbid { |
88 | my ($condition) = @_; | |
89 | $privacy = 'private' if $condition; | |
90 | return if !$condition || admin; | |
8d725691 | 91 | unwind $env->{authcomplex}->unauthorized, SUB UP |
594d53ba | 92 | } |
31d70015 | 93 | |
594d53ba MG |
94 | sub dispatch_request{ |
95 | $env = $_[PSGI_ENV]; | |
c039d63e MG |
96 | $privacy = 'public'; |
97 | ||
594d53ba | 98 | sub (GET) { |
3ef32174 | 99 | sub (/robots.txt) { NOT_FOUND }, |
3ef32174 | 100 | |
594d53ba MG |
101 | sub (/src/:job) { |
102 | return NOT_FOUND if !job; | |
c22928ed | 103 | my $isowner = remote_user && remote_user->id eq job->rawowner; |
e547b147 MG |
104 | my $private = job->private || job->problem->private || job->contest && job->contest->is_running; |
105 | forbid !$isowner && $private; | |
106 | my $privacy = $private ? 'private' : 'public'; | |
1bc7c028 | 107 | my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->extension}); |
e547b147 | 108 | push @headers, (Vary => 'Authorization') if $private; |
594d53ba MG |
109 | [200, \@headers, [job->source]] |
110 | }, | |
111 | ||
d3892d73 | 112 | sub (?:format~) { |
feaa8f5a | 113 | my $format = lc ($_{format} // ''); |
594d53ba MG |
114 | response_filter { |
115 | my ($r) = @_; | |
116 | return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; | |
c039d63e | 117 | my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}"); |
be6551aa | 118 | push @hdrs, Vary => 'Authorization' if $privacy eq 'private'; |
69c01de9 | 119 | return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; |
3b69df7a | 120 | my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; |
69c01de9 | 121 | [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]] |
594d53ba MG |
122 | }, |
123 | }, | |
124 | ||
594d53ba MG |
125 | sub (/st/:contest) { |
126 | response st => 'Standings', { | |
7468a6a7 | 127 | st => [ contest->standings ], |
ebca729d MG |
128 | problems => [ |
129 | map { [$_->id, $_->name] } | |
130 | sort { $a->value <=> $b->value } | |
131 | map { $_->problem } contest->contest_problems], | |
c039d63e | 132 | }, 10 |
594d53ba MG |
133 | }, |
134 | ||
645cfb7d | 135 | sub (/ed/:contest) { |
90f613d3 | 136 | forbid !contest->is_finished; |
42243f04 | 137 | my $pblist = db->problem_list(contest => $_{contest}, solution => 1); |
54bc2c5c | 138 | response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : (); |
645cfb7d MG |
139 | }, |
140 | ||
462db4aa MG |
141 | sub (/login) { |
142 | forbid !remote_user; | |
2beb67b4 | 143 | [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]] |
462db4aa MG |
144 | }, |
145 | ||
594d53ba MG |
146 | sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, |
147 | ||
148 | sub (/us/) { response us => 'Users', {us => db->user_list} }, | |
54bc2c5c | 149 | sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 }, |
23e32638 | 150 | sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) { |
b8a0fa71 | 151 | forbid $_{private}; |
7e158d3c | 152 | response log => 'Job list', db->job_list(%_), 5 |
b8a0fa71 | 153 | }, |
87ffd88b | 154 | sub (/pb/ + ?:owner~&:contest~&:private~) { |
b8a0fa71 | 155 | forbid $_{private}; |
2ca1b4a6 | 156 | forbid contest && contest->is_pending; |
ee38c972 | 157 | response pb => 'Problems', db->problem_list(%_) |
b8a0fa71 | 158 | }, |
594d53ba MG |
159 | |
160 | sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) }, | |
54bc2c5c | 161 | sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 }, |
28e89d6c MG |
162 | sub (/log/:job) { |
163 | forbid job->private; | |
54bc2c5c | 164 | response log_entry => "Job $_{job}", db->job_entry($_{job}), 10 |
28e89d6c | 165 | }, |
84ca7535 MG |
166 | sub (/pb/:problem + ?contest~) { |
167 | my (undef, undef, $contest) = @_; | |
168 | $_{contest} = $contest; | |
8a4acaca MG |
169 | return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem}); |
170 | forbid problem->private && !contest; | |
171 | if (contest) { | |
172 | return redirect "/pb/$_{problem}" unless contest->is_running; | |
5b76a57d MG |
173 | forbid !remote_user; |
174 | $privacy = 'private'; | |
175 | } | |
7e158d3c | 176 | response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : (); |
594d53ba | 177 | }, |
e4d5bdf5 | 178 | sub (/sol/:problem) { |
8a4acaca | 179 | forbid problem->private; |
e4d5bdf5 MG |
180 | response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution}; |
181 | }, | |
594d53ba MG |
182 | |
183 | sub (/) { redispatch_to '/index' }, | |
cb0122d7 | 184 | sub (/favicon.ico) { redirect '/static/favicon.ico' }, |
54bc2c5c | 185 | 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 |
186 | }, |
187 | ||
188 | sub (POST) { | |
08794667 MG |
189 | sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) { |
190 | my (undef, undef, $prog) = @_; | |
594d53ba | 191 | forbid !remote_user; |
8a4acaca | 192 | my $private = (problem->private && !contest) ? 1 : 0; |
3b69df7a | 193 | return reply 'This contest has finished' if contest && contest->is_finished; |
8a4acaca MG |
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}); | |
08794667 | 197 | return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024; |
594d53ba MG |
198 | return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30; |
199 | remote_user->update({lastjob => time}); | |
200 | ||
08794667 MG |
201 | my $source = $prog ? read_file $prog->path : $_{source_code}; |
202 | unlink $prog->path if $prog; | |
31dc8096 | 203 | my $newjob = db->jobs->create({ |
594d53ba | 204 | maybe contest => $_{contest}, |
cb6adaff | 205 | private => $private, |
594d53ba MG |
206 | date => time, |
207 | extension => FORMAT_EXTENSION->{$_{prog_format}}, | |
208 | format => $_{prog_format}, | |
209 | problem => $_{problem}, | |
08794667 | 210 | source => $source, |
594d53ba MG |
211 | owner => remote_user->id, |
212 | }); | |
213 | ||
31dc8096 | 214 | [303, [Location => '/log/' . $newjob->id], []] |
3c434a02 | 215 | }, |
594d53ba | 216 | } |
7dc32473 MG |
217 | } |
218 | ||
594d53ba | 219 | |
7dc32473 MG |
220 | 1; |
221 | __END__ |