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